不要怂,就是干,撸起袖子干!

Commit b00148e0 by Sushant Committed by GitHub

refactor: dialect support information for inserts (#9663)

1 parent 735b2a48
...@@ -11,8 +11,6 @@ AbstractDialect.prototype.supports = { ...@@ -11,8 +11,6 @@ AbstractDialect.prototype.supports = {
'ORDER NULLS': false, 'ORDER NULLS': false,
'UNION': true, 'UNION': true,
'UNION ALL': true, 'UNION ALL': true,
/* What is the dialect's keyword for INSERT IGNORE */
'IGNORE': '',
/* does the dialect support returning values for inserted/updated fields */ /* does the dialect support returning values for inserted/updated fields */
returnValues: false, returnValues: false,
...@@ -30,10 +28,6 @@ AbstractDialect.prototype.supports = { ...@@ -30,10 +28,6 @@ AbstractDialect.prototype.supports = {
}, },
/* Do we need to say DEFAULT for bulk insert */ /* Do we need to say DEFAULT for bulk insert */
bulkDefault: false, bulkDefault: false,
/* The dialect's words for INSERT IGNORE */
ignoreDuplicates: '',
/* Does the dialect support ON DUPLICATE KEY UPDATE */
updateOnDuplicate: false,
schemas: false, schemas: false,
transactions: true, transactions: true,
transactionOptions: { transactionOptions: {
...@@ -41,6 +35,10 @@ AbstractDialect.prototype.supports = { ...@@ -41,6 +35,10 @@ AbstractDialect.prototype.supports = {
}, },
migrations: true, migrations: true,
upserts: true, upserts: true,
inserts: {
ignoreDuplicates: false, /* dialect specific words for INSERT IGNORE or DO NOTHING*/
updateOnDuplicate: false /* dialect specific words for ON DUPLICATE KEY UPDATE or ON CONFLICT*/
},
constraints: { constraints: {
restrict: true, restrict: true,
addConstraint: true, addConstraint: true,
......
...@@ -232,7 +232,7 @@ class QueryGenerator { ...@@ -232,7 +232,7 @@ class QueryGenerator {
} }
const replacements = { const replacements = {
ignoreDuplicates: options.ignoreDuplicates ? this._dialect.supports.IGNORE : '', ignoreDuplicates: options.ignoreDuplicates ? this._dialect.supports.inserts.ignoreDuplicates : '',
table: this.quoteTable(table), table: this.quoteTable(table),
attributes: fields.join(','), attributes: fields.join(','),
output: outputFragment, output: outputFragment,
...@@ -307,7 +307,7 @@ class QueryGenerator { ...@@ -307,7 +307,7 @@ class QueryGenerator {
tuples.push(`(${values.join(',')})`); tuples.push(`(${values.join(',')})`);
} }
if (this._dialect.supports.updateOnDuplicate && options.updateOnDuplicate) { if (this._dialect.supports.inserts.updateOnDuplicate && options.updateOnDuplicate) {
onDuplicateKeyUpdate = ' ON DUPLICATE KEY UPDATE ' + options.updateOnDuplicate.map(attr => { onDuplicateKeyUpdate = ' ON DUPLICATE KEY UPDATE ' + options.updateOnDuplicate.map(attr => {
const key = this.quoteIdentifier(attr); const key = this.quoteIdentifier(attr);
return key + '=VALUES(' + key + ')'; return key + '=VALUES(' + key + ')';
...@@ -315,7 +315,7 @@ class QueryGenerator { ...@@ -315,7 +315,7 @@ class QueryGenerator {
} }
const replacements = { const replacements = {
ignoreDuplicates: options.ignoreDuplicates ? this._dialect.supports.ignoreDuplicates : '', ignoreDuplicates: options.ignoreDuplicates ? this._dialect.supports.inserts.ignoreDuplicates : '',
table: this.quoteTable(tableName), table: this.quoteTable(tableName),
attributes: allAttributes.map(attr => this.quoteIdentifier(attr)).join(','), attributes: allAttributes.map(attr => this.quoteIdentifier(attr)).join(','),
tuples: tuples.join(','), tuples: tuples.join(','),
......
...@@ -27,7 +27,6 @@ MssqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype. ...@@ -27,7 +27,6 @@ MssqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.
lock: false, lock: false,
transactions: true, transactions: true,
migrations: false, migrations: false,
upserts: true,
returnValues: { returnValues: {
output: true output: true
}, },
......
...@@ -22,9 +22,12 @@ class MysqlDialect extends AbstractDialect { ...@@ -22,9 +22,12 @@ class MysqlDialect extends AbstractDialect {
MysqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), { MysqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), {
'VALUES ()': true, 'VALUES ()': true,
'LIMIT ON UPDATE': true, 'LIMIT ON UPDATE': true,
'IGNORE': ' IGNORE',
lock: true, lock: true,
forShare: 'LOCK IN SHARE MODE', forShare: 'LOCK IN SHARE MODE',
inserts: {
ignoreDuplicates: ' IGNORE',
updateOnDuplicate: true
},
index: { index: {
collate: false, collate: false,
length: true, length: true,
...@@ -36,8 +39,6 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype. ...@@ -36,8 +39,6 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.
dropConstraint: false, dropConstraint: false,
check: false check: false
}, },
ignoreDuplicates: ' IGNORE',
updateOnDuplicate: true,
indexViaAlter: true, indexViaAlter: true,
NUMERIC: true, NUMERIC: true,
GEOMETRY: true, GEOMETRY: true,
......
...@@ -23,7 +23,9 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype ...@@ -23,7 +23,9 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype
'DEFAULT': false, 'DEFAULT': false,
'DEFAULT VALUES': true, 'DEFAULT VALUES': true,
'UNION ALL': false, 'UNION ALL': false,
'IGNORE': ' OR IGNORE', inserts: {
ignoreDuplicates: ' OR IGNORE'
},
index: { index: {
using: false, using: false,
where: true where: true
...@@ -38,7 +40,6 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype ...@@ -38,7 +40,6 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype
}, },
joinTableDependent: false, joinTableDependent: false,
groupedLimit: false, groupedLimit: false,
ignoreDuplicates: ' OR IGNORE',
JSON: true JSON: true
}); });
......
...@@ -395,7 +395,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -395,7 +395,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
if (current.dialect.supports.ignoreDuplicates) { if (current.dialect.supports.inserts.ignoreDuplicates) {
it('should support the ignoreDuplicates option', function() { it('should support the ignoreDuplicates option', function() {
const self = this; const self = this;
const data = [ const data = [
...@@ -436,7 +436,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -436,7 +436,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
} }
if (current.dialect.supports.updateOnDuplicate) { if (current.dialect.supports.inserts.updateOnDuplicate) {
describe('updateOnDuplicate', () => { describe('updateOnDuplicate', () => {
it('should support the updateOnDuplicate option', function() { it('should support the updateOnDuplicate option', function() {
const data = [ const data = [
......
...@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
it('Works when two separate uniqueKeys are passed', function() { it('works when two separate uniqueKeys are passed', function() {
const User = this.sequelize.define('User', { const User = this.sequelize.define('User', {
username: { username: {
type: Sequelize.STRING, type: Sequelize.STRING,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!