2016-05-17 12:00:56 +02:00
|
|
|
package boil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2016-06-08 07:45:34 +02:00
|
|
|
"github.com/nullbio/sqlboiler/strmangle"
|
2016-05-17 12:00:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bind executes the query and inserts the
|
|
|
|
// result into the passed in object pointer
|
|
|
|
func (q *Query) Bind(obj interface{}) error {
|
2016-08-06 23:42:22 +02:00
|
|
|
return nil
|
|
|
|
/*typ := reflect.TypeOf(obj)
|
2016-06-08 07:45:34 +02:00
|
|
|
kind := typ.Kind()
|
|
|
|
|
|
|
|
if kind != reflect.Ptr {
|
|
|
|
return fmt.Errorf("Bind not given a pointer to a slice or struct: %s", typ.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
typ = typ.Elem()
|
|
|
|
kind = typ.Kind()
|
|
|
|
|
|
|
|
if kind == reflect.Struct {
|
|
|
|
row := ExecQueryOne(q)
|
|
|
|
err := BindOne(row, q.selectCols, obj)
|
|
|
|
if err != nil {
|
2016-07-19 06:07:12 +02:00
|
|
|
return fmt.Errorf("Failed to execute Bind query for %s: %s", q.from, err)
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|
|
|
|
} else if kind == reflect.Slice {
|
|
|
|
rows, err := ExecQueryAll(q)
|
|
|
|
if err != nil {
|
2016-07-19 06:07:12 +02:00
|
|
|
return fmt.Errorf("Failed to execute Bind query for %s: %s", q.from, err)
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|
|
|
|
err = BindAll(rows, q.selectCols, obj)
|
|
|
|
if err != nil {
|
2016-07-19 06:07:12 +02:00
|
|
|
return fmt.Errorf("Failed to Bind results to object provided for %s: %s", q.from, err)
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Bind given a pointer to a non-slice or non-struct: %s", typ.String())
|
|
|
|
}
|
|
|
|
|
2016-08-06 23:42:22 +02:00
|
|
|
return nil*/
|
2016-05-17 12:00:56 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// BindP executes the query and inserts the
|
2016-07-16 13:22:57 +02:00
|
|
|
// result into the passed in object pointer.
|
|
|
|
// It panics on error.
|
|
|
|
func (q *Query) BindP(obj interface{}) {
|
|
|
|
if err := q.Bind(obj); err != nil {
|
|
|
|
panic(WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:00:56 +02:00
|
|
|
// BindOne inserts the returned row columns into the
|
|
|
|
// passed in object pointer
|
2016-06-05 08:13:38 +02:00
|
|
|
func BindOne(row *sql.Row, selectCols []string, obj interface{}) error {
|
|
|
|
kind := reflect.ValueOf(obj).Kind()
|
|
|
|
if kind != reflect.Ptr {
|
|
|
|
return fmt.Errorf("BindOne given a non-pointer type")
|
|
|
|
}
|
|
|
|
|
|
|
|
pointers := GetStructPointers(obj, selectCols...)
|
|
|
|
if err := row.Scan(pointers...); err != nil {
|
|
|
|
return fmt.Errorf("Unable to scan into pointers: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:00:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindAll inserts the returned rows columns into the
|
|
|
|
// passed in slice of object pointers
|
2016-06-05 08:13:38 +02:00
|
|
|
func BindAll(rows *sql.Rows, selectCols []string, obj interface{}) error {
|
2016-06-07 06:38:17 +02:00
|
|
|
ptrSlice := reflect.ValueOf(obj)
|
|
|
|
typ := ptrSlice.Type()
|
|
|
|
ptrSlice = ptrSlice.Elem()
|
|
|
|
kind := typ.Kind()
|
2016-06-05 08:13:38 +02:00
|
|
|
|
2016-06-07 06:38:17 +02:00
|
|
|
var structTyp reflect.Type
|
|
|
|
|
|
|
|
for i, exp := range []reflect.Kind{reflect.Ptr, reflect.Slice, reflect.Ptr, reflect.Struct} {
|
|
|
|
if i != 0 {
|
|
|
|
typ = typ.Elem()
|
|
|
|
kind = typ.Kind()
|
|
|
|
}
|
2016-06-05 08:13:38 +02:00
|
|
|
|
2016-06-07 06:38:17 +02:00
|
|
|
if kind != exp {
|
|
|
|
return fmt.Errorf("[%d] BindAll object type should be *[]*Type but was: %s", i, ptrSlice.Type().String())
|
|
|
|
}
|
2016-06-05 08:13:38 +02:00
|
|
|
|
2016-06-07 06:38:17 +02:00
|
|
|
if kind == reflect.Struct {
|
|
|
|
structTyp = typ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 08:13:38 +02:00
|
|
|
for rows.Next() {
|
2016-06-10 04:09:48 +02:00
|
|
|
newStruct := reflect.New(structTyp)
|
2016-06-07 06:38:17 +02:00
|
|
|
pointers := GetStructPointers(newStruct.Interface(), selectCols...)
|
2016-06-05 08:13:38 +02:00
|
|
|
if err := rows.Scan(pointers...); err != nil {
|
|
|
|
return fmt.Errorf("Unable to scan into pointers: %s", err)
|
|
|
|
}
|
2016-06-07 06:38:17 +02:00
|
|
|
|
|
|
|
ptrSlice.Set(reflect.Append(ptrSlice, newStruct))
|
2016-06-05 08:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 12:00:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStructValues returns the values (as interface) of the matching columns in obj
|
|
|
|
func GetStructValues(obj interface{}, columns ...string) []interface{} {
|
|
|
|
ret := make([]interface{}, len(columns))
|
|
|
|
val := reflect.Indirect(reflect.ValueOf(obj))
|
|
|
|
|
|
|
|
for i, c := range columns {
|
|
|
|
field := val.FieldByName(strmangle.TitleCase(c))
|
|
|
|
ret[i] = field.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStructPointers returns a slice of pointers to the matching columns in obj
|
|
|
|
func GetStructPointers(obj interface{}, columns ...string) []interface{} {
|
|
|
|
val := reflect.ValueOf(obj).Elem()
|
2016-08-06 23:37:55 +02:00
|
|
|
|
|
|
|
var ln int
|
|
|
|
var getField func(reflect.Value, int) reflect.Value
|
2016-06-07 06:38:17 +02:00
|
|
|
|
|
|
|
if len(columns) == 0 {
|
2016-08-06 23:37:55 +02:00
|
|
|
ln = val.NumField()
|
|
|
|
getField = func(v reflect.Value, i int) reflect.Value {
|
|
|
|
return v.Field(i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ln = len(columns)
|
|
|
|
getField = func(v reflect.Value, i int) reflect.Value {
|
|
|
|
return v.FieldByName(strmangle.TitleCase(columns[i]))
|
2016-06-07 06:38:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 23:37:55 +02:00
|
|
|
ret := make([]interface{}, ln)
|
|
|
|
for i := 0; i < ln; i++ {
|
|
|
|
field := getField(val, i)
|
2016-05-17 12:00:56 +02:00
|
|
|
|
|
|
|
if !field.IsValid() {
|
2016-08-06 23:37:55 +02:00
|
|
|
// Although this breaks the abstraction of getField above - we know that v.Field(i) can't actually
|
|
|
|
// produce an Invalid value, so we make a hopefully safe assumption here.
|
|
|
|
panic(fmt.Sprintf("Could not find field on struct %T for field %s", obj, strmangle.TitleCase(columns[i])))
|
2016-05-17 12:00:56 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 23:37:55 +02:00
|
|
|
ret[i] = field.Addr().Interface()
|
2016-05-17 12:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|