From 0973e800e2dae6510cf325bd8ec6caf10494ae9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Sala=C3=BCn?= Date: Fri, 30 Apr 2021 20:28:54 +0200 Subject: [PATCH] feat: quote identifiers --- builder/delete.go | 2 +- builder/delete_test.go | 36 +-- builder/insert.go | 2 +- builder/insert_test.go | 194 +++++++------- builder/select_test.go | 586 ++++++++++++++++++++--------------------- builder/update.go | 2 +- builder/update_test.go | 240 ++++++++--------- stmt/column.go | 4 +- stmt/expression.go | 20 +- stmt/on.go | 4 +- stmt/returning.go | 6 +- stmt/stmt.go | 12 + stmt/table.go | 4 +- 13 files changed, 570 insertions(+), 542 deletions(-) diff --git a/builder/delete.go b/builder/delete.go index 54e2adf..57c7e09 100644 --- a/builder/delete.go +++ b/builder/delete.go @@ -76,7 +76,7 @@ func (b Delete) Returning(values ...interface{}) Delete { panic("loukoum: delete builder has returning clause already defined") } - b.query.Returning = stmt.NewReturning(ToColumns(values)) + b.query.Returning = stmt.NewReturning(ToSelectExpressions(values)) return b } diff --git a/builder/delete_test.go b/builder/delete_test.go index 76e3d3b..680465d 100644 --- a/builder/delete_test.go +++ b/builder/delete_test.go @@ -16,7 +16,7 @@ func TestDelete(t *testing.T) { loukoum.Delete("table"), loukoum.Delete(loukoum.Table("table")), }, - SameQuery: "DELETE FROM table", + SameQuery: "DELETE FROM \"table\"", }, { Name: "Only", @@ -24,17 +24,17 @@ func TestDelete(t *testing.T) { loukoum.Delete("table").Only(), loukoum.Delete(loukoum.Table("table")).Only(), }, - SameQuery: "DELETE FROM ONLY table", + SameQuery: "DELETE FROM ONLY \"table\"", }, { Name: "As", Builder: loukoum.Delete(loukoum.Table("table").As("foobar")), - SameQuery: "DELETE FROM table AS foobar", + SameQuery: "DELETE FROM \"table\" AS \"foobar\"", }, { Name: "As only", Builder: loukoum.Delete(loukoum.Table("table").As("foobar")).Only(), - SameQuery: "DELETE FROM ONLY table AS foobar", + SameQuery: "DELETE FROM ONLY \"table\" AS \"foobar\"", }, }) } @@ -47,7 +47,7 @@ func TestDelete_Comment(t *testing.T) { loukoum.Delete("table").Comment("/foo"), loukoum.Delete(loukoum.Table("table")).Comment("/foo"), }, - SameQuery: "DELETE FROM table; -- /foo", + SameQuery: "DELETE FROM \"table\"; -- /foo", }, }) } @@ -60,12 +60,12 @@ func TestDelete_Using(t *testing.T) { loukoum.Delete("table").Using("foobar"), loukoum.Delete("table").Using(loukoum.Table("foobar")), }, - SameQuery: "DELETE FROM table USING foobar", + SameQuery: "DELETE FROM \"table\" USING \"foobar\"", }, { Name: "One table as", Builder: loukoum.Delete("table").Using(loukoum.Table("foobar").As("foo")), - SameQuery: "DELETE FROM table USING foobar AS foo", + SameQuery: "DELETE FROM \"table\" USING \"foobar\" AS \"foo\"", }, { Name: "Two tables", @@ -73,12 +73,12 @@ func TestDelete_Using(t *testing.T) { loukoum.Delete("table").Using("foobar", "example"), loukoum.Delete("table").Using(loukoum.Table("foobar"), "example"), }, - SameQuery: "DELETE FROM table USING foobar, example", + SameQuery: "DELETE FROM \"table\" USING \"foobar\", \"example\"", }, { Name: "Two tables as", Builder: loukoum.Delete("table").Using(loukoum.Table("example"), loukoum.Table("foobar").As("foo")), - SameQuery: "DELETE FROM table USING example, foobar AS foo", + SameQuery: "DELETE FROM \"table\" USING \"example\", \"foobar\" AS \"foo\"", }, }) } @@ -92,9 +92,9 @@ func TestDelete_Where(t *testing.T) { { Name: "Simple", Builder: loukoum.Delete("table").Where(loukoum.Condition("id").Equal(1)), - String: "DELETE FROM table WHERE (id = 1)", - Query: "DELETE FROM table WHERE (id = $1)", - NamedQuery: "DELETE FROM table WHERE (id = :arg_1)", + String: "DELETE FROM \"table\" WHERE (\"id\" = 1)", + Query: "DELETE FROM \"table\" WHERE (\"id\" = $1)", + NamedQuery: "DELETE FROM \"table\" WHERE (\"id\" = :arg_1)", Args: []interface{}{1}, }, { @@ -102,9 +102,9 @@ func TestDelete_Where(t *testing.T) { Builder: loukoum.Delete("table"). Where(loukoum.Condition("id").Equal(1)). And(loukoum.Condition("created_at").GreaterThan(when)), - String: "DELETE FROM table WHERE ((id = 1) AND (created_at > '2017-11-23 16:47:27+00'))", - Query: "DELETE FROM table WHERE ((id = $1) AND (created_at > $2))", - NamedQuery: "DELETE FROM table WHERE ((id = :arg_1) AND (created_at > :arg_2))", + String: "DELETE FROM \"table\" WHERE ((\"id\" = 1) AND (\"created_at\" > '2017-11-23 16:47:27+00'))", + Query: "DELETE FROM \"table\" WHERE ((\"id\" = $1) AND (\"created_at\" > $2))", + NamedQuery: "DELETE FROM \"table\" WHERE ((\"id\" = :arg_1) AND (\"created_at\" > :arg_2))", Args: []interface{}{1, when}, }, }) @@ -114,13 +114,13 @@ func TestDelete_Returning(t *testing.T) { RunBuilderTests(t, []BuilderTest{ { Name: "*", - Builder: loukoum.Delete("table").Returning("*"), - SameQuery: "DELETE FROM table RETURNING *", + Builder: loukoum.Delete("table").Returning(loukoum.Raw("*")), + SameQuery: "DELETE FROM \"table\" RETURNING *", }, { Name: "id", Builder: loukoum.Delete("table").Returning("id"), - SameQuery: "DELETE FROM table RETURNING id", + SameQuery: "DELETE FROM \"table\" RETURNING \"id\"", }, }) } diff --git a/builder/insert.go b/builder/insert.go index 51cf723..3ab646d 100644 --- a/builder/insert.go +++ b/builder/insert.go @@ -61,7 +61,7 @@ func (b Insert) Returning(values ...interface{}) Insert { panic("loukoum: insert builder has returning clause already defined") } - b.query.Returning = stmt.NewReturning(ToColumns(values)) + b.query.Returning = stmt.NewReturning(ToSelectExpressions(values)) return b } diff --git a/builder/insert_test.go b/builder/insert_test.go index a14c545..cebe730 100644 --- a/builder/insert_test.go +++ b/builder/insert_test.go @@ -18,12 +18,12 @@ func TestInsert_Columns(t *testing.T) { { Name: "With columns", Builder: loukoum.Insert("table").Columns("a", "b", "c"), - SameQuery: "INSERT INTO table (a, b, c)", + SameQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\")", }, { Name: "Without columns", Builder: loukoum.Insert("table"), - SameQuery: "INSERT INTO table", + SameQuery: "INSERT INTO \"table\"", }, }) } @@ -33,12 +33,12 @@ func TestInsert_Comment(t *testing.T) { { Name: "With columns", Builder: loukoum.Insert("table").Columns("a", "b", "c").Comment("/foo"), - SameQuery: "INSERT INTO table (a, b, c); -- /foo", + SameQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\"); -- /foo", }, { Name: "Without columns", Builder: loukoum.Insert("table").Comment("/foo"), - SameQuery: "INSERT INTO table; -- /foo", + SameQuery: "INSERT INTO \"table\"; -- /foo", }, }) } @@ -51,9 +51,9 @@ func TestInsert_Values(t *testing.T) { loukoum.Insert("table").Columns("a", "b", "c").Values([]string{"va", "vb", "vc"}), loukoum.Insert("table").Columns("a", "b", "c").Values("va", "vb", "vc"), }, - String: "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc')", - Query: "INSERT INTO table (a, b, c) VALUES ($1, $2, $3)", - NamedQuery: "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3)", + String: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc')", + Query: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3)", + NamedQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3)", Args: []interface{}{"va", "vb", "vc"}, }, { @@ -62,9 +62,9 @@ func TestInsert_Values(t *testing.T) { loukoum.Insert("table").Values([]string{"va", "vb", "vc"}), loukoum.Insert("table").Values("va", "vb", "vc"), }, - String: "INSERT INTO table VALUES ('va', 'vb', 'vc')", - Query: "INSERT INTO table VALUES ($1, $2, $3)", - NamedQuery: "INSERT INTO table VALUES (:arg_1, :arg_2, :arg_3)", + String: "INSERT INTO \"table\" VALUES ('va', 'vb', 'vc')", + Query: "INSERT INTO \"table\" VALUES ($1, $2, $3)", + NamedQuery: "INSERT INTO \"table\" VALUES (:arg_1, :arg_2, :arg_3)", Args: []interface{}{"va", "vb", "vc"}, }, { @@ -77,9 +77,9 @@ func TestInsert_Values(t *testing.T) { Columns("email", "enabled", "created_at"). Values([]interface{}{"tech@ulule.com", true, loukoum.Raw("NOW()")}), }, - String: "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW())", - Query: "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW())", - NamedQuery: "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW())", + String: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW())", + Query: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW())", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW())", Args: []interface{}{"tech@ulule.com", true}, }, { @@ -94,9 +94,9 @@ func TestInsert_Values(t *testing.T) { Columns("data"). Values([][]byte{{1, 2, 3}}), }, - String: "INSERT INTO table (data) VALUES (decode('010203', 'hex'))", - Query: "INSERT INTO table (data) VALUES ($1)", - NamedQuery: "INSERT INTO table (data) VALUES (:arg_1)", + String: "INSERT INTO \"table\" (\"data\") VALUES (decode('010203', 'hex'))", + Query: "INSERT INTO \"table\" (\"data\") VALUES ($1)", + NamedQuery: "INSERT INTO \"table\" (\"data\") VALUES (:arg_1)", Args: []interface{}{[]byte{1, 2, 3}}, }, }) @@ -112,15 +112,15 @@ func TestInsert_OnConflict(t *testing.T) { Values("tech@ulule.com", true, loukoum.Raw("NOW()")). OnConflict(loukoum.DoNothing()), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", "ON CONFLICT DO NOTHING", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", "ON CONFLICT DO NOTHING", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", "ON CONFLICT DO NOTHING", ), Args: []interface{}{"tech@ulule.com", true}, @@ -140,16 +140,16 @@ func TestInsert_OnConflict(t *testing.T) { OnConflict(loukoum.Column("email"), loukoum.DoNothing()), }, String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", - "ON CONFLICT (email) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", + "ON CONFLICT (\"email\") DO NOTHING", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", - "ON CONFLICT (email) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", + "ON CONFLICT (\"email\") DO NOTHING", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", - "ON CONFLICT (email) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", + "ON CONFLICT (\"email\") DO NOTHING", ), Args: []interface{}{"tech@ulule.com", true}, }, @@ -161,16 +161,16 @@ func TestInsert_OnConflict(t *testing.T) { Values("tech@ulule.com", true, loukoum.Raw("NOW()")). OnConflict("email", loukoum.Column("uuid"), "reference", loukoum.DoNothing()), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO NOTHING", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO NOTHING", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO NOTHING", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO NOTHING", ), Args: []interface{}{"tech@ulule.com", true}, }, @@ -195,16 +195,16 @@ func TestInsert_OnConflict(t *testing.T) { )), }, String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", - "ON CONFLICT (email) DO UPDATE SET created_at = NOW(), enabled = true", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", + "ON CONFLICT (\"email\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = true", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", - "ON CONFLICT (email) DO UPDATE SET created_at = NOW(), enabled = $3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", + "ON CONFLICT (\"email\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = $3", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", - "ON CONFLICT (email) DO UPDATE SET created_at = NOW(), enabled = :arg_3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", + "ON CONFLICT (\"email\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = :arg_3", ), Args: []interface{}{"tech@ulule.com", true, true}, }, @@ -219,16 +219,16 @@ func TestInsert_OnConflict(t *testing.T) { loukoum.Pair("enabled", true), )), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", - "ON CONFLICT (email, uuid) DO UPDATE SET created_at = NOW(), enabled = true", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = true", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", - "ON CONFLICT (email, uuid) DO UPDATE SET created_at = NOW(), enabled = $3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = $3", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", - "ON CONFLICT (email, uuid) DO UPDATE SET created_at = NOW(), enabled = :arg_3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = :arg_3", ), Args: []interface{}{"tech@ulule.com", true, true}, }, @@ -243,16 +243,16 @@ func TestInsert_OnConflict(t *testing.T) { loukoum.Pair("enabled", true), )), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', true, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO UPDATE SET created_at = NOW(), enabled = true", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', true, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = true", ), Query: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO UPDATE SET created_at = NOW(), enabled = $3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = $3", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, NOW()) ", - "ON CONFLICT (email, uuid, reference) DO UPDATE SET created_at = NOW(), enabled = :arg_3", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, NOW()) ", + "ON CONFLICT (\"email\", \"uuid\", \"reference\") DO UPDATE SET \"created_at\" = NOW(), \"enabled\" = :arg_3", ), Args: []interface{}{"tech@ulule.com", true, true}, }, @@ -341,9 +341,9 @@ func TestInsert_Returning(t *testing.T) { Columns("a", "b", "c"). Values([]string{"va", "vb", "vc"}). Returning("a"), - String: "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') RETURNING a", - Query: "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) RETURNING a", - NamedQuery: "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) RETURNING a", + String: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') RETURNING \"a\"", + Query: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) RETURNING \"a\"", + NamedQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) RETURNING \"a\"", Args: []interface{}{"va", "vb", "vc"}, }, { @@ -353,9 +353,9 @@ func TestInsert_Returning(t *testing.T) { Columns("a", "b", "c"). Values([]string{"va", "vb", "vc"}). Returning("b", "a"), - String: "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') RETURNING b, a", - Query: "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) RETURNING b, a", - NamedQuery: "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) RETURNING b, a", + String: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') RETURNING \"b\", \"a\"", + Query: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) RETURNING \"b\", \"a\"", + NamedQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) RETURNING \"b\", \"a\"", Args: []interface{}{"va", "vb", "vc"}, }, { @@ -365,9 +365,9 @@ func TestInsert_Returning(t *testing.T) { Columns("a", "b", "c"). Values([]string{"va", "vb", "vc"}). Returning("b", "a", "c"), - String: "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') RETURNING b, a, c", - Query: "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) RETURNING b, a, c", - NamedQuery: "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) RETURNING b, a, c", + String: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') RETURNING \"b\", \"a\", \"c\"", + Query: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) RETURNING \"b\", \"a\", \"c\"", + NamedQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) RETURNING \"b\", \"a\", \"c\"", //nolint:lll Args: []interface{}{"va", "vb", "vc"}, }, { @@ -377,9 +377,9 @@ func TestInsert_Returning(t *testing.T) { Columns("a", "b", "c"). Values([]string{"va", "vb", "vc"}). Returning(loukoum.Column("a").As("alias_a")), - String: "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') RETURNING a AS alias_a", - Query: "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) RETURNING a AS alias_a", - NamedQuery: "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) RETURNING a AS alias_a", + String: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') RETURNING \"a\" AS \"alias_a\"", + Query: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) RETURNING \"a\" AS \"alias_a\"", + NamedQuery: "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) RETURNING \"a\" AS \"alias_a\"", //nolint:lll Args: []interface{}{"va", "vb", "vc"}, }, { @@ -390,16 +390,16 @@ func TestInsert_Returning(t *testing.T) { Values([]string{"va", "vb", "vc"}). Returning(loukoum.Column("b").As("alias_b"), loukoum.Column("a").As("alias_a")), String: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') ", - "RETURNING b AS alias_b, a AS alias_a", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') ", + "RETURNING \"b\" AS \"alias_b\", \"a\" AS \"alias_a\"", ), Query: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) ", - "RETURNING b AS alias_b, a AS alias_a", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) ", + "RETURNING \"b\" AS \"alias_b\", \"a\" AS \"alias_a\"", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) ", - "RETURNING b AS alias_b, a AS alias_a", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) ", + "RETURNING \"b\" AS \"alias_b\", \"a\" AS \"alias_a\"", ), Args: []interface{}{"va", "vb", "vc"}, }, @@ -415,16 +415,16 @@ func TestInsert_Returning(t *testing.T) { loukoum.Column("c").As("alias_c"), ), String: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES ('va', 'vb', 'vc') ", - "RETURNING a AS alias_a, b AS alias_b, c AS alias_c", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ('va', 'vb', 'vc') ", + "RETURNING \"a\" AS \"alias_a\", \"b\" AS \"alias_b\", \"c\" AS \"alias_c\"", ), Query: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES ($1, $2, $3) ", - "RETURNING a AS alias_a, b AS alias_b, c AS alias_c", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES ($1, $2, $3) ", + "RETURNING \"a\" AS \"alias_a\", \"b\" AS \"alias_b\", \"c\" AS \"alias_c\"", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (a, b, c) VALUES (:arg_1, :arg_2, :arg_3) ", - "RETURNING a AS alias_a, b AS alias_b, c AS alias_c", + "INSERT INTO \"table\" (\"a\", \"b\", \"c\") VALUES (:arg_1, :arg_2, :arg_3) ", + "RETURNING \"a\" AS \"alias_a\", \"b\" AS \"alias_b\", \"c\" AS \"alias_c\"", ), Args: []interface{}{"va", "vb", "vc"}, }, @@ -446,11 +446,11 @@ func TestInsert_Valuer(t *testing.T) { Columns("email", "enabled", "created_at"). Values("tech@ulule.com", true, pq.NullTime{Time: when, Valid: true}), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', ", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', ", "true, '2017-11-23 16:47:27+00')", ), - Query: "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, $3)", - NamedQuery: "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, :arg_3)", + Query: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, $3)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, :arg_3)", Args: []interface{}{"tech@ulule.com", true, pq.NullTime{Time: when, Valid: true}}, }, { @@ -460,11 +460,11 @@ func TestInsert_Valuer(t *testing.T) { Columns("email", "enabled", "created_at"). Values("tech@ulule.com", true, pq.NullTime{}), String: fmt.Sprint( - "INSERT INTO table (email, enabled, created_at) VALUES ('tech@ulule.com', ", + "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ('tech@ulule.com', ", "true, NULL)", ), - Query: "INSERT INTO table (email, enabled, created_at) VALUES ($1, $2, $3)", - NamedQuery: "INSERT INTO table (email, enabled, created_at) VALUES (:arg_1, :arg_2, :arg_3)", + Query: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES ($1, $2, $3)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"enabled\", \"created_at\") VALUES (:arg_1, :arg_2, :arg_3)", Args: []interface{}{"tech@ulule.com", true, pq.NullTime{}}, }, { @@ -473,9 +473,9 @@ func TestInsert_Valuer(t *testing.T) { Insert("table"). Columns("email", "comment"). Values("tech@ulule.com", sql.NullString{String: "foobar", Valid: true}), - String: "INSERT INTO table (email, comment) VALUES ('tech@ulule.com', 'foobar')", - Query: "INSERT INTO table (email, comment) VALUES ($1, $2)", - NamedQuery: "INSERT INTO table (email, comment) VALUES (:arg_1, :arg_2)", + String: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES ('tech@ulule.com', 'foobar')", + Query: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES ($1, $2)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", sql.NullString{String: "foobar", Valid: true}}, }, { @@ -484,9 +484,9 @@ func TestInsert_Valuer(t *testing.T) { Insert("table"). Columns("email", "comment"). Values("tech@ulule.com", sql.NullString{}), - String: "INSERT INTO table (email, comment) VALUES ('tech@ulule.com', NULL)", - Query: "INSERT INTO table (email, comment) VALUES ($1, $2)", - NamedQuery: "INSERT INTO table (email, comment) VALUES (:arg_1, :arg_2)", + String: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES ('tech@ulule.com', NULL)", + Query: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES ($1, $2)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"comment\") VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", sql.NullString{}}, }, { @@ -495,9 +495,9 @@ func TestInsert_Valuer(t *testing.T) { Insert("table"). Columns("email", "login"). Values("tech@ulule.com", sql.NullInt64{Int64: 30, Valid: true}), - String: "INSERT INTO table (email, login) VALUES ('tech@ulule.com', 30)", - Query: "INSERT INTO table (email, login) VALUES ($1, $2)", - NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)", + String: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ('tech@ulule.com', 30)", + Query: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ($1, $2)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"login\") VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", sql.NullInt64{Int64: 30, Valid: true}}, }, { @@ -506,9 +506,9 @@ func TestInsert_Valuer(t *testing.T) { Insert("table"). Columns("email", "login"). Values("tech@ulule.com", sql.NullInt64{}), - String: "INSERT INTO table (email, login) VALUES ('tech@ulule.com', NULL)", - Query: "INSERT INTO table (email, login) VALUES ($1, $2)", - NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)", + String: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ('tech@ulule.com', NULL)", + Query: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ($1, $2)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"login\") VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", sql.NullInt64{}}, }, { @@ -517,9 +517,9 @@ func TestInsert_Valuer(t *testing.T) { Insert("table"). Columns("email", "login"). Values("tech@ulule.com", (*valuer)(nil)), - String: "INSERT INTO table (email, login) VALUES ('tech@ulule.com', NULL)", - Query: "INSERT INTO table (email, login) VALUES ($1, $2)", - NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)", + String: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ('tech@ulule.com', NULL)", + Query: "INSERT INTO \"table\" (\"email\", \"login\") VALUES ($1, $2)", + NamedQuery: "INSERT INTO \"table\" (\"email\", \"login\") VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", (*valuer)(nil)}, }, }) @@ -554,15 +554,15 @@ func TestInsert_Set(t *testing.T) { ), }, String: fmt.Sprint( - "INSERT INTO table (created_at, email, enabled) ", + "INSERT INTO \"table\" (\"created_at\", \"email\", \"enabled\") ", "VALUES (NOW(), 'tech@ulule.com', true)", ), Query: fmt.Sprint( - "INSERT INTO table (created_at, email, enabled) ", + "INSERT INTO \"table\" (\"created_at\", \"email\", \"enabled\") ", "VALUES (NOW(), $1, $2)", ), NamedQuery: fmt.Sprint( - "INSERT INTO table (created_at, email, enabled) ", + "INSERT INTO \"table\" (\"created_at\", \"email\", \"enabled\") ", "VALUES (NOW(), :arg_1, :arg_2)", ), Args: []interface{}{"tech@ulule.com", true}, diff --git a/builder/select_test.go b/builder/select_test.go index ac298b5..78e1cb4 100644 --- a/builder/select_test.go +++ b/builder/select_test.go @@ -19,9 +19,9 @@ func TestSelect_Value(t *testing.T) { loukoum.Value("fr"). Equal(loukoum.Raw("ANY(string_to_array(languages, ','))")), ), - String: "SELECT id FROM video WHERE ('fr' = ANY(string_to_array(languages, ',')))", - Query: "SELECT id FROM video WHERE ($1 = ANY(string_to_array(languages, ',')))", - NamedQuery: "SELECT id FROM video WHERE (:arg_1 = ANY(string_to_array(languages, ',')))", + String: "SELECT \"id\" FROM \"video\" WHERE ('fr' = ANY(string_to_array(languages, ',')))", + Query: "SELECT \"id\" FROM \"video\" WHERE ($1 = ANY(string_to_array(languages, ',')))", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (:arg_1 = ANY(string_to_array(languages, ',')))", Args: []interface{}{"fr"}, }, { @@ -32,9 +32,9 @@ func TestSelect_Value(t *testing.T) { loukoum.Value("fr"). NotEqual(loukoum.Raw("ANY(string_to_array(languages, ','))")), ), - String: "SELECT id FROM video WHERE ('fr' != ANY(string_to_array(languages, ',')))", - Query: "SELECT id FROM video WHERE ($1 != ANY(string_to_array(languages, ',')))", - NamedQuery: "SELECT id FROM video WHERE (:arg_1 != ANY(string_to_array(languages, ',')))", + String: "SELECT \"id\" FROM \"video\" WHERE ('fr' != ANY(string_to_array(languages, ',')))", + Query: "SELECT \"id\" FROM \"video\" WHERE ($1 != ANY(string_to_array(languages, ',')))", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (:arg_1 != ANY(string_to_array(languages, ',')))", Args: []interface{}{"fr"}, }, { @@ -45,9 +45,9 @@ func TestSelect_Value(t *testing.T) { loukoum.Value("{fr}"). Overlap(loukoum.Raw("languages")), ), - String: "SELECT id FROM video WHERE ('{fr}' && languages)", - Query: "SELECT id FROM video WHERE ($1 && languages)", - NamedQuery: "SELECT id FROM video WHERE (:arg_1 && languages)", + String: "SELECT \"id\" FROM \"video\" WHERE ('{fr}' && languages)", + Query: "SELECT \"id\" FROM \"video\" WHERE ($1 && languages)", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (:arg_1 && languages)", Args: []interface{}{"{fr}"}, }, { @@ -58,9 +58,9 @@ func TestSelect_Value(t *testing.T) { loukoum.Value("{fr}"). IsContainedBy(loukoum.Raw("languages")), ), - String: "SELECT id FROM video WHERE ('{fr}' <@ languages)", - Query: "SELECT id FROM video WHERE ($1 <@ languages)", - NamedQuery: "SELECT id FROM video WHERE (:arg_1 <@ languages)", + String: "SELECT \"id\" FROM \"video\" WHERE ('{fr}' <@ languages)", + Query: "SELECT \"id\" FROM \"video\" WHERE ($1 <@ languages)", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (:arg_1 <@ languages)", Args: []interface{}{"{fr}"}, }, { @@ -68,9 +68,9 @@ func TestSelect_Value(t *testing.T) { Builder: loukoum.Select("id"). From("video"). Where(loukoum.Condition("languages").Contains("{fr}")), - String: "SELECT id FROM video WHERE (languages @> '{fr}')", - Query: "SELECT id FROM video WHERE (languages @> $1)", - NamedQuery: "SELECT id FROM video WHERE (languages @> :arg_1)", + String: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" @> '{fr}')", + Query: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" @> $1)", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" @> :arg_1)", Args: []interface{}{"{fr}"}, }, { @@ -78,9 +78,9 @@ func TestSelect_Value(t *testing.T) { Builder: loukoum.Select("id"). From("video"). Where(loukoum.Condition("languages").IsContainedBy("{fr}")), - String: "SELECT id FROM video WHERE (languages <@ '{fr}')", - Query: "SELECT id FROM video WHERE (languages <@ $1)", - NamedQuery: "SELECT id FROM video WHERE (languages <@ :arg_1)", + String: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" <@ '{fr}')", + Query: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" <@ $1)", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" <@ :arg_1)", Args: []interface{}{"{fr}"}, }, { @@ -88,9 +88,9 @@ func TestSelect_Value(t *testing.T) { Builder: loukoum.Select("id"). From("video"). Where(loukoum.Condition("languages").Overlap("{fr}")), - String: "SELECT id FROM video WHERE (languages && '{fr}')", - Query: "SELECT id FROM video WHERE (languages && $1)", - NamedQuery: "SELECT id FROM video WHERE (languages && :arg_1)", + String: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" && '{fr}')", + Query: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" && $1)", + NamedQuery: "SELECT \"id\" FROM \"video\" WHERE (\"languages\" && :arg_1)", Args: []interface{}{"{fr}"}, }, }) @@ -101,7 +101,7 @@ func TestSelect_Comment(t *testing.T) { { Name: "Simple", Builder: loukoum.Select("test").Comment("/foo"), - SameQuery: "SELECT test; -- /foo", + SameQuery: "SELECT \"test\"; -- /foo", }, { Name: "Complex", @@ -110,9 +110,9 @@ func TestSelect_Comment(t *testing.T) { Where(loukoum.Condition("username"). Equal("thoas")). Comment("/foo"), - String: "SELECT test FROM users WHERE (username = 'thoas'); -- /foo", - Query: "SELECT test FROM users WHERE (username = $1); -- /foo", - NamedQuery: "SELECT test FROM users WHERE (username = :arg_1); -- /foo", + String: "SELECT \"test\" FROM \"users\" WHERE (\"username\" = 'thoas'); -- /foo", + Query: "SELECT \"test\" FROM \"users\" WHERE (\"username\" = $1); -- /foo", + NamedQuery: "SELECT \"test\" FROM \"users\" WHERE (\"username\" = :arg_1); -- /foo", Args: []interface{}{"thoas"}, }, }) @@ -123,27 +123,27 @@ func TestSelect_Columns(t *testing.T) { { Name: "Simple", Builder: loukoum.Select("test"), - SameQuery: "SELECT test", + SameQuery: "SELECT \"test\"", }, { Name: "Distinct", Builder: loukoum.Select("test").Distinct(), - SameQuery: "SELECT DISTINCT test", + SameQuery: "SELECT DISTINCT \"test\"", }, { Name: "As", Builder: loukoum.Select(loukoum.Column("test").As("foobar")), - SameQuery: "SELECT test AS foobar", + SameQuery: "SELECT \"test\" AS \"foobar\"", }, { Name: "Two columns", Builder: loukoum.Select("test", "foobar"), - SameQuery: "SELECT test, foobar", + SameQuery: "SELECT \"test\", \"foobar\"", }, { Name: "Two columns as", Builder: loukoum.Select("test", loukoum.Column("test2").As("foobar")), - SameQuery: "SELECT test, test2 AS foobar", + SameQuery: "SELECT \"test\", \"test2\" AS \"foobar\"", }, { Name: "Three columns as", @@ -151,7 +151,7 @@ func TestSelect_Columns(t *testing.T) { loukoum.Select("a", "b", loukoum.Column("c").As("x")), loukoum.Select("a", loukoum.Column("b"), loukoum.Column("c").As("x")), }, - SameQuery: "SELECT a, b, c AS x", + SameQuery: "SELECT \"a\", \"b\", \"c\" AS \"x\"", }, { Name: "Three columns", @@ -163,11 +163,11 @@ func TestSelect_Columns(t *testing.T) { loukoum.Column("c"), }), }, - SameQuery: "SELECT a, b, c", + SameQuery: "SELECT \"a\", \"b\", \"c\"", }, { Name: "Exists expression", - Builder: loukoum.Select(loukoum.Exists(loukoum.Select("1"))), + Builder: loukoum.Select(loukoum.Exists(loukoum.Select(loukoum.Raw("1")))), SameQuery: "SELECT EXISTS (SELECT 1)", }, { @@ -249,7 +249,7 @@ func TestSelect_DistinctOn(t *testing.T) { From("record"). Where(loukoum.Condition("disabled").IsNull(false)), }, - SameQuery: "SELECT DISTINCT ON (id) date FROM record WHERE (disabled IS NOT NULL)", + SameQuery: "SELECT DISTINCT ON (\"id\") \"date\" FROM \"record\" WHERE (\"disabled\" IS NOT NULL)", }, { Name: "Two columns", @@ -266,8 +266,8 @@ func TestSelect_DistinctOn(t *testing.T) { Where(loukoum.Condition("disabled").IsNull(false)), }, SameQuery: fmt.Sprint( - "SELECT DISTINCT ON (id, user_id) date FROM record ", - "WHERE (disabled IS NOT NULL)", + "SELECT DISTINCT ON (\"id\", \"user_id\") \"date\" FROM \"record\" ", + "WHERE (\"disabled\" IS NOT NULL)", ), }, { @@ -285,8 +285,8 @@ func TestSelect_DistinctOn(t *testing.T) { Where(loukoum.Condition("disabled").IsNull(false)), }, SameQuery: fmt.Sprint( - "SELECT DISTINCT ON (id, user_id, project_id) date FROM record ", - "WHERE (disabled IS NOT NULL)", + "SELECT DISTINCT ON (\"id\", \"user_id\", \"project_id\") \"date\" FROM \"record\" ", + "WHERE (\"disabled\" IS NOT NULL)", ), }, }) @@ -297,12 +297,12 @@ func TestSelect_From(t *testing.T) { { Name: "Simple", Builder: loukoum.Select("a", "b", "c").From("foobar"), - SameQuery: "SELECT a, b, c FROM foobar", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"foobar\"", }, { Name: "As", Builder: loukoum.Select("a").From(loukoum.Table("foobar").As("example")), - SameQuery: "SELECT a FROM foobar AS example", + SameQuery: "SELECT \"a\" FROM \"foobar\" AS \"example\"", }, }) } @@ -333,7 +333,7 @@ func TestSelect_Join(t *testing.T) { From("test1"). Join("test2", "ON test1.id = test2.fk_id", loukoum.InnerJoin), }, - SameQuery: "SELECT a, b, c FROM test1 INNER JOIN test2 ON test1.id = test2.fk_id", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"test1\" INNER JOIN \"test2\" ON \"test1\".\"id\" = \"test2\".\"fk_id\"", //nolint:lll }, { Name: "Left", @@ -341,7 +341,7 @@ func TestSelect_Join(t *testing.T) { Select("a", "b", "c"). From("test1"). Join("test3", "test3.fkey = test1.id", loukoum.LeftJoin), - SameQuery: "SELECT a, b, c FROM test1 LEFT JOIN test3 ON test3.fkey = test1.id", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"test1\" LEFT JOIN \"test3\" ON \"test3\".\"fkey\" = \"test1\".\"id\"", }, { Name: "Right", @@ -349,7 +349,7 @@ func TestSelect_Join(t *testing.T) { Select("a", "b", "c"). From("test2"). Join("test4", "test4.gid = test2.id", loukoum.RightJoin), - SameQuery: "SELECT a, b, c FROM test2 RIGHT JOIN test4 ON test4.gid = test2.id", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"test2\" RIGHT JOIN \"test4\" ON \"test4\".\"gid\" = \"test2\".\"id\"", }, { Name: "Left Outer", @@ -357,7 +357,7 @@ func TestSelect_Join(t *testing.T) { Select("a", "b", "c"). From("test1"). Join("test3", "test3.fkey = test1.id", loukoum.LeftOuterJoin), - SameQuery: "SELECT a, b, c FROM test1 LEFT OUTER JOIN test3 ON test3.fkey = test1.id", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"test1\" LEFT OUTER JOIN \"test3\" ON \"test3\".\"fkey\" = \"test1\".\"id\"", //nolint:lll }, { Name: "Right Outer", @@ -365,7 +365,7 @@ func TestSelect_Join(t *testing.T) { Select("a", "b", "c"). From("test1"). Join("test3", "test3.fkey = test1.id", loukoum.RightOuterJoin), - SameQuery: "SELECT a, b, c FROM test1 RIGHT OUTER JOIN test3 ON test3.fkey = test1.id", + SameQuery: "SELECT \"a\", \"b\", \"c\" FROM \"test1\" RIGHT OUTER JOIN \"test3\" ON \"test3\".\"fkey\" = \"test1\".\"id\"", //nolint:lll }, { Name: "Two tables", @@ -387,8 +387,8 @@ func TestSelect_Join(t *testing.T) { Join(loukoum.Table("test3"), loukoum.On("test4.uid", "test3.id")), }, SameQuery: fmt.Sprint( - "SELECT a, b, c FROM test2 INNER JOIN test4 ON test4.gid = test2.id ", - "INNER JOIN test3 ON test4.uid = test3.id", + "SELECT \"a\", \"b\", \"c\" FROM \"test2\" INNER JOIN \"test4\" ON \"test4\".\"gid\" = \"test2\".\"id\" ", + "INNER JOIN \"test3\" ON \"test4\".\"uid\" = \"test3\".\"id\"", ), }, { @@ -415,8 +415,8 @@ func TestSelect_Join(t *testing.T) { ), }, SameQuery: fmt.Sprint( - "SELECT a, b, c FROM test2 INNER JOIN test4 ON (test4.gid = test2.id AND test4.d = test2.d) ", - "INNER JOIN test3 ON (test4.uid = test3.id AND test3.e = test2.e)", + "SELECT \"a\", \"b\", \"c\" FROM \"test2\" INNER JOIN \"test4\" ON (\"test4\".\"gid\" = \"test2\".\"id\" AND \"test4\".\"d\" = \"test2\".\"d\") ", //nolint:lll + "INNER JOIN \"test3\" ON (\"test4\".\"uid\" = \"test3\".\"id\" AND \"test3\".\"e\" = \"test2\".\"e\")", ), }, { @@ -453,9 +453,9 @@ func TestSelect_Join(t *testing.T) { ), }, SameQuery: fmt.Sprint( - "SELECT a, b, c FROM test2 ", - "INNER JOIN test4 ON ((test4.gid = test2.id AND test4.d = test2.d) OR test4.f = test2.f) ", - "INNER JOIN test3 ON ((test4.uid = test3.id OR test3.e = test2.e) AND test3.g = test2.g)", + "SELECT \"a\", \"b\", \"c\" FROM \"test2\" ", + "INNER JOIN \"test4\" ON ((\"test4\".\"gid\" = \"test2\".\"id\" AND \"test4\".\"d\" = \"test2\".\"d\") OR \"test4\".\"f\" = \"test2\".\"f\") ", //nolint:lll + "INNER JOIN \"test3\" ON ((\"test4\".\"uid\" = \"test3\".\"id\" OR \"test3\".\"e\" = \"test2\".\"e\") AND \"test3\".\"g\" = \"test2\".\"g\")", //nolint:lll ), }, }) @@ -469,9 +469,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("id").Equal(1)), - String: `SELECT id FROM table WHERE (id = 1)`, - Query: `SELECT id FROM table WHERE (id = $1)`, - NamedQuery: `SELECT id FROM table WHERE (id = :arg_1)`, + String: `SELECT "id" FROM "table" WHERE ("id" = 1)`, + Query: `SELECT "id" FROM "table" WHERE ("id" = $1)`, + NamedQuery: `SELECT "id" FROM "table" WHERE ("id" = :arg_1)`, Args: []interface{}{1}, }, { @@ -481,9 +481,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { From("table"). Where(loukoum.Condition("id").Equal(1)). And(loukoum.Condition("slug").Equal("foo")), - String: "SELECT id FROM table WHERE ((id = 1) AND (slug = 'foo'))", - Query: "SELECT id FROM table WHERE ((id = $1) AND (slug = $2))", - NamedQuery: "SELECT id FROM table WHERE ((id = :arg_1) AND (slug = :arg_2))", + String: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = 1) AND (\"slug\" = 'foo'))", + Query: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = $1) AND (\"slug\" = $2))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = :arg_1) AND (\"slug\" = :arg_2))", Args: []interface{}{1, "foo"}, }, { @@ -494,9 +494,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Where(loukoum.Condition("id").Equal(1)). And(loukoum.Condition("slug").Equal("foo")). And(loukoum.Condition("title").Equal("hello")), - String: "SELECT id FROM table WHERE (((id = 1) AND (slug = 'foo')) AND (title = 'hello'))", - Query: "SELECT id FROM table WHERE (((id = $1) AND (slug = $2)) AND (title = $3))", - NamedQuery: "SELECT id FROM table WHERE (((id = :arg_1) AND (slug = :arg_2)) AND (title = :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = 1) AND (\"slug\" = 'foo')) AND (\"title\" = 'hello'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = $1) AND (\"slug\" = $2)) AND (\"title\" = $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = :arg_1) AND (\"slug\" = :arg_2)) AND (\"title\" = :arg_3))", //nolint:lll Args: []interface{}{1, "foo", "hello"}, }, { @@ -507,9 +507,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Where(loukoum.Condition("id").Equal(1)). Or(loukoum.Condition("slug").Equal("foo")). Or(loukoum.Condition("title").Equal("hello")), - String: "SELECT id FROM table WHERE (((id = 1) OR (slug = 'foo')) OR (title = 'hello'))", - Query: "SELECT id FROM table WHERE (((id = $1) OR (slug = $2)) OR (title = $3))", - NamedQuery: "SELECT id FROM table WHERE (((id = :arg_1) OR (slug = :arg_2)) OR (title = :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = 1) OR (\"slug\" = 'foo')) OR (\"title\" = 'hello'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = $1) OR (\"slug\" = $2)) OR (\"title\" = $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = :arg_1) OR (\"slug\" = :arg_2)) OR (\"title\" = :arg_3))", //nolint:lll Args: []interface{}{1, "foo", "hello"}, }, { @@ -520,9 +520,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Where(loukoum.Condition("id").Equal(1)). And(loukoum.Condition("slug").Equal("foo")). Or(loukoum.Condition("title").Equal("hello")), - String: `SELECT id FROM table WHERE (((id = 1) AND (slug = 'foo')) OR (title = 'hello'))`, - Query: `SELECT id FROM table WHERE (((id = $1) AND (slug = $2)) OR (title = $3))`, - NamedQuery: `SELECT id FROM table WHERE (((id = :arg_1) AND (slug = :arg_2)) OR (title = :arg_3))`, + String: `SELECT "id" FROM "table" WHERE ((("id" = 1) AND ("slug" = 'foo')) OR ("title" = 'hello'))`, + Query: `SELECT "id" FROM "table" WHERE ((("id" = $1) AND ("slug" = $2)) OR ("title" = $3))`, + NamedQuery: `SELECT "id" FROM "table" WHERE ((("id" = :arg_1) AND ("slug" = :arg_2)) OR ("title" = :arg_3))`, Args: []interface{}{1, "foo", "hello"}, }, { @@ -537,16 +537,16 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { loukoum.And(loukoum.Condition("slug").Equal("foo"), loukoum.Condition("active").Equal(true)), ), String: fmt.Sprint( - "SELECT id FROM table WHERE (((id = 1) OR (title = 'hello')) OR ", - "((slug = 'foo') AND (active = true)))", + "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = 1) OR (\"title\" = 'hello')) OR ", + "((\"slug\" = 'foo') AND (\"active\" = true)))", ), Query: fmt.Sprint( - "SELECT id FROM table WHERE (((id = $1) OR (title = $2)) OR ", - "((slug = $3) AND (active = $4)))", + "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = $1) OR (\"title\" = $2)) OR ", + "((\"slug\" = $3) AND (\"active\" = $4)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM table WHERE (((id = :arg_1) OR (title = :arg_2)) OR ", - "((slug = :arg_3) AND (active = :arg_4)))", + "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = :arg_1) OR (\"title\" = :arg_2)) OR ", + "((\"slug\" = :arg_3) AND (\"active\" = :arg_4)))", ), Args: []interface{}{1, "hello", "foo", true}, }, @@ -566,9 +566,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Where(loukoum.Condition("id").Equal(1)). Where(loukoum.Condition("title").Equal("hello")), }, - String: "SELECT id FROM table WHERE ((id = 1) AND (title = 'hello'))", - Query: "SELECT id FROM table WHERE ((id = $1) AND (title = $2))", - NamedQuery: "SELECT id FROM table WHERE ((id = :arg_1) AND (title = :arg_2))", + String: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = 1) AND (\"title\" = 'hello'))", + Query: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = $1) AND (\"title\" = $2))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = :arg_1) AND (\"title\" = :arg_2))", Args: []interface{}{1, "hello"}, }, { @@ -579,9 +579,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { Where(loukoum.Condition("id").Equal(1)). Where(loukoum.Condition("title").Equal("hello")). Where(loukoum.Condition("disable").Equal(false)), - String: "SELECT id FROM table WHERE (((id = 1) AND (title = 'hello')) AND (disable = false))", - Query: "SELECT id FROM table WHERE (((id = $1) AND (title = $2)) AND (disable = $3))", - NamedQuery: "SELECT id FROM table WHERE (((id = :arg_1) AND (title = :arg_2)) AND (disable = :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = 1) AND (\"title\" = 'hello')) AND (\"disable\" = false))", //nolint:lll + Query: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = $1) AND (\"title\" = $2)) AND (\"disable\" = $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = :arg_1) AND (\"title\" = :arg_2)) AND (\"disable\" = :arg_3))", //nolint:lll Args: []interface{}{1, "hello", false}, }, { @@ -595,9 +595,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { loukoum.Condition("slug").Equal("foo").And(loukoum.Condition("active").Equal(true)), ), }, - String: "SELECT id FROM table WHERE ((id = 1) OR ((slug = 'foo') AND (active = true)))", - Query: "SELECT id FROM table WHERE ((id = $1) OR ((slug = $2) AND (active = $3)))", - NamedQuery: "SELECT id FROM table WHERE ((id = :arg_1) OR ((slug = :arg_2) AND (active = :arg_3)))", + String: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = 1) OR ((\"slug\" = 'foo') AND (\"active\" = true)))", + Query: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = $1) OR ((\"slug\" = $2) AND (\"active\" = $3)))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE ((\"id\" = :arg_1) OR ((\"slug\" = :arg_2) AND (\"active\" = :arg_3)))", //nolint:lll Args: []interface{}{1, "foo", true}, }, { @@ -607,9 +607,9 @@ func TestSelect_WhereOperatorOrder(t *testing.T) { From("table"). Where(loukoum.Condition("id").Equal(1).And(loukoum.Condition("slug").Equal("foo"))). Or(loukoum.Condition("active").Equal(true)), - String: "SELECT id FROM table WHERE (((id = 1) AND (slug = 'foo')) OR (active = true))", - Query: "SELECT id FROM table WHERE (((id = $1) AND (slug = $2)) OR (active = $3))", - NamedQuery: "SELECT id FROM table WHERE (((id = :arg_1) AND (slug = :arg_2)) OR (active = :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = 1) AND (\"slug\" = 'foo')) OR (\"active\" = true))", + Query: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = $1) AND (\"slug\" = $2)) OR (\"active\" = $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (((\"id\" = :arg_1) AND (\"slug\" = :arg_2)) OR (\"active\" = :arg_3))", //nolint:lll Args: []interface{}{1, "foo", true}, }, }) @@ -623,9 +623,9 @@ func TestSelect_WhereEqual(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("disabled").Equal(false)), - String: "SELECT id FROM table WHERE (disabled = false)", - Query: "SELECT id FROM table WHERE (disabled = $1)", - NamedQuery: "SELECT id FROM table WHERE (disabled = :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" = false)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" = $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" = :arg_1)", Args: []interface{}{false}, }, { @@ -634,9 +634,9 @@ func TestSelect_WhereEqual(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("disabled").NotEqual(false)), - String: "SELECT id FROM table WHERE (disabled != false)", - Query: "SELECT id FROM table WHERE (disabled != $1)", - NamedQuery: "SELECT id FROM table WHERE (disabled != :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" != false)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" != $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" != :arg_1)", Args: []interface{}{false}, }, { @@ -649,16 +649,16 @@ func TestSelect_WhereEqual(t *testing.T) { Where(loukoum.Condition("public_id").Equal("01C9TZXM678JR3GFZE1Y6494G3")), )), String: fmt.Sprint( - "SELECT id FROM news WHERE (user_id = (SELECT id FROM users WHERE ", - "(public_id = '01C9TZXM678JR3GFZE1Y6494G3')))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" = (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = '01C9TZXM678JR3GFZE1Y6494G3')))", ), Query: fmt.Sprint( - "SELECT id FROM news WHERE (user_id = (SELECT id FROM users WHERE ", - "(public_id = $1)))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" = (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM news WHERE (user_id = (SELECT id FROM users WHERE ", - "(public_id = :arg_1)))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" = (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = :arg_1)))", ), Args: []interface{}{"01C9TZXM678JR3GFZE1Y6494G3"}, }, @@ -672,16 +672,16 @@ func TestSelect_WhereEqual(t *testing.T) { Where(loukoum.Condition("public_id").Equal("01C9TZXM678JR3GFZE1Y6494G3")), )), String: fmt.Sprint( - "SELECT id FROM news WHERE (user_id != (SELECT id FROM users WHERE ", - "(public_id = '01C9TZXM678JR3GFZE1Y6494G3')))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" != (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = '01C9TZXM678JR3GFZE1Y6494G3')))", ), Query: fmt.Sprint( - "SELECT id FROM news WHERE (user_id != (SELECT id FROM users WHERE ", - "(public_id = $1)))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" != (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM news WHERE (user_id != (SELECT id FROM users WHERE ", - "(public_id = :arg_1)))", + "SELECT \"id\" FROM \"news\" WHERE (\"user_id\" != (SELECT \"id\" FROM \"users\" WHERE ", + "(\"public_id\" = :arg_1)))", ), Args: []interface{}{"01C9TZXM678JR3GFZE1Y6494G3"}, }, @@ -691,9 +691,9 @@ func TestSelect_WhereEqual(t *testing.T) { Select("id"). From("table"). Where(stmt.NewCall("upper", stmt.NewIdentifier("email")).Equal("FOO@EXAMPLE.ORG")), - String: "SELECT id FROM table WHERE (upper(email) = 'FOO@EXAMPLE.ORG')", - Query: "SELECT id FROM table WHERE (upper(email) = $1)", - NamedQuery: "SELECT id FROM table WHERE (upper(email) = :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") = 'FOO@EXAMPLE.ORG')", + Query: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") = $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") = :arg_1)", Args: []interface{}{"FOO@EXAMPLE.ORG"}, }, }) @@ -707,7 +707,7 @@ func TestSelect_WhereIs(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("disabled").Is(nil)), - SameQuery: "SELECT id FROM table WHERE (disabled IS NULL)", + SameQuery: "SELECT \"id\" FROM \"table\" WHERE (\"disabled\" IS NULL)", }, { Name: "Not true", @@ -715,9 +715,9 @@ func TestSelect_WhereIs(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("active").IsNot(true)), - String: "SELECT id FROM table WHERE (active IS NOT true)", - Query: "SELECT id FROM table WHERE (active IS NOT $1)", - NamedQuery: "SELECT id FROM table WHERE (active IS NOT :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"active\" IS NOT true)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"active\" IS NOT $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"active\" IS NOT :arg_1)", Args: []interface{}{true}, }, }) @@ -731,9 +731,9 @@ func TestSelect_WhereGreaterThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").GreaterThan(2)), - String: "SELECT id FROM table WHERE (count > 2)", - Query: "SELECT id FROM table WHERE (count > $1)", - NamedQuery: "SELECT id FROM table WHERE (count > :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" > 2)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" > $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" > :arg_1)", Args: []interface{}{2}, }, { @@ -742,9 +742,9 @@ func TestSelect_WhereGreaterThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").GreaterThanOrEqual(4)), - String: "SELECT id FROM table WHERE (count >= 4)", - Query: "SELECT id FROM table WHERE (count >= $1)", - NamedQuery: "SELECT id FROM table WHERE (count >= :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" >= 4)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" >= $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" >= :arg_1)", Args: []interface{}{4}, }, { @@ -753,7 +753,7 @@ func TestSelect_WhereGreaterThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("updated_at").GreaterThanOrEqual(loukoum.Raw("NOW()"))), - SameQuery: "SELECT id FROM table WHERE (updated_at >= NOW())", + SameQuery: "SELECT \"id\" FROM \"table\" WHERE (\"updated_at\" >= NOW())", }, { Name: "Greater than subquery", @@ -765,16 +765,16 @@ func TestSelect_WhereGreaterThan(t *testing.T) { Where(loukoum.Condition("id").Equal(6598)), )), String: fmt.Sprint( - "SELECT id FROM table WHERE (counter > (SELECT counter FROM table WHERE ", - "(id = 6598)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" > (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = 6598)))", ), Query: fmt.Sprint( - "SELECT id FROM table WHERE (counter > (SELECT counter FROM table WHERE ", - "(id = $1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" > (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM table WHERE (counter > (SELECT counter FROM table WHERE ", - "(id = :arg_1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" > (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = :arg_1)))", ), Args: []interface{}{6598}, }, @@ -788,16 +788,16 @@ func TestSelect_WhereGreaterThan(t *testing.T) { Where(loukoum.Condition("id").Equal(6598)), )), String: fmt.Sprint( - "SELECT id FROM table WHERE (counter >= (SELECT counter FROM table WHERE ", - "(id = 6598)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" >= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = 6598)))", ), Query: fmt.Sprint( - "SELECT id FROM table WHERE (counter >= (SELECT counter FROM table WHERE ", - "(id = $1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" >= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM table WHERE (counter >= (SELECT counter FROM table WHERE ", - "(id = :arg_1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" >= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = :arg_1)))", ), Args: []interface{}{6598}, }, @@ -812,9 +812,9 @@ func TestSelect_WhereLessThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").LessThan(3)), - String: "SELECT id FROM table WHERE (count < 3)", - Query: "SELECT id FROM table WHERE (count < $1)", - NamedQuery: "SELECT id FROM table WHERE (count < :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" < 3)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" < $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" < :arg_1)", Args: []interface{}{3}, }, { @@ -823,9 +823,9 @@ func TestSelect_WhereLessThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").LessThanOrEqual(6)), - String: "SELECT id FROM table WHERE (count <= 6)", - Query: "SELECT id FROM table WHERE (count <= $1)", - NamedQuery: "SELECT id FROM table WHERE (count <= :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" <= 6)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" <= $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" <= :arg_1)", Args: []interface{}{6}, }, { @@ -834,7 +834,7 @@ func TestSelect_WhereLessThan(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("updated_at").LessThanOrEqual(loukoum.Raw("NOW()"))), - SameQuery: "SELECT id FROM table WHERE (updated_at <= NOW())", + SameQuery: "SELECT \"id\" FROM \"table\" WHERE (\"updated_at\" <= NOW())", }, { Name: "Less than subquery", @@ -846,16 +846,16 @@ func TestSelect_WhereLessThan(t *testing.T) { Where(loukoum.Condition("id").Equal(6598)), )), String: fmt.Sprint( - "SELECT id FROM table WHERE (counter < (SELECT counter FROM table WHERE ", - "(id = 6598)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" < (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = 6598)))", ), Query: fmt.Sprint( - "SELECT id FROM table WHERE (counter < (SELECT counter FROM table WHERE ", - "(id = $1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" < (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM table WHERE (counter < (SELECT counter FROM table WHERE ", - "(id = :arg_1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" < (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = :arg_1)))", ), Args: []interface{}{6598}, }, @@ -869,16 +869,16 @@ func TestSelect_WhereLessThan(t *testing.T) { Where(loukoum.Condition("id").Equal(6598)), )), String: fmt.Sprint( - "SELECT id FROM table WHERE (counter <= (SELECT counter FROM table WHERE ", - "(id = 6598)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" <= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = 6598)))", ), Query: fmt.Sprint( - "SELECT id FROM table WHERE (counter <= (SELECT counter FROM table WHERE ", - "(id = $1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" <= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = $1)))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM table WHERE (counter <= (SELECT counter FROM table WHERE ", - "(id = :arg_1)))", + "SELECT \"id\" FROM \"table\" WHERE (\"counter\" <= (SELECT \"counter\" FROM \"table\" WHERE ", + "(\"id\" = :arg_1)))", ), Args: []interface{}{6598}, }, @@ -893,9 +893,9 @@ func TestSelect_WhereLike(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("title").Like("foo%")), - String: "SELECT id FROM table WHERE (title LIKE 'foo%')", - Query: "SELECT id FROM table WHERE (title LIKE $1)", - NamedQuery: "SELECT id FROM table WHERE (title LIKE :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"title\" LIKE 'foo%')", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"title\" LIKE $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"title\" LIKE :arg_1)", Args: []interface{}{"foo%"}, }, { @@ -904,9 +904,9 @@ func TestSelect_WhereLike(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("title").NotLike("foo%")), - String: "SELECT id FROM table WHERE (title NOT LIKE 'foo%')", - Query: "SELECT id FROM table WHERE (title NOT LIKE $1)", - NamedQuery: "SELECT id FROM table WHERE (title NOT LIKE :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT LIKE 'foo%')", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT LIKE $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT LIKE :arg_1)", Args: []interface{}{"foo%"}, }, { @@ -915,9 +915,9 @@ func TestSelect_WhereLike(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("title").ILike("foo%")), - String: "SELECT id FROM table WHERE (title ILIKE 'foo%')", - Query: "SELECT id FROM table WHERE (title ILIKE $1)", - NamedQuery: "SELECT id FROM table WHERE (title ILIKE :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"title\" ILIKE 'foo%')", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"title\" ILIKE $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"title\" ILIKE :arg_1)", Args: []interface{}{"foo%"}, }, { @@ -926,9 +926,9 @@ func TestSelect_WhereLike(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("title").NotILike("foo%")), - String: "SELECT id FROM table WHERE (title NOT ILIKE 'foo%')", - Query: "SELECT id FROM table WHERE (title NOT ILIKE $1)", - NamedQuery: "SELECT id FROM table WHERE (title NOT ILIKE :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT ILIKE 'foo%')", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT ILIKE $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"title\" NOT ILIKE :arg_1)", Args: []interface{}{"foo%"}, }, }) @@ -942,9 +942,9 @@ func TestSelect_WhereBetween(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").Between(10, 20)), - String: "SELECT id FROM table WHERE (count BETWEEN 10 AND 20)", - Query: "SELECT id FROM table WHERE (count BETWEEN $1 AND $2)", - NamedQuery: "SELECT id FROM table WHERE (count BETWEEN :arg_1 AND :arg_2)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" BETWEEN 10 AND 20)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" BETWEEN $1 AND $2)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" BETWEEN :arg_1 AND :arg_2)", Args: []interface{}{10, 20}, }, { @@ -953,9 +953,9 @@ func TestSelect_WhereBetween(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("count").NotBetween(50, 70)), - String: "SELECT id FROM table WHERE (count NOT BETWEEN 50 AND 70)", - Query: "SELECT id FROM table WHERE (count NOT BETWEEN $1 AND $2)", - NamedQuery: "SELECT id FROM table WHERE (count NOT BETWEEN :arg_1 AND :arg_2)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"count\" NOT BETWEEN 50 AND 70)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"count\" NOT BETWEEN $1 AND $2)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"count\" NOT BETWEEN :arg_1 AND :arg_2)", Args: []interface{}{50, 70}, }, }) @@ -969,9 +969,9 @@ func TestSelect_WhereIsDistinctFrom(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("col").IsDistinctFrom(10)), - String: "SELECT id FROM table WHERE (col IS DISTINCT FROM 10)", - Query: "SELECT id FROM table WHERE (col IS DISTINCT FROM $1)", - NamedQuery: "SELECT id FROM table WHERE (col IS DISTINCT FROM :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS DISTINCT FROM 10)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS DISTINCT FROM $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS DISTINCT FROM :arg_1)", Args: []interface{}{10}, }, { @@ -980,9 +980,9 @@ func TestSelect_WhereIsDistinctFrom(t *testing.T) { Select("id"). From("table"). Where(loukoum.Condition("col").IsNotDistinctFrom(10)), - String: "SELECT id FROM table WHERE (col IS NOT DISTINCT FROM 10)", - Query: "SELECT id FROM table WHERE (col IS NOT DISTINCT FROM $1)", - NamedQuery: "SELECT id FROM table WHERE (col IS NOT DISTINCT FROM :arg_1)", + String: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS NOT DISTINCT FROM 10)", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS NOT DISTINCT FROM $1)", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"col\" IS NOT DISTINCT FROM :arg_1)", Args: []interface{}{10}, }, }) @@ -1002,9 +1002,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("id").In(int64(1), int64(2), int64(3))), }, - String: "SELECT id FROM table WHERE (id IN (1, 2, 3))", - Query: "SELECT id FROM table WHERE (id IN ($1, $2, $3))", - NamedQuery: "SELECT id FROM table WHERE (id IN (:arg_1, :arg_2, :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN (1, 2, 3))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN ($1, $2, $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN (:arg_1, :arg_2, :arg_3))", Args: []interface{}{int64(1), int64(2), int64(3)}, }, { @@ -1019,9 +1019,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("id").NotIn(1, 2, 3)), }, - String: "SELECT id FROM table WHERE (id NOT IN (1, 2, 3))", - Query: "SELECT id FROM table WHERE (id NOT IN ($1, $2, $3))", - NamedQuery: "SELECT id FROM table WHERE (id NOT IN (:arg_1, :arg_2, :arg_3))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN (1, 2, 3))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN ($1, $2, $3))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN (:arg_1, :arg_2, :arg_3))", Args: []interface{}{1, 2, 3}, }, { @@ -1036,9 +1036,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("status").In("read", "unread")), }, - String: "SELECT id FROM table WHERE (status IN ('read', 'unread'))", - Query: "SELECT id FROM table WHERE (status IN ($1, $2))", - NamedQuery: "SELECT id FROM table WHERE (status IN (:arg_1, :arg_2))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN ('read', 'unread'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN ($1, $2))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN (:arg_1, :arg_2))", Args: []interface{}{"read", "unread"}, }, { @@ -1053,9 +1053,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("status").NotIn("read", "unread")), }, - String: "SELECT id FROM table WHERE (status NOT IN ('read', 'unread'))", - Query: "SELECT id FROM table WHERE (status NOT IN ($1, $2))", - NamedQuery: "SELECT id FROM table WHERE (status NOT IN (:arg_1, :arg_2))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN ('read', 'unread'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN ($1, $2))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN (:arg_1, :arg_2))", Args: []interface{}{"read", "unread"}, }, { @@ -1070,9 +1070,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("status").In([]string{"read"})), }, - String: "SELECT id FROM table WHERE (status IN ('read'))", - Query: "SELECT id FROM table WHERE (status IN ($1))", - NamedQuery: "SELECT id FROM table WHERE (status IN (:arg_1))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN ('read'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN ($1))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"status\" IN (:arg_1))", Args: []interface{}{"read"}, }, { @@ -1087,16 +1087,16 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("status").NotIn([]string{"read"})), }, - String: "SELECT id FROM table WHERE (status NOT IN ('read'))", - Query: "SELECT id FROM table WHERE (status NOT IN ($1))", - NamedQuery: "SELECT id FROM table WHERE (status NOT IN (:arg_1))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN ('read'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN ($1))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"status\" NOT IN (:arg_1))", Args: []interface{}{"read"}, }, { Name: "In Raw", Builder: loukoum.Select("name").From("users"). Where(loukoum.Condition("id").In(loukoum.Raw("?"))), - SameQuery: "SELECT name FROM users WHERE (id IN (?))", + SameQuery: "SELECT \"name\" FROM \"users\" WHERE (\"id\" IN (?))", }, { Name: "In subquery", @@ -1108,9 +1108,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("id").Equal(1)), )), - String: "SELECT id FROM table WHERE (id IN (SELECT id FROM table WHERE (id = 1)))", - Query: "SELECT id FROM table WHERE (id IN (SELECT id FROM table WHERE (id = $1)))", - NamedQuery: "SELECT id FROM table WHERE (id IN (SELECT id FROM table WHERE (id = :arg_1)))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = 1)))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = $1)))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = :arg_1)))", Args: []interface{}{1}, }, { @@ -1123,9 +1123,9 @@ func TestSelect_WhereIn(t *testing.T) { From("table"). Where(loukoum.Condition("id").Equal(1)), )), - String: "SELECT id FROM table WHERE (id NOT IN (SELECT id FROM table WHERE (id = 1)))", - Query: "SELECT id FROM table WHERE (id NOT IN (SELECT id FROM table WHERE (id = $1)))", - NamedQuery: "SELECT id FROM table WHERE (id NOT IN (SELECT id FROM table WHERE (id = :arg_1)))", + String: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = 1)))", + Query: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = $1)))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" NOT IN (SELECT \"id\" FROM \"table\" WHERE (\"id\" = :arg_1)))", //nolint:lll Args: []interface{}{1}, }, { @@ -1134,7 +1134,7 @@ func TestSelect_WhereIn(t *testing.T) { Select("id").From("table").Where( loukoum.Condition("id").In([]int{}), ), - SameQuery: "SELECT id FROM table WHERE (id IN ())", + SameQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN ())", }, { Name: "In nil slice", @@ -1142,7 +1142,7 @@ func TestSelect_WhereIn(t *testing.T) { Select("id").From("table").Where( loukoum.Condition("id").In(nil), ), - SameQuery: "SELECT id FROM table WHERE (id IN ())", + SameQuery: "SELECT \"id\" FROM \"table\" WHERE (\"id\" IN ())", }, { Name: "Call In", @@ -1153,9 +1153,9 @@ func TestSelect_WhereIn(t *testing.T) { "FOO@EXAMPLE.ORG", "BAR@EXAMPLE.ORG", })), - String: "SELECT id FROM table WHERE (upper(email) IN ('FOO@EXAMPLE.ORG', 'BAR@EXAMPLE.ORG'))", - Query: "SELECT id FROM table WHERE (upper(email) IN ($1, $2))", - NamedQuery: "SELECT id FROM table WHERE (upper(email) IN (:arg_1, :arg_2))", + String: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") IN ('FOO@EXAMPLE.ORG', 'BAR@EXAMPLE.ORG'))", + Query: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") IN ($1, $2))", + NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") IN (:arg_1, :arg_2))", Args: []interface{}{ "FOO@EXAMPLE.ORG", "BAR@EXAMPLE.ORG", @@ -1172,20 +1172,20 @@ func TestSelect_Exists(t *testing.T) { Select("id"). From("users"). Where(loukoum.Condition("deleted_at").IsNull(true)). - And(loukoum.Exists(loukoum.Select("1").From("news").Where( + And(loukoum.Exists(loukoum.Select(loukoum.Raw("1")).From("news").Where( loukoum.Condition("news.created_at").GreaterThan(2)), )), String: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(EXISTS (SELECT 1 FROM news WHERE (news.created_at > 2))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > 2))))", ), Query: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(EXISTS (SELECT 1 FROM news WHERE (news.created_at > $1))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > $1))))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(EXISTS (SELECT 1 FROM news WHERE (news.created_at > :arg_1))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > :arg_1))))", ), Args: []interface{}{2}, }, @@ -1200,20 +1200,20 @@ func TestSelect_NotExists(t *testing.T) { Select("id"). From("users"). Where(loukoum.Condition("deleted_at").IsNull(true)). - And(loukoum.NotExists(loukoum.Select("1").From("news").Where( + And(loukoum.NotExists(loukoum.Select(loukoum.Raw("1")).From("news").Where( loukoum.Condition("news.created_at").GreaterThan(2)), )), String: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(NOT EXISTS (SELECT 1 FROM news WHERE (news.created_at > 2))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(NOT EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > 2))))", ), Query: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(NOT EXISTS (SELECT 1 FROM news WHERE (news.created_at > $1))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(NOT EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > $1))))", ), NamedQuery: fmt.Sprint( - "SELECT id FROM users WHERE ((deleted_at IS NULL) AND ", - "(NOT EXISTS (SELECT 1 FROM news WHERE (news.created_at > :arg_1))))", + "SELECT \"id\" FROM \"users\" WHERE ((\"deleted_at\" IS NULL) AND ", + "(NOT EXISTS (SELECT 1 FROM \"news\" WHERE (\"news\".\"created_at\" > :arg_1))))", ), Args: []interface{}{2}, }, @@ -1226,54 +1226,54 @@ func TestSelect_GroupBy(t *testing.T) { Name: "One column", Builders: []builder.Builder{ loukoum. - Select("name", "COUNT(*)"). + Select("name", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy("name"), loukoum. - Select("name", "COUNT(*)"). + Select("name", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy(loukoum.Column("name")), }, - SameQuery: "SELECT name, COUNT(*) FROM user WHERE (disabled IS NOT NULL) GROUP BY name", + SameQuery: "SELECT \"name\", COUNT(*) FROM \"user\" WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\"", }, { Name: "Two columns", Builders: []builder.Builder{ loukoum. - Select("name", "locale", "COUNT(*)"). + Select("name", "locale", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy("name", "locale"), loukoum. - Select("name", "locale", "COUNT(*)"). + Select("name", "locale", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy(loukoum.Column("name"), loukoum.Column("locale")), }, SameQuery: fmt.Sprint( - "SELECT name, locale, COUNT(*) FROM user ", - "WHERE (disabled IS NOT NULL) GROUP BY name, locale", + "SELECT \"name\", \"locale\", COUNT(*) FROM \"user\" ", + "WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\", \"locale\"", ), }, { Name: "Three columns", Builders: []builder.Builder{ loukoum. - Select("name", "locale", "country", "COUNT(*)"). + Select("name", "locale", "country", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy("name", "locale", "country"), loukoum. - Select("name", "locale", "country", "COUNT(*)"). + Select("name", "locale", "country", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy(loukoum.Column("name"), loukoum.Column("locale"), loukoum.Column("country")), }, SameQuery: fmt.Sprint( - "SELECT name, locale, country, COUNT(*) FROM user ", - "WHERE (disabled IS NOT NULL) GROUP BY name, locale, country", + "SELECT \"name\", \"locale\", \"country\", COUNT(*) FROM \"user\" ", + "WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\", \"locale\", \"country\"", ), }, }) @@ -1284,45 +1284,45 @@ func TestSelect_Having(t *testing.T) { { Name: "One condition", Builder: loukoum. - Select("name", "COUNT(*)"). + Select("name", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy("name"). - Having(loukoum.Condition("COUNT(*)").GreaterThan(10)), + Having(stmt.NewCall("COUNT", loukoum.Raw("*")).GreaterThan(10)), String: fmt.Sprint( - "SELECT name, COUNT(*) FROM user ", - "WHERE (disabled IS NOT NULL) GROUP BY name HAVING (COUNT(*) > 10)", + "SELECT \"name\", COUNT(*) FROM \"user\" ", + "WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" HAVING (COUNT(*) > 10)", ), Query: fmt.Sprint( - "SELECT name, COUNT(*) FROM user ", - "WHERE (disabled IS NOT NULL) GROUP BY name HAVING (COUNT(*) > $1)", + "SELECT \"name\", COUNT(*) FROM \"user\" ", + "WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" HAVING (COUNT(*) > $1)", ), NamedQuery: fmt.Sprint( - "SELECT name, COUNT(*) FROM user ", - "WHERE (disabled IS NOT NULL) GROUP BY name HAVING (COUNT(*) > :arg_1)", + "SELECT \"name\", COUNT(*) FROM \"user\" ", + "WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" HAVING (COUNT(*) > :arg_1)", ), Args: []interface{}{10}, }, { Name: "Two conditions", Builder: loukoum. - Select("name", "COUNT(*)"). + Select("name", loukoum.Raw("COUNT(*)")). From("user"). Where(loukoum.Condition("disabled").IsNull(false)). GroupBy("name"). Having( - loukoum.Condition("COUNT(*)").GreaterThan(10).And(loukoum.Condition("COUNT(*)").LessThan(500)), + stmt.NewCall("COUNT", loukoum.Raw("*")).GreaterThan(10).And(stmt.NewCall("COUNT", loukoum.Raw("*")).LessThan(500)), ), String: fmt.Sprint( - "SELECT name, COUNT(*) FROM user WHERE (disabled IS NOT NULL) GROUP BY name ", + "SELECT \"name\", COUNT(*) FROM \"user\" WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" ", "HAVING ((COUNT(*) > 10) AND (COUNT(*) < 500))", ), Query: fmt.Sprint( - "SELECT name, COUNT(*) FROM user WHERE (disabled IS NOT NULL) GROUP BY name ", + "SELECT \"name\", COUNT(*) FROM \"user\" WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" ", "HAVING ((COUNT(*) > $1) AND (COUNT(*) < $2))", ), NamedQuery: fmt.Sprint( - "SELECT name, COUNT(*) FROM user WHERE (disabled IS NOT NULL) GROUP BY name ", + "SELECT \"name\", COUNT(*) FROM \"user\" WHERE (\"disabled\" IS NOT NULL) GROUP BY \"name\" ", "HAVING ((COUNT(*) > :arg_1) AND (COUNT(*) < :arg_2))", ), Args: []interface{}{10, 500}, @@ -1348,7 +1348,7 @@ func TestSelect_OrderBy(t *testing.T) { From("user"). OrderBy(loukoum.Column("id").Asc()), }, - SameQuery: "SELECT name FROM user ORDER BY id ASC", + SameQuery: "SELECT \"name\" FROM \"user\" ORDER BY id ASC", }, { Name: "With order desc", @@ -1362,7 +1362,7 @@ func TestSelect_OrderBy(t *testing.T) { From("user"). OrderBy(loukoum.Column("id").Desc()), }, - SameQuery: "SELECT name FROM user ORDER BY id DESC", + SameQuery: "SELECT \"name\" FROM \"user\" ORDER BY id DESC", }, { Name: "With order desc and asc", @@ -1386,7 +1386,7 @@ func TestSelect_OrderBy(t *testing.T) { OrderBy(loukoum.Column("locale").Asc()). OrderBy(loukoum.Column("id").Desc()), }, - SameQuery: "SELECT name FROM user ORDER BY locale ASC, id DESC", + SameQuery: "SELECT \"name\" FROM \"user\" ORDER BY locale ASC, id DESC", }, }) } @@ -1399,7 +1399,7 @@ func TestSelect_Limit(t *testing.T) { Select("name"). From("user"). Limit(10), - SameQuery: "SELECT name FROM user LIMIT 10", + SameQuery: "SELECT \"name\" FROM \"user\" LIMIT 10", }, { Name: "string 50", @@ -1407,7 +1407,7 @@ func TestSelect_Limit(t *testing.T) { Select("name"). From("user"). Limit("50"), - SameQuery: "SELECT name FROM user LIMIT 50", + SameQuery: "SELECT \"name\" FROM \"user\" LIMIT 50", }, { Name: "uint64 700", @@ -1415,7 +1415,7 @@ func TestSelect_Limit(t *testing.T) { Select("name"). From("user"). Limit(uint64(700)), - SameQuery: "SELECT name FROM user LIMIT 700", + SameQuery: "SELECT \"name\" FROM \"user\" LIMIT 700", }, { Name: "Corner case 0", @@ -1446,7 +1446,7 @@ func TestSelect_Offset(t *testing.T) { Select("name"). From("user"). Offset(10), - SameQuery: "SELECT name FROM user OFFSET 10", + SameQuery: "SELECT \"name\" FROM \"user\" OFFSET 10", }, { Name: "string 50", @@ -1454,7 +1454,7 @@ func TestSelect_Offset(t *testing.T) { Select("name"). From("user"). Offset("50"), - SameQuery: "SELECT name FROM user OFFSET 50", + SameQuery: "SELECT \"name\" FROM \"user\" OFFSET 50", }, { Name: "uint64 700", @@ -1462,7 +1462,7 @@ func TestSelect_Offset(t *testing.T) { Select("name"). From("user"). Offset(uint64(700)), - SameQuery: "SELECT name FROM user OFFSET 700", + SameQuery: "SELECT \"name\" FROM \"user\" OFFSET 700", }, { Name: "Corner case 0", @@ -1490,27 +1490,27 @@ func TestSelect_With(t *testing.T) { { Name: "Count with simple with statement", Builder: loukoum. - Select("AVG(COUNT)"). + Select(loukoum.Raw("AVG(COUNT)")). From("members"). With(loukoum.With("members", - loukoum.Select("COUNT(*)"). + loukoum.Select(loukoum.Raw("COUNT(*)")). From("table"). Where(loukoum.Condition("deleted_at").IsNull(true)). GroupBy("group_id"), )), SameQuery: fmt.Sprint( - "WITH members AS (SELECT COUNT(*) FROM table WHERE (deleted_at IS NULL) GROUP BY group_id) ", - "SELECT AVG(COUNT) FROM members", + "WITH members AS (SELECT COUNT(*) FROM \"table\" WHERE (\"deleted_at\" IS NULL) GROUP BY \"group_id\") ", + "SELECT AVG(COUNT) FROM \"members\"", ), }, { Name: "Sum with simple with statement", Builder: loukoum. - Select("SUM(project.amount_raised - withdrawn.amount)"). + Select(loukoum.Raw("SUM(project.amount_raised - withdrawn.amount)")). From("project"). Join("withdrawn", loukoum.On("withdrawn.project_id", "project.id"), loukoum.LeftJoin). With(loukoum.With("withdrawn", - loukoum.Select("SUM(amount) AS amount", "project_id"). + loukoum.Select(loukoum.Raw("SUM(amount) AS amount"), "project_id"). From("withdrawal"). GroupBy("project_id"), )). @@ -1518,37 +1518,37 @@ func TestSelect_With(t *testing.T) { Where(loukoum.Condition("project.deleted_at").IsNull(true)). Where(loukoum.Condition("project.amount_raised").GreaterThan(loukoum.Raw("withdrawn.amount"))), String: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id WHERE (((project.amount_raised > 0) ", - "AND (project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" WHERE (((\"project\".\"amount_raised\" > 0) ", //nolint:lll + "AND (\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), Query: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id WHERE (((project.amount_raised > $1) ", - "AND (project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" WHERE (((\"project\".\"amount_raised\" > $1) ", //nolint:lll + "AND (\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), NamedQuery: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id WHERE (((project.amount_raised > :arg_1) ", - "AND (project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" WHERE (((\"project\".\"amount_raised\" > :arg_1) ", //nolint:lll + "AND (\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), Args: []interface{}{0}, }, { Name: "Multiple with statement", Builder: loukoum. - Select("SUM(project.amount_raised - withdrawn.amount)"). + Select(loukoum.Raw("SUM(project.amount_raised - withdrawn.amount)")). From("project"). With(loukoum.With("withdrawn", - loukoum.Select("SUM(amount) AS amount", "project_id"). + loukoum.Select(loukoum.Raw("SUM(amount) AS amount"), "project_id"). From("withdrawal"). GroupBy("project_id"), )). With(loukoum.With("contributions", - loukoum.Select("COUNT(*) AS count", "project_id"). + loukoum.Select(loukoum.Raw("COUNT(*) AS count"), "project_id"). From("contribution"). GroupBy("project_id"), )). @@ -1559,31 +1559,31 @@ func TestSelect_With(t *testing.T) { Where(loukoum.Condition("project.deleted_at").IsNull(true)). Where(loukoum.Condition("project.amount_raised").GreaterThan(loukoum.Raw("withdrawn.amount"))), String: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id), ", - "contributions AS (SELECT COUNT(*) AS count, project_id FROM contribution GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id ", - "LEFT JOIN contributions ON contributions.project_id = project.id ", - "WHERE ((((project.amount_raised > 0) AND (contributions.count > 10)) AND ", - "(project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\"), ", + "contributions AS (SELECT COUNT(*) AS count, \"project_id\" FROM \"contribution\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" ", + "LEFT JOIN \"contributions\" ON \"contributions\".\"project_id\" = \"project\".\"id\" ", + "WHERE ((((\"project\".\"amount_raised\" > 0) AND (\"contributions\".\"count\" > 10)) AND ", + "(\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), Query: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id), ", - "contributions AS (SELECT COUNT(*) AS count, project_id FROM contribution GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id ", - "LEFT JOIN contributions ON contributions.project_id = project.id ", - "WHERE ((((project.amount_raised > $1) AND (contributions.count > $2)) AND ", - "(project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\"), ", + "contributions AS (SELECT COUNT(*) AS count, \"project_id\" FROM \"contribution\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" ", + "LEFT JOIN \"contributions\" ON \"contributions\".\"project_id\" = \"project\".\"id\" ", + "WHERE ((((\"project\".\"amount_raised\" > $1) AND (\"contributions\".\"count\" > $2)) AND ", + "(\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), NamedQuery: fmt.Sprint( - "WITH withdrawn AS (SELECT SUM(amount) AS amount, project_id FROM withdrawal GROUP BY project_id), ", - "contributions AS (SELECT COUNT(*) AS count, project_id FROM contribution GROUP BY project_id) ", - "SELECT SUM(project.amount_raised - withdrawn.amount) FROM project ", - "LEFT JOIN withdrawn ON withdrawn.project_id = project.id ", - "LEFT JOIN contributions ON contributions.project_id = project.id ", - "WHERE ((((project.amount_raised > :arg_1) AND (contributions.count > :arg_2)) AND ", - "(project.deleted_at IS NULL)) AND (project.amount_raised > withdrawn.amount))", + "WITH withdrawn AS (SELECT SUM(amount) AS amount, \"project_id\" FROM \"withdrawal\" GROUP BY \"project_id\"), ", + "contributions AS (SELECT COUNT(*) AS count, \"project_id\" FROM \"contribution\" GROUP BY \"project_id\") ", + "SELECT SUM(project.amount_raised - withdrawn.amount) FROM \"project\" ", + "LEFT JOIN \"withdrawn\" ON \"withdrawn\".\"project_id\" = \"project\".\"id\" ", + "LEFT JOIN \"contributions\" ON \"contributions\".\"project_id\" = \"project\".\"id\" ", + "WHERE ((((\"project\".\"amount_raised\" > :arg_1) AND (\"contributions\".\"count\" > :arg_2)) AND ", + "(\"project\".\"deleted_at\" IS NULL)) AND (\"project\".\"amount_raised\" > withdrawn.amount))", ), Args: []interface{}{0, 10}, }, @@ -1598,7 +1598,7 @@ func TestSelect_Extra(t *testing.T) { Select("name"). From("user"). Prefix("EXPLAIN ANALYZE"), - SameQuery: "EXPLAIN ANALYZE SELECT name FROM user", + SameQuery: "EXPLAIN ANALYZE SELECT \"name\" FROM \"user\"", }, { Name: "Suffix", @@ -1606,7 +1606,7 @@ func TestSelect_Extra(t *testing.T) { Select("name"). From("user"). Suffix("FOR UPDATE"), - SameQuery: "SELECT name FROM user FOR UPDATE", + SameQuery: "SELECT \"name\" FROM \"user\" FOR UPDATE", }, }) } diff --git a/builder/update.go b/builder/update.go index 399f34c..f95bfe6 100644 --- a/builder/update.go +++ b/builder/update.go @@ -101,7 +101,7 @@ func (b Update) Returning(values ...interface{}) Update { panic("loukoum: update builder has returning clause already defined") } - b.query.Returning = stmt.NewReturning(ToColumns(values)) + b.query.Returning = stmt.NewReturning(ToSelectExpressions(values)) return b } diff --git a/builder/update_test.go b/builder/update_test.go index 1040411..da581c2 100644 --- a/builder/update_test.go +++ b/builder/update_test.go @@ -40,9 +40,9 @@ func TestUpdate_Comment(t *testing.T) { Where(loukoum.Condition("username"). Equal("thoas")). Comment("/foo"), - String: "UPDATE users SET username = 'novln' WHERE (username = 'thoas'); -- /foo", - Query: "UPDATE users SET username = $1 WHERE (username = $2); -- /foo", - NamedQuery: "UPDATE users SET username = :arg_1 WHERE (username = :arg_2); -- /foo", + String: "UPDATE \"users\" SET \"username\" = 'novln' WHERE (\"username\" = 'thoas'); -- /foo", + Query: "UPDATE \"users\" SET \"username\" = $1 WHERE (\"username\" = $2); -- /foo", + NamedQuery: "UPDATE \"users\" SET \"username\" = :arg_1 WHERE (\"username\" = :arg_2); -- /foo", Args: []interface{}{"novln", "thoas"}, }, }) @@ -66,9 +66,9 @@ func TestUpdate_Set_Map(t *testing.T) { map[string]interface{}{"c": 3, "d": 4}, ), }, - String: "UPDATE table SET a = 1, b = 2, c = 3, d = 4", - Query: "UPDATE table SET a = $1, b = $2, c = $3, d = $4", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3, d = :arg_4", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2, \"c\" = 3, \"d\" = 4", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3, \"d\" = $4", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3, \"d\" = :arg_4", Args: []interface{}{1, 2, 3, 4}, }, { @@ -76,9 +76,9 @@ func TestUpdate_Set_Map(t *testing.T) { Builder: loukoum. Update("table"). Set(loukoum.Map{loukoum.Column("foo"): 2, "a": 1}), - String: "UPDATE table SET a = 1, foo = 2", - Query: "UPDATE table SET a = $1, foo = $2", - NamedQuery: "UPDATE table SET a = :arg_1, foo = :arg_2", + String: "UPDATE \"table\" SET \"a\" = 1, \"foo\" = 2", + Query: "UPDATE \"table\" SET \"a\" = $1, \"foo\" = $2", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"foo\" = :arg_2", Args: []interface{}{1, 2}, }, { @@ -101,9 +101,9 @@ func TestUpdate_Set_Map(t *testing.T) { Set(loukoum.Map{"c": 3}). Set(map[string]interface{}{"d": 4, "e": 5}), }, - String: "UPDATE table SET a = 1, b = 2, c = 3, d = 4, e = 5", - Query: "UPDATE table SET a = $1, b = $2, c = $3, d = $4, e = $5", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3, d = :arg_4, e = :arg_5", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2, \"c\" = 3, \"d\" = 4, \"e\" = 5", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3, \"d\" = $4, \"e\" = $5", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3, \"d\" = :arg_4, \"e\" = :arg_5", Args: []interface{}{1, 2, 3, 4, 5}, }, }) @@ -139,9 +139,9 @@ func TestUpdate_Set_Map_Uniqueness(t *testing.T) { Set(loukoum.Map{"a": 1, "b": 2}). Set(map[string]interface{}{"b": 2}), }, - String: "UPDATE table SET a = 1, b = 2", - Query: "UPDATE table SET a = $1, b = $2", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2", Args: []interface{}{1, 2}, }, }) @@ -178,9 +178,9 @@ func TestUpdate_Set_Map_LastWriteWins(t *testing.T) { Set(loukoum.Map{"a": 1, "b": 2}). Set(map[string]interface{}{"a": 3}), }, - String: "UPDATE table SET a = 3, b = 2", - Query: "UPDATE table SET a = $1, b = $2", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2", + String: "UPDATE \"table\" SET \"a\" = 3, \"b\" = 2", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2", Args: []interface{}{3, 2}, }, }) @@ -191,9 +191,9 @@ func TestUpdate_Set_Pair(t *testing.T) { { Name: "Single Pair", Builder: loukoum.Update("table").Set(loukoum.Pair("a", 1)), - String: "UPDATE table SET a = 1", - Query: "UPDATE table SET a = $1", - NamedQuery: "UPDATE table SET a = :arg_1", + String: "UPDATE \"table\" SET \"a\" = 1", + Query: "UPDATE \"table\" SET \"a\" = $1", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1", Args: []interface{}{1}, }, { @@ -216,9 +216,9 @@ func TestUpdate_Set_Pair(t *testing.T) { loukoum.Pair("b", 2), ), }, - String: "UPDATE table SET a = 1, b = 2", - Query: "UPDATE table SET a = $1, b = $2", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2", Args: []interface{}{1, 2}, }, }) @@ -240,9 +240,9 @@ func TestUpdate_Set_Pair_Uniqueness(t *testing.T) { Set(loukoum.Pair("a", 1), loukoum.Pair("a", 1)). Set(loukoum.Pair("a", 1), loukoum.Pair("a", 1)), }, - String: "UPDATE table SET a = 1", - Query: "UPDATE table SET a = $1", - NamedQuery: "UPDATE table SET a = :arg_1", + String: "UPDATE \"table\" SET \"a\" = 1", + Query: "UPDATE \"table\" SET \"a\" = $1", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1", Args: []interface{}{1}, }, { @@ -263,9 +263,9 @@ func TestUpdate_Set_Pair_Uniqueness(t *testing.T) { Set(loukoum.Pair("b", 5), loukoum.Pair("c", 3)). Set(loukoum.Pair("a", 4)), }, - String: "UPDATE table SET a = 4, b = 5, c = 3", - Query: "UPDATE table SET a = $1, b = $2, c = $3", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3", + String: "UPDATE \"table\" SET \"a\" = 4, \"b\" = 5, \"c\" = 3", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3", Args: []interface{}{4, 5, 3}, }, }) @@ -291,9 +291,9 @@ func TestUpdate_Set_Pair_LastWriteWins(t *testing.T) { Set(loukoum.Pair("b", 5), loukoum.Pair("c", 3)). Set(loukoum.Pair("a", 4)), }, - String: "UPDATE table SET a = 4, b = 5, c = 3", - Query: "UPDATE table SET a = $1, b = $2, c = $3", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3", + String: "UPDATE \"table\" SET \"a\" = 4, \"b\" = 5, \"c\" = 3", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3", Args: []interface{}{4, 5, 3}, }, }) @@ -317,9 +317,9 @@ func TestUpdate_Set_MapAndPair(t *testing.T) { Set(loukoum.Map{"a": 1, "b": 2}, loukoum.Pair("d", 4)). Set(loukoum.Map{"c": 3}, loukoum.Pair("e", 5)), }, - String: "UPDATE table SET a = 1, b = 2, c = 3, d = 4, e = 5", - Query: "UPDATE table SET a = $1, b = $2, c = $3, d = $4, e = $5", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3, d = :arg_4, e = :arg_5", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2, \"c\" = 3, \"d\" = 4, \"e\" = 5", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3, \"d\" = $4, \"e\" = $5", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3, \"d\" = :arg_4, \"e\" = :arg_5", Args: []interface{}{1, 2, 3, 4, 5}, }, { @@ -327,9 +327,9 @@ func TestUpdate_Set_MapAndPair(t *testing.T) { Builder: loukoum. Update("table"). Set(loukoum.Map{loukoum.Column("foo"): 2, "a": 1}), - String: "UPDATE table SET a = 1, foo = 2", - Query: "UPDATE table SET a = $1, foo = $2", - NamedQuery: "UPDATE table SET a = :arg_1, foo = :arg_2", + String: "UPDATE \"table\" SET \"a\" = 1, \"foo\" = 2", + Query: "UPDATE \"table\" SET \"a\" = $1, \"foo\" = $2", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"foo\" = :arg_2", Args: []interface{}{1, 2}, }, }) @@ -354,9 +354,9 @@ func TestUpdate_Set_MapAndPair_Uniqueness(t *testing.T) { Set(loukoum.Map{"b": 2}, loukoum.Pair("b", 2)). Set(loukoum.Map{"c": 3}, loukoum.Pair("c", 3)), }, - String: "UPDATE table SET a = 1, b = 2, c = 3", - Query: "UPDATE table SET a = $1, b = $2, c = $3", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3", + String: "UPDATE \"table\" SET \"a\" = 1, \"b\" = 2, \"c\" = 3", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3", Args: []interface{}{1, 2, 3}, }, }) @@ -381,9 +381,9 @@ func TestUpdate_Set_MapAndPair_LastWriteWins(t *testing.T) { Set(loukoum.Map{"b": 2}, loukoum.Pair("b", 3)). Set(loukoum.Map{"c": 3}, loukoum.Pair("c", 4)), }, - String: "UPDATE table SET a = 2, b = 3, c = 4", - Query: "UPDATE table SET a = $1, b = $2, c = $3", - NamedQuery: "UPDATE table SET a = :arg_1, b = :arg_2, c = :arg_3", + String: "UPDATE \"table\" SET \"a\" = 2, \"b\" = 3, \"c\" = 4", + Query: "UPDATE \"table\" SET \"a\" = $1, \"b\" = $2, \"c\" = $3", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1, \"b\" = :arg_2, \"c\" = :arg_3", Args: []interface{}{2, 3, 4}, }, }) @@ -398,49 +398,49 @@ func TestUpdate_Set_Valuer(t *testing.T) { { Name: "pq.NullTime not null", Builder: loukoum.Update("table").Set(loukoum.Map{"created_at": pq.NullTime{Time: when, Valid: true}}), - String: "UPDATE table SET created_at = '2017-11-23 16:47:27+00'", - Query: "UPDATE table SET created_at = $1", - NamedQuery: "UPDATE table SET created_at = :arg_1", + String: "UPDATE \"table\" SET \"created_at\" = '2017-11-23 16:47:27+00'", + Query: "UPDATE \"table\" SET \"created_at\" = $1", + NamedQuery: "UPDATE \"table\" SET \"created_at\" = :arg_1", Args: []interface{}{pq.NullTime{Time: when, Valid: true}}, }, { Name: "pq.NullTime null", Builder: loukoum.Update("table").Set(loukoum.Map{"created_at": pq.NullTime{Time: when, Valid: false}}), - String: "UPDATE table SET created_at = NULL", - Query: "UPDATE table SET created_at = $1", - NamedQuery: "UPDATE table SET created_at = :arg_1", + String: "UPDATE \"table\" SET \"created_at\" = NULL", + Query: "UPDATE \"table\" SET \"created_at\" = $1", + NamedQuery: "UPDATE \"table\" SET \"created_at\" = :arg_1", Args: []interface{}{pq.NullTime{Time: when, Valid: false}}, }, { Name: "pq.NullString not null", Builder: loukoum.Update("table").Set(loukoum.Map{"message": sql.NullString{String: "ok", Valid: true}}), - String: "UPDATE table SET message = 'ok'", - Query: "UPDATE table SET message = $1", - NamedQuery: "UPDATE table SET message = :arg_1", + String: "UPDATE \"table\" SET \"message\" = 'ok'", + Query: "UPDATE \"table\" SET \"message\" = $1", + NamedQuery: "UPDATE \"table\" SET \"message\" = :arg_1", Args: []interface{}{sql.NullString{String: "ok", Valid: true}}, }, { Name: "pq.NullString null", Builder: loukoum.Update("table").Set(loukoum.Map{"message": sql.NullString{String: "ok", Valid: false}}), - String: "UPDATE table SET message = NULL", - Query: "UPDATE table SET message = $1", - NamedQuery: "UPDATE table SET message = :arg_1", + String: "UPDATE \"table\" SET \"message\" = NULL", + Query: "UPDATE \"table\" SET \"message\" = $1", + NamedQuery: "UPDATE \"table\" SET \"message\" = :arg_1", Args: []interface{}{sql.NullString{String: "ok", Valid: false}}, }, { Name: "sql.NullInt64 not null", Builder: loukoum.Update("table").Set(loukoum.Map{"count": sql.NullInt64{Int64: 30, Valid: true}}), - String: "UPDATE table SET count = 30", - Query: "UPDATE table SET count = $1", - NamedQuery: "UPDATE table SET count = :arg_1", + String: "UPDATE \"table\" SET \"count\" = 30", + Query: "UPDATE \"table\" SET \"count\" = $1", + NamedQuery: "UPDATE \"table\" SET \"count\" = :arg_1", Args: []interface{}{sql.NullInt64{Int64: 30, Valid: true}}, }, { Name: "sql.NullInt64 null", Builder: loukoum.Update("table").Set(loukoum.Map{"count": sql.NullInt64{Int64: 30, Valid: false}}), - String: "UPDATE table SET count = NULL", - Query: "UPDATE table SET count = $1", - NamedQuery: "UPDATE table SET count = :arg_1", + String: "UPDATE \"table\" SET \"count\" = NULL", + Query: "UPDATE \"table\" SET \"count\" = $1", + NamedQuery: "UPDATE \"table\" SET \"count\" = :arg_1", Args: []interface{}{sql.NullInt64{Int64: 30, Valid: false}}, }, }) @@ -452,21 +452,21 @@ func TestUpdate_Set_Subquery(t *testing.T) { Name: "Coalesce", Builder: loukoum.Update("test1"). Set(loukoum.Pair("count", - loukoum.Select("COALESCE(COUNT(*), 0)"). + loukoum.Select(loukoum.Raw("COALESCE(COUNT(*), 0)")). From("test2"). Where(loukoum.Condition("disabled").Equal(false)), )), String: fmt.Sprint( - "UPDATE test1 SET count = (SELECT COALESCE(COUNT(*), 0)", - " FROM test2 WHERE (disabled = false))", + "UPDATE \"test1\" SET \"count\" = (SELECT COALESCE(COUNT(*), 0)", + " FROM \"test2\" WHERE (\"disabled\" = false))", ), Query: fmt.Sprint( - "UPDATE test1 SET count = (SELECT COALESCE(COUNT(*), 0)", - " FROM test2 WHERE (disabled = $1))", + "UPDATE \"test1\" SET \"count\" = (SELECT COALESCE(COUNT(*), 0)", + " FROM \"test2\" WHERE (\"disabled\" = $1))", ), NamedQuery: fmt.Sprint( - "UPDATE test1 SET count = (SELECT COALESCE(COUNT(*), 0)", - " FROM test2 WHERE (disabled = :arg_1))", + "UPDATE \"test1\" SET \"count\" = (SELECT COALESCE(COUNT(*), 0)", + " FROM \"test2\" WHERE (\"disabled\" = :arg_1))", ), Args: []interface{}{false}, }, @@ -501,9 +501,9 @@ func TestUpdate_Set_Using(t *testing.T) { Set("c"). Using("d", "e", "f"), }, - String: "UPDATE table SET (a, b, c) = ('d', 'e', 'f')", - Query: "UPDATE table SET (a, b, c) = ($1, $2, $3)", - NamedQuery: "UPDATE table SET (a, b, c) = (:arg_1, :arg_2, :arg_3)", + String: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = ('d', 'e', 'f')", + Query: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = ($1, $2, $3)", + NamedQuery: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = (:arg_1, :arg_2, :arg_3)", Args: []interface{}{"d", "e", "f"}, }, { @@ -512,7 +512,7 @@ func TestUpdate_Set_Using(t *testing.T) { Update("table"). Set("a", "b", "c"). Using(loukoum.Raw("d+1"), loukoum.Raw("e+1"), loukoum.Raw("f+1")), - SameQuery: "UPDATE table SET (a, b, c) = (d+1, e+1, f+1)", + SameQuery: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = (d+1, e+1, f+1)", }, { Name: "Sub-select", @@ -523,9 +523,9 @@ func TestUpdate_Set_Using(t *testing.T) { From("table"). Where(loukoum.Condition("disabled").Equal(false)), ), - String: "UPDATE table SET (a, b, c) = (SELECT a, b, c FROM table WHERE (disabled = false))", - Query: "UPDATE table SET (a, b, c) = (SELECT a, b, c FROM table WHERE (disabled = $1))", - NamedQuery: "UPDATE table SET (a, b, c) = (SELECT a, b, c FROM table WHERE (disabled = :arg_1))", + String: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = (SELECT \"a\", \"b\", \"c\" FROM \"table\" WHERE (\"disabled\" = false))", //nolint:lll + Query: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = (SELECT \"a\", \"b\", \"c\" FROM \"table\" WHERE (\"disabled\" = $1))", //nolint:lll + NamedQuery: "UPDATE \"table\" SET (\"a\", \"b\", \"c\") = (SELECT \"a\", \"b\", \"c\" FROM \"table\" WHERE (\"disabled\" = :arg_1))", //nolint:lll Args: []interface{}{false}, }, }) @@ -536,9 +536,9 @@ func TestUpdate_OnlyTable(t *testing.T) { { Name: "Simple", Builder: loukoum.Update("table").Only().Set(loukoum.Map{"a": 1}), - String: "UPDATE ONLY table SET a = 1", - Query: "UPDATE ONLY table SET a = $1", - NamedQuery: "UPDATE ONLY table SET a = :arg_1", + String: "UPDATE ONLY \"table\" SET \"a\" = 1", + Query: "UPDATE ONLY \"table\" SET \"a\" = $1", + NamedQuery: "UPDATE ONLY \"table\" SET \"a\" = :arg_1", Args: []interface{}{1}, }, }) @@ -552,9 +552,9 @@ func TestUpdate_Where(t *testing.T) { Update("table"). Set(loukoum.Map{"a": 1}). Where(loukoum.Condition("id").Equal(1)), - String: "UPDATE table SET a = 1 WHERE (id = 1)", - Query: "UPDATE table SET a = $1 WHERE (id = $2)", - NamedQuery: "UPDATE table SET a = :arg_1 WHERE (id = :arg_2)", + String: "UPDATE \"table\" SET \"a\" = 1 WHERE (\"id\" = 1)", + Query: "UPDATE \"table\" SET \"a\" = $1 WHERE (\"id\" = $2)", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1 WHERE (\"id\" = :arg_2)", Args: []interface{}{1, 1}, }, { @@ -564,9 +564,9 @@ func TestUpdate_Where(t *testing.T) { Set(loukoum.Map{"a": 1}). Where(loukoum.Condition("id").Equal(1)). And(loukoum.Condition("status").Equal("online")), - String: "UPDATE table SET a = 1 WHERE ((id = 1) AND (status = 'online'))", - Query: "UPDATE table SET a = $1 WHERE ((id = $2) AND (status = $3))", - NamedQuery: "UPDATE table SET a = :arg_1 WHERE ((id = :arg_2) AND (status = :arg_3))", + String: "UPDATE \"table\" SET \"a\" = 1 WHERE ((\"id\" = 1) AND (\"status\" = 'online'))", + Query: "UPDATE \"table\" SET \"a\" = $1 WHERE ((\"id\" = $2) AND (\"status\" = $3))", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1 WHERE ((\"id\" = :arg_2) AND (\"status\" = :arg_3))", Args: []interface{}{1, 1, "online"}, }, { @@ -576,9 +576,9 @@ func TestUpdate_Where(t *testing.T) { Set(loukoum.Map{"a": 1}). Where(loukoum.Condition("id").Equal(1)). Or(loukoum.Condition("status").Equal("online")), - String: "UPDATE table SET a = 1 WHERE ((id = 1) OR (status = 'online'))", - Query: "UPDATE table SET a = $1 WHERE ((id = $2) OR (status = $3))", - NamedQuery: "UPDATE table SET a = :arg_1 WHERE ((id = :arg_2) OR (status = :arg_3))", + String: "UPDATE \"table\" SET \"a\" = 1 WHERE ((\"id\" = 1) OR (\"status\" = 'online'))", + Query: "UPDATE \"table\" SET \"a\" = $1 WHERE ((\"id\" = $2) OR (\"status\" = $3))", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1 WHERE ((\"id\" = :arg_2) OR (\"status\" = :arg_3))", Args: []interface{}{1, 1, "online"}, }, }) @@ -593,9 +593,9 @@ func TestUpdate_From(t *testing.T) { Set(loukoum.Map{"a": 1}). From("table2"). Where(loukoum.Condition("table2.id").Equal(loukoum.Raw("table1.id"))), - String: "UPDATE table1 SET a = 1 FROM table2 WHERE (table2.id = table1.id)", - Query: "UPDATE table1 SET a = $1 FROM table2 WHERE (table2.id = table1.id)", - NamedQuery: "UPDATE table1 SET a = :arg_1 FROM table2 WHERE (table2.id = table1.id)", + String: "UPDATE \"table1\" SET \"a\" = 1 FROM \"table2\" WHERE (\"table2\".\"id\" = table1.id)", + Query: "UPDATE \"table1\" SET \"a\" = $1 FROM \"table2\" WHERE (\"table2\".\"id\" = table1.id)", + NamedQuery: "UPDATE \"table1\" SET \"a\" = :arg_1 FROM \"table2\" WHERE (\"table2\".\"id\" = table1.id)", Args: []interface{}{1}, }, }) @@ -608,10 +608,10 @@ func TestUpdate_Returning(t *testing.T) { Builder: loukoum. Update("table"). Set(loukoum.Map{"a": 1}). - Returning("*"), - String: "UPDATE table SET a = 1 RETURNING *", - Query: "UPDATE table SET a = $1 RETURNING *", - NamedQuery: "UPDATE table SET a = :arg_1 RETURNING *", + Returning(loukoum.Raw("*")), + String: "UPDATE \"table\" SET \"a\" = 1 RETURNING *", + Query: "UPDATE \"table\" SET \"a\" = $1 RETURNING *", + NamedQuery: "UPDATE \"table\" SET \"a\" = :arg_1 RETURNING *", Args: []interface{}{1}, }, }) @@ -632,19 +632,19 @@ func TestUpdate_With(t *testing.T) { From("contributors"). Where(loukoum.Condition("users.id").Equal(loukoum.Raw("contributors.user_id"))), String: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = true FROM contributors ", - "WHERE (users.id = contributors.user_id)", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = true FROM \"contributors\" ", + "WHERE (\"users\".\"id\" = contributors.user_id)", ), Query: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = $1 FROM contributors ", - "WHERE (users.id = contributors.user_id)", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = $1 FROM \"contributors\" ", + "WHERE (\"users\".\"id\" = contributors.user_id)", ), NamedQuery: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = :arg_1 FROM contributors ", - "WHERE (users.id = contributors.user_id)", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = :arg_1 FROM \"contributors\" ", + "WHERE (\"users\".\"id\" = contributors.user_id)", ), Args: []interface{}{true}, }, @@ -666,25 +666,25 @@ func TestUpdate_With(t *testing.T) { Where(loukoum.Condition("users.id").In(loukoum.Select("user_id").From("contributors"))). Or(loukoum.Condition("users.id").In(loukoum.Select("user_id").From("commentators"))), String: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)), ", - "commentators AS (SELECT DISTINCT user_id FROM comment WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = true ", - "WHERE ((users.id IN (SELECT user_id FROM contributors)) ", - "OR (users.id IN (SELECT user_id FROM commentators)))", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)), ", + "commentators AS (SELECT DISTINCT \"user_id\" FROM \"comment\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = true ", + "WHERE ((\"users\".\"id\" IN (SELECT \"user_id\" FROM \"contributors\")) ", + "OR (\"users\".\"id\" IN (SELECT \"user_id\" FROM \"commentators\")))", ), Query: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)), ", - "commentators AS (SELECT DISTINCT user_id FROM comment WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = $1 ", - "WHERE ((users.id IN (SELECT user_id FROM contributors)) ", - "OR (users.id IN (SELECT user_id FROM commentators)))", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)), ", + "commentators AS (SELECT DISTINCT \"user_id\" FROM \"comment\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = $1 ", + "WHERE ((\"users\".\"id\" IN (SELECT \"user_id\" FROM \"contributors\")) ", + "OR (\"users\".\"id\" IN (SELECT \"user_id\" FROM \"commentators\")))", ), NamedQuery: fmt.Sprint( - "WITH contributors AS (SELECT DISTINCT user_id FROM contribution WHERE (deleted_at IS NULL)), ", - "commentators AS (SELECT DISTINCT user_id FROM comment WHERE (deleted_at IS NULL)) ", - "UPDATE users SET newsletter_subscribed = :arg_1 ", - "WHERE ((users.id IN (SELECT user_id FROM contributors)) ", - "OR (users.id IN (SELECT user_id FROM commentators)))", + "WITH contributors AS (SELECT DISTINCT \"user_id\" FROM \"contribution\" WHERE (\"deleted_at\" IS NULL)), ", + "commentators AS (SELECT DISTINCT \"user_id\" FROM \"comment\" WHERE (\"deleted_at\" IS NULL)) ", + "UPDATE \"users\" SET \"newsletter_subscribed\" = :arg_1 ", + "WHERE ((\"users\".\"id\" IN (SELECT \"user_id\" FROM \"contributors\")) ", + "OR (\"users\".\"id\" IN (SELECT \"user_id\" FROM \"commentators\")))", ), Args: []interface{}{true}, }, diff --git a/stmt/column.go b/stmt/column.go index 6ac0e95..4ad78a8 100644 --- a/stmt/column.go +++ b/stmt/column.go @@ -52,12 +52,12 @@ func (column Column) Desc() Order { // Write exposes statement as a SQL query. func (column Column) Write(ctx types.Context) { - ctx.Write(column.Name) + ctx.Write(quote(column.Name)) if column.Alias != "" { ctx.Write(" ") ctx.Write(token.As.String()) ctx.Write(" ") - ctx.Write(column.Alias) + ctx.Write(quote(column.Alias)) } } diff --git a/stmt/expression.go b/stmt/expression.go index 35404c3..03b1110 100644 --- a/stmt/expression.go +++ b/stmt/expression.go @@ -71,7 +71,7 @@ func (Identifier) expression() {} // Write exposes statement as a SQL query. func (identifier Identifier) Write(ctx types.Context) { - ctx.Write(identifier.Identifier) + ctx.Write(quote(identifier.Identifier)) } // IsEmpty returns true if statement is undefined. @@ -419,7 +419,8 @@ func NewRaw(value string) Raw { } } -func (Raw) expression() {} +func (Raw) expression() {} +func (Raw) selectExpression() {} // Write exposes statement as a SQL query. func (raw Raw) Write(ctx types.Context) { @@ -434,6 +435,9 @@ func (raw Raw) IsEmpty() bool { // Ensure that Raw is an Expression var _ Expression = Raw{} +// Ensure that Raw is a SelectExpression +var _ SelectExpression = Raw{} + // ---------------------------------------------------------------------------- // Wrapper // ---------------------------------------------------------------------------- @@ -519,6 +523,18 @@ func (call Call) Equal(what interface{}) InfixExpression { return NewInfixExpression(call, operator, NewWrapper(NewExpression(what))) } +// GreaterThan performs a "greater than" comparison. +func (call Call) GreaterThan(value interface{}) InfixExpression { + operator := NewComparisonOperator(types.GreaterThan) + return NewInfixExpression(call, operator, NewWrapper(NewExpression(value))) +} + +// LessThan performs a "less than" comparison. +func (call Call) LessThan(value interface{}) InfixExpression { + operator := NewComparisonOperator(types.LessThan) + return NewInfixExpression(call, operator, NewWrapper(NewExpression(value))) +} + // ILike performs a "ilike" condition. func (call Call) ILike(value interface{}) InfixExpression { operator := NewComparisonOperator(types.ILike) diff --git a/stmt/on.go b/stmt/on.go index d32f543..c11dbb5 100644 --- a/stmt/on.go +++ b/stmt/on.go @@ -43,11 +43,11 @@ func (on OnClause) Or(value OnExpression) OnExpression { // Write exposes statement as a SQL query. func (on OnClause) Write(ctx types.Context) { - ctx.Write(on.Left.Name) + on.Left.Write(ctx) ctx.Write(" ") ctx.Write(token.Equals.String()) ctx.Write(" ") - ctx.Write(on.Right.Name) + on.Right.Write(ctx) } // IsEmpty returns true if statement is undefined. diff --git a/stmt/returning.go b/stmt/returning.go index 55feb53..5c94460 100644 --- a/stmt/returning.go +++ b/stmt/returning.go @@ -7,13 +7,13 @@ import ( // Returning is a RETURNING clause. type Returning struct { - Columns []Column + Columns []SelectExpression } // NewReturning returns a new Returning instance. -func NewReturning(columns []Column) Returning { +func NewReturning(exprs []SelectExpression) Returning { return Returning{ - Columns: columns, + Columns: exprs, } } diff --git a/stmt/stmt.go b/stmt/stmt.go index 58af856..bf81364 100644 --- a/stmt/stmt.go +++ b/stmt/stmt.go @@ -1,6 +1,9 @@ package stmt import ( + "strconv" + "strings" + "github.com/ulule/loukoum/v3/types" ) @@ -12,3 +15,12 @@ type Statement interface { // Write exposes statement as a SQL query. Write(ctx types.Context) } + +func quote(ident string) string { + split := strings.Split(ident, ".") + quoted := make([]string, 0, len(split)) + for i := range split { + quoted = append(quoted, strconv.Quote(split[i])) + } + return strings.Join(quoted, ".") +} diff --git a/stmt/table.go b/stmt/table.go index edf802f..f9529db 100644 --- a/stmt/table.go +++ b/stmt/table.go @@ -32,12 +32,12 @@ func (table Table) As(alias string) Table { // Write exposes statement as a SQL query. func (table Table) Write(ctx types.Context) { - ctx.Write(table.Name) + ctx.Write(quote(table.Name)) if table.Alias != "" { ctx.Write(" ") ctx.Write(token.As.String()) ctx.Write(" ") - ctx.Write(table.Alias) + ctx.Write(quote(table.Alias)) } }