Source File
expr.go
Belonging Package
github.com/Masterminds/squirrel
package squirrel
import (
)
func ( string, ...interface{}) Sqlizer {
return expr{sql: , args: }
}
func ( expr) () ( string, []interface{}, error) {
:= true
for , := range .args {
if , := .(Sqlizer); {
= false
}
}
if {
return .sql, .args, nil
}
:= &bytes.Buffer{}
:= .args
:= .sql
var string
var []interface{}
for == nil && len() > 0 && len() > 0 {
:= strings.Index(, "?")
break
}
.WriteString([:+2])
= [+2:]
continue
}
, , = .ToSql()
.WriteString([:])
.WriteString()
= append(, ...)
.WriteString([:+1])
= append(, [0])
}
= [1:]
= [+1:]
}
.WriteString()
return .String(), append(, ...),
}
type concatExpr []interface{}
func ( concatExpr) () ( string, []interface{}, error) {
for , := range {
switch p := .(type) {
case string:
+=
case Sqlizer:
, , := .ToSql()
if != nil {
return "", nil,
}
+=
= append(, ...)
default:
return "", nil, fmt.Errorf("%#v is not a string or Sqlizer", )
}
}
return
}
func ( ...interface{}) concatExpr {
return concatExpr()
}
= sqlTrue
return
}
var (
[]string
= "="
= "IN"
= "IS"
= sqlFalse
)
if {
= "<>"
= "NOT IN"
= "IS NOT"
= sqlTrue
}
:= getSortedKeys()
for , := range {
var string
:= []
switch v := .(type) {
case driver.Valuer:
if , = .Value(); != nil {
return
}
}
:= reflect.ValueOf()
if .Kind() == reflect.Ptr {
if .IsNil() {
= nil
} else {
= .Elem().Interface()
}
}
if == nil {
= fmt.Sprintf("%s %s NULL", , )
} else {
if isListType() {
:= reflect.ValueOf()
if .Len() == 0 {
=
if == nil {
= []interface{}{}
}
} else {
for := 0; < .Len(); ++ {
= append(, .Index().Interface())
}
= fmt.Sprintf("%s %s (%s)", , , Placeholders(.Len()))
}
} else {
= fmt.Sprintf("%s %s ?", , )
= append(, )
}
}
= append(, )
}
= strings.Join(, " AND ")
return
}
func ( Eq) () ( string, []interface{}, error) {
return .toSQL(false)
}
type Like map[string]interface{}
func ( Like) ( string) ( string, []interface{}, error) {
var []string
for , := range {
:= ""
switch v := .(type) {
case driver.Valuer:
if , = .Value(); != nil {
return
}
}
if == nil {
= fmt.Errorf("cannot use null with like operators")
return
} else {
if isListType() {
= fmt.Errorf("cannot use array or slice with like operators")
return
} else {
= fmt.Sprintf("%s %s ?", , )
= append(, )
}
}
= append(, )
}
= strings.Join(, " AND ")
return
}
func ( Like) () ( string, []interface{}, error) {
return .toSql("LIKE")
}
type Lt map[string]interface{}
func ( Lt) (, bool) ( string, []interface{}, error) {
var (
[]string
= "<"
)
if {
= ">"
}
if {
= fmt.Sprintf("%s%s", , "=")
}
:= getSortedKeys()
for , := range {
var string
:= []
switch v := .(type) {
case driver.Valuer:
if , = .Value(); != nil {
return
}
}
if == nil {
= fmt.Errorf("cannot use null with less than or greater than operators")
return
}
if isListType() {
= fmt.Errorf("cannot use array or slice with less than or greater than operators")
return
}
= fmt.Sprintf("%s %s ?", , )
= append(, )
= append(, )
}
= strings.Join(, " AND ")
return
}
func ( Lt) () ( string, []interface{}, error) {
return .toSql(false, false)
}
type GtOrEq Lt
func ( GtOrEq) () ( string, []interface{}, error) {
return Lt().toSql(true, true)
}
type conj []Sqlizer
func ( conj) (, string) ( string, []interface{}, error) {
if len() == 0 {
return , []interface{}{}, nil
}
var []string
for , := range {
, , := .ToSql()
if != nil {
return "", nil,
}
if != "" {
= append(, )
= append(, ...)
}
}
if len() > 0 {
= fmt.Sprintf("(%s)", strings.Join(, ))
}
return
}
type Or conj
func ( Or) () (string, []interface{}, error) {
return conj().join(" OR ", sqlFalse)
}
func ( map[string]interface{}) []string {
:= make([]string, 0, len())
for := range {
= append(, )
}
sort.Strings()
return
}
func ( interface{}) bool {
if driver.IsValue() {
return false
}
:= reflect.ValueOf()
return .Kind() == reflect.Array || .Kind() == reflect.Slice
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |