diff options
author | Niall Sheridan <nsheridan@gmail.com> | 2017-04-10 21:18:42 +0100 |
---|---|---|
committer | Niall Sheridan <nsheridan@gmail.com> | 2017-04-10 21:38:33 +0100 |
commit | 30802e07b2d84fbc213b490d3402707dffe60096 (patch) | |
tree | 934aecb8f3582325dfd1aa6652193adac87d00db /vendor/github.com/jmoiron/sqlx/bind.go | |
parent | da7638dc112c4c106e8929601b642d2ca4596cba (diff) |
update dependencies
Diffstat (limited to 'vendor/github.com/jmoiron/sqlx/bind.go')
-rw-r--r-- | vendor/github.com/jmoiron/sqlx/bind.go | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/vendor/github.com/jmoiron/sqlx/bind.go b/vendor/github.com/jmoiron/sqlx/bind.go index 53659bc..10f7bdf 100644 --- a/vendor/github.com/jmoiron/sqlx/bind.go +++ b/vendor/github.com/jmoiron/sqlx/bind.go @@ -43,27 +43,28 @@ func Rebind(bindType int, query string) string { return query } - qb := []byte(query) // Add space enough for 10 params before we have to allocate - rqb := make([]byte, 0, len(qb)+10) - j := 1 - for _, b := range qb { - if b == '?' { - switch bindType { - case DOLLAR: - rqb = append(rqb, '$') - case NAMED: - rqb = append(rqb, ':', 'a', 'r', 'g') - } - for _, b := range strconv.Itoa(j) { - rqb = append(rqb, byte(b)) - } - j++ - } else { - rqb = append(rqb, b) + rqb := make([]byte, 0, len(query)+10) + + var i, j int + + for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") { + rqb = append(rqb, query[:i]...) + + switch bindType { + case DOLLAR: + rqb = append(rqb, '$') + case NAMED: + rqb = append(rqb, ':', 'a', 'r', 'g') } + + j++ + rqb = strconv.AppendInt(rqb, int64(j), 10) + + query = query[i+1:] } - return string(rqb) + + return string(append(rqb, query...)) } // Experimental implementation of Rebind which uses a bytes.Buffer. The code is @@ -135,9 +136,9 @@ func In(query string, args ...interface{}) (string, []interface{}, error) { } newArgs := make([]interface{}, 0, flatArgsCount) + buf := bytes.NewBuffer(make([]byte, 0, len(query)+len(", ?")*flatArgsCount)) var arg, offset int - var buf bytes.Buffer for i := strings.IndexByte(query[offset:], '?'); i != -1; i = strings.IndexByte(query[offset:], '?') { if arg >= len(meta) { @@ -163,13 +164,12 @@ func In(query string, args ...interface{}) (string, []interface{}, error) { // write everything up to and including our ? character buf.WriteString(query[:offset+i+1]) - newArgs = append(newArgs, argMeta.v.Index(0).Interface()) - for si := 1; si < argMeta.length; si++ { buf.WriteString(", ?") - newArgs = append(newArgs, argMeta.v.Index(si).Interface()) } + newArgs = appendReflectSlice(newArgs, argMeta.v, argMeta.length) + // slice the query and reset the offset. this avoids some bookkeeping for // the write after the loop query = query[offset+i+1:] @@ -184,3 +184,24 @@ func In(query string, args ...interface{}) (string, []interface{}, error) { return buf.String(), newArgs, nil } + +func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} { + switch val := v.Interface().(type) { + case []interface{}: + args = append(args, val...) + case []int: + for i := range val { + args = append(args, val[i]) + } + case []string: + for i := range val { + args = append(args, val[i]) + } + default: + for si := 0; si < vlen; si++ { + args = append(args, v.Index(si).Interface()) + } + } + + return args +} |