forcedotcom / forcedotcom/go-soql

Add support for count function in SOQL

Open
#20 1 comment 0 reactions 0 assignees View on GitHub
aggregate-functions help wanted
Dominant language
Go
Stars
53
Forks
12
PR merge metrics
No merged PRs in 30d

Description

As part of supporting aggregate functions add support for following functions:
```
COUNT()
COUNT_DISTINCT()
```
Add `count` and `countDistinct` tag that can be used in `selectClause` structs as follows:
```
type TestSoqlStruct struct {
SelectClause NestedStruct `soql:"selectClause,tableName=Account"`
WhereClause TestQueryCriteria `soql:"whereClause"`
}
type TestQueryCriteria struct {
IncludeNamePattern []string `soql:"likeOperator,fieldName=Name"`
}

type NestedStruct struct {
Count int `soql:"count"`
}

soqlStruct := TestSoqlStruct{
WhereClause: TestQueryCriteria {
IncludeNamePattern: []string{"a"},
},
}
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 COUNT() FROM Account WHERE Name LIKE '%a%'
```
Note that if `fieldName` is not specified then just `COUNT()` should be used. If `fieldName` is specified then that should be used.
```
type TestSoqlStruct struct {
SelectClause NestedStruct `soql:"selectClause,tableName=Account"`
WhereClause TestQueryCriteria `soql:"whereClause"`
}
type TestQueryCriteria struct {
IncludeNamePattern []string `soql:"likeOperator,fieldName=Name"`
}

type NestedStruct struct {
Count int `soql:"count,fieldName=Id"`
}
```
```
SELECT COUNT(Id) FROM Account WHERE Name LIKE '%a%'
```
Same should apply for `countDistinct` tag as well.

Note that if `count` or `countDistinct` is used without `fieldName` then there cannot be any other field in select clause as described [here](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_count.htm). If `groupBy` clause is used only then allow selectClause struct to have other fields. In case of error return `ErrInvalidCountTag` error.

Allow `count` and `countDistinct` to be used only for supported primitive [data types](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions_field_types.htm)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.