diff --git a/util/sqlgen/column.go b/util/sqlgen/column.go index af8139f301725a7778746c9e923324e2405c4209..4b3f972c2ff57c1e074d70a75dae2222ecae9774 100644 --- a/util/sqlgen/column.go +++ b/util/sqlgen/column.go @@ -22,6 +22,10 @@ func (self Column) Compile(layout *Template) string { chunks := reAliasSeparator.Split(input, 2) + if len(chunks) == 1 { + chunks = reSpaceSeparator.Split(input, 2) + } + name := chunks[0] nameChunks := strings.SplitN(name, layout.ColumnSeparator, 2) diff --git a/util/sqlgen/column_test.go b/util/sqlgen/column_test.go index 8420dc8812b421746bdbc55a1f902a0773d48871..62f3929def3ffcc912e5312bab6affcc4ad5d1c3 100644 --- a/util/sqlgen/column_test.go +++ b/util/sqlgen/column_test.go @@ -30,6 +30,19 @@ func TestColumnAs(t *testing.T) { } } +func TestColumnImplicitAs(t *testing.T) { + var s, e string + + column := Column{"role.name foo"} + + s = column.Compile(defaultTemplate) + e = `"role"."name" AS "foo"` + + if s != e { + t.Fatalf("Got: %s, Expecting: %s", s, e) + } +} + func TestColumnRaw(t *testing.T) { var s, e string diff --git a/util/sqlgen/main_test.go b/util/sqlgen/main_test.go index 7ebfb8fb479669e0c2a538563beb78d52df3f8b0..0cfe5c80ca9e0a2f43afc5c6c999f10f722d6c00 100644 --- a/util/sqlgen/main_test.go +++ b/util/sqlgen/main_test.go @@ -10,11 +10,11 @@ func TestTruncateTable(t *testing.T) { stmt = Statement{ Type: SqlTruncate, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `TRUNCATE TABLE "table name"` + e = `TRUNCATE TABLE "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -27,11 +27,11 @@ func TestDropTable(t *testing.T) { stmt = Statement{ Type: SqlDropTable, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `DROP TABLE "table name"` + e = `DROP TABLE "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -44,11 +44,11 @@ func TestDropDatabase(t *testing.T) { stmt = Statement{ Type: SqlDropDatabase, - Database: Database{"table name"}, + Database: Database{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `DROP DATABASE "table name"` + e = `DROP DATABASE "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -61,11 +61,11 @@ func TestSelectCount(t *testing.T) { stmt = Statement{ Type: SqlSelectCount, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT COUNT(1) AS _t FROM "table name"` + e = `SELECT COUNT(1) AS _t FROM "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -95,14 +95,14 @@ func TestSelectCountWhere(t *testing.T) { stmt = Statement{ Type: SqlSelectCount, - Table: Table{"table name"}, + Table: Table{"table_name"}, Where: Where{ ColumnValue{Column{"a"}, "=", Value{Raw{"7"}}}, }, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT COUNT(1) AS _t FROM "table name" WHERE ("a" = 7)` + e = `SELECT COUNT(1) AS _t FROM "table_name" WHERE ("a" = 7)` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -115,11 +115,11 @@ func TestSelectStarFrom(t *testing.T) { stmt = Statement{ Type: SqlSelect, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT * FROM "table name"` + e = `SELECT * FROM "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -248,11 +248,11 @@ func TestSelectFieldsFrom(t *testing.T) { {"bar"}, {"baz"}, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name"` + e = `SELECT "foo", "bar", "baz" FROM "table_name"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -272,11 +272,11 @@ func TestSelectFieldsFromWithLimitOffset(t *testing.T) { {"baz"}, }, Limit: 42, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" LIMIT 42` + e = `SELECT "foo", "bar", "baz" FROM "table_name" LIMIT 42` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -291,11 +291,11 @@ func TestSelectFieldsFromWithLimitOffset(t *testing.T) { {"baz"}, }, Offset: 17, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" OFFSET 17` + e = `SELECT "foo", "bar", "baz" FROM "table_name" OFFSET 17` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -311,11 +311,11 @@ func TestSelectFieldsFromWithLimitOffset(t *testing.T) { }, Limit: 42, Offset: 17, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" LIMIT 42 OFFSET 17` + e = `SELECT "foo", "bar", "baz" FROM "table_name" LIMIT 42 OFFSET 17` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -339,11 +339,11 @@ func TestSelectFieldsFromWithOrderBy(t *testing.T) { SortColumn{Column: Column{"foo"}}, }, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" ORDER BY "foo"` + e = `SELECT "foo", "bar", "baz" FROM "table_name" ORDER BY "foo"` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -362,11 +362,11 @@ func TestSelectFieldsFromWithOrderBy(t *testing.T) { SortColumn{Column{"foo"}, SqlSortAsc}, }, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" ORDER BY "foo" ASC` + e = `SELECT "foo", "bar", "baz" FROM "table_name" ORDER BY "foo" ASC` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -385,11 +385,11 @@ func TestSelectFieldsFromWithOrderBy(t *testing.T) { {Column{"foo"}, SqlSortDesc}, }, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" ORDER BY "foo" DESC` + e = `SELECT "foo", "bar", "baz" FROM "table_name" ORDER BY "foo" DESC` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -410,11 +410,11 @@ func TestSelectFieldsFromWithOrderBy(t *testing.T) { {Column{"baz"}, SqlSortDesc}, }, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" ORDER BY "foo" DESC, "bar" ASC, "baz" DESC` + e = `SELECT "foo", "bar", "baz" FROM "table_name" ORDER BY "foo" DESC, "bar" ASC, "baz" DESC` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -432,14 +432,14 @@ func TestSelectFieldsFromWhere(t *testing.T) { {"bar"}, {"baz"}, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, Where: Where{ ColumnValue{Column{"baz"}, "=", Value{99}}, }, } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = '99')` + e = `SELECT "foo", "bar", "baz" FROM "table_name" WHERE ("baz" = '99')` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -457,7 +457,7 @@ func TestSelectFieldsFromWhereLimitOffset(t *testing.T) { {"bar"}, {"baz"}, }, - Table: Table{"table name"}, + Table: Table{"table_name"}, Where: Where{ ColumnValue{Column{"baz"}, "=", Value{99}}, }, @@ -466,7 +466,7 @@ func TestSelectFieldsFromWhereLimitOffset(t *testing.T) { } s = trim(stmt.Compile(defaultTemplate)) - e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = '99') LIMIT 10 OFFSET 23` + e = `SELECT "foo", "bar", "baz" FROM "table_name" WHERE ("baz" = '99') LIMIT 10 OFFSET 23` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -479,14 +479,14 @@ func TestDelete(t *testing.T) { stmt = Statement{ Type: SqlDelete, - Table: Table{"table name"}, + Table: Table{"table_name"}, Where: Where{ ColumnValue{Column{"baz"}, "=", Value{99}}, }, } s = trim(stmt.Compile(defaultTemplate)) - e = `DELETE FROM "table name" WHERE ("baz" = '99')` + e = `DELETE FROM "table_name" WHERE ("baz" = '99')` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -499,7 +499,7 @@ func TestUpdate(t *testing.T) { stmt = Statement{ Type: SqlUpdate, - Table: Table{"table name"}, + Table: Table{"table_name"}, ColumnValues: ColumnValues{ {Column{"foo"}, "=", Value{76}}, }, @@ -509,7 +509,7 @@ func TestUpdate(t *testing.T) { } s = trim(stmt.Compile(defaultTemplate)) - e = `UPDATE "table name" SET "foo" = '76' WHERE ("baz" = '99')` + e = `UPDATE "table_name" SET "foo" = '76' WHERE ("baz" = '99')` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -517,7 +517,7 @@ func TestUpdate(t *testing.T) { stmt = Statement{ Type: SqlUpdate, - Table: Table{"table name"}, + Table: Table{"table_name"}, ColumnValues: ColumnValues{ {Column{"foo"}, "=", Value{76}}, {Column{"bar"}, "=", Value{Raw{"88"}}}, @@ -528,7 +528,7 @@ func TestUpdate(t *testing.T) { } s = trim(stmt.Compile(defaultTemplate)) - e = `UPDATE "table name" SET "foo" = '76', "bar" = 88 WHERE ("baz" = '99')` + e = `UPDATE "table_name" SET "foo" = '76', "bar" = 88 WHERE ("baz" = '99')` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -541,7 +541,7 @@ func TestInsert(t *testing.T) { stmt = Statement{ Type: SqlInsert, - Table: Table{"table name"}, + Table: Table{"table_name"}, Columns: Columns{ Column{"foo"}, Column{"bar"}, @@ -555,7 +555,7 @@ func TestInsert(t *testing.T) { } s = trim(stmt.Compile(defaultTemplate)) - e = `INSERT INTO "table name" ("foo", "bar", "baz") VALUES ('1', '2', 3)` + e = `INSERT INTO "table_name" ("foo", "bar", "baz") VALUES ('1', '2', 3)` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) @@ -568,7 +568,7 @@ func TestInsertExtra(t *testing.T) { stmt = Statement{ Type: SqlInsert, - Table: Table{"table name"}, + Table: Table{"table_name"}, Extra: "RETURNING id", Columns: Columns{ Column{"foo"}, @@ -583,7 +583,7 @@ func TestInsertExtra(t *testing.T) { } s = trim(stmt.Compile(defaultTemplate)) - e = `INSERT INTO "table name" ("foo", "bar", "baz") VALUES ('1', '2', 3) RETURNING id` + e = `INSERT INTO "table_name" ("foo", "bar", "baz") VALUES ('1', '2', 3) RETURNING id` if s != e { t.Fatalf("Got: %s, Expecting: %s", s, e) diff --git a/util/sqlgen/table.go b/util/sqlgen/table.go index 41e9308401c1f12f3597b07e7930a9c4cf74eea1..4802b892fd47571ba22ebabdc8f2029b078af923 100644 --- a/util/sqlgen/table.go +++ b/util/sqlgen/table.go @@ -8,6 +8,7 @@ import ( var ( reTableSeparator = regexp.MustCompile(`\s*?,\s*?`) reAliasSeparator = regexp.MustCompile(`(?i:\s+AS\s+)`) + reSpaceSeparator = regexp.MustCompile(`\s+`) ) type table_t struct { @@ -24,6 +25,10 @@ func quotedTableName(layout *Template, input string) string { chunks := reAliasSeparator.Split(input, 2) + if len(chunks) == 1 { + chunks = reSpaceSeparator.Split(input, 2) + } + name := chunks[0] nameChunks := strings.SplitN(name, layout.ColumnSeparator, 2) diff --git a/util/sqlgen/table_test.go b/util/sqlgen/table_test.go index e647f78ac45910e2db88dd0cdc737aecf1c7bcea..d6a88c736d740570ab763702ad8427e43f04d99e 100644 --- a/util/sqlgen/table_test.go +++ b/util/sqlgen/table_test.go @@ -46,6 +46,20 @@ func TestTableCompoundAlias(t *testing.T) { } } +func TestTableImplicitAlias(t *testing.T) { + var s, e string + var table Table + + table = Table{"artist.foo baz"} + + s = trim(table.Compile(defaultTemplate)) + e = `"artist"."foo" AS "baz"` + + if s != e { + t.Fatalf("Got: %s, Expecting: %s", s, e) + } +} + func TestTableMultiple(t *testing.T) { var s, e string var table Table