forcedotcom / go-soql

Golang tag library to generate SOQL queries
BSD 3-Clause "New" or "Revised" License
52 stars 12 forks source link

Add support for avg/sum functions #22

Open atulkc opened 4 years ago

atulkc commented 4 years ago

As part of supporting aggregate functions add support for following functions:

AVG()
SUM()

Add avg and sum tag that can be used in selectClause structs as follows:

type TestSoqlStruct struct {
    SelectClause NestedStruct      `soql:"selectClause,tableName= Opportunity"`
    GroupBy        []string                 `soql:"groupByClause"`
}

type NestedStruct struct {
        CampaignId  string `soql:"selectColumn,fieldName= CampaignId"`
    Average         int `soql:"avg,fieldName= Amount"`
}

soqlStruct := TestSoqlStruct{
   GroupBy: []string{"CampaignId"},
}
soqlQuery, err := Marshal(soqlStruct)
if err != nil {
    fmt.Printf("Error in marshaling: %s\n", err.Error())
}
fmt.Println(soqlQuery)

This should result in SOQL as follows

SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId

Note that if fieldName is not specified then return ErrInvalidTag.

Same should apply for sum tag as well.

Allow avg and sum to be used only for supported primitive data types