Skip to content
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
14 changes: 9 additions & 5 deletions lib/dialect/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Postgres.prototype.visit = function(node) {
case 'DEFAULT' : return this.visitDefault(node);
case 'IF EXISTS' : return this.visitIfExists();
case 'IF NOT EXISTS' : return this.visitIfNotExists();
case 'OR IGNORE' : return this.visitOrIgnore();
case 'CASCADE' : return this.visitCascade();
case 'RESTRICT' : return this.visitRestrict();
case 'RENAME' : return this.visitRename(node);
Expand Down Expand Up @@ -237,11 +238,10 @@ Postgres.prototype.visitInsert = function(insert) {
// don't use table.column for inserts
this._visitedInsert = true;

var result = [
'INSERT INTO',
this.visit(this._queryNode.table.toNode()),
'(' + insert.columns.map(this.visit.bind(this)).join(', ') + ')'
];
var result = ['INSERT'];
result = result.concat(insert.nodes.map(this.visit.bind(this)));
result.push('INTO ' + this.visit(this._queryNode.table.toNode()));
result.push('(' + insert.columns.map(this.visit.bind(this)).join(', ') + ')');

var paramNodes = insert.getParameters();

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

Postgres.prototype.visitOrIgnore = function() {
throw new Error('PostgreSQL does not allow orIgnore clause.');
};

Postgres.prototype.visitCascade = function() {
return ['CASCADE'];
};
Expand Down
4 changes: 4 additions & 0 deletions lib/dialect/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,8 @@ Sqlite.prototype.visitBinary = function(binary) {
return Sqlite.super_.prototype.visitBinary.call(this, binary);
};

Sqlite.prototype.visitOrIgnore = function() {
return ['OR IGNORE'];
};

module.exports = Sqlite;
7 changes: 7 additions & 0 deletions lib/node/orIgnore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var Node = require(__dirname);

module.exports = Node.define({
type: 'OR IGNORE'
});
6 changes: 6 additions & 0 deletions lib/node/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var ParameterNode = require('./parameter');
var PrefixUnaryNode = require('./prefixUnary');
var IfExists = require('./ifExists');
var IfNotExists = require('./ifNotExists');
var OrIgnore = require('./orIgnore');
var Cascade = require('./cascade');
var Restrict = require('./restrict');
var Indexes = require('./indexes');
Expand Down Expand Up @@ -468,6 +469,11 @@ var Query = Node.define({
return this;
},

orIgnore: function() {
this.nodes[0].unshift(new OrIgnore());
return this;
},

cascade: function() {
this.nodes[0].add(new Cascade());
return this;
Expand Down
24 changes: 24 additions & 0 deletions test/dialects/insert-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,30 @@ Harness.test({
params: [2, 'test']
});

Harness.test({
query: customerAliasTable.insert({
id : 2,
name : 'test'
}).orIgnore(),
mysql: {
throws: true
},
sqlite: {
text : 'INSERT OR IGNORE INTO "customer" ("id", "name") VALUES ($1, $2)',
string: 'INSERT OR IGNORE INTO "customer" ("id", "name") VALUES (2, \'test\')'
},
pg: {
throws: true
},
mssql: {
throws: true
},
oracle: {
throws: true
},
params: [2, 'test']
});

Harness.test({
query: post.insert({
content: 'test',
Expand Down