MCPcopy
hub / github.com/go-gorm/gorm / BuildCondition

Method BuildCondition

statement.go:292–490  ·  view source on GitHub ↗

BuildCondition build condition

(query interface{}, args ...interface{})

Source from the content-addressed store, hash-verified

290
291// BuildCondition build condition
292func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []clause.Expression {
293 if s, ok := query.(string); ok {
294 // if it is a number, then treats it as primary key
295 if _, err := strconv.Atoi(s); err != nil {
296 if s == "" && len(args) == 0 {
297 return nil
298 }
299
300 if len(args) == 0 || (len(args) > 0 && strings.Contains(s, "?")) {
301 // looks like a where condition
302 return []clause.Expression{clause.Expr{SQL: s, Vars: args}}
303 }
304
305 if len(args) > 0 && strings.Contains(s, "@") {
306 // looks like a named query
307 return []clause.Expression{clause.NamedExpr{SQL: s, Vars: args}}
308 }
309
310 if strings.Contains(strings.TrimSpace(s), " ") {
311 // looks like a where condition
312 return []clause.Expression{clause.Expr{SQL: s, Vars: args}}
313 }
314
315 if len(args) == 1 {
316 return []clause.Expression{clause.Eq{Column: s, Value: args[0]}}
317 }
318 }
319 }
320
321 conds := make([]clause.Expression, 0, 4)
322 args = append([]interface{}{query}, args...)
323 for idx, arg := range args {
324 if arg == nil {
325 continue
326 }
327 if valuer, ok := arg.(driver.Valuer); ok {
328 arg, _ = valuer.Value()
329 }
330
331 curTable := stmt.Table
332 if curTable == "" {
333 curTable = clause.CurrentTable
334 }
335
336 switch v := arg.(type) {
337 case clause.Expression:
338 conds = append(conds, v)
339 case []clause.Expression:
340 conds = append(conds, v...)
341 case *DB:
342 v.executeScopes()
343
344 if cs, ok := v.Statement.Clauses["WHERE"]; ok {
345 if where, ok := cs.Expression.(clause.Where); ok {
346 if len(where.Exprs) == 1 {
347 if orConds, ok := where.Exprs[0].(clause.OrConditions); ok {
348 if len(orConds.Exprs) == 1 {
349 where.Exprs[0] = clause.AndConditions(orConds)

Callers 14

ClausesMethod · 0.80
WhereMethod · 0.80
NotMethod · 0.80
OrMethod · 0.80
HavingMethod · 0.80
TestWhereCloneCorruptionFunction · 0.80
TestNilConditionFunction · 0.80
FirstMethod · 0.80
TakeMethod · 0.80
LastMethod · 0.80
FindMethod · 0.80

Calls 8

AndConditionsStruct · 0.92
AndFunction · 0.92
ParseFunction · 0.92
ContainsMethod · 0.80
executeScopesMethod · 0.80
LenMethod · 0.80
ValueMethod · 0.65
AddErrorMethod · 0.65

Tested by 2

TestWhereCloneCorruptionFunction · 0.64
TestNilConditionFunction · 0.64