good morning!!!!

Skip to content
Snippets Groups Projects
Commit c929b5aa authored by Carlos Nieto's avatar Carlos Nieto
Browse files

Using []interface{} instead of []string for SQL arguments.

parent 16148c32
No related branches found
No related tags found
No related merge requests found
......@@ -66,9 +66,9 @@ func (self *Table) Find(terms ...interface{}) db.Result {
}
// Transforms conditions into arguments for sql.Exec/sql.Query
func (self *Table) compileConditions(term interface{}) (string, []string) {
func (self *Table) compileConditions(term interface{}) (string, []interface{}) {
sql := []string{}
args := []string{}
args := []interface{}{}
switch t := term.(type) {
case []interface{}:
......@@ -111,10 +111,10 @@ func (self *Table) compileConditions(term interface{}) (string, []string) {
return "", args
}
func (self *Table) compileStatement(where db.Cond) (string, []string) {
func (self *Table) compileStatement(where db.Cond) (string, []interface{}) {
str := make([]string, len(where))
arg := make([]string, len(where))
arg := make([]interface{}, len(where))
i := 0
......@@ -138,7 +138,7 @@ func (self *Table) compileStatement(where db.Cond) (string, []string) {
case 1:
return str[0], arg
case 0:
return "", []string{}
return "", []interface{}{}
}
return `(` + strings.Join(str, ` AND `) + `)`, arg
......@@ -208,7 +208,7 @@ func toInternalInterface(val interface{}) interface{} {
}
// Converts a Go value into internal database representation.
func toInternal(val interface{}) string {
func toInternal(val interface{}) interface{} {
switch t := val.(type) {
case []byte:
......
......@@ -52,7 +52,7 @@ func init() {
db.Register(driverName, &Source{})
}
type sqlValues_t []string
type sqlValues_t []interface{}
type Source struct {
config db.Settings
......@@ -73,12 +73,6 @@ func sqlCompile(terms []interface{}) *sqlQuery {
for _, term := range terms {
switch t := term.(type) {
case string:
q.Query = append(q.Query, t)
case []string:
for _, arg := range t {
q.Args = append(q.Args, arg)
}
case sqlValues_t:
args := make([]string, len(t))
for i, arg := range t {
......@@ -86,6 +80,17 @@ func sqlCompile(terms []interface{}) *sqlQuery {
q.Args = append(q.Args, arg)
}
q.Query = append(q.Query, `(`+strings.Join(args, `, `)+`)`)
case string:
q.Query = append(q.Query, t)
default:
if reflect.TypeOf(t).Kind() == reflect.Slice {
var v = reflect.ValueOf(t)
for i := 0; i < v.Len(); i++ {
q.Args = append(q.Args, v.Index(i).Interface())
}
} else {
q.Args = append(q.Args, t)
}
}
}
......@@ -99,7 +104,7 @@ func sqlFields(names []string) string {
return `("` + strings.Join(names, `", "`) + `")`
}
func sqlValues(values []string) sqlValues_t {
func sqlValues(values []interface{}) sqlValues_t {
ret := make(sqlValues_t, len(values))
for i, _ := range values {
ret[i] = values[i]
......
......@@ -195,7 +195,7 @@ func (self *Result) Update(values interface{}) error {
total := len(ff)
updateFields := make([]string, total)
updateArgs := make([]string, total)
updateArgs := make([]interface{}, total)
for i := 0; i < total; i++ {
updateFields[i] = fmt.Sprintf(`%s = ?`, ff[i])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment