43 lines
917 B
Go
43 lines
917 B
Go
package orm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
func (q *Query) Create(val any) (any, error) {
|
|
_, err := q.model.insert(reflect.ValueOf(val), q, make(map[string]any))
|
|
return val, err
|
|
}
|
|
|
|
func (q *Query) Find() (any, error) {
|
|
sql, args := q.buildSQL()
|
|
fmt.Printf("[FIND] %s { %+v }\n", sql, args)
|
|
if !q.engine.dryRun {
|
|
rows, err := q.engine.conn.Query(q.ctx, sql, args...)
|
|
defer rows.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rmaps, err := rowsToMaps(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
wtype := q.model.Type
|
|
for wtype.Kind() == reflect.Pointer {
|
|
wtype = wtype.Elem()
|
|
}
|
|
return fillSlice(rmaps, wtype, q.engine.modelMap), nil
|
|
}
|
|
return make([]any, 0), nil
|
|
}
|
|
|
|
func (q *Query) Update(val any, cond any, args ...any) error {
|
|
if q.model != nil {
|
|
return q.model.update(reflect.ValueOf(val), q, make(map[string]any))
|
|
} else {
|
|
return errors.New("Please select a model")
|
|
}
|
|
}
|