From 6f5f42ab3c56352c6f14acead0bd0b5f59bab24c Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sun, 13 Oct 2019 06:58:22 -0700 Subject: [PATCH] dialect/sql: dialect based builders (#1550) Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1550 Pull Request resolved: https://github.com/facebookincubator/ent/pull/84 This is still WIP and you should ignore this. Reviewed By: alexsn Differential Revision: D17854477 fbshipit-source-id: 2d19713c118adb31164b7a2781327e64c87db8d4 --- dialect/dialect.go | 7 +- dialect/sql/builder.go | 1294 +++++++++++------ dialect/sql/builder_test.go | 589 +++++++- dialect/sql/schema/migrate.go | 2 +- entc/gen/internal/bindata.go | 88 +- entc/gen/template/dialect/sql/create.tmpl | 4 +- entc/integration/config/ent/user_create.go | 4 +- entc/integration/ent/card_create.go | 4 +- entc/integration/ent/comment_create.go | 4 +- entc/integration/ent/fieldtype_create.go | 4 +- entc/integration/ent/file_create.go | 4 +- entc/integration/ent/filetype_create.go | 4 +- entc/integration/ent/group_create.go | 4 +- entc/integration/ent/groupinfo_create.go | 4 +- entc/integration/ent/item_create.go | 4 +- entc/integration/ent/node_create.go | 4 +- entc/integration/ent/pet_create.go | 4 +- entc/integration/ent/user_create.go | 4 +- entc/integration/idtype/ent/user_create.go | 4 +- entc/integration/json/ent/user_create.go | 4 +- entc/integration/migrate/entv1/user_create.go | 4 +- .../integration/migrate/entv2/group_create.go | 4 +- entc/integration/migrate/entv2/pet_create.go | 4 +- entc/integration/migrate/entv2/user_create.go | 4 +- entc/integration/template/ent/group_create.go | 4 +- entc/integration/template/ent/pet_create.go | 4 +- entc/integration/template/ent/user_create.go | 4 +- entc/load/internal/bindata.go | 4 +- examples/edgeindex/ent/city_create.go | 4 +- examples/edgeindex/ent/street_create.go | 4 +- examples/entcpkg/ent/user_create.go | 4 +- examples/m2m2types/ent/group_create.go | 4 +- examples/m2m2types/ent/user_create.go | 4 +- examples/m2mbidi/ent/user_create.go | 4 +- examples/m2mrecur/ent/user_create.go | 4 +- examples/o2m2types/ent/pet_create.go | 4 +- examples/o2m2types/ent/user_create.go | 4 +- examples/o2mrecur/ent/node_create.go | 4 +- examples/o2o2types/ent/card_create.go | 4 +- examples/o2o2types/ent/user_create.go | 4 +- examples/o2obidi/ent/user_create.go | 4 +- examples/o2orecur/ent/node_create.go | 4 +- examples/start/ent/car_create.go | 4 +- examples/start/ent/group_create.go | 4 +- examples/start/ent/user_create.go | 4 +- examples/traversal/ent/group_create.go | 4 +- examples/traversal/ent/pet_create.go | 4 +- examples/traversal/ent/user_create.go | 4 +- 48 files changed, 1547 insertions(+), 605 deletions(-) diff --git a/dialect/dialect.go b/dialect/dialect.go index 3c547d61f..1a07a45c2 100644 --- a/dialect/dialect.go +++ b/dialect/dialect.go @@ -15,9 +15,10 @@ import ( // Dialect names for external usage. const ( - MySQL = "mysql" - SQLite = "sqlite3" - Gremlin = "gremlin" + MySQL = "mysql" + SQLite = "sqlite3" + Postgres = "postgres" + Gremlin = "gremlin" ) // ExecQuerier wraps the 2 database operations. diff --git a/dialect/sql/builder.go b/dialect/sql/builder.go index 573eed95a..e67c36e90 100644 --- a/dialect/sql/builder.go +++ b/dialect/sql/builder.go @@ -13,157 +13,21 @@ import ( "github.com/facebookincubator/ent/dialect" ) -// Querier wraps the basic Query method. +// Querier wraps the basic Query method implemented +// by the different builders in this file. type Querier interface { - // Query returns the query representation of the element and its arguments (if any). + // Query returns the query representation of the element + // and its arguments (if any). Query() (string, []interface{}) } -// Queries are list of queries join with space between them. -type Queries []Querier - -// Query returns query representation of Queriers. -func (n Queries) Query() (string, []interface{}) { - var b Builder - for i := range n { - if i > 0 { - b.Pad() - } - query, args := n[i].Query() - b.WriteString(query) - b.args = append(b.args, args...) - } - return b.String(), b.args -} - -// Builder is the base query builder for the sql dsl. -type Builder struct { - bytes.Buffer - args []interface{} - dialect string -} - -// Append appends the given string as a quoted parameter -func (b *Builder) Append(s string) *Builder { - switch { - case len(s) == 0: - case s != "*" && s[0] != '`' && !isFunc(s) && !isModifier(s): - fmt.Fprintf(b, "`%s`", s) - default: - b.WriteString(s) - } - return b -} - -// AppendComma appends calls Append on all arguments and adds a comma between them. -func (b *Builder) AppendComma(s ...string) *Builder { - for i := range s { - if i > 0 { - b.Comma() - } - b.Append(s[i]) - } - return b -} - -// Arg appends an argument to the builder. -func (b *Builder) Arg(a interface{}) *Builder { - switch a := a.(type) { - case *raw: - b.WriteString(a.s) - default: - b.WriteString("?") - b.args = append(b.args, a) - } - return b -} - -// Args appends a list of arguments to the builder. -func (b *Builder) Args(a ...interface{}) *Builder { - for i := range a { - if i > 0 { - b.Comma() - } - b.Arg(a[i]) - } - return b -} - -// Comma adds a comma to the query. -func (b *Builder) Comma() *Builder { - b.WriteString(", ") - return b -} - -// Pad adds a space to the query. -func (b *Builder) Pad() *Builder { - b.WriteString(" ") - return b -} - -// Join joins a list of Queriers to the builder. -func (b *Builder) Join(n ...Querier) *Builder { - for i := range n { - query, args := n[i].Query() - b.WriteString(query) - b.args = append(b.args, args...) - } - return b -} - -// JoinComma joins a list of Queriers and adds comma between them. -func (b *Builder) JoinComma(n ...Querier) *Builder { - for i := range n { - if i > 0 { - b.Comma() - } - query, args := n[i].Query() - b.WriteString(query) - b.args = append(b.args, args...) - } - return b -} - -// Nested gets a callback, and wraps its result with parentheses. -func (b *Builder) Nested(f func(*Builder)) *Builder { - nb := &Builder{} - nb.WriteString("(") - f(nb) - nb.WriteString(")") - nb.WriteTo(b) - b.args = append(b.args, nb.args...) - return b -} - -// clone returns a shallow clone of a builder. -func (b Builder) clone() Builder { - c := Builder{args: append([]interface{}{}, b.args...)} - c.Buffer.Write(c.Bytes()) - return c -} - -// SetDialect sets the builder dialect. It's used for garnering dialect specific queries. -func (b *Builder) SetDialect(dialect string) *Builder { - b.dialect = dialect - return b -} - -// Dialect returns the dialect of the builder. -func (b Builder) Dialect() string { - return b.dialect -} - -// Query implements the Querier interface. -func (b Builder) Query() (string, []interface{}) { - return b.String(), b.args -} - // ColumnBuilder is a builder for column definition in table creation. type ColumnBuilder struct { - b Builder - typ string // column type. - name string // column name. - attr string // extra attributes. + Builder + typ string // column type. + name string // column name. + attr string // extra attributes. + modify bool // modify existing. } // Column returns a new ColumnBuilder with the given name. @@ -179,36 +43,39 @@ func (c *ColumnBuilder) Type(t string) *ColumnBuilder { } // Attr sets an extra attribute for the column, like UNIQUE or AUTO_INCREMENT. -func (c *ColumnBuilder) Attr(a string) *ColumnBuilder { - if c.attr != "" && a != "" { +func (c *ColumnBuilder) Attr(attr string) *ColumnBuilder { + if c.attr != "" && attr != "" { c.attr += " " } - c.attr += a + c.attr += attr return c } // Query returns query representation of a Column. func (c *ColumnBuilder) Query() (string, []interface{}) { - c.b.Append(c.name) + c.Ident(c.name) + if c.postgres() && c.modify { + c.Pad().WriteString("TYPE") + } if c.typ != "" { - c.b.Pad().WriteString(c.typ) + c.Pad().WriteString(c.typ) } if c.attr != "" { - c.b.Pad().WriteString(c.attr) + c.Pad().WriteString(c.attr) } - return c.b.String(), c.b.args + return c.String(), c.args } // TableBuilder is a query builder for `CREATE TABLE` statement. type TableBuilder struct { - b Builder - name string // table name. - exists bool // check existence. - charset string // table charset. - collation string // table collation. - columns []*ColumnBuilder // table columns. - primary []string // primary key. - constraints []Querier // foreign keys and indices. + Builder + name string // table name. + exists bool // check existence. + charset string // table charset. + collation string // table collation. + columns []Querier // table columns. + primary []string // primary key. + constraints []Querier // foreign keys and indices. } // CreateTable returns a query builder for the `CREATE TABLE` statement. @@ -235,8 +102,11 @@ func (t *TableBuilder) Column(c *ColumnBuilder) *TableBuilder { } // Columns appends the a list of columns to the builder. -func (t *TableBuilder) Columns(c ...*ColumnBuilder) *TableBuilder { - t.columns = append(t.columns, c...) +func (t *TableBuilder) Columns(columns ...*ColumnBuilder) *TableBuilder { + t.columns = make([]Querier, 0, len(columns)) + for i := range columns { + t.columns = append(t.columns, columns[i]) + } return t } @@ -281,23 +151,23 @@ func (t *TableBuilder) Collate(s string) *TableBuilder { } // Query returns query representation of a `CREATE TABLE` statement. +// +// CREATE TABLE [IF NOT EXISTS] name +// (table definition) +// [charset and collation] +// func (t *TableBuilder) Query() (string, []interface{}) { - t.b.WriteString("CREATE TABLE ") + t.WriteString("CREATE TABLE ") if t.exists { - t.b.WriteString("IF NOT EXISTS ") + t.WriteString("IF NOT EXISTS ") } - t.b.Append(t.name) - t.b.Nested(func(b *Builder) { - for i, c := range t.columns { - if i > 0 { - b.Comma() - } - b.Join(c) - } + t.Ident(t.name) + t.Nested(func(b *Builder) { + b.JoinComma(t.columns...) if len(t.primary) > 0 { b.Comma().WriteString("PRIMARY KEY") b.Nested(func(b *Builder) { - b.AppendComma(t.primary...) + b.IdentComma(t.primary...) }) } if len(t.constraints) > 0 { @@ -305,17 +175,17 @@ func (t *TableBuilder) Query() (string, []interface{}) { } }) if t.charset != "" { - t.b.WriteString(" CHARACTER SET " + t.charset) + t.WriteString(" CHARACTER SET " + t.charset) } if t.collation != "" { - t.b.WriteString(" COLLATE " + t.collation) + t.WriteString(" COLLATE " + t.collation) } - return t.b.String(), t.b.args + return t.String(), t.args } // DescribeBuilder is a query builder for `DESCRIBE` statement. type DescribeBuilder struct { - b Builder + Builder name string // table name. } @@ -327,62 +197,74 @@ func Describe(name string) *DescribeBuilder { return &DescribeBuilder{name: name // Query returns query representation of a `DESCRIBE` statement. func (t *DescribeBuilder) Query() (string, []interface{}) { - t.b.WriteString("DESCRIBE ") - t.b.Append(t.name) - return t.b.String(), nil + t.WriteString("DESCRIBE ") + t.Ident(t.name) + return t.String(), nil } // TableAlter is a query builder for `ALTER TABLE` statement. type TableAlter struct { - b Builder - name string // table to alter. - Queriers []Querier // columns and foreign-keys to add. + Builder + name string // table to alter. + Queries []Querier // columns and foreign-keys to add. } // 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")) +// AddForeignKey(ForeignKey().Columns("group_id"). +// Reference(Reference().Table("groups").Columns("id")).OnDelete("CASCADE")), +// ) // func AlterTable(name string) *TableAlter { return &TableAlter{name: name} } // AddColumn appends the `ADD COLUMN` clause to the given `ALTER TABLE` statement. func (t *TableAlter) AddColumn(c *ColumnBuilder) *TableAlter { - t.Queriers = append(t.Queriers, &Wrapper{"ADD COLUMN %s", c}) + t.Queries = append(t.Queries, &Wrapper{"ADD COLUMN %s", c}) return t } // Modify appends the `MODIFY COLUMN` clause to the given `ALTER TABLE` statement. func (t *TableAlter) ModifyColumn(c *ColumnBuilder) *TableAlter { - t.Queriers = append(t.Queriers, &Wrapper{"MODIFY COLUMN %s", c}) + switch { + case t.postgres(): + c.modify = true + t.Queries = append(t.Queries, &Wrapper{"ALTER COLUMN %s", c}) + default: + t.Queries = append(t.Queries, &Wrapper{"MODIFY COLUMN %s", c}) + } return t } // DropColumn appends the `DROP COLUMN` clause to the given `ALTER TABLE` statement. func (t *TableAlter) DropColumn(c *ColumnBuilder) *TableAlter { - t.Queriers = append(t.Queriers, &Wrapper{"DROP COLUMN %s", c}) + t.Queries = append(t.Queries, &Wrapper{"DROP COLUMN %s", c}) return t } // AddForeignKey adds a foreign key constraint to the `ALTER TABLE` statement. func (t *TableAlter) AddForeignKey(fk *ForeignKeyBuilder) *TableAlter { - t.Queriers = append(t.Queriers, &Wrapper{"ADD CONSTRAINT %s", fk}) + t.Queries = append(t.Queries, &Wrapper{"ADD CONSTRAINT %s", fk}) return t } // Query returns query representation of the `ALTER TABLE` statement. +// +// ALTER TABLE name +// [alter_specification] +// func (t *TableAlter) Query() (string, []interface{}) { - t.b.WriteString("ALTER TABLE ") - t.b.Append(t.name) - t.b.Pad() - t.b.JoinComma(t.Queriers...) - return t.b.String(), t.b.args + t.WriteString("ALTER TABLE ") + t.Ident(t.name) + t.Pad() + t.JoinComma(t.Queries...) + return t.String(), t.args } // ForeignKeyBuilder is the builder for the foreign-key constraint clause. type ForeignKeyBuilder struct { - b Builder + Builder symbol string columns []string actions []string @@ -390,6 +272,7 @@ type ForeignKeyBuilder struct { } // 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")). @@ -436,24 +319,22 @@ func (fk *ForeignKeyBuilder) OnUpdate(action string) *ForeignKeyBuilder { // Query returns query representation of a foreign key constraint. func (fk *ForeignKeyBuilder) Query() (string, []interface{}) { if fk.symbol != "" { - fk.b.Append(fk.symbol) - fk.b.Pad() + fk.Ident(fk.symbol).Pad() } - fk.b.WriteString("FOREIGN KEY") - fk.b.Nested(func(b *Builder) { - b.AppendComma(fk.columns...) + fk.WriteString("FOREIGN KEY") + fk.Nested(func(b *Builder) { + b.IdentComma(fk.columns...) }) - fk.b.Pad() - fk.b.Join(fk.ref) + fk.Pad().Join(fk.ref) for _, action := range fk.actions { - fk.b.Pad().WriteString(action) + fk.Pad().WriteString(action) } - return fk.b.String(), fk.b.args + return fk.String(), fk.args } // ReferenceBuilder is a builder for the reference clause in constraints. For example, in foreign key creation. type ReferenceBuilder struct { - b Builder + Builder table string // referenced table. columns []string // referenced columns. } @@ -478,17 +359,17 @@ func (r *ReferenceBuilder) Columns(s ...string) *ReferenceBuilder { // Query returns query representation of a reference clause. func (r *ReferenceBuilder) Query() (string, []interface{}) { - r.b.WriteString("REFERENCES ") - r.b.Append(r.table) - r.b.Nested(func(b *Builder) { - b.AppendComma(r.columns...) + r.WriteString("REFERENCES ") + r.Ident(r.table) + r.Nested(func(b *Builder) { + b.IdentComma(r.columns...) }) - return r.b.String(), r.b.args + return r.String(), r.args } // IndexBuilder is a builder for `CREATE INDEX` statement. type IndexBuilder struct { - b Builder + Builder name string unique bool table string @@ -539,22 +420,22 @@ func (i *IndexBuilder) Columns(columns ...string) *IndexBuilder { // Query returns query representation of a reference clause. func (i *IndexBuilder) Query() (string, []interface{}) { - i.b.WriteString("CREATE ") + i.WriteString("CREATE ") if i.unique { - i.b.WriteString("UNIQUE ") + i.WriteString("UNIQUE ") } - i.b.WriteString("INDEX ") - i.b.Append(i.name) - i.b.WriteString(" ON ") - i.b.Append(i.table).Nested(func(b *Builder) { - b.AppendComma(i.columns...) + i.WriteString("INDEX ") + i.Ident(i.name) + i.WriteString(" ON ") + i.Ident(i.table).Nested(func(b *Builder) { + b.IdentComma(i.columns...) }) - return i.b.String(), nil + return i.String(), nil } // DropIndexBuilder is a builder for `DROP INDEX` statement. type DropIndexBuilder struct { - b Builder + Builder name string table string } @@ -581,19 +462,22 @@ func (d *DropIndexBuilder) Table(table string) *DropIndexBuilder { } // Query returns query representation of a reference clause. +// +// DROP INDEX index_name [ON table_name] +// func (d *DropIndexBuilder) Query() (string, []interface{}) { - d.b.WriteString("DROP INDEX ") - d.b.Append(d.name) + d.WriteString("DROP INDEX ") + d.Ident(d.name) if d.table != "" { - d.b.WriteString(" ON ") - d.b.Append(d.table) + d.WriteString(" ON ") + d.Ident(d.table) } - return d.b.String(), nil + return d.String(), nil } // InsertBuilder is a builder for `INSERT INTO` statement. type InsertBuilder struct { - b Builder + Builder table string columns []string defaults string @@ -634,11 +518,11 @@ func (i *InsertBuilder) Values(values ...interface{}) *InsertBuilder { } // Default sets the default values clause based on the dialect type. -func (i *InsertBuilder) Default(d string) *InsertBuilder { - switch d { +func (i *InsertBuilder) Default() *InsertBuilder { + switch i.Dialect() { case dialect.MySQL: i.defaults = "VALUES ()" - case dialect.SQLite: + case dialect.SQLite, dialect.Postgres: i.defaults = "DEFAULT VALUES" } return i @@ -646,28 +530,28 @@ func (i *InsertBuilder) Default(d string) *InsertBuilder { // Query returns query representation of an `INSERT INTO` statement. func (i *InsertBuilder) Query() (string, []interface{}) { - i.b.WriteString("INSERT INTO ") + i.WriteString("INSERT INTO ") if i.defaults != "" && len(i.columns) == 0 { - return i.b.Append(i.table).Pad().String() + i.defaults, nil + return i.Ident(i.table).Pad().String() + i.defaults, nil } - i.b.Append(i.table).Pad().Nested(func(b *Builder) { - b.AppendComma(i.columns...) + i.Ident(i.table).Pad().Nested(func(b *Builder) { + b.IdentComma(i.columns...) }) - i.b.WriteString(" VALUES ") + i.WriteString(" VALUES ") for j, v := range i.values { if j > 0 { - i.b.Comma() + i.Comma() } - i.b.Nested(func(b *Builder) { + i.Nested(func(b *Builder) { b.Args(v...) }) } - return i.b.String(), i.b.args + return i.String(), i.args } // UpdateBuilder is a builder for `UPDATE` statement. type UpdateBuilder struct { - b Builder + Builder table string where *Predicate nulls []string @@ -691,14 +575,14 @@ func (u *UpdateBuilder) Set(column string, v interface{}) *UpdateBuilder { // Add adds a numeric value to the given column. func (u *UpdateBuilder) Add(column string, v interface{}) *UpdateBuilder { u.columns = append(u.columns, column) - var b Builder - b.WriteString("COALESCE") - b.Nested(func(b *Builder) { - b.Append(column).Comma().Arg(0) - }) - b.WriteString(" + ") - b.Arg(v) - u.values = append(u.values, b) + u.values = append(u.values, P().append(func(b *Builder) { + b.WriteString("COALESCE") + b.Nested(func(b *Builder) { + b.Ident(column).Comma().Arg(0) + }) + b.WriteString(" + ") + b.Arg(v) + })) return u } @@ -725,39 +609,39 @@ func (u *UpdateBuilder) Empty() bool { // Query returns query representation of an `UPDATE` statement. func (u *UpdateBuilder) Query() (string, []interface{}) { - u.b.WriteString("UPDATE ") - u.b.Append(u.table).Pad().WriteString("SET ") + u.WriteString("UPDATE ") + u.Ident(u.table).Pad().WriteString("SET ") for i, c := range u.nulls { if i > 0 { - u.b.Comma() + u.Comma() } - u.b.Append(c).WriteString(" = NULL") + u.Ident(c).WriteString(" = NULL") } if len(u.nulls) > 0 && len(u.columns) > 0 { - u.b.Comma() + u.Comma() } for i, c := range u.columns { if i > 0 { - u.b.Comma() + u.Comma() } - u.b.Append(c).WriteString(" = ") + u.Ident(c).WriteString(" = ") switch v := u.values[i].(type) { case Querier: - u.b.Join(v) + u.Join(v) default: - u.b.Arg(v) + u.Arg(v) } } if u.where != nil { - u.b.WriteString(" WHERE ") - u.b.Join(u.where) + u.WriteString(" WHERE ") + u.Join(u.where) } - return u.b.String(), u.b.args + return u.String(), u.args } // DeleteBuilder is a builder for `DELETE` statement. type DeleteBuilder struct { - b Builder + Builder table string where *Predicate } @@ -799,18 +683,19 @@ func (d *DeleteBuilder) FromSelect(s *Selector) *DeleteBuilder { // Query returns query representation of a `DELETE` statement. func (d *DeleteBuilder) Query() (string, []interface{}) { - d.b.WriteString("DELETE FROM ") - d.b.Append(d.table) + d.WriteString("DELETE FROM ") + d.Ident(d.table) if d.where != nil { - d.b.WriteString(" WHERE ") - d.b.Join(d.where) + d.WriteString(" WHERE ") + d.Join(d.where) } - return d.b.String(), d.b.args + return d.String(), d.args } // Predicate is a where predicate. type Predicate struct { - b Builder + Builder + fns []func(*Builder) } // P creates a new predicates. @@ -824,21 +709,25 @@ func P() *Predicate { return &Predicate{} } // Or(EQ("name", "foo"), EQ("name", "bar")) // func Or(preds ...*Predicate) *Predicate { - p := P() - for i := range preds { - p.Or().b.Nested(func(b *Builder) { - b.Join(preds[i]) - }) - } - return p + return P().append(func(b *Builder) { + for i := range preds { + if i > 0 { + b.WriteString(" OR ") + } + b.Nested(func(b *Builder) { + b.Join(preds[i]) + }) + } + }) } // Or appends an OR only if it's not a start of expression. func (p *Predicate) Or() *Predicate { - if p.b.Len() > 0 { - p.b.WriteString(" OR ") - } - return p + return p.append(func(b *Builder) { + if b.Len() > 0 { + b.WriteString(" OR ") + } + }) } // False appends the FALSE keyword to the predicate. @@ -851,8 +740,9 @@ func False() *Predicate { // False appends FALSE to the predicate. func (p *Predicate) False() *Predicate { - p.b.WriteString("FALSE") - return p + return p.append(func(b *Builder) { + b.WriteString("FALSE") + }) } // Not wraps the given predicate with the not predicate. @@ -860,36 +750,41 @@ func (p *Predicate) False() *Predicate { // Not(Or(EQ("name", "foo"), EQ("name", "bar"))) // func Not(pred *Predicate) *Predicate { - p := P() - p.Not().b.Nested(func(b *Builder) { - b.Join(pred) + return P().Not().append(func(b *Builder) { + b.Nested(func(b *Builder) { + b.Join(pred) + }) }) - return p } // Not appends NOT to the predicate. func (p *Predicate) Not() *Predicate { - p.b.WriteString("NOT ") - return p + return p.append(func(b *Builder) { + b.WriteString("NOT ") + }) } // And combines all given predicates with AND between them. func And(preds ...*Predicate) *Predicate { - p := P() - for i := range preds { - p.And().b.Nested(func(b *Builder) { - b.Join(preds[i]) - }) - } - return p + return P().append(func(b *Builder) { + for i := range preds { + if i > 0 { + b.WriteString(" AND ") + } + b.Nested(func(b *Builder) { + b.Join(preds[i]) + }) + } + }) } // And appends And only if it's not a start of expression. func (p *Predicate) And() *Predicate { - if p.b.Len() > 0 { - p.b.WriteString(" AND ") - } - return p + return p.append(func(b *Builder) { + if b.Len() > 0 { + b.WriteString(" AND ") + } + }) } // EQ returns a "=" predicate. @@ -899,9 +794,10 @@ func EQ(col string, value interface{}) *Predicate { // EQ appends a "=" predicate. func (p *Predicate) EQ(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" = ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" = ") + b.Arg(arg) + }) } // NEQ returns a "<>" predicate. @@ -911,9 +807,10 @@ func NEQ(col string, value interface{}) *Predicate { // NEQ appends a "<>" predicate. func (p *Predicate) NEQ(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" <> ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" <> ") + b.Arg(arg) + }) } // LT returns a "<" predicate. @@ -923,9 +820,10 @@ func LT(col string, value interface{}) *Predicate { // LT appends a "<" predicate. func (p *Predicate) LT(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" < ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" < ") + b.Arg(arg) + }) } // LTE returns a "<=" predicate. @@ -935,9 +833,10 @@ func LTE(col string, value interface{}) *Predicate { // LTE appends a "<=" predicate. func (p *Predicate) LTE(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" <= ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" <= ") + b.Arg(arg) + }) } // GT returns a ">" predicate. @@ -947,9 +846,10 @@ func GT(col string, value interface{}) *Predicate { // GT appends a ">" predicate. func (p *Predicate) GT(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" > ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" > ") + b.Arg(arg) + }) } // GTE returns a ">=" predicate. @@ -959,9 +859,10 @@ func GTE(col string, value interface{}) *Predicate { // GTE appends a ">=" predicate. func (p *Predicate) GTE(col string, arg interface{}) *Predicate { - p.b.Append(col).WriteString(" >= ") - p.b.Arg(arg) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" >= ") + b.Arg(arg) + }) } // NotNull returns the `IS NOT NULL` predicate. @@ -971,8 +872,9 @@ func NotNull(col string) *Predicate { // NotNull appends the `IS NOT NULL` predicate. func (p *Predicate) NotNull(col string) *Predicate { - p.b.Append(col).WriteString(" IS NOT NULL") - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" IS NOT NULL") + }) } // IsNull returns the `IS NULL` predicate. @@ -982,8 +884,9 @@ func IsNull(col string) *Predicate { // IsNull appends the `IS NULL` predicate. func (p *Predicate) IsNull(col string) *Predicate { - p.b.Append(col).WriteString(" IS NULL") - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" IS NULL") + }) } // In returns the `IN` predicate. @@ -996,15 +899,16 @@ func (p *Predicate) In(col string, args ...interface{}) *Predicate { if len(args) == 0 { return p } - p.b.Append(col).WriteString(" IN ") - p.b.Nested(func(b *Builder) { - if s, ok := args[0].(*Selector); ok { - b.Join(s) - } else { - b.Args(args...) - } + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" IN ") + b.Nested(func(b *Builder) { + if s, ok := args[0].(*Selector); ok { + b.Join(s) + } else { + b.Args(args...) + } + }) }) - return p } // InInts returns the `IN` predicate for ints. @@ -1028,11 +932,12 @@ func NotIn(col string, args ...interface{}) *Predicate { // NotIn appends the `Not IN` predicate. func (p *Predicate) NotIn(col string, args ...interface{}) *Predicate { - p.b.Append(col).WriteString(" NOT IN ") - p.b.Nested(func(b *Builder) { - b.Args(args...) + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" NOT IN ") + b.Nested(func(b *Builder) { + b.Args(args...) + }) }) - return p } // Like returns the `LIKE` predicate. @@ -1042,9 +947,10 @@ func Like(col, pattern string) *Predicate { // Like appends the `LIKE` predicate. func (p *Predicate) Like(col, pattern string) *Predicate { - p.b.Append(col).WriteString(" LIKE ") - p.b.Arg(pattern) - return p + return p.append(func(b *Builder) { + b.Ident(col).WriteString(" LIKE ") + b.Arg(pattern) + }) } // HasPrefix is a helper predicate that checks prefix using the LIKE predicate. @@ -1070,7 +976,12 @@ func EqualFold(col, sub string) *Predicate { return (&Predicate{}).EqualFold(col // EqualFold is a helper predicate that applies the "=" predicate with case-folding. func (p *Predicate) EqualFold(col, sub string) *Predicate { - return p.EQ(Lower(col), strings.ToLower(sub)) + return p.append(func(b *Builder) { + f := &Func{} + f.SetDialect(b.dialect) + b.Ident(f.Lower(col)).WriteString(" = ") + b.Arg(strings.ToLower(sub)) + }) } // Contains is a helper predicate that checks substring using the LIKE predicate. @@ -1088,45 +999,29 @@ func ContainsFold(col, sub string) *Predicate { return (&Predicate{}).ContainsFo // The recommendation is to avoid using it, and to use a dialect specific feature, like // `ILIKE` in PostgreSQL, and `COLLATE` clause in MySQL. func (p *Predicate) ContainsFold(col, sub string) *Predicate { - return p.Like(Lower(col), "%"+strings.ToLower(sub)+"%") -} - -// Lower wraps the given column with the LOWER function. -// -// P().EQ(sql.Lower("name"), "a8m") -// -func Lower(name string) string { - var b Builder - b.WriteString("LOWER") - b.Nested(func(b *Builder) { - b.Append(name) + return p.append(func(b *Builder) { + f := &Func{} + f.SetDialect(b.dialect) + b.Ident(f.Lower(col)).WriteString(" LIKE ") + b.Arg("%" + strings.ToLower(sub) + "%") }) - return b.String() -} - -// Upper wraps the given column with the UPPER function. -// -// P().EQ(sql.Upper("name"), "a8m") -// -func Upper(name string) string { - var b Builder - b.WriteString("UPPER") - b.Nested(func(b *Builder) { - b.Append(name) - }) - return b.String() } // Query returns query representation of a predicate. func (p *Predicate) Query() (string, []interface{}) { - return p.b.String(), p.b.args + for _, f := range p.fns { + f(&p.Builder) + } + return p.String(), p.args } // merge two predicates. func (p *Predicate) merge(pred *Predicate) *Predicate { - query, args := pred.Query() - p.And().b.WriteString(query) - p.b.args = append(p.b.args, args...) + if len(pred.fns) == 0 { + return p + } + p.And() + p.fns = append(p.fns, pred.fns...) return p } @@ -1135,7 +1030,97 @@ func (p *Predicate) clone() *Predicate { if p == nil { return p } - return &Predicate{p.b.clone()} + return &Predicate{fns: append([]func(*Builder){}, p.fns...)} +} + +func (p *Predicate) append(f func(*Builder)) *Predicate { + p.fns = append(p.fns, f) + return p +} + +// Func represents an SQL function. +type Func struct { + Builder +} + +// Lower wraps the given column with the LOWER function. +// +// P().EQ(sql.Lower("name"), "a8m") +// +func Lower(name string) string { return Func{}.Lower(name) } + +// Lower wraps the given ident with the LOWER function. +func (f Func) Lower(name string) string { + return f.byName("LOWER", name) +} + +// Count wraps the ident with the COUNT aggregation function. +func Count(name string) string { return Func{}.Count(name) } + +// Count wraps the ident with the COUNT aggregation function. +func (f Func) Count(ident string) string { + return f.byName("COUNT", ident) +} + +// Max wraps the ident with the MAX aggregation function. +func Max(name string) string { return Func{}.Max(name) } + +// Max wraps the ident with the MAX aggregation function. +func (f Func) Max(ident string) string { + return f.byName("MAX", ident) +} + +// Min wraps the ident with the MIN aggregation function. +func Min(name string) string { return Func{}.Min(name) } + +// Min wraps the ident with the MIN aggregation function. +func (f Func) Min(ident string) string { + return f.byName("MIN", ident) +} + +// Sum wraps the ident with the SUM aggregation function. +func Sum(name string) string { return Func{}.Sum(name) } + +// Sum wraps the ident with the SUM aggregation function. +func (f Func) Sum(ident string) string { + return f.byName("SUM", ident) +} + +// Avg wraps the ident with the AVG aggregation function. +func Avg(name string) string { return Func{}.Avg(name) } + +// Avg wraps the ident with the AVG aggregation function. +func (f Func) Avg(ident string) string { + return f.byName("AVG", ident) +} + +// byName wraps an identifier with a function name. +func (f Func) byName(fn, ident string) string { + f.WriteString(fn) + f.Nested(func(b *Builder) { + b.Ident(ident) + }) + return f.String() +} + +// As suffixed the given column with an alias (`a` AS `b`). +func As(ident string, as string) string { + b := &Builder{} + b.fromIdent(ident) + b.Ident(ident).Pad().WriteString("AS") + b.Pad().Ident(as) + return b.String() +} + +// Distinct prefixed the given columns with the `DISTINCT` keyword (DISTINCT `id`). +func Distinct(idents ...string) string { + b := &Builder{} + if len(idents) > 0 { + b.fromIdent(idents[0]) + } + b.WriteString("DISTINCT") + b.Pad().IdentComma(idents...) + return b.String() } // TableView is a view that returns a table view. Can ne a Table, Selector or a View (WITH statement). @@ -1143,49 +1128,9 @@ type TableView interface { view() } -// Count wraps the column with the COUNT aggregation function. -func Count(column string) string { - return agg("COUNT", column) -} - -// Max wraps the column with the MAX aggregation function. -func Max(column string) string { - return agg("MAX", column) -} - -// Min wraps the column with the MIN aggregation function. -func Min(column string) string { - return agg("MIN", column) -} - -// Sum wraps the column with the SUM aggregation function. -func Sum(column string) string { - return agg("SUM", column) -} - -// Avg wraps the column with the AVG aggregation function. -func Avg(column string) string { - return agg("AVG", column) -} - -// As suffixed the given column with an alias (`a` AS `b`). -func As(column string, as string) string { - var b Builder - b.Append(column).Pad().WriteString("AS") - b.Pad().Append(as) - return b.String() -} - -// Distinct prefixed the given columns with the `DISTINCT` keyword (DISTINCT `id`). -func Distinct(columns ...string) string { - var b Builder - b.WriteString("DISTINCT") - b.Pad().AppendComma(columns...) - return b.String() -} - // SelectTable is a table selector. type SelectTable struct { + Builder quote bool name string as string @@ -1212,12 +1157,16 @@ func (s *SelectTable) C(column string) string { if s.as != "" { name = s.as } - return fmt.Sprintf("`%s`.`%s`", name, column) + b := &Builder{dialect: s.dialect} + b.Ident(name) + b.WriteByte('.') + b.Ident(column) + return b.String() } // Columns returns a list of formatted strings for the table columns. func (s *SelectTable) Columns(columns ...string) []string { - var names []string + names := make([]string, 0, len(columns)) for _, c := range columns { names = append(names, s.C(c)) } @@ -1234,14 +1183,16 @@ func (s *SelectTable) Unquote() *SelectTable { // ref returns the table reference. func (s *SelectTable) ref() string { - switch { - case !s.quote: + if !s.quote { return s.name - case s.as == "": - return fmt.Sprintf("`%s`", s.name) - default: - return fmt.Sprintf("`%s` AS `%s`", s.name, s.as) } + b := &Builder{dialect: s.dialect} + b.Ident(s.name) + if s.as != "" { + b.WriteString(" AS ") + b.Ident(s.as) + } + return b.String() } // implement the table view. @@ -1254,8 +1205,9 @@ type join struct { table TableView } -// Selector a builder for the `SELECT` statement. +// Selector is a builder for the `SELECT` statement. type Selector struct { + Builder as string columns []string from TableView @@ -1294,6 +1246,9 @@ func (s *Selector) Select(columns ...string) *Selector { // From sets the source of `FROM` clause. func (s *Selector) From(t TableView) *Selector { s.from = t + if st, ok := t.(state); ok { + st.SetDialect(s.dialect) + } return s } @@ -1372,20 +1327,27 @@ func (s *Selector) Join(t TableView) *Selector { view.as = "t" + strconv.Itoa(len(s.joins)) } } + if st, ok := t.(state); ok { + st.SetDialect(s.dialect) + } return s } // C returns a formatted string for a selected column from this statement. func (s *Selector) C(column string) string { if s.as != "" { - return fmt.Sprintf("`%s`.`%s`", s.as, column) + b := &Builder{dialect: s.dialect} + b.Ident(s.as) + b.WriteByte('.') + b.Ident(column) + return b.String() } return s.Table().C(column) } // Columns returns a list of formatted strings for a selected columns from this statement. func (s *Selector) Columns(columns ...string) []string { - var names []string + names := make([]string, 0, len(columns)) for _, c := range columns { names = append(names, s.C(c)) } @@ -1410,8 +1372,8 @@ func (s *Selector) As(alias string) *Selector { func (s *Selector) Count(columns ...string) *Selector { column := "*" if len(columns) > 0 { - var b Builder - b.AppendComma(columns...) + b := &Builder{} + b.IdentComma(columns...) column = b.String() } s.columns = []string{Count(column)} @@ -1425,6 +1387,7 @@ func (s *Selector) Clone() *Selector { return nil } return &Selector{ + Builder: s.Builder.clone(), as: s.as, or: s.or, not: s.not, @@ -1443,15 +1406,15 @@ func (s *Selector) Clone() *Selector { // Asc adds the ASC suffix for the given column. func Asc(column string) string { - var b Builder - b.Append(column).WriteString(" ASC") + b := &Builder{} + b.Ident(column).WriteString(" ASC") return b.String() } // Desc adds the DESC suffix for the given column. func Desc(column string) string { - var b Builder - b.Append(column).WriteString(" DESC") + b := &Builder{} + b.Ident(column).WriteString(" DESC") return b.String() } @@ -1475,69 +1438,76 @@ func (s *Selector) Having(p *Predicate) *Selector { // Query returns query representation of a `SELECT` statement. func (s *Selector) Query() (string, []interface{}) { - var b Builder - b.WriteString("SELECT ") + s.WriteString("SELECT ") if s.distinct { - b.WriteString("DISTINCT ") + s.WriteString("DISTINCT ") } if len(s.columns) > 0 { - b.AppendComma(s.columns...) + s.IdentComma(s.columns...) } else { - b.WriteString("*") + s.WriteString("*") } - b.WriteString(" FROM ") + s.WriteString(" FROM ") switch t := s.from.(type) { case *SelectTable: - b.WriteString(t.ref()) + t.SetDialect(s.dialect) + s.WriteString(t.ref()) case *Selector: + t.SetDialect(s.dialect) query, args := t.Query() - b.WriteString(fmt.Sprintf("(%s) AS `%s`", query, t.as)) - b.args = append(b.args, args...) + s.Nested(func(b *Builder) { + b.WriteString(query) + }) + s.WriteString(" AS ") + s.Ident(t.as) + s.args = append(s.args, args...) } for _, join := range s.joins { - b.WriteString(fmt.Sprintf(" %s ", join.kind)) + s.WriteString(" " + join.kind + " ") switch view := join.table.(type) { case *SelectTable: - b.WriteString(view.ref()) + view.SetDialect(s.dialect) + s.WriteString(view.ref()) case *Selector: + view.SetDialect(s.dialect) query, args := view.Query() - b.WriteString(fmt.Sprintf("(%s) AS `%s`", query, view.as)) - b.args = append(b.args, args...) + s.Nested(func(b *Builder) { + b.WriteString(query) + }) + s.WriteString(" AS ") + s.Ident(view.as) + s.args = append(s.args, args...) } if join.on != "" { - b.WriteString(" ON ") - b.WriteString(join.on) + s.WriteString(" ON ") + s.WriteString(join.on) } } if s.where != nil { - b.WriteString(" WHERE ") - query, args := s.where.Query() - b.WriteString(query) - b.args = append(b.args, args...) + s.WriteString(" WHERE ") + s.Builder.Join(s.where) } if len(s.group) > 0 { - b.WriteString(" GROUP BY ") - b.AppendComma(s.group...) + s.WriteString(" GROUP BY ") + s.IdentComma(s.group...) } if s.having != nil { - b.WriteString(" HAVING ") - query, args := s.where.Query() - b.WriteString(query) - b.args = append(b.args, args...) + s.WriteString(" HAVING ") + s.Builder.Join(s.having) } if len(s.order) > 0 { - b.WriteString(" ORDER BY ") - b.AppendComma(s.order...) + s.WriteString(" ORDER BY ") + s.IdentComma(s.order...) } if s.limit != nil { - b.WriteString(" LIMIT ") - b.Arg(*s.limit) + s.WriteString(" LIMIT ") + s.Arg(*s.limit) } if s.offset != nil { - b.WriteString(" OFFSET ") - b.Arg(*s.offset) + s.WriteString(" OFFSET ") + s.Arg(*s.offset) } - return b.String(), b.args + return s.String(), s.args } // implement the table view interface. @@ -1545,7 +1515,7 @@ func (*Selector) view() {} // WithBuilder is the builder for the `WITH` statement. type WithBuilder struct { - b Builder + Builder name string s *Selector } @@ -1570,12 +1540,11 @@ func (w *WithBuilder) As(s *Selector) *WithBuilder { // Query returns query representation of a `WITH` clause. func (w *WithBuilder) Query() (string, []interface{}) { - w.b.WriteString("WITH " + w.name) - w.b.WriteString(" AS ") - w.b.Nested(func(b *Builder) { + w.WriteString(fmt.Sprintf("WITH %s AS ", w.name)) + w.Nested(func(b *Builder) { b.Join(w.s) }) - return w.b.String(), w.b.args + return w.String(), w.args } // implement the table view interface. @@ -1594,6 +1563,37 @@ func (w *Wrapper) Query() (string, []interface{}) { return fmt.Sprintf(w.format, query), args } +// SetDialect calls SetDialect on the wrapped query. +func (w *Wrapper) SetDialect(name string) { + if s, ok := w.wrapped.(state); ok { + s.SetDialect(name) + } +} + +// Dialect calls Dialect on the wrapped query. +func (w *Wrapper) Dialect() string { + if s, ok := w.wrapped.(state); ok { + return s.Dialect() + } + return "" +} + +// Total returns the total number of arguments so far. +func (w *Wrapper) Total() int { + if s, ok := w.wrapped.(state); ok { + return s.Total() + } + return 0 +} + +// SetTotal sets the value of the total arguments. +// Used to pass this information between sub queries/expressions. +func (w *Wrapper) SetTotal(total int) { + if s, ok := w.wrapped.(state); ok { + s.SetTotal(total) + } +} + // Raw returns a raw sql Querier that is placed as-is in the query. func Raw(s string) Querier { return &raw{s} } @@ -1601,24 +1601,382 @@ type raw struct{ s string } func (r *raw) Query() (string, []interface{}) { return r.s, nil } +// Queries are list of queries join with space between them. +type Queries []Querier + +// Query returns query representation of Queriers. +func (n Queries) Query() (string, []interface{}) { + b := &Builder{} + for i := range n { + if i > 0 { + b.Pad() + } + query, args := n[i].Query() + b.WriteString(query) + b.args = append(b.args, args...) + } + return b.String(), b.args +} + +// Builder is the base query builder for the sql dsl. +type Builder struct { + bytes.Buffer // underlying buffer. + dialect string // configured dialect. + args []interface{} // query parameters. + total int // total number of parameters in query tree. +} + +// Quote quotes the given identifier with the characters based +// on the configured dialect. It defaults to "`". +func (b *Builder) Quote(ident string) string { + switch { + case b.postgres(): + // if it was quoted with the wrong + // identifier character. + if strings.Contains(ident, "`") { + return strings.ReplaceAll(ident, "`", `"`) + } + return strconv.Quote(ident) + // an identifier for unknown dialect. + case b.dialect == "" && strings.ContainsAny(ident, "`\""): + return ident + default: + return fmt.Sprintf("`%s`", ident) + } +} + +// isIdent reports if the given string is a dialect identifier. +func (b *Builder) isIdent(s string) bool { + switch { + case b.postgres(): + return strings.Contains(s, `"`) + default: + return strings.Contains(s, "`") + } +} + +// Ident appends the given string as an identifier. +func (b *Builder) Ident(s string) *Builder { + switch { + case len(s) == 0: + case s != "*" && !b.isIdent(s) && !isFunc(s) && !isModifier(s): + b.WriteString(b.Quote(s)) + case (isFunc(s) || isModifier(s)) && b.postgres(): + // modifiers and aggregation functions that + // were called without dialect information. + b.WriteString(strings.ReplaceAll(s, "`", `"`)) + default: + b.WriteString(s) + } + return b +} + +// IdentComma calls Ident on all arguments and adds a comma between them. +func (b *Builder) IdentComma(s ...string) *Builder { + for i := range s { + if i > 0 { + b.Comma() + } + b.Ident(s[i]) + } + return b +} + +// Arg appends an input argument to the builder. +func (b *Builder) Arg(a interface{}) *Builder { + if r, ok := a.(*raw); ok { + b.WriteString(r.s) + return b + } + b.total++ + b.args = append(b.args, a) + switch { + case b.postgres(): + // PostgreSQL arguments are referenced using the syntax $n. + // $1 refers to the 1st argument, $2 to the 2nd, and so on. + b.WriteString("$" + strconv.Itoa(b.total)) + default: + b.WriteString("?") + } + return b +} + +// Args appends a list of arguments to the builder. +func (b *Builder) Args(a ...interface{}) *Builder { + for i := range a { + if i > 0 { + b.Comma() + } + b.Arg(a[i]) + } + return b +} + +// Comma adds a comma to the query. +func (b *Builder) Comma() *Builder { + b.WriteString(", ") + return b +} + +// Pad adds a space to the query. +func (b *Builder) Pad() *Builder { + b.WriteString(" ") + return b +} + +// Join joins a list of Queries to the builder. +func (b *Builder) Join(qs ...Querier) *Builder { + return b.join(qs, "") +} + +// JoinComma joins a list of Queries and adds comma between them. +func (b *Builder) JoinComma(qs ...Querier) *Builder { + return b.join(qs, ", ") +} + +// join joins a list of Queries to the builder with a given separator. +func (b *Builder) join(qs []Querier, sep string) *Builder { + for i, q := range qs { + if i > 0 { + b.WriteString(sep) + } + st, ok := q.(state) + if ok { + st.SetDialect(b.dialect) + st.SetTotal(b.total) + } + query, args := q.Query() + b.WriteString(query) + b.args = append(b.args, args...) + b.total = len(b.args) + if ok { + b.total = st.Total() + } + } + return b +} + +// Nested gets a callback, and wraps its result with parentheses. +func (b *Builder) Nested(f func(*Builder)) *Builder { + nb := &Builder{dialect: b.dialect, total: b.total} + nb.WriteString("(") + f(nb) + nb.WriteString(")") + nb.WriteTo(b) + b.args = append(b.args, nb.args...) + b.total = nb.total + return b +} + +// SetDialect sets the builder dialect. It's used for garnering dialect specific queries. +func (b *Builder) SetDialect(dialect string) { + b.dialect = dialect +} + +// Dialect returns the dialect of the builder. +func (b Builder) Dialect() string { + return b.dialect +} + +// Total returns the total number of arguments so far. +func (b Builder) Total() int { + return b.total +} + +// SetTotal sets the value of the total arguments. +// Used to pass this information between sub queries/expressions. +func (b *Builder) SetTotal(total int) { + b.total = total +} + +// Query implements the Querier interface. +func (b Builder) Query() (string, []interface{}) { + return b.String(), b.args +} + +// clone returns a shallow clone of a builder. +func (b Builder) clone() Builder { + c := Builder{dialect: b.dialect, args: append([]interface{}{}, b.args...)} + c.Buffer.Write(c.Bytes()) + return c +} + +// postgres reports if the builder dialect is postgres. +func (b Builder) postgres() bool { + return b.Dialect() == dialect.Postgres +} + +// fromIdent sets the builder dialect from the identifier format. +func (b *Builder) fromIdent(ident string) { + if strings.Contains(ident, `"`) { + b.SetDialect(dialect.Postgres) + } + // otherwise, use the default. +} + +// state wraps the all methods for setting and getting +// update state between all queries in the query tree. +type state interface { + Dialect() string + SetDialect(string) + Total() int + SetTotal(int) +} + +// dialectBuilder prefixes all root builders with the `Dialect` constructor. +type dialectBuilder struct { + dialect string +} + +// Dialect creates a new dialectBuilder with the given dialect name. +func Dialect(name string) *dialectBuilder { + return &dialectBuilder{name} +} + +// Describe creates a DescribeBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// Describe("users") +// +func (d *dialectBuilder) Describe(name string) *DescribeBuilder { + b := Describe(name) + b.SetDialect(d.dialect) + return b +} + +// 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 (d *dialectBuilder) CreateTable(name string) *TableBuilder { + b := CreateTable(name) + b.SetDialect(d.dialect) + return b +} + +// 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 (d *dialectBuilder) AlterTable(name string) *TableAlter { + b := AlterTable(name) + b.SetDialect(d.dialect) + return b +} + +// Insert creates a InsertBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// Insert("users").Columns("age").Values(1) +// +func (d *dialectBuilder) Insert(table string) *InsertBuilder { + b := Insert(table) + b.SetDialect(d.dialect) + return b +} + +// Update creates a UpdateBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// Update("users").Set("name", "foo") +// +func (d *dialectBuilder) Update(table string) *UpdateBuilder { + b := Update(table) + b.SetDialect(d.dialect) + return b +} + +// Delete creates a DeleteBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// Delete().From("users") +// +func (d *dialectBuilder) Delete(table string) *DeleteBuilder { + b := Delete(table) + b.SetDialect(d.dialect) + return b +} + +// Select creates a Selector for the configured dialect. +// +// Dialect(dialect.Postgres). +// Select().From(Table("users")) +// +func (d *dialectBuilder) Select(columns ...string) *Selector { + b := Select(columns...) + b.SetDialect(d.dialect) + return b +} + +// Table creates a SelectTable for the configured dialect. +// +// Dialect(dialect.Postgres). +// Table("users").As("u") +// +func (d *dialectBuilder) Table(name string) *SelectTable { + b := Table(name) + b.SetDialect(d.dialect) + return b +} + +// With creates a WithBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// With("users_view"). +// As(Select().From(Table("users"))) +// +func (d *dialectBuilder) With(name string) *WithBuilder { + b := With(name) + b.SetDialect(d.dialect) + return b +} + +// CreateIndex creates a IndexBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// CreateIndex("unique_name"). +// Unique(). +// Table("users"). +// Columns("first", "last") +// +func (d *dialectBuilder) CreateIndex(name string) *IndexBuilder { + b := CreateIndex(name) + b.SetDialect(d.dialect) + return b +} + +// DropIndex creates a DropIndexBuilder for the configured dialect. +// +// Dialect(dialect.Postgres). +// DropIndex("name") +// +func (d *dialectBuilder) DropIndex(name string) *DropIndexBuilder { + b := DropIndex(name) + b.SetDialect(d.dialect) + return b +} + func isFunc(s string) bool { return strings.Contains(s, "(") && strings.Contains(s, ")") } func isModifier(s string) bool { - for _, m := range []string{"DISTINCT", "ALL", "WITH ROLLUP"} { + for _, m := range [...]string{"DISTINCT", "ALL", "WITH ROLLUP"} { if strings.HasPrefix(s, m) { return true } } return false } - -func agg(fn, column string) string { - var b Builder - b.WriteString(fn) - b.Nested(func(b *Builder) { - b.Append(column) - }) - return b.String() -} diff --git a/dialect/sql/builder_test.go b/dialect/sql/builder_test.go index 91c9d27b4..e991fa230 100644 --- a/dialect/sql/builder_test.go +++ b/dialect/sql/builder_test.go @@ -8,6 +8,8 @@ import ( "strconv" "testing" + "github.com/facebookincubator/ent/dialect" + "github.com/stretchr/testify/require" ) @@ -30,6 +32,14 @@ func TestBuilder(t *testing.T) { PrimaryKey("id"), wantQuery: "CREATE TABLE `users`(`id` int auto_increment, `name` varchar(255), PRIMARY KEY(`id`))", }, + { + input: Dialect(dialect.Postgres).CreateTable("users"). + Columns( + Column("id").Type("serial").Attr("PRIMARY KEY"), + Column("name").Type("varchar"), + ), + wantQuery: `CREATE TABLE "users"("id" serial PRIMARY KEY, "name" varchar)`, + }, { input: CreateTable("users"). Columns( @@ -72,6 +82,18 @@ func TestBuilder(t *testing.T) { Reference(Reference().Table("cards").Columns("id")).OnDelete("SET NULL")), wantQuery: "CREATE TABLE IF NOT EXISTS `users`(`id` int auto_increment, `card_id` int, PRIMARY KEY(`id`, `name`), FOREIGN KEY(`card_id`) REFERENCES `cards`(`id`) ON DELETE SET NULL)", }, + { + input: Dialect(dialect.Postgres).CreateTable("users"). + IfNotExists(). + Columns( + Column("id").Type("serial"), + Column("card_id").Type("int"), + ). + PrimaryKey("id", "name"). + ForeignKeys(ForeignKey().Columns("card_id"). + Reference(Reference().Table("cards").Columns("id")).OnDelete("SET NULL")), + wantQuery: `CREATE TABLE IF NOT EXISTS "users"("id" serial, "card_id" int, PRIMARY KEY("id", "name"), FOREIGN KEY("card_id") REFERENCES "cards"("id") ON DELETE SET NULL)`, + }, { input: AlterTable("users"). AddColumn(Column("group_id").Type("int").Attr("UNIQUE")). @@ -81,6 +103,15 @@ func TestBuilder(t *testing.T) { ), wantQuery: "ALTER TABLE `users` ADD COLUMN `group_id` int UNIQUE, ADD CONSTRAINT FOREIGN KEY(`group_id`) REFERENCES `groups`(`id`) ON DELETE CASCADE", }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + AddColumn(Column("group_id").Type("int").Attr("UNIQUE")). + AddForeignKey(ForeignKey("constraint").Columns("group_id"). + Reference(Reference().Table("groups").Columns("id")). + OnDelete("CASCADE"), + ), + wantQuery: `ALTER TABLE "users" ADD COLUMN "group_id" int UNIQUE, ADD CONSTRAINT "constraint" FOREIGN KEY("group_id") REFERENCES "groups"("id") ON DELETE CASCADE`, + }, { input: AlterTable("users"). AddColumn(Column("group_id").Type("int").Attr("UNIQUE")). @@ -89,12 +120,26 @@ func TestBuilder(t *testing.T) { ), wantQuery: "ALTER TABLE `users` ADD COLUMN `group_id` int UNIQUE, ADD CONSTRAINT FOREIGN KEY(`group_id`) REFERENCES `groups`(`id`)", }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + AddColumn(Column("group_id").Type("int").Attr("UNIQUE")). + AddForeignKey(ForeignKey().Columns("group_id"). + Reference(Reference().Table("groups").Columns("id")), + ), + wantQuery: `ALTER TABLE "users" ADD COLUMN "group_id" int UNIQUE, ADD CONSTRAINT FOREIGN KEY("group_id") REFERENCES "groups"("id")`, + }, { input: AlterTable("users"). AddColumn(Column("age").Type("int")). AddColumn(Column("name").Type("varchar(255)")), wantQuery: "ALTER TABLE `users` ADD COLUMN `age` int, ADD COLUMN `name` varchar(255)", }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + AddColumn(Column("age").Type("int")). + AddColumn(Column("name").Type("varchar(255)")), + wantQuery: `ALTER TABLE "users" ADD COLUMN "age" int, ADD COLUMN "name" varchar(255)`, + }, { input: AlterTable("users"). AddForeignKey(ForeignKey().Columns("group_id"). @@ -110,47 +155,109 @@ func TestBuilder(t *testing.T) { ModifyColumn(Column("age").Type("int")), wantQuery: "ALTER TABLE `users` MODIFY COLUMN `age` int", }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + ModifyColumn(Column("age").Type("int")), + wantQuery: `ALTER TABLE "users" ALTER COLUMN "age" TYPE int`, + }, { input: AlterTable("users"). ModifyColumn(Column("age").Type("int")). DropColumn(Column("name")), wantQuery: "ALTER TABLE `users` MODIFY COLUMN `age` int, DROP COLUMN `name`", }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + ModifyColumn(Column("age").Type("int")). + DropColumn(Column("name")), + wantQuery: `ALTER TABLE "users" ALTER COLUMN "age" TYPE int, DROP COLUMN "name"`, + }, + { + input: Dialect(dialect.Postgres).AlterTable("users"). + AddColumn(Column("boring").Type("varchar")). + ModifyColumn(Column("age").Type("int")). + DropColumn(Column("name")), + wantQuery: `ALTER TABLE "users" ADD COLUMN "boring" varchar, ALTER COLUMN "age" TYPE int, DROP COLUMN "name"`, + }, { input: Insert("users").Columns("age").Values(1), wantQuery: "INSERT INTO `users` (`age`) VALUES (?)", wantArgs: []interface{}{1}, }, + { + input: Dialect(dialect.Postgres).Insert("users").Columns("age").Values(1), + wantQuery: `INSERT INTO "users" ("age") VALUES ($1)`, + wantArgs: []interface{}{1}, + }, { input: Insert("users").Columns("name", "age").Values("a8m", 10), wantQuery: "INSERT INTO `users` (`name`, `age`) VALUES (?, ?)", wantArgs: []interface{}{"a8m", 10}, }, + { + input: Dialect(dialect.Postgres).Insert("users").Columns("name", "age").Values("a8m", 10), + wantQuery: `INSERT INTO "users" ("name", "age") VALUES ($1, $2)`, + wantArgs: []interface{}{"a8m", 10}, + }, { input: Insert("users").Columns("name", "age").Values("a8m", 10).Values("foo", 20), wantQuery: "INSERT INTO `users` (`name`, `age`) VALUES (?, ?), (?, ?)", wantArgs: []interface{}{"a8m", 10, "foo", 20}, }, + { + input: Dialect(dialect.Postgres).Insert("users").Columns("name", "age").Values("a8m", 10).Values("foo", 20), + wantQuery: `INSERT INTO "users" ("name", "age") VALUES ($1, $2), ($3, $4)`, + wantArgs: []interface{}{"a8m", 10, "foo", 20}, + }, + { + input: Dialect(dialect.Postgres).Insert("users"). + Columns("name", "age"). + Values("a8m", 10). + Values("foo", 20). + Values("bar", 30), + wantQuery: `INSERT INTO "users" ("name", "age") VALUES ($1, $2), ($3, $4), ($5, $6)`, + wantArgs: []interface{}{"a8m", 10, "foo", 20, "bar", 30}, + }, { input: Update("users").Set("name", "foo"), wantQuery: "UPDATE `users` SET `name` = ?", wantArgs: []interface{}{"foo"}, }, + { + input: Dialect(dialect.Postgres).Update("users").Set("name", "foo"), + wantQuery: `UPDATE "users" SET "name" = $1`, + wantArgs: []interface{}{"foo"}, + }, { input: Update("users").Set("name", "foo").Set("age", 10), wantQuery: "UPDATE `users` SET `name` = ?, `age` = ?", wantArgs: []interface{}{"foo", 10}, }, + { + input: Dialect(dialect.Postgres).Update("users").Set("name", "foo").Set("age", 10), + wantQuery: `UPDATE "users" SET "name" = $1, "age" = $2`, + wantArgs: []interface{}{"foo", 10}, + }, { input: Update("users").Set("name", "foo").Where(EQ("name", "bar")), wantQuery: "UPDATE `users` SET `name` = ? WHERE `name` = ?", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: Dialect(dialect.Postgres).Update("users").Set("name", "foo").Where(EQ("name", "bar")), + wantQuery: `UPDATE "users" SET "name" = $1 WHERE "name" = $2`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: Update("users").Set("name", "foo").SetNull("spouse_id"), wantQuery: "UPDATE `users` SET `spouse_id` = NULL, `name` = ?", wantArgs: []interface{}{"foo"}, }, + { + input: Dialect(dialect.Postgres).Update("users").Set("name", "foo").SetNull("spouse_id"), + wantQuery: `UPDATE "users" SET "spouse_id" = NULL, "name" = $1`, + wantArgs: []interface{}{"foo"}, + }, { input: Update("users").Set("name", "foo"). Where(EQ("name", "bar")). @@ -158,6 +265,15 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `name` = ? WHERE `name` = ? AND `age` = ?", wantArgs: []interface{}{"foo", "bar", 20}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Set("name", "foo"). + Where(EQ("name", "bar")). + Where(EQ("age", 20)), + wantQuery: `UPDATE "users" SET "name" = $1 WHERE "name" = $2 AND "age" = $3`, + wantArgs: []interface{}{"foo", "bar", 20}, + }, { input: Update("users"). Set("name", "foo"). @@ -166,6 +282,15 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `name` = ?, `age` = ? WHERE `name` = ? OR `name` = ?", wantArgs: []interface{}{"foo", 10, "bar", "baz"}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Set("name", "foo"). + Set("age", 10). + Where(EQ("name", "bar").Or().EQ("name", "baz")), + wantQuery: `UPDATE "users" SET "name" = $1, "age" = $2 WHERE "name" = $3 OR "name" = $4`, + wantArgs: []interface{}{"foo", 10, "bar", "baz"}, + }, { input: Update("users"). Set("name", "foo"). @@ -174,6 +299,15 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `name` = ?, `age` = ? WHERE `name` = ?", wantArgs: []interface{}{"foo", 10, "foo"}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Set("name", "foo"). + Set("age", 10). + Where(P().EQ("name", "foo")), + wantQuery: `UPDATE "users" SET "name" = $1, "age" = $2 WHERE "name" = $3`, + wantArgs: []interface{}{"foo", 10, "foo"}, + }, { input: Update("users"). Set("name", "foo"). @@ -181,6 +315,14 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `name` = ? WHERE `name` IN (?, ?) AND `age` NOT IN (?, ?)", wantArgs: []interface{}{"foo", "bar", "baz", 1, 2}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Set("name", "foo"). + Where(In("name", "bar", "baz").And().NotIn("age", 1, 2)), + wantQuery: `UPDATE "users" SET "name" = $1 WHERE "name" IN ($2, $3) AND "age" NOT IN ($4, $5)`, + wantArgs: []interface{}{"foo", "bar", "baz", 1, 2}, + }, { input: Update("users"). Set("name", "foo"). @@ -188,6 +330,14 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `name` = ? WHERE `nickname` LIKE ? AND `lastname` LIKE ?", wantArgs: []interface{}{"foo", "a8m%", "%mash%"}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Set("name", "foo"). + Where(HasPrefix("nickname", "a8m").And().Contains("lastname", "mash")), + wantQuery: `UPDATE "users" SET "name" = $1 WHERE "nickname" LIKE $2 AND "lastname" LIKE $3`, + wantArgs: []interface{}{"foo", "a8m%", "%mash%"}, + }, { input: Update("users"). Add("age", 1). @@ -195,6 +345,14 @@ func TestBuilder(t *testing.T) { wantQuery: "UPDATE `users` SET `age` = COALESCE(`age`, ?) + ? WHERE `nickname` LIKE ?", wantArgs: []interface{}{0, 1, "a8m%"}, }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Add("age", 1). + Where(HasPrefix("nickname", "a8m")), + wantQuery: `UPDATE "users" SET "age" = COALESCE("age", $1) + $2 WHERE "nickname" LIKE $3`, + wantArgs: []interface{}{0, 1, "a8m%"}, + }, { input: Update("users"). Add("age", 1). @@ -205,32 +363,55 @@ func TestBuilder(t *testing.T) { wantArgs: []interface{}{0, 1, "a8m", 0, 10, "mashraki"}, }, { - input: Select(). - From(Table("users")). - Where(EqualFold("name", "Alex")), - wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) = ?", - wantArgs: []interface{}{"alex"}, + input: Dialect(dialect.Postgres). + Update("users"). + Add("age", 1). + Set("nickname", "a8m"). + Add("version", 10). + Set("name", "mashraki"), + wantQuery: `UPDATE "users" SET "age" = COALESCE("age", $1) + $2, "nickname" = $3, "version" = COALESCE("version", $4) + $5, "name" = $6`, + wantArgs: []interface{}{0, 1, "a8m", 0, 10, "mashraki"}, + }, + { + input: Dialect(dialect.Postgres). + Update("users"). + Add("age", 1). + Set("nickname", "a8m"). + Add("version", 10). + Set("name", "mashraki"). + Set("first", "ariel"). + Add("score", 1e5). + Where(EQ("age", 1).Or().EQ("age", 2)), + wantQuery: `UPDATE "users" SET "age" = COALESCE("age", $1) + $2, "nickname" = $3, "version" = COALESCE("version", $4) + $5, "name" = $6, "first" = $7, "score" = COALESCE("score", $8) + $9 WHERE "age" = $10 OR "age" = $11`, + wantArgs: []interface{}{0, 1, "a8m", 0, 10, "mashraki", "ariel", 0, 1e5, 1, 2}, }, { input: Select(). From(Table("users")). - Where(EqualFold("name", "BAR").Or().EqualFold("name", "BAZ")), - wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) = ? OR LOWER(`name`) = ?", - wantArgs: []interface{}{"bar", "baz"}, + Where(EQ("name", "Alex")), + wantQuery: "SELECT * FROM `users` WHERE `name` = ?", + wantArgs: []interface{}{"Alex"}, + }, + { + input: Dialect(dialect.Postgres). + Select(). + From(Table("users")), + wantQuery: `SELECT * FROM "users"`, + }, + { + input: Dialect(dialect.Postgres). + Select(). + From(Table("users")). + Where(EQ("name", "Ariel")), + wantQuery: `SELECT * FROM "users" WHERE "name" = $1`, + wantArgs: []interface{}{"Ariel"}, }, { input: Select(). From(Table("users")). - Where(ContainsFold("name", "Ariel")), - wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) LIKE ?", - wantArgs: []interface{}{"%ariel%"}, - }, - { - input: Select(). - From(Table("users")). - Where(ContainsFold("name", "Ariel").And().ContainsFold("nick", "Bar")), - wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) LIKE ? AND LOWER(`nick`) LIKE ?", - wantArgs: []interface{}{"%ariel%", "%bar%"}, + Where(EQ("name", "BAR").Or().EQ("name", "BAZ")), + wantQuery: "SELECT * FROM `users` WHERE `name` = ? OR `name` = ?", + wantArgs: []interface{}{"BAR", "BAZ"}, }, { input: Update("users"). @@ -246,9 +427,10 @@ func TestBuilder(t *testing.T) { wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NOT NULL", }, { - input: Delete("users"). + input: Dialect(dialect.Postgres). + Delete("users"). Where(IsNull("parent_id")), - wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NULL", + wantQuery: `DELETE FROM "users" WHERE "parent_id" IS NULL`, }, { input: Delete("users"). @@ -256,17 +438,37 @@ func TestBuilder(t *testing.T) { wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NULL AND `name` NOT IN (?, ?)", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: Dialect(dialect.Postgres). + Delete("users"). + Where(IsNull("parent_id").And().NotIn("name", "foo", "bar")), + wantQuery: `DELETE FROM "users" WHERE "parent_id" IS NULL AND "name" NOT IN ($1, $2)`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: Delete("users"). Where(False().And().False()), wantQuery: "DELETE FROM `users` WHERE FALSE AND FALSE", }, + { + input: Dialect(dialect.Postgres). + Delete("users"). + Where(False().And().False()), + wantQuery: `DELETE FROM "users" WHERE FALSE AND FALSE`, + }, { input: Delete("users"). Where(NotNull("parent_id").Or().EQ("parent_id", 10)), wantQuery: "DELETE FROM `users` WHERE `parent_id` IS NOT NULL OR `parent_id` = ?", wantArgs: []interface{}{10}, }, + { + input: Dialect(dialect.Postgres). + Delete("users"). + Where(NotNull("parent_id").Or().EQ("parent_id", 10)), + wantQuery: `DELETE FROM "users" WHERE "parent_id" IS NOT NULL OR "parent_id" = $1`, + wantArgs: []interface{}{10}, + }, { input: Delete("users"). Where( @@ -283,41 +485,45 @@ func TestBuilder(t *testing.T) { wantArgs: []interface{}{"foo", 10, "bar", 20, "qux", 1, 2}, }, { - input: Select(Lower("name")). - From(Table("users")), - wantQuery: "SELECT LOWER(`name`) FROM `users`", - }, - { - input: Select(Lower("name")). - From(Table("users")). - Where(EQ(Lower("name"), "a8m")), - wantQuery: "SELECT LOWER(`name`) FROM `users` WHERE LOWER(`name`) = ?", - wantArgs: []interface{}{"a8m"}, - }, - { - input: Select(Upper("name")). - From(Table("users")), - wantQuery: "SELECT UPPER(`name`) FROM `users`", - }, - { - input: Select(Upper("name")). - From(Table("users")). - Where(EQ(Upper("name"), "a8m")), - wantQuery: "SELECT UPPER(`name`) FROM `users` WHERE UPPER(`name`) = ?", - wantArgs: []interface{}{"a8m"}, + input: Dialect(dialect.Postgres). + 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), + ), + ), + ), + wantQuery: `DELETE FROM "users" WHERE ("name" = $1 AND "age" = $2) OR ("name" = $3 AND "age" = $4) OR (("name" = $5) AND ("age" = $6 OR "age" = $7))`, + wantArgs: []interface{}{"foo", 10, "bar", 20, "qux", 1, 2}, }, { input: Select().From(Table("users")), wantQuery: "SELECT * FROM `users`", }, + { + input: Dialect(dialect.Postgres).Select().From(Table("users")), + wantQuery: `SELECT * FROM "users"`, + }, { input: Select().From(Table("users").Unquote()), wantQuery: "SELECT * FROM users", }, + { + input: Dialect(dialect.Postgres).Select().From(Table("users").Unquote()), + wantQuery: "SELECT * FROM users", + }, { input: Select().From(Table("users").As("u")), wantQuery: "SELECT * FROM `users` AS `u`", }, + { + input: Dialect(dialect.Postgres).Select().From(Table("users").As("u")), + wantQuery: `SELECT * FROM "users" AS "u"`, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -326,6 +532,14 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT `u`.`id`, `g`.`name` FROM `users` AS `u` JOIN `groups` AS `g`", }, + { + input: func() Querier { + t1 := Table("users").As("u") + t2 := Table("groups").As("g") + return Dialect(dialect.Postgres).Select(t1.C("id"), t2.C("name")).From(t1).Join(t2) + }(), + wantQuery: `SELECT "u"."id", "g"."name" FROM "users" AS "u" JOIN "groups" AS "g"`, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -337,6 +551,18 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT `u`.`id`, `g`.`name` FROM `users` AS `u` JOIN `groups` AS `g` ON `u`.`id` = `g`.`user_id`", }, + { + input: func() Querier { + t1 := Table("users").As("u") + t2 := Table("groups").As("g") + return Dialect(dialect.Postgres). + Select(t1.C("id"), t2.C("name")). + From(t1). + Join(t2). + On(t1.C("id"), t2.C("user_id")) + }(), + wantQuery: `SELECT "u"."id", "g"."name" FROM "users" AS "u" JOIN "groups" AS "g" ON "u"."id" = "g"."user_id"`, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -350,6 +576,20 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT `u`.`id`, `g`.`name` FROM `users` AS `u` JOIN `groups` AS `g` ON `u`.`id` = `g`.`user_id` WHERE `u`.`name` = ? AND `g`.`name` IS NOT NULL", wantArgs: []interface{}{"bar"}, }, + { + input: func() Querier { + t1 := Table("users").As("u") + t2 := Table("groups").As("g") + return Dialect(dialect.Postgres). + Select(t1.C("id"), t2.C("name")). + From(t1). + Join(t2). + On(t1.C("id"), t2.C("user_id")). + Where(EQ(t1.C("name"), "bar").And().NotNull(t2.C("name"))) + }(), + wantQuery: `SELECT "u"."id", "g"."name" FROM "users" AS "u" JOIN "groups" AS "g" ON "u"."id" = "g"."user_id" WHERE "u"."name" = $1 AND "g"."name" IS NOT NULL`, + wantArgs: []interface{}{"bar"}, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -357,6 +597,23 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT `u`.`name`, `u`.`age` FROM `users` AS `u`", }, + { + input: func() Querier { + t1 := Table("users").As("u") + return Dialect(dialect.Postgres). + Select(t1.Columns("name", "age")...).From(t1) + }(), + wantQuery: `SELECT "u"."name", "u"."age" FROM "users" AS "u"`, + }, + { + input: func() Querier { + t1 := Dialect(dialect.Postgres). + Table("users").As("u") + return Dialect(dialect.Postgres). + Select(t1.Columns("name", "age")...).From(t1) + }(), + wantQuery: `SELECT "u"."name", "u"."age" FROM "users" AS "u"`, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -369,6 +626,19 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT `u`.`id`, `g`.`name` FROM `users` AS `u` JOIN (SELECT * FROM `groups` WHERE `user_id` = ?) AS `g` ON `u`.`id` = `g`.`user_id`", wantArgs: []interface{}{10}, }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + t1 := d.Table("users").As("u") + t2 := d.Select().From(Table("groups")).Where(EQ("user_id", 10)).As("g") + return d.Select(t1.C("id"), t2.C("name")). + From(t1). + Join(t2). + On(t1.C("id"), t2.C("user_id")) + }(), + wantQuery: `SELECT "u"."id", "g"."name" FROM "users" AS "u" JOIN (SELECT * FROM "groups" WHERE "user_id" = $1) AS "g" ON "u"."id" = "g"."user_id"`, + wantArgs: []interface{}{10}, + }, { input: func() Querier { selector := Select().Where(EQ("name", "foo").Or().EQ("name", "bar")) @@ -377,6 +647,15 @@ func TestBuilder(t *testing.T) { wantQuery: "DELETE FROM `users` WHERE `name` = ? OR `name` = ?", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + selector := d.Select().Where(EQ("name", "foo").Or().EQ("name", "bar")) + return d.Delete("users").FromSelect(selector) + }(), + wantQuery: `DELETE FROM "users" WHERE "name" = $1 OR "name" = $2`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: func() Querier { selector := Select().From(Table("users")).As("t") @@ -384,6 +663,14 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT `t`.`name` FROM `users`", }, + { + input: func() Querier { + selector := Dialect(dialect.Postgres). + Select().From(Table("users")).As("t") + return selector.Select(selector.C("name")) + }(), + wantQuery: `SELECT "t"."name" FROM "users"`, + }, { input: func() Querier { selector := Select().From(Table("groups")).Where(EQ("name", "foo")) @@ -392,6 +679,15 @@ func TestBuilder(t *testing.T) { wantQuery: "DELETE FROM `groups` WHERE `name` = ?", wantArgs: []interface{}{"foo"}, }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + selector := d.Select().From(Table("groups")).Where(EQ("name", "foo")) + return d.Delete("users").FromSelect(selector) + }(), + wantQuery: `DELETE FROM "groups" WHERE "name" = $1`, + wantArgs: []interface{}{"foo"}, + }, { input: func() Querier { selector := Select() @@ -400,18 +696,79 @@ func TestBuilder(t *testing.T) { wantQuery: "DELETE FROM `users`", }, { - input: Select().From(Table("users")).Where(Not(EQ("name", "foo").And().EQ("age", "bar"))), + input: func() Querier { + d := Dialect(dialect.Postgres) + selector := d.Select() + return d.Delete("users").FromSelect(selector) + }(), + wantQuery: `DELETE FROM "users"`, + }, + { + input: Select(). + From(Table("users")). + Where(Not(EQ("name", "foo").And().EQ("age", "bar"))), wantQuery: "SELECT * FROM `users` WHERE NOT (`name` = ? AND `age` = ?)", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: Dialect(dialect.Postgres). + Select(). + From(Table("users")). + Where(Not(EQ("name", "foo").And().EQ("age", "bar"))), + wantQuery: `SELECT * FROM "users" WHERE NOT ("name" = $1 AND "age" = $2)`, + wantArgs: []interface{}{"foo", "bar"}, + }, + { + input: Select(). + From(Table("users")). + Where(EqualFold("name", "BAR").Or().EqualFold("name", "BAZ")), + wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) = ? OR LOWER(`name`) = ?", + wantArgs: []interface{}{"bar", "baz"}, + }, + { + input: Dialect(dialect.Postgres). + Select(). + From(Table("users")). + Where(EqualFold("name", "BAR").Or().EqualFold("name", "BAZ")), + wantQuery: `SELECT * FROM "users" WHERE LOWER("name") = $1 OR LOWER("name") = $2`, + wantArgs: []interface{}{"bar", "baz"}, + }, + { + input: Select(). + From(Table("users")). + Where(ContainsFold("name", "Ariel").And().ContainsFold("nick", "Bar")), + wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) LIKE ? AND LOWER(`nick`) LIKE ?", + wantArgs: []interface{}{"%ariel%", "%bar%"}, + }, + { + input: Dialect(dialect.Postgres). + Select(). + From(Table("users")). + Where(ContainsFold("name", "Ariel").And().ContainsFold("nick", "Bar")), + wantQuery: `SELECT * FROM "users" WHERE LOWER("name") LIKE $1 AND LOWER("nick") LIKE $2`, + wantArgs: []interface{}{"%ariel%", "%bar%"}, + }, { input: func() Querier { - s1 := Select().From(Table("users")).Where(Not(EQ("name", "foo").And().EQ("age", "bar"))) + s1 := Select(). + From(Table("users")). + Where(Not(EQ("name", "foo").And().EQ("age", "bar"))) return Queries{With("users_view").As(s1), Select("name").From(Table("users_view"))} }(), wantQuery: "WITH users_view AS (SELECT * FROM `users` WHERE NOT (`name` = ? AND `age` = ?)) SELECT `name` FROM `users_view`", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + s1 := d.Select(). + From(Table("users")). + Where(Not(EQ("name", "foo").And().EQ("age", "bar"))) + return Queries{d.With("users_view").As(s1), d.Select("name").From(Table("users_view"))} + }(), + wantQuery: `WITH users_view AS (SELECT * FROM "users" WHERE NOT ("name" = $1 AND "age" = $2)) SELECT "name" FROM "users_view"`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: func() Querier { s1 := Select().From(Table("users")).Where(Not(EQ("name", "foo").And().EQ("age", "bar"))).As("users_view") @@ -420,6 +777,15 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT `name` FROM (SELECT * FROM `users` WHERE NOT (`name` = ? AND `age` = ?)) AS `users_view`", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + s1 := d.Select().From(Table("users")).Where(Not(EQ("name", "foo").And().EQ("age", "bar"))).As("users_view") + return d.Select("name").From(s1) + }(), + wantQuery: `SELECT "name" FROM (SELECT * FROM "users" WHERE NOT ("name" = $1 AND "age" = $2)) AS "users_view"`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: func() Querier { t1 := Table("users") @@ -430,6 +796,17 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT * FROM `users` WHERE `users`.`id` IN (SELECT `owner_id` FROM `pets` WHERE `name` = ?)", wantArgs: []interface{}{"pedro"}, }, + { + input: func() Querier { + t1 := Table("users") + return Dialect(dialect.Postgres). + Select(). + From(t1). + Where(In(t1.C("id"), Select("owner_id").From(Table("pets")).Where(EQ("name", "pedro")))) + }(), + wantQuery: `SELECT * FROM "users" WHERE "users"."id" IN (SELECT "owner_id" FROM "pets" WHERE "name" = $1)`, + wantArgs: []interface{}{"pedro"}, + }, { input: func() Querier { t1 := Table("users") @@ -440,14 +817,35 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT * FROM `users` WHERE NOT (`users`.`id` IN (SELECT `owner_id` FROM `pets` WHERE `name` = ?))", wantArgs: []interface{}{"pedro"}, }, + { + input: func() Querier { + t1 := Table("users") + return Dialect(dialect.Postgres). + Select(). + From(t1). + Where(Not(In(t1.C("id"), Select("owner_id").From(Table("pets")).Where(EQ("name", "pedro"))))) + }(), + wantQuery: `SELECT * FROM "users" WHERE NOT ("users"."id" IN (SELECT "owner_id" FROM "pets" WHERE "name" = $1))`, + wantArgs: []interface{}{"pedro"}, + }, { input: Select().Count().From(Table("users")), wantQuery: "SELECT COUNT(*) FROM `users`", }, + { + input: Dialect(dialect.Postgres). + Select().Count().From(Table("users")), + wantQuery: `SELECT COUNT(*) FROM "users"`, + }, { input: Select().Count(Distinct("id")).From(Table("users")), wantQuery: "SELECT COUNT(DISTINCT `id`) FROM `users`", }, + { + input: Dialect(dialect.Postgres). + Select().Count(Distinct("id")).From(Table("users")), + wantQuery: `SELECT COUNT(DISTINCT "id") FROM "users"`, + }, { input: func() Querier { t1 := Table("users") @@ -457,10 +855,26 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT COUNT(DISTINCT `t0`.`id`, `t0`.`name`) FROM `users` AS `t0` JOIN `users` AS `t0` ON `groups`.`id` = `t0`.`blocked_id`", }, + { + input: func() Querier { + d := Dialect(dialect.Postgres) + t1 := d.Table("users") + t2 := d.Select().From(Table("groups")) + t3 := d.Select().Count().From(t1).Join(t1).On(t2.C("id"), t1.C("blocked_id")) + return t3.Count(Distinct(t3.Columns("id", "name")...)) + }(), + wantQuery: `SELECT COUNT(DISTINCT "t0"."id", "t0"."name") FROM "users" AS "t0" JOIN "users" AS "t0" ON "groups"."id" = "t0"."blocked_id"`, + }, { input: Select(Sum("age"), Min("age")).From(Table("users")), wantQuery: "SELECT SUM(`age`), MIN(`age`) FROM `users`", }, + { + input: Dialect(dialect.Postgres). + Select(Sum("age"), Min("age")). + From(Table("users")), + wantQuery: `SELECT SUM("age"), MIN("age") FROM "users"`, + }, { input: func() Querier { t1 := Table("users").As("u") @@ -468,12 +882,28 @@ func TestBuilder(t *testing.T) { }(), wantQuery: "SELECT MAX(`u`.`age`) AS `max_age` FROM `users` AS `u`", }, + { + input: func() Querier { + t1 := Table("users").As("u") + return Dialect(dialect.Postgres). + Select(As(Max(t1.C("age")), "max_age")). + From(t1) + }(), + wantQuery: `SELECT MAX("u"."age") AS "max_age" FROM "users" AS "u"`, + }, { input: Select("name", Count("*")). From(Table("users")). GroupBy("name"), wantQuery: "SELECT `name`, COUNT(*) FROM `users` GROUP BY `name`", }, + { + input: Dialect(dialect.Postgres). + Select("name", Count("*")). + From(Table("users")). + GroupBy("name"), + wantQuery: `SELECT "name", COUNT(*) FROM "users" GROUP BY "name"`, + }, { input: Select("name", Count("*")). From(Table("users")). @@ -481,6 +911,14 @@ func TestBuilder(t *testing.T) { OrderBy("name"), wantQuery: "SELECT `name`, COUNT(*) FROM `users` GROUP BY `name` ORDER BY `name`", }, + { + input: Dialect(dialect.Postgres). + Select("name", Count("*")). + From(Table("users")). + GroupBy("name"). + OrderBy("name"), + wantQuery: `SELECT "name", COUNT(*) FROM "users" GROUP BY "name" ORDER BY "name"`, + }, { input: Select("name", "age", Count("*")). From(Table("users")). @@ -489,23 +927,64 @@ func TestBuilder(t *testing.T) { wantQuery: "SELECT `name`, `age`, COUNT(*) FROM `users` GROUP BY `name`, `age` ORDER BY `name` DESC, `age`", }, { - input: Select("*").From(Table("users")).Limit(1), + input: Dialect(dialect.Postgres). + Select("name", "age", Count("*")). + From(Table("users")). + GroupBy("name", "age"). + OrderBy(Desc("name"), "age"), + wantQuery: `SELECT "name", "age", COUNT(*) FROM "users" GROUP BY "name", "age" ORDER BY "name" DESC, "age"`, + }, + { + input: Select("*"). + From(Table("users")). + Limit(1), wantQuery: "SELECT * FROM `users` LIMIT ?", wantArgs: []interface{}{1}, }, + { + input: Dialect(dialect.Postgres). + Select("*"). + From(Table("users")). + Limit(1), + wantQuery: `SELECT * FROM "users" LIMIT $1`, + wantArgs: []interface{}{1}, + }, { input: Select("age").Distinct().From(Table("users")), wantQuery: "SELECT DISTINCT `age` FROM `users`", }, + { + input: Dialect(dialect.Postgres). + Select("age"). + Distinct(). + From(Table("users")), + wantQuery: `SELECT DISTINCT "age" FROM "users"`, + }, { input: Select("age", "name").From(Table("users")).Distinct().OrderBy("name"), wantQuery: "SELECT DISTINCT `age`, `name` FROM `users` ORDER BY `name`", }, + { + input: Dialect(dialect.Postgres). + Select("age", "name"). + From(Table("users")). + Distinct(). + OrderBy("name"), + wantQuery: `SELECT DISTINCT "age", "name" FROM "users" ORDER BY "name"`, + }, { input: Select("age").From(Table("users")).Where(EQ("name", "foo")).Or().Where(EQ("name", "bar")), wantQuery: "SELECT `age` FROM `users` WHERE (`name` = ?) OR (`name` = ?)", wantArgs: []interface{}{"foo", "bar"}, }, + { + input: Dialect(dialect.Postgres). + Select("age"). + From(Table("users")). + Where(EQ("name", "foo")).Or().Where(EQ("name", "bar")), + wantQuery: `SELECT "age" FROM "users" WHERE ("name" = $1) OR ("name" = $2)`, + wantArgs: []interface{}{"foo", "bar"}, + }, { input: Queries{With("users_view").As(Select().From(Table("users"))), Select().From(Table("users_view"))}, wantQuery: "WITH users_view AS (SELECT * FROM `users`) SELECT * FROM `users_view`", @@ -522,14 +1001,34 @@ func TestBuilder(t *testing.T) { input: CreateIndex("name_index").Table("users").Column("name"), wantQuery: "CREATE INDEX `name_index` ON `users`(`name`)", }, + { + input: Dialect(dialect.Postgres). + CreateIndex("name_index"). + Table("users"). + Column("name"), + wantQuery: `CREATE INDEX "name_index" ON "users"("name")`, + }, { input: CreateIndex("unique_name").Unique().Table("users").Columns("first", "last"), wantQuery: "CREATE UNIQUE INDEX `unique_name` ON `users`(`first`, `last`)", }, + { + input: Dialect(dialect.Postgres). + CreateIndex("unique_name"). + Unique(). + Table("users"). + Columns("first", "last"), + wantQuery: `CREATE UNIQUE INDEX "unique_name" ON "users"("first", "last")`, + }, { input: DropIndex("name_index"), wantQuery: "DROP INDEX `name_index`", }, + { + input: Dialect(dialect.Postgres). + DropIndex("name_index"), + wantQuery: `DROP INDEX "name_index"`, + }, { input: DropIndex("name_index").Table("users"), wantQuery: "DROP INDEX `name_index` ON `users`", diff --git a/dialect/sql/schema/migrate.go b/dialect/sql/schema/migrate.go index f11317e03..13eb9d087 100644 --- a/dialect/sql/schema/migrate.go +++ b/dialect/sql/schema/migrate.go @@ -202,7 +202,7 @@ func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change } } // if there's actual action to execute on ALTER TABLE. - if len(b.Queriers) != 0 { + if len(b.Queries) != 0 { query, args := b.Query() if err := tx.Exec(ctx, query, args, new(sql.Result)); err != nil { return fmt.Errorf("alter table %q: %v", table, err) diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index 84ccbdfe4..cdc2fb485 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -134,7 +134,7 @@ func templateBaseTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/base.tmpl", size: 5214, mode: os.FileMode(420), modTime: time.Unix(1570440164, 0)} + info := bindataFileInfo{name: "template/base.tmpl", size: 5214, mode: os.FileMode(420), modTime: time.Unix(1570451137, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -154,7 +154,7 @@ func templateBuilderCreateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/builder/create.tmpl", size: 3054, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/builder/create.tmpl", size: 3054, mode: os.FileMode(420), modTime: time.Unix(1570030748, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -174,7 +174,7 @@ func templateBuilderDeleteTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/builder/delete.tmpl", size: 2446, mode: os.FileMode(420), modTime: time.Unix(1568794256, 0)} + info := bindataFileInfo{name: "template/builder/delete.tmpl", size: 2446, mode: os.FileMode(420), modTime: time.Unix(1568645716, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -194,7 +194,7 @@ func templateBuilderQueryTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/builder/query.tmpl", size: 16113, mode: os.FileMode(420), modTime: time.Unix(1568794256, 0)} + info := bindataFileInfo{name: "template/builder/query.tmpl", size: 16113, mode: os.FileMode(420), modTime: time.Unix(1568645715, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -214,7 +214,7 @@ func templateBuilderSetterTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/builder/setter.tmpl", size: 4397, mode: os.FileMode(420), modTime: time.Unix(1568897494, 0)} + info := bindataFileInfo{name: "template/builder/setter.tmpl", size: 4397, mode: os.FileMode(420), modTime: time.Unix(1568906061, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -234,7 +234,7 @@ func templateBuilderUpdateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/builder/update.tmpl", size: 8088, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/builder/update.tmpl", size: 8088, mode: os.FileMode(420), modTime: time.Unix(1570030781, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -254,7 +254,7 @@ func templateClientTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/client.tmpl", size: 6205, mode: os.FileMode(420), modTime: time.Unix(1570020385, 0)} + info := bindataFileInfo{name: "template/client.tmpl", size: 6205, mode: os.FileMode(420), modTime: time.Unix(1570008718, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -274,7 +274,7 @@ func templateConfigTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/config.tmpl", size: 1254, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/config.tmpl", size: 1254, mode: os.FileMode(420), modTime: time.Unix(1567330565, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -294,7 +294,7 @@ func templateContextTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/context.tmpl", size: 719, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/context.tmpl", size: 719, mode: os.FileMode(420), modTime: time.Unix(1567330561, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -314,7 +314,7 @@ func templateDialectGremlinByTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/by.tmpl", size: 1875, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/by.tmpl", size: 1875, mode: os.FileMode(420), modTime: time.Unix(1567330626, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -334,7 +334,7 @@ func templateDialectGremlinCreateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/create.tmpl", size: 2763, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/create.tmpl", size: 2763, mode: os.FileMode(420), modTime: time.Unix(1567330629, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -354,7 +354,7 @@ func templateDialectGremlinDecodeTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/decode.tmpl", size: 2132, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/decode.tmpl", size: 2132, mode: os.FileMode(420), modTime: time.Unix(1570281258, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -374,7 +374,7 @@ func templateDialectGremlinDeleteTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/delete.tmpl", size: 825, mode: os.FileMode(420), modTime: time.Unix(1568794256, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/delete.tmpl", size: 825, mode: os.FileMode(420), modTime: time.Unix(1568645716, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -394,7 +394,7 @@ func templateDialectGremlinErrorsTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/errors.tmpl", size: 1804, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/errors.tmpl", size: 1804, mode: os.FileMode(420), modTime: time.Unix(1567330638, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -414,7 +414,7 @@ func templateDialectGremlinGroupTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/group.tmpl", size: 1347, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/group.tmpl", size: 1347, mode: os.FileMode(420), modTime: time.Unix(1567526275, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -434,7 +434,7 @@ func templateDialectGremlinMetaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/meta.tmpl", size: 704, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/meta.tmpl", size: 704, mode: os.FileMode(420), modTime: time.Unix(1567330643, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -454,7 +454,7 @@ func templateDialectGremlinOpenTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/open.tmpl", size: 542, mode: os.FileMode(420), modTime: time.Unix(1570440164, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/open.tmpl", size: 542, mode: os.FileMode(420), modTime: time.Unix(1570451137, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -474,7 +474,7 @@ func templateDialectGremlinPredicateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/predicate.tmpl", size: 3313, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/predicate.tmpl", size: 3313, mode: os.FileMode(420), modTime: time.Unix(1567330647, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -494,7 +494,7 @@ func templateDialectGremlinQueryTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/query.tmpl", size: 3929, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/query.tmpl", size: 3929, mode: os.FileMode(420), modTime: time.Unix(1570094435, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -514,7 +514,7 @@ func templateDialectGremlinSelectTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/select.tmpl", size: 1078, mode: os.FileMode(420), modTime: time.Unix(1568019515, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/select.tmpl", size: 1078, mode: os.FileMode(420), modTime: time.Unix(1567600027, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -534,7 +534,7 @@ func templateDialectGremlinUpdateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/update.tmpl", size: 6095, mode: os.FileMode(420), modTime: time.Unix(1568624278, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/update.tmpl", size: 6095, mode: os.FileMode(420), modTime: time.Unix(1568542264, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -554,12 +554,12 @@ func templateDialectSqlByTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/by.tmpl", size: 949, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/sql/by.tmpl", size: 949, mode: os.FileMode(420), modTime: time.Unix(1567330589, 0)} a := &asset{bytes: bytes, info: info} return a, nil } -var _templateDialectSqlCreateTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x58\xdf\x53\xdb\xb8\x16\x7e\xb6\xff\x8a\x73\x19\xda\xb1\x99\x20\x28\x6f\x97\x5e\xee\x0c\xcb\x8f\x99\xec\x16\x68\x09\xdd\x7d\x68\x3b\x3b\x8a\x7d\x94\x68\x51\xa4\x20\xc9\x29\x4c\xc6\xff\xfb\x8e\x64\x3b\x76\x8c\x81\x90\x76\xca\xee\xce\x3e\x25\xb6\xcf\x2f\x7d\xfa\x74\x3e\x49\xf3\xf9\xce\x56\x78\xa4\xa6\x77\x9a\x8f\xc6\x16\xf6\x76\xdf\xfc\x77\x7b\xaa\xd1\xa0\xb4\x70\x4a\x13\x1c\x2a\x75\x0d\x7d\x99\x10\x38\x14\x02\xbc\x91\x01\xf7\x5d\xcf\x30\x25\xe1\xd5\x98\x1b\x30\x2a\xd3\x09\x42\xa2\x52\x04\x6e\x40\xf0\x04\xa5\xc1\x14\x32\x99\xa2\x06\x3b\x46\x38\x9c\xd2\x64\x8c\xb0\x47\x76\xab\xaf\xc0\x54\x26\xd3\x90\x4b\xff\xfd\x5d\xff\xe8\xe4\x7c\x70\x02\x8c\x0b\x84\xf2\x9d\x56\xca\x42\xca\x35\x26\x56\xe9\x3b\x50\x0c\x6c\x23\x99\xd5\x88\x24\xdc\xda\xc9\xf3\x30\x9c\xcf\x21\x45\xc6\x25\xc2\x46\xca\xa9\xc0\xc4\xee\x98\x1b\xb1\x93\x68\xa4\x16\x37\x20\xcf\x9d\xc5\xe6\x30\xe3\xc2\xd5\xb3\x7f\x00\x53\x6a\x12\x2a\x60\x93\x0c\x12\x35\x45\xf2\x53\xf9\xa5\x34\xd4\x98\x20\x9f\x15\x96\x8b\xff\x0b\x77\x97\x90\x65\x32\x81\x68\xc9\x36\xcf\x61\xab\x99\x25\xcf\x63\x30\x37\x62\x40\x67\x18\x25\xf6\x16\x12\x25\x2d\xde\x5a\x72\x54\xfc\xc6\x10\x79\x73\x72\x4e\x27\x08\x79\xde\x03\xd4\x5a\xe9\x18\xe6\x61\x30\xa3\x1a\xa2\x30\x08\x34\x1a\x17\x82\x5c\xa2\xc9\x84\x0d\x83\xc0\x3b\x5c\x36\x32\x1e\xc0\xeb\x66\x90\x79\xa2\x24\xe3\xa3\x7d\x68\x55\x46\x8a\xf7\x79\x18\xc4\x61\x60\x6f\x7d\x2e\x37\xb8\xb6\x59\xaa\xdd\x3f\x72\x75\xeb\x2a\x8e\xc3\x80\x33\x6f\xf9\x9f\x03\x90\x5c\xb8\xca\x02\x8d\x36\xd3\xd2\x3d\xfa\x20\x61\x90\x87\x41\x03\x57\x57\x6d\x5f\x1a\xd4\xd6\x83\x43\xde\xd3\xe4\x9a\x8e\x5c\x69\xe4\x8a\x0e\x05\xc6\xe4\x18\x19\xcd\x84\x6d\x63\x57\xa5\x3e\x2e\xa6\x2f\x8a\xe3\x30\x98\xcf\xb7\x41\x53\x39\x42\xd8\xfc\xbd\x07\x9b\xcc\x25\xd8\x24\xa7\x1c\x45\x6a\xdc\x2c\x04\xae\xbe\x19\x15\x19\x76\x8d\xc5\x79\x6f\x32\x32\xb0\x3a\x4b\xac\x77\x82\x3c\x7f\x5b\xda\x37\x46\xe4\xd3\x70\xe6\x6c\xfb\xe6\xe7\xc1\xc5\x79\x11\x3a\x08\x86\x19\x5b\x00\xf5\x87\x51\x92\x9c\x51\x6d\xc6\x54\x44\x5b\x3e\x46\xec\x8d\xee\x23\x14\x74\x80\x14\x04\x41\x15\xd3\x43\x45\x06\x78\x1f\x20\xf7\xcc\x1c\x3d\x8c\xa5\xd2\x7a\x4a\x0c\x33\x16\x57\x25\xa2\x30\x58\x97\xf6\xac\x30\x8d\x82\x7d\x24\x99\x96\x81\xda\x7c\x72\xce\xd5\xd2\x60\x15\xa9\xc0\x63\xcb\x19\x48\x65\xfd\x6b\x2e\x84\x9b\x4b\xc8\x73\xc7\xe0\x22\x9a\xcf\x10\xfa\x61\x36\x53\xdc\x64\xa8\xef\x7a\x40\xf5\xc8\x38\x18\xab\xba\x3f\xb8\xd7\x51\x4d\xb0\xfd\x03\xb0\xb7\xe4\xe4\x16\x13\x47\xbc\x1e\x34\xdc\x7a\xf0\x5a\xa3\x89\xdf\x3e\x46\x44\xad\x84\x18\xd2\xe4\x3a\x2a\xa9\x1d\x7b\x5a\xf2\x74\x31\x7d\x1a\x0d\x79\x47\x8d\x2d\xa8\xd9\x4f\xa3\x27\xb9\xdd\x1d\xf2\x1e\x5e\xfd\xe3\x05\x3a\x9b\xa4\x7f\x4c\xfa\x66\x60\x35\x97\x23\xc8\x73\x63\x75\xa2\xe4\x8c\x9c\x2a\x3d\xa1\xb6\x2f\x6d\xe4\x0a\x7a\xb3\x1b\x3b\xcc\x8a\xb9\xf4\xe1\xfa\xc7\xe4\xea\x6e\xea\x1e\x23\x9e\xc6\x0b\x40\xef\xd1\x1f\x0b\xfa\x9f\xa4\x23\xac\xd9\x2f\x50\xde\x5b\x49\xee\x19\x5b\xbc\x8f\xe1\xff\xb0\xbb\x44\x77\x2a\x53\x67\xf6\x51\xf2\x9b\x0c\xbd\x03\x0a\x76\x89\xcc\x97\xb5\xb3\x05\x17\x7b\x17\xf0\x95\xdb\x31\x18\x14\x0c\x34\x32\xd4\x28\x13\x04\xdf\x70\x1d\x07\x99\xd2\x80\x3c\xf5\xf0\xfa\x2a\x57\xa9\xa3\x5a\x21\xae\x08\x8b\x93\xa9\xa0\xb6\xb3\x67\xef\x38\xe8\x50\x5b\x9e\x6e\xb8\x91\x6f\x97\x39\xdb\x7c\x72\xed\xe6\xe3\x34\xa5\x16\x3b\x97\x01\x16\x4d\xa7\xb1\x16\x62\x52\xc4\x09\x82\x87\x96\x0e\x92\x23\x25\xb2\x89\x5c\x5a\x40\xc8\xd3\xda\xf3\xb7\x31\x6a\x8c\x5c\xea\x93\x0f\x9d\x21\xdc\x94\x2e\xb9\xf3\x34\x8e\x6b\xce\x37\xda\xc6\x1a\xbc\x6f\xf7\x97\x0e\xa2\xd6\xed\x66\x09\xaf\x1f\x07\xd7\xa3\x68\xa1\xa7\x7b\x47\x6e\xf7\xb6\x8d\x9c\x07\xfe\x50\xa6\x51\x4c\xfa\xe6\x3c\x13\x62\xd5\x22\x5e\x0a\x70\xca\x18\x26\x16\x97\x5b\xcf\xa5\xfa\x6a\x0e\xcb\x0f\xad\x82\xd6\x4e\xc4\x19\x70\x69\xa3\x2a\x5f\x0c\xff\x7b\x46\x33\x78\x32\xdd\xeb\x13\xad\x3d\x9a\x9a\x72\x69\x4f\x29\x17\x98\xce\x27\x66\xb4\x0f\x6c\x62\xc9\x60\xaa\xb9\xb4\x2c\xda\xf8\xbc\x51\xc4\x2f\x95\xe2\xf3\x06\x44\xaf\x66\x31\x50\xa1\x91\xa6\x77\x6e\xcb\x23\x7d\x75\x60\x15\x50\x48\x39\xf3\x5d\xc4\x42\xe1\x57\xbb\x6d\x14\x33\x9d\x2f\x0d\x31\x5f\x52\x3e\xd7\x61\x91\x9c\xed\x9d\x01\xbc\x64\x03\x72\x31\xa9\x4b\xb9\x5b\xf6\xef\xa1\x7b\x78\xe3\x1f\xb6\xcb\x22\xfb\xa6\xef\x7c\x17\x2d\x9e\x42\x65\xe1\xcc\x17\xae\xb5\x0c\x77\xf6\xb5\x07\xb6\x51\x4f\x2d\xd4\x62\x25\x98\x07\xfc\xde\xff\xd2\x70\xfa\x54\x14\x97\xe7\x5f\x7a\xb0\xaa\xf9\xd0\x99\xd7\xd9\x7e\x75\xb2\x6f\xbc\xb2\x2d\xf5\xc8\x1a\x8c\x96\xac\x38\x35\xd9\xd6\xc8\xa0\x80\xd9\xf8\x8d\x3e\x7a\x4d\xe3\x12\x86\xca\x8e\xe1\x2b\xbd\x33\xa4\xd6\x99\x46\x1a\x74\x79\xda\x69\x9a\x30\x06\xc1\x4b\x2c\xfa\x6e\xa2\x5e\xbc\x28\x4f\xbf\x9b\x50\xae\xad\x93\x6b\xca\x64\xf8\xd7\x9a\xc7\x8b\xbd\xb3\x6a\x1e\xa7\x15\x90\xef\xcb\xba\x5e\x64\x62\xa7\xe4\x42\x47\xf1\xda\x62\xda\x38\x8b\x7c\x2f\x8a\xac\xb9\x35\xa8\xf9\xe1\xf4\x7d\xda\x2b\x9a\xde\x33\x45\xbe\x0a\xd6\xa4\xcb\x37\xb1\xe5\x69\xb2\x14\xe0\xad\xaa\xf3\x4f\x9e\x0f\x1f\x4b\xf2\x3d\x34\xfe\x5b\x25\x5e\x49\x04\xc5\xe0\xbe\xd2\xbf\x9a\xad\xa5\xf3\xd7\x78\x67\x56\x1b\x41\xb5\x1d\xc8\x5b\x27\xe0\xc5\xf1\x64\x21\x11\x95\xda\x2c\x68\xdf\x38\x86\x15\x28\x60\xe3\x30\x58\x1d\xcc\x0e\xad\xe2\xd1\xea\xe5\x7c\xda\xfd\xb2\xf2\xd6\xad\x3a\xf4\x07\x75\x7d\xcd\xe3\x7b\x50\x76\x8d\x67\x25\x0f\x3b\x04\xaf\x7b\xcf\xf1\xb7\x56\x80\x75\x77\xfb\x1d\xba\x71\x0f\xf6\x97\x81\xe4\x31\x44\x7e\xd8\x61\xe8\x21\x78\x6a\x2e\xfd\xdb\x36\xff\xa9\x6d\xb3\x9a\xe3\xd6\xdd\xdc\xd2\x84\x1f\xa9\xc9\x84\xdb\xe8\xf1\x7b\xb6\xea\xc2\xb7\x7c\xd7\xbe\x0d\xeb\x39\xab\xb0\xb8\x8e\x2f\x93\x3c\x7e\x33\xdf\xdc\xe3\x54\x37\x5e\x8f\xf4\xf1\x07\x9b\x78\xb9\xb1\xe9\x20\xcd\x53\x94\x59\x86\xa4\xae\xfb\xcf\x00\x00\x00\xff\xff\x6f\x02\x81\xbd\x15\x19\x00\x00") +var _templateDialectSqlCreateTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x58\xdf\x53\xdb\xb8\x16\x7e\xb6\xff\x8a\x73\x19\xda\xb1\x99\x20\x28\x6f\x97\x5e\xee\x0c\xcb\x8f\x99\xec\x16\x68\x09\xdd\x7d\x68\x3b\x3b\x8a\x7d\x94\x68\x51\xa4\x20\xc9\x29\x4c\xc6\xff\xfb\x8e\x64\x3b\x76\x8c\x81\x90\x76\xca\xee\xce\xf2\x42\x6c\x9f\x5f\xfa\xf4\xe9\x7c\x92\xe6\xf3\x9d\xad\xf0\x48\x4d\xef\x34\x1f\x8d\x2d\xec\xed\xbe\xf9\xef\xf6\x54\xa3\x41\x69\xe1\x94\x26\x38\x54\xea\x1a\xfa\x32\x21\x70\x28\x04\x78\x23\x03\xee\xbb\x9e\x61\x4a\xc2\xab\x31\x37\x60\x54\xa6\x13\x84\x44\xa5\x08\xdc\x80\xe0\x09\x4a\x83\x29\x64\x32\x45\x0d\x76\x8c\x70\x38\xa5\xc9\x18\x61\x8f\xec\x56\x5f\x81\xa9\x4c\xa6\x21\x97\xfe\xfb\xbb\xfe\xd1\xc9\xf9\xe0\x04\x18\x17\x08\xe5\x3b\xad\x94\x85\x94\x6b\x4c\xac\xd2\x77\xa0\x18\xd8\x46\x32\xab\x11\x49\xb8\xb5\x93\xe7\x61\x38\x9f\x43\x8a\x8c\x4b\x84\x8d\x94\x53\x81\x89\xdd\x31\x37\x62\x27\xd1\x48\x2d\x6e\x40\x9e\x3b\x8b\xcd\x61\xc6\x85\xab\x67\xff\x00\xa6\xd4\x24\x54\xc0\x26\x19\x24\x6a\x8a\xe4\xa7\xf2\x4b\x69\xa8\x31\x41\x3e\x2b\x2c\x17\xbf\x17\xee\x2e\x21\xcb\x64\x02\xd1\x92\x6d\x9e\xc3\x56\x33\x4b\x9e\xc7\x60\x6e\xc4\x80\xce\x30\x4a\xec\x2d\x24\x4a\x5a\xbc\xb5\xe4\xa8\xf8\x1f\x43\xe4\xcd\xc9\x39\x9d\x20\xe4\x79\x0f\x50\x6b\xa5\x63\x98\x87\xc1\x8c\x6a\x88\xc2\x20\xd0\x68\x5c\x08\x72\x89\x26\x13\x36\x0c\x02\xef\x70\xd9\xc8\x78\x00\xaf\x9b\x41\xe6\x89\x92\x8c\x8f\xf6\xa1\x55\x19\x29\xde\xe7\x61\x10\x87\x81\xbd\xf5\xb9\xdc\xe0\xda\x66\xa9\x76\xbf\xc8\xd5\xad\xab\x38\x0e\x03\xce\xbc\xe5\x7f\x0e\x40\x72\xe1\x2a\x0b\x34\xda\x4c\x4b\xf7\xe8\x83\x84\x41\x1e\x06\x0d\x5c\x5d\xb5\xc7\xc5\x0c\xb4\xd1\xa9\x82\x57\x9f\xe3\x98\x84\x41\xd0\x97\x06\x75\x61\x4b\xde\xd3\xe4\x9a\x8e\xdc\x38\xc8\x15\x1d\x0a\xf4\x06\xc7\xc8\x68\x26\x6c\x14\x87\xc1\x7c\xbe\x0d\x9a\xca\x11\xc2\xe6\xef\x3d\xd8\x64\x2e\xe1\x26\x39\xe5\x28\x52\xe3\x66\x25\x70\xf5\xce\xa8\xc8\xb0\x6b\x6c\xce\x7b\x93\x91\x81\xd5\x59\x62\xbd\x13\xe4\xf9\xdb\xd2\xbe\x31\x42\x9f\x86\x33\x67\xdb\x37\x3f\x0f\x2e\xce\x8b\xd0\x41\x30\xcc\xd8\x02\xb8\x3f\x8c\x92\xe4\x8c\x6a\x33\xa6\x22\xda\xf2\x31\x62\x6f\x74\x1f\xb1\xa0\x03\xb4\x20\x08\xaa\x98\x1e\x3a\x32\xc0\xfb\x18\xb8\x67\xe6\xe8\x62\x2c\x95\xd6\x53\x64\x98\xb1\xb8\x2a\x11\x85\xc1\xba\xb4\x67\x85\x69\x14\xec\x23\xc9\xb4\x0c\xd4\xe6\x97\x73\xae\x96\x0a\xab\x48\x06\x1e\x5b\xce\x40\x2a\xeb\x5f\x73\x21\xdc\x74\x41\x9e\x3b\x46\x17\xd1\x7c\x86\xd0\x0f\xb3\x99\xe2\x26\x43\x7d\xd7\x03\xaa\x47\xc6\xc1\x58\xd5\xfd\xc1\xbd\x8e\x6a\xc2\xed\x1f\x80\xbd\x25\x27\xb7\x98\x38\x22\xf6\xa0\xe1\xd6\x83\xd7\x1a\x4d\xfc\xf6\x31\x62\x6a\x25\xc4\x90\x26\xd7\x51\x49\xf5\xd8\xd3\x94\xa7\x8b\xe9\xd3\x68\xc8\x3b\x6a\x6c\xc1\xbe\x7e\x1a\x3d\xc9\xf5\xee\x90\xf7\xf0\xea\x1f\x2f\xd0\xd9\x24\xfd\x63\xd2\x37\x03\xab\xb9\x1c\x41\x9e\x1b\xab\x13\x25\x67\xe4\x54\xe9\x09\xb5\x7d\x69\x23\x57\xd0\x9b\xdd\xd8\x61\x56\xcc\xa5\x0f\xd7\x3f\x26\x57\x77\x53\xf7\x18\xf1\x34\x5e\x00\x7a\x8f\xfe\x58\xd0\xff\x24\x1d\x61\xcd\x7e\x81\xf2\xde\xba\x73\xcf\xd8\xe2\x7d\x0c\xff\x87\xdd\x25\xba\x53\x99\x3a\xb3\x8f\x92\xdf\x64\xe8\x1d\x50\xb0\x4b\x64\xbe\xac\x9d\x2d\xb8\xd8\xbb\x80\xaf\xdc\x8e\xc1\xa0\x60\xa0\x91\xa1\x46\x99\x20\xf8\x06\xec\x38\xc8\x94\x06\xe4\xa9\x87\xd7\x57\xb9\x4a\x1d\xd5\x0a\x71\x45\x58\x9c\x4c\x05\xb5\x9d\x3d\x7c\xc7\x41\x87\xda\xf2\x74\xc3\x8d\x7c\xbb\xcc\xd9\xe6\x93\x6b\x3f\x1f\xa7\x29\xb5\xd8\xb9\x0c\xb0\xe8\x2b\x8d\xb5\xe0\x5b\x8c\xff\x7b\x68\xe9\x20\x39\x52\x22\x9b\xc8\xa5\x05\x84\x3c\xad\x3d\x7f\x1b\xa3\xc6\xc8\xa5\x3e\xf9\xd0\x19\xc2\x4d\xe9\x92\x3b\x4f\xe3\xb8\xe6\x7c\xa3\x6d\xac\xc1\xfb\x76\x7f\xe9\x20\x6a\xdd\x6e\x96\xf0\xfa\x71\x70\x3d\x8a\x16\x7a\xba\x77\xe4\x76\x6f\xdb\xc8\x79\xe0\x0f\x65\x1a\xc5\xa4\x6f\xce\x33\x21\x56\x2d\xe2\xa5\x00\xa7\x8c\x61\x62\x71\xb9\xf5\x5c\xaa\xaf\xe6\xb0\xfc\xd0\x2a\x68\xed\x44\x9c\x01\x97\x36\xaa\xf2\xc5\xf0\xbf\x67\x34\x83\x27\xd3\xbd\x3e\xd1\xda\xa3\xa9\x29\x97\xf6\x94\x72\x81\xe9\x7c\x62\x46\xfb\xc0\x26\x96\x0c\xa6\x9a\x4b\xcb\xa2\x8d\xcf\x1b\x45\xfc\x52\x29\x3e\x6f\x40\xf4\x6a\x16\x03\x15\x1a\x69\x7a\xe7\xb6\x40\xd2\x57\x07\x56\x01\x85\x94\x33\xdf\x45\x2c\x14\x7e\xb5\xdb\x46\x31\xd3\xf9\xd2\x10\xf3\x25\xe5\x73\x1d\x16\xc9\xd9\xde\x19\xc0\x4b\x36\x20\x17\x93\xba\x94\xbb\x65\xff\x1e\xba\x87\x37\xfe\x61\xbb\x2c\xb2\x6f\xfa\xce\x77\xd1\xe2\x29\x54\x16\xce\x7c\xe1\x5a\xcb\x70\x67\x5f\x7b\x60\xa7\xf4\xd4\x42\x2d\x56\x82\x79\xc0\xef\xfd\x2f\x0d\xa7\x4f\x45\x71\x79\xfe\xa5\x07\xab\x9a\x0f\x9d\x79\x9d\xed\x57\x27\xfb\xc6\x2b\xdb\x52\x8f\xac\xc1\x68\xc9\x8a\x53\x93\x6d\x8d\x0c\x0a\x98\x8d\xdf\xf8\xa3\xd7\x34\x2e\x61\xa8\xec\x18\xbe\xd2\x3b\x43\x6a\x9d\x69\xa4\x41\x97\xa7\x9d\xa6\x09\x63\x10\xbc\xc4\xa2\xef\x26\xea\xc5\x8b\xf2\xf4\xbb\x09\xe5\xda\x3a\xb9\xa6\x4c\x86\x7f\xad\x79\xbc\xd8\x3b\xab\xe6\x71\x5a\x01\xf9\xbe\xac\xeb\x45\x26\x76\x4a\x2e\x74\x14\xaf\x2d\xa6\x8d\xb3\xc8\xf7\xa2\xc8\x9a\x5b\x83\x9a\x1f\x4e\xdf\xa7\xbd\xa2\xe9\x3d\x53\xe4\xab\x60\x4d\xba\x7c\x13\x5b\x9e\x26\x4b\x01\xde\xaa\x3a\xff\xe4\xf9\xf0\xb1\x24\xdf\x43\xe3\xbf\x55\xe2\x95\x44\x50\x0c\xee\x2b\xfd\xab\xd9\x5a\x3a\x7f\x8d\x77\x66\xb5\x11\x54\xdb\x81\xbc\x75\x02\x5e\x1c\x4f\x16\x12\x51\xa9\xcd\x82\xf6\x8d\x63\x58\x81\x02\x36\x0e\x83\xd5\xc1\xec\xd0\x2a\x1e\xad\x5e\xce\xa7\xdd\x2f\x2b\x6f\xdd\xaa\x43\x7f\x50\xd7\xd7\x3c\xbe\x07\x65\xd7\x78\x56\xf2\xb0\x43\xf0\xba\xf7\x1c\x7f\x6b\x05\x58\x77\xb7\xdf\xa1\x1b\xf7\x60\x7f\x19\x48\x1e\x43\xe4\x87\x1d\x86\x1e\x82\xa7\xe6\xd2\xbf\x6d\xf3\x9f\xda\x36\xab\x39\x6e\xdd\xcd\x2d\x4d\xf8\x91\x9a\x4c\xb8\x8d\x1e\xbf\x67\xab\x2e\x80\xcb\x77\xed\xdb\xb0\x9e\xb3\x0a\x8b\xeb\xf9\x32\xc9\xe3\x37\xf5\xcd\x3d\x4e\x75\xe3\xf5\x48\x1f\x7f\xb0\x89\x97\x1b\x9b\x0e\xd2\x3c\x45\x99\x65\x48\xea\xba\xff\x0c\x00\x00\xff\xff\xb6\xcb\x6e\xfb\x25\x19\x00\x00") func templateDialectSqlCreateTmplBytes() ([]byte, error) { return bindataRead( @@ -574,7 +574,7 @@ func templateDialectSqlCreateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/create.tmpl", size: 6421, mode: os.FileMode(420), modTime: time.Unix(1569318665, 0)} + info := bindataFileInfo{name: "template/dialect/sql/create.tmpl", size: 6437, mode: os.FileMode(420), modTime: time.Unix(1570914948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -594,7 +594,7 @@ func templateDialectSqlDecodeTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/decode.tmpl", size: 2142, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/dialect/sql/decode.tmpl", size: 2142, mode: os.FileMode(420), modTime: time.Unix(1570187959, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -614,7 +614,7 @@ func templateDialectSqlDeleteTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/delete.tmpl", size: 828, mode: os.FileMode(420), modTime: time.Unix(1568794256, 0)} + info := bindataFileInfo{name: "template/dialect/sql/delete.tmpl", size: 828, mode: os.FileMode(420), modTime: time.Unix(1568645716, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -634,7 +634,7 @@ func templateDialectSqlErrorsTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/errors.tmpl", size: 967, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/sql/errors.tmpl", size: 967, mode: os.FileMode(420), modTime: time.Unix(1567330602, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -654,7 +654,7 @@ func templateDialectSqlGroupTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/group.tmpl", size: 1031, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/sql/group.tmpl", size: 1031, mode: os.FileMode(420), modTime: time.Unix(1567330605, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -674,7 +674,7 @@ func templateDialectSqlMetaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/meta.tmpl", size: 1782, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/sql/meta.tmpl", size: 1782, mode: os.FileMode(420), modTime: time.Unix(1567330610, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -694,7 +694,7 @@ func templateDialectSqlOpenTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/open.tmpl", size: 389, mode: os.FileMode(420), modTime: time.Unix(1570020385, 0)} + info := bindataFileInfo{name: "template/dialect/sql/open.tmpl", size: 389, mode: os.FileMode(420), modTime: time.Unix(1570008718, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -714,7 +714,7 @@ func templateDialectSqlPredicateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/predicate.tmpl", size: 4526, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/dialect/sql/predicate.tmpl", size: 4526, mode: os.FileMode(420), modTime: time.Unix(1567330614, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -734,7 +734,7 @@ func templateDialectSqlQueryTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/query.tmpl", size: 6382, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/dialect/sql/query.tmpl", size: 6382, mode: os.FileMode(420), modTime: time.Unix(1570094421, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -754,7 +754,7 @@ func templateDialectSqlSelectTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/select.tmpl", size: 809, mode: os.FileMode(420), modTime: time.Unix(1568019515, 0)} + info := bindataFileInfo{name: "template/dialect/sql/select.tmpl", size: 809, mode: os.FileMode(420), modTime: time.Unix(1567539807, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -774,7 +774,7 @@ func templateDialectSqlUpdateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/update.tmpl", size: 12705, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/dialect/sql/update.tmpl", size: 12705, mode: os.FileMode(420), modTime: time.Unix(1570107181, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -794,7 +794,7 @@ func templateEntTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/ent.tmpl", size: 4199, mode: os.FileMode(420), modTime: time.Unix(1570458335, 0)} + info := bindataFileInfo{name: "template/ent.tmpl", size: 4199, mode: os.FileMode(420), modTime: time.Unix(1570646976, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -814,7 +814,7 @@ func templateExampleTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/example.tmpl", size: 2425, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/example.tmpl", size: 2425, mode: os.FileMode(420), modTime: time.Unix(1567330554, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -834,7 +834,7 @@ func templateHeaderTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/header.tmpl", size: 436, mode: os.FileMode(420), modTime: time.Unix(1570458334, 0)} + info := bindataFileInfo{name: "template/header.tmpl", size: 436, mode: os.FileMode(420), modTime: time.Unix(1570451141, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -854,7 +854,7 @@ func templateImportTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/import.tmpl", size: 984, mode: os.FileMode(420), modTime: time.Unix(1568897494, 0)} + info := bindataFileInfo{name: "template/import.tmpl", size: 984, mode: os.FileMode(420), modTime: time.Unix(1568822311, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -874,7 +874,7 @@ func templateMetaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/meta.tmpl", size: 4291, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/meta.tmpl", size: 4291, mode: os.FileMode(420), modTime: time.Unix(1570033406, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -894,7 +894,7 @@ func templateMigrateMigrateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/migrate/migrate.tmpl", size: 2450, mode: os.FileMode(420), modTime: time.Unix(1568019515, 0)} + info := bindataFileInfo{name: "template/migrate/migrate.tmpl", size: 2450, mode: os.FileMode(420), modTime: time.Unix(1567952288, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -914,7 +914,7 @@ func templateMigrateSchemaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/migrate/schema.tmpl", size: 3755, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/migrate/schema.tmpl", size: 3755, mode: os.FileMode(420), modTime: time.Unix(1570019529, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -934,7 +934,7 @@ func templatePredicateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/predicate.tmpl", size: 1213, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/predicate.tmpl", size: 1213, mode: os.FileMode(420), modTime: time.Unix(1567330539, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -954,7 +954,7 @@ func templateTxTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/tx.tmpl", size: 3382, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/tx.tmpl", size: 3382, mode: os.FileMode(420), modTime: time.Unix(1567330536, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -974,7 +974,7 @@ func templateWhereTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/where.tmpl", size: 5353, mode: os.FileMode(420), modTime: time.Unix(1570346843, 0)} + info := bindataFileInfo{name: "template/where.tmpl", size: 5353, mode: os.FileMode(420), modTime: time.Unix(1570012859, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/dialect/sql/create.tmpl b/entc/gen/template/dialect/sql/create.tmpl index e6ba84a93..51e0973a0 100644 --- a/entc/gen/template/dialect/sql/create.tmpl +++ b/entc/gen/template/dialect/sql/create.tmpl @@ -17,7 +17,9 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name if err != nil { return nil, err } - builder := sql.Insert({{ $.Package }}.Table).Default({{ $receiver }}.driver.Dialect()) + builder := sql.Dialect({{ $receiver }}.driver.Dialect()). + Insert({{ $.Package }}.Table). + Default() {{- range $_, $f := $.Fields }} if value := {{ $receiver }}.{{- $f.StructField }}; value != nil { {{- if $f.IsJSON }} diff --git a/entc/integration/config/ent/user_create.go b/entc/integration/config/ent/user_create.go index 955060627..265461d13 100644 --- a/entc/integration/config/ent/user_create.go +++ b/entc/integration/config/ent/user_create.go @@ -41,7 +41,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() query, args := builder.Query() if err := tx.Exec(ctx, query, args, &res); err != nil { return nil, rollback(tx, err) diff --git a/entc/integration/ent/card_create.go b/entc/integration/ent/card_create.go index a15768efd..adb52d010 100644 --- a/entc/integration/ent/card_create.go +++ b/entc/integration/ent/card_create.go @@ -136,7 +136,9 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { if err != nil { return nil, err } - builder := sql.Insert(card.Table).Default(cc.driver.Dialect()) + builder := sql.Dialect(cc.driver.Dialect()). + Insert(card.Table). + Default() if value := cc.created_at; value != nil { builder.Set(card.FieldCreatedAt, *value) c.CreatedAt = *value diff --git a/entc/integration/ent/comment_create.go b/entc/integration/ent/comment_create.go index 330c3484e..344458e60 100644 --- a/entc/integration/ent/comment_create.go +++ b/entc/integration/ent/comment_create.go @@ -91,7 +91,9 @@ func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { if err != nil { return nil, err } - builder := sql.Insert(comment.Table).Default(cc.driver.Dialect()) + builder := sql.Dialect(cc.driver.Dialect()). + Insert(comment.Table). + Default() if value := cc.unique_int; value != nil { builder.Set(comment.FieldUniqueInt, *value) c.UniqueInt = *value diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index 65efb08f4..2da8cfbe2 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -295,7 +295,9 @@ func (ftc *FieldTypeCreate) sqlSave(ctx context.Context) (*FieldType, error) { if err != nil { return nil, err } - builder := sql.Insert(fieldtype.Table).Default(ftc.driver.Dialect()) + builder := sql.Dialect(ftc.driver.Dialect()). + Insert(fieldtype.Table). + Default() if value := ftc.int; value != nil { builder.Set(fieldtype.FieldInt, *value) ft.Int = *value diff --git a/entc/integration/ent/file_create.go b/entc/integration/ent/file_create.go index c87f51cad..793871b1f 100644 --- a/entc/integration/ent/file_create.go +++ b/entc/integration/ent/file_create.go @@ -171,7 +171,9 @@ func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) { if err != nil { return nil, err } - builder := sql.Insert(file.Table).Default(fc.driver.Dialect()) + builder := sql.Dialect(fc.driver.Dialect()). + Insert(file.Table). + Default() if value := fc.size; value != nil { builder.Set(file.FieldSize, *value) f.Size = *value diff --git a/entc/integration/ent/filetype_create.go b/entc/integration/ent/filetype_create.go index f40a22526..6cdf09347 100644 --- a/entc/integration/ent/filetype_create.go +++ b/entc/integration/ent/filetype_create.go @@ -89,7 +89,9 @@ func (ftc *FileTypeCreate) sqlSave(ctx context.Context) (*FileType, error) { if err != nil { return nil, err } - builder := sql.Insert(filetype.Table).Default(ftc.driver.Dialect()) + builder := sql.Dialect(ftc.driver.Dialect()). + Insert(filetype.Table). + Default() if value := ftc.name; value != nil { builder.Set(filetype.FieldName, *value) ft.Name = *value diff --git a/entc/integration/ent/group_create.go b/entc/integration/ent/group_create.go index 8c953dbf2..d4831cd8a 100644 --- a/entc/integration/ent/group_create.go +++ b/entc/integration/ent/group_create.go @@ -228,7 +228,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() if value := gc.active; value != nil { builder.Set(group.FieldActive, *value) gr.Active = *value diff --git a/entc/integration/ent/groupinfo_create.go b/entc/integration/ent/groupinfo_create.go index 04782044a..d9078ac22 100644 --- a/entc/integration/ent/groupinfo_create.go +++ b/entc/integration/ent/groupinfo_create.go @@ -108,7 +108,9 @@ func (gic *GroupInfoCreate) sqlSave(ctx context.Context) (*GroupInfo, error) { if err != nil { return nil, err } - builder := sql.Insert(groupinfo.Table).Default(gic.driver.Dialect()) + builder := sql.Dialect(gic.driver.Dialect()). + Insert(groupinfo.Table). + Default() if value := gic.desc; value != nil { builder.Set(groupinfo.FieldDesc, *value) gi.Desc = *value diff --git a/entc/integration/ent/item_create.go b/entc/integration/ent/item_create.go index 0bdd5684e..e85ea4aee 100644 --- a/entc/integration/ent/item_create.go +++ b/entc/integration/ent/item_create.go @@ -54,7 +54,9 @@ func (ic *ItemCreate) sqlSave(ctx context.Context) (*Item, error) { if err != nil { return nil, err } - builder := sql.Insert(item.Table).Default(ic.driver.Dialect()) + builder := sql.Dialect(ic.driver.Dialect()). + Insert(item.Table). + Default() query, args := builder.Query() if err := tx.Exec(ctx, query, args, &res); err != nil { return nil, rollback(tx, err) diff --git a/entc/integration/ent/node_create.go b/entc/integration/ent/node_create.go index 4d29a2330..042ad79b2 100644 --- a/entc/integration/ent/node_create.go +++ b/entc/integration/ent/node_create.go @@ -124,7 +124,9 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { if err != nil { return nil, err } - builder := sql.Insert(node.Table).Default(nc.driver.Dialect()) + builder := sql.Dialect(nc.driver.Dialect()). + Insert(node.Table). + Default() if value := nc.value; value != nil { builder.Set(node.FieldValue, *value) n.Value = *value diff --git a/entc/integration/ent/pet_create.go b/entc/integration/ent/pet_create.go index 0c7cda43c..db1ae62d9 100644 --- a/entc/integration/ent/pet_create.go +++ b/entc/integration/ent/pet_create.go @@ -120,7 +120,9 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err != nil { return nil, err } - builder := sql.Insert(pet.Table).Default(pc.driver.Dialect()) + builder := sql.Dialect(pc.driver.Dialect()). + Insert(pet.Table). + Default() if value := pc.name; value != nil { builder.Set(pet.FieldName, *value) pe.Name = *value diff --git a/entc/integration/ent/user_create.go b/entc/integration/ent/user_create.go index 730db4292..7de8e9800 100644 --- a/entc/integration/ent/user_create.go +++ b/entc/integration/ent/user_create.go @@ -395,7 +395,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/entc/integration/idtype/ent/user_create.go b/entc/integration/idtype/ent/user_create.go index 17726caa2..8361a0c64 100644 --- a/entc/integration/idtype/ent/user_create.go +++ b/entc/integration/idtype/ent/user_create.go @@ -121,7 +121,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.name; value != nil { builder.Set(user.FieldName, *value) u.Name = *value diff --git a/entc/integration/json/ent/user_create.go b/entc/integration/json/ent/user_create.go index 7d85a9660..b3943cbe8 100644 --- a/entc/integration/json/ent/user_create.go +++ b/entc/integration/json/ent/user_create.go @@ -86,7 +86,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.url; value != nil { buf, err := json.Marshal(*value) if err != nil { diff --git a/entc/integration/migrate/entv1/user_create.go b/entc/integration/migrate/entv1/user_create.go index 23af336f7..fd79b8a35 100644 --- a/entc/integration/migrate/entv1/user_create.go +++ b/entc/integration/migrate/entv1/user_create.go @@ -123,7 +123,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/entc/integration/migrate/entv2/group_create.go b/entc/integration/migrate/entv2/group_create.go index 2cee5faf8..ed9774b3b 100644 --- a/entc/integration/migrate/entv2/group_create.go +++ b/entc/integration/migrate/entv2/group_create.go @@ -41,7 +41,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() query, args := builder.Query() if err := tx.Exec(ctx, query, args, &res); err != nil { return nil, rollback(tx, err) diff --git a/entc/integration/migrate/entv2/pet_create.go b/entc/integration/migrate/entv2/pet_create.go index cdc9bc18d..0cfb5676d 100644 --- a/entc/integration/migrate/entv2/pet_create.go +++ b/entc/integration/migrate/entv2/pet_create.go @@ -41,7 +41,9 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err != nil { return nil, err } - builder := sql.Insert(pet.Table).Default(pc.driver.Dialect()) + builder := sql.Dialect(pc.driver.Dialect()). + Insert(pet.Table). + Default() query, args := builder.Query() if err := tx.Exec(ctx, query, args, &res); err != nil { return nil, rollback(tx, err) diff --git a/entc/integration/migrate/entv2/user_create.go b/entc/integration/migrate/entv2/user_create.go index e664092bb..4d86f56df 100644 --- a/entc/integration/migrate/entv2/user_create.go +++ b/entc/integration/migrate/entv2/user_create.go @@ -145,7 +145,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/entc/integration/template/ent/group_create.go b/entc/integration/template/ent/group_create.go index 9e71edeeb..a26eb1e14 100644 --- a/entc/integration/template/ent/group_create.go +++ b/entc/integration/template/ent/group_create.go @@ -52,7 +52,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() if value := gc.max_users; value != nil { builder.Set(group.FieldMaxUsers, *value) gr.MaxUsers = *value diff --git a/entc/integration/template/ent/pet_create.go b/entc/integration/template/ent/pet_create.go index 7a1fadabc..d728b4fbf 100644 --- a/entc/integration/template/ent/pet_create.go +++ b/entc/integration/template/ent/pet_create.go @@ -94,7 +94,9 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err != nil { return nil, err } - builder := sql.Insert(pet.Table).Default(pc.driver.Dialect()) + builder := sql.Dialect(pc.driver.Dialect()). + Insert(pet.Table). + Default() if value := pc.age; value != nil { builder.Set(pet.FieldAge, *value) pe.Age = *value diff --git a/entc/integration/template/ent/user_create.go b/entc/integration/template/ent/user_create.go index 7467172b8..706419f57 100644 --- a/entc/integration/template/ent/user_create.go +++ b/entc/integration/template/ent/user_create.go @@ -96,7 +96,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.name; value != nil { builder.Set(user.FieldName, *value) u.Name = *value diff --git a/entc/load/internal/bindata.go b/entc/load/internal/bindata.go index d789f1676..666f4914f 100644 --- a/entc/load/internal/bindata.go +++ b/entc/load/internal/bindata.go @@ -93,7 +93,7 @@ func templateMainTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/main.tmpl", size: 843, mode: os.FileMode(420), modTime: time.Unix(1567406598, 0)} + info := bindataFileInfo{name: "template/main.tmpl", size: 843, mode: os.FileMode(420), modTime: time.Unix(1567330508, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -113,7 +113,7 @@ func schemaGo() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "schema.go", size: 7340, mode: os.FileMode(420), modTime: time.Unix(1570458335, 0)} + info := bindataFileInfo{name: "schema.go", size: 7340, mode: os.FileMode(420), modTime: time.Unix(1570646976, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/examples/edgeindex/ent/city_create.go b/examples/edgeindex/ent/city_create.go index e2a3329a7..97a91fe99 100644 --- a/examples/edgeindex/ent/city_create.go +++ b/examples/edgeindex/ent/city_create.go @@ -75,7 +75,9 @@ func (cc *CityCreate) sqlSave(ctx context.Context) (*City, error) { if err != nil { return nil, err } - builder := sql.Insert(city.Table).Default(cc.driver.Dialect()) + builder := sql.Dialect(cc.driver.Dialect()). + Insert(city.Table). + Default() if value := cc.name; value != nil { builder.Set(city.FieldName, *value) c.Name = *value diff --git a/examples/edgeindex/ent/street_create.go b/examples/edgeindex/ent/street_create.go index 057337077..0fbb8cde7 100644 --- a/examples/edgeindex/ent/street_create.go +++ b/examples/edgeindex/ent/street_create.go @@ -78,7 +78,9 @@ func (sc *StreetCreate) sqlSave(ctx context.Context) (*Street, error) { if err != nil { return nil, err } - builder := sql.Insert(street.Table).Default(sc.driver.Dialect()) + builder := sql.Dialect(sc.driver.Dialect()). + Insert(street.Table). + Default() if value := sc.name; value != nil { builder.Set(street.FieldName, *value) s.Name = *value diff --git a/examples/entcpkg/ent/user_create.go b/examples/entcpkg/ent/user_create.go index 942c16807..4ff65fd5c 100644 --- a/examples/entcpkg/ent/user_create.go +++ b/examples/entcpkg/ent/user_create.go @@ -41,7 +41,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() query, args := builder.Query() if err := tx.Exec(ctx, query, args, &res); err != nil { return nil, rollback(tx, err) diff --git a/examples/m2m2types/ent/group_create.go b/examples/m2m2types/ent/group_create.go index 59e88bcb4..22bd6cf0b 100644 --- a/examples/m2m2types/ent/group_create.go +++ b/examples/m2m2types/ent/group_create.go @@ -73,7 +73,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() if value := gc.name; value != nil { builder.Set(group.FieldName, *value) gr.Name = *value diff --git a/examples/m2m2types/ent/user_create.go b/examples/m2m2types/ent/user_create.go index 5f04ca058..783cef300 100644 --- a/examples/m2m2types/ent/user_create.go +++ b/examples/m2m2types/ent/user_create.go @@ -83,7 +83,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/m2mbidi/ent/user_create.go b/examples/m2mbidi/ent/user_create.go index 9b1435ed3..cb128ffc6 100644 --- a/examples/m2mbidi/ent/user_create.go +++ b/examples/m2mbidi/ent/user_create.go @@ -83,7 +83,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/m2mrecur/ent/user_create.go b/examples/m2mrecur/ent/user_create.go index 5e67e0ac0..2e9d41c17 100644 --- a/examples/m2mrecur/ent/user_create.go +++ b/examples/m2mrecur/ent/user_create.go @@ -104,7 +104,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/o2m2types/ent/pet_create.go b/examples/o2m2types/ent/pet_create.go index 2b176bbe1..f31b75808 100644 --- a/examples/o2m2types/ent/pet_create.go +++ b/examples/o2m2types/ent/pet_create.go @@ -78,7 +78,9 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err != nil { return nil, err } - builder := sql.Insert(pet.Table).Default(pc.driver.Dialect()) + builder := sql.Dialect(pc.driver.Dialect()). + Insert(pet.Table). + Default() if value := pc.name; value != nil { builder.Set(pet.FieldName, *value) pe.Name = *value diff --git a/examples/o2m2types/ent/user_create.go b/examples/o2m2types/ent/user_create.go index d4a874443..bcab785c6 100644 --- a/examples/o2m2types/ent/user_create.go +++ b/examples/o2m2types/ent/user_create.go @@ -85,7 +85,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/o2mrecur/ent/node_create.go b/examples/o2mrecur/ent/node_create.go index a2ce06082..5d33c4db6 100644 --- a/examples/o2mrecur/ent/node_create.go +++ b/examples/o2mrecur/ent/node_create.go @@ -100,7 +100,9 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { if err != nil { return nil, err } - builder := sql.Insert(node.Table).Default(nc.driver.Dialect()) + builder := sql.Dialect(nc.driver.Dialect()). + Insert(node.Table). + Default() if value := nc.value; value != nil { builder.Set(node.FieldValue, *value) n.Value = *value diff --git a/examples/o2o2types/ent/card_create.go b/examples/o2o2types/ent/card_create.go index 76c7d218e..7f30e6551 100644 --- a/examples/o2o2types/ent/card_create.go +++ b/examples/o2o2types/ent/card_create.go @@ -85,7 +85,9 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { if err != nil { return nil, err } - builder := sql.Insert(card.Table).Default(cc.driver.Dialect()) + builder := sql.Dialect(cc.driver.Dialect()). + Insert(card.Table). + Default() if value := cc.expired; value != nil { builder.Set(card.FieldExpired, *value) c.Expired = *value diff --git a/examples/o2o2types/ent/user_create.go b/examples/o2o2types/ent/user_create.go index a73a7bb8b..b7cc21542 100644 --- a/examples/o2o2types/ent/user_create.go +++ b/examples/o2o2types/ent/user_create.go @@ -90,7 +90,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/o2obidi/ent/user_create.go b/examples/o2obidi/ent/user_create.go index 2fab28fd9..223b5e9b0 100644 --- a/examples/o2obidi/ent/user_create.go +++ b/examples/o2obidi/ent/user_create.go @@ -89,7 +89,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/o2orecur/ent/node_create.go b/examples/o2orecur/ent/node_create.go index da9f3a06b..3c2f5b5ca 100644 --- a/examples/o2orecur/ent/node_create.go +++ b/examples/o2orecur/ent/node_create.go @@ -105,7 +105,9 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { if err != nil { return nil, err } - builder := sql.Insert(node.Table).Default(nc.driver.Dialect()) + builder := sql.Dialect(nc.driver.Dialect()). + Insert(node.Table). + Default() if value := nc.value; value != nil { builder.Set(node.FieldValue, *value) n.Value = *value diff --git a/examples/start/ent/car_create.go b/examples/start/ent/car_create.go index 1ace0daea..a35334f0c 100644 --- a/examples/start/ent/car_create.go +++ b/examples/start/ent/car_create.go @@ -89,7 +89,9 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { if err != nil { return nil, err } - builder := sql.Insert(car.Table).Default(cc.driver.Dialect()) + builder := sql.Dialect(cc.driver.Dialect()). + Insert(car.Table). + Default() if value := cc.model; value != nil { builder.Set(car.FieldModel, *value) c.Model = *value diff --git a/examples/start/ent/group_create.go b/examples/start/ent/group_create.go index 3a598599b..82eb043c7 100644 --- a/examples/start/ent/group_create.go +++ b/examples/start/ent/group_create.go @@ -77,7 +77,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() if value := gc.name; value != nil { builder.Set(group.FieldName, *value) gr.Name = *value diff --git a/examples/start/ent/user_create.go b/examples/start/ent/user_create.go index e497c5ba3..6f4ce39d8 100644 --- a/examples/start/ent/user_create.go +++ b/examples/start/ent/user_create.go @@ -118,7 +118,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value diff --git a/examples/traversal/ent/group_create.go b/examples/traversal/ent/group_create.go index 8b8647272..ea573b9f9 100644 --- a/examples/traversal/ent/group_create.go +++ b/examples/traversal/ent/group_create.go @@ -99,7 +99,9 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { if err != nil { return nil, err } - builder := sql.Insert(group.Table).Default(gc.driver.Dialect()) + builder := sql.Dialect(gc.driver.Dialect()). + Insert(group.Table). + Default() if value := gc.name; value != nil { builder.Set(group.FieldName, *value) gr.Name = *value diff --git a/examples/traversal/ent/pet_create.go b/examples/traversal/ent/pet_create.go index 3292592a9..8e2d93b04 100644 --- a/examples/traversal/ent/pet_create.go +++ b/examples/traversal/ent/pet_create.go @@ -99,7 +99,9 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err != nil { return nil, err } - builder := sql.Insert(pet.Table).Default(pc.driver.Dialect()) + builder := sql.Dialect(pc.driver.Dialect()). + Insert(pet.Table). + Default() if value := pc.name; value != nil { builder.Set(pet.FieldName, *value) pe.Name = *value diff --git a/examples/traversal/ent/user_create.go b/examples/traversal/ent/user_create.go index cb2a2cdd4..3f2c9935a 100644 --- a/examples/traversal/ent/user_create.go +++ b/examples/traversal/ent/user_create.go @@ -149,7 +149,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err != nil { return nil, err } - builder := sql.Insert(user.Table).Default(uc.driver.Dialect()) + builder := sql.Dialect(uc.driver.Dialect()). + Insert(user.Table). + Default() if value := uc.age; value != nil { builder.Set(user.FieldAge, *value) u.Age = *value