Skip to content

Commit e40b45c

Browse files
Merge pull request #2 from doug-martin/master
sync from origin
2 parents 90a0aeb + 31d438d commit e40b45c

10 files changed

+110
-1
lines changed

HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v9.18.0
2+
* [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)
3+
* [FEATURE] Add support for aliasing BooleanExpressions [#307](https://github.com/doug-martin/goqu/pull/307) - [@XIELongDragon](https://github.com/XIELongDragon)
4+
15
# v9.17.0
26
* [FEATURE] Add support bitwise operations [#303](https://github.com/doug-martin/goqu/pull/303) - [@XIELongDragon](https://github.com/XIELongDragon)
37
* [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)

exp/bool.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func (b boolean) Op() BooleanOperation {
3535
return b.op
3636
}
3737

38+
func (b boolean) As(val interface{}) AliasedExpression {
39+
return NewAliasExpression(b, val)
40+
}
41+
3842
// used internally to create an equality BooleanExpression
3943
func eq(lhs Expression, rhs interface{}) BooleanExpression {
4044
return checkBoolExpType(EqOp, lhs, rhs, false)

exp/exp.go

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ type (
209209
BooleanOperation int
210210
BooleanExpression interface {
211211
Expression
212+
Aliaseable
212213
// Returns the operator for the expression
213214
Op() BooleanOperation
214215
// The left hand side of the expression (e.g. I("a")

exp/insert_clauses.go

+20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type (
2828
HasRows() bool
2929
SetRows(rows []interface{}) InsertClauses
3030

31+
HasAlias() bool
32+
Alias() IdentifierExpression
33+
SetAlias(ie IdentifierExpression) InsertClauses
34+
3135
Vals() [][]interface{}
3236
HasVals() bool
3337
SetVals(vals [][]interface{}) InsertClauses
@@ -41,6 +45,7 @@ type (
4145
cols ColumnListExpression
4246
into Expression
4347
returning ColumnListExpression
48+
alias IdentifierExpression
4449
rows []interface{}
4550
values [][]interface{}
4651
from AppendableExpression
@@ -62,6 +67,7 @@ func (ic *insertClauses) clone() *insertClauses {
6267
cols: ic.cols,
6368
into: ic.into,
6469
returning: ic.returning,
70+
alias: ic.alias,
6571
rows: ic.rows,
6672
values: ic.values,
6773
from: ic.from,
@@ -117,6 +123,20 @@ func (ic *insertClauses) HasReturning() bool {
117123
return ic.returning != nil && !ic.returning.IsEmpty()
118124
}
119125

126+
func (ic *insertClauses) HasAlias() bool {
127+
return ic.alias != nil
128+
}
129+
130+
func (ic *insertClauses) Alias() IdentifierExpression {
131+
return ic.alias
132+
}
133+
134+
func (ic *insertClauses) SetAlias(ie IdentifierExpression) InsertClauses {
135+
ret := ic.clone()
136+
ret.alias = ie
137+
return ret
138+
}
139+
120140
func (ic *insertClauses) SetReturning(cl ColumnListExpression) InsertClauses {
121141
ret := ic.clone()
122142
ret.returning = cl

insert_dataset.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ func (id *InsertDataset) AppendSQL(b sb.SQLBuilder) {
242242
}
243243

244244
func (id *InsertDataset) GetAs() exp.IdentifierExpression {
245-
return nil
245+
return id.clauses.Alias()
246+
}
247+
248+
// Sets the alias for this dataset. This is typically used when using a Dataset as MySQL upsert
249+
func (id *InsertDataset) As(alias string) *InsertDataset {
250+
return id.copy(id.clauses.SetAlias(T(alias)))
246251
}
247252

248253
func (id *InsertDataset) ReturnsColumns() bool {

insert_dataset_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,37 @@ func (ids *insertDatasetSuite) TestOnConflict() {
369369
)
370370
}
371371

372+
func (ids *insertDatasetSuite) TestAs() {
373+
du := goqu.DoUpdate("other_items", goqu.Record{"new.a": 1})
374+
375+
bd := goqu.Insert("items").As("new")
376+
ids.assertCases(
377+
insertTestCase{
378+
ds: bd.OnConflict(nil),
379+
clauses: exp.NewInsertClauses().SetInto(goqu.C("items")).
380+
SetAlias(exp.NewIdentifierExpression("", "new", "")),
381+
},
382+
insertTestCase{
383+
ds: bd.OnConflict(goqu.DoNothing()),
384+
clauses: exp.NewInsertClauses().SetInto(goqu.C("items")).
385+
SetAlias(exp.NewIdentifierExpression("", "new", "")).
386+
SetOnConflict(goqu.DoNothing()),
387+
},
388+
insertTestCase{
389+
ds: bd.OnConflict(du),
390+
clauses: exp.NewInsertClauses().
391+
SetAlias(exp.NewIdentifierExpression("", "new", "")).
392+
SetInto(goqu.C("items")).SetOnConflict(du),
393+
},
394+
insertTestCase{
395+
ds: bd,
396+
clauses: exp.NewInsertClauses().
397+
SetAlias(exp.NewIdentifierExpression("", "new", "")).
398+
SetInto(goqu.C("items")),
399+
},
400+
)
401+
}
402+
372403
func (ids *insertDatasetSuite) TestClearOnConflict() {
373404
du := goqu.DoUpdate("other_items", goqu.Record{"a": 1})
374405

sqlgen/expression_sql_generator_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,16 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_AliasedExpression() {
473473
)
474474
}
475475

476+
func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpressionAliased() {
477+
ident := exp.NewIdentifierExpression("", "", "a")
478+
479+
esgs.assertCases(
480+
sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()),
481+
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = 1) AS "b"`},
482+
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = ?) AS "b"`,
483+
isPrepared: true, args: []interface{}{int64(1)}},
484+
)
485+
}
476486
func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpression() {
477487
ae := newTestAppendableExpression(`SELECT "id" FROM "test2"`, emptyArgs, nil, nil)
478488
re := regexp.MustCompile("[ab]")

sqlgen/insert_sql_generator.go

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func (isg *insertSQLGenerator) InsertSQL(b sb.SQLBuilder, ic exp.InsertClauses)
100100
default:
101101
isg.defaultValuesSQL(b)
102102
}
103+
if ic.HasAlias() {
104+
b.Write(isg.DialectOptions().AsFragment)
105+
isg.ExpressionSQLGenerator().Generate(b, ic.Alias())
106+
}
103107
isg.onConflictSQL(b, ic.OnConflict())
104108
}
105109

sqlgen/insert_sql_generator_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {
257257
})
258258
icDn := ic.SetOnConflict(exp.NewDoNothingConflictExpression())
259259
icDu := ic.SetOnConflict(exp.NewDoUpdateConflictExpression("test", exp.Record{"a": "b"}))
260+
icAsDu := ic.SetAlias(exp.NewIdentifierExpression("", "new", "")).SetOnConflict(
261+
exp.NewDoUpdateConflictExpression("test", exp.Record{"a": exp.NewIdentifierExpression("", "new", "a")}),
262+
)
260263
icDoc := ic.SetOnConflict(exp.NewDoUpdateConflictExpression("on constraint test", exp.Record{"a": "b"}))
261264
icDuw := ic.SetOnConflict(
262265
exp.NewDoUpdateConflictExpression("test", exp.Record{"a": "b"}).Where(exp.Ex{"foo": true}),
@@ -283,6 +286,14 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {
283286
args: []interface{}{"a1", "b"},
284287
},
285288

289+
insertTestCase{clause: icAsDu, sql: `INSERT INTO "test" ("a") VALUES ('a1') AS "new" on conflict (test) do update set "a"="new"."a"`},
290+
insertTestCase{
291+
clause: icAsDu,
292+
sql: `INSERT INTO "test" ("a") VALUES (?) AS "new" on conflict (test) do update set "a"="new"."a"`,
293+
isPrepared: true,
294+
args: []interface{}{"a1"},
295+
},
296+
286297
insertTestCase{clause: icDoc, sql: `INSERT INTO "test" ("a") VALUES ('a1') on conflict on constraint test do update set "a"='b'`},
287298
insertTestCase{
288299
clause: icDoc,

sqlgen/select_sql_generator_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,32 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate() {
5757
sc := exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test"))
5858
scWithCols := sc.SetSelect(exp.NewColumnListExpression("a", "b"))
5959

60+
ident := exp.NewIdentifierExpression("", "", "a")
61+
scWithBooExpAliased := sc.SetSelect(
62+
exp.NewColumnListExpression(
63+
ident.Eq(1).As("x"),
64+
ident.IsNull().As("y"),
65+
),
66+
)
67+
6068
ssgs.assertCases(
6169
sqlgen.NewSelectSQLGenerator("test", opts),
6270
selectTestCase{clause: sc, sql: `select # FROM "test"`},
6371
selectTestCase{clause: sc, sql: `select # FROM "test"`, isPrepared: true},
6472

6573
selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`},
6674
selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`, isPrepared: true},
75+
76+
selectTestCase{
77+
clause: scWithBooExpAliased,
78+
sql: `select ("a" = 1) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
79+
},
80+
selectTestCase{
81+
clause: scWithBooExpAliased,
82+
sql: `select ("a" = ?) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
83+
isPrepared: true,
84+
args: []interface{}{int64(1)},
85+
},
6786
)
6887
}
6988

0 commit comments

Comments
 (0)