import "github.com/GoAdminGroup/go-admin/modules/db"
base.go connection.go converter.go mssql.go mysql.go performer.go postgresql.go sqlite.go statement.go types.go
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" )
const ( INSERT = 0 DELETE = 1 UPDATE = 2 QUERY = 3 )
var ( // StringTypeList is a DatabaseType list of string. StringTypeList = []DatabaseType{Date, Time, Year, Datetime, Timestamptz, Timestamp, Timetz, Varchar, Char, Mediumtext, Longtext, Tinytext, Text, JSON, Blob, Tinyblob, Mediumblob, Longblob, Interval, Point, Bpchar, 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, Int2, Int8, Int, Tinyint, Mediumint, Smallint, Smallserial, Serial, Bigserial, Integer, Bigint} // FloatTypeList is a DatabaseType list of float. FloatTypeList = []DatabaseType{Float, Float4, Float8, Double, Real, Doubleprecision} // UintTypeList is a DatabaseType list of uint. UintTypeList = []DatabaseType{Decimal, Bit, Money, Numeric} )
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: "", Order: "", Group: "", Limit: "", }, diver: nil, dialect: nil, } }, }
SQLPool is a object pool of SQL.
CommonBeginTxWithLevel starts a transaction with given transaction isolation level and db connection.
CommonExec is a common method of exec.
CommonExecWithTx is a common method of exec.
CommonQuery is a common method of query.
func CommonQueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
CommonQueryWithTx is a common method of query.
func Contains(v DatabaseType, a []DatabaseType) bool
Contains check the given DatabaseType is in the list or not.
RecycleSQL clear the SQL and put into the pool.
SetColVarType set the column type.
func SetResultValue(result *map[string]interface{}, index string, colVar interface{}, typeName string)
SetResultValue set the result value.
Base is a common Connection.
Close implements the method Connection.Close.
GetDB implements the method Connection.GetDB.
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) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) QueryWith(tx *sql.Tx, 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) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error) BeginTxWithReadUncommitted() *sql.Tx BeginTxWithReadCommitted() *sql.Tx BeginTxWithRepeatableRead() *sql.Tx BeginTx() *sql.Tx BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx BeginTxAndConnection(conn string) *sql.Tx BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx // InitDB initialize the database connections. InitDB(cfg map[string]config.Database) Connection // GetName get the connection name. Name() string Close() []error GetDelimiter() string GetDelimiter2() string GetDelimiters() []string GetDB(key string) *sql.DB GetConfig(name string) config.Database CreateDB(name string, beans ...interface{}) error }
Connection is a connection handler of database.
func GetConnection(srvs service.List) Connection
func GetConnectionByDriver(driver string) Connection
GetConnectionByDriver return the Connection by given driver name.
func GetConnectionFromService(srv interface{}) Connection
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" Int8 DatabaseType = "INT8" Int4 DatabaseType = "INT4" Int2 DatabaseType = "INT2" Integer DatabaseType = "INTEGER" Numeric DatabaseType = "NUMERIC" Smallserial DatabaseType = "SMALLSERIAL" Serial DatabaseType = "SERIAL" Bigserial DatabaseType = "BIGSERIAL" Money DatabaseType = "MONEY" Real DatabaseType = "REAL" Float DatabaseType = "FLOAT" Float4 DatabaseType = "FLOAT4" Float8 DatabaseType = "FLOAT8" 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" Bpchar DatabaseType = "BPCHAR" 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" Timetz DatabaseType = "TIMETZ" )
func DT(s string) DatabaseType
DT turn the string value into DatabaseType.
func GetDTAndCheck(s string) DatabaseType
GetDTAndCheck check the DatabaseType.
H is a shorthand of map.
Mssql is a Connection of mssql.
GetMssqlDB return the global mssql connection.
BeginTx starts a transaction with level LevelDefault.
BeginTxAndConnection starts a transaction with level LevelDefault and connection.
BeginTxWithLevel starts a transaction with given transaction isolation level.
BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
Exec implements the method Connection.Exec.
func (db *Mssql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
ExecWithTx is exec method within the transaction.
GetDelimiter implements the method Connection.GetDelimiter.
GetDelimiter2 implements the method Connection.GetDelimiter2.
GetDelimiters implements the method Connection.GetDelimiters.
InitDB implements the method Connection.InitDB.
Name implements the method Connection.Name.
Query implements the method Connection.Query.
func (db *Mssql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error)
func (db *Mssql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
func (db *Mssql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithTx is query method within the transaction.
Mysql is a Connection of mysql.
GetMysqlDB return the global mysql connection.
BeginTx starts a transaction with level LevelDefault.
BeginTxAndConnection starts a transaction with level LevelDefault and connection.
BeginTxWithLevel starts a transaction with given transaction isolation level.
BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
Exec implements the method Connection.Exec.
func (db *Mysql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
ExecWithTx is exec method within the transaction.
GetDelimiter implements the method Connection.GetDelimiter.
GetDelimiter2 implements the method Connection.GetDelimiter2.
GetDelimiters implements the method Connection.GetDelimiters.
InitDB implements the method Connection.InitDB.
Name implements the method Connection.Name.
Query implements the method Connection.Query.
func (db *Mysql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error)
func (db *Mysql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
func (db *Mysql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithTx is query method within the transaction.
Postgresql is a Connection of postgresql.
func GetPostgresqlDB() *Postgresql
GetPostgresqlDB return the global postgresql connection.
func (db *Postgresql) BeginTx() *sql.Tx
BeginTx starts a transaction with level LevelDefault.
func (db *Postgresql) BeginTxAndConnection(conn string) *sql.Tx
BeginTxAndConnection starts a transaction with level LevelDefault and connection.
func (db *Postgresql) BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx
BeginTxWithLevel starts a transaction with given transaction isolation level.
func (db *Postgresql) BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx
BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
func (db *Postgresql) BeginTxWithReadCommitted() *sql.Tx
BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
func (db *Postgresql) BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx
BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
func (db *Postgresql) BeginTxWithReadUncommitted() *sql.Tx
BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
func (db *Postgresql) BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx
BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
func (db *Postgresql) BeginTxWithRepeatableRead() *sql.Tx
BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
func (db *Postgresql) BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx
BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
Exec implements the method Connection.Exec.
func (db *Postgresql) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error)
func (db *Postgresql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
func (db *Postgresql) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error)
ExecWithTx is exec method within the transaction.
func (db *Postgresql) GetDelimiter() string
GetDelimiter implements the method Connection.GetDelimiter.
func (db *Postgresql) GetDelimiter2() string
GetDelimiter2 implements the method Connection.GetDelimiter2.
func (db *Postgresql) GetDelimiters() []string
GetDelimiters implements the method Connection.GetDelimiters.
func (db *Postgresql) InitDB(cfgList map[string]config.Database) Connection
InitDB implements the method Connection.InitDB.
func (db *Postgresql) Name() string
Name implements the method Connection.Name.
func (db *Postgresql) Query(query string, args ...interface{}) ([]map[string]interface{}, error)
Query implements the method Connection.Query.
func (db *Postgresql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error)
func (db *Postgresql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
func (db *Postgresql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithTx is query method within the transaction.
type SQL struct { dialect.SQLComponent // contains filtered or unexported fields }
SQL wraps the Connection and driver dialect methods.
TableName return a SQL with given table and default connection.
func WithDriver(conn Connection) *SQL
WithDriver return a SQL with given driver.
func WithDriverAndConnection(connName string, conn Connection) *SQL
WithDriverAndConnection return a SQL with given driver and connection name.
All query all the result and return.
Avg find the average value of given field.
Count query the count of query results.
Delete exec the delete method.
Exec exec the exec method.
Find query the sql result with given id assuming that primary key name is "id".
First query the result and return the first row.
GroupByRaw set group by.
Insert exec the insert method of given key/value pairs.
LeftJoin add a left join info.
Max find the maximal value of given field.
Min find the minimal value of given field.
OrderBy set order fields.
OrderByRaw set order by.
Select set select fields.
ShowColumns show columns info.
ShowTables show table info.
Skip set offset value.
Sum sum the value of given field.
TableName set table of SQL.
Take set limit value.
Update exec the update method of given key/value pairs.
UpdateRaw set UpdateRaw.
Where add the where operation and argument value.
WhereIn add the where operation of "in" and argument values.
WhereNotIn add the where operation of "not in" and argument values.
WhereRaw set WhereRaws and arguments.
WithConnection set the connection name of SQL.
func (sql *SQL) WithDriver(conn Connection) *SQL
WithDriver return a SQL with given driver.
WithTransaction call the callback function within the transaction and catch the error.
func (sql *SQL) WithTransactionByLevel(level dbsql.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.
WithTx set the database transaction object of SQL.
SQLTx is an in-progress database transaction.
Sqlite is a Connection of sqlite.
GetSqliteDB return the global sqlite connection.
BeginTx starts a transaction with level LevelDefault.
BeginTxAndConnection starts a transaction with level LevelDefault and connection.
BeginTxWithLevel starts a transaction with given transaction isolation level.
BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
Exec implements the method Connection.Exec.
func (db *Sqlite) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error)
func (db *Sqlite) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error)
ExecWithConnection implements the method Connection.ExecWithConnection.
ExecWithTx is exec method within the transaction.
GetDelimiter implements the method Connection.GetDelimiter.
GetDelimiter2 implements the method Connection.GetDelimiter2.
GetDelimiters implements the method Connection.GetDelimiters.
InitDB implements the method Connection.InitDB.
Name implements the method Connection.Name.
Query implements the method Connection.Query.
func (db *Sqlite) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error)
func (db *Sqlite) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithConnection implements the method Connection.QueryWithConnection.
func (db *Sqlite) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
QueryWithTx is query method within the transaction.
TxFn is the transaction callback function.
Value is a string.
func GetValueFromDatabaseType(typ DatabaseType, value interface{}, json bool) Value
func GetValueFromJSONOfDatabaseType(typ DatabaseType, value interface{}) Value
GetValueFromJSONOfDatabaseType return Value of given DatabaseType and interface from JSON string value.
func GetValueFromSQLOfDatabaseType(typ DatabaseType, value interface{}) Value
GetValueFromDatabaseType return Value of given DatabaseType and interface.
HTML return the template.HTML value.
String return the string value.
ToInt64 turn the string to a int64.
Path | Synopsis |
---|---|
dialect | |
drivers/mssql | |
drivers/mysql | |
drivers/postgres | |
drivers/sqlite |
Package db imports 14 packages (graph) and is imported by 40 packages. Updated 2020-12-28. Refresh now. Tools for package owners.