forcedotcom / go-soql

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

Marshal SOQL queries with OR operators between different columns #13

Closed t-margheim closed 4 years ago

t-margheim commented 4 years ago

Marshal SOQL queries with OR operators between different columns. For example, I don't believe this query is currently marshal-able:

SELECT Name,Email,Phone
FROM Contact
WHERE Title = 'Purchasing Manager' OR Department = 'Accounting'

I've been playing with an implementation that I think might work and is in keeping with the current patterns: adding an optional clause tag called joiner for whereClause. If not set, the marshal function uses AND to join (to maintain current functionality). If it's set to OR (case insensitive), then it will separate the clauses with OR.

type contact struct {
    Name  string `soql:"selectColumn,fieldName=Name" json:"Name"`
    Email string `soql:"selectColumn,fieldName=Email" json:"Email"`
    Phone string `soql:"selectColumn,fieldName=Phone" json:"Phone"`
}

type orSOQLQuery struct {
    SelectClause contact                `soql:"selectClause,tableName=Contact"`
    WhereClause  positionOrDeptCriteria `soql:"whereClause,joiner=OR"`
}

type positionOrDeptCriteria struct {
    Title      string `soql:"equalsOperator,fieldName=Title"`
    Department string `soql:"equalsOperator,fieldName=Department"`
}

// this would generate the soql query
soql.Marshal(orSOQLQuery{
    WhereClause: positionOrDeptCriteria{
        Title:      "Purchasing Manager",
        Department: "Accounting",
    },
})
atulkc commented 4 years ago

Thanks @t-margheim for submitting the issue and corresponding PR. I think this is a great addition to this library.