Documentation
¶
Overview ¶
Package leopards is a simple ORM for golang
Index ¶
- Constants
- func As(ident string, as string) string
- func Asc(column string) string
- func Avg(ident string) string
- func Bit2Uint(v []byte) uint64
- func Count(ident string) string
- func DSN(opt *OpenOptions) string
- func Desc(column string) string
- func Distinct(idents ...string) string
- func FieldsIn[T any](col string, val ...T) func(s *Selector)
- func Key[K comparable](data any, key any) K
- func Lower(ident string) string
- func Max(ident string) string
- func Min(ident string) string
- func Pick[K comparable](data any, index int) K
- func StringOmitErr(value any) string
- func Sum(ident string) string
- type Builder
- func (b *Builder) AddError(err error) *Builder
- func (b *Builder) Arg(a any) *Builder
- func (b *Builder) Argf(format string, a any) *Builder
- func (b *Builder) Args(a ...any) *Builder
- func (b *Builder) Comma() *Builder
- func (b Builder) Dialect() string
- func (b *Builder) Err() error
- func (b *Builder) Ident(s string) *Builder
- func (b *Builder) IdentComma(s ...string) *Builder
- func (b *Builder) Join(qs ...Querier) *Builder
- func (b *Builder) JoinComma(qs ...Querier) *Builder
- func (b *Builder) Len() int
- func (b *Builder) Nested(f func(*Builder)) *Builderdeprecated
- func (b *Builder) Pad() *Builder
- func (b *Builder) Quote(ident string) string
- func (b *Builder) Reset() *Builder
- func (b *Builder) S(s string) *Builder
- func (b *Builder) SetDialect(dialect string)
- func (b *Builder) SetTotal(total int)
- func (b *Builder) String() string
- func (b Builder) Total() int
- func (b *Builder) Wrap(f func(*Builder)) *Builder
- func (b *Builder) WriteByte(c byte) *Builder
- func (b *Builder) WriteOp(op Op) *Builder
- func (b *Builder) WriteString(s string) *Builder
- type ColumnBuilder
- type ConflictOption
- func ConflictColumns(names ...string) ConflictOption
- func ConflictConstraint(name string) ConflictOption
- func ConflictWhere(p *Predicate) ConflictOption
- func DoNothing() ConflictOption
- func ResolveWith(fn func(*UpdateSet)) ConflictOption
- func ResolveWithIgnore() ConflictOption
- func ResolveWithNewValues() ConflictOption
- func UpdateWhere(p *Predicate) ConflictOption
- type DB
- func (b *DB) Commit(ctx context.Context) error
- func (b *DB) Delete() *DeleteBuilder
- func (b *DB) Insert() *InsertBuilder
- func (b *DB) InterceptorsAfterDelete(ii func(*DeleteBuilder, any))
- func (b *DB) InterceptorsAfterInsert(ii func(*InsertBuilder, any))
- func (b *DB) InterceptorsAfterQuery(ia func(*Selector, any))
- func (b *DB) InterceptorsAfterUpdate(ii func(*UpdateBuilder, any))
- func (b *DB) InterceptorsDelete(ii func(*DeleteBuilder))
- func (b *DB) InterceptorsInsert(ii func(*InsertBuilder))
- func (b *DB) InterceptorsQuery(iq func(*Selector))
- func (b *DB) InterceptorsUpdate(ii func(*UpdateBuilder))
- func (b *DB) Query() *Selector
- func (b *DB) Rollback(ctx context.Context) error
- func (b *DB) Scan(rows *sql.Rows, dest any) error
- func (b *DB) ScanSlice(rows *sql.Rows, dest any) error
- func (b *DB) TX(ctx context.Context) (*DB, error)
- func (b *DB) Table(name string) *SelectTable
- func (b *DB) Update() *UpdateBuilder
- type DeleteBuilder
- func (d *DeleteBuilder) Exec(ctx context.Context) (sql.Result, error)
- func (d *DeleteBuilder) FromSelect(s *Selector) *DeleteBuilder
- func (d *DeleteBuilder) Save(ctx context.Context) (sql.Result, error)
- func (d *DeleteBuilder) Schema(name string) *DeleteBuilder
- func (d *DeleteBuilder) Table(table string) *DeleteBuilder
- func (d *DeleteBuilder) Where(p *Predicate) *DeleteBuilder
- func (d *DeleteBuilder) WhereMap(cond map[string]any) *DeleteBuilder
- func (d *DeleteBuilder) WhereOrMap(cond map[string]any) *DeleteBuilder
- type DescribeBuilder
- type DialectBuilder
- func (d *DialectBuilder) AlterIndex(name string) *IndexAlter
- func (d *DialectBuilder) AlterTable(name string) *TableAlter
- func (d *DialectBuilder) Column(name string) *ColumnBuilder
- func (d *DialectBuilder) CreateIndex(name string) *IndexBuilder
- func (d *DialectBuilder) CreateTable(name string) *TableBuilder
- func (d *DialectBuilder) Delete(driver *DB) *DeleteBuilder
- func (d *DialectBuilder) Describe(name string) *DescribeBuilder
- func (d *DialectBuilder) DropIndex(name string) *DropIndexBuilder
- func (d *DialectBuilder) Expr(f func(*Builder)) Querier
- func (d *DialectBuilder) Insert(driver *DB) *InsertBuilder
- func (d *DialectBuilder) Select(driver *DB, columns ...string) *Selector
- func (d *DialectBuilder) SelectExpr(exprs ...Querier) *Selector
- func (d *DialectBuilder) String(f func(*Builder)) string
- func (d *DialectBuilder) Table(name string) *SelectTable
- func (d *DialectBuilder) Update(driver *DB) *UpdateBuilder
- func (d *DialectBuilder) With(name string) *WithBuilder
- type DropIndexBuilder
- type ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) Columns(s ...string) *ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) OnDelete(action string) *ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) OnUpdate(action string) *ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) Reference(r *ReferenceBuilder) *ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) Symbol(s string) *ForeignKeyBuilder
- type Func
- type IndexAlter
- type IndexBuilder
- func (i *IndexBuilder) Column(column string) *IndexBuilder
- func (i *IndexBuilder) Columns(columns ...string) *IndexBuilder
- func (i *IndexBuilder) IfNotExists() *IndexBuilder
- func (i *IndexBuilder) Table(table string) *IndexBuilder
- func (i *IndexBuilder) Unique() *IndexBuilder
- func (i *IndexBuilder) Using(method string) *IndexBuilder
- type InsertBuilder
- func (i *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (i *InsertBuilder) Default() *InsertBuilder
- func (i *InsertBuilder) OnConflict(opts ...ConflictOption) *InsertBuilder
- func (i *InsertBuilder) QueryErr() (string, []any, error)
- func (i *InsertBuilder) Returning(columns ...string) *InsertBuilder
- func (i *InsertBuilder) Save(ctx context.Context) (sql.Result, error)
- func (i *InsertBuilder) Schema(name string) *InsertBuilder
- func (i *InsertBuilder) Set(column string, v any) *InsertBuilder
- func (i *InsertBuilder) SetMap(kv map[string]any) *InsertBuilder
- func (i *InsertBuilder) Table(table string) *InsertBuilder
- func (i *InsertBuilder) Values(values ...any) *InsertBuilder
- type LockAction
- type LockOption
- type LockOptions
- type LockStrength
- type Op
- type OpenOptions
- type ParamFormatter
- type Predicate
- func And(preds ...*Predicate) *Predicate
- func Between(col string, v1, v2 any) *Predicate
- func ColumnsEQ(col1, col2 string) *Predicate
- func ColumnsGT(col1, col2 string) *Predicate
- func ColumnsGTE(col1, col2 string) *Predicate
- func ColumnsHasPrefix(col, prefixC string) *Predicate
- func ColumnsLT(col1, col2 string) *Predicate
- func ColumnsLTE(col1, col2 string) *Predicate
- func ColumnsNEQ(col1, col2 string) *Predicate
- func ColumnsOp(col1, col2 string, op Op) *Predicate
- func CompositeGT(columns []string, args ...any) *Predicate
- func CompositeLT(columns []string, args ...any) *Predicate
- func Contains(col, sub string) *Predicate
- func ContainsFold(col, sub string) *Predicate
- func EQ(col string, value any) *Predicate
- func EqualFold(col, sub string) *Predicate
- func Exists(query Querier) *Predicate
- func ExprP(exr string, args ...any) *Predicate
- func False() *Predicate
- func GT(col string, value any) *Predicate
- func GTE(col string, value any) *Predicate
- func HasPrefix(col, prefix string) *Predicate
- func HasSuffix(col, suffix string) *Predicate
- func In(col string, args ...any) *Predicate
- func InInts(col string, args ...int) *Predicate
- func InValues(col string, args ...driver.Value) *Predicate
- func IsFalse(col string) *Predicate
- func IsNull(col string) *Predicate
- func IsTrue(col string) *Predicate
- func LT(col string, value any) *Predicate
- func LTE(col string, value any) *Predicate
- func Like(col, pattern string) *Predicate
- func NEQ(col string, value any) *Predicate
- func Not(pred *Predicate) *Predicate
- func NotExists(query Querier) *Predicate
- func NotIn(col string, args ...any) *Predicate
- func NotNull(col string) *Predicate
- func Or(preds ...*Predicate) *Predicate
- func P(fns ...func(*Builder)) *Predicate
- func (p *Predicate) Append(f func(*Builder)) *Predicate
- func (p *Predicate) BetweenAnd(col string, v1, v2 any) *Predicate
- func (p *Predicate) ColumnsEQ(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsGT(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsGTE(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsHasPrefix(col, prefixC string) *Predicate
- func (p *Predicate) ColumnsLT(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsLTE(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsNEQ(col1, col2 string) *Predicate
- func (p *Predicate) ColumnsOp(col1, col2 string, op Op) *Predicate
- func (p *Predicate) CompositeGT(columns []string, args ...any) *Predicate
- func (p *Predicate) CompositeLT(columns []string, args ...any) *Predicate
- func (p *Predicate) Contains(col, substr string) *Predicate
- func (p *Predicate) ContainsFold(col, substr string) *Predicate
- func (p *Predicate) EQ(col string, arg any) *Predicate
- func (p *Predicate) EqualFold(col, sub string) *Predicate
- func (p *Predicate) Exists(query Querier) *Predicate
- func (p *Predicate) False() *Predicate
- func (p *Predicate) GT(col string, arg any) *Predicate
- func (p *Predicate) GTE(col string, arg any) *Predicate
- func (p *Predicate) HasPrefix(col, prefix string) *Predicate
- func (p *Predicate) HasSuffix(col, suffix string) *Predicate
- func (p *Predicate) In(col string, args ...any) *Predicate
- func (p *Predicate) InInts(col string, args ...int) *Predicate
- func (p *Predicate) InValues(col string, args ...driver.Value) *Predicate
- func (p *Predicate) IsFalse(col string) *Predicate
- func (p *Predicate) IsNull(col string) *Predicate
- func (p *Predicate) IsTrue(col string) *Predicate
- func (p *Predicate) LT(col string, arg any) *Predicate
- func (p *Predicate) LTE(col string, arg any) *Predicate
- func (p *Predicate) Like(col, pattern string) *Predicate
- func (p *Predicate) NEQ(col string, arg any) *Predicate
- func (p *Predicate) Not() *Predicate
- func (p *Predicate) NotExists(query Querier) *Predicate
- func (p *Predicate) NotIn(col string, args ...any) *Predicate
- func (p *Predicate) NotNull(col string) *Predicate
- type Querier
- type Queries
- type ReferenceBuilder
- type SelectTable
- type Selector
- func (s *Selector) AppendFrom(t TableView) *Selector
- func (s *Selector) AppendFromExpr(x Querier) *Selector
- func (s *Selector) AppendSelect(columns ...string) *Selector
- func (s *Selector) AppendSelectAs(column, as string) *Selector
- func (s *Selector) AppendSelectExpr(exprs ...Querier) *Selector
- func (s *Selector) AppendSelectExprAs(expr Querier, as string) *Selector
- func (s *Selector) As(alias string) *Selector
- func (s *Selector) C(column string) string
- func (s *Selector) ClearOrder() *Selector
- func (s *Selector) Clone() *Selector
- func (s *Selector) CollectPredicates() *Selector
- func (s *Selector) CollectedPredicates() []*Predicate
- func (s *Selector) Columns(columns ...string) []string
- func (s *Selector) Context() context.Context
- func (s *Selector) Count(columns ...string) *Selector
- func (s *Selector) Distinct() *Selector
- func (s *Selector) Except(t TableView) *Selector
- func (s *Selector) ExceptAll(t TableView) *Selector
- func (s *Selector) FindSelection(name string) (matches []string)
- func (s *Selector) For(l LockStrength, opts ...LockOption) *Selector
- func (s *Selector) ForShare(opts ...LockOption) *Selector
- func (s *Selector) ForUpdate(opts ...LockOption) *Selector
- func (s *Selector) From(t string) *Selector
- func (s *Selector) FromExpr(x Querier) *Selector
- func (s *Selector) FromSelect(s2 *Selector) *Selector
- func (s *Selector) FromTable(t TableView) *Selector
- func (s *Selector) FullJoin(t TableView) *Selector
- func (s *Selector) GroupBy(columns ...string) *Selector
- func (s *Selector) HasJoins() bool
- func (s *Selector) Having(p *Predicate) *Selector
- func (s *Selector) Intersect(t TableView) *Selector
- func (s *Selector) IntersectAll(t TableView) *Selector
- func (s *Selector) Join(t TableView) *Selector
- func (s *Selector) JoinedTable(name string) (*SelectTable, bool)
- func (s *Selector) JoinedTableView(name string) (TableView, bool)
- func (s *Selector) LeftJoin(t TableView) *Selector
- func (s *Selector) Limit(limit int) *Selector
- func (s *Selector) Not() *Selector
- func (s *Selector) Offset(offset int) *Selector
- func (s *Selector) On(c1, c2 string) *Selector
- func (s *Selector) OnP(p *Predicate) *Selector
- func (s *Selector) Or() *Selector
- func (s *Selector) OrderBy(columns ...string) *Selector
- func (s *Selector) OrderColumns() []string
- func (s *Selector) OrderExpr(exprs ...Querier) *Selector
- func (s *Selector) OrderExprFunc(f func(*Builder)) *Selector
- func (s *Selector) P() *Predicate
- func (s *Selector) Prefix(queries ...Querier) *Selector
- func (s *Selector) RightJoin(t TableView) *Selector
- func (s *Selector) Scan(ctx context.Context, dest any) error
- func (s *Selector) Select(columns ...string) *Selector
- func (s *Selector) SelectExpr(exprs ...Querier) *Selector
- func (s *Selector) SelectedColumns() []string
- func (s *Selector) SetDistinct(v bool) *Selector
- func (s *Selector) SetP(p *Predicate) *Selector
- func (s *Selector) Table() *SelectTable
- func (s *Selector) TableName() string
- func (s *Selector) UncollectedPredicates() *Selector
- func (s *Selector) Union(t TableView) *Selector
- func (s *Selector) UnionAll(t TableView) *Selector
- func (s *Selector) UnionDistinct(t TableView) *Selector
- func (s *Selector) UnqualifiedColumns() []string
- func (s *Selector) Where(p *Predicate) *Selector
- func (s *Selector) WhereMap(p map[string]any) *Selector
- func (s *Selector) WhereOrMap(p map[string]any) *Selector
- func (s *Selector) WithContext(ctx context.Context) *Selector
- type StmtInfo
- type TableAlter
- func (t *TableAlter) AddColumn(c *ColumnBuilder) *TableAlter
- func (t *TableAlter) AddForeignKey(fk *ForeignKeyBuilder) *TableAlter
- func (t *TableAlter) AddIndex(idx *IndexBuilder) *TableAlter
- func (t *TableAlter) ChangeColumn(name string, c *ColumnBuilder) *TableAlter
- func (t *TableAlter) DropColumn(c *ColumnBuilder) *TableAlter
- func (t *TableAlter) DropConstraint(ident string) *TableAlter
- func (t *TableAlter) DropForeignKey(ident string) *TableAlter
- func (t *TableAlter) DropIndex(name string) *TableAlter
- func (t *TableAlter) ModifyColumn(c *ColumnBuilder) *TableAlter
- func (t *TableAlter) ModifyColumns(cs ...*ColumnBuilder) *TableAlter
- func (t *TableAlter) RenameColumn(old, new string) *TableAlter
- func (t *TableAlter) RenameIndex(curr, new string) *TableAlter
- type TableBuilder
- func (t *TableBuilder) Charset(s string) *TableBuilder
- func (t *TableBuilder) Checks(checks ...func(*Builder)) *TableBuilder
- func (t *TableBuilder) Collate(s string) *TableBuilder
- func (t *TableBuilder) Column(c *ColumnBuilder) *TableBuilder
- func (t *TableBuilder) Columns(columns ...*ColumnBuilder) *TableBuilder
- func (t *TableBuilder) Constraints(fks ...*ForeignKeyBuilder) *TableBuilder
- func (t *TableBuilder) ForeignKeys(fks ...*ForeignKeyBuilder) *TableBuilder
- func (t *TableBuilder) IfNotExists() *TableBuilder
- func (t *TableBuilder) Options(s string) *TableBuilder
- func (t *TableBuilder) PrimaryKey(column ...string) *TableBuilder
- type TableView
- type UpdateBuilder
- func (u *UpdateBuilder) Add(column string, v any) *UpdateBuilder
- func (u *UpdateBuilder) Empty() bool
- func (u *UpdateBuilder) FromSelect(s *Selector) *UpdateBuilder
- func (u *UpdateBuilder) Limit(limit int) *UpdateBuilder
- func (u *UpdateBuilder) OrderBy(columns ...string) *UpdateBuilder
- func (u *UpdateBuilder) Prefix(stmts ...Querier) *UpdateBuilder
- func (u *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
- func (u *UpdateBuilder) Save(ctx context.Context) (sql.Result, error)
- func (u *UpdateBuilder) Schema(name string) *UpdateBuilder
- func (u *UpdateBuilder) Set(column string, v any) *UpdateBuilder
- func (u *UpdateBuilder) SetMap(kv map[string]any) *UpdateBuilder
- func (u *UpdateBuilder) SetNull(column string) *UpdateBuilder
- func (u *UpdateBuilder) Table(table string) *UpdateBuilder
- func (u *UpdateBuilder) Where(p *Predicate) *UpdateBuilder
- func (u *UpdateBuilder) WhereMap(p map[string]any) *UpdateBuilder
- type UpdateSet
- func (u *UpdateSet) Add(column string, v any) *UpdateSet
- func (u *UpdateSet) Columns() []string
- func (u *UpdateSet) Set(column string, v any) *UpdateSet
- func (u *UpdateSet) SetExcluded(name string) *UpdateSet
- func (u *UpdateSet) SetIgnore(name string) *UpdateSet
- func (u *UpdateSet) SetNull(column string) *UpdateSet
- func (u *UpdateSet) Table() *SelectTable
- func (u *UpdateSet) UpdateColumns() []string
- type WindowBuilder
- type WithBuilder
- type Wrapper
Constants ¶
const ( MySQL = "mysql" SQLite = "sqlite3" Postgres = "postgres" Gremlin = "gremlin" )
Dialect names for external usage.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶ added in v0.0.2
func DSN(opt *OpenOptions) string
DSN Generate dsn for database connecting
func StringOmitErr ¶ added in v0.3.1
StringOmitErr turn value into string
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is the base query builder for the sql dsl.
func (*Builder) Argf ¶
Argf appends an input argument to the builder with the given format. For example:
FormatArg("JSON(?)", b).
FormatArg("ST_GeomFromText(?)", geom)
func (*Builder) Err ¶
Err returns a concatenated error of all errors encountered during the query-building, or were added manually by calling AddError.
func (*Builder) IdentComma ¶
IdentComma calls Ident on all arguments and adds a comma between them.
func (*Builder) Quote ¶
Quote quotes the given identifier with the characters based on the configured dialect. It defaults to "`".
func (*Builder) SetDialect ¶
SetDialect sets the builder dialect. It's used for garnering dialect specific queries.
func (*Builder) SetTotal ¶
SetTotal sets the value of the total arguments. Used to pass this information between sub queries/expressions.
func (*Builder) WriteByte ¶
WriteByte wraps the Buffer.WriteByte to make it chainable with other methods.
func (*Builder) WriteString ¶
WriteString wraps the Buffer.WriteString to make it chainable with other methods.
type ColumnBuilder ¶
type ColumnBuilder struct {
Builder
// contains filtered or unexported fields
}
ColumnBuilder is a builder for column definition in table creation.
func Column ¶
func Column(name string) *ColumnBuilder
Column returns a new ColumnBuilder with the given name.
sql.Column("group_id").Type("int").Attr("UNIQUE")
func (*ColumnBuilder) Attr ¶
func (c *ColumnBuilder) Attr(attr string) *ColumnBuilder
Attr sets an extra attribute for the column, like UNIQUE or AUTO_INCREMENT.
func (*ColumnBuilder) Check ¶
func (c *ColumnBuilder) Check(check func(*Builder)) *ColumnBuilder
Check adds a CHECK clause to the ADD COLUMN statement.
func (*ColumnBuilder) Constraint ¶
func (c *ColumnBuilder) Constraint(fk *ForeignKeyBuilder) *ColumnBuilder
Constraint adds the CONSTRAINT clause to the ADD COLUMN statement in SQLite.
func (*ColumnBuilder) Type ¶
func (c *ColumnBuilder) Type(t string) *ColumnBuilder
Type sets the column type.
type ConflictOption ¶
type ConflictOption func(*conflict)
ConflictOption allows configuring the conflict config using functional options.
func ConflictColumns ¶
func ConflictColumns(names ...string) ConflictOption
ConflictColumns sets the unique constraints that trigger the conflict resolution on insert to perform an upsert operation. The columns must have a unique constraint applied to trigger this behaviour.
sql.Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
sql.ConflictColumns("id"),
sql.ResolveWithNewValues(),
)
func ConflictConstraint ¶
func ConflictConstraint(name string) ConflictOption
ConflictConstraint allows setting the constraint name (i.e. `ON CONSTRAINT <name>`) for PostgreSQL.
sql.Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
sql.ConflictConstraint("users_pkey"),
sql.ResolveWithNewValues(),
)
func ConflictWhere ¶
func ConflictWhere(p *Predicate) ConflictOption
ConflictWhere allows inference of partial unique indexes. See, PostgreSQL doc: https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT
func DoNothing ¶
func DoNothing() ConflictOption
DoNothing configures the conflict_action to `DO NOTHING`. Supported by SQLite and PostgreSQL.
sql.Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
sql.ConflictColumns("id"),
sql.DoNothing()
)
func ResolveWith ¶
func ResolveWith(fn func(*UpdateSet)) ConflictOption
ResolveWith allows setting a custom function to set the `UPDATE` clause.
Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
ConflictColumns("name"),
ResolveWith(func(u *UpdateSet) {
u.SetIgnore("id")
u.SetNull("created_at")
u.Set("name", Expr(u.Excluded().C("name")))
}),
)
func ResolveWithIgnore ¶
func ResolveWithIgnore() ConflictOption
ResolveWithIgnore sets each column to itself to force an update and return the ID, otherwise does not change any data. This may still trigger update hooks in the database.
sql.Insert("users").
Columns("id").
Values(1).
OnConflict(
sql.ConflictColumns("id"),
sql.ResolveWithIgnore()
)
// Output:
// MySQL: INSERT INTO `users` (`id`) VALUES(1) ON DUPLICATE KEY UPDATE `id` = `users`.`id`
// PostgreSQL: INSERT INTO "users" ("id") VALUES(1) ON CONFLICT ("id") DO UPDATE SET "id" = "users"."id
func ResolveWithNewValues ¶
func ResolveWithNewValues() ConflictOption
ResolveWithNewValues updates columns using the new values proposed for insertion using the special EXCLUDED/VALUES table.
sql.Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
sql.ConflictColumns("id"),
sql.ResolveWithNewValues()
)
// Output:
// MySQL: INSERT INTO `users` (`id`, `name`) VALUES(1, 'Mashraki) ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `name` = VALUES(`name`),
// PostgreSQL: INSERT INTO "users" ("id") VALUES(1) ON CONFLICT ("id") DO UPDATE SET "id" = "excluded"."id, "name" = "excluded"."name"
func UpdateWhere ¶
func UpdateWhere(p *Predicate) ConflictOption
UpdateWhere allows setting the update condition. Only rows for which this expression returns true will be updated.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a wrapper around sql.DB
func (*DB) Delete ¶
func (b *DB) Delete() *DeleteBuilder
func (*DB) Insert ¶
func (b *DB) Insert() *InsertBuilder
func (*DB) InterceptorsAfterDelete ¶
func (b *DB) InterceptorsAfterDelete(ii func(*DeleteBuilder, any))
InterceptorsAfterDelete allows you to add interceptors to all queries after each delete
func (*DB) InterceptorsAfterInsert ¶
func (b *DB) InterceptorsAfterInsert(ii func(*InsertBuilder, any))
InterceptorsAfterInsert allows you to add interceptors to all queries after each insert
func (*DB) InterceptorsAfterQuery ¶
InterceptorsAfterQuery allows you to add interceptors to all queries after each query
func (*DB) InterceptorsAfterUpdate ¶
func (b *DB) InterceptorsAfterUpdate(ii func(*UpdateBuilder, any))
InterceptorsAfterUpdate allows you to add interceptors to all queries after each update
func (*DB) InterceptorsDelete ¶
func (b *DB) InterceptorsDelete(ii func(*DeleteBuilder))
InterceptorsDelete allows you to add interceptors to all queries before each delete
func (*DB) InterceptorsInsert ¶
func (b *DB) InterceptorsInsert(ii func(*InsertBuilder))
InterceptorsInsert allows you to add interceptors to all queries before each insert
func (*DB) InterceptorsQuery ¶
InterceptorsQuery allows you to add interceptors to all queries before each query
func (*DB) InterceptorsUpdate ¶
func (b *DB) InterceptorsUpdate(ii func(*UpdateBuilder))
InterceptorsUpdate allows you to add interceptors to all queries before each update
func (*DB) Table ¶ added in v0.1.6
func (b *DB) Table(name string) *SelectTable
Table returns a new table selector.
t1 := Table("users").As("u")
return Select(t1.C("name"))
func (*DB) Update ¶
func (b *DB) Update() *UpdateBuilder
type DeleteBuilder ¶
type DeleteBuilder struct {
Builder
// contains filtered or unexported fields
}
DeleteBuilder is a builder for `DELETE` statement.
func (*DeleteBuilder) FromSelect ¶
func (d *DeleteBuilder) FromSelect(s *Selector) *DeleteBuilder
FromSelect makes it possible to delete a sub query.
func (*DeleteBuilder) Schema ¶
func (d *DeleteBuilder) Schema(name string) *DeleteBuilder
Schema sets the database name for the table whose row will be deleted.
func (*DeleteBuilder) Table ¶
func (d *DeleteBuilder) Table(table string) *DeleteBuilder
func (*DeleteBuilder) Where ¶
func (d *DeleteBuilder) Where(p *Predicate) *DeleteBuilder
Where appends a where predicate to the `DELETE` statement.
func (*DeleteBuilder) WhereMap ¶
func (d *DeleteBuilder) WhereMap(cond map[string]any) *DeleteBuilder
WhereMap appends a where predicate to the `DELETE` statement.
func (*DeleteBuilder) WhereOrMap ¶
func (d *DeleteBuilder) WhereOrMap(cond map[string]any) *DeleteBuilder
WhereOrMap appends a where predicate to the `DELETE` statement.
type DescribeBuilder ¶
type DescribeBuilder struct {
Builder
// contains filtered or unexported fields
}
DescribeBuilder is a query builder for `DESCRIBE` statement.
func Describe ¶
func Describe(name string) *DescribeBuilder
Describe returns a query builder for the `DESCRIBE` statement.
Describe("users")
type DialectBuilder ¶
type DialectBuilder struct {
// contains filtered or unexported fields
}
DialectBuilder prefixes all root builders with the `Dialect` constructor.
func Dialect ¶
func Dialect(name string) *DialectBuilder
Dialect creates a new DialectBuilder with the given dialect name.
func (*DialectBuilder) AlterIndex ¶
func (d *DialectBuilder) AlterIndex(name string) *IndexAlter
AlterIndex creates an IndexAlter for the configured dialect.
Dialect(dialect.Postgres).
AlterIndex("old").
Rename("new")
func (*DialectBuilder) AlterTable ¶
func (d *DialectBuilder) AlterTable(name string) *TableAlter
AlterTable creates a TableAlter for the configured dialect.
Dialect(dialect.Postgres).
AlterTable("users").
AddColumn(Column("group_id").Type("int").Attr("UNIQUE")).
AddForeignKey(ForeignKey().Columns("group_id").
Reference(Reference().Table("groups").Columns("id")).
OnDelete("CASCADE"),
)
func (*DialectBuilder) Column ¶
func (d *DialectBuilder) Column(name string) *ColumnBuilder
Column creates a ColumnBuilder for the configured dialect.
Dialect(dialect.Postgres)..
Column("group_id").Type("int").Attr("UNIQUE")
func (*DialectBuilder) CreateIndex ¶
func (d *DialectBuilder) CreateIndex(name string) *IndexBuilder
CreateIndex creates a IndexBuilder for the configured dialect.
Dialect(dialect.Postgres).
CreateIndex("unique_name").
Unique().
Table("users").
Columns("first", "last")
func (*DialectBuilder) CreateTable ¶
func (d *DialectBuilder) CreateTable(name string) *TableBuilder
CreateTable creates a TableBuilder for the configured dialect.
Dialect(dialect.Postgres).
CreateTable("users").
Columns(
Column("id").Type("int").Attr("auto_increment"),
Column("name").Type("varchar(255)"),
).
PrimaryKey("id")
func (*DialectBuilder) Delete ¶
func (d *DialectBuilder) Delete(driver *DB) *DeleteBuilder
Delete creates a DeleteBuilder for the configured dialect.
Dialect(dialect.Postgres).
Delete().From("users")
func (*DialectBuilder) Describe ¶
func (d *DialectBuilder) Describe(name string) *DescribeBuilder
Describe creates a DescribeBuilder for the configured dialect.
Dialect(dialect.Postgres).
Describe("users")
func (*DialectBuilder) DropIndex ¶
func (d *DialectBuilder) DropIndex(name string) *DropIndexBuilder
DropIndex creates a DropIndexBuilder for the configured dialect.
Dialect(dialect.Postgres).
DropIndex("name")
func (*DialectBuilder) Expr ¶
func (d *DialectBuilder) Expr(f func(*Builder)) Querier
Expr builds a dialect-aware expression from the given callback.
func (*DialectBuilder) Insert ¶
func (d *DialectBuilder) Insert(driver *DB) *InsertBuilder
Insert creates a InsertBuilder for the configured dialect.
Dialect(dialect.Postgres).
Insert().Table("users").Columns("age").Values(1)
func (*DialectBuilder) Select ¶
func (d *DialectBuilder) Select(driver *DB, columns ...string) *Selector
Select creates a Selector for the configured dialect.
Dialect(dialect.Postgres).
Select().From(Table("users"))
func (*DialectBuilder) SelectExpr ¶
func (d *DialectBuilder) SelectExpr(exprs ...Querier) *Selector
SelectExpr is like Select, but supports passing arbitrary expressions for SELECT clause.
Dialect(dialect.Postgres).
SelectExpr(expr...).
From(Table("users"))
func (*DialectBuilder) String ¶
func (d *DialectBuilder) String(f func(*Builder)) string
String builds a dialect-aware expression string from the given callback.
func (*DialectBuilder) Table ¶
func (d *DialectBuilder) Table(name string) *SelectTable
Table creates a SelectTable for the configured dialect.
Dialect(dialect.Postgres).
Table("users").As("u")
func (*DialectBuilder) Update ¶
func (d *DialectBuilder) Update(driver *DB) *UpdateBuilder
Update creates a UpdateBuilder for the configured dialect.
Dialect(dialect.Postgres).
Update().Table("users").Set("name", "foo")
func (*DialectBuilder) With ¶
func (d *DialectBuilder) With(name string) *WithBuilder
With creates a WithBuilder for the configured dialect.
Dialect(dialect.Postgres).
With("users_view").
As(Select().From(Table("users")))
type DropIndexBuilder ¶
type DropIndexBuilder struct {
Builder
// contains filtered or unexported fields
}
DropIndexBuilder is a builder for `DROP INDEX` statement.
func DropIndex ¶
func DropIndex(name string) *DropIndexBuilder
DropIndex creates a builder for the `DROP INDEX` statement.
MySQL:
DropIndex("index_name").
Table("users").
SQLite/PostgreSQL:
DropIndex("index_name")
func (*DropIndexBuilder) Table ¶
func (d *DropIndexBuilder) Table(table string) *DropIndexBuilder
Table defines the table for the index.
type ForeignKeyBuilder ¶
type ForeignKeyBuilder struct {
Builder
// contains filtered or unexported fields
}
ForeignKeyBuilder is the builder for the foreign-key constraint clause.
func ForeignKey ¶
func ForeignKey(symbol ...string) *ForeignKeyBuilder
ForeignKey returns a builder for the foreign-key constraint clause in create/alter table statements.
ForeignKey().
Columns("group_id").
Reference(Reference().Table("groups").Columns("id")).
OnDelete("CASCADE")
func (*ForeignKeyBuilder) Columns ¶
func (fk *ForeignKeyBuilder) Columns(s ...string) *ForeignKeyBuilder
Columns sets the columns of the foreign key in the source table.
func (*ForeignKeyBuilder) OnDelete ¶
func (fk *ForeignKeyBuilder) OnDelete(action string) *ForeignKeyBuilder
OnDelete sets the on delete action for this constraint.
func (*ForeignKeyBuilder) OnUpdate ¶
func (fk *ForeignKeyBuilder) OnUpdate(action string) *ForeignKeyBuilder
OnUpdate sets the on delete action for this constraint.
func (*ForeignKeyBuilder) Reference ¶
func (fk *ForeignKeyBuilder) Reference(r *ReferenceBuilder) *ForeignKeyBuilder
Reference sets the reference clause.
func (*ForeignKeyBuilder) Symbol ¶
func (fk *ForeignKeyBuilder) Symbol(s string) *ForeignKeyBuilder
Symbol sets the symbol of the foreign key.
type Func ¶
type Func struct {
Builder
// contains filtered or unexported fields
}
Func represents an SQL function.
func (*Func) Append ¶
Append appends a new function to the function callbacks. The callback list are executed on call to String.
type IndexAlter ¶
type IndexAlter struct {
Builder
Queries []Querier // alter options.
// contains filtered or unexported fields
}
IndexAlter is a query builder for `ALTER INDEX` statement.
func AlterIndex ¶
func AlterIndex(name string) *IndexAlter
AlterIndex returns a query builder for the `ALTER INDEX` statement.
AlterIndex("old_key").
Rename("new_key")
func (*IndexAlter) Rename ¶
func (i *IndexAlter) Rename(name string) *IndexAlter
Rename appends the `RENAME TO` clause to the `ALTER INDEX` statement.
type IndexBuilder ¶
type IndexBuilder struct {
Builder
// contains filtered or unexported fields
}
IndexBuilder is a builder for `CREATE INDEX` statement.
func CreateIndex ¶
func CreateIndex(name string) *IndexBuilder
CreateIndex creates a builder for the `CREATE INDEX` statement.
CreateIndex("index_name").
Unique().
Table("users").
Column("name")
Or:
CreateIndex("index_name").
Unique().
Table("users").
Columns("name", "age")
func (*IndexBuilder) Column ¶
func (i *IndexBuilder) Column(column string) *IndexBuilder
Column appends a column to the column list for the index.
func (*IndexBuilder) Columns ¶
func (i *IndexBuilder) Columns(columns ...string) *IndexBuilder
Columns appends the given columns to the column list for the index.
func (*IndexBuilder) IfNotExists ¶
func (i *IndexBuilder) IfNotExists() *IndexBuilder
IfNotExists appends the `IF NOT EXISTS` clause to the `CREATE INDEX` statement.
func (*IndexBuilder) Table ¶
func (i *IndexBuilder) Table(table string) *IndexBuilder
Table defines the table for the index.
func (*IndexBuilder) Unique ¶
func (i *IndexBuilder) Unique() *IndexBuilder
Unique sets the index to be a unique index.
func (*IndexBuilder) Using ¶
func (i *IndexBuilder) Using(method string) *IndexBuilder
Using sets the method to create the index with.
type InsertBuilder ¶
type InsertBuilder struct {
Builder
// contains filtered or unexported fields
}
InsertBuilder is a builder for `INSERT INTO` statement.
func (*InsertBuilder) Columns ¶
func (i *InsertBuilder) Columns(columns ...string) *InsertBuilder
Columns appends columns to the INSERT statement.
func (*InsertBuilder) Default ¶
func (i *InsertBuilder) Default() *InsertBuilder
Default sets the default values clause based on the dialect type.
func (*InsertBuilder) OnConflict ¶
func (i *InsertBuilder) OnConflict(opts ...ConflictOption) *InsertBuilder
OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause of the `INSERT` statement. For example:
sql.Insert("users").
Columns("id", "name").
Values(1, "Mashraki").
OnConflict(
sql.ConflictColumns("id"),
sql.ResolveWithNewValues()
)
func (*InsertBuilder) QueryErr ¶
func (i *InsertBuilder) QueryErr() (string, []any, error)
QueryErr returns query representation of an `INSERT INTO` statement and any error occurred in building the statement.
func (*InsertBuilder) Returning ¶
func (i *InsertBuilder) Returning(columns ...string) *InsertBuilder
Returning adds the `RETURNING` clause to the insert statement. Supported by SQLite and PostgreSQL.
func (*InsertBuilder) Schema ¶
func (i *InsertBuilder) Schema(name string) *InsertBuilder
Schema sets the database name for the insert table.
func (*InsertBuilder) Set ¶
func (i *InsertBuilder) Set(column string, v any) *InsertBuilder
Set is a syntactic sugar API for inserting only one row.
func (*InsertBuilder) SetMap ¶
func (i *InsertBuilder) SetMap(kv map[string]any) *InsertBuilder
SetMap is a syntactic sugar API for inserting only one row.
func (*InsertBuilder) Table ¶
func (i *InsertBuilder) Table(table string) *InsertBuilder
func (*InsertBuilder) Values ¶
func (i *InsertBuilder) Values(values ...any) *InsertBuilder
Values append a value tuple for the insert statement.
type LockAction ¶
type LockAction string
LockAction tells the transaction what to do in case of requesting a row that is locked by other transaction.
const ( // NoWait means never wait and returns an error. NoWait LockAction = "NOWAIT" // SkipLocked means never wait and skip. SkipLocked LockAction = "SKIP LOCKED" )
type LockOption ¶
type LockOption func(*LockOptions)
LockOption allows configuring the LockOptions using functional options.
func WithLockAction ¶
func WithLockAction(action LockAction) LockOption
WithLockAction sets the Action of the lock.
func WithLockClause ¶
func WithLockClause(clause string) LockOption
WithLockClause allows providing a custom clause for locking the statement. For example, in MySQL <= 8.22:
Select().
From(Table("users")).
ForShare(
WithLockClause("LOCK IN SHARE MODE"),
)
func WithLockTables ¶
func WithLockTables(tables ...string) LockOption
WithLockTables sets the Tables of the lock.
type LockOptions ¶
type LockOptions struct {
// Strength of the lock.
Strength LockStrength
// Action of the lock.
Action LockAction
// Tables are an option tables.
Tables []string
// contains filtered or unexported fields
}
LockOptions defines a SELECT statement lock for protecting concurrent updates.
type LockStrength ¶
type LockStrength string
LockStrength defines the strength of the lock (see the list below).
const ( LockUpdate LockStrength = "UPDATE" LockNoKeyUpdate LockStrength = "NO KEY UPDATE" )
A list of all locking clauses.
type Op ¶
type Op int
An Op represents an operator.
const ( OpEQ Op = iota // = OpNEQ // <> OpGT // > OpGTE // >= OpLT // < OpLTE // <= OpIn // IN OpNotIn // NOT IN OpLike // LIKE OpIsNull // IS NULL OpNotNull // IS NOT NULL OpAdd // + OpSub // - OpMul // * OpDiv // / (Quotient) OpMod // % (Reminder) OpBetween // between and OpBetweenAnd // between and )
Predicate and arithmetic operators.
type OpenOptions ¶ added in v0.0.2
type OpenOptions struct {
User string // 用户
Password string // 密码
Host string // 主机
Port string // 端口
Database string // 数据库
Debug bool // 调试模式
Dialect string // 数据库类型, 可选 leopards.MySQL | leopards.SQLite | leopards.Postgres | leopards.Gremlin
FileForSQLite string // SQLite 数据库需要配置, 其他类型忽略
Charset string
}
OpenOptions 链接选项
func (OpenOptions) Open ¶ added in v0.0.2
func (p OpenOptions) Open() (*DB, error)
Open 打开链接获取一个DB操作类
type ParamFormatter ¶
type ParamFormatter interface {
// The FormatParam function lets users define
// custom placeholder formatting for their types.
// For example, formatting the default placeholder
// from '?' to 'ST_GeomFromWKB(?)' for MySQL dialect.
FormatParam(placeholder string, info *StmtInfo) string
}
ParamFormatter wraps the FormatPram function.
type Predicate ¶
type Predicate struct {
Builder
// contains filtered or unexported fields
}
Predicate is a where predicate.
func ColumnsGTE ¶
ColumnsGTE appends a ">=" predicate between 2 columns.
func ColumnsHasPrefix ¶
ColumnsHasPrefix appends a new predicate that checks if the given column begins with the other column (prefix).
func ColumnsLTE ¶
ColumnsLTE appends a "<=" predicate between 2 columns.
func ColumnsNEQ ¶
ColumnsNEQ appends a "<>" predicate between 2 columns.
func CompositeGT ¶
CompositeGT returns a composite ">" predicate
func CompositeLT ¶
CompositeLT returns a composite "<" predicate
func ContainsFold ¶
ContainsFold is a helper predicate that checks substring using the LIKE predicate with case-folding.
func ExprP ¶
ExprP creates a new predicate from the given expression.
ExprP("A = ? AND B > ?", args...)
func False ¶
func False() *Predicate
False appends the FALSE keyword to the predicate.
Delete().From("users").Where(False())
func Not ¶
Not wraps the given predicate with the not predicate.
Not(Or(EQ("name", "foo"), EQ("name", "bar")))
func Or ¶
Or combines all given predicates with OR between them.
Or(EQ("name", "foo"), EQ("name", "bar"))
func (*Predicate) Append ¶
Append appends a new function to the predicate callbacks. The callback list are executed on call to Query.
func (*Predicate) BetweenAnd ¶
BetweenAnd appends a "<=" predicate.
func (*Predicate) ColumnsGTE ¶
ColumnsGTE appends a ">=" predicate between 2 columns.
func (*Predicate) ColumnsHasPrefix ¶
ColumnsHasPrefix appends a new predicate that checks if the given column begins with the other column (prefix).
func (*Predicate) ColumnsLTE ¶
ColumnsLTE appends a "<=" predicate between 2 columns.
func (*Predicate) ColumnsNEQ ¶
ColumnsNEQ appends a "<>" predicate between 2 columns.
func (*Predicate) CompositeGT ¶
CompositeGT returns a composite ">" predicate.
func (*Predicate) CompositeLT ¶
CompositeLT appends a composite "<" predicate.
func (*Predicate) Contains ¶
Contains is a helper predicate that checks substring using the LIKE predicate.
func (*Predicate) ContainsFold ¶
ContainsFold is a helper predicate that applies the LIKE predicate with case-folding.
func (*Predicate) EqualFold ¶
EqualFold is a helper predicate that applies the "=" predicate with case-folding.
func (*Predicate) HasPrefix ¶
HasPrefix is a helper predicate that checks prefix using the LIKE predicate.
func (*Predicate) HasSuffix ¶
HasSuffix is a helper predicate that checks suffix using the LIKE predicate.
type Querier ¶
type Querier interface {
// contains filtered or unexported methods
}
Querier wraps the basic Query method that is implemented by the different builders in this file.
type ReferenceBuilder ¶
type ReferenceBuilder struct {
Builder
// contains filtered or unexported fields
}
ReferenceBuilder is a builder for the reference clause in constraints. For example, in foreign key creation.
func Reference ¶
func Reference() *ReferenceBuilder
Reference creates a reference builder for the reference_option clause.
Reference().Table("groups").Columns("id")
func (*ReferenceBuilder) Columns ¶
func (r *ReferenceBuilder) Columns(s ...string) *ReferenceBuilder
Columns sets the columns of the referenced table.
func (*ReferenceBuilder) Table ¶
func (r *ReferenceBuilder) Table(s string) *ReferenceBuilder
Table sets the referenced table.
type SelectTable ¶
type SelectTable struct {
Builder
// contains filtered or unexported fields
}
SelectTable is a table selector.
func Table ¶
func Table(name string) *SelectTable
Table returns a new table selector.
t1 := Table("users").As("u")
return Select(t1.C("name"))
func (*SelectTable) As ¶
func (s *SelectTable) As(alias string) *SelectTable
As adds the AS clause to the table selector.
func (*SelectTable) C ¶
func (s *SelectTable) C(column string) string
C returns a formatted string for the table column.
func (*SelectTable) Columns ¶
func (s *SelectTable) Columns(columns ...string) []string
Columns returns a list of formatted strings for the table columns.
func (*SelectTable) Schema ¶
func (s *SelectTable) Schema(name string) *SelectTable
Schema sets the schema name of the table.
func (*SelectTable) Unquote ¶
func (s *SelectTable) Unquote() *SelectTable
Unquote makes the table name to be formatted as raw string (unquoted). It is useful when you don't want to query tables under the current database. For example: "INFORMATION_SCHEMA.TABLE_CONSTRAINTS" in MySQL.
type Selector ¶
type Selector struct {
Builder
// contains filtered or unexported fields
}
Selector is a builder for the `SELECT` statement.
func Select ¶
Select returns a new selector for the `SELECT` statement.
t1 := Table("users").As("u")
t2 := Select().From(Table("groups")).Where(EQ("user_id", 10)).As("g")
return Select(t1.C("id"), t2.C("name")).
From(t1).
Join(t2).
On(t1.C("id"), t2.C("user_id"))
func SelectExpr ¶
SelectExpr is like Select, but supports passing arbitrary expressions for SELECT clause.
func (*Selector) AppendFrom ¶
AppendFrom appends a new TableView to the `FROM` clause.
func (*Selector) AppendFromExpr ¶
AppendFromExpr appends an expression (Queries) to the `FROM` clause.
func (*Selector) AppendSelect ¶
AppendSelect appends additional columns to the SELECT statement.
func (*Selector) AppendSelectAs ¶
AppendSelectAs appends additional column to the SELECT statement with the given alias.
func (*Selector) AppendSelectExpr ¶
AppendSelectExpr appends additional expressions to the SELECT statement.
func (*Selector) AppendSelectExprAs ¶
AppendSelectExprAs appends additional expressions to the SELECT statement with the given name.
func (*Selector) ClearOrder ¶
ClearOrder clears the ORDER BY clause to be empty.
func (*Selector) Clone ¶
Clone returns a duplicate of the selector, including all associated steps. It can be used to prepare common SELECT statements and use them differently after the clone is made.
func (*Selector) CollectPredicates ¶
CollectPredicates indicates the appended predicated should be collected and not appended to the `WHERE` clause.
func (*Selector) CollectedPredicates ¶
CollectedPredicates returns the collected predicates.
func (*Selector) Columns ¶
Columns returns a list of formatted strings for a selected columns from this statement.
func (*Selector) FindSelection ¶
FindSelection returns all occurrences in the selection that match the given column name. For example, for column "a" the following match: a, "a", "t"."a", "t"."b" AS "a".
func (*Selector) For ¶
func (s *Selector) For(l LockStrength, opts ...LockOption) *Selector
For sets the lock configuration for suffixing the `SELECT` statement with the `FOR [SHARE | UPDATE] ...` clause.
func (*Selector) ForShare ¶
func (s *Selector) ForShare(opts ...LockOption) *Selector
ForShare sets the lock configuration for suffixing the `SELECT` statement with the `FOR SHARE` clause.
func (*Selector) ForUpdate ¶
func (s *Selector) ForUpdate(opts ...LockOption) *Selector
ForUpdate sets the lock configuration for suffixing the `SELECT` statement with the `FOR UPDATE` clause.
func (*Selector) FromSelect ¶
FromSelect copies the predicate from a selector.
func (*Selector) IntersectAll ¶
IntersectAll appends the INTERSECT ALL clause to the query.
func (*Selector) JoinedTable ¶
func (s *Selector) JoinedTable(name string) (*SelectTable, bool)
JoinedTable returns the first joined table with the given name.
func (*Selector) JoinedTableView ¶
JoinedTableView returns the first joined TableView with the given name or alias.
func (*Selector) OnP ¶
OnP sets or appends the given predicate for the `ON` clause of the statement.
func (*Selector) OrderColumns ¶
OrderColumns returns the ordered columns in the Selector. Note, this function skips columns selected with expressions.
func (*Selector) OrderExpr ¶
OrderExpr appends the `ORDER BY` clause to the `SELECT` statement with custom list of expressions.
func (*Selector) OrderExprFunc ¶
OrderExprFunc appends the `ORDER BY` expression that evaluates the given function.
func (*Selector) Select ¶
Select changes the columns selection of the SELECT statement. Empty selection means all columns *.
func (*Selector) SelectExpr ¶
SelectExpr changes the columns selection of the SELECT statement with custom list of expressions.
func (*Selector) SelectedColumns ¶
SelectedColumns returns the selected columns in the Selector.
func (*Selector) SetDistinct ¶
SetDistinct sets explicitly if the returned rows are distinct or indistinct.
func (*Selector) SetP ¶
SetP sets explicitly the predicate function for the selector and clear its previous state.
func (*Selector) UncollectedPredicates ¶
UncollectedPredicates stop collecting predicates.
func (*Selector) UnionDistinct ¶
UnionDistinct appends the UNION DISTINCT clause to the query. Deprecated: use Union instead as by default, duplicate rows are eliminated unless ALL is specified.
func (*Selector) UnqualifiedColumns ¶
UnqualifiedColumns returns an unqualified version of the selected columns in the Selector. e.g. "t1"."c" => "c".
func (*Selector) WhereOrMap ¶
WhereOrMap sets or appends the given predicate to the statement.
type StmtInfo ¶
type StmtInfo struct {
// The Dialect of the SQL driver.
Dialect string
}
StmtInfo holds an information regarding the statement
type TableAlter ¶
type TableAlter struct {
Builder
Queries []Querier // columns and foreign-keys to add.
// contains filtered or unexported fields
}
TableAlter is a query builder for `ALTER TABLE` statement.
func AlterTable ¶
func AlterTable(name string) *TableAlter
AlterTable returns a query builder for the `ALTER TABLE` statement.
AlterTable("users").
AddColumn(Column("group_id").Type("int").Attr("UNIQUE")).
AddForeignKey(ForeignKey().Columns("group_id").
Reference(Reference().Table("groups").Columns("id")).OnDelete("CASCADE")),
)
func (*TableAlter) AddColumn ¶
func (t *TableAlter) AddColumn(c *ColumnBuilder) *TableAlter
AddColumn appends the `ADD COLUMN` clause to the given `ALTER TABLE` statement.
func (*TableAlter) AddForeignKey ¶
func (t *TableAlter) AddForeignKey(fk *ForeignKeyBuilder) *TableAlter
AddForeignKey adds a foreign key constraint to the `ALTER TABLE` statement.
func (*TableAlter) AddIndex ¶
func (t *TableAlter) AddIndex(idx *IndexBuilder) *TableAlter
AddIndex appends the `ADD INDEX` clause to the given `ALTER TABLE` statement.
func (*TableAlter) ChangeColumn ¶
func (t *TableAlter) ChangeColumn(name string, c *ColumnBuilder) *TableAlter
ChangeColumn appends the `CHANGE COLUMN` clause to the given `ALTER TABLE` statement.
func (*TableAlter) DropColumn ¶
func (t *TableAlter) DropColumn(c *ColumnBuilder) *TableAlter
DropColumn appends the `DROP COLUMN` clause to the given `ALTER TABLE` statement.
func (*TableAlter) DropConstraint ¶
func (t *TableAlter) DropConstraint(ident string) *TableAlter
DropConstraint appends the `DROP CONSTRAINT` clause to the given `ALTER TABLE` statement.
func (*TableAlter) DropForeignKey ¶
func (t *TableAlter) DropForeignKey(ident string) *TableAlter
DropForeignKey appends the `DROP FOREIGN KEY` clause to the given `ALTER TABLE` statement.
func (*TableAlter) DropIndex ¶
func (t *TableAlter) DropIndex(name string) *TableAlter
DropIndex appends the `DROP INDEX` clause to the given `ALTER TABLE` statement.
func (*TableAlter) ModifyColumn ¶
func (t *TableAlter) ModifyColumn(c *ColumnBuilder) *TableAlter
ModifyColumn appends the `MODIFY/ALTER COLUMN` clause to the given `ALTER TABLE` statement.
func (*TableAlter) ModifyColumns ¶
func (t *TableAlter) ModifyColumns(cs ...*ColumnBuilder) *TableAlter
ModifyColumns calls ModifyColumn with each of the given builders.
func (*TableAlter) RenameColumn ¶
func (t *TableAlter) RenameColumn(old, new string) *TableAlter
RenameColumn appends the `RENAME COLUMN` clause to the given `ALTER TABLE` statement.
func (*TableAlter) RenameIndex ¶
func (t *TableAlter) RenameIndex(curr, new string) *TableAlter
RenameIndex appends the `RENAME INDEX` clause to the given `ALTER TABLE` statement.
type TableBuilder ¶
type TableBuilder struct {
Builder
// contains filtered or unexported fields
}
TableBuilder is a query builder for `CREATE TABLE` statement.
func CreateTable ¶
func CreateTable(name string) *TableBuilder
CreateTable returns a query builder for the `CREATE TABLE` statement.
CreateTable("users").
Columns(
Column("id").Type("int").Attr("auto_increment"),
Column("name").Type("varchar(255)"),
).
PrimaryKey("id")
func (*TableBuilder) Charset ¶
func (t *TableBuilder) Charset(s string) *TableBuilder
Charset appends the `CHARACTER SET` clause to the statement. MySQL only.
func (*TableBuilder) Checks ¶
func (t *TableBuilder) Checks(checks ...func(*Builder)) *TableBuilder
Checks adds CHECK clauses to the CREATE TABLE statement.
func (*TableBuilder) Collate ¶
func (t *TableBuilder) Collate(s string) *TableBuilder
Collate appends the `COLLATE` clause to the statement. MySQL only.
func (*TableBuilder) Column ¶
func (t *TableBuilder) Column(c *ColumnBuilder) *TableBuilder
Column appends the given column to the `CREATE TABLE` statement.
func (*TableBuilder) Columns ¶
func (t *TableBuilder) Columns(columns ...*ColumnBuilder) *TableBuilder
Columns appends a list of columns to the builder.
func (*TableBuilder) Constraints ¶
func (t *TableBuilder) Constraints(fks ...*ForeignKeyBuilder) *TableBuilder
Constraints adds a list of foreign-key constraints to the statement.
func (*TableBuilder) ForeignKeys ¶
func (t *TableBuilder) ForeignKeys(fks ...*ForeignKeyBuilder) *TableBuilder
ForeignKeys adds a list of foreign-keys to the statement (without constraints).
func (*TableBuilder) IfNotExists ¶
func (t *TableBuilder) IfNotExists() *TableBuilder
IfNotExists appends the `IF NOT EXISTS` clause to the `CREATE TABLE` statement.
func (*TableBuilder) Options ¶
func (t *TableBuilder) Options(s string) *TableBuilder
Options appends additional options to the statement (MySQL only).
func (*TableBuilder) PrimaryKey ¶
func (t *TableBuilder) PrimaryKey(column ...string) *TableBuilder
PrimaryKey adds a column to the primary-key constraint in the statement.
type TableView ¶
type TableView interface {
// C returns a formatted string prefixed
// with the table view qualifier.
C(string) string
// contains filtered or unexported methods
}
TableView is a view that returns a table view. Can be a Table, Selector or a View (WITH statement).
type UpdateBuilder ¶
type UpdateBuilder struct {
Builder
// contains filtered or unexported fields
}
UpdateBuilder is a builder for `UPDATE` statement.
func (*UpdateBuilder) Add ¶
func (u *UpdateBuilder) Add(column string, v any) *UpdateBuilder
Add adds a numeric value to the given column. Note that, calling Set(c) after Add(c) will erase previous calls with c from the builder.
func (*UpdateBuilder) Empty ¶
func (u *UpdateBuilder) Empty() bool
Empty reports whether this builder does not contain update changes.
func (*UpdateBuilder) FromSelect ¶
func (u *UpdateBuilder) FromSelect(s *Selector) *UpdateBuilder
FromSelect makes it possible to update entities that match the sub-query.
func (*UpdateBuilder) Limit ¶
func (u *UpdateBuilder) Limit(limit int) *UpdateBuilder
Limit appends the `LIMIT` clause to the `UPDATE` statement. Supported by SQLite and MySQL.
func (*UpdateBuilder) OrderBy ¶
func (u *UpdateBuilder) OrderBy(columns ...string) *UpdateBuilder
OrderBy appends the `ORDER BY` clause to the `UPDATE` statement. Supported by SQLite and MySQL.
func (*UpdateBuilder) Prefix ¶
func (u *UpdateBuilder) Prefix(stmts ...Querier) *UpdateBuilder
Prefix prefixes the UPDATE statement with list of statements.
func (*UpdateBuilder) Returning ¶
func (u *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
Returning adds the `RETURNING` clause to the insert statement. Supported by SQLite and PostgreSQL.
func (*UpdateBuilder) Schema ¶
func (u *UpdateBuilder) Schema(name string) *UpdateBuilder
Schema sets the database name for the updated table.
func (*UpdateBuilder) Set ¶
func (u *UpdateBuilder) Set(column string, v any) *UpdateBuilder
Set sets a column to a given value. If `Set` was called before with the same column name, it overrides the value of the previous call.
func (*UpdateBuilder) SetMap ¶
func (u *UpdateBuilder) SetMap(kv map[string]any) *UpdateBuilder
SetMap sets a column to a given value. If `Set` was called before with the same column name, it overrides the value of the previous call.
func (*UpdateBuilder) SetNull ¶
func (u *UpdateBuilder) SetNull(column string) *UpdateBuilder
SetNull sets a column as null value.
func (*UpdateBuilder) Table ¶
func (u *UpdateBuilder) Table(table string) *UpdateBuilder
func (*UpdateBuilder) Where ¶
func (u *UpdateBuilder) Where(p *Predicate) *UpdateBuilder
Where adds a where predicate for update statement.
func (*UpdateBuilder) WhereMap ¶ added in v0.3.7
func (u *UpdateBuilder) WhereMap(p map[string]any) *UpdateBuilder
WhereMap adds a where predicate for update statement.
type UpdateSet ¶
type UpdateSet struct {
*UpdateBuilder
// contains filtered or unexported fields
}
UpdateSet describes a set of changes of the `DO UPDATE` clause.
func (*UpdateSet) SetExcluded ¶
SetExcluded sets the column name to its EXCLUDED/VALUES value. For example, "c" = "excluded"."c", or `c` = VALUES(`c`).
func (*UpdateSet) SetIgnore ¶
SetIgnore sets the column to itself. For example, "id" = "users"."id".
func (*UpdateSet) Table ¶
func (u *UpdateSet) Table() *SelectTable
Table returns the table the `UPSERT` statement is executed on.
func (*UpdateSet) UpdateColumns ¶
UpdateColumns returns all columns in the `UPDATE` statement.
type WindowBuilder ¶
type WindowBuilder struct {
Builder
// contains filtered or unexported fields
}
WindowBuilder represents a builder for a window clause. Note that window functions support is limited and used only to query rows-limited edges in pagination.
func RowNumber ¶
func RowNumber() *WindowBuilder
RowNumber returns a new window clause with the ROW_NUMBER() as a function. Using this function will assign each row a number, from 1 to N, in the order defined by the ORDER BY clause in the window spec.
func Window ¶
func Window(fn func(*Builder)) *WindowBuilder
Window returns a new window clause with a custom selector allowing for custom window functions.
Window(func(b *Builder) {
b.WriteString(Sum(posts.C("duration")))
}).PartitionBy("author_id").OrderBy("id"), "duration").
func (*WindowBuilder) OrderBy ¶
func (w *WindowBuilder) OrderBy(columns ...string) *WindowBuilder
OrderBy indicates how to sort rows in each partition.
func (*WindowBuilder) OrderExpr ¶
func (w *WindowBuilder) OrderExpr(exprs ...Querier) *WindowBuilder
OrderExpr appends the `ORDER BY` clause to the window partition with custom list of expressions.
func (*WindowBuilder) PartitionBy ¶
func (w *WindowBuilder) PartitionBy(columns ...string) *WindowBuilder
PartitionBy indicates to divide the query rows into groups by the given columns. Note that, standard SQL spec allows partition only by columns, and in order to use the "expression" version, use the PartitionByExpr.
func (*WindowBuilder) PartitionExpr ¶
func (w *WindowBuilder) PartitionExpr(x Querier) *WindowBuilder
PartitionExpr indicates to divide the query rows into groups by the given expression.
type WithBuilder ¶
type WithBuilder struct {
Builder
// contains filtered or unexported fields
}
WithBuilder is the builder for the `WITH` statement.
func With ¶
func With(name string, columns ...string) *WithBuilder
With returns a new builder for the `WITH` statement.
n := Queries{
With("users_view").As(Select().From(Table("users"))),
Select().From(Table("users_view")),
}
return n.Query()
func WithRecursive ¶
func WithRecursive(name string, columns ...string) *WithBuilder
WithRecursive returns a new builder for the `WITH RECURSIVE` statement.
n := Queries{
WithRecursive("users_view").As(Select().From(Table("users"))),
Select().From(Table("users_view")),
}
return n.Query()
func (*WithBuilder) As ¶
func (w *WithBuilder) As(s *Selector) *WithBuilder
As sets the view sub query.
func (*WithBuilder) C ¶
func (w *WithBuilder) C(column string) string
C returns a formatted string for the WITH column.
func (*WithBuilder) With ¶
func (w *WithBuilder) With(name string, columns ...string) *WithBuilder
With appends another named CTE to the statement.
type Wrapper ¶
type Wrapper struct {
// contains filtered or unexported fields
}
Wrapper wraps a given Querier with different format. Used to prefix/suffix other queries.
func (*Wrapper) SetDialect ¶
SetDialect calls SetDialect on the wrapped query.