Skip to content

Commit 73bf260

Browse files
katz12brianc
authored andcommitted
add OR IGNORE for sqlite (brianc#347)
* add OR IGNORE for sqlite * add missing semicolon
1 parent bc02f78 commit 73bf260

5 files changed

Lines changed: 50 additions & 5 deletions

File tree

lib/dialect/postgres.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Postgres.prototype.visit = function(node) {
162162
case 'DEFAULT' : return this.visitDefault(node);
163163
case 'IF EXISTS' : return this.visitIfExists();
164164
case 'IF NOT EXISTS' : return this.visitIfNotExists();
165+
case 'OR IGNORE' : return this.visitOrIgnore();
165166
case 'CASCADE' : return this.visitCascade();
166167
case 'RESTRICT' : return this.visitRestrict();
167168
case 'RENAME' : return this.visitRename(node);
@@ -237,11 +238,10 @@ Postgres.prototype.visitInsert = function(insert) {
237238
// don't use table.column for inserts
238239
this._visitedInsert = true;
239240

240-
var result = [
241-
'INSERT INTO',
242-
this.visit(this._queryNode.table.toNode()),
243-
'(' + insert.columns.map(this.visit.bind(this)).join(', ') + ')'
244-
];
241+
var result = ['INSERT'];
242+
result = result.concat(insert.nodes.map(this.visit.bind(this)));
243+
result.push('INTO ' + this.visit(this._queryNode.table.toNode()));
244+
result.push('(' + insert.columns.map(this.visit.bind(this)).join(', ') + ')');
245245

246246
var paramNodes = insert.getParameters();
247247

@@ -995,6 +995,10 @@ Postgres.prototype.visitIfNotExists = function() {
995995
return ['IF NOT EXISTS'];
996996
};
997997

998+
Postgres.prototype.visitOrIgnore = function() {
999+
throw new Error('PostgreSQL does not allow orIgnore clause.');
1000+
};
1001+
9981002
Postgres.prototype.visitCascade = function() {
9991003
return ['CASCADE'];
10001004
};

lib/dialect/sqlite.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,8 @@ Sqlite.prototype.visitBinary = function(binary) {
165165
return Sqlite.super_.prototype.visitBinary.call(this, binary);
166166
};
167167

168+
Sqlite.prototype.visitOrIgnore = function() {
169+
return ['OR IGNORE'];
170+
};
171+
168172
module.exports = Sqlite;

lib/node/orIgnore.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var Node = require(__dirname);
4+
5+
module.exports = Node.define({
6+
type: 'OR IGNORE'
7+
});

lib/node/query.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var ParameterNode = require('./parameter');
3737
var PrefixUnaryNode = require('./prefixUnary');
3838
var IfExists = require('./ifExists');
3939
var IfNotExists = require('./ifNotExists');
40+
var OrIgnore = require('./orIgnore');
4041
var Cascade = require('./cascade');
4142
var Restrict = require('./restrict');
4243
var Indexes = require('./indexes');
@@ -468,6 +469,11 @@ var Query = Node.define({
468469
return this;
469470
},
470471

472+
orIgnore: function() {
473+
this.nodes[0].unshift(new OrIgnore());
474+
return this;
475+
},
476+
471477
cascade: function() {
472478
this.nodes[0].add(new Cascade());
473479
return this;

test/dialects/insert-tests.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,30 @@ Harness.test({
680680
params: [2, 'test']
681681
});
682682

683+
Harness.test({
684+
query: customerAliasTable.insert({
685+
id : 2,
686+
name : 'test'
687+
}).orIgnore(),
688+
mysql: {
689+
throws: true
690+
},
691+
sqlite: {
692+
text : 'INSERT OR IGNORE INTO "customer" ("id", "name") VALUES ($1, $2)',
693+
string: 'INSERT OR IGNORE INTO "customer" ("id", "name") VALUES (2, \'test\')'
694+
},
695+
pg: {
696+
throws: true
697+
},
698+
mssql: {
699+
throws: true
700+
},
701+
oracle: {
702+
throws: true
703+
},
704+
params: [2, 'test']
705+
});
706+
683707
Harness.test({
684708
query: post.insert({
685709
content: 'test',

0 commit comments

Comments
 (0)