Documentation ΒΆ
Overview ΒΆ
Package xl provides convenience functions for building SQL queries.
Index ΒΆ
- func MultiExec(e sqlx.Execer, query string) error
- func NextInt64(db Queryer, seq string) (int64, error)
- func Placeholders(n int) string
- func SetLogger(fn Logger)
- type Context
- type DB
- type DeleteQuery
- func (q *DeleteQuery) Exec(e Execer) (sql.Result, error)
- func (q *DeleteQuery) ExecCount(e Execer) (int64, error)
- func (q *DeleteQuery) ExecErr(e Execer) error
- func (q *DeleteQuery) ExecOne(e Execer) error
- func (q *DeleteQuery) Statement(d Dialect) (*Statement, error)
- func (q *DeleteQuery) Where(expr string, params ...interface{})
- type Dialect
- type Execer
- type InsertQuery
- func (q *InsertQuery) Exec(e Execer) (sql.Result, error)
- func (q *InsertQuery) ExecErr(e Execer) error
- func (q *InsertQuery) ExecId(e Execer) (int64, error)
- func (q *InsertQuery) First(queryer Queryer, dest interface{}) error
- func (q *InsertQuery) Returning(expr string)
- func (q *InsertQuery) Set(name string, param interface{})
- func (q *InsertQuery) SetRaw(name, rawvalue string)
- func (q *InsertQuery) Statement(d Dialect) (*Statement, error)
- type Logger
- type NamedValue
- type Queryer
- type SelectQuery
- func (q *SelectQuery) All(queryer Queryer, dest interface{}) error
- func (q *SelectQuery) Clone() *SelectQuery
- func (q *SelectQuery) Column(expr string, params ...interface{})
- func (q *SelectQuery) Columns(exprs ...string)
- func (q *SelectQuery) ColumnsAlias(columns ...string)
- func (q *SelectQuery) Distinct()
- func (q *SelectQuery) First(queryer Queryer, dest interface{}) error
- func (q *SelectQuery) From(table string) *SelectQuery
- func (q *SelectQuery) FromAs(table, alias string) *SelectQuery
- func (q *SelectQuery) FromLateralSubselectAs(sq *SelectQuery, alias string)
- func (q *SelectQuery) FromSubselect(sq *SelectQuery)
- func (q *SelectQuery) FromSubselectAs(sq *SelectQuery, alias string)
- func (q *SelectQuery) GroupBy(expr string)
- func (q *SelectQuery) InnerJoin(jq *SelectQuery, cond string, params ...interface{})
- func (q *SelectQuery) Join(jq *SelectQuery, joinType, cond string, params ...interface{})
- func (q *SelectQuery) LeftJoin(jq *SelectQuery, cond string, params ...interface{})
- func (q *SelectQuery) LimitOffset(limit, offset int64) *SelectQuery
- func (q *SelectQuery) OrderBy(expr string, params ...interface{})
- func (q *SelectQuery) Queryx(queryer Queryer) (*sqlx.Rows, error)
- func (q *SelectQuery) Statement(d Dialect) (*Statement, error)
- func (q *SelectQuery) Total(queryer Queryer) (int, error)
- func (q *SelectQuery) Where(expr string, params ...interface{})
- type Statement
- func (s *Statement) All(q Queryer, dest interface{}) error
- func (s *Statement) Exec(e Execer) (sql.Result, error)
- func (s *Statement) ExecCount(e Execer) (int64, error)
- func (s *Statement) ExecOne(e Execer) error
- func (s *Statement) First(q Queryer, dest interface{}) error
- func (s *Statement) QueryRowx(q Queryer) *sqlx.Row
- func (s *Statement) Queryx(q Queryer) (*sqlx.Rows, error)
- type Statementer
- type TXContext
- func (c TXContext) Begin() (TXContext, error)
- func (c TXContext) Commit() error
- func (c TXContext) DB() *DB
- func (c TXContext) Deadline() (time.Time, bool)
- func (c TXContext) Done() <-chan struct{}
- func (c TXContext) Err() error
- func (c TXContext) Rollback() error
- func (c TXContext) Tx() *Tx
- func (c TXContext) Value(key interface{}) interface{}
- func (c TXContext) WithValue(key, value interface{}) TXContext
- type Tx
- func (tx *Tx) Beginxl() (*Tx, error)
- func (tx *Tx) Commit() error
- func (tx *Tx) Dialect() Dialect
- func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (tx *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row
- func (tx *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (tx *Tx) Rollback() error
- type UpdateQuery
- func (q *UpdateQuery) Exec(e Execer) (sql.Result, error)
- func (q *UpdateQuery) ExecCount(e Execer) (int64, error)
- func (q *UpdateQuery) ExecErr(e Execer) error
- func (q *UpdateQuery) ExecOne(e Execer) error
- func (q *UpdateQuery) First(queryer Queryer, dest interface{}) error
- func (q *UpdateQuery) Returning(expr string)
- func (q *UpdateQuery) Set(name string, param interface{})
- func (q *UpdateQuery) SetNull(name string)
- func (q *UpdateQuery) SetRaw(name, rawvalue string)
- func (q *UpdateQuery) Statement(d Dialect) (*Statement, error)
- func (q *UpdateQuery) Where(expr string, params ...interface{})
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func MultiExec ΒΆ
MultiExec executes a batch of SQL statements. Based on MultiExec from sqlx_test.go at github.com/jmoiron/sqlx.
func Placeholders ΒΆ
Types ΒΆ
type DB ΒΆ
A DB is a wrapper type around sqlx.DB that implements xl.Execer and xl.Queryer interfaces.
type DeleteQuery ΒΆ
type DeleteQuery struct {
// contains filtered or unexported fields
}
func Delete ΒΆ
func Delete(table string) *DeleteQuery
func (*DeleteQuery) ExecErr ΒΆ
func (q *DeleteQuery) ExecErr(e Execer) error
func (*DeleteQuery) ExecOne ΒΆ
func (q *DeleteQuery) ExecOne(e Execer) error
func (*DeleteQuery) Where ΒΆ
func (q *DeleteQuery) Where(expr string, params ...interface{})
Where adds a WHERE clause. All WHERE clauses will be joined with AND. Note that Where doesn't surround the expression with parentheses. See SelectQuery doc for example.
type Dialect ΒΆ
type Dialect struct { // sqlx bind type BindType int }
A Dialect keep tracks of SQL dialect-specific settings.
type Execer ΒΆ
type Execer interface { Dialect() Dialect Exec(query string, args ...interface{}) (sql.Result, error) }
Execer can execute an SQL query and is also aware of which SQL dialect the database speaks.
type InsertQuery ΒΆ
type InsertQuery struct {
// contains filtered or unexported fields
}
func Insert ΒΆ
func Insert(table string) *InsertQuery
func (*InsertQuery) ExecErr ΒΆ
func (q *InsertQuery) ExecErr(e Execer) error
func (*InsertQuery) First ΒΆ
func (q *InsertQuery) First(queryer Queryer, dest interface{}) error
func (*InsertQuery) Returning ΒΆ
func (q *InsertQuery) Returning(expr string)
func (*InsertQuery) Set ΒΆ
func (q *InsertQuery) Set(name string, param interface{})
func (*InsertQuery) SetRaw ΒΆ
func (q *InsertQuery) SetRaw(name, rawvalue string)
type NamedValue ΒΆ
type NamedValue interface {
Name() string
}
type Queryer ΒΆ
type Queryer interface { Dialect() Dialect Query(query string, args ...interface{}) (*sql.Rows, error) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) QueryRowx(query string, args ...interface{}) *sqlx.Row }
Execer can execute an SQL query and fetch fetch the ros and is also aware of which SQL dialect the database speaks.
type SelectQuery ΒΆ
type SelectQuery struct {
// contains filtered or unexported fields
}
func From ΒΆ
func From(table string) *SelectQuery
func FromAs ΒΆ
func FromAs(table, alias string) *SelectQuery
func NewSelect ΒΆ
func NewSelect() *SelectQuery
func Select ΒΆ
func Select(expr string, params ...interface{}) *SelectQuery
func SelectAlias ΒΆ
func SelectAlias(cols ...string) *SelectQuery
func (*SelectQuery) All ΒΆ
func (q *SelectQuery) All(queryer Queryer, dest interface{}) error
func (*SelectQuery) Clone ΒΆ
func (q *SelectQuery) Clone() *SelectQuery
func (*SelectQuery) Column ΒΆ
func (q *SelectQuery) Column(expr string, params ...interface{})
func (*SelectQuery) Columns ΒΆ
func (q *SelectQuery) Columns(exprs ...string)
func (*SelectQuery) ColumnsAlias ΒΆ
func (q *SelectQuery) ColumnsAlias(columns ...string)
func (*SelectQuery) Distinct ΒΆ
func (q *SelectQuery) Distinct()
func (*SelectQuery) First ΒΆ
func (q *SelectQuery) First(queryer Queryer, dest interface{}) error
func (*SelectQuery) From ΒΆ
func (q *SelectQuery) From(table string) *SelectQuery
func (*SelectQuery) FromAs ΒΆ
func (q *SelectQuery) FromAs(table, alias string) *SelectQuery
func (*SelectQuery) FromLateralSubselectAs ΒΆ
func (q *SelectQuery) FromLateralSubselectAs(sq *SelectQuery, alias string)
func (*SelectQuery) FromSubselect ΒΆ
func (q *SelectQuery) FromSubselect(sq *SelectQuery)
func (*SelectQuery) FromSubselectAs ΒΆ
func (q *SelectQuery) FromSubselectAs(sq *SelectQuery, alias string)
func (*SelectQuery) GroupBy ΒΆ
func (q *SelectQuery) GroupBy(expr string)
func (*SelectQuery) InnerJoin ΒΆ
func (q *SelectQuery) InnerJoin(jq *SelectQuery, cond string, params ...interface{})
func (*SelectQuery) Join ΒΆ
func (q *SelectQuery) Join(jq *SelectQuery, joinType, cond string, params ...interface{})
func (*SelectQuery) LeftJoin ΒΆ
func (q *SelectQuery) LeftJoin(jq *SelectQuery, cond string, params ...interface{})
func (*SelectQuery) LimitOffset ΒΆ
func (q *SelectQuery) LimitOffset(limit, offset int64) *SelectQuery
func (*SelectQuery) OrderBy ΒΆ
func (q *SelectQuery) OrderBy(expr string, params ...interface{})
func (*SelectQuery) Total ΒΆ
func (q *SelectQuery) Total(queryer Queryer) (int, error)
Count runs this query without LIMIT/OFFSET and returns the COUNT.
func (*SelectQuery) Where ΒΆ
func (q *SelectQuery) Where(expr string, params ...interface{})
Where adds a WHERE clause. All WHERE clauses will be joined with AND. Note that Where doesn't surround the expression with parentheses.
Example ΒΆ
package main import ( "fmt" "github.com/tomyl/xl" ) func main() { q := xl.Select("salary").From("employee") q.Where("name=?", "Alice Γrn") q.Where("(city='Hong Kong' OR city='Stockholm')") st, _ := q.Statement(xl.Dialect{}) fmt.Println(st.SQL) }
Output: SELECT salary FROM employee WHERE name=? AND (city='Hong Kong' OR city='Stockholm')
type Statement ΒΆ
type Statement struct { SQL string Params []interface{} }
A Statement represents a complied SQL statement and its parameters. Bind type is undefined, i.e. the statement is must use correct bind type already.
func (*Statement) ExecOne ΒΆ
Execute compiled statement and return error if affected rows is not exactly 1.
type Statementer ΒΆ
type Tx ΒΆ
type Tx struct {
// contains filtered or unexported fields
}
Wrapper type around sqlx.Tx that implements xl.Execer and xl.Queryer interfaces.
type UpdateQuery ΒΆ
type UpdateQuery struct {
// contains filtered or unexported fields
}
func Update ΒΆ
func Update(table string) *UpdateQuery
func (*UpdateQuery) ExecErr ΒΆ
func (q *UpdateQuery) ExecErr(e Execer) error
func (*UpdateQuery) ExecOne ΒΆ
func (q *UpdateQuery) ExecOne(e Execer) error
func (*UpdateQuery) First ΒΆ
func (q *UpdateQuery) First(queryer Queryer, dest interface{}) error
func (*UpdateQuery) Returning ΒΆ
func (q *UpdateQuery) Returning(expr string)
func (*UpdateQuery) Set ΒΆ
func (q *UpdateQuery) Set(name string, param interface{})
Set sets column to provided parameter.
q.Set("title", userTitle)
func (*UpdateQuery) SetNull ΒΆ
func (q *UpdateQuery) SetNull(name string)
SetNull is a shorthand for SetRaw(col, "NULL").
q.SetNull("error")
func (*UpdateQuery) SetRaw ΒΆ
func (q *UpdateQuery) SetRaw(name, rawvalue string)
SetRaw sets column to provided SQL expression. The value will not be escaped in any way. Use Set() for values provided by untrusted sources.
q.SetRaw("updated_at", "current_timestamp")
func (*UpdateQuery) Where ΒΆ
func (q *UpdateQuery) Where(expr string, params ...interface{})
Where adds a WHERE clause. All WHERE clauses will be joined with AND. Note that Where doesn't surround the expression with parentheses. See SelectQuery doc for example.