Beerus-DB: a database operation framework, currently only supports Mysql, Use [go-sql-driver/mysql] to do database connection and basic operations

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

https://beeruscc.com/beerusdb

Examples

No sql additions, deletions, update and select example

Query specified table data based on custom conditions

?", 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)">
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

Similar Resources

A decentralized, trusted, high performance, SQL database with blockchain features

A decentralized, trusted, high performance, SQL database with blockchain features

中文简介 CovenantSQL(CQL) is a Byzantine Fault Tolerant relational database built on SQLite: ServerLess: Free, High Availabile, Auto Sync Database Service

Jan 3, 2023

LBADD: An experimental, distributed SQL database

LBADD: An experimental, distributed SQL database

LBADD Let's build a distributed database. LBADD is an experimental distributed SQL database, written in Go. The goal of this project is to build a dat

Nov 29, 2022

A course to build the SQL layer of a distributed database.

TinySQL TinySQL is a course designed to teach you how to implement a distributed relational database in Go. TinySQL is also the name of the simplifed

Jan 8, 2023

This is a simple Golang application that executes SQL commands to clean up a mirror node's database.

This is a simple Golang application that executes SQL commands to clean up a mirror node's database.

Jan 24, 2022

A simple, fast, embeddable, persistent key/value store written in pure Go. It supports fully serializable transactions and many data structures such as list, set, sorted set.

NutsDB English | 简体中文 NutsDB is a simple, fast, embeddable and persistent key/value store written in pure Go. It supports fully serializable transacti

Jan 1, 2023

A tiny date object in Go. Tinydate uses only 4 bytes of memory

go-tinydate A tiny date object in Go. Tinydate uses 4 bytes of memory vs the 24 bytes of a standard time.Time{} A tinydate only has day precision. It

Mar 17, 2022

Kv-badgerdb - Simple BadgerDB driver for Kilovolt

BadgerDB driver for Kilovolt Simple BadgerDB driver for Kilovolt. Usage Usage is

Jan 28, 2022

TiDB is an open source distributed HTAP database compatible with the MySQL protocol

TiDB is an open source distributed HTAP database compatible with the MySQL protocol

Slack Channel Twitter: @PingCAP Reddit Mailing list: lists.tidb.io For support, please contact PingCAP What is TiDB? TiDB ("Ti" stands for Titanium) i

Jan 9, 2023

A MySQL-compatible relational database with a storage agnostic query engine. Implemented in pure Go.

go-mysql-server is a SQL engine which parses standard SQL (based on MySQL syntax) and executes queries on data sources of your choice. A simple in-memory database and table implementation are provided, and you can query any data source you want by implementing a few interfaces.

Dec 27, 2022
Comments
  • 加上多参数的支持

    加上多参数的支持

    现在在实际使用中遇到这样的一个情况

    做单表无SQL操作的时候,如果有这样一个条件需要设置,实现起来比较麻烦

    where id > ? and (name = ? or age > ?)
    

    v1.1.1 版本将会支持这个,我们可以这么做

    conditions := make([]*entity.Condition, 0)
    conditions = append(conditions, entity.GetCondition("id > ?", 10))
    conditions = append(conditions, entity.GetCondition("and (name = ? or age > ?)", "bee", 18)) // 重点看这句
    conditions = append(conditions, entity.GetCondition("order by id desc", entity.NotWhere))
    
    resultMap, err := operation.GetDBTemplate("dbPoolTest").Select("xt_message_board", conditions)
    
Go code for PostgreSQL. A Go language code which connects to PostgreSQL database for CRUD operation

Go code for PostgreSQL. A Go language code which connects to PostgreSQL database for CRUD operation

Jan 25, 2022
☄ The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct.
☄ The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct.

Gormat - Cross platform gopher tool The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct. 中文说明 Features Data

Dec 20, 2022
Owl is a db manager platform,committed to standardizing the data, index in the database and operations to the database, to avoid risks and failures.

Owl is a db manager platform,committed to standardizing the data, index in the database and operations to the database, to avoid risks and failures. capabilities which owl provides include Process approval、sql Audit、sql execute and execute as crontab、data backup and recover .

Nov 9, 2022
Virsas-mod-db - Quick way to init mysql, postgres and redis connection from multiple services without duplicating the code

Quick way to init mysql, postgres and redis connection from multiple services without duplicating the code.

Jan 23, 2022
Export output from pg_stat_activity and pg_stat_statements from Postgres into a time-series database that supports the Influx Line Protocol (ILP).

pgstat2ilp pgstat2ilp is a command-line program for exporting output from pg_stat_activity and pg_stat_statements (if the extension is installed/enabl

Dec 15, 2021
Simple key value database that use json files to store the database

KValDB Simple key value database that use json files to store the database, the key and the respective value. This simple database have two gRPC metho

Nov 13, 2021
A rudimentary implementation of a basic document (NoSQL) database in Go
A rudimentary implementation of a basic document (NoSQL) database in Go

tiedot Documentation Keywords: Golang, go, document database, NoSQL, JSON tiedot - Your NoSQL database powered by Golang tiedot is a document database

Dec 11, 2022
Dolt is a SQL database that you can fork, clone, branch, merge, push and pull just like a git repository.

Dolt is a SQL database that you can fork, clone, branch, merge, push and pull just like a git repository. Connect to Dolt just like any MySQL database to run queries or update the data using SQL commands. Use the command line interface to import CSV files, commit your changes, push them to a remote, or merge your teammate's changes.

Dec 31, 2022
DonutDB: A SQL database implemented on DynamoDB and SQLite

DonutDB: A SQL database implemented on DynamoDB and SQLite

Dec 21, 2022
CockroachDB - the open source, cloud-native distributed SQL database.
CockroachDB - the open source, cloud-native distributed SQL database.

CockroachDB is a cloud-native SQL database for building global, scalable cloud services that survive disasters. What is CockroachDB? Docs Quickstart C

Jan 2, 2023