tsgmysqlutils

package module
v0.0.0-...-db5fa8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2018 License: MIT Imports: 8 Imported by: 0

README

Go MySQL utils

Two main functions: database operation and automatic ORM(object(struct) relational mapping).

Installation
$ go get -u github.com/typa01/go-utils
$ go get -u github.com/typa01/go-mysql-utils
Usage
1. Create a new mysql client:
func TestDbClient() *DBClient {
	var dbConfig tsgmysqlutils.DBConfig
	dbConfig.DbHost = "127.0.0.1"
	dbConfig.DbUser = "root"
	dbConfig.DbPass = "123456"
	dbConfig.IsLocalTime = true
	dbConfig.DbName = "test"
	return tsgmysqlutils.NewDbClient(dbConfig)
}
2. Create Object(struct) Relational Mapping:
func TestGenerateORM(t *testing.T) {
	client := TestDbClient()
	orm := tsgmysqlutils.NewORMGenerator(client)
	orm.AddComment = true
	tabNames := []string{"we_test_tab1", "we_test_tab2"}
	orm.DefaultGenerator(tabNames)
	client.CloseConn()
}
More info see:
See the client operation: mysql_test.go
See the orm result: mysql_test_assist.go
https://blog.csdn.net/typa01_kk/article/category/7629914

Documentation

Index

Constants

View Source
const (
	MySQL                     = "MySQL"
	SlowSqlTimeoutMillisecond = 2000
	ConnDBTimeoutMillisecond  = 2000
)

Variables

View Source
var DBGoTypes = map[string]string{
	"TINYINT":    "int64",
	"SMALLINT":   "int64",
	"MEDIUMINT":  "int64",
	"INT":        "int64",
	"BIGINT":     "int64",
	"DECIMAL":    "float64",
	"FLOAT":      "float64",
	"DOUBLE":     "float64",
	"NUMERIC":    "float64",
	"CHAR":       "string",
	"VARCHAR":    "string",
	"BINARY":     "string",
	"VARBINARY":  "string",
	"BLOB":       "string",
	"TINYTEXT":   "string",
	"TEXT":       "string",
	"MEDIUMTEXT": "string",
	"LONGTEXT":   "string",
	"ENUM":       "string",
	"SET":        "string",
	"DATE":       "time.Time",
	"DATETIME":   "time.Time",
	"TIMESTAMP":  "time.Time",
}
View Source
var ORMTabsCols []ORMTable

Functions

func GetConn

func GetConn(config DBConfig) *db.DB

Get a MySQL connection

func PrintErrorSql

func PrintErrorSql(err error, sql string, args ...interface{})

func PrintSlowConn

func PrintSlowConn(driverName, host, dbName string, consume int64)

func PrintSlowSql

func PrintSlowSql(host, dbName string, consume int64, sql string, args ...interface{})

Types

type DBClient

type DBClient struct {
	Config DBConfig
	Db     *db.DB
}

func NewDbClient

func NewDbClient(config DBConfig) *DBClient

Get a MySQL client

func TestDbClient

func TestDbClient() *DBClient

func (*DBClient) CloseConn

func (client *DBClient) CloseConn()

Close MySQL connection

func (*DBClient) CloseStmt

func (client *DBClient) CloseStmt(stmt *db.Stmt)

Close MySQL statement

func (*DBClient) Exec

func (client *DBClient) Exec(sql string, args ...interface{}) (result int64, err error)

Modify database table info or data

func (*DBClient) GetStmt

func (client *DBClient) GetStmt(sql string) (stmt *db.Stmt, err error)

Get MySQL statement

func (*DBClient) GetTxStmt

func (client *DBClient) GetTxStmt(tx *db.Tx, sql string) (stmt *db.Stmt, err error)

Get MySQL statement,transaction

func (*DBClient) QueryAggregate

func (client *DBClient) QueryAggregate(sql string, args ...interface{}) (aggregate int64, err error)

Database aggregate function, eg: SUM(*),COUNT(*) etc.

func (*DBClient) QueryDBInfo

func (client *DBClient) QueryDBInfo() *db.Rows

Get database information

func (*DBClient) QueryList

func (client *DBClient) QueryList(orm ORMBase, sql string, args ...interface{}) (rows *db.Rows, err error)

Get database table multiple rows data

func (*DBClient) QueryMetaData

func (client *DBClient) QueryMetaData(tabName string) *db.Rows

Get database table metadata

func (*DBClient) QueryRow

func (client *DBClient) QueryRow(orm ORMBase, sql string, args ...interface{}) (row *db.Row, err error)

Get database table a row data

func (*DBClient) TxBegin

func (client *DBClient) TxBegin() (tx *db.Tx, err error)

Begin the transaction

func (*DBClient) TxCommit

func (client *DBClient) TxCommit(tx *db.Tx) bool

Commit the transaction

func (*DBClient) TxExec

func (client *DBClient) TxExec(tx *db.Tx, sql string, args ...interface{}) (result int64, err error)

Modify database table info or data,transaction

func (*DBClient) TxQueryAggregate

func (client *DBClient) TxQueryAggregate(tx *db.Tx, sql string, args ...interface{}) (aggregate int64, err error)

Database aggregate function, eg: SUM(*),COUNT(*) etc,transaction

func (*DBClient) TxQueryList

func (client *DBClient) TxQueryList(tx *db.Tx, orm ORMBase, sql string, args ...interface{}) (rows *db.Rows, err error)

Get database table multiple rows data,transaction

func (*DBClient) TxQueryRow

func (client *DBClient) TxQueryRow(tx *db.Tx, orm ORMBase, sql string, args ...interface{}) (row *db.Row, err error)

Get database table a row data,transaction

func (*DBClient) TxRollback

func (client *DBClient) TxRollback(tx *db.Tx)

Rollback the transaction

type DBConfig

type DBConfig struct {
	DbHost string
	DbUser string
	DbPass string
	DbName string
	// if false, local default UTC +0
	IsLocalTime bool
}

MySQL database connect configuration

type ORMBase

type ORMBase interface {
	RowToStruct(row *db.Row) error
	RowsToStruct(rows *db.Rows) error
}

type ORMColumn

type ORMColumn struct {
	CName    string
	CType    string
	CComment string
}

type ORMGenerator

type ORMGenerator struct {
	Client     *DBClient
	AddComment bool
}

func NewORMGenerator

func NewORMGenerator(client *DBClient) *ORMGenerator

func (*ORMGenerator) DefaultGenerator

func (orm *ORMGenerator) DefaultGenerator(tabName []string)

func (*ORMGenerator) ORMBuilder

func (orm *ORMGenerator) ORMBuilder(tabNames []string)

type ORMTable

type ORMTable struct {
	TName    string
	TComment string
	TColumns []ORMColumn
}

type WeTestTab1

type WeTestTab1 struct {
	Id           int64        `column:"id"`            // The primary key id
	Name         string       `column:"name"`          // The user name
	Gender       int64        `column:"gender"`        // The user gerder, 1:male 2:female 0:default
	Birthday     time.Time    `column:"birthday"`      // The user birthday, eg: 2018-04-16
	Stature      float64      `column:"stature"`       // The user stature, eg: 172.22cm
	Weight       float64      `column:"weight"`        // The user weight, eg: 21.77kg
	CreatedTime  time.Time    `column:"created_time"`  // created time
	ModifiedTime time.Time    `column:"modified_time"` // record time
	IsDeleted    int64        `column:"is_deleted"`    // Logic to delete(0:normal 1:deleted)
	WeTestTab1s  []WeTestTab1 // This value is used for batch queries and inserts.
}

test table1

func (*WeTestTab1) BatchInsert

func (weTestTab1 *WeTestTab1) BatchInsert(client *DBClient, idSet, returnIds bool) ([]int64, error)

func (*WeTestTab1) DeleteWeTestTab1ById

func (weTestTab1 *WeTestTab1) DeleteWeTestTab1ById(client *DBClient) (int64, error)

func (*WeTestTab1) Insert

func (weTestTab1 *WeTestTab1) Insert(client *DBClient, idSet bool) (int64, error)

func (*WeTestTab1) RowToStruct

func (weTestTab1 *WeTestTab1) RowToStruct(row *db.Row) error

func (*WeTestTab1) RowsToStruct

func (weTestTab1 *WeTestTab1) RowsToStruct(rows *db.Rows) error

func (*WeTestTab1) UpdateWeTestTab1ById

func (weTestTab1 *WeTestTab1) UpdateWeTestTab1ById(client *DBClient) (int64, error)

type WeTestTab2

type WeTestTab2 struct {
	Id                 int64        `column:"id"`                  // The primary key id
	UserId             int64        `column:"user_id"`             // The user id
	AreaCode           int64        `column:"area_code"`           // The user area code
	Phone              int64        `column:"phone"`               // The user phone
	Email              string       `column:"email"`               // The user email
	Postcode           int64        `column:"postcode"`            // The user postcode
	AdministrationCode int64        `column:"administration_code"` // The user administration code
	Address            string       `column:"address"`             // The user address
	CreatedTime        time.Time    `column:"created_time"`        // created time
	ModifiedTime       time.Time    `column:"modified_time"`       // modified time
	IsDeleted          int64        `column:"is_deleted"`          // Logic to delete(0:normal 1:deleted)
	WeTestTab2s        []WeTestTab2 // This value is used for batch queries and inserts.
}

test table2

func (*WeTestTab2) BatchInsert

func (weTestTab2 *WeTestTab2) BatchInsert(client *DBClient, idSet, returnIds bool) ([]int64, error)

func (*WeTestTab2) DeleteWeTestTab2ById

func (weTestTab2 *WeTestTab2) DeleteWeTestTab2ById(client *DBClient) (int64, error)

func (*WeTestTab2) Insert

func (weTestTab2 *WeTestTab2) Insert(client *DBClient, idSet bool) (int64, error)

func (*WeTestTab2) RowToStruct

func (weTestTab2 *WeTestTab2) RowToStruct(row *db.Row) error

func (*WeTestTab2) RowsToStruct

func (weTestTab2 *WeTestTab2) RowsToStruct(rows *db.Rows) error

func (*WeTestTab2) UpdateWeTestTab2ById

func (weTestTab2 *WeTestTab2) UpdateWeTestTab2ById(client *DBClient) (int64, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL