38 lines
983 B
Go
38 lines
983 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func BuildWhereQuery(params interface{}) string {
|
|
conditions := []string{}
|
|
v := reflect.ValueOf(params)
|
|
t := v.Type()
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
field := v.Field(i)
|
|
if field.Kind() == reflect.Ptr && !field.IsNil() {
|
|
dbTag := t.Field(i).Tag.Get("db")
|
|
if dbTag != "" {
|
|
switch field.Elem().Kind() {
|
|
case reflect.String:
|
|
conditions = append(conditions, fmt.Sprintf("%s = '%s'", dbTag, field.Elem().String()))
|
|
case reflect.Bool:
|
|
conditions = append(conditions, fmt.Sprintf("%s = %t", dbTag, field.Elem().Bool()))
|
|
case reflect.Int:
|
|
conditions = append(conditions, fmt.Sprintf("%s = %d", dbTag, field.Elem().Int()))
|
|
case reflect.Int64:
|
|
conditions = append(conditions, fmt.Sprintf("%s = %d", dbTag, field.Elem().Int()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(conditions) > 0 {
|
|
return "WHERE " + strings.Join(conditions, " AND ")
|
|
}
|
|
return ""
|
|
}
|