MCPcopy
hub / github.com/jmoiron/sqlx / bindArray

Function bindArray

named.go:273–301  ·  view source on GitHub ↗

bindArray binds a named parameter query with fields from an array or slice of structs argument.

(bindType int, query string, arg interface{}, m *reflectx.Mapper)

Source from the content-addressed store, hash-verified

271// bindArray binds a named parameter query with fields from an array or slice of
272// structs argument.
273func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
274 // do the initial binding with QUESTION; if bindType is not question,
275 // we can rebind it at the end.
276 bound, names, err := compileNamedQuery([]byte(query), QUESTION)
277 if err != nil {
278 return "", []interface{}{}, err
279 }
280 arrayValue := reflect.ValueOf(arg)
281 arrayLen := arrayValue.Len()
282 if arrayLen == 0 {
283 return "", []interface{}{}, fmt.Errorf("length of array is 0: %#v", arg)
284 }
285 var arglist = make([]interface{}, 0, len(names)*arrayLen)
286 for i := 0; i < arrayLen; i++ {
287 elemArglist, err := bindAnyArgs(names, arrayValue.Index(i).Interface(), m)
288 if err != nil {
289 return "", []interface{}{}, err
290 }
291 arglist = append(arglist, elemArglist...)
292 }
293 if arrayLen > 1 {
294 bound = fixBound(bound, arrayLen)
295 }
296 // adjust binding type if we weren't on question
297 if bindType != QUESTION {
298 bound = Rebind(bindType, bound)
299 }
300 return bound, arglist, nil
301}
302
303// bindMap binds a named parameter query with a map of arguments.
304func bindMap(bindType int, query string, args map[string]interface{}) (string, []interface{}, error) {

Callers 1

bindNamedMapperFunction · 0.85

Calls 5

compileNamedQueryFunction · 0.85
bindAnyArgsFunction · 0.85
fixBoundFunction · 0.85
RebindFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected