Beerus-DB ·
Beerus-DB is a database operation framework, currently only supports Mysql, Use [go-sql-driver/mysql] to do database connection and basic operations, based on this do a lot of extensions, such as, connection pool management, multiple data sources, transaction management, single table no sql operation, multiple tables and complex operations can write their own sql, sql support {} placeholder, can use struct as parameters to operate the database, etc.
Installation
go get github.com/yuyenews/Beerus-DB
Documentation
Examples
No sql additions, deletions, update and select example
Query specified table data based on custom conditions
conditions := make([]*entity.Condition,0)
conditions = append(conditions, &entity.Condition{Key:"id > ?", Val: 10})
conditions = append(conditions, &entity.Condition{Key:"and user_name = ?", Val: "bee"})
conditions = append(conditions, &entity.Condition{Key: "order by create_time desc", Val: entity.NotWhere})
resultMap, err := operation.GetDBTemplate("Data source name").Select("table name", conditions)
Update data according to conditions
conditions := make([]*entity.Condition,0)
conditions = append(conditions, &entity.Condition{Key:"id = ?", Val: 1})
data := ResultStruct{UserName: "TestNoSqlUpdate"}
operation.GetDBTemplate("Data source name").Update("table name", dbutil.StructToMapIgnore(&data, data, true), conditions)
Deleting data based on conditions
conditions := make([]*entity.Condition,0)
conditions = append(conditions, &entity.Condition{Key:"id = ?", Val: 2})
_, err := operation.GetDBTemplate("Data source name").Delete("table name", conditions)
Insert data
data := ResultStruct{
UserName: "TestNoSqlInsert",
UserEmail: "[email protected]",
UpdateTime: "2021-12-09 13:50:00",
}
result, err := operation.GetDBTemplate("Data source name").Insert("table name", dbutil.StructToMapIgnore(&data, data, true))
Using sql to add, delete, update and select
Add, delete, update
sql can be any one of add, delete, modify
// with struct as parameter
res := ResultStruct{Id: 1, UserName: "TestUpdateByMap"}
operation.GetDBTemplate("Data source name").ExecByMap("update xt_message_board set user_name = {user_name} where id = {id}", dbutil.StructToMap(&res, res))
// Using arrays as parameters
param := make([]interface{}, 2)
param[0] = "TestUpdate"
param[1] = 1
operation.GetDBTemplate("Data source name").Exec("update xt_message_board set user_name = ? where id = ?", param)
Select
Support any query sql
// Using arrays as parameters
param := make([]interface{}, 1)
param[0] = 1
resultMap, err := operation.GetDBTemplate("Data source name").SelectList("select * from xt_message_board where id = ?", param)
// with struct as parameter
res := ResultStruct{Id: 1}
resultMap, err := operation.GetDBTemplate("Data source name").SelectListByMap("select * from xt_message_board where id < {id}", dbutil.StructToMap(&res, res))
Paging queries
data := ResultStruct{
UserName: "TestNoSqlInsert",
UserEmail: "[email protected]",
}
param := entity.PageParam{CurrentPage: 1, PageSize: 20, Params: dbutil.StructToMap(&data, data)}
result, err := operation.GetDBTemplate("Data source name").SelectPage("select * from xt_message_board where user_name = {user_name} and user_email = {user_email}", param)
Transaction Management
id, err := db.Transaction()
if err != nil {
t.Error("TestUpdateTx: " + err.Error())
return
}
res := ResultStruct{Id: 1, UserName: "TestUpdateTx"}
ss, err := operation.GetDBTemplateTx(id, "Data source name").ExecByTxMap("update xt_message_board set user_name = {user_name} where id = {id}", dbutil.StructToMap(&res, res))
if err != nil {
db.Rollback(id)
t.Error("Data source name: " + err.Error())
return
}
log.Println(ss.RowsAffected())
db.Commmit(id)
License
Beerus is MIT licensed