Documentation ¶
Index ¶
- Variables
- func CheckClause(cond clause.Expression) error
- type Columns
- func (cs Columns) Eq(query SubQuery) field.Expr
- func (cs Columns) Gt(query SubQuery) field.Expr
- func (cs Columns) Gte(query SubQuery) field.Expr
- func (cs Columns) In(queryOrValue Condition) field.Expr
- func (cs Columns) Lt(query SubQuery) field.Expr
- func (cs Columns) Lte(query SubQuery) field.Expr
- func (cs Columns) Neq(query SubQuery) field.Expr
- func (cs Columns) NotIn(queryOrValue Condition) field.Expr
- func (cs Columns) Set(query SubQuery) field.AssignExpr
- type Condition
- type Config
- func (cfg *Config) Revise() (err error)
- func (cfg *Config) WithDataTypeMap(newMap map[string]func(columnType gorm.ColumnType) (dataType string))
- func (cfg *Config) WithDbNameOpts(opts ...model.SchemaNameOpt)
- func (cfg *Config) WithFileNameStrategy(ns func(tableName string) (fileName string))
- func (cfg *Config) WithImportPkgPath(paths ...string)
- func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagContent string))
- func (cfg *Config) WithModelNameStrategy(ns func(tableName string) (modelName string))
- func (cfg *Config) WithOpts(opts ...ModelOpt)
- func (cfg *Config) WithTableNameStrategy(ns func(tableName string) (targetTableName string))
- type DO
- func (d *DO) AddError(err error) error
- func (d *DO) Alias() string
- func (d DO) As(alias string) Dao
- func (d *DO) Assign(attrs ...field.AssignExpr) Dao
- func (d *DO) Attrs(attrs ...field.AssignExpr) Dao
- func (d *DO) BeCond() interface{}
- func (d *DO) Build(builder clause.Builder)
- func (d *DO) Clauses(conds ...clause.Expression) Dao
- func (*DO) Columns(cols ...field.Expr) Columns
- func (d *DO) CondError() error
- func (d *DO) Count() (count int64, err error)
- func (d *DO) Create(value interface{}) error
- func (d *DO) CreateInBatches(value interface{}, batchSize int) error
- func (d *DO) Debug() Dao
- func (d *DO) Delete(models ...interface{}) (info ResultInfo, err error)
- func (d *DO) Distinct(columns ...field.Expr) Dao
- func (d *DO) Find() (results interface{}, err error)
- func (d *DO) FindInBatches(dest interface{}, batchSize int, fc func(tx Dao, batch int) error) error
- func (d *DO) First() (result interface{}, err error)
- func (d *DO) FirstOrCreate() (result interface{}, err error)
- func (d *DO) FirstOrInit() (result interface{}, err error)
- func (d *DO) Group(columns ...field.Expr) Dao
- func (d *DO) Having(conds ...Condition) Dao
- func (d *DO) Join(table schema.Tabler, conds ...field.Expr) Dao
- func (d *DO) Joins(field field.RelationField) Dao
- func (d *DO) Last() (result interface{}, err error)
- func (d *DO) LeftJoin(table schema.Tabler, conds ...field.Expr) Dao
- func (d *DO) Limit(limit int) Dao
- func (d *DO) Not(conds ...Condition) Dao
- func (d *DO) Offset(offset int) Dao
- func (d *DO) Omit(columns ...field.Expr) Dao
- func (d *DO) Or(conds ...Condition) Dao
- func (d *DO) Order(columns ...field.Expr) Dao
- func (d *DO) Pluck(column field.Expr, dest interface{}) error
- func (d *DO) Preload(field field.RelationField) Dao
- func (d *DO) Quote(raw string) string
- func (d *DO) ReplaceConnPool(pool gorm.ConnPool)
- func (d *DO) ReplaceDB(db *gorm.DB)
- func (d DO) Returning(value interface{}, columns ...string) Dao
- func (d *DO) RightJoin(table schema.Tabler, conds ...field.Expr) Dao
- func (d *DO) Row() *sql.Row
- func (d *DO) Rows() (*sql.Rows, error)
- func (d *DO) Save(value interface{}) error
- func (d *DO) Scan(dest interface{}) error
- func (d *DO) ScanRows(rows *sql.Rows, dest interface{}) error
- func (d *DO) Scopes(funcs ...func(Dao) Dao) Dao
- func (d *DO) Select(columns ...field.Expr) Dao
- func (d *DO) Session(config *gorm.Session) Dao
- func (d DO) TableName() string
- func (d *DO) Take() (result interface{}, err error)
- func (d *DO) UnderlyingDB() *gorm.DB
- func (d *DO) Unscoped() Dao
- func (d *DO) Update(column field.Expr, value interface{}) (info ResultInfo, err error)
- func (d *DO) UpdateColumn(column field.Expr, value interface{}) (info ResultInfo, err error)
- func (d *DO) UpdateColumnSimple(columns ...field.AssignExpr) (info ResultInfo, err error)
- func (d *DO) UpdateColumns(value interface{}) (info ResultInfo, err error)
- func (d *DO) UpdateFrom(q SubQuery) Dao
- func (d *DO) UpdateSimple(columns ...field.AssignExpr) (info ResultInfo, err error)
- func (d *DO) Updates(value interface{}) (info ResultInfo, err error)
- func (d *DO) UseDB(db *gorm.DB, opts ...DOOption)
- func (d *DO) UseModel(model interface{})
- func (d *DO) UseTable(tableName string)
- func (d *DO) Where(conds ...Condition) Dao
- func (d *DO) WithContext(ctx context.Context) Dao
- func (d DO) WithResult(fc func(tx Dao)) ResultInfo
- type DOConfig
- type DOOption
- type Dao
- type Field
- type GenerateMode
- type Generator
- func (g *Generator) ApplyBasic(models ...interface{})
- func (g *Generator) ApplyInterface(fc interface{}, models ...interface{})
- func (g *Generator) Execute()
- func (g *Generator) GenerateAllTable(opts ...ModelOpt) (tableModels []interface{})
- func (g *Generator) GenerateModel(tableName string, opts ...ModelOpt) *generate.QueryStructMeta
- func (g *Generator) GenerateModelAs(tableName string, modelName string, opts ...ModelOpt) *generate.QueryStructMeta
- func (g *Generator) GenerateModelFrom(obj helper.Object) *generate.QueryStructMeta
- func (g *Generator) UseDB(db *gorm.DB)
- type M
- type ModelOpt
- type ResultInfo
- type RowsAffected
- type SQLResult
- type SQLRow
- type SQLRows
- type SubQuery
- type T
Constants ¶
This section is empty.
Variables ¶
var ( FieldModify = func(opt func(Field) Field) model.ModifyFieldOpt { return func(f *model.Field) *model.Field { return opt(f) } } // FieldNew add new field (any type your want) FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt { return func(*model.Field) *model.Field { return &model.Field{ Name: fieldName, Type: fieldType, Tag: fieldTag, } } } // FieldIgnore ignore some columns by name FieldIgnore = func(columnNames ...string) model.FilterFieldOpt { return func(m *model.Field) *model.Field { for _, name := range columnNames { if m.ColumnName == name { return nil } } return m } } // FieldIgnoreReg ignore some columns by RegExp FieldIgnoreReg = func(columnNameRegs ...string) model.FilterFieldOpt { regs := make([]regexp.Regexp, len(columnNameRegs)) for i, reg := range columnNameRegs { regs[i] = *regexp.MustCompile(reg) } return func(m *model.Field) *model.Field { for _, reg := range regs { if reg.MatchString(m.ColumnName) { return nil } } return m } } // FieldRename specify field name in generated struct FieldRename = func(columnName string, newName string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.Name = newName } return m } } // FieldComment specify field comment in generated struct FieldComment = func(columnName string, comment string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.ColumnComment = comment m.MultilineComment = strings.Contains(comment, "\n") } return m } } // FieldType specify field type in generated struct FieldType = func(columnName string, newType string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.Type = newType } return m } } // FieldTypeReg specify field type in generated struct by RegExp FieldTypeReg = func(columnNameReg string, newType string) model.ModifyFieldOpt { reg := regexp.MustCompile(columnNameReg) return func(m *model.Field) *model.Field { if reg.MatchString(m.ColumnName) { m.Type = newType } return m } } // FieldGenType specify field gen type in generated dao FieldGenType = func(columnName string, newType string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.CustomGenType = newType } return m } } // FieldGenTypeReg specify field gen type in generated dao by RegExp FieldGenTypeReg = func(columnNameReg string, newType string) model.ModifyFieldOpt { reg := regexp.MustCompile(columnNameReg) return func(m *model.Field) *model.Field { if reg.MatchString(m.ColumnName) { m.CustomGenType = newType } return m } } // FieldTag specify GORM tag and JSON tag FieldTag = func(columnName string, tagFunc func(tag field.Tag) field.Tag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.Tag = tagFunc(m.Tag) } return m } } // FieldJSONTag specify JSON tag FieldJSONTag = func(columnName string, jsonTag string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.Tag.Set(field.TagKeyJson, jsonTag) } return m } } // FieldJSONTagWithNS specify JSON tag with name strategy FieldJSONTagWithNS = func(schemaName func(columnName string) (tagContent string)) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { var required = strings.Contains(m.GORMTag.Build(), "not null") jsonTag := schemaName(m.ColumnName) if schemaName != nil { if required { jsonTag += ",required" } if m.Type == "int64" { jsonTag += ",string" } m.Tag.Set(field.TagKeyJson, jsonTag) } return m } } // FieldGORMTag specify GORM tag FieldGORMTag = func(columnName string, gormTag func(tag field.GormTag) field.GormTag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { m.GORMTag = gormTag(m.GORMTag) } return m } } // FieldGORMTagReg specify GORM tag by RegExp FieldGORMTagReg = func(columnNameReg string, gormTag func(tag field.GormTag) field.GormTag) model.ModifyFieldOpt { reg := regexp.MustCompile(columnNameReg) return func(m *model.Field) *model.Field { if reg.MatchString(m.ColumnName) { m.GORMTag = gormTag(m.GORMTag) } return m } } // FieldNewTag add new tag FieldNewTag = func(columnName string, newTag field.Tag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { for k, v := range newTag { m.Tag.Set(k, v) } } return m } } // FieldNewTagWithNS add new tag with name strategy FieldNewTagWithNS = func(tagName string, schemaName func(columnName string) string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if schemaName == nil { schemaName = func(name string) string { return name } } m.Tag.Set(tagName, schemaName(m.ColumnName)) return m } } // FieldTrimPrefix trim column name's prefix FieldTrimPrefix = func(prefix string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { m.Name = strings.TrimPrefix(m.Name, prefix) return m } } // FieldTrimSuffix trim column name's suffix FieldTrimSuffix = func(suffix string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { m.Name = strings.TrimSuffix(m.Name, suffix) return m } } // FieldAddPrefix add prefix to struct's memeber name FieldAddPrefix = func(prefix string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { m.Name = prefix + m.Name return m } } // FieldAddSuffix add suffix to struct's memeber name FieldAddSuffix = func(suffix string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { m.Name += suffix return m } } // FieldRelate relate to table in database FieldRelate = func(relationship field.RelationshipType, fieldName string, table *generate.QueryStructMeta, config *field.RelateConfig) model.CreateFieldOpt { if config == nil { config = &field.RelateConfig{} } return func(*model.Field) *model.Field { return &model.Field{ Name: fieldName, Type: config.RelateFieldPrefix(relationship) + table.StructInfo.Type, Tag: config.GetTag(fieldName), GORMTag: config.GORMTag, Relation: field.NewRelationWithType( relationship, fieldName, table.StructInfo.Package+"."+table.StructInfo.Type, table.Relations()...), } } } // FieldRelateModel relate to exist table model FieldRelateModel = func(relationship field.RelationshipType, fieldName string, relModel interface{}, config *field.RelateConfig) model.CreateFieldOpt { st := reflect.TypeOf(relModel) if st.Kind() == reflect.Ptr { st = st.Elem() } fieldType := st.String() if config == nil { config = &field.RelateConfig{} } return func(*model.Field) *model.Field { return &model.Field{ Name: fieldName, Type: config.RelateFieldPrefix(relationship) + fieldType, GORMTag: config.GORMTag, Tag: config.GetTag(fieldName), Relation: field.NewRelationWithModel(relationship, fieldName, fieldType, relModel), } } } // WithMethod add custom method for table model WithMethod = func(methods ...interface{}) model.AddMethodOpt { return func() []interface{} { return methods } } )
var ( // Debug use DB in debug mode Debug doOptions = func(db *gorm.DB) *gorm.DB { return db.Debug() } )
var (
DefaultMethodTableWithNamer = (&defaultModel{}).TableName
)
var ( // ErrEmptyCondition empty condition ErrEmptyCondition = errors.New("empty condition") )
Functions ¶
func CheckClause ¶
func CheckClause(cond clause.Expression) error
CheckClause check security of Expression
Types ¶
type Condition ¶
type Condition interface { BeCond() interface{} CondError() error }
Condition query condition field.Expr and subquery are expect value
func Cond ¶
func Cond(exprs ...clause.Expression) []Condition
Cond convert expression array to condition array
type Config ¶
type Config struct { OutPath string // query code path OutFile string // query code file name, default: gen.go ModelPkgPath string // generated model code's package name WithUnitTest bool // generate unit test for query code // generate model global configuration FieldNullable bool // generate pointer when field is nullable FieldCoverable bool // generate pointer when field has default value, to fix problem zero value cannot be assign: https://gorm.io/docs/create.html#Default-Values FieldSignable bool // detect integer field's unsigned type, adjust generated data type FieldWithIndexTag bool // generate with gorm index tag FieldWithTypeTag bool // generate with gorm column type tag Mode GenerateMode // generate mode // contains filtered or unexported fields }
Config generator's basic configuration
func (*Config) WithDataTypeMap ¶
func (cfg *Config) WithDataTypeMap(newMap map[string]func(columnType gorm.ColumnType) (dataType string))
WithDataTypeMap specify data type mapping relationship, only work when syncing table from db
func (*Config) WithDbNameOpts ¶
func (cfg *Config) WithDbNameOpts(opts ...model.SchemaNameOpt)
WithDbNameOpts set get database name function
func (*Config) WithFileNameStrategy ¶
WithFileNameStrategy specify file name naming strategy, only work when syncing table from db
func (*Config) WithImportPkgPath ¶
WithImportPkgPath specify import package path
func (*Config) WithJSONTagNameStrategy ¶
WithJSONTagNameStrategy specify json tag naming strategy
func (*Config) WithModelNameStrategy ¶
WithModelNameStrategy specify model struct name naming strategy, only work when syncing table from db
func (*Config) WithTableNameStrategy ¶
WithTableNameStrategy specify table name naming strategy, only work when syncing table from db
type DO ¶
type DO struct { *DOConfig // contains filtered or unexported fields }
DO (data object): implement basic query methods the structure embedded with a *gorm.DB, and has a element item "alias" will be used when used as a sub query
func (*DO) Build ¶
Build implement the interface of claues.Expression only call WHERE clause's Build
func (*DO) CreateInBatches ¶
CreateInBatches ...
func (*DO) Delete ¶
func (d *DO) Delete(models ...interface{}) (info ResultInfo, err error)
Delete ...
func (*DO) FindInBatches ¶
FindInBatches ...
func (*DO) FirstOrCreate ¶
FirstOrCreate ...
func (*DO) ReplaceConnPool ¶
ReplaceConnPool replace db connection pool
func (*DO) UnderlyingDB ¶
UnderlyingDB return the underlying database connection
func (*DO) Update ¶
func (d *DO) Update(column field.Expr, value interface{}) (info ResultInfo, err error)
Update ...
func (*DO) UpdateColumn ¶
func (d *DO) UpdateColumn(column field.Expr, value interface{}) (info ResultInfo, err error)
UpdateColumn ...
func (*DO) UpdateColumnSimple ¶
func (d *DO) UpdateColumnSimple(columns ...field.AssignExpr) (info ResultInfo, err error)
UpdateColumnSimple ...
func (*DO) UpdateColumns ¶
func (d *DO) UpdateColumns(value interface{}) (info ResultInfo, err error)
UpdateColumns ...
func (*DO) UpdateSimple ¶
func (d *DO) UpdateSimple(columns ...field.AssignExpr) (info ResultInfo, err error)
UpdateSimple ...
func (*DO) Updates ¶
func (d *DO) Updates(value interface{}) (info ResultInfo, err error)
Updates ...
func (*DO) UseModel ¶
func (d *DO) UseModel(model interface{})
UseModel specify a data model structure as a source for table name
func (*DO) WithContext ¶
WithContext return a DO with db with context
type DOConfig ¶
type DOConfig struct { }
func (*DOConfig) AfterInitialize ¶
AfterInitialize initialize plugins after db connected
type Dao ¶
type Dao interface { SubQuery schema.Tabler As(alias string) Dao Not(conds ...Condition) Dao Or(conds ...Condition) Dao Select(columns ...field.Expr) Dao Where(conds ...Condition) Dao Order(columns ...field.Expr) Dao Distinct(columns ...field.Expr) Dao Omit(columns ...field.Expr) Dao Join(table schema.Tabler, conds ...field.Expr) Dao LeftJoin(table schema.Tabler, conds ...field.Expr) Dao RightJoin(table schema.Tabler, conds ...field.Expr) Dao Group(columns ...field.Expr) Dao Having(conds ...Condition) Dao Limit(limit int) Dao Offset(offset int) Dao Scopes(funcs ...func(Dao) Dao) Dao Unscoped() Dao Attrs(attrs ...field.AssignExpr) Dao Assign(attrs ...field.AssignExpr) Dao Joins(field field.RelationField) Dao Preload(field field.RelationField) Dao Clauses(conds ...clause.Expression) Dao Create(value interface{}) error CreateInBatches(value interface{}, batchSize int) error Save(value interface{}) error First() (result interface{}, err error) Take() (result interface{}, err error) Last() (result interface{}, err error) Find() (results interface{}, err error) FindInBatches(dest interface{}, batchSize int, fc func(tx Dao, batch int) error) error FirstOrInit() (result interface{}, err error) FirstOrCreate() (result interface{}, err error) Update(column field.Expr, value interface{}) (info ResultInfo, err error) UpdateSimple(columns ...field.AssignExpr) (info ResultInfo, err error) Updates(values interface{}) (info ResultInfo, err error) UpdateColumn(column field.Expr, value interface{}) (info ResultInfo, err error) UpdateColumns(values interface{}) (info ResultInfo, err error) UpdateColumnSimple(columns ...field.AssignExpr) (info ResultInfo, err error) Delete(...interface{}) (info ResultInfo, err error) Count() (int64, error) Row() *sql.Row Rows() (*sql.Rows, error) Scan(dest interface{}) error Pluck(column field.Expr, dest interface{}) error ScanRows(rows *sql.Rows, dest interface{}) error AddError(err error) error }
Dao CRUD methods
type GenerateMode ¶
type GenerateMode uint
GenerateMode generate mode
const ( // WithDefaultQuery create default query in generated code WithDefaultQuery GenerateMode = 1 << iota // WithoutContext generate code without context constrain WithoutContext // WithQueryInterface generate code with exported interface object WithQueryInterface )
type Generator ¶
type Generator struct { Config Data map[string]*genInfo //gen query data Models map[string]*generate.QueryStructMeta //gen model data Tables map[string]*generate.QueryStructMeta //gen model data }
Generator code generator
func (*Generator) ApplyBasic ¶
func (g *Generator) ApplyBasic(models ...interface{})
ApplyBasic specify Models which will implement basic .diy_method
func (*Generator) ApplyInterface ¶
func (g *Generator) ApplyInterface(fc interface{}, models ...interface{})
ApplyInterface specifies .diy_method interfaces on structures, implment codes will be generated after calling g.Execute() eg: g.ApplyInterface(func(model.Method){}, model.User{}, model.Company{})
func (*Generator) GenerateAllTable ¶
GenerateAllTable generate all tables in db
func (*Generator) GenerateModel ¶
func (g *Generator) GenerateModel(tableName string, opts ...ModelOpt) *generate.QueryStructMeta
GenerateModel catch table info from db, return a BaseStruct
func (*Generator) GenerateModelAs ¶
func (g *Generator) GenerateModelAs(tableName string, modelName string, opts ...ModelOpt) *generate.QueryStructMeta
GenerateModelAs catch table info from db, return a BaseStruct
func (*Generator) GenerateModelFrom ¶
func (g *Generator) GenerateModelFrom(obj helper.Object) *generate.QueryStructMeta
GenerateModelFrom generate model from object
type ResultInfo ¶
ResultInfo query/execute info