mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/schema: add multi schema and view support for schema dump (#4335)
This commit is contained in:
@@ -240,6 +240,80 @@ func (t *TableBuilder) Query() (string, []any) {
|
||||
return t.String(), t.args
|
||||
}
|
||||
|
||||
// ViewBuilder is a query builder for `CREATE VIEW` statement.
|
||||
type ViewBuilder struct {
|
||||
Builder
|
||||
schema string // view schema.
|
||||
name string // view name.
|
||||
exists bool // check existence.
|
||||
columns []Querier // table columns.
|
||||
as Querier // view query.
|
||||
}
|
||||
|
||||
// CreateView returns a query builder for the `CREATE VIEW` statement.
|
||||
//
|
||||
// t := Table("users")
|
||||
// CreateView("clean_users").
|
||||
// Columns(
|
||||
// Column("id").Type("int").Attr("auto_increment"),
|
||||
// Column("name").Type("varchar(255)"),
|
||||
// ).
|
||||
// As(Select(t.C("id"), t.C("name")).From(t))
|
||||
func CreateView(name string) *ViewBuilder { return &ViewBuilder{name: name} }
|
||||
|
||||
// Schema sets the database name for the view.
|
||||
func (v *ViewBuilder) Schema(name string) *ViewBuilder {
|
||||
v.schema = name
|
||||
return v
|
||||
}
|
||||
|
||||
// IfNotExists appends the `IF NOT EXISTS` clause to the `CREATE VIEW` statement.
|
||||
func (v *ViewBuilder) IfNotExists() *ViewBuilder {
|
||||
v.exists = true
|
||||
return v
|
||||
}
|
||||
|
||||
// Column appends the given column to the `CREATE VIEW` statement.
|
||||
func (v *ViewBuilder) Column(c *ColumnBuilder) *ViewBuilder {
|
||||
v.columns = append(v.columns, c)
|
||||
return v
|
||||
}
|
||||
|
||||
// Columns appends a list of columns to the builder.
|
||||
func (v *ViewBuilder) Columns(columns ...*ColumnBuilder) *ViewBuilder {
|
||||
v.columns = make([]Querier, 0, len(columns))
|
||||
for i := range columns {
|
||||
v.columns = append(v.columns, columns[i])
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// As sets the view definition to the builder.
|
||||
func (v *ViewBuilder) As(as Querier) *ViewBuilder {
|
||||
v.as = as
|
||||
return v
|
||||
}
|
||||
|
||||
// Query returns query representation of a `CREATE VIEW` statement.
|
||||
//
|
||||
// CREATE VIEW [IF NOT EXISTS] name AS
|
||||
//
|
||||
// (view definition)
|
||||
func (v *ViewBuilder) Query() (string, []any) {
|
||||
v.WriteString("CREATE VIEW ")
|
||||
if v.exists {
|
||||
v.WriteString("IF NOT EXISTS ")
|
||||
}
|
||||
v.writeSchema(v.schema)
|
||||
v.Ident(v.name)
|
||||
if len(v.columns) > 0 {
|
||||
v.Pad().Wrap(func(b *Builder) { b.JoinComma(v.columns...) })
|
||||
}
|
||||
v.WriteString(" AS ")
|
||||
v.Join(v.as)
|
||||
return v.String(), v.args
|
||||
}
|
||||
|
||||
// DescribeBuilder is a query builder for `DESCRIBE` statement.
|
||||
type DescribeBuilder struct {
|
||||
Builder
|
||||
@@ -3884,7 +3958,7 @@ func (b *Builder) isQualified(s string) bool {
|
||||
ident && !pg && strings.Contains(s, "`.`") // `qualifier`.`column`
|
||||
}
|
||||
|
||||
// state wraps the all methods for setting and getting
|
||||
// state wraps all methods for setting and getting
|
||||
// update state between all queries in the query tree.
|
||||
type state interface {
|
||||
Dialect() string
|
||||
@@ -3930,17 +4004,33 @@ func (d *DialectBuilder) Describe(name string) *DescribeBuilder {
|
||||
//
|
||||
// Dialect(dialect.Postgres).
|
||||
// CreateTable("users").
|
||||
// Columns(
|
||||
// Column("id").Type("int").Attr("auto_increment"),
|
||||
// Column("name").Type("varchar(255)"),
|
||||
// ).
|
||||
// PrimaryKey("id")
|
||||
// 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
|
||||
}
|
||||
|
||||
// CreateView creates a ViewBuilder for the configured dialect.
|
||||
//
|
||||
// t := Table("users")
|
||||
// Dialect(dialect.Postgres).
|
||||
// CreateView("users").
|
||||
// Columns(
|
||||
// Column("id").Type("int"),
|
||||
// Column("name").Type("varchar(255)"),
|
||||
// ).
|
||||
// As(Select(t.C("id"), t.C("name")).From(t))
|
||||
func (d *DialectBuilder) CreateView(name string) *ViewBuilder {
|
||||
b := CreateView(name)
|
||||
b.SetDialect(d.dialect)
|
||||
return b
|
||||
}
|
||||
|
||||
// AlterTable creates a TableAlter for the configured dialect.
|
||||
//
|
||||
// Dialect(dialect.Postgres).
|
||||
|
||||
Reference in New Issue
Block a user