Documentation ¶
Index ¶
- Constants
- Variables
- func CommonExec(db *sql.DB, query string, args ...interface{}) (sql.Result, error)
- func CommonQuery(db *sql.DB, query string, args ...interface{}) ([]map[string]interface{}, error)
- func Contains(v DatabaseType, a []DatabaseType) bool
- func Exec(query string, args ...interface{}) (sql.Result, error)
- func Query(query string, args ...interface{}) ([]map[string]interface{}, error)
- func RecycleSQL(sql *SQL)
- func SetColVarType(colVar *[]interface{}, i int, typeName string)
- func SetResultValue(result *map[string]interface{}, index string, colVar interface{}, ...)
- type Connection
- type DatabaseType
- type H
- type Mssql
- func (db *Mssql) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Mssql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
- func (db *Mssql) GetDelimiter() string
- func (db *Mssql) GetName() string
- func (db *Mssql) InitDB(cfglist map[string]config.Database)
- func (db *Mssql) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
- func (db *Mssql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
- type Mysql
- func (db *Mysql) BeginTransactions() *SQLTx
- func (db *Mysql) BeginTransactionsWithLevel(level sql.IsolationLevel) *SQLTx
- func (db *Mysql) BeginTransactionsWithReadCommitted() *SQLTx
- func (db *Mysql) BeginTransactionsWithReadUncommitted() *SQLTx
- func (db *Mysql) BeginTransactionsWithRepeatableRead() *SQLTx
- func (db *Mysql) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Mysql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
- func (db *Mysql) GetDelimiter() string
- func (db *Mysql) GetName() string
- func (db *Mysql) InitDB(cfgs map[string]config.Database)
- func (db *Mysql) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
- func (db *Mysql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
- func (db *Mysql) WithTransaction(fn TxFn) (res map[string]interface{}, err error)
- func (db *Mysql) WithTransactionByLevel(level sql.IsolationLevel, fn TxFn) (res map[string]interface{}, err error)
- type Postgresql
- func (db *Postgresql) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Postgresql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
- func (db *Postgresql) GetDelimiter() string
- func (db *Postgresql) GetName() string
- func (db *Postgresql) InitDB(cfgList map[string]config.Database)
- func (db *Postgresql) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
- func (db *Postgresql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
- type SQL
- func (sql *SQL) All() ([]map[string]interface{}, error)
- func (sql *SQL) Count() (int64, error)
- func (sql *SQL) Delete() error
- func (sql *SQL) Exec() (int64, error)
- func (sql *SQL) Find(arg interface{}) (map[string]interface{}, error)
- func (sql *SQL) First() (map[string]interface{}, error)
- func (sql *SQL) Insert(values dialect.H) (int64, error)
- func (sql *SQL) LeftJoin(table string, fieldA string, operation string, fieldB string) *SQL
- func (sql *SQL) OrderBy(fields ...string) *SQL
- func (sql *SQL) Select(fields ...string) *SQL
- func (sql *SQL) ShowColumns() ([]map[string]interface{}, error)
- func (sql *SQL) ShowTables() ([]map[string]interface{}, error)
- func (sql *SQL) Skip(offset int) *SQL
- func (sql *SQL) Table(table string) *SQL
- func (sql *SQL) Take(take int) *SQL
- func (sql *SQL) Update(values dialect.H) (int64, error)
- func (sql *SQL) UpdateRaw(raw string, args ...interface{}) *SQL
- func (sql *SQL) Where(field string, operation string, arg interface{}) *SQL
- func (sql *SQL) WhereIn(field string, arg []interface{}) *SQL
- func (sql *SQL) WhereNotIn(field string, arg []interface{}) *SQL
- func (sql *SQL) WhereRaw(raw string, args ...interface{}) *SQL
- func (sql *SQL) WithConnection(conn string) *SQL
- type SQLTx
- type Sqlite
- func (db *Sqlite) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Sqlite) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
- func (db *Sqlite) GetDelimiter() string
- func (db *Sqlite) GetName() string
- func (db *Sqlite) InitDB(cfgList map[string]config.Database)
- func (db *Sqlite) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
- func (db *Sqlite) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
- type TxFn
- type Value
Constants ¶
const ( // DriverMysql is a const value of mysql driver. DriverMysql = "mysql" // DriverSqlite is a const value of sqlite driver. DriverSqlite = "sqlite" // DriverPostgresql is a const value of postgresql driver. DriverPostgresql = "postgresql" // DriverMssql is a const value of mssql driver. DriverMssql = "mssql" )
Variables ¶
var ( // StringTypeList is a DatabaseType list of string. StringTypeList = []DatabaseType{Date, Time, Year, Datetime, Timestamptz, Timestamp, Varchar, Char, Mediumtext, Longtext, Tinytext, Text, JSON, Blob, Tinyblob, Mediumblob, Longblob, Interval, Point, Line, Lseg, Box, Path, Polygon, Circle, Cidr, Inet, Macaddr, Character, Varyingcharacter, Nchar, Nativecharacter, Nvarchar, Clob, Binary, Varbinary, Enum, Set, Geometry, Multilinestring, Multipolygon, Linestring, Multipoint, Geometrycollection, Name, UUID, Timestamptz, Name, UUID, Inet} // BoolTypeList is a DatabaseType list of bool. BoolTypeList = []DatabaseType{Bool, Boolean} // IntTypeList is a DatabaseType list of integer. IntTypeList = []DatabaseType{Int4, Int, Tinyint, Mediumint, Smallint, Numeric, Smallserial, Serial, Bigserial, Money, Integer, Bigint} // FloatTypeList is a DatabaseType list of float. FloatTypeList = []DatabaseType{Float, Double, Real, Doubleprecision} // UintTypeList is a DatabaseType list of uint. UintTypeList = []DatabaseType{Decimal, Bit} )
var DB = Sqlite{ DbList: map[string]*sql.DB{}, }
DB is a global variable which handles the sqlite connection.
var MssqlDB = Mssql{ DbList: map[string]*sql.DB{}, }
MssqlDB is a global variable which handles the mssql connection.
var MysqlDB = Mysql{ DbList: map[string]*sql.DB{}, }
MysqlDB is a global variable which handles the mysql connection.
var PostgresqlDB = Postgresql{ DbList: map[string]*sql.DB{}, }
PostgresqlDB is a global variable which handles the postgresql connection.
var SQLPool = sync.Pool{ New: func() interface{} { return &SQL{ SQLComponent: dialect.SQLComponent{ Fields: make([]string, 0), TableName: "", Args: make([]interface{}, 0), Wheres: make([]dialect.Where, 0), Leftjoins: make([]dialect.Join, 0), UpdateRaws: make([]dialect.RawUpdate, 0), WhereRaws: "", }, diver: nil, dialect: nil, } }, }
SQLPool is a object pool of SQL.
Functions ¶
func CommonExec ¶
CommonExec is a common method of exec.
func CommonQuery ¶
CommonQuery is a common method of query.
func Contains ¶
func Contains(v DatabaseType, a []DatabaseType) bool
Contains check the given DatabaseType is in the list or not.
func RecycleSQL ¶ added in v1.0.8
func RecycleSQL(sql *SQL)
RecycleSQL clear the SQL and put into the pool.
func SetColVarType ¶
SetColVarType set the column type.
func SetResultValue ¶
func SetResultValue(result *map[string]interface{}, index string, colVar interface{}, typeName string)
SetResultValue set the result value.
Types ¶
type Connection ¶
type Connection interface { // Query is the query method of sql. Query(query string, args ...interface{}) ([]map[string]interface{}, error) // Exec is the exec method of sql. Exec(query string, args ...interface{}) (sql.Result, error) // QueryWithConnection is the query method with given connection of sql. QueryWithConnection(conn, query string, args ...interface{}) ([]map[string]interface{}, error) // ExecWithConnection is the exec method with given connection of sql. ExecWithConnection(conn, query string, args ...interface{}) (sql.Result, error) // InitDB initialize the database connections. InitDB(cfg map[string]config.Database) // GetName get the connection name. GetName() string // GetDelimiter get the default delimiter. GetDelimiter() string }
Connection is a connection handler of database.
func GetConnectionByDriver ¶
func GetConnectionByDriver(driver string) Connection
GetConnectionByDriver return the Connection by given driver name.
type DatabaseType ¶
type DatabaseType string
DatabaseType is the database field type.
const ( Int DatabaseType = "INT" Tinyint DatabaseType = "TINYINT" Mediumint DatabaseType = "MEDIUMINT" Smallint DatabaseType = "SMALLINT" Bigint DatabaseType = "BIGINT" Bit DatabaseType = "BIT" Int4 DatabaseType = "INT4" Integer DatabaseType = "INTEGER" Numeric DatabaseType = "NUMERIC" Smallserial DatabaseType = "SMALLSERIAL" Serial DatabaseType = "SERIAL" Bigserial DatabaseType = "BIGSERIAL" Money DatabaseType = "MONEY" Real DatabaseType = "REAL" Float DatabaseType = "FLOAT" Double DatabaseType = "DOUBLE" Decimal DatabaseType = "DECIMAL" Doubleprecision DatabaseType = "DOUBLEPRECISION" Date DatabaseType = "DATE" Time DatabaseType = "TIME" Year DatabaseType = "YEAR" Datetime DatabaseType = "DATETIME" Timestamp DatabaseType = "TIMESTAMP" Text DatabaseType = "TEXT" Longtext DatabaseType = "LONGTEXT" Mediumtext DatabaseType = "MEDIUMTEXT" Tinytext DatabaseType = "TINYTEXT" Varchar DatabaseType = "VARCHAR" Char DatabaseType = "CHAR" JSON DatabaseType = "JSON" Blob DatabaseType = "BLOB" Tinyblob DatabaseType = "TINYBLOB" Mediumblob DatabaseType = "MEDIUMBLOB" Longblob DatabaseType = "LONGBLOB" Interval DatabaseType = "INTERVAL" Boolean DatabaseType = "BOOLEAN" Bool DatabaseType = "Bool" Point DatabaseType = "POINT" Line DatabaseType = "LINE" Lseg DatabaseType = "LSEG" Box DatabaseType = "BOX" Path DatabaseType = "PATH" Polygon DatabaseType = "POLYGON" Circle DatabaseType = "CIRCLE" Cidr DatabaseType = "CIDR" Inet DatabaseType = "INET" Macaddr DatabaseType = "MACADDR" Character DatabaseType = "CHARACTER" Varyingcharacter DatabaseType = "VARYINGCHARACTER" Nchar DatabaseType = "NCHAR" Nativecharacter DatabaseType = "NATIVECHARACTER" Nvarchar DatabaseType = "NVARCHAR" Clob DatabaseType = "CLOB" Binary DatabaseType = "BINARY" Varbinary DatabaseType = "VARBINARY" Enum DatabaseType = "ENUM" Set DatabaseType = "SET" Geometry DatabaseType = "GEOMETRY" Multilinestring DatabaseType = "MULTILINESTRING" Multipolygon DatabaseType = "MULTIPOLYGON" Linestring DatabaseType = "LINESTRING" Multipoint DatabaseType = "MULTIPOINT" Geometrycollection DatabaseType = "GEOMETRYCOLLECTION" Name DatabaseType = "NAME" UUID DatabaseType = "UUID" Timestamptz DatabaseType = "TIMESTAMPTZ" )
func GetDTAndCheck ¶
func GetDTAndCheck(s string) DatabaseType
GetDTAndCheck check the DatabaseType.
type Mssql ¶
Mssql is a Connection of mssql.
func (*Mssql) ExecWithConnection ¶
func (db *Mssql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
func (*Mssql) GetDelimiter ¶
GetDelimiter implements the method Connection.GetDelimiter.
type Mysql ¶
Mysql is a Connection of mssql.
func (*Mysql) BeginTransactions ¶
BeginTransactions starts a transaction with level LevelDefault.
func (*Mysql) BeginTransactionsWithLevel ¶
func (db *Mysql) BeginTransactionsWithLevel(level sql.IsolationLevel) *SQLTx
BeginTransactionsWithLevel starts a transaction with given transaction isolation level.
func (*Mysql) BeginTransactionsWithReadCommitted ¶
BeginTransactionsWithReadCommitted starts a transaction with level LevelReadCommitted.
func (*Mysql) BeginTransactionsWithReadUncommitted ¶
BeginTransactionsWithReadUncommitted starts a transaction with level LevelReadUncommitted.
func (*Mysql) BeginTransactionsWithRepeatableRead ¶
BeginTransactionsWithRepeatableRead starts a transaction with level LevelRepeatableRead.
func (*Mysql) ExecWithConnection ¶
func (db *Mysql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
func (*Mysql) GetDelimiter ¶
GetDelimiter implements the method Connection.GetDelimiter.
func (*Mysql) QueryWithConnection ¶
func (db *Mysql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
func (*Mysql) WithTransaction ¶
WithTransaction call the callback function within the transaction and catch the error.
func (*Mysql) WithTransactionByLevel ¶
func (db *Mysql) WithTransactionByLevel(level sql.IsolationLevel, fn TxFn) (res map[string]interface{}, err error)
WithTransactionByLevel call the callback function within the transaction of given transaction level and catch the error.
type Postgresql ¶
Postgresql is a Connection of mssql.
func GetPostgresqlDB ¶
func GetPostgresqlDB() *Postgresql
GetPostgresqlDB return the global mssql connection.
func (*Postgresql) Exec ¶
func (db *Postgresql) Exec(query string, args ...interface{}) (sql.Result, error)
Exec implements the method Connection.Exec.
func (*Postgresql) ExecWithConnection ¶
func (db *Postgresql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
func (*Postgresql) GetDelimiter ¶
func (db *Postgresql) GetDelimiter() string
GetDelimiter implements the method Connection.GetDelimiter.
func (*Postgresql) GetName ¶
func (db *Postgresql) GetName() string
GetName implements the method Connection.GetName.
func (*Postgresql) InitDB ¶
func (db *Postgresql) InitDB(cfgList map[string]config.Database)
InitDB implements the method Connection.InitDB.
func (*Postgresql) Query ¶
func (db *Postgresql) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
Query implements the method Connection.Query.
func (*Postgresql) QueryWithConnection ¶
func (db *Postgresql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
type SQL ¶ added in v1.0.8
type SQL struct { dialect.SQLComponent // contains filtered or unexported fields }
SQL wraps the Connection and driver dialect methods.
func WithDriverAndConnection ¶
WithDriverAndConnection return a SQL with given driver and connection name.
func (*SQL) Find ¶ added in v1.0.8
Find query the sql result with given id assuming that primary key name is "id".
func (*SQL) ShowColumns ¶ added in v1.0.8
ShowColumns show columns info.
func (*SQL) ShowTables ¶ added in v1.0.8
ShowTables show table info.
func (*SQL) WhereNotIn ¶ added in v1.0.8
WhereNotIn add the where operation of "not in" and argument values.
func (*SQL) WithConnection ¶ added in v1.0.8
WithConnection set the connection name of SQL.
type SQLTx ¶ added in v1.0.8
SQLTx is an in-progress database transaction.
type Sqlite ¶
Sqlite is a Connection of mssql.
func (*Sqlite) ExecWithConnection ¶
func (db *Sqlite) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
func (*Sqlite) GetDelimiter ¶
GetDelimiter implements the method Connection.GetDelimiter.
type Value ¶
type Value string
Value is a string.
func GetValueFromDatabaseType ¶
func GetValueFromDatabaseType(typ DatabaseType, value interface{}) Value
GetValueFromDatabaseType return Value of given DatabaseType and interface.