123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package model
- import (
- "fmt"
- "strings"
- )
- type NullType byte
- const (
- _ NullType = iota
- // IsNull the same as `is null`
- IsNull
- // IsNotNull the same as `is not null`
- IsNotNull
- )
- // sql build where
- func whereBuild(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
- for k, v := range where {
- ks := strings.Split(k, " ")
- src := k
- if whereSQL != "" {
- whereSQL += " AND "
- }
- strings.Join(ks, ",")
- switch len(ks) {
- case 1:
- switch v := v.(type) {
- case NullType:
- if v == IsNotNull {
- whereSQL += fmt.Sprint(k, " IS NOT NULL")
- } else {
- whereSQL += fmt.Sprint(k, " IS NULL")
- }
- default:
- whereSQL += fmt.Sprint(k, "=?")
- vals = append(vals, v)
- }
- break
- case 2:
- k = ks[0]
- switch ks[1] {
- case "=":
- whereSQL += fmt.Sprint(k, " = ?")
- vals = append(vals, v)
- break
- case ">":
- whereSQL += fmt.Sprint(k, " > ?")
- vals = append(vals, v)
- break
- case ">=":
- whereSQL += fmt.Sprint(k, " >= ?")
- vals = append(vals, v)
- break
- case "<":
- whereSQL += fmt.Sprint(k, " < ?")
- vals = append(vals, v)
- break
- case "<=":
- whereSQL += fmt.Sprint(k, " <= ?")
- vals = append(vals, v)
- break
- case "!=":
- whereSQL += fmt.Sprint(k, " != ?")
- vals = append(vals, v)
- break
- case "<>":
- whereSQL += fmt.Sprint(k, " != ?")
- vals = append(vals, v)
- break
- case "in":
- whereSQL += fmt.Sprint(k, " in (?)")
- vals = append(vals, v)
- break
- case "like":
- whereSQL += fmt.Sprint(k, " like ?")
- vals = append(vals, v)
- }
- default:
- whereSQL += fmt.Sprint(src)
- if fmt.Sprintf("%v", v) != "" {
- vals = append(vals, v)
- }
- break
- }
- }
- return
- }
- func whereBuildOr(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
- for k, v := range where {
- ks := strings.Split(k, " ")
- src := k
- if whereSQL != "" {
- whereSQL += " Or "
- }
- strings.Join(ks, ",")
- switch len(ks) {
- case 1:
- switch v := v.(type) {
- case NullType:
- if v == IsNotNull {
- whereSQL += fmt.Sprint(k, " IS NOT NULL")
- } else {
- whereSQL += fmt.Sprint(k, " IS NULL")
- }
- default:
- whereSQL += fmt.Sprint(k, "=?")
- vals = append(vals, v)
- }
- break
- case 2:
- k = ks[0]
- switch ks[1] {
- case "=":
- whereSQL += fmt.Sprint(k, " = ?")
- vals = append(vals, v)
- break
- case ">":
- whereSQL += fmt.Sprint(k, " > ?")
- vals = append(vals, v)
- break
- case ">=":
- whereSQL += fmt.Sprint(k, " >= ?")
- vals = append(vals, v)
- break
- case "<":
- whereSQL += fmt.Sprint(k, " < ?")
- vals = append(vals, v)
- break
- case "<=":
- whereSQL += fmt.Sprint(k, " <= ?")
- vals = append(vals, v)
- break
- case "!=":
- whereSQL += fmt.Sprint(k, " != ?")
- vals = append(vals, v)
- break
- case "<>":
- whereSQL += fmt.Sprint(k, " != ?")
- vals = append(vals, v)
- break
- case "in":
- whereSQL += fmt.Sprint(k, " in (?)")
- vals = append(vals, v)
- break
- case "like":
- whereSQL += fmt.Sprint(k, " like ?")
- vals = append(vals, v)
- }
- default:
- whereSQL += fmt.Sprint(src)
- if fmt.Sprintf("%v", v) != "" {
- vals = append(vals, v)
- }
- break
- }
- }
- return
- }
- func whereBuildAndOr(where map[string]interface{}, or map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
- if len(where) > 0 {
- andSql, andVars, err := whereBuild(where)
- if err != nil {
- return "", nil, err
- }
- whereSQL = andSql
- vals = andVars
- }
- if len(or) > 0 {
- orSql, orVars, err := whereBuildOr(or)
- if err != nil {
- return "", nil, err
- }
- if whereSQL != "" {
- whereSQL = fmt.Sprintf("%s and (%s)", whereSQL, orSql)
- } else {
- whereSQL = fmt.Sprintf("%s", orSql)
- }
- vals = append(vals, orVars...)
- }
- return
- }
|