2017-01-03 09:20:28 +01:00
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-16 05:07:00 +01:00
|
|
|
"reflect"
|
2016-11-03 23:16:01 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type condIn struct {
|
|
|
|
col string
|
|
|
|
vals []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Cond = condIn{}
|
|
|
|
|
2017-01-03 09:20:28 +01:00
|
|
|
// In generates IN condition
|
2016-11-03 23:16:01 +01:00
|
|
|
func In(col string, values ...interface{}) Cond {
|
|
|
|
return condIn{col, values}
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:50:33 +02:00
|
|
|
func (condIn condIn) handleBlank(w Writer) error {
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN ()", condIn.col); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
func (condIn condIn) WriteTo(w Writer) error {
|
|
|
|
if len(condIn.vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch condIn.vals[0].(type) {
|
|
|
|
case []int8:
|
|
|
|
vals := condIn.vals[0].([]int8)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
vals := condIn.vals[0].([]int16)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
vals := condIn.vals[0].([]int)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
vals := condIn.vals[0].([]int32)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []int64:
|
|
|
|
vals := condIn.vals[0].([]int64)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []uint8:
|
|
|
|
vals := condIn.vals[0].([]uint8)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
vals := condIn.vals[0].([]uint16)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []uint:
|
|
|
|
vals := condIn.vals[0].([]uint)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
vals := condIn.vals[0].([]uint32)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
vals := condIn.vals[0].([]uint64)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []string:
|
|
|
|
vals := condIn.vals[0].([]string)
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Append(val)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
vals := condIn.vals[0].([]interface{})
|
|
|
|
if len(vals) <= 0 {
|
2017-05-02 02:50:33 +02:00
|
|
|
return condIn.handleBlank(w)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
questionMark := strings.Repeat("?,", len(vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Append(vals...)
|
|
|
|
case expr:
|
|
|
|
val := condIn.vals[0].(expr)
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := val.WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *Builder:
|
|
|
|
bd := condIn.vals[0].(*Builder)
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := bd.WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2017-02-16 05:07:00 +01:00
|
|
|
v := reflect.ValueOf(condIn.vals[0])
|
|
|
|
if v.Kind() == reflect.Slice {
|
|
|
|
l := v.Len()
|
2017-05-02 02:50:33 +02:00
|
|
|
if l == 0 {
|
|
|
|
return condIn.handleBlank(w)
|
|
|
|
}
|
2017-02-16 05:07:00 +01:00
|
|
|
|
|
|
|
questionMark := strings.Repeat("?,", l)
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
w.Append(v.Index(i).Interface())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
questionMark := strings.Repeat("?,", len(condIn.vals))
|
|
|
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Append(condIn.vals...)
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (condIn condIn) And(conds ...Cond) Cond {
|
|
|
|
return And(condIn, And(conds...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (condIn condIn) Or(conds ...Cond) Cond {
|
|
|
|
return Or(condIn, Or(conds...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (condIn condIn) IsValid() bool {
|
|
|
|
return len(condIn.col) > 0 && len(condIn.vals) > 0
|
|
|
|
}
|