Finished finishers
This commit is contained in:
parent
8f3ff66339
commit
019ab3b502
5 changed files with 45 additions and 204 deletions
84
boil/bind.go
84
boil/bind.go
|
@ -1,84 +0,0 @@
|
||||||
package boil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/pobri19/sqlboiler/strmangle"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (q *Query) Bind(obj interface{}) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkType(obj interface{}) (reflect.Type, bool, error) {
|
|
||||||
val := reflect.ValueOf(obj)
|
|
||||||
typ := val.Type()
|
|
||||||
kind := val.Kind()
|
|
||||||
|
|
||||||
if kind != reflect.Ptr {
|
|
||||||
return nil, false, fmt.Errorf("Bind must be given pointers to structs but got type: %s, kind: %s", typ.String(), kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
typ = typ.Elem()
|
|
||||||
kind = typ.Kind()
|
|
||||||
isSlice := false
|
|
||||||
|
|
||||||
switch kind {
|
|
||||||
case reflect.Slice:
|
|
||||||
typ = typ.Elem()
|
|
||||||
kind = typ.Kind()
|
|
||||||
isSlice = true
|
|
||||||
case reflect.Struct:
|
|
||||||
return typ, isSlice, nil
|
|
||||||
default:
|
|
||||||
return nil, false, fmt.Errorf("Bind was given an invalid object must be []*T or *T but got type: %s, kind: %s", typ.String(), kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
if kind != reflect.Ptr {
|
|
||||||
return nil, false, fmt.Errorf("Bind must be given pointers to structs but got type: %s, kind: %s", typ.String(), kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
typ = typ.Elem()
|
|
||||||
kind = typ.Kind()
|
|
||||||
|
|
||||||
if kind != reflect.Struct {
|
|
||||||
return nil, false, fmt.Errorf("Bind must be a struct but got type: %s, kind: %s", typ.String(), kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
return typ, isSlice, 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.ValueOf(obj)
|
|
||||||
if val.Kind() == reflect.Ptr {
|
|
||||||
val = val.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
ret := make([]interface{}, len(columns))
|
|
||||||
|
|
||||||
for i, c := range columns {
|
|
||||||
field := val.FieldByName(strmangle.TitleCase(c))
|
|
||||||
if !field.IsValid() {
|
|
||||||
panic(fmt.Sprintf("Could not find field on struct %T for field %s", obj, strmangle.TitleCase(c)))
|
|
||||||
}
|
|
||||||
|
|
||||||
field = field.Addr()
|
|
||||||
ret[i] = field.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
|
@ -1,112 +0,0 @@
|
||||||
package boil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/guregu/null"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetStructValues(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
timeThing := time.Now()
|
|
||||||
o := struct {
|
|
||||||
TitleThing string
|
|
||||||
Name string
|
|
||||||
ID int
|
|
||||||
Stuff int
|
|
||||||
Things int
|
|
||||||
Time time.Time
|
|
||||||
NullBool null.Bool
|
|
||||||
}{
|
|
||||||
TitleThing: "patrick",
|
|
||||||
Stuff: 10,
|
|
||||||
Things: 0,
|
|
||||||
Time: timeThing,
|
|
||||||
NullBool: null.NewBool(true, false),
|
|
||||||
}
|
|
||||||
|
|
||||||
vals := GetStructValues(&o, "title_thing", "name", "id", "stuff", "things", "time", "null_bool")
|
|
||||||
if vals[0].(string) != "patrick" {
|
|
||||||
t.Errorf("Want test, got %s", vals[0])
|
|
||||||
}
|
|
||||||
if vals[1].(string) != "" {
|
|
||||||
t.Errorf("Want empty string, got %s", vals[1])
|
|
||||||
}
|
|
||||||
if vals[2].(int) != 0 {
|
|
||||||
t.Errorf("Want 0, got %d", vals[2])
|
|
||||||
}
|
|
||||||
if vals[3].(int) != 10 {
|
|
||||||
t.Errorf("Want 10, got %d", vals[3])
|
|
||||||
}
|
|
||||||
if vals[4].(int) != 0 {
|
|
||||||
t.Errorf("Want 0, got %d", vals[4])
|
|
||||||
}
|
|
||||||
if !vals[5].(time.Time).Equal(timeThing) {
|
|
||||||
t.Errorf("Want %s, got %s", o.Time, vals[5])
|
|
||||||
}
|
|
||||||
if !vals[6].(null.Bool).IsZero() {
|
|
||||||
t.Errorf("Want %v, got %v", o.NullBool, vals[6])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetStructPointers(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
o := struct {
|
|
||||||
Title string
|
|
||||||
ID *int
|
|
||||||
}{
|
|
||||||
Title: "patrick",
|
|
||||||
}
|
|
||||||
|
|
||||||
ptrs := GetStructPointers(&o, "title", "id")
|
|
||||||
*ptrs[0].(*string) = "test"
|
|
||||||
if o.Title != "test" {
|
|
||||||
t.Errorf("Expected test, got %s", o.Title)
|
|
||||||
}
|
|
||||||
x := 5
|
|
||||||
*ptrs[1].(**int) = &x
|
|
||||||
if *o.ID != 5 {
|
|
||||||
t.Errorf("Expected 5, got %d", *o.ID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCheckType(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
type Thing struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
validTest := []struct {
|
|
||||||
Input interface{}
|
|
||||||
IsSlice bool
|
|
||||||
TypeName string
|
|
||||||
}{
|
|
||||||
{&[]*Thing{}, true, "boil.Thing"},
|
|
||||||
{[]Thing{}, false, ""},
|
|
||||||
{&[]Thing{}, false, ""},
|
|
||||||
{Thing{}, false, ""},
|
|
||||||
{new(int), false, ""},
|
|
||||||
{5, false, ""},
|
|
||||||
{&Thing{}, false, "boil.Thing"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, test := range validTest {
|
|
||||||
typ, isSlice, err := checkType(test.Input)
|
|
||||||
if err != nil {
|
|
||||||
if len(test.TypeName) > 0 {
|
|
||||||
t.Errorf("%d) Type: %T %#v - should have succeded but got err: %v", i, test.Input, test.Input, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if isSlice != test.IsSlice {
|
|
||||||
t.Errorf("%d) Type: %T %#v - succeded but wrong isSlice value: %t, want %t", i, test.Input, test.Input, isSlice, test.IsSlice)
|
|
||||||
}
|
|
||||||
|
|
||||||
if got := typ.String(); got != test.TypeName {
|
|
||||||
t.Errorf("%d) Type: %T %#v - succeded but wrong type name: %s, want: %s", i, test.Input, test.Input, got, test.TypeName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -22,6 +22,7 @@ type Query struct {
|
||||||
delete bool
|
delete bool
|
||||||
update map[string]interface{}
|
update map[string]interface{}
|
||||||
selectCols []string
|
selectCols []string
|
||||||
|
count bool
|
||||||
table string
|
table string
|
||||||
innerJoins []join
|
innerJoins []join
|
||||||
outerJoins []join
|
outerJoins []join
|
||||||
|
@ -97,6 +98,10 @@ func ExecQueryAll(q *Query) (*sql.Rows, error) {
|
||||||
return q.executor.Query(qs, args)
|
return q.executor.Query(qs, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetCount(q *Query) {
|
||||||
|
q.count = true
|
||||||
|
}
|
||||||
|
|
||||||
func SetDelete(q *Query) {
|
func SetDelete(q *Query) {
|
||||||
q.delete = true
|
q.delete = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,44 @@
|
||||||
type {{$varNameSingular}}Slice []*{{$tableNameSingular}}
|
type {{$varNameSingular}}Slice []*{{$tableNameSingular}}
|
||||||
|
|
||||||
func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
||||||
//var o *{{$tableNameSingular}}
|
var o *{{$tableNameSingular}}
|
||||||
|
|
||||||
//qs.Apply(q, qs.Limit(1))
|
boil.SetLimit(q.Query, 1)
|
||||||
|
|
||||||
//res := boil.ExecQueryOne(q)
|
res := boil.ExecQueryOne(q.Query)
|
||||||
|
err := boil.BindOne(res, o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("{{.PkgName}}: failed to execute a one query for {{.Table.Name}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
|
func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
|
||||||
return nil, nil
|
var o []*{{$tableNameSingular}}
|
||||||
|
|
||||||
|
res, err := boil.ExecQueryAll(q.Query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("{{.PkgName}}: failed to execute an all query for {{.Table.Name}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = boil.BindAll(res, o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("{{.PkgName}}: failed to assign all query results to {{$tableNameSingular}} slice: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q {{$varNameSingular}}Query) Count() (int64, error) {
|
func (q {{$varNameSingular}}Query) Count() (int64, error) {
|
||||||
return 0, nil
|
var count int64
|
||||||
|
|
||||||
|
boil.SetCount(q.Query)
|
||||||
|
|
||||||
|
err := boil.ExecQueryOne(q.Query).Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("{{.PkgName}}: failed to count {{.Table.Name}} rows: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,14 @@ func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{primaryKeyFuncS
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v {{$varNameSingular}}Query) UpdateAll(cols M) error {
|
func (q {{$varNameSingular}}Query) UpdateAll(cols M) error {
|
||||||
return nil
|
boil.SetUpdate(q.Query, cols)
|
||||||
|
|
||||||
|
_, err := boil.ExecQuery(q.Query)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("{{.PkgName}}: unable to update all for {{.Table.Name}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue