Documentation ¶
Overview ¶
Package connector: connect to mysql, auto create database if not exists, execute raw sql statements, return raw execution result or error.
We also provide some useful functions:
- init database, see Connector.InitDB
- extract sqls from a sql file, see ExtractSQL
- init database with a sql file, see Connector.InitDBWithDDL, Connector.InitDBWithDDLPath
- connectorPool, see NewConnectorPool, ConnectorPool.WaitForFree, ConnectorPool.BackToPool
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connector ¶
type Connector struct { Host string Port int Username string Password string DbName string // contains filtered or unexported fields }
Connector: connect to MySQL, execute raw sql statements, return raw execution result or error.
func NewConnector ¶
func NewConnector(host string, port int, username string, password string, dbname string) (*Connector, error)
NewConnector: create Connector. CREATE DATABASE IF NOT EXISTS dbname + USE dbname when dbname != ""
func (*Connector) InitDB ¶
Connector.InitDB:
DROP DATABASE IF EXISTS Connector.DbName CREATE DATABASE Connector.DbName USE Connector.DbName
func (*Connector) InitDBWithDDL ¶ added in v1.2.0
Connector.InitDBWithDDL: init database and execute ddl sqls
func (*Connector) InitDBWithDDLPath ¶ added in v1.2.0
Connector.InitDBWithDDLPath: init database and execute ddl sqls from ddlPath
type ConnectorPool ¶ added in v1.2.0
type ConnectorPool struct { Host string Port int Username string Password string DbPrefix string ThreadNum int ThreadPool chan *Connector }
func NewConnectorPool ¶ added in v1.2.0
func NewConnectorPool(host string, port int, username string, password string, dbPrefix string, threadNum int) (*ConnectorPool, error)
NewConnectorPool: create thread pool with size ThreadNum, fill with *Connector, the database name of each connector is config.DbPrefix + thread id
func (*ConnectorPool) BackToPool ¶ added in v1.2.0
func (connPool *ConnectorPool) BackToPool(conn *Connector)
ConnectorPool.BackToPool: give a connector back to pool
func (*ConnectorPool) Close ¶ added in v1.5.1
func (connPool *ConnectorPool) Close()
ConnectorPool.Close: close all connectors, close channel
func (*ConnectorPool) WaitForFree ¶ added in v1.2.0
func (connPool *ConnectorPool) WaitForFree() *Connector
ConnectorPool.WaitForFree wait for a free connector
type EachSql ¶ added in v1.2.0
func ExtractSQL ¶ added in v1.2.0
ExtractSQL: s is a sqls string, each sql statement is separated by ';' in s. We will extract each sql statement into []*EachSql.
Note that:
- we will ignore the ';' in “, ”, "";
- we will ignore the escaped characters in “, ”, "";
- your comments cannot have ';'
func ExtractSqlFromPath ¶ added in v1.2.0
ExtractSqlFromPath: extract sql from sql file, see ExtractSQL
type Result ¶
type Result struct { ColumnNames []string ColumnTypes []string Rows [][]string Err error Time time.Duration // total time }
Result:
query result, for example:
+-----+------+------+ | 1+2 | ID | NAME | -> ColumnNames: 1+2, ID, NAME +-----+------+------+ -> ColumnTypes: BIGINT, INT, TEXT | 3 | 1 | H | -> Rows[0]: 3, 1, H | 3 | 2 | Z | -> Rows[1]: 3, 2, Z | 3 | 3 | Y | -> Rows[2]: 3, 3, Y +-----+------+------+
or error, for example:
Err: ERROR 1054 (42S22): Unknown column 'T' in 'field list'
note that:
len(ColumnNames) = len(ColumnTypes) = len(Rows[i]);
if the statement is not SELECT, then the ColumnNames, ColumnTypes and Rows are empty
func (*Result) CMP ¶ added in v1.2.0
Result.CMP:
-1: another contains this 0: eq 1: this contains another 2: others error: this.Err or another.Err do not consider the column name