Categorygithub.com/RivenZoo/sqlagent
modulepackage
1.4.1
Repository: https://github.com/rivenzoo/sqlagent.git
Documentation: pkg.go.dev

# README

Sqlagent

License: MIT Build Status Godoc

A golang sql wrap with sqlx and squirrel

Create instance with db config

cfg := dsncfg.Database{
    Host:     "localhost",
    Port:     3306,
    Name:     "test",
    Type:     "mysql",
    User:     "admin",
    Password: "passwd",
}
sa, err := NewSqlAgent(cfg)

Init with config file

$ ls
database.json
$ cat database.json
{
	"host":     "localhost",
	"port":     3306,
	"name":     "dbName",
	"type":     "mysql",
	"user":     "user",
	"password": "passwd"
}
InitFromConfig("database.json")

Init with env variable

  • DB_CONFIG set config file path
  • DB_LABEL set config file name pattern: database-$DB_LABEL.[json|yaml|yml]
$ echo $DB_CONFIG
/etc/database.yaml
$ ls /etc/database.yaml
/etc/database.yaml
$ pwd
/data/apps
$ echo $DB_LABEL
prod
$ ls ./config/database*
./config/database-prod.json
InitFromEnv()

Insert

insertBuilder := InsertBuilder(table).
		Columns("name", "uid").
		Values(userName, uid)
_, err := ExecContext(context.TODO(), insertBuilder)

Delete

delBuilder := DeleteBuilder(table).
    Where("name=?", userName)
res, err = ExecContext(context.TODO(), delBuilder)

Update

updateBuilder := UpdateBuilder(table).Where(sq.Eq{"name": oldUserName}).
        Set("name", userName)
res, err := ExecContext(context.TODO(), updateBuilder)

Select

selectBuilder := SelectBuilder("*").From(table).
    Where(sq.Eq{"name": userName})
userRes := []*tableUser{}
err = SelectContext(context.TODO(), selectBuilder, &userRes)

Use raw sqlx.DB

DB().SetMaxIdleConns(2)
DB().SetMaxOpenConns(4)

License

Sqlagent is released under the MIT License.

# Functions

Close SqlAgent inited by module init method.
DB return sqlx.DB held by module SqlAgent.
No description provided by the author
ExecContext exec sql built by sq.InsertBuilder/sq.UpdateBuilder/sq.DeleteBuilder and return result.
GetContext get one record by sql built by sq.SelectBuilder and scan to dest.
Init module SqlAgent with database config.
Init module SqlAgent with database config.
InitFromEnv use Env variable to detect config file and init SqlAgent with first found config file.
InsertBuilder return squirrel.InsertBuilder for table into into: insert table name.
No description provided by the author
ModelColumns use module sqlagent to extract model columns.
No description provided by the author
No description provided by the author
SelectContext get one or multi records by sql built by sq.SelectBuilder and scan to dest.
SetConnectionConfig set conenction for module sqlagent.
SetDBMapper set mapper for module sqlagent.
No description provided by the author
No description provided by the author
TxExecContext exec sql built by sq.InsertBuilder/sq.UpdateBuilder/sq.DeleteBuilder and return result.
TxGetContext get one record by sql built by sq.SelectBuilder and scan to dest.
TxSelectContext get one or multi records by sql built by sq.SelectBuilder and scan to dest.
No description provided by the author

# Structs

No description provided by the author