Documentation ¶
Index ¶
- Constants
- Variables
- func DBTooler()
- func GetColParamSQL(model interface{}) (sql string)
- func GetColSQL(model interface{}) (sql string)
- func GetColSQLAlias(model interface{}, alias string) (sql string)
- func GetDataSQL(gt *GT) (sql string, args []interface{})
- func GetInsertSQL(table string, params cmap.CMap) (sql string, args []interface{})
- func GetMoreSearchSQL(gt *GT) (sqlNt, sql string, clientPage, everyPage int64, args []interface{})
- func GetMoreTableColumnSQL(model interface{}, tables ...string) (sql string)
- func GetParams(data interface{}) (params []interface{})
- func GetReflectTag(reflectType reflect.Type, buf *bytes.Buffer)
- func GetReflectTagAlias(ref reflect.Type, buf *bytes.Buffer, alias string)
- func GetReflectTagMore(ref reflect.Type, buf *bytes.Buffer, tables ...string)
- func GetSearchSQL(gt *GT) (sqlNt, sql string, clientPage, everyPage int64, args []interface{})
- func GetSelectSearchSQL(gt *GT) (sqlNt, sql string, clientPage, everyPage int64)
- func GetUpdateSQL(table string, params cmap.CMap) (sql string, args []interface{})
- type Config
- type Crud
- type DBCrud
- func (c *DBCrud) Begin() Crud
- func (c *DBCrud) Commit() Crud
- func (c *DBCrud) Create() Crud
- func (c *DBCrud) CreateForm(params cmap.CMap) error
- func (c *DBCrud) CreateMore() Crud
- func (c *DBCrud) CreateResID(params cmap.CMap) (str.ID, error)
- func (c *DBCrud) DB() *DBTool
- func (c *DBCrud) Delete(id interface{}) Crud
- func (c *DBCrud) Error() error
- func (c *DBCrud) Exec() Crud
- func (c *DBCrud) From(query string) Crud
- func (c *DBCrud) GetByData(params cmap.CMap) Crud
- func (c *DBCrud) GetByID(id interface{}) Crud
- func (c *DBCrud) GetBySearch(params cmap.CMap) Crud
- func (c *DBCrud) GetMoreBySearch(params cmap.CMap) Crud
- func (c *DBCrud) Group(query string) Crud
- func (c *DBCrud) Pager() result.Pager
- func (c *DBCrud) Params(params ...Param) Crud
- func (c *DBCrud) Rollback() Crud
- func (c *DBCrud) RowsAffected() int64
- func (c *DBCrud) Search(params cmap.CMap) Crud
- func (c *DBCrud) Select(q interface{}, args ...interface{}) Crud
- func (c *DBCrud) Single() Crud
- func (c *DBCrud) Update() Crud
- func (c *DBCrud) UpdateForm(params cmap.CMap) error
- type DBTool
- func (db *DBTool) CreateData(table string, data interface{})
- func (db *DBTool) CreateDataResID(table string, params cmap.CMap) (id str.ID, err error)
- func (db *DBTool) CreateFormData(table string, params cmap.CMap) error
- func (db *DBTool) CreateMoreData(table string, model interface{}, data interface{})
- func (db *DBTool) Delete(table string, id interface{})
- func (db *DBTool) ExecSQL(sql string, args ...interface{})
- func (db *DBTool) GetBasicTypesData(typ reflect.Type, data interface{}, sql string, args ...interface{})
- func (db *DBTool) GetData(gt *GT)
- func (db *DBTool) GetDataByID(gt *GT, id interface{})
- func (db *DBTool) GetDataByName(data interface{}, name, value string) (err error)
- func (db *DBTool) GetDataBySQL(data interface{}, sql string, args ...interface{})
- func (db *DBTool) GetDataBySQLSearch(data interface{}, sql, sqlNt string, clientPage, everyPage int64, ...) (pager result.Pager)
- func (db *DBTool) GetDataBySearch(gt *GT) (pager result.Pager)
- func (db *DBTool) GetDataBySelectSQLSearch(gt *GT) (pager result.Pager)
- func (db *DBTool) GetMoreDataBySearch(gt *GT) (pager result.Pager)
- func (db *DBTool) NewDB()
- func (db *DBTool) UpdateData(gt *GT)
- func (db *DBTool) UpdateFormData(table string, params cmap.CMap) (err error)
- func (db *DBTool) UpdateStructData(data interface{}) (err error)
- func (db *DBTool) ValidateSQL(sql string) (err error)
- type GT
- type Log
- type LogWriter
- type Param
- func Data(Data interface{}) Param
- func Inner(InnerTables ...string) Param
- func InnerTable(InnerTables []string) Param
- func KeyModel(KeyModel interface{}) Param
- func Left(LeftTable ...string) Param
- func LeftTable(LeftTable []string) Param
- func Model(Model interface{}) Param
- func SubSQL(SubSQL ...string) Param
- func SubWhereSQL(SubWhereSQL ...string) Param
- func Table(Table string) Param
- type Params
- type Yaml
Constants ¶
const ( Debug = "debug" // default level Info = "info" Warn = "warn" Error = "error" )
log level
const Version = "1.9.0+"
Variables ¶
var LogFormatter = func(values ...interface{}) (messages []interface{}) { if len(values) > 1 { var ( sql string formattedValues []string level = values[0] currentTime = "\033[33m[" + gorm.NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m" ) messages = []interface{}{currentTime} if len(values) == 2 { currentTime = currentTime[1:] messages = []interface{}{currentTime} } if level == "sql" { messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0)) for _, value := range values[4].([]interface{}) { indirectValue := reflect.Indirect(reflect.ValueOf(value)) if indirectValue.IsValid() { value = indirectValue.Interface() if t, ok := value.(time.Time); ok { if t.IsZero() { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00")) } else { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))) } } else if b, ok := value.([]byte); ok { if str := string(b); isPrintable(str) { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str)) } else { formattedValues = append(formattedValues, "'<binary>'") } } else if r, ok := value.(driver.Valuer); ok { if value, err := r.Value(); err == nil && value != nil { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } else { formattedValues = append(formattedValues, "NULL") } } else { switch value.(type) { case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) default: formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } } } else { formattedValues = append(formattedValues, "NULL") } } if numericPlaceHolderRegexp.MatchString(values[3].(string)) { sql = values[3].(string) for index, value := range formattedValues { placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1) sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1") } } else { formattedValuesLength := len(formattedValues) for index, value := range sqlRegexp.Split(values[3].(string), -1) { sql += value if index < formattedValuesLength { sql += formattedValues[index] } } } messages = append(messages, sql) messages = append(messages, fmt.Sprintf(" \n\033[36;31m[%v]\033[0m ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned ")) } else { messages = append(messages, "\033[31;1m") messages = append(messages, values[2:]...) messages = append(messages, "\033[0m") } } return }
Functions ¶
func GetColSQL ¶
func GetColSQL(model interface{}) (sql string)
根据model中表模型的json标签获取表字段 将select* 变为对应的字段名
func GetColSQLAlias ¶
根据model中表模型的json标签获取表字段 将select* 中'*'变为对应的字段名 增加别名,表连接问题
func GetInsertSQL ¶
传入数据库表名 插入语句拼接
func GetMoreSearchSQL ¶
More Table params: innerTables is inner join tables, must even number params: leftTables is left join tables return: select sql table1 as main table, include other tables_id(foreign key)
func GetMoreTableColumnSQL ¶
select * replace select more tables tables : table name / table alias name 主表放在tables中第一个, 紧接着为主表关联的外键表名(无顺序)
func GetParams ¶
func GetParams(data interface{}) (params []interface{})
get data value like GetColSQL
func GetReflectTagAlias ¶
层级递增解析tag, 别名
func GetReflectTagMore ¶
层级递增解析tag, more tables
func GetSearchSQL ¶
分页参数不传, 查询所有 默认根据id倒序 单张表
func GetSelectSearchSQL ¶
select sql
Types ¶
type Config ¶
type Config struct { // different devMode yaml data YamlS map[string]*Yaml // contains filtered or unexported fields }
config
type Crud ¶
type Crud interface { // DB DB() *DBTool // new/replace param // return param Params(param ...Param) Crud // get url params // like form data GetBySearch(params cmap.CMap) Crud // search GetByData(params cmap.CMap) Crud // get data no search GetByID(id interface{}) Crud // by id GetMoreBySearch(params cmap.CMap) Crud // more search // delete by id Delete(id interface{}) Crud // delete // crud and search id // form data // [create/update] future all will use json replace form request // form will not update UpdateForm(params cmap.CMap) error // update CreateForm(params cmap.CMap) error // create CreateResID(params cmap.CMap) (str.ID, error) // create res insert id // crud and search id // json data Update() Crud // update Create() Crud // create, include res insert id CreateMore() Crud // create more, data must array type, single table // select Select(q interface{}, args ...interface{}) Crud // select sql From(query string) Crud // from sql, if use search, From must only once Group(query string) Crud // the last group by Search(params cmap.CMap) Crud // search pager Single() Crud // no search Exec() Crud // exec insert/update/delete sql Error() error // crud error RowsAffected() int64 // inflect rows Pager() result.Pager // search pager Begin() Crud // start a transaction Commit() Crud // commit a transaction Rollback() Crud // rollback a transaction // contains filtered or unexported methods }
crud is db driver extend
type DBCrud ¶
type DBCrud struct {
// contains filtered or unexported fields
}
implement DBCrud form data
func (*DBCrud) CreateResID ¶
create res insert id
func (*DBCrud) GetMoreBySearch ¶
the same as search more tables
func (*DBCrud) RowsAffected ¶
type DBTool ¶
DB tool
func (*DBTool) CreateDataResID ¶
创建数据,通用 返回id,事务,慎用 业务少可用
func (*DBTool) CreateFormData ¶
via form data create
func (*DBTool) CreateMoreData ¶
data must array type more data create single table
func (*DBTool) GetBasicTypesData ¶ added in v1.9.4
func (*DBTool) GetDataByName ¶
获得数据,根据name条件 Deprecated
func (*DBTool) GetDataBySQL ¶
获得数据,根据sql语句,无分页
func (*DBTool) GetDataBySQLSearch ¶
func (db *DBTool) GetDataBySQLSearch(data interface{}, sql, sqlNt string, clientPage, everyPage int64, args []interface{}, argsNt []interface{}) (pager result.Pager)
获得数据,根据sql语句,分页 args : sql参数'?' sql, sqlNt args 相同, 共用args
func (*DBTool) GetDataBySearch ¶
获得数据,分页/查询
func (*DBTool) GetDataBySelectSQLSearch ¶
select sql search
func (*DBTool) GetMoreDataBySearch ¶
More Table params: innerTables is inner join tables params: leftTables is left join tables return: search info table1 as main table, include other tables_id(foreign key)
func (*DBTool) UpdateFormData ¶
via form data update
func (*DBTool) UpdateStructData ¶
结合struct修改
func (*DBTool) ValidateSQL ¶
select检查是否存在 == nil 即存在
type GT ¶
type GT struct { *Params // CMap CMaps cmap.CMap // params // select sql Select string // select sql From string // only once Group string // the last group Args []interface{} ArgsNt []interface{} // contains filtered or unexported fields }
GT SQL struct
type Log ¶
log
func (*Log) DefaultFileLog ¶
func (l *Log) DefaultFileLog()
Default file log maintain 7 days data, every 24 hour split file
func (*Log) ErrorLine ¶ added in v1.9.2
func (l *Log) ErrorLine(args ...interface{})
print error line
type LogWriter ¶
type LogWriter interface {
Println(v ...interface{})
}
LogWriter log writer interface
type Params ¶
type Params struct { // attributes InnerTable []string // inner join tables LeftTable []string // left join tables Table string // table name Model interface{} // table model, like User{} KeyModel interface{} // key like model Data interface{} // table model data, like var user User{}, it is 'user', it store real data // count SubSQL string // SubQuery SQL // where SubWhereSQL string // SubWhere SQL }
crud params