dialect/sql/schema: add postgres table and column builders

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/114

Reviewed By: alexsn

Differential Revision: D18059082

fbshipit-source-id: fffec25143fa5f2a0ed2c79787abacefad724862
This commit is contained in:
Ariel Mashraki
2019-10-22 08:15:21 -07:00
committed by Facebook Github Bot
parent 67c1d5262a
commit 8be31d18f9
2 changed files with 37 additions and 0 deletions

View File

@@ -227,6 +227,19 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
}
}
// tBuilder returns the TableBuilder for the given table.
func (d *Postgres) tBuilder(t *Table) *sql.TableBuilder {
b := sql.Dialect(dialect.Postgres).
CreateTable(t.Name).IfNotExists()
for _, c := range t.Columns {
b.Column(d.cBuilder(c))
}
for _, pk := range t.PrimaryKey {
b.PrimaryKey(pk.Name)
}
return b
}
// cType returns the PostgreSQL string type for this column.
func (d *Postgres) cType(c *Column) (t string) {
switch c.Type {
@@ -262,3 +275,16 @@ func (d *Postgres) cType(c *Column) (t string) {
}
return t
}
// cBuilder returns the ColumnBuilder for the given column.
func (d *Postgres) cBuilder(c *Column) *sql.ColumnBuilder {
b := sql.Dialect(dialect.Postgres).
Column(c.Name).Type(d.cType(c)).Attr(c.Attr)
c.unique(b)
if c.Increment {
b.Attr("GENERATED BY DEFAULT AS IDENTITY")
}
c.nullable(b)
c.defaultValue(b)
return b
}