Skip to content

Commit

Permalink
Merge pull request #2 from doug-martin/master
Browse files Browse the repository at this point in the history
sync from origin
  • Loading branch information
XIELongDragon authored Oct 17, 2021
2 parents 90a0aeb + 31d438d commit e40b45c
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 1 deletion.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v9.18.0
* [FEATURE] Add support for aliasing insert datasets to support upsert alias [#306](https://github.com/doug-martin/goqu/pull/306) - [@XIELongDragon](https://github.com/XIELongDragon)
* [FEATURE] Add support for aliasing BooleanExpressions [#307](https://github.com/doug-martin/goqu/pull/307) - [@XIELongDragon](https://github.com/XIELongDragon)

# v9.17.0
* [FEATURE] Add support bitwise operations [#303](https://github.com/doug-martin/goqu/pull/303) - [@XIELongDragon](https://github.com/XIELongDragon)
* [FEATURE] Add support for specifying tables to be locked in ForUpdate, ForNoKeyUpdate, ForKeyShare, ForShare [#299](https://github.com/doug-martin/goqu/pull/299) - [@jbub](https://github.com/jbub)
Expand Down
4 changes: 4 additions & 0 deletions exp/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (b boolean) Op() BooleanOperation {
return b.op
}

func (b boolean) As(val interface{}) AliasedExpression {
return NewAliasExpression(b, val)
}

// used internally to create an equality BooleanExpression
func eq(lhs Expression, rhs interface{}) BooleanExpression {
return checkBoolExpType(EqOp, lhs, rhs, false)
Expand Down
1 change: 1 addition & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ type (
BooleanOperation int
BooleanExpression interface {
Expression
Aliaseable
// Returns the operator for the expression
Op() BooleanOperation
// The left hand side of the expression (e.g. I("a")
Expand Down
20 changes: 20 additions & 0 deletions exp/insert_clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type (
HasRows() bool
SetRows(rows []interface{}) InsertClauses

HasAlias() bool
Alias() IdentifierExpression
SetAlias(ie IdentifierExpression) InsertClauses

Vals() [][]interface{}
HasVals() bool
SetVals(vals [][]interface{}) InsertClauses
Expand All @@ -41,6 +45,7 @@ type (
cols ColumnListExpression
into Expression
returning ColumnListExpression
alias IdentifierExpression
rows []interface{}
values [][]interface{}
from AppendableExpression
Expand All @@ -62,6 +67,7 @@ func (ic *insertClauses) clone() *insertClauses {
cols: ic.cols,
into: ic.into,
returning: ic.returning,
alias: ic.alias,
rows: ic.rows,
values: ic.values,
from: ic.from,
Expand Down Expand Up @@ -117,6 +123,20 @@ func (ic *insertClauses) HasReturning() bool {
return ic.returning != nil && !ic.returning.IsEmpty()
}

func (ic *insertClauses) HasAlias() bool {
return ic.alias != nil
}

func (ic *insertClauses) Alias() IdentifierExpression {
return ic.alias
}

func (ic *insertClauses) SetAlias(ie IdentifierExpression) InsertClauses {
ret := ic.clone()
ret.alias = ie
return ret
}

func (ic *insertClauses) SetReturning(cl ColumnListExpression) InsertClauses {
ret := ic.clone()
ret.returning = cl
Expand Down
7 changes: 6 additions & 1 deletion insert_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ func (id *InsertDataset) AppendSQL(b sb.SQLBuilder) {
}

func (id *InsertDataset) GetAs() exp.IdentifierExpression {
return nil
return id.clauses.Alias()
}

// Sets the alias for this dataset. This is typically used when using a Dataset as MySQL upsert
func (id *InsertDataset) As(alias string) *InsertDataset {
return id.copy(id.clauses.SetAlias(T(alias)))
}

func (id *InsertDataset) ReturnsColumns() bool {
Expand Down
31 changes: 31 additions & 0 deletions insert_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,37 @@ func (ids *insertDatasetSuite) TestOnConflict() {
)
}

func (ids *insertDatasetSuite) TestAs() {
du := goqu.DoUpdate("other_items", goqu.Record{"new.a": 1})

bd := goqu.Insert("items").As("new")
ids.assertCases(
insertTestCase{
ds: bd.OnConflict(nil),
clauses: exp.NewInsertClauses().SetInto(goqu.C("items")).
SetAlias(exp.NewIdentifierExpression("", "new", "")),
},
insertTestCase{
ds: bd.OnConflict(goqu.DoNothing()),
clauses: exp.NewInsertClauses().SetInto(goqu.C("items")).
SetAlias(exp.NewIdentifierExpression("", "new", "")).
SetOnConflict(goqu.DoNothing()),
},
insertTestCase{
ds: bd.OnConflict(du),
clauses: exp.NewInsertClauses().
SetAlias(exp.NewIdentifierExpression("", "new", "")).
SetInto(goqu.C("items")).SetOnConflict(du),
},
insertTestCase{
ds: bd,
clauses: exp.NewInsertClauses().
SetAlias(exp.NewIdentifierExpression("", "new", "")).
SetInto(goqu.C("items")),
},
)
}

func (ids *insertDatasetSuite) TestClearOnConflict() {
du := goqu.DoUpdate("other_items", goqu.Record{"a": 1})

Expand Down
10 changes: 10 additions & 0 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_AliasedExpression() {
)
}

func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpressionAliased() {
ident := exp.NewIdentifierExpression("", "", "a")

esgs.assertCases(
sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()),
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = 1) AS "b"`},
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = ?) AS "b"`,
isPrepared: true, args: []interface{}{int64(1)}},
)
}
func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpression() {
ae := newTestAppendableExpression(`SELECT "id" FROM "test2"`, emptyArgs, nil, nil)
re := regexp.MustCompile("[ab]")
Expand Down
4 changes: 4 additions & 0 deletions sqlgen/insert_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (isg *insertSQLGenerator) InsertSQL(b sb.SQLBuilder, ic exp.InsertClauses)
default:
isg.defaultValuesSQL(b)
}
if ic.HasAlias() {
b.Write(isg.DialectOptions().AsFragment)
isg.ExpressionSQLGenerator().Generate(b, ic.Alias())
}
isg.onConflictSQL(b, ic.OnConflict())
}

Expand Down
11 changes: 11 additions & 0 deletions sqlgen/insert_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {
})
icDn := ic.SetOnConflict(exp.NewDoNothingConflictExpression())
icDu := ic.SetOnConflict(exp.NewDoUpdateConflictExpression("test", exp.Record{"a": "b"}))
icAsDu := ic.SetAlias(exp.NewIdentifierExpression("", "new", "")).SetOnConflict(
exp.NewDoUpdateConflictExpression("test", exp.Record{"a": exp.NewIdentifierExpression("", "new", "a")}),
)
icDoc := ic.SetOnConflict(exp.NewDoUpdateConflictExpression("on constraint test", exp.Record{"a": "b"}))
icDuw := ic.SetOnConflict(
exp.NewDoUpdateConflictExpression("test", exp.Record{"a": "b"}).Where(exp.Ex{"foo": true}),
Expand All @@ -283,6 +286,14 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {
args: []interface{}{"a1", "b"},
},

insertTestCase{clause: icAsDu, sql: `INSERT INTO "test" ("a") VALUES ('a1') AS "new" on conflict (test) do update set "a"="new"."a"`},
insertTestCase{
clause: icAsDu,
sql: `INSERT INTO "test" ("a") VALUES (?) AS "new" on conflict (test) do update set "a"="new"."a"`,
isPrepared: true,
args: []interface{}{"a1"},
},

insertTestCase{clause: icDoc, sql: `INSERT INTO "test" ("a") VALUES ('a1') on conflict on constraint test do update set "a"='b'`},
insertTestCase{
clause: icDoc,
Expand Down
19 changes: 19 additions & 0 deletions sqlgen/select_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,32 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate() {
sc := exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test"))
scWithCols := sc.SetSelect(exp.NewColumnListExpression("a", "b"))

ident := exp.NewIdentifierExpression("", "", "a")
scWithBooExpAliased := sc.SetSelect(
exp.NewColumnListExpression(
ident.Eq(1).As("x"),
ident.IsNull().As("y"),
),
)

ssgs.assertCases(
sqlgen.NewSelectSQLGenerator("test", opts),
selectTestCase{clause: sc, sql: `select # FROM "test"`},
selectTestCase{clause: sc, sql: `select # FROM "test"`, isPrepared: true},

selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`},
selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`, isPrepared: true},

selectTestCase{
clause: scWithBooExpAliased,
sql: `select ("a" = 1) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
},
selectTestCase{
clause: scWithBooExpAliased,
sql: `select ("a" = ?) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
isPrepared: true,
args: []interface{}{int64(1)},
},
)
}

Expand Down

0 comments on commit e40b45c

Please sign in to comment.