Closed atulkc closed 4 years ago
The above design would not support the case of mixing ASC and DESC order, e.g. SELECT col1, col2, col3 FROM table1 ORDER BY col1 ASC, col2 DESC, col3 ASC
, since we are keeping the order by columns as two separate slices.
I suggest creating a struct that contains the name of the column and its ordering, and allow users to provide a slice of this struct to construct the ORDER BY
clause:
type Order struct {
Field string
IsDesc bool
}
And the TestSoqlStruct would look like:
type TestSoqlStruct struct {
SelectClause NonNestedStruct `soql:"selectClause,tableName=SM_SomeObject__c"`
WhereClause TestQueryCriteria `soql:"whereClause"`
OrderByClause []Order `soql:"orderByClause"`
}
To generate the ORDER BY
clause ORDER BY Name__c ASC, SomeValue___c DESC, SomeValue2__c ASC
, we would create the following slice and set it to TestSoqlStruct.OrderByClause
:
ordering := []Order{
Order{"Name", false},
Order{"SomeValue", true},
Order{"SomeValue2", false},
}
@kchoy-sfdc good catch about limitation of proposed solution. Thanks for suggesting a new solution. I agree with your approach. Lets use your suggestion in building this feature.
Library should support
ORDER BY
clause. User should be able to annotate[]string
member of struct withorderBy
tag and indicate if theorder
isASC
orDESC
. The values in the[]string
should be the names of the field of the struct that is tagged asselectClause
. This will enable developers to take[]string
as parameter to their method where the users of their library can specify the name of the fields of the struct that is being returned to them on which they desire ordering. Below given is example of how this should work:To use above structs to create SOQL query
Above struct will result in following SOQL query: