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

Commit 55df0a2d by Matt Broadstone

fix primary key availability after create, other style cleanups

1 parent 0107d544
Showing with 31 additions and 20 deletions
...@@ -34,28 +34,25 @@ module.exports = (function() { ...@@ -34,28 +34,25 @@ module.exports = (function() {
} }
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
if (!sql) { if (!sql) {
resolve(self.formatResults()); resolve(self.formatResults());
return promise; return promise;
} }
if(self.sql === 'BEGIN TRANSACTION'){ if (self.sql === 'BEGIN TRANSACTION') {
var trans = new self.connection.lib.Transaction(self.connection.context); var trans = new self.connection.lib.Transaction(self.connection.context);
trans.begin(function(err){ trans.begin(function(err){
if (err) { if (err) {
this.sequelize.log(err.message); this.sequelize.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
}else{ } else {
self.connection.lib._transaction = trans; self.connection.lib._transaction = trans;
resolve(); resolve();
} }
}); });
} else{ } else {
var request, transCommand; var request, transCommand;
if (self.connection.lib._transaction if (self.connection.lib._transaction && self.connection.uuid) {
&& self.connection.uuid){
request = new self.connection.lib.Request(self.connection.lib._transaction); request = new self.connection.lib.Request(self.connection.lib._transaction);
if (self.sql === 'COMMIT TRANSACTION;') { if (self.sql === 'COMMIT TRANSACTION;') {
...@@ -64,8 +61,7 @@ module.exports = (function() { ...@@ -64,8 +61,7 @@ module.exports = (function() {
transCommand = 'rollback'; transCommand = 'rollback';
} }
if (self.sql === 'COMMIT TRANSACTION;' || if (self.sql === 'COMMIT TRANSACTION;' || self.sql === 'ROLLBACK TRANSACTION;') {
self.sql === 'ROLLBACK TRANSACTION;') {
self.connection.lib._transaction[transCommand](function (err, result) { self.connection.lib._transaction[transCommand](function (err, result) {
if (err) { if (err) {
self.sequelize.log(err.message); self.sequelize.log(err.message);
...@@ -80,11 +76,13 @@ module.exports = (function() { ...@@ -80,11 +76,13 @@ module.exports = (function() {
} else { } else {
request = new self.connection.lib.Request(self.connection.context); request = new self.connection.lib.Request(self.connection.context);
} }
request.query(self.sql, function(err, recordset) { request.query(self.sql, function(err, recordset) {
if(promise){ if (promise) {
promise.emit('sql', self.sql, self.connection.uuid); promise.emit('sql', self.sql, self.connection.uuid);
} }
if(err){
if (err) {
self.sequelize.log(err.message); self.sequelize.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
} else { } else {
...@@ -115,7 +113,7 @@ module.exports = (function() { ...@@ -115,7 +113,7 @@ module.exports = (function() {
*/ */
Query.prototype.formatResults = function(data) { Query.prototype.formatResults = function(data) {
var result = this.callee; var result = this.callee;
if(data){ if (data) {
if (this.isInsertQuery(data)) { if (this.isInsertQuery(data)) {
this.handleInsertQuery(data); this.handleInsertQuery(data);
} else if (this.isShowTableQuery()) { } else if (this.isShowTableQuery()) {
...@@ -125,8 +123,9 @@ module.exports = (function() { ...@@ -125,8 +123,9 @@ module.exports = (function() {
if (this.sql.toLowerCase().indexOf("select c.column_name as 'name', c.data_type as 'type', c.is_nullable as 'isnull'") === 0) { if (this.sql.toLowerCase().indexOf("select c.column_name as 'name', c.data_type as 'type', c.is_nullable as 'isnull'") === 0) {
result = {}; result = {};
data.forEach(function(_result) { data.forEach(function(_result) {
if(_result.Default) if (_result.Default)
_result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,''); _result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,'');
result[_result.Name] = { result[_result.Name] = {
type: _result.Type.toUpperCase(), type: _result.Type.toUpperCase(),
allowNull: (_result.IsNull === 'YES' ? true : false), allowNull: (_result.IsNull === 'YES' ? true : false),
...@@ -144,20 +143,20 @@ module.exports = (function() { ...@@ -144,20 +143,20 @@ module.exports = (function() {
result = data.length; result = data.length;
} else if (this.isBulkDeleteQuery()){ } else if (this.isBulkDeleteQuery()){
result = data[0].AFFECTEDROWS; result = data[0].AFFECTEDROWS;
} else if (result && result.dataValues){} } else{
else{
result = this.handleSelectQuery(data); result = this.handleSelectQuery(data);
} }
} else if(!result) { } else if (!result) {
result = data; result = data;
} }
return result; return result;
}; };
Query.prototype.formatError = function (err) { Query.prototype.formatError = function (err) {
var match; var match;
match = err.message.match(/Violation of UNIQUE KEY constraint '(.*)'. Cannot insert duplicate key in object '?(.*?)$/); match = err.message.match(/Violation of UNIQUE KEY constraint '(.*)'. Cannot insert duplicate key in object '?(.*?)$/);
if(match && match.length > 1){ if (match && match.length > 1) {
return new sequelizeErrors.UniqueConstraintError({ return new sequelizeErrors.UniqueConstraintError({
name: 'SequelizeUniqueConstraintError', name: 'SequelizeUniqueConstraintError',
fields: null, fields: null,
...@@ -166,16 +165,20 @@ module.exports = (function() { ...@@ -166,16 +165,20 @@ module.exports = (function() {
parent: err parent: err
}); });
} }
match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./); match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./);
match = err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./); match = err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
if(match && match.length > 0){ if (match && match.length > 0) {
return new sequelizeErrors.ForeignKeyConstraintError({ return new sequelizeErrors.ForeignKeyConstraintError({
fields: null, fields: null,
index: match[1], index: match[1],
parent: err parent: err
}); });
} }
return new sequelizeErrors.DatabaseError(err);
}; };
Query.prototype.isShowOrDescribeQuery = function() { Query.prototype.isShowOrDescribeQuery = function() {
var result = false; var result = false;
...@@ -223,10 +226,18 @@ module.exports = (function() { ...@@ -223,10 +226,18 @@ module.exports = (function() {
if (this.callee) { if (this.callee) {
// add the inserted row id to the instance // add the inserted row id to the instance
var autoIncrementField = this.callee.Model.autoIncrementField var autoIncrementField = this.callee.Model.autoIncrementField
, autoIncrementFieldAlias = null
, id = null; , id = null;
if (this.callee.Model.rawAttributes.hasOwnProperty(autoIncrementField) &&
this.callee.Model.rawAttributes[autoIncrementField].field !== undefined)
autoIncrementFieldAlias = this.callee.Model.rawAttributes[autoIncrementField].field ;
id = id || (results && results[0][this.getInsertIdField()]); id = id || (results && results[0][this.getInsertIdField()]);
id = id || (metaData && metaData[this.getInsertIdField()]); id = id || (metaData && metaData[this.getInsertIdField()]);
id = id || (results && results[0][autoIncrementField]); id = id || (results && results[0][autoIncrementField]);
id = id || (autoIncrementFieldAlias && results && results[0][autoIncrementFieldAlias]);
this.callee[autoIncrementField] = id; this.callee[autoIncrementField] = id;
} }
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!