Documentation ¶
Overview ¶
Package sql provides wrappers around the standard database/sql package to allow the generated code to interact with a statically-typed API.
Users that are interacting with this package should be aware that the following builders don't check the given SQL syntax nor validate or escape user-inputs. ~All validations are expected to be happened in the generated ent package.
Index ¶
- func As(ident string, as string) string
- func Asc(column string) string
- func Avg(ident string) string
- func Count(ident string) string
- func Desc(column string) string
- func Distinct(idents ...string) string
- func Lower(ident string) string
- func Max(ident string) string
- func Min(ident string) string
- func ScanInt(rows ColumnScanner) (int, error)
- func ScanInt64(rows ColumnScanner) (int64, error)
- func ScanOne(rows ColumnScanner, v interface{}) error
- func ScanSlice(rows ColumnScanner, v interface{}) error
- func ScanString(rows ColumnScanner) (string, error)
- func ScanValue(rows ColumnScanner) (driver.Value, error)
- func Sum(ident string) string
- type Builder
- func (b *Builder) AddError(err error) *Builder
- func (b *Builder) Arg(a interface{}) *Builder
- func (b *Builder) Args(a ...interface{}) *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) Nested(f func(*Builder)) *Builder
- func (b *Builder) Pad() *Builder
- func (b Builder) Query() (string, []interface{})
- func (b *Builder) Quote(ident string) string
- func (b *Builder) SetDialect(dialect string)
- func (b *Builder) SetTotal(total int)
- func (b Builder) Total() int
- func (b *Builder) WriteByte(c byte) *Builder
- func (b *Builder) WriteOp(op Op) *Builder
- func (b *Builder) WriteString(s string) *Builder
- type ColumnBuilder
- func (c *ColumnBuilder) Attr(attr string) *ColumnBuilder
- func (c *ColumnBuilder) Check(check func(*Builder)) *ColumnBuilder
- func (c *ColumnBuilder) Constraint(fk *ForeignKeyBuilder) *ColumnBuilder
- func (c *ColumnBuilder) Query() (string, []interface{})
- func (c *ColumnBuilder) Type(t string) *ColumnBuilder
- type ColumnScanner
- type Conn
- type 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(table string) *DeleteBuilder
- func (d *DialectBuilder) Describe(name string) *DescribeBuilder
- func (d *DialectBuilder) DropIndex(name string) *DropIndexBuilder
- func (d *DialectBuilder) Insert(table string) *InsertBuilder
- func (d *DialectBuilder) Select(columns ...string) *Selector
- func (d *DialectBuilder) Table(name string) *SelectTable
- func (d *DialectBuilder) Update(table string) *UpdateBuilder
- func (d *DialectBuilder) With(name string) *WithBuilder
- type Driver
- type DropIndexBuilder
- type ExecQuerier
- 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) Query() (string, []interface{})
- func (fk *ForeignKeyBuilder) Reference(r *ReferenceBuilder) *ForeignKeyBuilder
- func (fk *ForeignKeyBuilder) Symbol(s string) *ForeignKeyBuilder
- type Func
- type IndexAlter
- type IndexBuilder
- type InsertBuilder
- func (i *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (i *InsertBuilder) Default() *InsertBuilder
- func (i *InsertBuilder) Query() (string, []interface{})
- func (i *InsertBuilder) Returning(columns ...string) *InsertBuilder
- func (i *InsertBuilder) Schema(name string) *InsertBuilder
- func (i *InsertBuilder) Set(column string, v interface{}) *InsertBuilder
- func (i *InsertBuilder) Values(values ...interface{}) *InsertBuilder
- type NullBool
- type NullFloat64
- type NullInt64
- type NullString
- type NullTime
- type Op
- type Predicate
- func And(preds ...*Predicate) *Predicate
- func CompositeGT(columns []string, args ...interface{}) *Predicate
- func CompositeLT(columns []string, args ...interface{}) *Predicate
- func Contains(col, sub string) *Predicate
- func ContainsFold(col, sub string) *Predicate
- func EQ(col string, value interface{}) *Predicate
- func EqualFold(col, sub string) *Predicate
- func False() *Predicate
- func GT(col string, value interface{}) *Predicate
- func GTE(col string, value interface{}) *Predicate
- func HasPrefix(col, prefix string) *Predicate
- func HasSuffix(col, suffix string) *Predicate
- func In(col string, args ...interface{}) *Predicate
- func InInts(col string, args ...int) *Predicate
- func InValues(col string, args ...driver.Value) *Predicate
- func IsNull(col string) *Predicate
- func LT(col string, value interface{}) *Predicate
- func LTE(col string, value interface{}) *Predicate
- func Like(col, pattern string) *Predicate
- func NEQ(col string, value interface{}) *Predicate
- func Not(pred *Predicate) *Predicate
- func NotIn(col string, args ...interface{}) *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) CompositeGT(columns []string, args ...interface{}) *Predicate
- func (p *Predicate) CompositeLT(columns []string, args ...interface{}) *Predicate
- func (p *Predicate) Contains(col, sub string) *Predicate
- func (p *Predicate) ContainsFold(col, sub string) *Predicate
- func (p *Predicate) EQ(col string, arg interface{}) *Predicate
- func (p *Predicate) EqualFold(col, sub string) *Predicate
- func (p *Predicate) False() *Predicate
- func (p *Predicate) GT(col string, arg interface{}) *Predicate
- func (p *Predicate) GTE(col string, arg interface{}) *Predicate
- func (p *Predicate) HasPrefix(col, prefix string) *Predicate
- func (p *Predicate) HasSuffix(col, suffix string) *Predicate
- func (p *Predicate) In(col string, args ...interface{}) *Predicate
- func (p *Predicate) InInts(col string, args ...int) *Predicate
- func (p *Predicate) InValues(col string, args ...driver.Value) *Predicate
- func (p *Predicate) IsNull(col string) *Predicate
- func (p *Predicate) LT(col string, arg interface{}) *Predicate
- func (p *Predicate) LTE(col string, arg interface{}) *Predicate
- func (p *Predicate) Like(col, pattern string) *Predicate
- func (p *Predicate) NEQ(col string, arg interface{}) *Predicate
- func (p *Predicate) Not() *Predicate
- func (p *Predicate) NotIn(col string, args ...interface{}) *Predicate
- func (p *Predicate) NotNull(col string) *Predicate
- func (p *Predicate) Query() (string, []interface{})
- type Querier
- type Queries
- type ReferenceBuilder
- type Result
- type Rows
- type SelectTable
- type Selector
- func (s *Selector) As(alias string) *Selector
- func (s *Selector) C(column string) string
- func (s *Selector) Clone() *Selector
- 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) From(t TableView) *Selector
- func (s *Selector) FromSelect(s2 *Selector) *Selector
- func (s *Selector) GroupBy(columns ...string) *Selector
- func (s *Selector) Having(p *Predicate) *Selector
- func (s *Selector) Join(t TableView) *Selector
- 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) OrderExpr(exprs ...Querier) *Selector
- func (s *Selector) P() *Predicate
- func (s *Selector) Query() (string, []interface{})
- func (s *Selector) RightJoin(t TableView) *Selector
- func (s *Selector) Select(columns ...string) *Selector
- func (s *Selector) SetDistinct(v bool) *Selector
- func (s *Selector) SetP(p *Predicate) *Selector
- func (s *Selector) Table() *SelectTable
- func (s *Selector) Where(p *Predicate) *Selector
- func (s *Selector) WithContext(ctx context.Context) *Selector
- 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) Query() (string, []interface{})
- 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) 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
- func (t *TableBuilder) Query() (string, []interface{})
- type TableView
- type Tx
- type TxOptions
- type UpdateBuilder
- func (u *UpdateBuilder) Add(column string, v interface{}) *UpdateBuilder
- func (u *UpdateBuilder) Empty() bool
- func (u *UpdateBuilder) FromSelect(s *Selector) *UpdateBuilder
- func (u *UpdateBuilder) Query() (string, []interface{})
- func (u *UpdateBuilder) Schema(name string) *UpdateBuilder
- func (u *UpdateBuilder) Set(column string, v interface{}) *UpdateBuilder
- func (u *UpdateBuilder) SetNull(column string) *UpdateBuilder
- func (u *UpdateBuilder) Where(p *Predicate) *UpdateBuilder
- type WithBuilder
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ScanInt ¶
func ScanInt(rows ColumnScanner) (int, error)
ScanInt scans and returns an int from the rows columns.
func ScanInt64 ¶
func ScanInt64(rows ColumnScanner) (int64, error)
ScanInt64 scans and returns an int64 from the rows columns.
func ScanOne ¶
func ScanOne(rows ColumnScanner, v interface{}) error
ScanOne scans one row to the given value. It fails if the rows holds more than 1 row.
func ScanSlice ¶
func ScanSlice(rows ColumnScanner, v interface{}) error
ScanSlice scans the given ColumnScanner (basically, sql.Row or sql.Rows) into the given slice.
func ScanString ¶
func ScanString(rows ColumnScanner) (string, error)
ScanString scans and returns a string from the rows columns.
Types ¶
type Builder ¶
type Builder struct { bytes.Buffer // underlying buffer. // contains filtered or unexported fields }
Builder is the base query builder for the sql dsl.
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) Query ¶
func (c *ColumnBuilder) Query() (string, []interface{})
Query returns query representation of a Column.
func (*ColumnBuilder) Type ¶
func (c *ColumnBuilder) Type(t string) *ColumnBuilder
Type sets the column type.
type ColumnScanner ¶
type ColumnScanner interface { Next() bool Scan(...interface{}) error Columns() ([]string, error) Err() error }
ColumnScanner is the interface that wraps the four sql.Rows methods used for scanning.
type Conn ¶
type Conn struct {
ExecQuerier
}
Conn implements dialect.ExecQuerier given ExecQuerier.
type DeleteBuilder ¶
type DeleteBuilder struct { Builder // contains filtered or unexported fields }
DeleteBuilder is a builder for `DELETE` statement.
func Delete ¶
func Delete(table string) *DeleteBuilder
Delete creates a builder for the `DELETE` statement.
Delete("users"). Where( Or( EQ("name", "foo").And().EQ("age", 10), EQ("name", "bar").And().EQ("age", 20), And( EQ("name", "qux"), EQ("age", 1).Or().EQ("age", 2), ), ), )
func (*DeleteBuilder) FromSelect ¶
func (d *DeleteBuilder) FromSelect(s *Selector) *DeleteBuilder
FromSelect makes it possible to delete a sub query.
func (*DeleteBuilder) Query ¶
func (d *DeleteBuilder) Query() (string, []interface{})
Query returns query representation of a `DELETE` statement.
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) Where ¶
func (d *DeleteBuilder) Where(p *Predicate) *DeleteBuilder
Where 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")
func (*DescribeBuilder) Query ¶
func (t *DescribeBuilder) Query() (string, []interface{})
Query returns query representation of a `DESCRIBE` statement.
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(table string) *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) Insert ¶
func (d *DialectBuilder) Insert(table string) *InsertBuilder
Insert creates a InsertBuilder for the configured dialect.
Dialect(dialect.Postgres). Insert("users").Columns("age").Values(1)
func (*DialectBuilder) Select ¶
func (d *DialectBuilder) Select(columns ...string) *Selector
Select creates a Selector for the configured dialect.
Dialect(dialect.Postgres). Select().From(Table("users"))
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(table string) *UpdateBuilder
Update creates a UpdateBuilder for the configured dialect.
Dialect(dialect.Postgres). Update("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 Driver ¶
type Driver struct { Conn // contains filtered or unexported fields }
Driver is a dialect.Driver implementation for SQL based databases.
func Open ¶
Open wraps the database/sql.Open method and returns a dialect.Driver that implements the an ent/dialect.Driver interface.
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) Query ¶
func (d *DropIndexBuilder) Query() (string, []interface{})
Query returns query representation of a reference clause.
DROP INDEX index_name [ON table_name]
func (*DropIndexBuilder) Table ¶
func (d *DropIndexBuilder) Table(table string) *DropIndexBuilder
Table defines the table for the index.
type ExecQuerier ¶
type ExecQuerier interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) }
ExecQuerier wraps the standard Exec and Query methods.
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) Query ¶
func (fk *ForeignKeyBuilder) Query() (string, []interface{})
Query returns query representation of a foreign key 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) Query ¶
func (i *IndexAlter) Query() (string, []interface{})
Query returns query representation of the `ALTER INDEX` statement.
ALTER INDEX name [alter_specification]
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) Query ¶
func (i *IndexBuilder) Query() (string, []interface{})
Query returns query representation of a reference clause.
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.
type InsertBuilder ¶
type InsertBuilder struct { Builder // contains filtered or unexported fields }
InsertBuilder is a builder for `INSERT INTO` statement.
func Insert ¶
func Insert(table string) *InsertBuilder
Insert creates a builder for the `INSERT INTO` statement.
Insert("users"). Columns("name", "age"). Values("a8m", 10). Values("foo", 20)
Note: Insert inserts all values in one batch.
func (*InsertBuilder) Columns ¶
func (i *InsertBuilder) Columns(columns ...string) *InsertBuilder
Columns sets the columns of the insert statement.
func (*InsertBuilder) Default ¶
func (i *InsertBuilder) Default() *InsertBuilder
Default sets the default values clause based on the dialect type.
func (*InsertBuilder) Query ¶
func (i *InsertBuilder) Query() (string, []interface{})
Query returns query representation of an `INSERT INTO` statement.
func (*InsertBuilder) Returning ¶
func (i *InsertBuilder) Returning(columns ...string) *InsertBuilder
Returning adds the `RETURNING` clause to the insert statement. PostgreSQL only.
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 interface{}) *InsertBuilder
Set is a syntactic sugar API for inserting only one row.
func (*InsertBuilder) Values ¶
func (i *InsertBuilder) Values(values ...interface{}) *InsertBuilder
Values append a value tuple for the insert statement.
type Predicate ¶
type Predicate struct { Builder // contains filtered or unexported fields }
Predicate is a where predicate.
func CompositeGT ¶
CompositeGT returns a comiposite ">" predicate
func CompositeLT ¶
CompositeLT returns a comiposite "<" predicate
func ContainsFold ¶
ContainsFold is a helper predicate that checks substring using the LIKE predicate.
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) 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 { // Query returns the query representation of the element // and its arguments (if any). Query() (string, []interface{}) }
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 create 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) Query ¶
func (r *ReferenceBuilder) Query() (string, []interface{})
Query returns query representation of a reference clause.
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 whe 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 (*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) Columns ¶
Columns returns a list of formatted strings for a selected columns from this statement.
func (*Selector) FromSelect ¶
FromSelect copies the predicate from a selector.
func (*Selector) OnP ¶
OnP sets or appends the given predicate for the `ON` clause of the statement.
func (*Selector) OrderExpr ¶
OrderExpr appends the `ORDER BY` clause to the `SELECT` statement with custom list of expressions.
func (*Selector) Select ¶
Select changes the columns selection of the SELECT statement. Empty selection means all columns *.
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.
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) Query ¶
func (t *TableAlter) Query() (string, []interface{})
Query returns query representation of the `ALTER TABLE` statement.
ALTER TABLE name [alter_specification]
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) 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 the 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 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.
func (*TableBuilder) Query ¶
func (t *TableBuilder) Query() (string, []interface{})
Query returns query representation of a `CREATE TABLE` statement.
CREATE TABLE [IF NOT EXISTS] name
(table definition) [charset and collation]
type TableView ¶
type TableView interface {
// contains filtered or unexported methods
}
TableView is a view that returns a table view. Can ne 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 Update ¶
func Update(table string) *UpdateBuilder
Update creates a builder for the `UPDATE` statement.
Update("users").Set("name", "foo").Set("age", 10)
func (*UpdateBuilder) Add ¶
func (u *UpdateBuilder) Add(column string, v interface{}) *UpdateBuilder
Add adds a numeric value to the given column.
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) Query ¶
func (u *UpdateBuilder) Query() (string, []interface{})
Query returns query representation of an `UPDATE` statement.
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 interface{}) *UpdateBuilder
Set sets a column and a its value.
func (*UpdateBuilder) SetNull ¶
func (u *UpdateBuilder) SetNull(column string) *UpdateBuilder
SetNull sets a column as null value.
func (*UpdateBuilder) Where ¶
func (u *UpdateBuilder) Where(p *Predicate) *UpdateBuilder
Where adds a where predicate for update statement.
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) *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 (*WithBuilder) As ¶
func (w *WithBuilder) As(s *Selector) *WithBuilder
As sets the view sub query.
func (*WithBuilder) Query ¶
func (w *WithBuilder) Query() (string, []interface{})
Query returns query representation of a `WITH` clause.
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.
Directories ¶
Path | Synopsis |
---|---|
Package schema contains all schema migration logic for SQL dialects.
|
Package schema contains all schema migration logic for SQL dialects. |
Package sqlgraph provides graph abstraction capabilities on top of sql-based databases for ent codegen.
|
Package sqlgraph provides graph abstraction capabilities on top of sql-based databases for ent codegen. |