Skip to content

Commit cc897ca

Browse files
rohitishere1brianc
authored andcommitted
the ON CONFLICT feature for #PostgreSQL was missing camelcase support… (brianc#342)
* the ON CONFLICT feature for #PostgreSQL was missing camelcase support of column names this commit fixes that and takes care of issue brianc#341 it now honours snakeToCamel set to true * we can quote the column name only once and save extra operation
1 parent 3f48a69 commit cc897ca

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

lib/dialect/postgres.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,13 +1041,13 @@ Postgres.prototype.visitOnDuplicate = function(onDuplicate) {
10411041
Postgres.prototype.visitOnConflict = function(onConflict) {
10421042
var result = ['ON CONFLICT'];
10431043
var columns = [];
1044-
var updateClause = [], i;
1045-
1044+
var updateClause = [], i, col;
1045+
var table = this._queryNode.table;
10461046
if(onConflict.constraint)
10471047
result.push(['ON CONSTRAINT', this.quote(onConflict.constraint)].join(' '));
10481048
else if(onConflict.columns) {
10491049
for(i=0; i < onConflict.columns.length; i++) {
1050-
columns.push(this.quote(onConflict.columns[i]));
1050+
columns.push(this.quote(table.getColumn(onConflict.columns[i]).name));
10511051
}
10521052
result.push( '(' + columns.join(', ') + ')' );
10531053
}
@@ -1057,7 +1057,8 @@ Postgres.prototype.visitOnConflict = function(onConflict) {
10571057
var update = onConflict.update;
10581058
var setClause = [];
10591059
for(i=0; i<update.length; i++) {
1060-
setClause.push(this.quote(update[i]) + ' = EXCLUDED.' +this.quote(update[i]));
1060+
col = this.quote(table.getColumn(update[i]).name);
1061+
setClause.push(col + ' = EXCLUDED.' + col);
10611062
}
10621063
updateClause.push(setClause.join(', '));
10631064
}

test/dialects/insert-tests.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var Harness = require('./support');
44
var post = Harness.definePostTable();
55
var user = Harness.defineUserTable();
6+
var contentTable = Harness.defineContentTable();
7+
var customerAliasTable = Harness.defineCustomerAliasTable();
68

79
var arrayTable = require('../../lib/table').define({
810
name: 'arraytest',
@@ -626,6 +628,33 @@ Harness.test({
626628
params: ['test', 2, 'testupdate']
627629
});
628630

631+
Harness.test({
632+
query: customerAliasTable.insert({
633+
id : 2,
634+
name : 'test'
635+
}).onConflict({
636+
columns: ['id'],
637+
update: ['name']
638+
}),
639+
mysql: {
640+
throws: true
641+
},
642+
sqlite: {
643+
throws: true
644+
},
645+
pg: {
646+
text : 'INSERT INTO "customer" ("id", "name") VALUES ($1, $2) ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"',
647+
string: 'INSERT INTO "customer" ("id", "name") VALUES (2, \'test\') ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"'
648+
},
649+
mssql: {
650+
throws: true
651+
},
652+
oracle: {
653+
throws: true
654+
},
655+
params: [2, 'test']
656+
});
657+
629658
Harness.test({
630659
query: post.insert({
631660
content: 'test',
@@ -786,6 +815,60 @@ Harness.test({
786815
params: ['test', 2]
787816
});
788817

818+
Harness.test({
819+
query: contentTable.insert({
820+
contentId: 20,
821+
text : "something"
822+
}).onConflict({
823+
columns: ['contentId'],
824+
}),
825+
mysql: {
826+
throws: true
827+
},
828+
sqlite: {
829+
throws: true
830+
},
831+
pg: {
832+
text : 'INSERT INTO "content" ("content_id", "text") VALUES ($1, $2) ON CONFLICT ("content_id") DO NOTHING',
833+
string: 'INSERT INTO "content" ("content_id", "text") VALUES (20, \'something\') ON CONFLICT ("content_id") DO NOTHING'
834+
},
835+
mssql: {
836+
throws: true
837+
},
838+
oracle: {
839+
throws: true
840+
},
841+
params: [20, "something"]
842+
});
843+
844+
Harness.test({
845+
query: contentTable.insert({
846+
contentId: 20,
847+
text : "something",
848+
contentPosts : "another thing",
849+
}).onConflict({
850+
columns: ['contentId'],
851+
update: ['contentPosts']
852+
}),
853+
mysql: {
854+
throws: true
855+
},
856+
sqlite: {
857+
throws: true
858+
},
859+
pg: {
860+
text : 'INSERT INTO "content" ("content_id", "text", "content_posts") VALUES ($1, $2, $3) ON CONFLICT ("content_id") DO UPDATE SET "content_posts" = EXCLUDED."content_posts"',
861+
string: 'INSERT INTO "content" ("content_id", "text", "content_posts") VALUES (20, \'something\', \'another thing\') ON CONFLICT ("content_id") DO UPDATE SET "content_posts" = EXCLUDED."content_posts"'
862+
},
863+
mssql: {
864+
throws: true
865+
},
866+
oracle: {
867+
throws: true
868+
},
869+
params: [20, "something", "another thing"]
870+
});
871+
789872
Harness.test({
790873
query: post.insert([]),
791874

test/dialects/support.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,14 @@ module.exports = {
124124
name: 'variable',
125125
columns: ['a', 'b', 'c', 'd', 't', 'u', 'v', 'x', 'y', 'z']
126126
});
127+
},
128+
129+
// this table is for testing snakeName related stuff
130+
defineContentTable: function() {
131+
return Table.define({
132+
name: 'content',
133+
columns: ['content_id', 'text', 'content_posts'],
134+
snakeToCamel: true
135+
});
127136
}
128137
};

0 commit comments

Comments
 (0)