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

insert select on conflict do update #55

Merged
merged 2 commits into from
Dec 27, 2017
Merged
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
7 changes: 5 additions & 2 deletions dataset_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (me *Dataset) toInsertSql(o ConflictExpression, rows ...interface{}) (strin
}
switch rows[0].(type) {
case *Dataset:
return me.insertFromSql(*rows[0].(*Dataset), me.isPrepared)
return me.insertFromSql(*rows[0].(*Dataset), me.isPrepared, o)
}

}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (me *Dataset) insertSql(cols ColumnList, values [][]interface{}, prepared b
}

//Creates an insert statement with values coming from another dataset
func (me *Dataset) insertFromSql(other Dataset, prepared bool) (string, []interface{}, error) {
func (me *Dataset) insertFromSql(other Dataset, prepared bool, c ConflictExpression) (string, []interface{}, error) {
buf := NewSqlBuilder(prepared)
if err := me.adapter.CommonTablesSql(buf, me.clauses.CommonTables); err != nil {
return "", nil, err
Expand All @@ -217,6 +217,9 @@ func (me *Dataset) insertFromSql(other Dataset, prepared bool) (string, []interf
if err := other.selectSqlWriteTo(buf); err != nil {
return "", nil, err
}
if err := me.adapter.OnConflictSql(buf, c); err != nil {
return "", nil, err
}
if me.adapter.SupportsReturn() {
if err := me.adapter.ReturningSql(buf, me.clauses.Returning); err != nil {
return "", nil, err
Expand Down
15 changes: 15 additions & 0 deletions dataset_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,21 @@ func (me *datasetTest) TestInsertConflictSql__OnConflictDoUpdateWhere() {
assert.Equal(t, `INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test') ON CONFLICT (name) DO UPDATE SET "address"=excluded.address WHERE ("name" = 'Test')`, sql)
}

func (me *datasetTest) TestInsertConflictSqlWithDataset__OnConflictDoUpdateWhere() {
t := me.T()
ds1 := From("items")
type item struct {
Address string `db:"address"`
Name string `db:"name"`
}

ds2 := From("ds2")

sql, _, err := ds1.ToInsertConflictSql(DoUpdate("name", Record{"address": L("excluded.address")}).Where(I("name").Eq("Test")), ds2)
assert.NoError(t, err)
assert.Equal(t, `INSERT INTO "items" SELECT * FROM "ds2" ON CONFLICT (name) DO UPDATE SET "address"=excluded.address WHERE ("name" = 'Test')`, sql)
}

func (me *datasetTest) TestInsertIgnoreSql() {
t := me.T()
ds1 := From("items")
Expand Down