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.
go get github.com/Beerus-go/Beerus-DB@v1.1.5
go get github.com/go-sql-driver/mysql
Query specified table data based on custom conditions
conditions := builder.Create().
Add("id > ?", 10).
Add("and (user_name = ? or age > ?)", "bee", 18).
Add("order by create_time desc", entity.NotWhere).
Build()
resultMap, err := operation.GetDBTemplate("Data source name").Select("table name", conditions)
Update data according to conditions
// Conditions set
conditions := builder.Create().
Add("id = ?", 1).
Build()
// Data settings to be modified
data := ResultStruct{UserName: "TestNoSqlUpdate"}
// Execute the modification operation
result, err := operation.GetDBTemplate("Data source name").Update("table name", dbutil.StructToMapIgnore(&data, true),conditions)
Deleting data based on conditions
// Set delete conditions
conditions := builder.Create().
Add("id = ?", 2).
Build()
// Perform a delete operation
_, err := operation.GetDBTemplate("Data source name").Delete("table name", conditions)
Insert data
data := ResultStruct{
UserName: "TestNoSqlInsert",
UserEmail: "xxxxx@163.com",
UpdateTime: "2021-12-09 13:50:00",
}
result, err := operation.GetDBTemplate("Data source name").Insert("table name", dbutil.StructToMapIgnore(&data, true))
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))
// 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))
Paging queries
data := ResultStruct{
UserName: "TestNoSqlInsert",
UserEmail: "xxxxx@163.com",
}
param := entity.PageParam{CurrentPage: 1, PageSize: 20, Params: dbutil.StructToMap(&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))
if err != nil {
db.Rollback(id)
t.Error("Data source name: " + err.Error())
return
}
log.Println(ss.RowsAffected())
db.Commmit(id)
Beerus is MIT licensed