Created
July 11, 2018 06:42
-
-
Save JesseYan/27efd8050ca4ecd7dfe2f65f49b5ffb6 to your computer and use it in GitHub Desktop.
interface value type convert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"database/sql" | |
"reflect" | |
"strings" | |
"github.com/Guazi-inc/squirrel" | |
"github.com/caojia/go-orm" | |
"golang.guazi-corp.com/bi/bi_data_models/bierrors" | |
"golang.guazi-corp.com/bi/bi_data_models/consts" | |
"golang.guazi-corp.com/bi/bi_data_models/utils/log" | |
"golang.guazi-corp.com/finance/go-common/cache" | |
"golang.guazi-corp.com/finance/go-common/db" | |
biProto "proto/bi/common" | |
) | |
// BaseModel 所有model的继承 | |
type BaseModel struct { | |
*db.Model | |
Cache cache.Cache | |
} | |
// Model 对外调用入口 | |
var Model = &BaseModel{} | |
// getTableName 获取table名 | |
func getTableName(s interface{}) string { | |
tableNameMethod := reflect.ValueOf(s).MethodByName("TableName") | |
if tableNameMethod.IsValid() { | |
tableName := tableNameMethod.Call([]reflect.Value{}) | |
if len(tableName) == 1 && tableName[0].Kind() == reflect.String { | |
return tableName[0].String() | |
} | |
} | |
panic("param `s` is not a model") | |
} | |
// Insert 插入数据对象 | |
func (m BaseModel) Insert(s interface{}) error { | |
if s == nil { | |
log.Logger.Error(bierrors.ErrParamNil) | |
panic(bierrors.ErrParamNil) | |
} | |
conn := db.GetConn(biProto.ConfigKey_guazi_bi_db_new) | |
f := func(t *orm.ORMTran) error { | |
return t.Insert(s) | |
} | |
return conn.Master().DoTransaction(f) | |
} | |
// Update 更新 | |
func (m BaseModel) Update(s interface{}) error { | |
if s == nil { | |
log.Logger.Error(bierrors.ErrParamNil) | |
panic(bierrors.ErrParamNil) | |
} | |
conn := db.GetConn(biProto.ConfigKey_guazi_bi_db_new) | |
f := func(t *orm.ORMTran) error { | |
return t.UpdateByPK(s) | |
} | |
return conn.Master().DoTransaction(f) | |
} | |
// Delete 假删除 | |
func (m BaseModel) Delete(s interface{}, ID int) error { | |
if s == nil { | |
log.Logger.Error(bierrors.ErrParamNil) | |
panic(bierrors.ErrParamNil) | |
} | |
conn := db.GetConn(biProto.ConfigKey_guazi_bi_db_new) | |
table := getTableName(s) | |
//args := make([]reflect.Value, 0) | |
//table := reflect.ValueOf(s).MethodByName("TableName").Call(args)[0].String() | |
updateSQL, params := squirrel.Update(table). | |
Set("is_deleted", consts.DBDeletedValue). | |
Eq("id", ID).ToSql() | |
log.Logger.Info("Delete-SQL=", updateSQL) | |
f := func(t *orm.ORMTran) error { | |
_, err := t.Exec(updateSQL, params...) | |
return err | |
} | |
return conn.DoTransaction(f) | |
} | |
// SelectByID 由ID查询对象 | |
func (m BaseModel) SelectByID(s interface{}, ID int) error { | |
if s == nil { | |
log.Logger.Error(bierrors.ErrParamNil) | |
panic(bierrors.ErrParamNil) | |
} | |
querySQL, params := squirrel. | |
Select("*").From(getTableName(s)). | |
Eq("id", ID).ToSql() | |
log.Logger.Info("SelectByID-SQL=", querySQL) | |
conn := db.GetConn(biProto.ConfigKey_guazi_bi_db_new) | |
err := conn.Slave().SelectOne(s, querySQL, params...) | |
if err == sql.ErrNoRows { | |
log.Logger.Warn("ErrNoRows for select ID:", ID) | |
return bierrors.ErrNoRows | |
} else if err != nil { | |
log.Logger.Error("SelectOne ID:", ID, " error:%", err) | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment