123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
|
package sqladapter
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
|
||
|
"git.hexq.cn/tiglog/mydb"
|
||
|
"git.hexq.cn/tiglog/mydb/internal/sqlbuilder"
|
||
|
)
|
||
|
|
||
|
func recordID(store mydb.Store, record mydb.Record) (mydb.Cond, error) {
|
||
|
if record == nil {
|
||
|
return nil, mydb.ErrNilRecord
|
||
|
}
|
||
|
|
||
|
if hasConstraints, ok := record.(mydb.HasConstraints); ok {
|
||
|
return hasConstraints.Constraints(), nil
|
||
|
}
|
||
|
|
||
|
id := mydb.Cond{}
|
||
|
|
||
|
keys, fields, err := recordPrimaryKeyFieldValues(store, record)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for i := range fields {
|
||
|
if fields[i] == reflect.Zero(reflect.TypeOf(fields[i])).Interface() {
|
||
|
return nil, mydb.ErrRecordIDIsZero
|
||
|
}
|
||
|
id[keys[i]] = fields[i]
|
||
|
}
|
||
|
if len(id) < 1 {
|
||
|
return nil, mydb.ErrRecordIDIsZero
|
||
|
}
|
||
|
|
||
|
return id, nil
|
||
|
}
|
||
|
|
||
|
func recordPrimaryKeyFieldValues(store mydb.Store, record mydb.Record) ([]string, []interface{}, error) {
|
||
|
sess := store.Session()
|
||
|
|
||
|
pKeys, err := sess.(Session).PrimaryKeys(store.Name())
|
||
|
if err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
|
||
|
fields := sqlbuilder.Mapper.FieldsByName(reflect.ValueOf(record), pKeys)
|
||
|
|
||
|
values := make([]interface{}, 0, len(fields))
|
||
|
for i := range fields {
|
||
|
if fields[i].IsValid() {
|
||
|
values = append(values, fields[i].Interface())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return pKeys, values, nil
|
||
|
}
|
||
|
|
||
|
func recordCreate(store mydb.Store, record mydb.Record) error {
|
||
|
sess := store.Session()
|
||
|
|
||
|
if validator, ok := record.(mydb.Validator); ok {
|
||
|
if err := validator.Validate(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if hook, ok := record.(mydb.BeforeCreateHook); ok {
|
||
|
if err := hook.BeforeCreate(sess); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if creator, ok := store.(mydb.StoreCreator); ok {
|
||
|
if err := creator.Create(record); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
if err := store.InsertReturning(record); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if hook, ok := record.(mydb.AfterCreateHook); ok {
|
||
|
if err := hook.AfterCreate(sess); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func recordUpdate(store mydb.Store, record mydb.Record) error {
|
||
|
sess := store.Session()
|
||
|
|
||
|
if validator, ok := record.(mydb.Validator); ok {
|
||
|
if err := validator.Validate(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if hook, ok := record.(mydb.BeforeUpdateHook); ok {
|
||
|
if err := hook.BeforeUpdate(sess); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if updater, ok := store.(mydb.StoreUpdater); ok {
|
||
|
if err := updater.Update(record); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
if err := record.Store(sess).UpdateReturning(record); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if hook, ok := record.(mydb.AfterUpdateHook); ok {
|
||
|
if err := hook.AfterUpdate(sess); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|