Documentation ¶
Index ¶
- Constants
- Variables
- func Build(e Expr) (sql string, args []interface{})
- func Exec(ctx context.Context, ex Executor, expr Expr) (sql.Result, error)
- func FormatSQLArgs(sql string, args []interface{}) string
- func Query(ctx context.Context, ex Executor, expr Expr) (*sql.Rows, error)
- func QueryRow(ctx context.Context, ex Executor, expr Expr) *sql.Row
- func SetDialect(d *Dialect)
- type And
- type ArgStyle
- type Array
- type Builder
- func (b *Builder) Append(item interface{})
- func (b *Builder) AppendAll(items ...interface{})
- func (b *Builder) AppendExpr(expr Expr)
- func (b *Builder) AppendName(s string)
- func (b *Builder) AppendRaw(s string)
- func (b *Builder) Args() []interface{}
- func (b *Builder) SQL() string
- func (b *Builder) SQLArgs() (sql string, args []interface{})
- func (b *Builder) String() string
- type Clause
- type Column
- type Delete
- func (s *Delete) AddField(fields ...Expr)
- func (s *Delete) AddWhere(conds ...Expr)
- func (s Delete) AppendToSQLBuilder(b *Builder)
- func (s *Delete) Exec(ctx context.Context, ex Executor) (sql.Result, error)
- func (s *Delete) Query(ctx context.Context, ex Executor) (*sql.Rows, error)
- func (s *Delete) QueryRow(ctx context.Context, ex Executor) *sql.Row
- type Dialect
- type Executor
- type Expr
- func As(v Expr, name Column) Expr
- func Asc(v Expr) Expr
- func Count(v Expr) Expr
- func Desc(v Expr) Expr
- func Dot(a, b Expr) Expr
- func Eq(lhs interface{}, rhs interface{}) Expr
- func Func(name string, args ...interface{}) Expr
- func In(lhs interface{}, items Expr) Expr
- func InnerJoin(a Expr, aCol Column, b Expr, bCol Column) Expr
- func IsNotNull(v interface{}) Expr
- func IsNull(v interface{}) Expr
- func Like(lhs interface{}, rhs interface{}) Expr
- func LikeCaseInsensitive(lhs interface{}, rhs interface{}) Expr
- func Max(v Expr) Expr
- func MaybeForUpdate(forUpdate bool) Expr
- func Min(v Expr) Expr
- func Not(v interface{}) Expr
- func NotLike(lhs interface{}, rhs interface{}) Expr
- func NotLikeCaseInsensitive(lhs interface{}, rhs interface{}) Expr
- func Op(lhs interface{}, op string, rhs interface{}) Expr
- func Parens(v Expr) Expr
- func Qualified(exprs ...Expr) Expr
- func Value(v interface{}) Expr
- type Fieldable
- type Fragment
- type Insert
- func (s *Insert) AddField(fields ...Expr)
- func (s Insert) AppendToSQLBuilder(b *Builder)
- func (s *Insert) Exec(ctx context.Context, ex Executor) (sql.Result, error)
- func (s *Insert) Query(ctx context.Context, ex Executor) (*sql.Rows, error)
- func (s *Insert) QueryRow(ctx context.Context, ex Executor) *sql.Row
- func (s *Insert) Set(field Expr, value interface{})
- type LimitExpr
- type List
- type Or
- type OrderBy
- type Raw
- type Returning
- type Select
- type Settable
- type Setter
- type Table
- type Update
- func (s *Update) AddField(fields ...Expr)
- func (s *Update) AddWhere(conds ...Expr)
- func (s Update) AppendToSQLBuilder(b *Builder)
- func (s *Update) Exec(ctx context.Context, ex Executor) (sql.Result, error)
- func (s *Update) Query(ctx context.Context, ex Executor) (*sql.Rows, error)
- func (s *Update) QueryRow(ctx context.Context, ex Executor) *sql.Row
- func (s *Update) Set(field Expr, value interface{})
- type Where
- type Whereable
Examples ¶
Constants ¶
Variables ¶
View Source
var MySQLDialect = &Dialect{ Name: "MySQL", ArgStyle: QuestionMarkArgs, }
View Source
var PostgresDialect = &Dialect{ Name: "PostgreSQL", ArgStyle: DollarNumberArgs, }
View Source
var SQLiteDialect = &Dialect{ Name: "SQLite", ArgStyle: QuestionMarkArgs, }
Functions ¶
func FormatSQLArgs ¶
func SetDialect ¶
func SetDialect(d *Dialect)
Types ¶
type Array ¶
type Array []interface{}
func ArrayOfInt64s ¶
func ArrayOfInts ¶
func ArrayOfStrings ¶
func (Array) AppendToSQLBuilder ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func (*Builder) AppendExpr ¶
func (*Builder) AppendName ¶
type Clause ¶
func (Clause) AppendToSQLBuilder ¶
type Delete ¶
Example ¶
package main import ( "fmt" "github.com/andreyvit/sqlexpr" ) func main() { const ( accounts = sqlexpr.Table("accounts") id = sqlexpr.Column("id") email = sqlexpr.Column("email") name = sqlexpr.Column("name") notes = sqlexpr.Column("notes") deleted = sqlexpr.Column("deleted") ) s := sqlexpr.Delete{Table: accounts} s.AddWhere(sqlexpr.Eq(id, 42)) sql, args := sqlexpr.Build(s) fmt.Println(sql) fmt.Printf("%#v", args) }
Output: DELETE FROM accounts WHERE id = $1 []interface {}{42}
func (Delete) AppendToSQLBuilder ¶
type Dialect ¶
func (*Dialect) FormatPlaceholder ¶
type Executor ¶
type Executor interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
Executor is compatible with *sql.DB, *sql.Tx (or an arbitrary middleman)
type Expr ¶
type Expr interface {
AppendToSQLBuilder(b *Builder)
}
func LikeCaseInsensitive ¶
func LikeCaseInsensitive(lhs interface{}, rhs interface{}) Expr
func MaybeForUpdate ¶
func NotLikeCaseInsensitive ¶
func NotLikeCaseInsensitive(lhs interface{}, rhs interface{}) Expr
type Fieldable ¶
type Fieldable interface {
AddField(fields ...Expr)
}
Fieldable represents SELECTs (SELECT field list) and INSERTs/UPDATEs/DELETEs (RETURNING clause)
type Insert ¶
Example ¶
package main import ( "fmt" "github.com/andreyvit/sqlexpr" ) func main() { const ( accounts = sqlexpr.Table("accounts") id = sqlexpr.Column("id") email = sqlexpr.Column("email") name = sqlexpr.Column("name") notes = sqlexpr.Column("notes") updated_at = sqlexpr.Column("updated_at") deleted = sqlexpr.Column("deleted") ) s := sqlexpr.Insert{Table: accounts} s.Set(email, "john@example.com") s.Set(name, "John Doe") s.Set(notes, "Little Johny") s.Set(updated_at, sqlexpr.NOW) s.AddField(id) sql, args := sqlexpr.Build(s) fmt.Println(sql) fmt.Printf("%#v", args) }
Output: INSERT INTO accounts (email, name, notes, updated_at) VALUES ($1, $2, $3, NOW()) RETURNING id []interface {}{"john@example.com", "John Doe", "Little Johny"}
func (Insert) AppendToSQLBuilder ¶
type Select ¶
type Select struct { Leading Expr From Expr Fields List Where Where Grouping Expr OrderBy OrderBy Limit int Trailing Expr }
Example ¶
package main import ( "fmt" "github.com/andreyvit/sqlexpr" ) func main() { const ( accounts = sqlexpr.Table("accounts") id = sqlexpr.Column("id") email = sqlexpr.Column("email") name = sqlexpr.Column("name") notes = sqlexpr.Column("notes") deleted = sqlexpr.Column("deleted") ) includeNotes := true query := "%abc%" s := sqlexpr.Select{From: accounts} s.AddField(id, email, name) if includeNotes { s.AddField(notes) } s.AddWhere(sqlexpr.Or{sqlexpr.Like(name, query), sqlexpr.Like(notes, query)}) s.AddWhere(sqlexpr.Not(deleted)) sql, args := sqlexpr.Build(s) fmt.Println(sql) fmt.Printf("%#v", args) }
Output: SELECT id, email, name, notes FROM accounts WHERE (name LIKE $1 OR notes LIKE $2) AND NOT deleted []interface {}{"%abc%", "%abc%"}
func (Select) AppendToSQLBuilder ¶
type Settable ¶
type Settable interface {
Set(field Expr, value interface{})
}
Settable represents INSERTs and UPDATEs
type Update ¶
type Update struct { Table Expr Leading Expr Setters []Setter Where Where Trailing Expr Returning Returning }
Example ¶
package main import ( "fmt" "github.com/andreyvit/sqlexpr" ) func main() { const ( accounts = sqlexpr.Table("accounts") id = sqlexpr.Column("id") email = sqlexpr.Column("email") name = sqlexpr.Column("name") notes = sqlexpr.Column("notes") updated_at = sqlexpr.Column("updated_at") deleted = sqlexpr.Column("deleted") ) s := sqlexpr.Update{Table: accounts} s.Set(email, "john@example.com") s.Set(name, "John Doe") s.Set(notes, "Little Johny") s.Set(updated_at, sqlexpr.NOW) s.AddWhere(sqlexpr.Eq(id, 42)) s.AddField(updated_at) sql, args := sqlexpr.Build(s) fmt.Println(sql) fmt.Printf("%#v", args) }
Output: UPDATE accounts SET email = $1, name = $2, notes = $3, updated_at = NOW() WHERE id = $4 RETURNING updated_at []interface {}{"john@example.com", "John Doe", "Little Johny", 42}
func (Update) AppendToSQLBuilder ¶
Click to show internal directories.
Click to hide internal directories.