MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / ResolveFieldCondition

Function ResolveFieldCondition

cmd/cql-proxy/resolver/resolver.go:132–207  ·  view source on GitHub ↗

ResolveFieldCondition resolves field condition object and returns as where condition statement.

(field string, v interface{})

Source from the content-addressed store, hash-verified

130
131// ResolveFieldCondition resolves field condition object and returns as where condition statement.
132func ResolveFieldCondition(field string, v interface{}) (statement string, args []interface{}, err error) {
133 if rv, ok := v.(map[string]interface{}); ok {
134 var subStatements []string
135
136 for k, v := range rv {
137 switch k {
138 case "$eq", "$gt", "$gte", "$lt", "$lte", "$ne":
139 if !isLiteral(v) {
140 err = errors.Errorf("argument of operator %s is not a literal", k)
141 return
142 }
143
144 subStatements = append(subStatements, fmt.Sprintf(`"%s" %s ?`, field, opMap[k]))
145 args = append(args, v)
146 case "$in", "$nin":
147 var lv []interface{}
148
149 if lv, ok = v.([]interface{}); !ok {
150 err = errors.New("$in/$nin operator needs array")
151 return
152 }
153
154 for _, lve := range lv {
155 if !isLiteral(lve) {
156 err = errors.Errorf("can not nest non-literal under %s operator", k)
157 return
158 }
159 }
160
161 if len(lv) == 0 {
162 if k == "$in" {
163 subStatements = append(subStatements, "1=0")
164 } else {
165 subStatements = append(subStatements, "1=1")
166 }
167 } else {
168 subStatements = append(subStatements, fmt.Sprintf(`"%s" %s (%s?)`,
169 field, opMap[k], strings.Repeat("?,", len(lv)-1)))
170 args = append(args, lv...)
171 }
172 case "$not":
173 if rv, ok := v.(map[string]interface{}); !ok || len(rv) == 0 {
174 err = errors.New("$not operator needs non-empty object")
175 return
176 }
177
178 var (
179 notSubStatement string
180 notSubArgs []interface{}
181 )
182 notSubStatement, notSubArgs, err = ResolveFieldCondition(field, v)
183 if err != nil {
184 return
185 }
186
187 subStatements = append(subStatements, "NOT ("+notSubStatement+")")
188 args = append(args, notSubArgs...)
189 case "$comment":

Callers 1

ResolveFilterFunction · 0.85

Calls 3

isLiteralFunction · 0.85
ErrorfMethod · 0.80
NewMethod · 0.65

Tested by

no test coverage detected