Skip to content

Commit fe5d5c3

Browse files
committed
fmt
1 parent f566907 commit fe5d5c3

File tree

14 files changed

+144
-39
lines changed

14 files changed

+144
-39
lines changed

internal/endtoend/testdata/join_where_clause/postgresql/pgx/v5/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ WHERE owner = $1;
1414
SELECT foo.*
1515
FROM foo
1616
CROSS JOIN bar
17-
WHERE bar.id = $2 AND owner = $1;
17+
WHERE bar.id = $2 AND owner = $1;

internal/endtoend/testdata/materialized_views/postgresql/pgx/v4/go/query.sql.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
1-
CREATE TABLE authors (
2-
id BIGSERIAL PRIMARY KEY,
3-
name TEXT NOT NULL,
4-
bio TEXT
5-
);
6-
7-
ALTER TABLE authors ADD COLUMN gender INTEGER NULL;
8-
9-
CREATE MATERIALIZED VIEW authors_names as SELECT name from authors;
10-
1+
-- name: ListAuthors :many
112
SELECT * FROM authors;

internal/endtoend/testdata/materialized_views/postgresql/pgx/v5/go/query.sql.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
1-
CREATE TABLE authors (
2-
id BIGSERIAL PRIMARY KEY,
3-
name TEXT NOT NULL,
4-
bio TEXT
5-
);
6-
7-
ALTER TABLE authors ADD COLUMN gender INTEGER NULL;
8-
9-
CREATE MATERIALIZED VIEW authors_names as SELECT name from authors;
10-
1+
-- name: ListAuthors :many
112
SELECT * FROM authors;
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
1-
CREATE TABLE authors (
2-
id BIGSERIAL PRIMARY KEY,
3-
name TEXT NOT NULL,
4-
bio TEXT
5-
);
6-
7-
ALTER TABLE authors ADD COLUMN gender INTEGER NULL;
8-
9-
CREATE MATERIALIZED VIEW authors_names as SELECT name from authors;
10-
1+
-- name: ListAuthors :many
112
SELECT * FROM authors;

internal/engine/postgresql/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,6 +3643,7 @@ func convertNode(node *pg.Node) ast.Node {
36433643
return convertXmlSerialize(n.XmlSerialize)
36443644

36453645
default:
3646+
fmt.Printf("todo: %T\n", n)
36463647
return &ast.TODO{}
36473648
}
36483649
}

internal/engine/postgresql/parse.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,21 @@ func translate(node *nodes.Node) (ast.Node, error) {
438438
if err != nil {
439439
return nil, err
440440
}
441+
442+
primary := false
443+
for _, con := range item.ColumnDef.Constraints {
444+
if constraint, ok := con.Node.(*nodes.Node_Constraint); ok {
445+
primary = constraint.Constraint.Contype == nodes.ConstrType_CONSTR_PRIMARY
446+
}
447+
}
448+
441449
create.Cols = append(create.Cols, &ast.ColumnDef{
442-
Colname: item.ColumnDef.Colname,
443-
TypeName: rel.TypeName(),
444-
IsNotNull: isNotNull(item.ColumnDef) || primaryKey[item.ColumnDef.Colname],
445-
IsArray: isArray(item.ColumnDef.TypeName),
446-
ArrayDims: len(item.ColumnDef.TypeName.ArrayBounds),
450+
Colname: item.ColumnDef.Colname,
451+
TypeName: rel.TypeName(),
452+
IsNotNull: isNotNull(item.ColumnDef) || primaryKey[item.ColumnDef.Colname],
453+
IsArray: isArray(item.ColumnDef.TypeName),
454+
ArrayDims: len(item.ColumnDef.TypeName.ArrayBounds),
455+
PrimaryKey: primary,
447456
})
448457
}
449458
}

internal/sql/ast/alter_table_cmd.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ type AlterTableCmd struct {
3939
func (n *AlterTableCmd) Pos() int {
4040
return 0
4141
}
42+
43+
func (n *AlterTableCmd) Format(buf *TrackedBuffer) {
44+
if n == nil {
45+
return
46+
}
47+
switch n.Subtype {
48+
case AT_AddColumn:
49+
buf.WriteString(" ADD COLUMN ")
50+
case AT_DropColumn:
51+
buf.WriteString(" DROP COLUMN ")
52+
}
53+
54+
buf.astFormat(n.Def)
55+
}

internal/sql/ast/alter_table_stmt.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ type AlterTableStmt struct {
1212
func (n *AlterTableStmt) Pos() int {
1313
return 0
1414
}
15+
16+
func (n *AlterTableStmt) Format(buf *TrackedBuffer) {
17+
if n == nil {
18+
return
19+
}
20+
buf.WriteString("ALTER TABLE ")
21+
buf.astFormat(n.Relation)
22+
buf.astFormat(n.Table)
23+
buf.astFormat(n.Cmds)
24+
}

internal/sql/ast/column_def.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ColumnDef struct {
99
ArrayDims int
1010
Vals *List
1111
Length *int
12+
PrimaryKey bool
1213

1314
// From pg.ColumnDef
1415
Inhcount int
@@ -38,7 +39,10 @@ func (n *ColumnDef) Format(buf *TrackedBuffer) {
3839
buf.WriteString(n.Colname)
3940
buf.WriteString(" ")
4041
buf.astFormat(n.TypeName)
41-
if n.IsNotNull {
42+
if n.PrimaryKey {
43+
buf.WriteString(" PRIMARY KEY")
44+
} else if n.IsNotNull {
4245
buf.WriteString(" NOT NULL")
4346
}
47+
buf.astFormat(n.Constraints)
4448
}

internal/sql/ast/insert_stmt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func (n *InsertStmt) Format(buf *TrackedBuffer) {
3838
buf.astFormat(n.SelectStmt)
3939
}
4040

41+
if n.OnConflictClause != nil {
42+
buf.WriteString(" ON CONFLICT DO NOTHING ")
43+
}
44+
4145
if items(n.ReturningList) {
4246
buf.WriteString(" RETURNING ")
4347
buf.astFormat(n.ReturningList)

internal/sql/ast/join_expr.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@ func (n *JoinExpr) Format(buf *TrackedBuffer) {
3030
}
3131
buf.astFormat(n.Rarg)
3232
buf.WriteString(" ON ")
33-
buf.astFormat(n.Quals)
33+
if n.Jointype == JoinTypeInner {
34+
if set(n.Quals) {
35+
buf.astFormat(n.Quals)
36+
} else {
37+
buf.WriteString("TRUE")
38+
}
39+
} else {
40+
buf.astFormat(n.Quals)
41+
}
3442
}

internal/sql/ast/type_name.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func (n *TypeName) Format(buf *TrackedBuffer) {
2727
if items(n.Names) {
2828
buf.join(n.Names, ".")
2929
} else {
30-
buf.WriteString(n.Name)
30+
if n.Name == "int4" {
31+
buf.WriteString("INTEGER")
32+
} else {
33+
buf.WriteString(n.Name)
34+
}
3135
}
3236
if items(n.ArrayBounds) {
3337
buf.WriteString("[]")

0 commit comments

Comments
 (0)