Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable custom join statements #430

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
src
*.iml
coverage.*
coverage.*
/.direnv/
1 change: 1 addition & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ const (
NaturalRightJoinType
NaturalFullJoinType
CrossJoinType
CustomJoinType

UsingJoinCondType JoinConditionType = iota
OnJoinCondType
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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
];
};
});
}
5 changes: 5 additions & 0 deletions select_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 12 additions & 2 deletions select_dataset_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions select_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
6 changes: 6 additions & 0 deletions sqlgen/select_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"
Expand Down Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions sqlgen/sql_dialect_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down