Menu

Tree [817017] master v1.1.3 /
 History

HTTPS access


File Date Author Commit
 commons 2022-01-21 yuyenews yuyenews [817017] Add ConditionBuilder to simplify the complexity...
 db 2021-12-09 yuyenews yuyenews [d699d0] init commit
 operation 2021-12-18 yuyenews yuyenews [9937f9] Condition adds support for multiple parameters
 pool 2021-12-09 yuyenews yuyenews [b650a5] init commit
 test 2022-01-21 yuyenews yuyenews [817017] Add ConditionBuilder to simplify the complexity...
 LICENSE 2021-12-06 yuyenews yuyenews [52b3e3] init commit
 README.md 2022-01-21 yuyenews yuyenews [817017] Add ConditionBuilder to simplify the complexity...
 go.mod 2021-12-06 yuyenews yuyenews [7c4c11] init commit
 go.sum 2021-12-08 yuyenews yuyenews [b20823] init commit
 push.sh 2021-12-11 yuyenews yuyenews [8b012a] init commit

Read Me

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@v1.1.3

go get github.com/go-sql-driver/mysql

Documentation

https://beeruscc.com/beerusdb

Examples

No sql additions, deletions, update and select example

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))

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))

// 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)

License

Beerus is MIT licensed