Documentation ¶
Index ¶
- func GetSelectorFromPOs(pos any) ([]string, error)
- type DAL
- type ForceIndexer
- type GDAL
- func (gdal *GDAL[PO, Where, Update]) Clauses(conds ...clause.Expression) *GDAL[PO, Where, Update]
- func (gdal *GDAL[PO, Where, Update]) Count(ctx context.Context, where *Where, options ...QueryOption) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) Create(ctx context.Context, po *PO) error
- func (gdal *GDAL[PO, Where, Update]) DB(options ...QueryOption) *gorm.DB
- func (gdal *GDAL[PO, Where, Update]) DBWithCtx(ctx context.Context, options ...QueryOption) *gorm.DB
- func (gdal *GDAL[PO, Where, Update]) Delete(ctx context.Context, where *Where) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) DeleteByID(ctx context.Context, id int64) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) Find(ctx context.Context, pos any, where any, options ...QueryOption) error
- func (gdal *GDAL[PO, Where, Update]) First(ctx context.Context, po any, where any, options ...QueryOption) error
- func (gdal *GDAL[PO, Where, Update]) MCreate(ctx context.Context, pos *[]*PO) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) MQuery(ctx context.Context, where *Where, options ...QueryOption) ([]*PO, error)
- func (gdal *GDAL[PO, Where, Update]) MQueryByIDs(ctx context.Context, ids []int64, options ...QueryOption) ([]*PO, error)
- func (gdal *GDAL[PO, Where, Update]) MQueryByPaging(ctx context.Context, where *Where, limit *int64, offset *int64, order *string, ...) ([]*PO, int64, error)
- func (gdal *GDAL[PO, Where, Update]) MQueryByPagingOpt(ctx context.Context, where *Where, options ...QueryOption) ([]*PO, int64, error)
- func (gdal *GDAL[PO, Where, Update]) MSave(ctx context.Context, pos *[]*PO) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) MUpdate(ctx context.Context, where *Where, update *Update) (int64, error)
- func (gdal *GDAL[PO, Where, Update]) MakePO() PO
- func (gdal *GDAL[PO, Where, Update]) QueryByID(ctx context.Context, id int64, options ...QueryOption) (*PO, error)
- func (gdal *GDAL[PO, Where, Update]) QueryFirst(ctx context.Context, where *Where, options ...QueryOption) (*PO, error)
- func (gdal *GDAL[PO, Where, Update]) Save(ctx context.Context, po *PO) error
- func (gdal *GDAL[PO, Where, Update]) TableName() string
- func (gdal *GDAL[PO, Where, Update]) Update(ctx context.Context, where *Where, update *Update) error
- func (gdal *GDAL[PO, Where, Update]) UpdateByID(ctx context.Context, id int64, update *Update) error
- func (gdal *GDAL[PO, Where, Update]) WithTx(tx *gorm.DB) *GDAL[PO, Where, Update]
- type InjectDefaulter
- type QueryConfig
- type QueryOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetSelectorFromPOs ¶
GetSelectorFromPOs read pos read gorm tag from pos, then build []string of selector
Types ¶
type DAL ¶
type DAL interface { Create(ctx context.Context, po any) error Save(ctx context.Context, po any) (int64, error) Delete(ctx context.Context, po any, where any) (int64, error) Update(ctx context.Context, po any, where any, update any) (int64, error) Find(ctx context.Context, po any, where any, options ...QueryOption) (err error) First(ctx context.Context, po, where any, options ...QueryOption) error Count(ctx context.Context, po any, where any, options ...QueryOption) (int32, error) Exist(ctx context.Context, po any, where any, options ...QueryOption) (bool, error) DBWithCtx(ctx context.Context, options ...QueryOption) *gorm.DB DB(options ...QueryOption) *gorm.DB }
DAL Data Access Layer.
type ForceIndexer ¶
type ForceIndexer interface {
ForceIndex() string
}
ForceIndexer assign force index for Where. no force index when return "" or unimplemented.
⚠️ WARNING: please implement ForceIndexer for Where in spite of *Where
type GDAL ¶
GDAL Generic Data Access Layer. Business DAL should embed *GDAL,and assign PO、Where and Update struct,so that you can extend or override the methods as you will.
💡 HINT: *Where can implement interface InjectDefaulter so that we can inject the customized default value into struct `where` when you query or update.
💡 HINT: Where can implement interface ForceIndexer, if so, we will force index when you query.
⚠️ WARNING: PO must implement interface Tabler and set the corresponding table name.
⚠️ WARNING: Fields of PO mapping to column must include tag `gorm:"column:{{column_name}}"` where `{{}}` represents placeholder, so we can know which columns you need.
🚀 example:
type User struct { ID int64 `gorm:"column:id"` Name string `gorm:"column:name"` Age uint `gorm:"column:age"` Birthday *time.Time `gorm:"column:birthday"` CompanyID *int `gorm:"column:company_id"` ManagerID *uint `gorm:"column:manager_id"` Active bool `gorm:"column:active"` CreateTime time.Time `gorm:"column:create_time"` UpdateTime time.Time `gorm:"column:update_time"` IsDeleted bool `gorm:"column:is_deleted"` } func (u User) TableName() string { return "user" } type UserWhere struct { ID *int64 `sql_field:"id"` Name *string `sql_field:"name"` Age *uint `sql_field:"age"` CompanyID *int `sql_field:"company_id"` ManagerID *uint `sql_field:"manager_id"` Active *bool `sql_field:"active"` IsDeleted *bool `sql_field:"is_deleted"` BirthdayGE *time.Time `sql_field:"birthday" sql_operator:">="` BirthdayLT *time.Time `sql_field:"birthday" sql_operator:"<"` CreateTimeGE *time.Time `sql_field:"create_time" sql_operator:">="` CreateTimeLT *time.Time `sql_field:"create_time" sql_operator:"<"` UpdateTimeGE *time.Time `sql_field:"update_time" sql_operator:">="` UpdateTimeLT *time.Time `sql_field:"update_time" sql_operator:"<"` CompanyIDIn []int `sql_field:"company_id" sql_operator:"in"` ManagerIDIn []uint `sql_field:"manager_id" sql_operator:"in"` } func (where UserWhere) ForceIndex() string { if where.ID != nil { return "id" } return "" } func (where *UserWhere) InjectDefault() { where.IsDeleted = gptr.Of(false) } type UserUpdate struct { ID *int64 `sql_field:"id"` Name *string `sql_field:"name"` Age *uint `sql_field:"age"` Birthday *time.Time `sql_field:"birthday"` CompanyID *int `sql_field:"company_id"` ManagerID *uint `sql_field:"manager_id"` Active *bool `sql_field:"active"` CreateTime *time.Time `sql_field:"create_time"` UpdateTime *time.Time `sql_field:"update_time"` IsDeleted *bool `sql_field:"is_deleted"` } type UserDAL struct { *gdal.GDAL[model.User, model.UserWhere, model.UserUpdate] }
func (*GDAL[PO, Where, Update]) Clauses ¶ added in v0.1.8
func (gdal *GDAL[PO, Where, Update]) Clauses(conds ...clause.Expression) *GDAL[PO, Where, Update]
Clauses generate a new GDAL with clause supplemented.
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) Count ¶
func (gdal *GDAL[PO, Where, Update]) Count(ctx context.Context, where *Where, options ...QueryOption) (int64, error)
Count
💡 HINT:
⚠️ WARNING:
🚀 example:
where := &tests.UserWhere{ Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } count, err := UserDAL.Count(ctx, where)
SQL: SELECT count(*) FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= "1999-01-01 00:00:00" and `birthday` < "2019-01-01 00:00:00"
func (*GDAL[PO, Where, Update]) Create ¶
Create insert a single record.
💡 HINT: the po should be a pointer so that we can inject the returning primary key.
💡 HINT: if you want to insert multiple records, use MCreate.
🚀 example:
user := tests.User{ Name: "Ella", Age: 17, Birthday: gptr.Of(time.Date(1999, 1, 1, 1, 0, 0, 0, time.Local)), CompanyID: gptr.Of(110), ManagerID: gptr.Of[uint](210), Active: true, CreateTime: time.Now(), UpdateTime: time.Now(), IsDeleted: false, } UserDAL.Create(ctx, &user)
SQL: INSERT INTO `user` (`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted`) VALUES ("Ella",17,"1999-01-01 01:00:00",110,210,true,"2023-06-11 09:38:14.483","2023-06-11 09:38:14.483",false) RETURNING `id`Ï
func (*GDAL[PO, Where, Update]) DB ¶
func (gdal *GDAL[PO, Where, Update]) DB(options ...QueryOption) *gorm.DB
DB get embedded DB
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) DBWithCtx ¶
func (gdal *GDAL[PO, Where, Update]) DBWithCtx(ctx context.Context, options ...QueryOption) *gorm.DB
DBWithCtx get embedded DB with context
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) Delete ¶
Delete deletes physically by condition
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) DeleteByID ¶
DeleteByID deletes physically by primary key
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) Find ¶
func (gdal *GDAL[PO, Where, Update]) Find(ctx context.Context, pos any, where any, options ...QueryOption) error
Find query by condition with paging options.
💡 HINT: this is the most primary multiple query in GDAL, on which other multiple query methods are based.
⚠️ WARNING: `pos` must be a pointer to (sub-)persistent objects. Fields mapping to column must include tag `gorm:"column:{{column_name}}"` where `{{}}` represents placeholder, so we can know which columns you need.
🚀 example:
var users []User where := &UserWhere { Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } err := userDAL.Find(ctx, &users, where, gdal.WithLimit(10), gdal.WithOrder("birthday"))
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= '1999-01-01 00:00:00' and `birthday` < '2019-01-01 00:00:00' ORDER BY birthday LIMIT 10
func (*GDAL[PO, Where, Update]) First ¶
func (gdal *GDAL[PO, Where, Update]) First(ctx context.Context, po any, where any, options ...QueryOption) error
First query the first record by condition
💡 HINT: this is the most primary single query in GDAL, on which other single query methods are based.
⚠️ WARNING: `po` must be a pointer to a (sub-)persistent object. Fields mapping to column must include tag `gorm:"column:{{column_name}}"` where `{{}}` represents placeholder, so we can know which columns you need.
🚀 example:
var user User where := &UserWhere { Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } err := userDAL.First(ctx, &user, where, gdal.WithOrder("birthday"))
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= '1999-01-01 00:00:00' and `birthday` < '2019-01-01 00:00:00' ORDER BY birthday LIMIT 1
func (*GDAL[PO, Where, Update]) MCreate ¶
MCreate insert multiple records.
💡 HINT: the pos should be a pointer to slice so that we can inject the returning primary keys.
🚀 example:
users := []*tests.User{ GetUser("find"), GetUser("find"), GetUser("find"), } _, err := UserDAL.MCreate(ctx, &users)
SQL: INSERT INTO `user` (`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted`) VALUES ("find",18,"2023-06-11 09:38:14",NULL,NULL,false,"2023-06-11 09:38:14.484","2023-06-11 09:38:14.484",false), ("find",18,"2023-06-11 09:38:14",NULL,NULL,false,"2023-06-11 09:38:14.484","2023-06-11 09:38:14.484",false), ("find",18,"2023-06-11 09:38:14",NULL,NULL,false,"2023-06-11 09:38:14.484","2023-06-11 09:38:14.484",false) RETURNING `id`
func (*GDAL[PO, Where, Update]) MQuery ¶
func (gdal *GDAL[PO, Where, Update]) MQuery(ctx context.Context, where *Where, options ...QueryOption) ([]*PO, error)
MQuery query by condition with paging options.
💡 HINT: When you just need complete persistent objects by condition, this method is what you want.
🚀 example:
where := &UserWhere { Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } users, err := userDAL.MQuery(ctx, where, gdal.WithLimit(10), gdal.WithOrder("birthday"))
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= '1999-01-01 00:00:00' and `birthday` < '2019-01-01 00:00:00' ORDER BY birthday LIMIT 10
func (*GDAL[PO, Where, Update]) MQueryByIDs ¶
func (gdal *GDAL[PO, Where, Update]) MQueryByIDs(ctx context.Context, ids []int64, options ...QueryOption) ([]*PO, error)
MQueryByIDs query by primary keys.
💡 HINT: When you just need complete persistent objects by primary key list, this method is what you want.
⚠️ WARNING: nothing returns when ids is empty slice.å
🚀 example:
users, err := userDAL.MQueryByIDs(ctx, gslice.Of(123, 456, 789), gdal.WithLimit(10), gdal.WithOrder("birthday"))
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `id` in (123, 456, 789) ORDER BY birthday LIMIT 10
func (*GDAL[PO, Where, Update]) MQueryByPaging ¶
func (gdal *GDAL[PO, Where, Update]) MQueryByPaging(ctx context.Context, where *Where, limit *int64, offset *int64, order *string, options ...QueryOption) ([]*PO, int64, error)
MQueryByPaging query by paging.
💡 HINT: ref Count and Find
⚠️ WARNING: the second return is the number of total records satisfy where condition in spite of limit and offset.
🚀 example:
where := &tests.UserWhere{ Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } users, total, err := UserDAL.MQueryByPaging(ctx, where, gptr.Of(10), nil, gptr.Of("birthday"))
SQL: SELECT count(*) FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= "1999-01-01 00:00:00" and `birthday` < "2019-01-01 00:00:00" SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= "1999-01-01 00:00:00" and `birthday` < "2019-01-01 00:00:00" ORDER BY birthday LIMIT 10
func (*GDAL[PO, Where, Update]) MQueryByPagingOpt ¶
func (gdal *GDAL[PO, Where, Update]) MQueryByPagingOpt(ctx context.Context, where *Where, options ...QueryOption) ([]*PO, int64, error)
MQueryByPagingOpt query by paging options.
💡 HINT: ref Count and Find
⚠️ WARNING: the second return is the number of total records satisfy where condition in spite of limit and offset.
🚀 example:
where := &tests.UserWhere{ Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } users, total, err := UserDAL.MQueryByPagingOpt(ctx, where, gdal.WithLimit(10), gdal.WithOrder("birthday"))
SQL: SELECT count(*) FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= "1999-01-01 00:00:00" and `birthday` < "2019-01-01 00:00:00" SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= "1999-01-01 00:00:00" and `birthday` < "2019-01-01 00:00:00" ORDER BY birthday LIMIT 10
func (*GDAL[PO, Where, Update]) MSave ¶ added in v0.1.10
MSave saves multiple records, and return success count
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) MUpdate ¶
func (gdal *GDAL[PO, Where, Update]) MUpdate(ctx context.Context, where *Where, update *Update) (int64, error)
MUpdate updates multiple records by condition, return success count
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) QueryByID ¶
func (gdal *GDAL[PO, Where, Update]) QueryByID(ctx context.Context, id int64, options ...QueryOption) (*PO, error)
QueryByID query the record by primary key
⚠️ WARNING: if primary key is not exist, return nil pointer.
🚀 example:
users, err := UserDAL.QueryByID(ctx, 123)
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `id` = 123 ORDER BY `user`.`id` LIMIT 1
func (*GDAL[PO, Where, Update]) QueryFirst ¶
func (gdal *GDAL[PO, Where, Update]) QueryFirst(ctx context.Context, where *Where, options ...QueryOption) (*PO, error)
QueryFirst query the first record by condition.
💡 HINT: ref First.
🚀 example:
var user User where := &UserWhere { Active: gptr.Of(true), BirthdayGE: gptr.Of(time.Date(1999, 1, 1, 0, 0, 0, 0, time.Local)), BirthdayLT: gptr.Of(time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)), } user, err := userDAL.QueryFirst(ctx, where, gdal.WithOrder("birthday"))
SQL: SELECT `id`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`create_time`,`update_time`,`is_deleted` FROM `user` WHERE `active` = true and `is_deleted` = false and `birthday` >= '1999-01-01 00:00:00' and `birthday` < '2019-01-01 00:00:00' ORDER BY birthday LIMIT 1
func (*GDAL[PO, Where, Update]) Save ¶ added in v0.1.10
Save saves single record
💡 HINT:
⚠️ WARNING:
🚀 example:
func (*GDAL[PO, Where, Update]) Update ¶
func (gdal *GDAL[PO, Where, Update]) Update(ctx context.Context, where *Where, update *Update) error
Update updates records by condition
💡 HINT:
⚠️ WARNING:
🚀 example:
type InjectDefaulter ¶
type InjectDefaulter interface {
InjectDefault()
}
InjectDefaulter inject default for *Where
⚠️ WARNING: please implement InjectDefaulter for *Where in spite of Where
type QueryConfig ¶
type QueryConfig struct { // export field Limit *int Offset *int Order *string Selects []string // contains filtered or unexported fields }
func MakeQueryConfig ¶
func MakeQueryConfig(options []QueryOption) *QueryConfig
MakeQueryConfig convert options to QueryConfig
type QueryOption ¶
type QueryOption func(v *QueryConfig)
func WithMaster ¶
func WithMaster() QueryOption
WithMaster read master
💡 HINT:
⚠️ WARNING:
🚀 example:
func WithSelects ¶
func WithSelects(selects []string) QueryOption
WithSelects assign selected columns
💡 HINT:
⚠️ WARNING:
🚀 example: