Skip to content

Commit

Permalink
Pretty print only CREATE TABLE (#234)
Browse files Browse the repository at this point in the history
* Pretty printing only CREATE TABLE

* Update testdata

* Update testdata
  • Loading branch information
apstndb authored Dec 19, 2024
1 parent 1048614 commit 42e487c
Show file tree
Hide file tree
Showing 39 changed files with 969 additions and 44 deletions.
22 changes: 15 additions & 7 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
"github.com/cloudspannerecosystem/memefish/token"
)

// indentLevel is the whitespace indentation level.
// Currently, memefish does not perform pretty printing in general, it is only used by CreateTable.
const indentLevel = 4

// indent is a whitespace indentation of indentLevel width.
// You should use this rather than using indentLevel directly.
var indent = strings.Repeat(" ", indentLevel)

// ================================================================================
//
// Helper functions for SQL()
Expand Down Expand Up @@ -748,11 +756,11 @@ func (d *DropProtoBundle) SQL() string { return "DROP PROTO BUNDLE" }
func (c *CreateTable) SQL() string {
return "CREATE TABLE " +
strOpt(c.IfNotExists, "IF NOT EXISTS ") +
c.Name.SQL() + " (" +
sqlJoin(c.Columns, ", ") + strOpt(len(c.Columns) > 0 && (len(c.TableConstraints) > 0 || len(c.Synonyms) > 0), ", ") +
sqlJoin(c.TableConstraints, ", ") + strOpt(len(c.TableConstraints) > 0 && len(c.Synonyms) > 0, ", ") +
sqlJoin(c.Synonyms, ", ") +
") PRIMARY KEY (" + sqlJoin(c.PrimaryKeys, ", ") + ")" +
c.Name.SQL() + " (\n" +
indent + sqlJoin(c.Columns, ",\n"+indent) + strOpt(len(c.Columns) > 0 && (len(c.TableConstraints) > 0 || len(c.Synonyms) > 0), ",\n") +
strOpt(len(c.TableConstraints) > 0, indent) + sqlJoin(c.TableConstraints, ",\n"+indent) + strOpt(len(c.TableConstraints) > 0 && len(c.Synonyms) > 0, ",\n") +
strOpt(len(c.Synonyms) > 0, indent) + sqlJoin(c.Synonyms, ",\n") +
"\n) PRIMARY KEY (" + sqlJoin(c.PrimaryKeys, ", ") + ")" +
sqlOpt("", c.Cluster, "") +
sqlOpt("", c.RowDeletionPolicy, "")
}
Expand Down Expand Up @@ -812,12 +820,12 @@ func (i *IndexKey) SQL() string {
}

func (c *Cluster) SQL() string {
return ", INTERLEAVE IN PARENT " + c.TableName.SQL() +
return ",\nINTERLEAVE IN PARENT " + c.TableName.SQL() +
strOpt(c.OnDelete != "", " "+string(c.OnDelete))
}

func (c *CreateRowDeletionPolicy) SQL() string {
return ", " + c.RowDeletionPolicy.SQL()
return ",\n" + c.RowDeletionPolicy.SQL()
}

func (r *RowDeletionPolicy) SQL() string {
Expand Down
18 changes: 15 additions & 3 deletions parse_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package memefish_test

import (
"fmt"

"github.com/MakeNowJust/heredoc/v2"

"github.com/cloudspannerecosystem/memefish"
)

Expand Down Expand Up @@ -84,7 +86,10 @@ func ExampleParseDDL() {
fmt.Println(ddl.SQL())

// Output:
// CREATE TABLE foo (x INT64, y INT64) PRIMARY KEY (x)
// CREATE TABLE foo (
// x INT64,
// y INT64
// ) PRIMARY KEY (x)
}

func ExampleParseDDLs() {
Expand All @@ -108,8 +113,15 @@ func ExampleParseDDLs() {
}

// Output:
// CREATE TABLE foo (x INT64, y INT64) PRIMARY KEY (x);
// CREATE TABLE bar (x INT64, z INT64) PRIMARY KEY (x, z), INTERLEAVE IN PARENT foo;
// CREATE TABLE foo (
// x INT64,
// y INT64
// ) PRIMARY KEY (x);
// CREATE TABLE bar (
// x INT64,
// z INT64
// ) PRIMARY KEY (x, z),
// INTERLEAVE IN PARENT foo;
}

func ExampleParseDML() {
Expand Down
13 changes: 13 additions & 0 deletions testdata/input/ddl/create_table_for_format_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
create table if not exists foo (
foo int64,
bar float64 not null,
baz string(255) not null options(allow_commit_timestamp = null),
qux string(255) not null as (concat(baz, "a")) stored,
foreign key (foo) references t2 (t2key1),
constraint fkname foreign key (foo, bar) references t2 (t2key1, t2key2),
check (foo > 0),
constraint cname check (bar > 0),
corge timestamp not null default (current_timestamp())
) primary key (foo),
interleave in parent foobar,
row deletion policy ( older_than ( baz, INTERVAL 30 DAY ) )
15 changes: 14 additions & 1 deletion testdata/result/ddl/create_table.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,17 @@ create table foo (
}

--- SQL
CREATE TABLE foo (foo INT64, bar FLOAT64 NOT NULL, baz STRING(255) NOT NULL OPTIONS (allow_commit_timestamp = null), qux STRING(255) NOT NULL AS (concat(baz, "a")) STORED, quux JSON, corge TIMESTAMP NOT NULL DEFAULT (current_timestamp()), FOREIGN KEY (foo) REFERENCES t2 (t2key1), FOREIGN KEY (bar) REFERENCES t2 (t2key2) ON DELETE CASCADE, FOREIGN KEY (baz) REFERENCES t2 (t2key3) ON DELETE NO ACTION, CONSTRAINT fkname FOREIGN KEY (foo, bar) REFERENCES t2 (t2key1, t2key2), CHECK (foo > 0), CONSTRAINT cname CHECK (bar > 0)) PRIMARY KEY (foo, bar)
CREATE TABLE foo (
foo INT64,
bar FLOAT64 NOT NULL,
baz STRING(255) NOT NULL OPTIONS (allow_commit_timestamp = null),
qux STRING(255) NOT NULL AS (concat(baz, "a")) STORED,
quux JSON,
corge TIMESTAMP NOT NULL DEFAULT (current_timestamp()),
FOREIGN KEY (foo) REFERENCES t2 (t2key1),
FOREIGN KEY (bar) REFERENCES t2 (t2key2) ON DELETE CASCADE,
FOREIGN KEY (baz) REFERENCES t2 (t2key3) ON DELETE NO ACTION,
CONSTRAINT fkname FOREIGN KEY (foo, bar) REFERENCES t2 (t2key1, t2key2),
CHECK (foo > 0),
CONSTRAINT cname CHECK (bar > 0)
) PRIMARY KEY (foo, bar)
6 changes: 5 additions & 1 deletion testdata/result/ddl/create_table_cluster.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ create table foo (
}

--- SQL
CREATE TABLE foo (foo INT64, bar INT64) PRIMARY KEY (), INTERLEAVE IN PARENT foobar
CREATE TABLE foo (
foo INT64,
bar INT64
) PRIMARY KEY (),
INTERLEAVE IN PARENT foobar
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ create table foo (
}

--- SQL
CREATE TABLE foo (foo INT64, bar INT64, baz TIMESTAMP) PRIMARY KEY (), INTERLEAVE IN PARENT foobar, ROW DELETION POLICY ( OLDER_THAN ( baz, INTERVAL 30 DAY ))
CREATE TABLE foo (
foo INT64,
bar INT64,
baz TIMESTAMP
) PRIMARY KEY (),
INTERLEAVE IN PARENT foobar,
ROW DELETION POLICY ( OLDER_THAN ( baz, INTERVAL 30 DAY ))
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ create table foo (
}

--- SQL
CREATE TABLE foo (foo INT64) PRIMARY KEY (foo), INTERLEAVE IN PARENT foobar ON DELETE NO ACTION
CREATE TABLE foo (
foo INT64
) PRIMARY KEY (foo),
INTERLEAVE IN PARENT foobar ON DELETE NO ACTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ create table foo (
}

--- SQL
CREATE TABLE foo (foo INT64) PRIMARY KEY (foo), INTERLEAVE IN PARENT foobar ON DELETE CASCADE
CREATE TABLE foo (
foo INT64
) PRIMARY KEY (foo),
INTERLEAVE IN PARENT foobar ON DELETE CASCADE
Loading

0 comments on commit 42e487c

Please sign in to comment.