Documentation
¶
Index ¶
- Variables
- func CheckClause(cond clause.Expression) error
- func WriteDiagnosticJSON(w io.Writer, err error) error
- type ClauseChecker
- 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) WithDataTypesNullType(all bool)
- 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) SetLogger(logger Logger)
- func (g *Generator) UseDB(db *gorm.DB)
- type GenericsDo
- func (b GenericsDo[T, E]) Assign(attrs ...field.AssignExpr) T
- func (b GenericsDo[T, E]) Attrs(attrs ...field.AssignExpr) T
- func (b GenericsDo[T, E]) Clauses(conds ...clause.Expression) T
- func (b GenericsDo[T, E]) Create(values ...E) error
- func (b GenericsDo[T, E]) CreateInBatches(values []E, batchSize int) error
- func (b GenericsDo[T, E]) Debug() T
- func (b GenericsDo[T, E]) Delete(models ...E) (result ResultInfo, err error)
- func (b GenericsDo[T, E]) Distinct(cols ...field.Expr) T
- func (b GenericsDo[T, E]) Find() ([]E, error)
- func (b GenericsDo[T, E]) FindByPage(offset int, limit int) (result []E, count int64, err error)
- func (b GenericsDo[T, E]) FindInBatch(batchSize int, fc func(tx Dao, batch int) error) (results []E, err error)
- func (b GenericsDo[T, E]) FindInBatches(result *[]E, batchSize int, fc func(tx Dao, batch int) error) error
- func (b GenericsDo[T, E]) First() (E, error)
- func (b GenericsDo[T, E]) FirstOrCreate() (E, error)
- func (b GenericsDo[T, E]) FirstOrInit() (E, error)
- func (b GenericsDo[T, E]) Group(cols ...field.Expr) T
- func (b GenericsDo[T, E]) Having(conds ...Condition) T
- func (b GenericsDo[T, E]) Join(table schema.Tabler, on ...field.Expr) T
- func (b GenericsDo[T, E]) Joins(fields ...field.RelationField) T
- func (b GenericsDo[T, E]) Last() (E, error)
- func (b GenericsDo[T, E]) LeftJoin(table schema.Tabler, on ...field.Expr) T
- func (b GenericsDo[T, E]) Limit(limit int) T
- func (b GenericsDo[T, E]) Not(conds ...Condition) T
- func (b GenericsDo[T, E]) Offset(offset int) T
- func (b GenericsDo[T, E]) Omit(cols ...field.Expr) T
- func (b GenericsDo[T, E]) Or(conds ...Condition) T
- func (b GenericsDo[T, E]) Order(conds ...field.Expr) T
- func (b GenericsDo[T, E]) Preload(fields ...field.RelationField) T
- func (b GenericsDo[T, E]) ReadDB() T
- func (b GenericsDo[T, E]) Returning(value interface{}, columns ...string) T
- func (b GenericsDo[T, E]) RightJoin(table schema.Tabler, on ...field.Expr) T
- func (b GenericsDo[T, E]) Save(values ...E) error
- func (b GenericsDo[T, E]) Scan(result interface{}) (err error)
- func (b GenericsDo[T, E]) ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
- func (b GenericsDo[T, E]) Scopes(funcs ...func(Dao) Dao) T
- func (b GenericsDo[T, E]) Select(conds ...field.Expr) T
- func (b GenericsDo[T, E]) Session(config *gorm.Session) T
- func (b GenericsDo[T, E]) Take() (E, error)
- func (b GenericsDo[T, E]) ToSQL(queryFn func(T)) string
- func (b GenericsDo[T, E]) Unscoped() T
- func (b GenericsDo[T, E]) Where(conds ...Condition) T
- func (b GenericsDo[T, E]) WithContext(ctx context.Context) T
- func (b GenericsDo[T, E]) WriteDB() T
- type IGenericsDo
- type IWithDO
- type Logger
- type M
- type ModelOpt
- type ResultInfo
- type RowsAffected
- type SQLResult
- type SQLRow
- type SQLRows
- type SubQuery
- type T
- type WithDOFunc
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) } } FieldFilter = func(opt func(Field) Field) model.FilterFieldOpt { return func(f *model.Field) *model.Field { return opt(f) } } // WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts. /** * * @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types. * If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types. * * Examples: * * When `all` is true: * - `int64` will be replaced with `datatypes.NullInt64` * - `string` will be replaced with `datatypes.NullString` * * When `all` is false: * - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types. * * Note: * Ensure that proper error handling is implemented when converting * fields to their `datatypes.Null[T]` types to avoid runtime issues. */ WithDataTypesNullType = func(all bool) model.ModifyFieldOpt { return func(f *model.Field) *model.Field { ft := f.Type nullable := false if strings.HasPrefix(ft, "*") { nullable = true ft = strings.TrimLeft(ft, "*") } if !all && !nullable { return f } switch ft { case "time.Time", "string", "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "float64", "float32", "byte", "bool": ft = fmt.Sprintf("datatypes.Null[%s]", ft) default: return f } f.CustomGenType = f.GenType() f.Type = ft return 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 { if schemaName != nil { m.Tag.Set(field.TagKeyJson, schemaName(m.ColumnName)) } 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 ErrClauseNotHandled = errors.New("clause not handled")
var ( // ErrEmptyCondition empty condition ErrEmptyCondition = errors.New("empty condition") )
Functions ¶
func CheckClause ¶ added in v0.1.28
func CheckClause(cond clause.Expression) error
CheckClause check security of Expression
Types ¶
type ClauseChecker ¶ added in v0.3.28
type ClauseChecker func(clause.Expression) error
type Columns ¶ added in v0.3.9
Columns columns array
type Condition ¶
type Condition interface {
BeCond() interface{}
CondError() error
}
Condition query condition field.Expr and subquery are expect value
func Cond ¶ added in v0.0.17
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
Incremental bool // skip writing unchanged generated files (based on manifest hash)
MergeQuery bool // keep previously generated query entries (A+B) when generating subsets
// 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
FieldWithDefaultTag bool
Mode GenerateMode // generate mode
UnitTestTemplate string
// contains filtered or unexported fields
}
Config generator's basic configuration
func (*Config) WithDataTypeMap ¶ added in v0.1.20
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) WithDataTypesNullType ¶ added in v0.3.27
WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts. *
*
* @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types.
* If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types.
*
* Examples:
*
* When `all` is true:
* - `int64` will be replaced with `datatypes.NullInt64`
* - `string` will be replaced with `datatypes.NullString`
*
* When `all` is false:
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types.
*
* Note:
* Ensure that proper error handling is implemented when converting
* fields to their `datatypes.Null[T]` types to avoid runtime issues.
func (*Config) WithDbNameOpts ¶
func (cfg *Config) WithDbNameOpts(opts ...model.SchemaNameOpt)
WithDbNameOpts set get database name function
func (*Config) WithFileNameStrategy ¶ added in v0.2.27
WithFileNameStrategy specify file name naming strategy, only work when syncing table from db
func (*Config) WithImportPkgPath ¶ added in v0.2.31
WithImportPkgPath specify import package path
func (*Config) WithJSONTagNameStrategy ¶ added in v0.1.26
WithJSONTagNameStrategy specify json tag naming strategy
func (*Config) WithModelNameStrategy ¶ added in v0.2.27
WithModelNameStrategy specify model struct name naming strategy, only work when syncing table from db
func (*Config) WithTableNameStrategy ¶ added in v0.2.27
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) Clauses ¶ added in v0.0.16
func (d *DO) Clauses(conds ...clause.Expression) Dao
Clauses specify Clauses
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) Preload ¶ added in v0.0.24
func (d *DO) Preload(field field.RelationField) Dao
Preload ...
func (*DO) ReplaceConnPool ¶ added in v0.3.18
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 ¶ added in v0.0.15
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) UpdateFrom ¶ added in v0.1.18
UpdateFrom specify update sub query
func (*DO) UpdateSimple ¶ added in v0.0.15
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 ¶ added in v0.0.16
WithContext return a DO with db with context
func (DO) WithResult ¶ added in v0.2.39
func (d DO) WithResult(fc func(tx Dao)) ResultInfo
WithResult ...
type DOConfig ¶ added in v0.3.18
type DOConfig struct {
ClauseChecker ClauseChecker
}
func (*DOConfig) AfterInitialize ¶ added in v0.3.18
AfterInitialize initialize plugins after db connected
type DOOption ¶ added in v0.3.18
DOOption gorm option interface
func WithClauseChecker ¶ added in v0.3.28
func WithClauseChecker(checker ClauseChecker) DOOption
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 ¶ added in v0.0.16
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 // WithGeneric generate code with generic WithGeneric )
type Generator ¶
type Generator struct {
Config
Data map[string]*genInfo //gen query data
// contains filtered or unexported fields
}
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, implement codes will be generated after calling g.Execute() eg: g.ApplyInterface(func(model.Method){}, model.User{}, model.Company{})
func (*Generator) GenerateAllTable ¶ added in v0.1.20
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 ¶ added in v0.3.0
func (g *Generator) GenerateModelFrom(obj helper.Object) *generate.QueryStructMeta
GenerateModelFrom generate model from object
type GenericsDo ¶ added in v0.3.28
type GenericsDo[T IGenericsDo[T, E], E any] struct { DO IWithDO[T] }
GenericsDo base impl of IGenericsDo
func (GenericsDo[T, E]) Assign ¶ added in v0.3.28
func (b GenericsDo[T, E]) Assign(attrs ...field.AssignExpr) T
Assign ...
func (GenericsDo[T, E]) Attrs ¶ added in v0.3.28
func (b GenericsDo[T, E]) Attrs(attrs ...field.AssignExpr) T
Attrs ...
func (GenericsDo[T, E]) Clauses ¶ added in v0.3.28
func (b GenericsDo[T, E]) Clauses(conds ...clause.Expression) T
Clauses ...
func (GenericsDo[T, E]) Create ¶ added in v0.3.28
func (b GenericsDo[T, E]) Create(values ...E) error
Create ...
func (GenericsDo[T, E]) CreateInBatches ¶ added in v0.3.28
func (b GenericsDo[T, E]) CreateInBatches(values []E, batchSize int) error
CreateInBatches ...
func (GenericsDo[T, E]) Delete ¶ added in v0.3.28
func (b GenericsDo[T, E]) Delete(models ...E) (result ResultInfo, err error)
Delete ...
func (GenericsDo[T, E]) Distinct ¶ added in v0.3.28
func (b GenericsDo[T, E]) Distinct(cols ...field.Expr) T
Distinct ...
func (GenericsDo[T, E]) Find ¶ added in v0.3.28
func (b GenericsDo[T, E]) Find() ([]E, error)
Find ...
func (GenericsDo[T, E]) FindByPage ¶ added in v0.3.28
func (b GenericsDo[T, E]) FindByPage(offset int, limit int) (result []E, count int64, err error)
FindByPage ...
func (GenericsDo[T, E]) FindInBatch ¶ added in v0.3.28
func (b GenericsDo[T, E]) FindInBatch(batchSize int, fc func(tx Dao, batch int) error) (results []E, err error)
FindInBatch ...
func (GenericsDo[T, E]) FindInBatches ¶ added in v0.3.28
func (b GenericsDo[T, E]) FindInBatches(result *[]E, batchSize int, fc func(tx Dao, batch int) error) error
FindInBatches ...
func (GenericsDo[T, E]) First ¶ added in v0.3.28
func (b GenericsDo[T, E]) First() (E, error)
First ...
func (GenericsDo[T, E]) FirstOrCreate ¶ added in v0.3.28
func (b GenericsDo[T, E]) FirstOrCreate() (E, error)
FirstOrCreate ...
func (GenericsDo[T, E]) FirstOrInit ¶ added in v0.3.28
func (b GenericsDo[T, E]) FirstOrInit() (E, error)
FirstOrInit ...
func (GenericsDo[T, E]) Group ¶ added in v0.3.28
func (b GenericsDo[T, E]) Group(cols ...field.Expr) T
Group ...
func (GenericsDo[T, E]) Having ¶ added in v0.3.28
func (b GenericsDo[T, E]) Having(conds ...Condition) T
Having ...
func (GenericsDo[T, E]) Join ¶ added in v0.3.28
func (b GenericsDo[T, E]) Join(table schema.Tabler, on ...field.Expr) T
Join ...
func (GenericsDo[T, E]) Joins ¶ added in v0.3.28
func (b GenericsDo[T, E]) Joins(fields ...field.RelationField) T
Joins ...
func (GenericsDo[T, E]) Last ¶ added in v0.3.28
func (b GenericsDo[T, E]) Last() (E, error)
Last ...
func (GenericsDo[T, E]) LeftJoin ¶ added in v0.3.28
func (b GenericsDo[T, E]) LeftJoin(table schema.Tabler, on ...field.Expr) T
LeftJoin ...
func (GenericsDo[T, E]) Limit ¶ added in v0.3.28
func (b GenericsDo[T, E]) Limit(limit int) T
Limit ...
func (GenericsDo[T, E]) Not ¶ added in v0.3.28
func (b GenericsDo[T, E]) Not(conds ...Condition) T
Not ...
func (GenericsDo[T, E]) Offset ¶ added in v0.3.28
func (b GenericsDo[T, E]) Offset(offset int) T
Offset ...
func (GenericsDo[T, E]) Omit ¶ added in v0.3.28
func (b GenericsDo[T, E]) Omit(cols ...field.Expr) T
Omit ...
func (GenericsDo[T, E]) Or ¶ added in v0.3.28
func (b GenericsDo[T, E]) Or(conds ...Condition) T
Or ...
func (GenericsDo[T, E]) Order ¶ added in v0.3.28
func (b GenericsDo[T, E]) Order(conds ...field.Expr) T
Order ...
func (GenericsDo[T, E]) Preload ¶ added in v0.3.28
func (b GenericsDo[T, E]) Preload(fields ...field.RelationField) T
Preload ...
func (GenericsDo[T, E]) Returning ¶ added in v0.3.28
func (b GenericsDo[T, E]) Returning(value interface{}, columns ...string) T
Returning ...
func (GenericsDo[T, E]) RightJoin ¶ added in v0.3.28
func (b GenericsDo[T, E]) RightJoin(table schema.Tabler, on ...field.Expr) T
RightJoin ...
func (GenericsDo[T, E]) Save ¶ added in v0.3.28
func (b GenericsDo[T, E]) Save(values ...E) error
Save : !!! underlying implementation is different with GORM The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (GenericsDo[T, E]) Scan ¶ added in v0.3.28
func (b GenericsDo[T, E]) Scan(result interface{}) (err error)
Scan ...
func (GenericsDo[T, E]) ScanByPage ¶ added in v0.3.28
func (b GenericsDo[T, E]) ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
ScanByPage ...
func (GenericsDo[T, E]) Scopes ¶ added in v0.3.28
func (b GenericsDo[T, E]) Scopes(funcs ...func(Dao) Dao) T
Scopes ...
func (GenericsDo[T, E]) Select ¶ added in v0.3.28
func (b GenericsDo[T, E]) Select(conds ...field.Expr) T
Select ...
func (GenericsDo[T, E]) Session ¶ added in v0.3.28
func (b GenericsDo[T, E]) Session(config *gorm.Session) T
Session ...
func (GenericsDo[T, E]) Take ¶ added in v0.3.28
func (b GenericsDo[T, E]) Take() (E, error)
Take ...
func (GenericsDo[T, E]) ToSQL ¶ added in v0.3.28
func (b GenericsDo[T, E]) ToSQL(queryFn func(T)) string
ToSQL ...
func (GenericsDo[T, E]) Unscoped ¶ added in v0.3.28
func (b GenericsDo[T, E]) Unscoped() T
Unscoped ...
func (GenericsDo[T, E]) Where ¶ added in v0.3.28
func (b GenericsDo[T, E]) Where(conds ...Condition) T
Where ...
func (GenericsDo[T, E]) WithContext ¶ added in v0.3.28
func (b GenericsDo[T, E]) WithContext(ctx context.Context) T
WithContext ...
func (GenericsDo[T, E]) WriteDB ¶ added in v0.3.28
func (b GenericsDo[T, E]) WriteDB() T
WriteDB ...
type IGenericsDo ¶ added in v0.3.28
type IGenericsDo[T any, E any] interface { SubQuery Debug() T WithContext(ctx context.Context) T WithResult(fc func(tx Dao)) ResultInfo ReplaceDB(db *gorm.DB) ReadDB() T WriteDB() T As(alias string) Dao Session(config *gorm.Session) T Columns(cols ...field.Expr) Columns Clauses(conds ...clause.Expression) T Not(conds ...Condition) T Or(conds ...Condition) T Select(conds ...field.Expr) T Where(conds ...Condition) T Order(conds ...field.Expr) T Distinct(cols ...field.Expr) T Omit(cols ...field.Expr) T Join(table schema.Tabler, on ...field.Expr) T LeftJoin(table schema.Tabler, on ...field.Expr) T RightJoin(table schema.Tabler, on ...field.Expr) T Group(cols ...field.Expr) T Having(conds ...Condition) T Limit(limit int) T Offset(offset int) T Count() (count int64, err error) Scopes(funcs ...func(Dao) Dao) T Unscoped() T Create(values ...E) error CreateInBatches(values []E, batchSize int) error Save(values ...E) error First() (E, error) Take() (E, error) Last() (E, error) Find() ([]E, error) FindInBatch(batchSize int, fc func(tx Dao, batch int) error) (results []E, err error) FindInBatches(result *[]E, batchSize int, fc func(tx Dao, batch int) error) error Pluck(column field.Expr, dest interface{}) error Delete(...E) (info ResultInfo, err error) Update(column field.Expr, value interface{}) (info ResultInfo, err error) UpdateSimple(columns ...field.AssignExpr) (info ResultInfo, err error) Updates(value interface{}) (info ResultInfo, err error) UpdateColumn(column field.Expr, value interface{}) (info ResultInfo, err error) UpdateColumnSimple(columns ...field.AssignExpr) (info ResultInfo, err error) UpdateColumns(value interface{}) (info ResultInfo, err error) UpdateFrom(q SubQuery) Dao Attrs(attrs ...field.AssignExpr) T Assign(attrs ...field.AssignExpr) T Joins(fields ...field.RelationField) T Preload(fields ...field.RelationField) T FirstOrInit() (E, error) FirstOrCreate() (E, error) FindByPage(offset int, limit int) (result []E, count int64, err error) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) Rows() (*sql.Rows, error) Row() *sql.Row Scan(result interface{}) (err error) Returning(value interface{}, columns ...string) T UnderlyingDB() *gorm.DB schema.Tabler ToSQL(queryFn func(T)) string }
IGenericsDo generic query interface
type IWithDO ¶ added in v0.3.28
type IWithDO[T any] interface { // WithDO associates a DAO instance with the implementing type // - do: The DAO (Data Access Object) to be used for data operations // Returns a new instance of the implementing type with the DAO set // This enables fluent-style method chaining while maintaining type safety WithDO(do Dao) T }
IWithDO is a generic interface for types that can be associated with a DAO (Data Access Object) It enables method chaining by returning the concrete implementation type
type Logger ¶ added in v0.3.27
type Logger interface {
Println(v ...any)
}
Logger gen logger interface
type ResultInfo ¶ added in v0.1.21
ResultInfo query/execute info
type SubQuery ¶ added in v0.3.9
type SubQuery interface {
Condition
// contains filtered or unexported methods
}
SubQuery sub query interface
type WithDOFunc ¶ added in v0.3.28
WithDOFunc is a function type that implements the IWithDO interface through functional options. It provides a mechanism to inject DAO dependencies while maintaining type safety with generics.
The generic type T should be constrained to pointer receivers of concrete types to ensure proper value semantics and immutability.
func (WithDOFunc[T]) WithDO ¶ added in v0.3.28
func (b WithDOFunc[T]) WithDO(do Dao) T
WithDO executes the receiver function with the provided DAO instance, returning a new instance of type T. This method enables fluent chaining while preserving immutability by design.
Parameters:
do - Data Access Object instance (non-nil) to be injected
Returns:
New instance of type T initialized with the DAO dependency
Example:
constructor := func(do Dao) *UserService {
return &UserService{dao: do}
}
service := WithDOFunc[*UserService](constructor).WithDO(db)