A simple sql wrapper provides convenient CRUD operations for struct objects.
-
Column name is converted from field name with CamelToSnake pattern by default
-
Custom column name can be declared with
sql
tag -
primary key
,auto_increment
are supported in db tag -
Use `sql:"-"` to ignore fields
-
Column must be field which can be exported
type Product struct { ID int `sql:"primary key,auto_increment"` Name string Price float32 Text string `sql:"txt"` UpdatedAt int64 Ext interface{} `sql:"-"' }
-
Default table name is the plural form of struct name.
type Product struct
's table name isproducts
type User struct
's table name isusers
-
Custom table name is provided by
TableName
methodtype User struct { ID int `sql:"primary key"` Name string } func (u *User) TableName() string { return "users" + fmt.Sprint(u.id%100) }
db, err := NewDBWrapper("mysql", "dbuser:dbpassword@tcp(localhost:3306)/dbname")
...
p := &Product{
Name: "apple",
Price: 0.1,
Text: "nice",
UpdatedAt: time.Now().Unix(),
}
db.Insert(p)
p.Price = 0.2
db.Update(p)
Save is supported by mysql and sqlite3 drivers. It will insert the record if it does't exist, otherwise update the record.
p.Price = 0.3
db.Save(p)
p = &Product{
Name: "apple",
Price: 0.1,
Text: "nice",
UpdatedAt: time.Now().Unix(),
}
db.Save(p)
var products []*Product
//Select all products
db.Select(&products)
//Select products whose price is less than 0.2
db.Select(&products, "price<?", 0.2)
var p1 *Product
db.SelectOne(&p1)
var p2 Product
db.SelectOne(&p2, "id=?", 3)
db.Table("products").Insert(p)
tx, err := db.Begin()
tx.Insert(p1)
tx.Insert(p2)
tx.Table("products").Insert(p3)
tx.Commit()
type Product struct {
ID int `sql:"primary key,auto_increment"`
Name string
Price float32
Text string `sql:"txt"`
UpdatedAt int64
}
type ProductDetail struct {
Product
Detail string
}
type Coordinate struct {
Lng float
Lat float
}
type Topic struct {
ID int64
Title string
Location *Coordinate `sql:"json"`
}