mysql-bricks
Advanced tools
Comparing version
16
index.js
@@ -64,4 +64,11 @@ 'use strict'; | ||
return 'ON DUPLICATE KEY UPDATE ' + this._onDuplicateColumns.map(function(col) { | ||
col = sql._handleColumn(col, null); | ||
return `${col} = VALUES(${col})`; | ||
if (col.constructor === "".constructor) { // is string | ||
col = sql._handleColumn(col, null); | ||
return `${col} = VALUES(${col})`; | ||
} else if (col.constructor === {}.constructor && Object.keys(col).length > 0) { // is column-value pair object | ||
let colKey = Object.keys(col)[0]; | ||
let colValue = col[colKey]; | ||
colKey = sql._handleColumn(colKey, null); | ||
return `${colKey} = ${colValue}`; | ||
} | ||
}).join(', '); | ||
@@ -81,8 +88,3 @@ }, | ||
// TODO: The ORDER BY and LIMIT clauses of the UPDATE and DELETE statements + tests | ||
// TODO: extend on duplicate key update - allow expression | ||
// Replace ? | ||
module.exports = mysqlBricks; |
{ | ||
"name": "mysql-bricks", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "sql-bricks extension for MySQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,7 +29,22 @@ # mysql-bricks | ||
INSERT INTO main.some_table (id, name, age, grade) | ||
VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) | ||
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) | ||
ON DUPLICATE KEY UPDATE grade = VALUES(grade) | ||
*/ | ||
``` | ||
ON DUPLICATE KEY UPDATE clause can also contain a column-value pair, where the value is a free text sql. | ||
Note that free-text sql will not be automatically quoted if it contains reserved words. | ||
```javascript | ||
let values = [[123, 'Daniel', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]]; | ||
let query = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter') | ||
.values(values) | ||
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }]) | ||
.toString(); | ||
/* | ||
INSERT INTO main.some_table (id, name, age, grade, counter) | ||
VALUES (123, \'Daniel\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0) | ||
ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1 | ||
*/ | ||
``` | ||
#### INSERT IGNORE ... | ||
@@ -45,3 +60,3 @@ | ||
INSERT IGNORE INTO main.some_table (id, name, age, grade) | ||
VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) | ||
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) | ||
*/ | ||
@@ -48,0 +63,0 @@ ``` |
16
tests.js
@@ -48,2 +48,18 @@ 'use strict'; | ||
it("should return correct insert query when using '.onDuplicateKeyUpdate' option with column-value pair", function () { | ||
let values = [[123, 'Moshe', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]]; | ||
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade, counter) ' + | ||
'VALUES (123, \'Moshe\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0) ' + | ||
'ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1'; | ||
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter') | ||
.values(values) | ||
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }]) | ||
.toString(); | ||
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' ')); | ||
}); | ||
it("should ignore '.onDuplicateKeyUpdate' when cols is null", function () { | ||
@@ -50,0 +66,0 @@ |
21564
10.18%257
6.2%111
15.63%