MCPcopy Index your code
hub / github.com/upper/db / preprocess

Method preprocess

internal/sqlbuilder/comparison.go:67–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

65}
66
67func (ow *operatorWrapper) preprocess() (string, []interface{}) {
68 placeholder := "?"
69
70 column, err := ow.cv.Column.Compile(ow.tu.Template)
71 if err != nil {
72 panic(fmt.Sprintf("could not compile column: %v", err.Error()))
73 }
74
75 c := ow.cmp()
76
77 op := ow.tu.comparisonOperatorMapper(c.Operator())
78
79 var args []interface{}
80
81 switch c.Operator() {
82 case adapter.ComparisonOperatorNone:
83 panic("no operator given")
84 case adapter.ComparisonOperatorCustom:
85 op = c.CustomOperator()
86 case adapter.ComparisonOperatorIn, adapter.ComparisonOperatorNotIn:
87 values := c.Value().([]interface{})
88 if len(values) < 1 {
89 placeholder, args = "(NULL)", []interface{}{}
90 break
91 }
92 placeholder, args = "(?"+strings.Repeat(", ?", len(values)-1)+")", values
93 case adapter.ComparisonOperatorIs, adapter.ComparisonOperatorIsNot:
94 switch c.Value() {
95 case nil:
96 placeholder, args = "NULL", []interface{}{}
97 case false:
98 placeholder, args = "FALSE", []interface{}{}
99 case true:
100 placeholder, args = "TRUE", []interface{}{}
101 }
102 case adapter.ComparisonOperatorBetween, adapter.ComparisonOperatorNotBetween:
103 values := c.Value().([]interface{})
104 placeholder, args = "? AND ?", []interface{}{values[0], values[1]}
105 case adapter.ComparisonOperatorEqual:
106 v := c.Value()
107 if b, ok := v.([]byte); ok {
108 v = string(b)
109 }
110 args = []interface{}{v}
111 }
112
113 if args == nil {
114 args = []interface{}{c.Value()}
115 }
116
117 if strings.Contains(op, ":column") {
118 return strings.Replace(op, ":column", column, -1), args
119 }
120
121 return column + " " + op + " " + placeholder, args
122}

Callers 1

toColumnValuesMethod · 0.95

Calls 7

cmpMethod · 0.95
CustomOperatorMethod · 0.80
CompileMethod · 0.65
ErrorMethod · 0.65
OperatorMethod · 0.65
ValueMethod · 0.65

Tested by

no test coverage detected