Skip to content

Commit

Permalink
feat: support InsertQuery.Ignore on PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 23, 2021
1 parent b4cdd9d commit 1aa9d14
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion dialect/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ const (
TableCascade
TableIdentity
TableTruncate
OnDuplicateKey
InsertOnConflict // INSERT ... ON CONFLICT
InsertOnDuplicateKey // INSERT ... ON DUPLICATE KEY
InsertIgnore // INSERT IGNORE ...
)
3 changes: 2 additions & 1 deletion dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func New() *Dialect {
feature.UpdateMultiTable |
feature.ValuesRow |
feature.TableTruncate |
feature.OnDuplicateKey
feature.InsertIgnore |
feature.InsertOnDuplicateKey
return d
}

Expand Down
3 changes: 2 additions & 1 deletion dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func New() *Dialect {
feature.DeleteTableAlias |
feature.TableCascade |
feature.TableIdentity |
feature.TableTruncate
feature.TableTruncate |
feature.InsertOnConflict
return d
}

Expand Down
3 changes: 2 additions & 1 deletion dialect/sqlitedialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func New() *Dialect {
d.features = feature.CTE |
feature.Returning |
feature.InsertTableAlias |
feature.DeleteTableAlias
feature.DeleteTableAlias |
feature.InsertOnConflict
return d
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-54
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT IGNORE INTO "models" ("id", "str") VALUES (DEFAULT, '') RETURNING "id"
INSERT INTO "models" AS "model" ("id", "str") VALUES (DEFAULT, '') ON CONFLICT DO NOTHING RETURNING "id"
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-54
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT IGNORE INTO "models" ("id", "str") VALUES (DEFAULT, '') RETURNING "id"
INSERT INTO "models" AS "model" ("id", "str") VALUES (DEFAULT, '') ON CONFLICT DO NOTHING RETURNING "id"
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-54
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT IGNORE INTO "models" ("str") VALUES ('') RETURNING "id"
INSERT INTO "models" AS "model" ("str") VALUES ('') ON CONFLICT DO NOTHING RETURNING "id"
13 changes: 10 additions & 3 deletions query_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ func (q *InsertQuery) hasReturning() bool {

//------------------------------------------------------------------------------

// Ignore generates an `INSERT IGNORE INTO` query (MySQL).
// Ignore generates different queries depending on the DBMS:
// - On MySQL, it generates `INSERT IGNORE INTO`.
// - On PostgreSQL, it generates `ON CONFLICT DO NOTHING`.
func (q *InsertQuery) Ignore() *InsertQuery {
q.ignore = true
if q.db.fmter.HasFeature(feature.InsertOnConflict) {
return q.On("CONFLICT DO NOTHING")
}
if q.db.fmter.HasFeature(feature.InsertIgnore) {
q.ignore = true
}
return q
}

Expand Down Expand Up @@ -421,7 +428,7 @@ func (q *InsertQuery) appendOn(fmter schema.Formatter, b []byte) (_ []byte, err
}

if len(q.set) > 0 {
if fmter.HasFeature(feature.OnDuplicateKey) {
if fmter.HasFeature(feature.InsertOnDuplicateKey) {
b = append(b, ' ')
} else {
b = append(b, " SET "...)
Expand Down

0 comments on commit 1aa9d14

Please sign in to comment.