connector

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

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

func (conn *Connector) Close()

func (*Connector) ExecSQL

func (conn *Connector) ExecSQL(sql string) *Result

Connector.ExecSQL: execute sql, return *Result.

func (*Connector) InitDB

func (conn *Connector) InitDB() error

Connector.InitDB:

DROP DATABASE IF EXISTS Connector.DbName
CREATE DATABASE Connector.DbName
USE Connector.DbName

func (*Connector) InitDBWithDDL added in v1.2.0

func (conn *Connector) InitDBWithDDL(ddlSqls []*EachSql) error

Connector.InitDBWithDDL: init database and execute ddl sqls

func (*Connector) InitDBWithDDLPath added in v1.2.0

func (conn *Connector) InitDBWithDDLPath(ddlPath string) error

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

type EachSql struct {
	Id  int    `json:"id"`
	Sql string `json:"sql"`
}

func ExtractSQL added in v1.2.0

func ExtractSQL(s string) []*EachSql

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

func ExtractSqlFromPath(sqlPath string) ([]*EachSql, error)

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

func (this *Result) CMP(another *Result) (int, error)

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

func (*Result) FlatRows

func (result *Result) FlatRows() []string

Result.FlatRows: [["1","2"],["3","4"]] -> ["1,2", "3,4"]

func (*Result) GetErrorCode

func (result *Result) GetErrorCode() (int, error)

func (*Result) IsEmpty

func (result *Result) IsEmpty() bool

Result.IsEmpty: if the result is empty

func (*Result) ToString

func (result *Result) ToString() string

Jump to

Keyboard shortcuts

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