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

Method toColumnValues

internal/sqlbuilder/template.go:157–276  ·  view source on GitHub ↗
(term interface{})

Source from the content-addressed store, hash-verified

155}
156
157func (tu *templateWithUtils) toColumnValues(term interface{}) (cv exql.ColumnValues, args []interface{}) {
158 args = []interface{}{}
159
160 switch t := term.(type) {
161 case adapter.Constraint:
162 columnValue := exql.ColumnValue{}
163
164 // TODO: Key and Value are similar. Can we refactor this? Maybe think about
165 // Left/Right rather than Key/Value.
166
167 switch key := t.Key().(type) {
168 case string:
169 chunks := strings.SplitN(strings.TrimSpace(key), " ", 2)
170 columnValue.Column = exql.ColumnWithName(chunks[0])
171 if len(chunks) > 1 {
172 columnValue.Operator = chunks[1]
173 }
174 case *adapter.RawExpr:
175 columnValue.Column = &exql.Raw{Value: key.Raw()}
176 args = append(args, key.Arguments()...)
177 case *db.FuncExpr:
178 fnName, fnArgs := key.Name(), key.Arguments()
179 if len(fnArgs) == 0 {
180 fnName = fnName + "()"
181 } else {
182 fnName = fnName + "(?" + strings.Repeat(", ?", len(fnArgs)-1) + ")"
183 }
184 fnName, fnArgs = Preprocess(fnName, fnArgs)
185 columnValue.Column = &exql.Raw{Value: fnName}
186 args = append(args, fnArgs...)
187 default:
188 columnValue.Column = &exql.Raw{Value: fmt.Sprintf("%v", key)}
189 }
190
191 switch value := t.Value().(type) {
192 case *db.FuncExpr:
193 fnName, fnArgs := value.Name(), value.Arguments()
194 if len(fnArgs) == 0 {
195 // A function with no arguments.
196 fnName = fnName + "()"
197 } else {
198 // A function with one or more arguments.
199 fnName = fnName + "(?" + strings.Repeat(", ?", len(fnArgs)-1) + ")"
200 }
201 fnName, fnArgs = Preprocess(fnName, fnArgs)
202 columnValue.Value = &exql.Raw{Value: fnName}
203 args = append(args, fnArgs...)
204 case *db.RawExpr:
205 q, a := Preprocess(value.Raw(), value.Arguments())
206 columnValue.Value = &exql.Raw{Value: q}
207 args = append(args, a...)
208 case driver.Valuer:
209 columnValue.Value = sqlPlaceholder
210 args = append(args, value)
211 case *db.Comparison:
212 wrapper := &operatorWrapper{
213 tu: tu,
214 cv: &columnValue,

Callers 1

toWhereWithArgumentsMethod · 0.95

Calls 10

preprocessMethod · 0.95
ColumnWithNameFunction · 0.92
PreprocessFunction · 0.85
RawMethod · 0.80
KeyMethod · 0.65
ArgumentsMethod · 0.65
NameMethod · 0.65
ValueMethod · 0.65
ConstraintsMethod · 0.65

Tested by

no test coverage detected