Documentation ¶
Overview ¶
Package dbx provides a set of DB-agnostic and easy-to-use query building methods for relational databases.
Example (CrudOperations) ¶
This example shows how to do CRUD operations.
package main import ( "github.com/LcTheSecond/dbx" ) type Customer struct { ID string Name string } func main() { db, _ := dbx.Open("mysql", "user:pass@/example") var customer Customer // read a customer: SELECT * FROM customer WHERE id=100 db.Select().Model(100, &customer) // create a customer: INSERT INTO customer (name) VALUES ('test') db.Model(&customer).Insert() // update a customer: UPDATE customer SET name='test' WHERE id=100 db.Model(&customer).Update() // delete a customer: DELETE FROM customer WHERE id=100 db.Model(&customer).Delete() }
Output:
Example (DbQueries) ¶
This example shows how to populate DB data in different ways.
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") // create a new query q := db.NewQuery("SELECT id, name FROM users LIMIT 10") // fetch all rows into a struct array var users []struct { ID, Name string } q.All(&users) // fetch a single row into a struct var user struct { ID, Name string } q.One(&user) // fetch a single row into a string map data := dbx.NullStringMap{} q.One(data) // fetch row by row rows2, _ := q.Rows() for rows2.Next() { rows2.ScanStruct(&user) // rows.ScanMap(data) // rows.Scan(&id, &name) } }
Output:
Example (QueryBuilder) ¶
This example shows how to use query builder to build DB queries.
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") // build a SELECT query // SELECT `id`, `name` FROM `users` WHERE `name` LIKE '%Charles%' ORDER BY `id` q := db.Select("id", "name"). From("users"). Where(dbx.Like("name", "Charles")). OrderBy("id") // fetch all rows into a struct array var users []struct { ID, Name string } q.All(&users) // build an INSERT query // INSERT INTO `users` (name) VALUES ('James') db.Insert("users", dbx.Params{ "name": "James", }).Execute() }
Output:
Example (Transactions) ¶
This example shows how to use query builder in transactions.
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") db.Transactional(func(tx *dbx.Tx) error { _, err := tx.Insert("user", dbx.Params{ "name": "user1", }).Execute() if err != nil { return err } _, err = tx.Insert("user", dbx.Params{ "name": "user2", }).Execute() return err }) }
Output:
Index ¶
- Variables
- func DefaultFieldMapFunc(f string) string
- func GetTableName(a interface{}) string
- type AllHookFunc
- type AndOrExp
- type BaseBuilder
- func (b *BaseBuilder) AddColumn(table, col, typ string) *Query
- func (b *BaseBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query
- func (b *BaseBuilder) AddPrimaryKey(table, name string, cols ...string) *Query
- func (b *BaseBuilder) AlterColumn(table, col, typ string) *Query
- func (b *BaseBuilder) CreateIndex(table, name string, cols ...string) *Query
- func (b *BaseBuilder) CreateTable(table string, cols map[string]string, options ...string) *Query
- func (b *BaseBuilder) CreateUniqueIndex(table, name string, cols ...string) *Query
- func (b *BaseBuilder) DB() *DB
- func (b *BaseBuilder) Delete(table string, where Expression) *Query
- func (b *BaseBuilder) DropColumn(table, col string) *Query
- func (b *BaseBuilder) DropForeignKey(table, name string) *Query
- func (b *BaseBuilder) DropIndex(table, name string) *Query
- func (b *BaseBuilder) DropPrimaryKey(table, name string) *Query
- func (b *BaseBuilder) DropTable(table string) *Query
- func (b *BaseBuilder) Executor() Executor
- func (b *BaseBuilder) GeneratePlaceholder(int) string
- func (b *BaseBuilder) Insert(table string, cols Params) *Query
- func (b *BaseBuilder) NewQuery(sql string) *Query
- func (b *BaseBuilder) Quote(s string) string
- func (b *BaseBuilder) QuoteSimpleColumnName(s string) string
- func (b *BaseBuilder) QuoteSimpleTableName(s string) string
- func (b *BaseBuilder) RenameColumn(table, oldName, newName string) *Query
- func (b *BaseBuilder) RenameTable(oldName, newName string) *Query
- func (b *BaseBuilder) TruncateTable(table string) *Query
- func (b *BaseBuilder) Update(table string, cols Params, where Expression) *Query
- func (b *BaseBuilder) Upsert(table string, cols Params, constraints ...string) *Query
- type BaseQueryBuilder
- func (q *BaseQueryBuilder) BuildFrom(tables []string) string
- func (q *BaseQueryBuilder) BuildGroupBy(cols []string) string
- func (q *BaseQueryBuilder) BuildHaving(e Expression, params Params) string
- func (q *BaseQueryBuilder) BuildJoin(joins []JoinInfo, params Params) string
- func (q *BaseQueryBuilder) BuildLimit(limit int64, offset int64) string
- func (q *BaseQueryBuilder) BuildOrderBy(cols []string) string
- func (q *BaseQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string
- func (q *BaseQueryBuilder) BuildSelect(cols []string, distinct bool, option string) string
- func (q *BaseQueryBuilder) BuildUnion(unions []UnionInfo, params Params) string
- func (q *BaseQueryBuilder) BuildWhere(e Expression, params Params) string
- func (q *BaseQueryBuilder) DB() *DB
- type BetweenExp
- type BuildHookFunc
- type Builder
- func NewMssqlBuilder(db *DB, executor Executor) Builder
- func NewMysqlBuilder(db *DB, executor Executor) Builder
- func NewOciBuilder(db *DB, executor Executor) Builder
- func NewPgsqlBuilder(db *DB, executor Executor) Builder
- func NewSqliteBuilder(db *DB, executor Executor) Builder
- func NewStandardBuilder(db *DB, executor Executor) Builder
- type BuilderFunc
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Clone() *DB
- func (db *DB) Close() error
- func (db *DB) Context() context.Context
- func (db *DB) DB() *sql.DB
- func (db *DB) DriverName() string
- func (db *DB) QuoteColumnName(s string) string
- func (db *DB) QuoteTableName(s string) string
- func (db *DB) Transactional(f func(*Tx) error) (err error)
- func (db *DB) TransactionalContext(ctx context.Context, opts *sql.TxOptions, f func(*Tx) error) (err error)
- func (db *DB) WithContext(ctx context.Context) *DB
- func (db *DB) Wrap(sqlTx *sql.Tx) *Tx
- type EncloseExp
- type Errors
- type ExecHookFunc
- type ExecLogFunc
- type Executor
- type ExistsExp
- type Exp
- type Expression
- func And(exps ...Expression) Expression
- func Between(col string, from, to interface{}) Expression
- func Enclose(exp Expression) Expression
- func Exists(exp Expression) Expression
- func In(col string, values ...interface{}) Expression
- func NewExp(e string, params ...Params) Expression
- func Not(e Expression) Expression
- func NotBetween(col string, from, to interface{}) Expression
- func NotExists(exp Expression) Expression
- func NotIn(col string, values ...interface{}) Expression
- func Or(exps ...Expression) Expression
- type FieldMapFunc
- type HashExp
- type InExp
- type JoinInfo
- type LikeExp
- type LogFunc
- type ModelQuery
- func (q *ModelQuery) Context() context.Context
- func (q *ModelQuery) Delete() error
- func (q *ModelQuery) Exclude(attrs ...string) *ModelQuery
- func (q *ModelQuery) Insert(attrs ...string) error
- func (q *ModelQuery) Update(attrs ...string) error
- func (q *ModelQuery) WithContext(ctx context.Context) *ModelQuery
- type MssqlBuilder
- func (b *MssqlBuilder) AlterColumn(table, col, typ string) *Query
- func (b *MssqlBuilder) Model(model interface{}) *ModelQuery
- func (b *MssqlBuilder) QueryBuilder() QueryBuilder
- func (b *MssqlBuilder) QuoteSimpleColumnName(s string) string
- func (b *MssqlBuilder) QuoteSimpleTableName(s string) string
- func (b *MssqlBuilder) RenameColumn(table, oldName, newName string) *Query
- func (b *MssqlBuilder) RenameTable(oldName, newName string) *Query
- func (b *MssqlBuilder) Select(cols ...string) *SelectQuery
- type MssqlQueryBuilder
- type MysqlBuilder
- func (b *MysqlBuilder) DropForeignKey(table, name string) *Query
- func (b *MysqlBuilder) DropPrimaryKey(table, name string) *Query
- func (b *MysqlBuilder) Model(model interface{}) *ModelQuery
- func (b *MysqlBuilder) QueryBuilder() QueryBuilder
- func (b *MysqlBuilder) QuoteSimpleColumnName(s string) string
- func (b *MysqlBuilder) QuoteSimpleTableName(s string) string
- func (b *MysqlBuilder) RenameColumn(table, oldName, newName string) *Query
- func (b *MysqlBuilder) Select(cols ...string) *SelectQuery
- func (b *MysqlBuilder) Upsert(table string, cols Params, constraints ...string) *Query
- type NotExp
- type NullStringMap
- type OciBuilder
- func (b *OciBuilder) AlterColumn(table, col, typ string) *Query
- func (b *OciBuilder) DropIndex(table, name string) *Query
- func (b *OciBuilder) GeneratePlaceholder(i int) string
- func (b *OciBuilder) Model(model interface{}) *ModelQuery
- func (b *OciBuilder) QueryBuilder() QueryBuilder
- func (b *OciBuilder) RenameTable(oldName, newName string) *Query
- func (b *OciBuilder) Select(cols ...string) *SelectQuery
- type OciQueryBuilder
- type OneHookFunc
- type Params
- type PerfFunc
- type PgsqlBuilder
- func (b *PgsqlBuilder) AlterColumn(table, col, typ string) *Query
- func (b *PgsqlBuilder) DropIndex(table, name string) *Query
- func (b *PgsqlBuilder) GeneratePlaceholder(i int) string
- func (b *PgsqlBuilder) Model(model interface{}) *ModelQuery
- func (b *PgsqlBuilder) QueryBuilder() QueryBuilder
- func (b *PgsqlBuilder) RenameTable(oldName, newName string) *Query
- func (b *PgsqlBuilder) Select(cols ...string) *SelectQuery
- func (b *PgsqlBuilder) Upsert(table string, cols Params, constraints ...string) *Query
- type PostScanner
- type Query
- func (q *Query) All(slice interface{}) error
- func (q *Query) Bind(params Params) *Query
- func (q *Query) Close() error
- func (q *Query) Column(a interface{}) error
- func (q *Query) Context() context.Context
- func (q *Query) Execute() (sql.Result, error)
- func (q *Query) One(a interface{}) error
- func (q *Query) Params() Params
- func (q *Query) Prepare() *Query
- func (q *Query) Row(a ...interface{}) error
- func (q *Query) Rows() (rows *Rows, err error)
- func (q *Query) SQL() string
- func (q *Query) WithAllHook(fn AllHookFunc) *Query
- func (q *Query) WithContext(ctx context.Context) *Query
- func (q *Query) WithExecHook(fn ExecHookFunc) *Query
- func (q *Query) WithOneHook(fn OneHookFunc) *Query
- type QueryBuilder
- type QueryInfo
- type QueryLogFunc
- type Rows
- type SelectQuery
- func (s *SelectQuery) All(slice interface{}) error
- func (s *SelectQuery) AndBind(params Params) *SelectQuery
- func (s *SelectQuery) AndGroupBy(cols ...string) *SelectQuery
- func (s *SelectQuery) AndHaving(e Expression) *SelectQuery
- func (s *SelectQuery) AndOrderBy(cols ...string) *SelectQuery
- func (s *SelectQuery) AndSelect(cols ...string) *SelectQuery
- func (s *SelectQuery) AndWhere(e Expression) *SelectQuery
- func (s *SelectQuery) Bind(params Params) *SelectQuery
- func (s *SelectQuery) Build() *Query
- func (s *SelectQuery) Column(a interface{}) error
- func (q *SelectQuery) Context() context.Context
- func (s *SelectQuery) Distinct(v bool) *SelectQuery
- func (s *SelectQuery) From(tables ...string) *SelectQuery
- func (s *SelectQuery) GroupBy(cols ...string) *SelectQuery
- func (s *SelectQuery) Having(e Expression) *SelectQuery
- func (s *SelectQuery) Info() *QueryInfo
- func (s *SelectQuery) InnerJoin(table string, on Expression) *SelectQuery
- func (s *SelectQuery) Join(typ string, table string, on Expression) *SelectQuery
- func (s *SelectQuery) LeftJoin(table string, on Expression) *SelectQuery
- func (s *SelectQuery) Limit(limit int64) *SelectQuery
- func (s *SelectQuery) Model(pk, model interface{}) error
- func (s *SelectQuery) Offset(offset int64) *SelectQuery
- func (s *SelectQuery) One(a interface{}) error
- func (s *SelectQuery) OrHaving(e Expression) *SelectQuery
- func (s *SelectQuery) OrWhere(e Expression) *SelectQuery
- func (s *SelectQuery) OrderBy(cols ...string) *SelectQuery
- func (s *SelectQuery) RightJoin(table string, on Expression) *SelectQuery
- func (s *SelectQuery) Row(a ...interface{}) error
- func (s *SelectQuery) Rows() (*Rows, error)
- func (s *SelectQuery) Select(cols ...string) *SelectQuery
- func (s *SelectQuery) SelectOption(option string) *SelectQuery
- func (s *SelectQuery) Union(q *Query) *SelectQuery
- func (s *SelectQuery) UnionAll(q *Query) *SelectQuery
- func (s *SelectQuery) Where(e Expression) *SelectQuery
- func (q *SelectQuery) WithBuildHook(fn BuildHookFunc) *SelectQuery
- func (q *SelectQuery) WithContext(ctx context.Context) *SelectQuery
- type SqliteBuilder
- func (b *SqliteBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query
- func (b *SqliteBuilder) AddPrimaryKey(table, name string, cols ...string) *Query
- func (b *SqliteBuilder) AlterColumn(table, col, typ string) *Query
- func (b *SqliteBuilder) DropForeignKey(table, name string) *Query
- func (b *SqliteBuilder) DropIndex(table, name string) *Query
- func (b *SqliteBuilder) DropPrimaryKey(table, name string) *Query
- func (b *SqliteBuilder) Model(model interface{}) *ModelQuery
- func (b *SqliteBuilder) QueryBuilder() QueryBuilder
- func (b *SqliteBuilder) QuoteSimpleColumnName(s string) string
- func (b *SqliteBuilder) QuoteSimpleTableName(s string) string
- func (b *SqliteBuilder) RenameTable(oldName, newName string) *Query
- func (b *SqliteBuilder) Select(cols ...string) *SelectQuery
- func (b *SqliteBuilder) TruncateTable(table string) *Query
- type StandardBuilder
- type TableMapFunc
- type TableModel
- type Tx
- type UnionInfo
- type VarTypeError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( MissingPKError = errors.New("missing primary key declaration") CompositePKError = errors.New("composite primary key is not supported") )
var BuilderFuncMap = map[string]BuilderFunc{ "sqlite": NewSqliteBuilder, "sqlite3": NewSqliteBuilder, "mysql": NewMysqlBuilder, "postgres": NewPgsqlBuilder, "pgx": NewPgsqlBuilder, "mssql": NewMssqlBuilder, "oci8": NewOciBuilder, }
BuilderFuncMap lists supported BuilderFunc according to DB driver names. You may modify this variable to add the builder support for a new DB driver. If a DB driver is not listed here, the StandardBuilder will be used.
var (
// DbTag is the name of the struct tag used to specify the column name for the associated struct field
DbTag = "db"
)
var DefaultLikeEscape = []string{"\\", "\\\\", "%", "\\%", "_", "\\_"}
DefaultLikeEscape specifies the default special character escaping for LIKE expressions The strings at 2i positions are the special characters to be escaped while those at 2i+1 positions are the corresponding escaped versions.
Functions ¶
func DefaultFieldMapFunc ¶
DefaultFieldMapFunc maps a field name to a DB column name. The mapping rule set by this method is that words in a field name will be separated by underscores and the name will be turned into lower case. For example, "FirstName" maps to "first_name", and "MyID" becomes "my_id". See DB.FieldMapper for more details.
func GetTableName ¶
func GetTableName(a interface{}) string
GetTableName implements the default way of determining the table name corresponding to the given model struct or slice of structs. To get the actual table name for a model, you should use DB.TableMapFunc() instead. Do not call this method in a model's TableName() method because it will cause infinite loop.
Types ¶
type AllHookFunc ¶
AllHookFunc executes right before the query populate the row result from All() call (aka. op).
type AndOrExp ¶
type AndOrExp struct {
// contains filtered or unexported fields
}
AndOrExp represents an expression that concatenates multiple expressions using either "AND" or "OR".
type BaseBuilder ¶
type BaseBuilder struct {
// contains filtered or unexported fields
}
BaseBuilder provides a basic implementation of the Builder interface.
func NewBaseBuilder ¶
func NewBaseBuilder(db *DB, executor Executor) *BaseBuilder
NewBaseBuilder creates a new BaseBuilder instance.
func (*BaseBuilder) AddColumn ¶
func (b *BaseBuilder) AddColumn(table, col, typ string) *Query
AddColumn creates a Query that can be used to add a column to a table.
func (*BaseBuilder) AddForeignKey ¶
func (b *BaseBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query
AddForeignKey creates a Query that can be used to add a foreign key constraint to a table. The length of cols and refCols must be the same as they refer to the primary and referential columns. The optional "options" parameters will be appended to the SQL statement. They can be used to specify options such as "ON DELETE CASCADE".
func (*BaseBuilder) AddPrimaryKey ¶
func (b *BaseBuilder) AddPrimaryKey(table, name string, cols ...string) *Query
AddPrimaryKey creates a Query that can be used to specify primary key(s) for a table. The "name" parameter specifies the name of the primary key constraint.
func (*BaseBuilder) AlterColumn ¶
func (b *BaseBuilder) AlterColumn(table, col, typ string) *Query
AlterColumn creates a Query that can be used to change the definition of a table column.
func (*BaseBuilder) CreateIndex ¶
func (b *BaseBuilder) CreateIndex(table, name string, cols ...string) *Query
CreateIndex creates a Query that can be used to create an index for a table.
func (*BaseBuilder) CreateTable ¶
CreateTable creates a Query that represents a CREATE TABLE SQL statement. The keys of cols are the column names, while the values of cols are the corresponding column types. The optional "options" parameters will be appended to the generated SQL statement.
func (*BaseBuilder) CreateUniqueIndex ¶
func (b *BaseBuilder) CreateUniqueIndex(table, name string, cols ...string) *Query
CreateUniqueIndex creates a Query that can be used to create a unique index for a table.
func (*BaseBuilder) DB ¶
func (b *BaseBuilder) DB() *DB
DB returns the DB instance that this builder is associated with.
func (*BaseBuilder) Delete ¶
func (b *BaseBuilder) Delete(table string, where Expression) *Query
Delete creates a Query that represents a DELETE SQL statement. If the "where" expression is nil, the DELETE SQL statement will have no WHERE clause (be careful in this case as the SQL statement will delete ALL rows in the table).
func (*BaseBuilder) DropColumn ¶
func (b *BaseBuilder) DropColumn(table, col string) *Query
DropColumn creates a Query that can be used to drop a column from a table.
func (*BaseBuilder) DropForeignKey ¶
func (b *BaseBuilder) DropForeignKey(table, name string) *Query
DropForeignKey creates a Query that can be used to remove the named foreign key constraint from a table.
func (*BaseBuilder) DropIndex ¶
func (b *BaseBuilder) DropIndex(table, name string) *Query
DropIndex creates a Query that can be used to remove the named index from a table.
func (*BaseBuilder) DropPrimaryKey ¶
func (b *BaseBuilder) DropPrimaryKey(table, name string) *Query
DropPrimaryKey creates a Query that can be used to remove the named primary key constraint from a table.
func (*BaseBuilder) DropTable ¶
func (b *BaseBuilder) DropTable(table string) *Query
DropTable creates a Query that can be used to drop a table.
func (*BaseBuilder) Executor ¶
func (b *BaseBuilder) Executor() Executor
Executor returns the executor object (a DB instance or a transaction) for executing SQL statements.
func (*BaseBuilder) GeneratePlaceholder ¶
func (b *BaseBuilder) GeneratePlaceholder(int) string
GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID.
func (*BaseBuilder) Insert ¶
func (b *BaseBuilder) Insert(table string, cols Params) *Query
Insert creates a Query that represents an INSERT SQL statement. The keys of cols are the column names, while the values of cols are the corresponding column values to be inserted.
func (*BaseBuilder) NewQuery ¶
func (b *BaseBuilder) NewQuery(sql string) *Query
NewQuery creates a new Query object with the given SQL statement. The SQL statement may contain parameter placeholders which can be bound with actual parameter values before the statement is executed.
func (*BaseBuilder) Quote ¶
func (b *BaseBuilder) Quote(s string) string
Quote quotes a string so that it can be embedded in a SQL statement as a string value.
func (*BaseBuilder) QuoteSimpleColumnName ¶
func (b *BaseBuilder) QuoteSimpleColumnName(s string) string
QuoteSimpleColumnName quotes a simple column name. A simple column name does not contain any table prefix.
func (*BaseBuilder) QuoteSimpleTableName ¶
func (b *BaseBuilder) QuoteSimpleTableName(s string) string
QuoteSimpleTableName quotes a simple table name. A simple table name does not contain any schema prefix.
func (*BaseBuilder) RenameColumn ¶
func (b *BaseBuilder) RenameColumn(table, oldName, newName string) *Query
RenameColumn creates a Query that can be used to rename a column in a table.
func (*BaseBuilder) RenameTable ¶
func (b *BaseBuilder) RenameTable(oldName, newName string) *Query
RenameTable creates a Query that can be used to rename a table.
func (*BaseBuilder) TruncateTable ¶
func (b *BaseBuilder) TruncateTable(table string) *Query
TruncateTable creates a Query that can be used to truncate a table.
func (*BaseBuilder) Update ¶
func (b *BaseBuilder) Update(table string, cols Params, where Expression) *Query
Update creates a Query that represents an UPDATE SQL statement. The keys of cols are the column names, while the values of cols are the corresponding new column values. If the "where" expression is nil, the UPDATE SQL statement will have no WHERE clause (be careful in this case as the SQL statement will update ALL rows in the table).
func (*BaseBuilder) Upsert ¶
func (b *BaseBuilder) Upsert(table string, cols Params, constraints ...string) *Query
Upsert creates a Query that represents an UPSERT SQL statement. Upsert inserts a row into the table if the primary key or unique index is not found. Otherwise it will update the row with the new values. The keys of cols are the column names, while the values of cols are the corresponding column values to be inserted.
type BaseQueryBuilder ¶
type BaseQueryBuilder struct {
// contains filtered or unexported fields
}
BaseQueryBuilder provides a basic implementation of QueryBuilder.
func NewBaseQueryBuilder ¶
func NewBaseQueryBuilder(db *DB) *BaseQueryBuilder
NewBaseQueryBuilder creates a new BaseQueryBuilder instance.
func (*BaseQueryBuilder) BuildFrom ¶
func (q *BaseQueryBuilder) BuildFrom(tables []string) string
BuildFrom generates a FROM clause from the given tables.
func (*BaseQueryBuilder) BuildGroupBy ¶
func (q *BaseQueryBuilder) BuildGroupBy(cols []string) string
BuildGroupBy generates a GROUP BY clause from the given group-by columns.
func (*BaseQueryBuilder) BuildHaving ¶
func (q *BaseQueryBuilder) BuildHaving(e Expression, params Params) string
BuildHaving generates a HAVING clause from the given expression.
func (*BaseQueryBuilder) BuildJoin ¶
func (q *BaseQueryBuilder) BuildJoin(joins []JoinInfo, params Params) string
BuildJoin generates a JOIN clause from the given join information.
func (*BaseQueryBuilder) BuildLimit ¶
func (q *BaseQueryBuilder) BuildLimit(limit int64, offset int64) string
BuildLimit generates the LIMIT clause.
func (*BaseQueryBuilder) BuildOrderBy ¶
func (q *BaseQueryBuilder) BuildOrderBy(cols []string) string
BuildOrderBy generates the ORDER BY clause.
func (*BaseQueryBuilder) BuildOrderByAndLimit ¶
func (q *BaseQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string
BuildOrderByAndLimit generates the ORDER BY and LIMIT clauses.
func (*BaseQueryBuilder) BuildSelect ¶
func (q *BaseQueryBuilder) BuildSelect(cols []string, distinct bool, option string) string
BuildSelect generates a SELECT clause from the given selected column names.
func (*BaseQueryBuilder) BuildUnion ¶
func (q *BaseQueryBuilder) BuildUnion(unions []UnionInfo, params Params) string
BuildUnion generates a UNION clause from the given union information.
func (*BaseQueryBuilder) BuildWhere ¶
func (q *BaseQueryBuilder) BuildWhere(e Expression, params Params) string
BuildWhere generates a WHERE clause from the given expression.
func (*BaseQueryBuilder) DB ¶
func (q *BaseQueryBuilder) DB() *DB
DB returns the DB instance associated with the query builder.
type BetweenExp ¶
type BetweenExp struct {
// contains filtered or unexported fields
}
BetweenExp represents a BETWEEN or a NOT BETWEEN expression.
type BuildHookFunc ¶
type BuildHookFunc func(q *Query)
BuildHookFunc defines a callback function that is executed on Query creation.
type Builder ¶
type Builder interface { // NewQuery creates a new Query object with the given SQL statement. // The SQL statement may contain parameter placeholders which can be bound with actual parameter // values before the statement is executed. NewQuery(string) *Query // Select returns a new SelectQuery object that can be used to build a SELECT statement. // The parameters to this method should be the list column names to be selected. // A column name may have an optional alias name. For example, Select("id", "my_name AS name"). Select(...string) *SelectQuery // ModelQuery returns a new ModelQuery object that can be used to perform model insertion, update, and deletion. // The parameter to this method should be a pointer to the model struct that needs to be inserted, updated, or deleted. Model(interface{}) *ModelQuery // GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID. GeneratePlaceholder(int) string // Quote quotes a string so that it can be embedded in a SQL statement as a string value. Quote(string) string // QuoteSimpleTableName quotes a simple table name. // A simple table name does not contain any schema prefix. QuoteSimpleTableName(string) string // QuoteSimpleColumnName quotes a simple column name. // A simple column name does not contain any table prefix. QuoteSimpleColumnName(string) string // QueryBuilder returns the query builder supporting the current DB. QueryBuilder() QueryBuilder // Insert creates a Query that represents an INSERT SQL statement. // The keys of cols are the column names, while the values of cols are the corresponding column // values to be inserted. Insert(table string, cols Params) *Query // Upsert creates a Query that represents an UPSERT SQL statement. // Upsert inserts a row into the table if the primary key or unique index is not found. // Otherwise it will update the row with the new values. // The keys of cols are the column names, while the values of cols are the corresponding column // values to be inserted. Upsert(table string, cols Params, constraints ...string) *Query // Update creates a Query that represents an UPDATE SQL statement. // The keys of cols are the column names, while the values of cols are the corresponding new column // values. If the "where" expression is nil, the UPDATE SQL statement will have no WHERE clause // (be careful in this case as the SQL statement will update ALL rows in the table). Update(table string, cols Params, where Expression) *Query // Delete creates a Query that represents a DELETE SQL statement. // If the "where" expression is nil, the DELETE SQL statement will have no WHERE clause // (be careful in this case as the SQL statement will delete ALL rows in the table). Delete(table string, where Expression) *Query // CreateTable creates a Query that represents a CREATE TABLE SQL statement. // The keys of cols are the column names, while the values of cols are the corresponding column types. // The optional "options" parameters will be appended to the generated SQL statement. CreateTable(table string, cols map[string]string, options ...string) *Query // RenameTable creates a Query that can be used to rename a table. RenameTable(oldName, newName string) *Query // DropTable creates a Query that can be used to drop a table. DropTable(table string) *Query // TruncateTable creates a Query that can be used to truncate a table. TruncateTable(table string) *Query // AddColumn creates a Query that can be used to add a column to a table. AddColumn(table, col, typ string) *Query // DropColumn creates a Query that can be used to drop a column from a table. DropColumn(table, col string) *Query // RenameColumn creates a Query that can be used to rename a column in a table. RenameColumn(table, oldName, newName string) *Query // AlterColumn creates a Query that can be used to change the definition of a table column. AlterColumn(table, col, typ string) *Query // AddPrimaryKey creates a Query that can be used to specify primary key(s) for a table. // The "name" parameter specifies the name of the primary key constraint. AddPrimaryKey(table, name string, cols ...string) *Query // DropPrimaryKey creates a Query that can be used to remove the named primary key constraint from a table. DropPrimaryKey(table, name string) *Query // AddForeignKey creates a Query that can be used to add a foreign key constraint to a table. // The length of cols and refCols must be the same as they refer to the primary and referential columns. // The optional "options" parameters will be appended to the SQL statement. They can be used to // specify options such as "ON DELETE CASCADE". AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query // DropForeignKey creates a Query that can be used to remove the named foreign key constraint from a table. DropForeignKey(table, name string) *Query // CreateIndex creates a Query that can be used to create an index for a table. CreateIndex(table, name string, cols ...string) *Query // CreateUniqueIndex creates a Query that can be used to create a unique index for a table. CreateUniqueIndex(table, name string, cols ...string) *Query // DropIndex creates a Query that can be used to remove the named index from a table. DropIndex(table, name string) *Query }
Builder supports building SQL statements in a DB-agnostic way. Builder mainly provides two sets of query building methods: those building SELECT statements and those manipulating DB data or schema (e.g. INSERT statements, CREATE TABLE statements).
func NewMssqlBuilder ¶
NewMssqlBuilder creates a new MssqlBuilder instance.
func NewMysqlBuilder ¶
NewMysqlBuilder creates a new MysqlBuilder instance.
func NewOciBuilder ¶
NewOciBuilder creates a new OciBuilder instance.
func NewPgsqlBuilder ¶
NewPgsqlBuilder creates a new PgsqlBuilder instance.
func NewSqliteBuilder ¶
NewSqliteBuilder creates a new SqliteBuilder instance.
func NewStandardBuilder ¶
NewStandardBuilder creates a new StandardBuilder instance.
type BuilderFunc ¶
BuilderFunc creates a Builder instance using the given DB instance and Executor.
type DB ¶
type DB struct { Builder // FieldMapper maps struct fields to DB columns. Defaults to DefaultFieldMapFunc. FieldMapper FieldMapFunc // TableMapper maps structs to table names. Defaults to GetTableName. TableMapper TableMapFunc // LogFunc logs the SQL statements being executed. Defaults to nil, meaning no logging. LogFunc LogFunc // PerfFunc logs the SQL execution time. Defaults to nil, meaning no performance profiling. // Deprecated: Please use QueryLogFunc and ExecLogFunc instead. PerfFunc PerfFunc // QueryLogFunc is called each time when performing a SQL query that returns data. QueryLogFunc QueryLogFunc // ExecLogFunc is called each time when a SQL statement is executed. ExecLogFunc ExecLogFunc // contains filtered or unexported fields }
DB enhances sql.DB by providing a set of DB-agnostic query building methods. DB allows easier query building and population of data into Go variables.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") // queries data through a plain SQL var users []struct { ID, Name string } db.NewQuery("SELECT id, name FROM users WHERE age=30").All(&users) // queries data using query builder db.Select("id", "name").From("users").Where(dbx.HashExp{"age": 30}).All(&users) // executes a plain SQL db.NewQuery("INSERT INTO users (name) SET ({:name})").Bind(dbx.Params{"name": "James"}).Execute() // executes a SQL using query builder db.Insert("users", dbx.Params{"name": "James"}).Execute() }
Output:
func MustOpen ¶
MustOpen opens a database and establishes a connection to it. Please refer to sql.Open() and sql.Ping() for more information.
func Open ¶
Open opens a database specified by a driver name and data source name (DSN). Note that Open does not check if DSN is specified correctly. It doesn't try to establish a DB connection either. Please refer to sql.Open() for more information.
func (*DB) Begin ¶
Begin starts a transaction.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") tx, _ := db.Begin() _, err1 := tx.Insert("user", dbx.Params{ "name": "user1", }).Execute() _, err2 := tx.Insert("user", dbx.Params{ "name": "user2", }).Execute() if err1 == nil && err2 == nil { tx.Commit() } else { tx.Rollback() } }
Output:
func (*DB) Close ¶
Close closes the database, releasing any open resources. It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
func (*DB) Context ¶
Context returns the context associated with the DB instance. It returns nil if no context is associated.
func (*DB) DriverName ¶
DriverName returns the name of the DB driver.
func (*DB) QuoteColumnName ¶
QuoteColumnName quotes the given column name appropriately. If the table name contains table name prefix, it will be handled accordingly. This method will do nothing if the column name is already quoted or if it contains parenthesis.
func (*DB) QuoteTableName ¶
QuoteTableName quotes the given table name appropriately. If the table name contains DB schema prefix, it will be handled accordingly. This method will do nothing if the table name is already quoted or if it contains parenthesis.
func (*DB) Transactional ¶
Transactional starts a transaction and executes the given function. If the function returns an error, the transaction will be rolled back. Otherwise, the transaction will be committed.
func (*DB) TransactionalContext ¶
func (db *DB) TransactionalContext(ctx context.Context, opts *sql.TxOptions, f func(*Tx) error) (err error)
TransactionalContext starts a transaction and executes the given function with the given context and transaction options. If the function returns an error, the transaction will be rolled back. Otherwise, the transaction will be committed.
func (*DB) WithContext ¶
WithContext returns a new instance of DB associated with the given context.
type EncloseExp ¶
type EncloseExp struct {
// contains filtered or unexported fields
}
EncloseExp represents a parenthesis enclosed expression.
type ExecHookFunc ¶
ExecHookFunc executes before op allowing custom handling like auto fail/retry.
type ExecLogFunc ¶
type ExecLogFunc func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error)
ExecLogFunc is called each time when a SQL statement is executed. The "t" parameter gives the time that the SQL statement takes to execute, while result and err refer to the result of the execution.
type Executor ¶
type Executor interface { // Exec executes a SQL statement Exec(query string, args ...interface{}) (sql.Result, error) // ExecContext executes a SQL statement with the given context ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) // Query queries a SQL statement Query(query string, args ...interface{}) (*sql.Rows, error) // QueryContext queries a SQL statement with the given context QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) // Prepare creates a prepared statement Prepare(query string) (*sql.Stmt, error) }
Executor prepares, executes, or queries a SQL statement.
type ExistsExp ¶
type ExistsExp struct {
// contains filtered or unexported fields
}
ExistsExp represents an EXISTS or NOT EXISTS expression.
type Exp ¶
type Exp struct {
// contains filtered or unexported fields
}
Exp represents an expression with a SQL fragment and a list of optional binding parameters.
type Expression ¶
type Expression interface { // Build converts an expression into a SQL fragment. // If the expression contains binding parameters, they will be added to the given Params. Build(*DB, Params) string }
Expression represents a DB expression that can be embedded in a SQL statement.
func And ¶
func And(exps ...Expression) Expression
And generates an AND expression which concatenates the given expressions with "AND".
func Between ¶
func Between(col string, from, to interface{}) Expression
Between generates a BETWEEN expression. For example, Between("age", 10, 30) generates: "age" BETWEEN 10 AND 30
func Enclose ¶
func Enclose(exp Expression) Expression
Enclose surrounds the provided nonempty expression with parenthesis "()".
func Exists ¶
func Exists(exp Expression) Expression
Exists generates an EXISTS expression by prefixing "EXISTS" to the given expression.
func In ¶
func In(col string, values ...interface{}) Expression
In generates an IN expression for the specified column and the list of allowed values. If values is empty, a SQL "0=1" will be generated which represents a false expression.
func NewExp ¶
func NewExp(e string, params ...Params) Expression
NewExp generates an expression with the specified SQL fragment and the optional binding parameters.
func Not ¶
func Not(e Expression) Expression
Not generates a NOT expression which prefixes "NOT" to the specified expression.
func NotBetween ¶
func NotBetween(col string, from, to interface{}) Expression
NotBetween generates a NOT BETWEEN expression. For example, NotBetween("age", 10, 30) generates: "age" NOT BETWEEN 10 AND 30
func NotExists ¶
func NotExists(exp Expression) Expression
NotExists generates an EXISTS expression by prefixing "NOT EXISTS" to the given expression.
func NotIn ¶
func NotIn(col string, values ...interface{}) Expression
NotIn generates an NOT IN expression for the specified column and the list of disallowed values. If values is empty, an empty string will be returned indicating a true expression.
func Or ¶
func Or(exps ...Expression) Expression
Or generates an OR expression which concatenates the given expressions with "OR".
type FieldMapFunc ¶
FieldMapFunc converts a struct field name into a DB column name.
type HashExp ¶
type HashExp map[string]interface{}
HashExp represents a hash expression.
A hash expression is a map whose keys are DB column names which need to be filtered according to the corresponding values. For example, HashExp{"level": 2, "dept": 10} will generate the SQL: "level"=2 AND "dept"=10.
HashExp also handles nil values and slice values. For example, HashExp{"level": []interface{}{1, 2}, "dept": nil} will generate: "level" IN (1, 2) AND "dept" IS NULL.
type InExp ¶
type InExp struct {
// contains filtered or unexported fields
}
InExp represents an "IN" or "NOT IN" expression.
type JoinInfo ¶
type JoinInfo struct { Join string Table string On Expression }
JoinInfo contains the specification for a JOIN clause.
type LikeExp ¶
type LikeExp struct { // Like stores the LIKE operator. It can be "LIKE", "NOT LIKE". // It may also be customized as something like "ILIKE". Like string // contains filtered or unexported fields }
LikeExp represents a variant of LIKE expressions.
func Like ¶
Like generates a LIKE expression for the specified column and the possible strings that the column should be like. If multiple values are present, the column should be like *all* of them. For example, Like("name", "key", "word") will generate a SQL expression: "name" LIKE "%key%" AND "name" LIKE "%word%".
By default, each value will be surrounded by "%" to enable partial matching. If a value contains special characters such as "%", "\", "_", they will also be properly escaped.
You may call Escape() and/or Match() to change the default behavior. For example, Like("name", "key").Match(false, true) generates "name" LIKE "key%".
func NotLike ¶
NotLike generates a NOT LIKE expression. For example, NotLike("name", "key", "word") will generate a SQL expression: "name" NOT LIKE "%key%" AND "name" NOT LIKE "%word%". Please see Like() for more details.
func OrLike ¶
OrLike generates an OR LIKE expression. This is similar to Like() except that the column should be like one of the possible values. For example, OrLike("name", "key", "word") will generate a SQL expression: "name" LIKE "%key%" OR "name" LIKE "%word%". Please see Like() for more details.
func OrNotLike ¶
OrNotLike generates an OR NOT LIKE expression. For example, OrNotLike("name", "key", "word") will generate a SQL expression: "name" NOT LIKE "%key%" OR "name" NOT LIKE "%word%". Please see Like() for more details.
type LogFunc ¶
type LogFunc func(format string, a ...interface{})
LogFunc logs a message for each SQL statement being executed. This method takes one or multiple parameters. If a single parameter is provided, it will be treated as the log message. If multiple parameters are provided, they will be passed to fmt.Sprintf() to generate the log message.
type ModelQuery ¶
type ModelQuery struct {
// contains filtered or unexported fields
}
ModelQuery represents a query associated with a struct model.
func NewModelQuery ¶
func NewModelQuery(model interface{}, fieldMapFunc FieldMapFunc, db *DB, builder Builder) *ModelQuery
func (*ModelQuery) Context ¶
func (q *ModelQuery) Context() context.Context
Context returns the context associated with the query.
func (*ModelQuery) Delete ¶
func (q *ModelQuery) Delete() error
Delete deletes a row in the table using the primary key specified by the struct model associated with this query.
func (*ModelQuery) Exclude ¶
func (q *ModelQuery) Exclude(attrs ...string) *ModelQuery
Exclude excludes the specified struct fields from being inserted/updated into the DB table.
func (*ModelQuery) Insert ¶
func (q *ModelQuery) Insert(attrs ...string) error
Insert inserts a row in the table using the struct model associated with this query.
By default, it inserts *all* public fields into the table, including those nil or empty ones. You may pass a list of the fields to this method to indicate that only those fields should be inserted. You may also call Exclude to exclude some fields from being inserted.
If a model has an empty primary key, it is considered auto-incremental and the corresponding struct field will be filled with the generated primary key value after a successful insertion.
func (*ModelQuery) Update ¶
func (q *ModelQuery) Update(attrs ...string) error
Update updates a row in the table using the struct model associated with this query. The row being updated has the same primary key as specified by the model.
By default, it updates *all* public fields in the table, including those nil or empty ones. You may pass a list of the fields to this method to indicate that only those fields should be updated. You may also call Exclude to exclude some fields from being updated.
func (*ModelQuery) WithContext ¶
func (q *ModelQuery) WithContext(ctx context.Context) *ModelQuery
WithContext associates a context with the query.
type MssqlBuilder ¶
type MssqlBuilder struct { *BaseBuilder // contains filtered or unexported fields }
MssqlBuilder is the builder for SQL Server databases.
func (*MssqlBuilder) AlterColumn ¶
func (b *MssqlBuilder) AlterColumn(table, col, typ string) *Query
AlterColumn creates a Query that can be used to change the definition of a table column.
func (*MssqlBuilder) Model ¶
func (b *MssqlBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*MssqlBuilder) QueryBuilder ¶
func (b *MssqlBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*MssqlBuilder) QuoteSimpleColumnName ¶
func (b *MssqlBuilder) QuoteSimpleColumnName(s string) string
QuoteSimpleColumnName quotes a simple column name. A simple column name does not contain any table prefix.
func (*MssqlBuilder) QuoteSimpleTableName ¶
func (b *MssqlBuilder) QuoteSimpleTableName(s string) string
QuoteSimpleTableName quotes a simple table name. A simple table name does not contain any schema prefix.
func (*MssqlBuilder) RenameColumn ¶
func (b *MssqlBuilder) RenameColumn(table, oldName, newName string) *Query
RenameColumn creates a Query that can be used to rename a column in a table.
func (*MssqlBuilder) RenameTable ¶
func (b *MssqlBuilder) RenameTable(oldName, newName string) *Query
RenameTable creates a Query that can be used to rename a table.
func (*MssqlBuilder) Select ¶
func (b *MssqlBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
type MssqlQueryBuilder ¶
type MssqlQueryBuilder struct {
*BaseQueryBuilder
}
MssqlQueryBuilder is the query builder for SQL Server databases.
func (*MssqlQueryBuilder) BuildOrderByAndLimit ¶
func (q *MssqlQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string
BuildOrderByAndLimit generates the ORDER BY and LIMIT clauses.
type MysqlBuilder ¶
type MysqlBuilder struct { *BaseBuilder // contains filtered or unexported fields }
MysqlBuilder is the builder for MySQL databases.
func (*MysqlBuilder) DropForeignKey ¶
func (b *MysqlBuilder) DropForeignKey(table, name string) *Query
DropForeignKey creates a Query that can be used to remove the named foreign key constraint from a table.
func (*MysqlBuilder) DropPrimaryKey ¶
func (b *MysqlBuilder) DropPrimaryKey(table, name string) *Query
DropPrimaryKey creates a Query that can be used to remove the named primary key constraint from a table.
func (*MysqlBuilder) Model ¶
func (b *MysqlBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*MysqlBuilder) QueryBuilder ¶
func (b *MysqlBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*MysqlBuilder) QuoteSimpleColumnName ¶
func (b *MysqlBuilder) QuoteSimpleColumnName(s string) string
QuoteSimpleColumnName quotes a simple column name. A simple column name does not contain any table prefix.
func (*MysqlBuilder) QuoteSimpleTableName ¶
func (b *MysqlBuilder) QuoteSimpleTableName(s string) string
QuoteSimpleTableName quotes a simple table name. A simple table name does not contain any schema prefix.
func (*MysqlBuilder) RenameColumn ¶
func (b *MysqlBuilder) RenameColumn(table, oldName, newName string) *Query
RenameColumn creates a Query that can be used to rename a column in a table.
func (*MysqlBuilder) Select ¶
func (b *MysqlBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
func (*MysqlBuilder) Upsert ¶
func (b *MysqlBuilder) Upsert(table string, cols Params, constraints ...string) *Query
Upsert creates a Query that represents an UPSERT SQL statement. Upsert inserts a row into the table if the primary key or unique index is not found. Otherwise it will update the row with the new values. The keys of cols are the column names, while the values of cols are the corresponding column values to be inserted.
type NotExp ¶
type NotExp struct {
// contains filtered or unexported fields
}
NotExp represents an expression that should prefix "NOT" to a specified expression.
type NullStringMap ¶
type NullStringMap map[string]sql.NullString
NullStringMap is a map of sql.NullString that can be used to hold DB query result. The map keys correspond to the DB column names, while the map values are their corresponding column values.
type OciBuilder ¶
type OciBuilder struct { *BaseBuilder // contains filtered or unexported fields }
OciBuilder is the builder for Oracle databases.
func (*OciBuilder) AlterColumn ¶
func (b *OciBuilder) AlterColumn(table, col, typ string) *Query
AlterColumn creates a Query that can be used to change the definition of a table column.
func (*OciBuilder) DropIndex ¶
func (b *OciBuilder) DropIndex(table, name string) *Query
DropIndex creates a Query that can be used to remove the named index from a table.
func (*OciBuilder) GeneratePlaceholder ¶
func (b *OciBuilder) GeneratePlaceholder(i int) string
GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID.
func (*OciBuilder) Model ¶
func (b *OciBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*OciBuilder) QueryBuilder ¶
func (b *OciBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*OciBuilder) RenameTable ¶
func (b *OciBuilder) RenameTable(oldName, newName string) *Query
RenameTable creates a Query that can be used to rename a table.
func (*OciBuilder) Select ¶
func (b *OciBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
type OciQueryBuilder ¶
type OciQueryBuilder struct {
*BaseQueryBuilder
}
OciQueryBuilder is the query builder for Oracle databases.
func (*OciQueryBuilder) BuildOrderByAndLimit ¶
func (q *OciQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string
BuildOrderByAndLimit generates the ORDER BY and LIMIT clauses.
type OneHookFunc ¶
OneHookFunc executes right before the query populate the row result from One() call (aka. op).
type Params ¶
type Params map[string]interface{}
Params represents a list of parameter values to be bound to a SQL statement. The map keys are the parameter names while the map values are the corresponding parameter values.
type PerfFunc ¶
PerfFunc is called when a query finishes execution. The query execution time is passed to this function so that the DB performance can be profiled. The "ns" parameter gives the number of nanoseconds that the SQL statement takes to execute, while the "execute" parameter indicates whether the SQL statement is executed or queried (usually SELECT statements).
type PgsqlBuilder ¶
type PgsqlBuilder struct { *BaseBuilder // contains filtered or unexported fields }
PgsqlBuilder is the builder for PostgreSQL databases.
func (*PgsqlBuilder) AlterColumn ¶
func (b *PgsqlBuilder) AlterColumn(table, col, typ string) *Query
AlterColumn creates a Query that can be used to change the definition of a table column.
func (*PgsqlBuilder) DropIndex ¶
func (b *PgsqlBuilder) DropIndex(table, name string) *Query
DropIndex creates a Query that can be used to remove the named index from a table.
func (*PgsqlBuilder) GeneratePlaceholder ¶
func (b *PgsqlBuilder) GeneratePlaceholder(i int) string
GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID.
func (*PgsqlBuilder) Model ¶
func (b *PgsqlBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*PgsqlBuilder) QueryBuilder ¶
func (b *PgsqlBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*PgsqlBuilder) RenameTable ¶
func (b *PgsqlBuilder) RenameTable(oldName, newName string) *Query
RenameTable creates a Query that can be used to rename a table.
func (*PgsqlBuilder) Select ¶
func (b *PgsqlBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
func (*PgsqlBuilder) Upsert ¶
func (b *PgsqlBuilder) Upsert(table string, cols Params, constraints ...string) *Query
Upsert creates a Query that represents an UPSERT SQL statement. Upsert inserts a row into the table if the primary key or unique index is not found. Otherwise it will update the row with the new values. The keys of cols are the column names, while the values of cols are the corresponding column values to be inserted.
type PostScanner ¶
type PostScanner interface { // PostScan executes right after the struct has been populated // with the DB values, allowing you to further normalize or validate // the loaded data. PostScan() error }
PostScanner is an optional interface used by ScanStruct.
type Query ¶
type Query struct { // FieldMapper maps struct field names to DB column names. FieldMapper FieldMapFunc // LastError contains the last error (if any) of the query. // LastError is cleared by Execute(), Row(), Rows(), One(), and All(). LastError error // LogFunc is used to log the SQL statement being executed. LogFunc LogFunc // PerfFunc is used to log the SQL execution time. It is ignored if nil. // Deprecated: Please use QueryLogFunc and ExecLogFunc instead. PerfFunc PerfFunc // QueryLogFunc is called each time when performing a SQL query that returns data. QueryLogFunc QueryLogFunc // ExecLogFunc is called each time when a SQL statement is executed. ExecLogFunc ExecLogFunc // contains filtered or unexported fields }
Query represents a SQL statement to be executed.
func (*Query) All ¶
All executes the SQL statement and populates all the resulting rows into a slice of struct or NullStringMap. The slice must be given as a pointer. Each slice element must be either a struct or a NullStringMap. Refer to Rows.ScanStruct() and Rows.ScanMap() for more details on how each slice element can be. If the query returns no row, the slice will be an empty slice (not nil).
Example ¶
package main import ( "fmt" "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users LIMIT 10" // fetches data into a slice of struct var users []struct { ID, Name string } db.NewQuery(sql).All(&users) // fetches data into a slice of NullStringMap var users2 []dbx.NullStringMap db.NewQuery(sql).All(&users2) for _, user := range users2 { fmt.Println(user["id"].String, user["name"].String) } }
Output:
func (*Query) Bind ¶
Bind sets the parameters that should be bound to the SQL statement. The parameter placeholders in the SQL statement are in the format of "{:ParamName}".
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { var user struct { ID, Name string } db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users WHERE age>{:age} AND status={:status}" q := db.NewQuery(sql) q.Bind(dbx.Params{"age": 30, "status": 1}).One(&user) }
Output:
func (*Query) Close ¶
Close closes the underlying prepared statement. Close does nothing if the query has not been prepared before.
func (*Query) Column ¶
Column executes the SQL statement and populates the first column of the result into a slice. Note that the parameter must be a pointer to a slice.
func (*Query) One ¶
One executes the SQL statement and populates the first row of the result into a struct or NullStringMap. Refer to Rows.ScanStruct() and Rows.ScanMap() for more details on how to specify the variable to be populated. Note that when the query has no rows in the result set, an sql.ErrNoRows will be returned.
Example ¶
package main import ( "fmt" "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users LIMIT 10" // fetches data into a struct var user struct { ID, Name string } db.NewQuery(sql).One(&user) // fetches data into a NullStringMap var user2 dbx.NullStringMap db.NewQuery(sql).All(user2) fmt.Println(user2["id"].String, user2["name"].String) }
Output:
func (*Query) Params ¶
Params returns the parameters to be bound to the SQL statement represented by this query.
func (*Query) Prepare ¶
Prepare creates a prepared statement for later queries or executions. Close() should be called after finishing all queries.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { var users1, users2, users3 []struct { ID, Name string } db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users WHERE age>{:age} AND status={:status}" q := db.NewQuery(sql).Prepare() defer q.Close() q.Bind(dbx.Params{"age": 30, "status": 1}).All(&users1) q.Bind(dbx.Params{"age": 20, "status": 1}).All(&users2) q.Bind(dbx.Params{"age": 10, "status": 1}).All(&users3) }
Output:
func (*Query) Row ¶
Row executes the SQL statement and populates the first row of the result into a list of variables. Note that the number of the variables should match to that of the columns in the query result. Note that when the query has no rows in the result set, an sql.ErrNoRows will be returned.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users LIMIT 10" // fetches data into a struct var ( id int name string ) db.NewQuery(sql).Row(&id, &name) }
Output:
func (*Query) Rows ¶
Rows executes the SQL statement and returns a Rows object to allow retrieving data row by row.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { var user struct { ID, Name string } db, _ := dbx.Open("mysql", "user:pass@/example") sql := "SELECT id, name FROM users LIMIT 10" rows, _ := db.NewQuery(sql).Rows() for rows.Next() { rows.ScanStruct(&user) // ... } }
Output:
func (*Query) SQL ¶
SQL returns the original SQL used to create the query. The actual SQL (RawSQL) being executed is obtained by replacing the named parameter placeholders with anonymous ones.
func (*Query) WithAllHook ¶
func (q *Query) WithAllHook(fn AllHookFunc) *Query
WithOneHook associates the provided hook function with the query, called on q.All(), allowing you to implement custom slice scan based on the All() argument and/or result.
func (*Query) WithContext ¶
WithContext associates a context with the query.
func (*Query) WithExecHook ¶
func (q *Query) WithExecHook(fn ExecHookFunc) *Query
WithExecHook associates the provided exec hook function with the query.
It is called for every Query resolver (Execute(), One(), All(), Row(), Column()), allowing you to implement auto fail/retry or any other additional handling.
func (*Query) WithOneHook ¶
func (q *Query) WithOneHook(fn OneHookFunc) *Query
WithOneHook associates the provided hook function with the query, called on q.One(), allowing you to implement custom struct scan based on the One() argument and/or result.
type QueryBuilder ¶
type QueryBuilder interface { // BuildSelect generates a SELECT clause from the given selected column names. BuildSelect(cols []string, distinct bool, option string) string // BuildFrom generates a FROM clause from the given tables. BuildFrom(tables []string) string // BuildGroupBy generates a GROUP BY clause from the given group-by columns. BuildGroupBy(cols []string) string // BuildJoin generates a JOIN clause from the given join information. BuildJoin([]JoinInfo, Params) string // BuildWhere generates a WHERE clause from the given expression. BuildWhere(Expression, Params) string // BuildHaving generates a HAVING clause from the given expression. BuildHaving(Expression, Params) string // BuildOrderByAndLimit generates the ORDER BY and LIMIT clauses. BuildOrderByAndLimit(string, []string, int64, int64) string // BuildUnion generates a UNION clause from the given union information. BuildUnion([]UnionInfo, Params) string }
QueryBuilder builds different clauses for a SELECT SQL statement.
type QueryInfo ¶
type QueryInfo struct { Builder Builder Selects []string Distinct bool SelectOption string From []string Where Expression Join []JoinInfo OrderBy []string GroupBy []string Having Expression Union []UnionInfo Limit int64 Offset int64 Params Params Context context.Context BuildHook BuildHookFunc }
QueryInfo represents a debug/info struct with exported SelectQuery fields.
type QueryLogFunc ¶
QueryLogFunc is called each time when performing a SQL query. The "t" parameter gives the time that the SQL statement takes to execute, while rows and err are the result of the query.
type Rows ¶
Rows enhances sql.Rows by providing additional data query methods. Rows can be obtained by calling Query.Rows(). It is mainly used to populate data row by row.
func (*Rows) ScanMap ¶
func (r *Rows) ScanMap(a NullStringMap) error
ScanMap populates the current row of data into a NullStringMap. Note that the NullStringMap must not be nil, or it will panic. The NullStringMap will be populated using column names as keys and their values as the corresponding element values.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") user := dbx.NullStringMap{} sql := "SELECT id, name FROM users LIMIT 10" rows, _ := db.NewQuery(sql).Rows() for rows.Next() { rows.ScanMap(user) // ... } }
Output:
func (*Rows) ScanStruct ¶
ScanStruct populates the current row of data into a struct. The struct must be given as a pointer.
ScanStruct associates struct fields with DB table columns through a field mapping function. It populates a struct field with the data of its associated column. Note that only exported struct fields will be populated.
By default, DefaultFieldMapFunc() is used to map struct fields to table columns. This function separates each word in a field name with a underscore and turns every letter into lower case. For example, "LastName" is mapped to "last_name", "MyID" is mapped to "my_id", and so on. To change the default behavior, set DB.FieldMapper with your custom mapping function. You may also set Query.FieldMapper to change the behavior for particular queries.
Example ¶
package main import ( "github.com/LcTheSecond/dbx" ) func main() { db, _ := dbx.Open("mysql", "user:pass@/example") var user struct { ID, Name string } sql := "SELECT id, name FROM users LIMIT 10" rows, _ := db.NewQuery(sql).Rows() for rows.Next() { rows.ScanStruct(&user) // ... } }
Output:
type SelectQuery ¶
type SelectQuery struct { // FieldMapper maps struct field names to DB column names. FieldMapper FieldMapFunc // TableMapper maps structs to DB table names. TableMapper TableMapFunc // contains filtered or unexported fields }
SelectQuery represents a DB-agnostic SELECT query. It can be built into a DB-specific query by calling the Build() method.
func NewSelectQuery ¶
func NewSelectQuery(builder Builder, db *DB) *SelectQuery
NewSelectQuery creates a new SelectQuery instance.
func (*SelectQuery) All ¶
func (s *SelectQuery) All(slice interface{}) error
All executes the SELECT query and populates all rows of the result into a slice.
Note that the slice must be passed in as a pointer.
If the query does not specify a "from" clause, the method will try to infer the name of the table to be selected from by calling getTableName() which will return either the type name of the slice elements or the TableName() method if the slice element implements the TableModel interface.
func (*SelectQuery) AndBind ¶
func (s *SelectQuery) AndBind(params Params) *SelectQuery
AndBind appends additional parameters to be bound to the query.
func (*SelectQuery) AndGroupBy ¶
func (s *SelectQuery) AndGroupBy(cols ...string) *SelectQuery
AndGroupBy appends additional columns to the existing GROUP BY clause. Column names will be properly quoted.
func (*SelectQuery) AndHaving ¶
func (s *SelectQuery) AndHaving(e Expression) *SelectQuery
AndHaving concatenates a new HAVING condition with the existing one (if any) using "AND".
func (*SelectQuery) AndOrderBy ¶
func (s *SelectQuery) AndOrderBy(cols ...string) *SelectQuery
AndOrderBy appends additional columns to the existing ORDER BY clause. Column names will be properly quoted. A column name can contain "ASC" or "DESC" to indicate its ordering direction.
func (*SelectQuery) AndSelect ¶
func (s *SelectQuery) AndSelect(cols ...string) *SelectQuery
AndSelect adds additional columns to be selected. Column names will be automatically quoted.
func (*SelectQuery) AndWhere ¶
func (s *SelectQuery) AndWhere(e Expression) *SelectQuery
AndWhere concatenates a new WHERE condition with the existing one (if any) using "AND".
func (*SelectQuery) Bind ¶
func (s *SelectQuery) Bind(params Params) *SelectQuery
Bind specifies the parameter values to be bound to the query.
func (*SelectQuery) Build ¶
func (s *SelectQuery) Build() *Query
Build builds the SELECT query and returns an executable Query object.
func (*SelectQuery) Column ¶
func (s *SelectQuery) Column(a interface{}) error
Column builds and executes the SELECT statement and populates the first column of the result into a slice. Note that the parameter must be a pointer to a slice. This is a shortcut to SelectQuery.Build().Column()
func (*SelectQuery) Context ¶
func (q *SelectQuery) Context() context.Context
Context returns the context associated with the query.
func (*SelectQuery) Distinct ¶
func (s *SelectQuery) Distinct(v bool) *SelectQuery
Distinct specifies whether to select columns distinctively. By default, distinct is false.
func (*SelectQuery) From ¶
func (s *SelectQuery) From(tables ...string) *SelectQuery
From specifies which tables to select from. Table names will be automatically quoted.
func (*SelectQuery) GroupBy ¶
func (s *SelectQuery) GroupBy(cols ...string) *SelectQuery
GroupBy specifies the GROUP BY clause. Column names will be properly quoted.
func (*SelectQuery) Having ¶
func (s *SelectQuery) Having(e Expression) *SelectQuery
Having specifies the HAVING clause.
func (*SelectQuery) Info ¶
func (s *SelectQuery) Info() *QueryInfo
Info exports common SelectQuery fields allowing to inspect the current select query options.
func (*SelectQuery) InnerJoin ¶
func (s *SelectQuery) InnerJoin(table string, on Expression) *SelectQuery
InnerJoin specifies an INNER JOIN clause. This is a shortcut method for Join.
func (*SelectQuery) Join ¶
func (s *SelectQuery) Join(typ string, table string, on Expression) *SelectQuery
Join specifies a JOIN clause. The "typ" parameter specifies the JOIN type (e.g. "INNER JOIN", "LEFT JOIN").
func (*SelectQuery) LeftJoin ¶
func (s *SelectQuery) LeftJoin(table string, on Expression) *SelectQuery
LeftJoin specifies a LEFT JOIN clause. This is a shortcut method for Join.
func (*SelectQuery) Limit ¶
func (s *SelectQuery) Limit(limit int64) *SelectQuery
Limit specifies the LIMIT clause. A negative limit means no limit.
func (*SelectQuery) Model ¶
func (s *SelectQuery) Model(pk, model interface{}) error
Model selects the row with the specified primary key and populates the model with the row data.
The model variable should be a pointer to a struct. If the query does not specify a "from" clause, it will use the model struct to determine which table to select data from. It will also use the model to infer the name of the primary key column. Only simple primary key is supported. For composite primary keys, please use Where() to specify the filtering condition.
func (*SelectQuery) Offset ¶
func (s *SelectQuery) Offset(offset int64) *SelectQuery
Offset specifies the OFFSET clause. A negative offset means no offset.
func (*SelectQuery) One ¶
func (s *SelectQuery) One(a interface{}) error
One executes the SELECT query and populates the first row of the result into the specified variable.
If the query does not specify a "from" clause, the method will try to infer the name of the table to be selected from by calling getTableName() which will return either the variable type name or the TableName() method if the variable implements the TableModel interface.
Note that when the query has no rows in the result set, an sql.ErrNoRows will be returned.
func (*SelectQuery) OrHaving ¶
func (s *SelectQuery) OrHaving(e Expression) *SelectQuery
OrHaving concatenates a new HAVING condition with the existing one (if any) using "OR".
func (*SelectQuery) OrWhere ¶
func (s *SelectQuery) OrWhere(e Expression) *SelectQuery
OrWhere concatenates a new WHERE condition with the existing one (if any) using "OR".
func (*SelectQuery) OrderBy ¶
func (s *SelectQuery) OrderBy(cols ...string) *SelectQuery
OrderBy specifies the ORDER BY clause. Column names will be properly quoted. A column name can contain "ASC" or "DESC" to indicate its ordering direction.
func (*SelectQuery) RightJoin ¶
func (s *SelectQuery) RightJoin(table string, on Expression) *SelectQuery
RightJoin specifies a RIGHT JOIN clause. This is a shortcut method for Join.
func (*SelectQuery) Row ¶
func (s *SelectQuery) Row(a ...interface{}) error
Row builds and executes the SELECT query and populates the first row of the result into the specified variables. This is a shortcut to SelectQuery.Build().Row()
func (*SelectQuery) Rows ¶
func (s *SelectQuery) Rows() (*Rows, error)
Rows builds and executes the SELECT query and returns a Rows object for data retrieval purpose. This is a shortcut to SelectQuery.Build().Rows()
func (*SelectQuery) Select ¶
func (s *SelectQuery) Select(cols ...string) *SelectQuery
Select specifies the columns to be selected. Column names will be automatically quoted.
func (*SelectQuery) SelectOption ¶
func (s *SelectQuery) SelectOption(option string) *SelectQuery
SelectOption specifies additional option that should be append to "SELECT".
func (*SelectQuery) Union ¶
func (s *SelectQuery) Union(q *Query) *SelectQuery
Union specifies a UNION clause.
func (*SelectQuery) UnionAll ¶
func (s *SelectQuery) UnionAll(q *Query) *SelectQuery
UnionAll specifies a UNION ALL clause.
func (*SelectQuery) Where ¶
func (s *SelectQuery) Where(e Expression) *SelectQuery
Where specifies the WHERE condition.
func (*SelectQuery) WithBuildHook ¶
func (q *SelectQuery) WithBuildHook(fn BuildHookFunc) *SelectQuery
WithBuildHook runs the provided hook function with the query created on Build().
func (*SelectQuery) WithContext ¶
func (q *SelectQuery) WithContext(ctx context.Context) *SelectQuery
WithContext associates a context with the query.
type SqliteBuilder ¶
type SqliteBuilder struct { *BaseBuilder // contains filtered or unexported fields }
SqliteBuilder is the builder for SQLite databases.
func (*SqliteBuilder) AddForeignKey ¶
func (b *SqliteBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query
AddForeignKey creates a Query that can be used to add a foreign key constraint to a table. The length of cols and refCols must be the same as they refer to the primary and referential columns. The optional "options" parameters will be appended to the SQL statement. They can be used to specify options such as "ON DELETE CASCADE".
func (*SqliteBuilder) AddPrimaryKey ¶
func (b *SqliteBuilder) AddPrimaryKey(table, name string, cols ...string) *Query
AddPrimaryKey creates a Query that can be used to specify primary key(s) for a table. The "name" parameter specifies the name of the primary key constraint.
func (*SqliteBuilder) AlterColumn ¶
func (b *SqliteBuilder) AlterColumn(table, col, typ string) *Query
AlterColumn creates a Query that can be used to change the definition of a table column.
func (*SqliteBuilder) DropForeignKey ¶
func (b *SqliteBuilder) DropForeignKey(table, name string) *Query
DropForeignKey creates a Query that can be used to remove the named foreign key constraint from a table.
func (*SqliteBuilder) DropIndex ¶
func (b *SqliteBuilder) DropIndex(table, name string) *Query
DropIndex creates a Query that can be used to remove the named index from a table.
func (*SqliteBuilder) DropPrimaryKey ¶
func (b *SqliteBuilder) DropPrimaryKey(table, name string) *Query
DropPrimaryKey creates a Query that can be used to remove the named primary key constraint from a table.
func (*SqliteBuilder) Model ¶
func (b *SqliteBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*SqliteBuilder) QueryBuilder ¶
func (b *SqliteBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*SqliteBuilder) QuoteSimpleColumnName ¶
func (b *SqliteBuilder) QuoteSimpleColumnName(s string) string
QuoteSimpleColumnName quotes a simple column name. A simple column name does not contain any table prefix.
func (*SqliteBuilder) QuoteSimpleTableName ¶
func (b *SqliteBuilder) QuoteSimpleTableName(s string) string
QuoteSimpleTableName quotes a simple table name. A simple table name does not contain any schema prefix.
func (*SqliteBuilder) RenameTable ¶
func (b *SqliteBuilder) RenameTable(oldName, newName string) *Query
RenameTable creates a Query that can be used to rename a table.
func (*SqliteBuilder) Select ¶
func (b *SqliteBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
func (*SqliteBuilder) TruncateTable ¶
func (b *SqliteBuilder) TruncateTable(table string) *Query
TruncateTable creates a Query that can be used to truncate a table.
type StandardBuilder ¶
type StandardBuilder struct { *BaseBuilder // contains filtered or unexported fields }
StandardBuilder is the builder that is used by DB for an unknown driver.
func (*StandardBuilder) Model ¶
func (b *StandardBuilder) Model(model interface{}) *ModelQuery
Model returns a new ModelQuery object that can be used to perform model-based DB operations. The model passed to this method should be a pointer to a model struct.
func (*StandardBuilder) QueryBuilder ¶
func (b *StandardBuilder) QueryBuilder() QueryBuilder
QueryBuilder returns the query builder supporting the current DB.
func (*StandardBuilder) Select ¶
func (b *StandardBuilder) Select(cols ...string) *SelectQuery
Select returns a new SelectQuery object that can be used to build a SELECT statement. The parameters to this method should be the list column names to be selected. A column name may have an optional alias name. For example, Select("id", "my_name AS name").
type TableMapFunc ¶
type TableMapFunc func(a interface{}) string
TableMapFunc converts a sample struct into a DB table name.
type TableModel ¶
type TableModel interface {
TableName() string
}
TableModel is the interface that should be implemented by models which have unconventional table names.
type Tx ¶
type Tx struct { Builder // contains filtered or unexported fields }
Tx enhances sql.Tx with additional querying methods.
type VarTypeError ¶
type VarTypeError string
VarTypeError indicates a variable type error when trying to populating a variable with DB result.