Documentation ¶
Index ¶
- Constants
- func GetEnv[V any](key string, init V) V
- func SetValue(model any, key string, data any) (err error)
- func ToString(data interface{}) (res string)
- type Column
- type DB
- type DBModel
- func (db *DBModel) Begin() *DBModel
- func (db *DBModel) Commit() error
- func (db *DBModel) Create(model any) (err error)
- func (db *DBModel) Delete(model any, args ...any) (err error)
- func (db *DBModel) Fetch(offset, fetch int) *DBModel
- func (db *DBModel) Find(model any, params ...any) (total int, err error)
- func (db *DBModel) First(model any) (err error)
- func (db *DBModel) Get(model any, getType GetOne) (err error)
- func (db *DBModel) GroupBy(fields ...string) *DBModel
- func (db *DBModel) Having(field any, opt fluentsql.WhereOpt, value any) *DBModel
- func (db *DBModel) Join(join fluentsql.JoinType, table string, condition fluentsql.Condition) *DBModel
- func (db *DBModel) Last(model any) (err error)
- func (db *DBModel) Limit(limit, offset int) *DBModel
- func (db *DBModel) Model(model any) *DBModel
- func (db *DBModel) Omit(columns ...any) *DBModel
- func (db *DBModel) OrderBy(field string, dir fluentsql.OrderByDir) *DBModel
- func (db *DBModel) Raw(sqlStr string, args ...any) *DBModel
- func (db *DBModel) RemoveFetch() fluentsql.Fetch
- func (db *DBModel) RemoveLimit() fluentsql.Limit
- func (db *DBModel) Rollback() error
- func (db *DBModel) Select(columns ...any) *DBModel
- func (db *DBModel) Take(model any, args ...any) (err error)
- func (db *DBModel) Update(model any) (err error)
- func (db *DBModel) When(condition bool, groupCondition fluentsql.FnWhereBuilder) *DBModel
- func (db *DBModel) Where(field any, opt fluentsql.WhereOpt, value any) *DBModel
- func (db *DBModel) WhereGroup(groupCondition fluentsql.FnWhereBuilder) *DBModel
- func (db *DBModel) WhereOr(field any, opt fluentsql.WhereOpt, value any) *DBModel
- type GetOne
- type MetaData
- type Raw
- type Table
Constants ¶
const ( MODEL = "model" // Tag `model` TABLE = "table" // Table name TYPE = "type" // Column types REFERENCE = "ref" // Column reference CASCADE = "cascade" // Column cascade DELETE, UPDATE RELATION = "rel" // Column relationship NAME = "name" // Column name )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Column ¶
type Column struct { Key string Name string Primary bool Types string Ref string // Reference id to table Relation string // Relation to table IsZero bool // Keep Zero value of type HasValue bool }
Column structure
type DBModel ¶
type DBModel struct {
// contains filtered or unexported fields
}
func (*DBModel) Create ¶
Create add new data for table via model type Slice, Struct, *Struct
------ Insert a user ------ user := &User{ Name: "Vinh", Age: 42, } err = db.Create(user) if err != nil { log.Fatal(err) } log.Printf("User ID: %d", user.Id) ------ Insert slice (multi) users ------ var users []*User users = append(users, &User{ Name: "John", Age: 39, }) users = append(users, &User{ Name: "Kite", Age: 42, }) err = db.Create(users) if err != nil { log.Fatal(err) } for _, user := range users { log.Printf("User ID: %d", user.Id) } ------ Insert Map ------ user = &User{} err = db.Model(user).Create(map[string]interface{}{ "Name": "Toi Lest", "Age": 39, }) if err != nil { log.Fatal(err) } log.Printf("User ID: %d", user.Id)
func (*DBModel) Delete ¶
Delete perform delete data for table via model type Struct, *Struct
-------- Delete via Model --------
var user User err = db.First(&user) err = db.Delete(user) if err != nil { log.Fatal(err) }
-------- Delete via ID --------
err = db.Delete(User{}, 157) if err != nil { log.Fatal(err) } -------- Delete via List ID -------- err = db.Delete(User{}, []int{154, 155, 156}) if err != nil { log.Fatal(err) }
-------- Delete via Where condition --------
err = db.Where("Id", fluentsql.Eq, 153).Delete(&User{}) if err != nil { log.Fatal(err) }
func (*DBModel) First ¶
First get the first record ordered by primary key
Example
-------- Query a First --------
var user User err = db.First(&user)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user)
-------- Query a First by ID --------
var user3 User err = db.First(&user3, 103)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user3) var user4 User user4 = User{Id: 103} err = db.First(&user4)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user4)
-------- Query a First by Model --------
var user5 User err = db.Model(User{Id: 102}).First(&user5)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user5)
-------- Query a First by Where --------
var user6 User err = db.Where("Id", fluentsql.Eq, 100).First(&user6)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user6)
-------- Query a First by WhereGroup --------
var user7 User err = db.Where("Id", fluentsql.Eq, 100).
WhereGroup(func(query fluentsql.WhereBuilder) *fluentsql.WhereBuilder { query.Where("age", fluentsql.Eq, 42). WhereOr("age", fluentsql.Eq, 39) return &query }).First(&user7) if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user7)
func (*DBModel) Join ¶
func (db *DBModel) Join(join fluentsql.JoinType, table string, condition fluentsql.Condition) *DBModel
Join builder
func (*DBModel) Last ¶
Last last record, ordered by primary key desc
Example
-------- Query a Last --------
var user1 User err = db.Select("name").Last(&user1)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user1)
func (*DBModel) OrderBy ¶
func (db *DBModel) OrderBy(field string, dir fluentsql.OrderByDir) *DBModel
OrderBy builder
func (*DBModel) Take ¶
Take get one record, no specified order
Example
-------- Query a Take --------
var user2 User err = db.Take(&user2)
if err != nil { log.Fatal(err) }
log.Printf("User %v\n", user2)
func (*DBModel) Update ¶
Update modify data for table via model type Struct, *Struct
// -------- Update from Struct -------- var user User err = db.First(&user) user.Name = sql.NullString{ String: "Cat John", Valid: true, } err = db.Update(user) if err != nil { log.Fatal(err) } log.Printf("User %v\n", user) var user1 User err = db.First(&user1) user1.Name = sql.NullString{ String: "Cat John", Valid: true, } user1.Age = 100 err = db. Where("id", fluentsql.Eq, 1). Update(user1) if err != nil { log.Fatal(err) } log.Printf("User %v\n", user1) // -------- Update from Map -------- var user2 User err = db.First(&user2) err = db.Model(&user2). Omit("Name"). Update(map[string]interface{}{"Name": "Tah Go Tab x3", "Age": 88}) if err != nil { log.Fatal(err) } log.Printf("User %v\n", user2)
func (*DBModel) When ¶ added in v1.2.1
func (db *DBModel) When(condition bool, groupCondition fluentsql.FnWhereBuilder) *DBModel
When checking TRUE to build Where condition.
func (*DBModel) WhereGroup ¶
func (db *DBModel) WhereGroup(groupCondition fluentsql.FnWhereBuilder) *DBModel
WhereGroup combine multi where conditions into a group.