From 27c29e0ac268794ed99f989ecf7883f48fa09d00 Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Mon, 9 Sep 2024 14:52:03 -0700 Subject: [PATCH 1/3] setup nix for dev --- .envrc | 1 + .gitignore | 3 ++- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 28 +++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..3550a30f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index d884bf35..f6ec06ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea src *.iml -coverage.* \ No newline at end of file +coverage.* +/.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..bddf4d3b --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1725634671, + "narHash": "sha256-v3rIhsJBOMLR8e/RNWxr828tB+WywYIoajrZKFM+0Gg=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "574d1eac1c200690e27b8eb4e24887f8df7ac27c", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..9f145a49 --- /dev/null +++ b/flake.nix @@ -0,0 +1,28 @@ +{ + description = "goqu"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let pkgs = import nixpkgs { inherit system; }; + + in { + devShells.default = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + go + golangci-lint + gotests + gomodifytags + gore + gotools + + # LSPs + gopls + ]; + }; + }); +} From 95f448a35acdd5b617d5998f5d112d9231ec312a Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Mon, 9 Sep 2024 14:52:45 -0700 Subject: [PATCH 2/3] add custom join option --- exp/exp.go | 1 + select_dataset.go | 5 +++++ sqlgen/sql_dialect_options.go | 1 + 3 files changed, 7 insertions(+) diff --git a/exp/exp.go b/exp/exp.go index 240a96a6..4395be03 100644 --- a/exp/exp.go +++ b/exp/exp.go @@ -533,6 +533,7 @@ const ( NaturalRightJoinType NaturalFullJoinType CrossJoinType + CustomJoinType UsingJoinCondType JoinConditionType = iota OnJoinCondType diff --git a/select_dataset.go b/select_dataset.go index 775c387d..2998a346 100644 --- a/select_dataset.go +++ b/select_dataset.go @@ -343,6 +343,11 @@ func (sd *SelectDataset) CrossJoin(table exp.Expression) *SelectDataset { return sd.joinTable(exp.NewUnConditionedJoinExpression(exp.CrossJoinType, table)) } +// Adds a custom join clause. See examples +func (sd *SelectDataset) CustomJoin(expression exp.Expression) *SelectDataset { + return sd.joinTable(exp.NewUnConditionedJoinExpression(exp.CustomJoinType, expression)) +} + // Joins this Datasets table with another func (sd *SelectDataset) joinTable(join exp.JoinExpression) *SelectDataset { return sd.copy(sd.clauses.JoinsAppend(join)) diff --git a/sqlgen/sql_dialect_options.go b/sqlgen/sql_dialect_options.go index a0df394e..1d140d12 100644 --- a/sqlgen/sql_dialect_options.go +++ b/sqlgen/sql_dialect_options.go @@ -547,6 +547,7 @@ func DefaultDialectOptions() *SQLDialectOptions { exp.NaturalRightJoinType: []byte(" NATURAL RIGHT JOIN "), exp.NaturalFullJoinType: []byte(" NATURAL FULL JOIN "), exp.CrossJoinType: []byte(" CROSS JOIN "), + exp.CustomJoinType: []byte(" "), // User need to fill in the join statement themselves }, TimeFormat: time.RFC3339Nano, From 32d72a1f1736606b73b1ac658200c1e6969f682d Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Mon, 9 Sep 2024 14:52:59 -0700 Subject: [PATCH 3/3] update tests --- select_dataset_example_test.go | 14 ++++++++++++-- select_dataset_test.go | 14 ++++++++++++++ sqlgen/select_sql_generator_test.go | 6 ++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/select_dataset_example_test.go b/select_dataset_example_test.go index e8d7fc2d..cb141362 100644 --- a/select_dataset_example_test.go +++ b/select_dataset_example_test.go @@ -15,7 +15,7 @@ import ( const schema = ` DROP TABLE IF EXISTS "user_role"; - DROP TABLE IF EXISTS "goqu_user"; + DROP TABLE IF EXISTS "goqu_user"; CREATE TABLE "goqu_user" ( "id" SERIAL PRIMARY KEY NOT NULL, "first_name" VARCHAR(45) NOT NULL, @@ -27,7 +27,7 @@ const schema = ` "user_id" BIGINT NOT NULL REFERENCES goqu_user(id) ON DELETE CASCADE, "name" VARCHAR(45) NOT NULL, "created" TIMESTAMP NOT NULL DEFAULT now() - ); + ); ` const defaultDBURI = "postgres://postgres:@localhost:5435/goqupostgres?sslmode=disable" @@ -968,6 +968,16 @@ func ExampleSelectDataset_CrossJoin() { // SELECT * FROM "test" CROSS JOIN (SELECT * FROM "test2" WHERE ("amount" > 0)) AS "t" } +func ExampleSelectDataset_CustomJoin() { + join := goqu.L("ARRAY JOIN tags").As("tag") + + sql, _, _ := goqu.From("test").CustomJoin(join).ToSQL() + fmt.Println(sql) + + // Output: + // SELECT * FROM "test" ARRAY JOIN tags AS tag +} + func ExampleSelectDataset_FromSelf() { sql, _, _ := goqu.From("test").FromSelf().ToSQL() fmt.Println(sql) diff --git a/select_dataset_test.go b/select_dataset_test.go index 6579cfad..a85a6172 100644 --- a/select_dataset_test.go +++ b/select_dataset_test.go @@ -625,6 +625,20 @@ func (sds *selectDatasetSuite) TestCrossJoin() { ) } +func (sds *selectDatasetSuite) TestCustomJoin() { + bd := goqu.From("test") + sds.assertCases( + selectTestCase{ + ds: bd.CustomJoin(goqu.L("ARRAY JOIN tags").As("tag")), + clauses: exp.NewSelectClauses(). + SetFrom(exp.NewColumnListExpression("test")). + JoinsAppend( + exp.NewUnConditionedJoinExpression(exp.CustomJoinType, goqu.L("ARRAY JOIN tags").As("tag")), + ), + }, + ) +} + func (sds *selectDatasetSuite) TestWhere() { w := goqu.Ex{"a": 1} w2 := goqu.Ex{"b": "c"} diff --git a/sqlgen/select_sql_generator_test.go b/sqlgen/select_sql_generator_test.go index 61ad589a..e86b0729 100644 --- a/sqlgen/select_sql_generator_test.go +++ b/sqlgen/select_sql_generator_test.go @@ -216,6 +216,7 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate_withJoin() { opts.JoinTypeLookup = map[exp.JoinType][]byte{ exp.LeftJoinType: []byte(" left join "), exp.NaturalJoinType: []byte(" natural join "), + exp.CustomJoinType: []byte(" "), } sc := exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")) @@ -224,6 +225,7 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate_withJoin() { cjo := exp.NewConditionedJoinExpression(exp.LeftJoinType, ti, exp.NewJoinOnCondition(exp.Ex{"a": "foo"})) cju := exp.NewConditionedJoinExpression(exp.LeftJoinType, ti, exp.NewJoinUsingCondition("a")) rj := exp.NewConditionedJoinExpression(exp.RightJoinType, ti, exp.NewJoinUsingCondition(exp.NewIdentifierExpression("", "", "a"))) + cj := exp.NewUnConditionedJoinExpression(exp.CustomJoinType, goqu.L("ARRAY JOIN tags").As("tag")) badJoin := exp.NewConditionedJoinExpression(exp.LeftJoinType, ti, exp.NewJoinUsingCondition()) expectedRjError := "goqu: dialect does not support RightJoinType" @@ -254,6 +256,10 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate_withJoin() { isPrepared: true, args: []interface{}{"foo"}, }, + selectTestCase{ + clause: sc.JoinsAppend(cj), + sql: `SELECT * FROM "test" ARRAY JOIN tags AS "tag"`, + }, selectTestCase{clause: sc.JoinsAppend(rj), err: expectedRjError}, selectTestCase{clause: sc.JoinsAppend(rj), err: expectedRjError, isPrepared: true},