BuildCondition build condition
(query interface{}, args ...interface{})
| 290 | |
| 291 | // BuildCondition build condition |
| 292 | func (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) |