Skip to content

Commit

Permalink
Merge pull request #1 from thinkjs/feature/insert-update-on-duplicate…
Browse files Browse the repository at this point in the history
…-key

[feature]add insert update on duplicate key
  • Loading branch information
welefen committed Sep 17, 2018
2 parents 112fc63 + 6a9c29d commit 91a22be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '6'
- '8'
sudo: false
script:
- "npm test"
Expand Down
39 changes: 38 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const helper = require('think-helper');
const {Parser} = require('think-model-abstract');
const { Parser } = require('think-model-abstract');

module.exports = class MysqlParser extends Parser {
/**
Expand All @@ -23,6 +23,8 @@ module.exports = class MysqlParser extends Parser {
*/
escapeString(str) {
if (!str) return '';

// eslint-disable-next-line no-control-regex
return str.replace(/[\0\n\r\b\t\\'"\x1a]/g, s => {
switch (s) {
case '\0':
Expand All @@ -42,4 +44,39 @@ module.exports = class MysqlParser extends Parser {
}
});
}

/**
* get insert sql
* @param {Object} options
*/
buildInsertSql(options, replace) {
const isUpdate = helper.isObject(options.update) || helper.isArray(options.update);
if (replace || !isUpdate) {
return super.buildInsertSql(options, replace);
}

const table = this.parseTable(options.table);
const values = options.values[0] !== '(' ? `(${options.values})` : options.values;

let sets = [];
if (helper.isArray(options.update)) {
sets = options.update.map(field => {
field = this.parseKey(field);
return field + '=' + `VALUES(${field})`;
});
} else {
for (const key in options.update) {
const value = this.parseValue(options.update[key]);
if (helper.isString(value) || helper.isNumber(value) || helper.isBoolean(value)) {
sets.push(this.parseKey(key) + '=' + value);
}
}
}

const duplicateUpdate = sets.length ? ' ON DUPLICATE KEY UPDATE ' + sets.join(',') : '';
const lock = this.parseLock(options.lock);
const comment = this.parseComment(options.comment);

return `INSERT INTO ${table} (${options.fields}) VALUES ${values}${duplicateUpdate}${lock}${comment}`;
}
};
23 changes: 6 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,16 @@
},
"homepage": "https://github.com/thinkjs/think-model-mysql#readme",
"dependencies": {
"think-debounce": "^1.0.3",
"think-helper": "^1.0.5",
"think-model-abstract": "^1.0.5",
"think-mysql": "^1.0.2"
"think-debounce": "^1.0.4",
"think-helper": "^1.1.2",
"think-model-abstract": "^1.2.2",
"think-mysql": "^1.2.4"
},
"devDependencies": {
"ava": "^0.19.1",
"eslint": "^4.2.0",
"eslint": "^4.19.1",
"eslint-config-think": "^1.0.1",
"muk": "^0.5.3",
"nyc": "^10.3.0"
},
"ava": {
"require": [
"babel-register"
],
"babel": "inherit"
},
"babel": {
"presets": [
"think-node"
]
}
}
}

0 comments on commit 91a22be

Please sign in to comment.