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

Commit cf21f351 by Mick Hansen

just ONE insertQuery

1 parent c65f35e3
...@@ -3,7 +3,8 @@ var AbstractDialect = function() { ...@@ -3,7 +3,8 @@ var AbstractDialect = function() {
} }
AbstractDialect.prototype.supports = { AbstractDialect.prototype.supports = {
'RETURNING': false 'RETURNING': false,
'DEFAULT': true
} }
module.exports = AbstractDialect module.exports = AbstractDialect
\ No newline at end of file
...@@ -104,10 +104,54 @@ module.exports = (function() { ...@@ -104,10 +104,54 @@ module.exports = (function() {
/* /*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs. Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/ */
insertQuery: function(tableName, attrValueHash) { insertQuery: function(table, valueHash, modelAttributes) {
throwMethodUndefined('insertQuery') var query
}, , valueQuery = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>)"
, emptyQuery = "INSERT INTO <%= table %>"
, fields = []
, values = []
, key
, value
if (this._dialect.supports['DEFAULT']) {
emptyQuery += " DEFAULT VALUES"
}
if (this._dialect.supports['RETURNING']) {
valueQuery += " RETURNING *"
emptyQuery += " RETURNING *"
}
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull)
for (key in valueHash) {
if (valueHash.hasOwnProperty(key)) {
value = valueHash[key]
fields.push(this.quoteIdentifier(key))
// SERIALS' can't be NULL in postgresql, use DEFAULT where supported
if (modelAttributes && modelAttributes[key] && modelAttributes[key].autoIncrement === true && !value) {
if (this._dialect.supports['DEFAULT']) {
values.push('DEFAULT')
} else {
values.push(this.escape(null))
}
} else {
values.push(this.escape(value, (modelAttributes && modelAttributes[key]) || undefined))
}
}
}
var replacements = {
table: this.quoteTable(table),
attributes: fields.join(","),
values: values.join(",")
}
query = (replacements.attributes.length ? valueQuery : emptyQuery) + ";"
return Utils._.template(query)(replacements)
},
/* /*
Returns an insert into command for multiple values. Returns an insert into command for multiple values.
Parameters: table name + list of hashes of attribute-value-pairs. Parameters: table name + list of hashes of attribute-value-pairs.
......
...@@ -157,18 +157,6 @@ module.exports = (function() { ...@@ -157,18 +157,6 @@ module.exports = (function() {
return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') })
}, },
insertQuery: function(tableName, attrValueHash) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var table = this.quoteIdentifier(tableName)
var attributes = Object.keys(attrValueHash).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(",")
var values = Utils._.values(attrValueHash).map(function(v) { return this.escape(v) }.bind(this)).join(",")
var query = "INSERT INTO " + table + " (" + attributes + ") VALUES (" + values + ");"
return query
},
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = [] , tuples = []
......
...@@ -247,43 +247,6 @@ module.exports = (function() { ...@@ -247,43 +247,6 @@ module.exports = (function() {
}) })
}, },
insertQuery: function(tableName, attrValueHash, attributes) {
var query
, valueQuery = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
, emptyQuery = "INSERT INTO <%= table %> DEFAULT VALUES RETURNING *;"
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
// Remove serials that are null or undefined, which causes an error in PG
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName]) {
switch (tables[tableName][key]) {
case 'bigserial':
case 'serial':
if ([null, undefined].indexOf(hash[key]) !== -1) delete hash[key]
break
}
}
});
var rowValues = []
Object.keys(attrValueHash).forEach(function(attr) {
rowValues[rowValues.length] = this.escape(attrValueHash[attr], (!!attributes && !!attributes[attr] ? attributes[attr] : undefined))
}.bind(this))
var replacements = {
table: this.quoteIdentifiers(tableName)
, attributes: Object.keys(attrValueHash).map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(",")
, values: rowValues.join(",")
}
query = replacements.attributes.length ? valueQuery : emptyQuery
return Utils._.template(query)(replacements)
},
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = [] , tuples = []
......
...@@ -6,7 +6,7 @@ var SqliteDialect = function(sequelize) { ...@@ -6,7 +6,7 @@ var SqliteDialect = function(sequelize) {
} }
SqliteDialect.prototype.supports = _.extend(Abstract.prototype.supports, { SqliteDialect.prototype.supports = _.extend(Abstract.prototype.supports, {
'DEFAULT': false
}) })
module.exports = SqliteDialect module.exports = SqliteDialect
\ No newline at end of file
...@@ -153,25 +153,6 @@ module.exports = (function() { ...@@ -153,25 +153,6 @@ module.exports = (function() {
return "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';" return "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
}, },
insertQuery: function(tableName, attrValueHash) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query
, valueQuery = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
, emptyQuery = "INSERT INTO <%= table %> DEFAULT VALUES;"
var replacements = {
table: this.quoteIdentifier(tableName),
attributes: Object.keys(attrValueHash).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(","),
values: Utils._.values(attrValueHash).map(function(value){
return this.escape(value)
}.bind(this)).join(",")
}
query = replacements.attributes.length ? valueQuery : emptyQuery
return Utils._.template(query)(replacements)
},
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = [] , tuples = []
...@@ -371,6 +352,10 @@ module.exports = (function() { ...@@ -371,6 +352,10 @@ module.exports = (function() {
}) })
}, },
startTransactionQuery: function(options) {
return "BEGIN TRANSACTION;"
},
setAutocommitQuery: function(value) { setAutocommitQuery: function(value) {
return "-- SQLite does not support SET autocommit." return "-- SQLite does not support SET autocommit."
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!