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

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() {
}
var promise = new Utils.Promise(function(resolve, reject) {
if (!sql) {
resolve(self.formatResults());
return promise;
}
if(self.sql === 'BEGIN TRANSACTION'){
if (self.sql === 'BEGIN TRANSACTION') {
var trans = new self.connection.lib.Transaction(self.connection.context);
trans.begin(function(err){
if (err) {
this.sequelize.log(err.message);
reject(self.formatError(err));
}else{
self.connection.lib._transaction = trans;
resolve();
} else {
self.connection.lib._transaction = trans;
resolve();
}
});
} else{
} else {
var request, transCommand;
if (self.connection.lib._transaction
&& self.connection.uuid){
if (self.connection.lib._transaction && self.connection.uuid) {
request = new self.connection.lib.Request(self.connection.lib._transaction);
if (self.sql === 'COMMIT TRANSACTION;') {
......@@ -64,8 +61,7 @@ module.exports = (function() {
transCommand = 'rollback';
}
if (self.sql === 'COMMIT TRANSACTION;' ||
self.sql === 'ROLLBACK TRANSACTION;') {
if (self.sql === 'COMMIT TRANSACTION;' || self.sql === 'ROLLBACK TRANSACTION;') {
self.connection.lib._transaction[transCommand](function (err, result) {
if (err) {
self.sequelize.log(err.message);
......@@ -80,11 +76,13 @@ module.exports = (function() {
} else {
request = new self.connection.lib.Request(self.connection.context);
}
request.query(self.sql, function(err, recordset) {
if(promise){
if (promise) {
promise.emit('sql', self.sql, self.connection.uuid);
}
if(err){
if (err) {
self.sequelize.log(err.message);
reject(self.formatError(err));
} else {
......@@ -115,7 +113,7 @@ module.exports = (function() {
*/
Query.prototype.formatResults = function(data) {
var result = this.callee;
if(data){
if (data) {
if (this.isInsertQuery(data)) {
this.handleInsertQuery(data);
} else if (this.isShowTableQuery()) {
......@@ -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) {
result = {};
data.forEach(function(_result) {
if(_result.Default)
if (_result.Default)
_result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,'');
result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: (_result.IsNull === 'YES' ? true : false),
......@@ -144,20 +143,20 @@ module.exports = (function() {
result = data.length;
} else if (this.isBulkDeleteQuery()){
result = data[0].AFFECTEDROWS;
} else if (result && result.dataValues){}
else{
} else{
result = this.handleSelectQuery(data);
}
} else if(!result) {
} else if (!result) {
result = data;
}
return result;
};
Query.prototype.formatError = function (err) {
var match;
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({
name: 'SequelizeUniqueConstraintError',
fields: null,
......@@ -166,16 +165,20 @@ module.exports = (function() {
parent: err
});
}
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 '(.*)'./);
if(match && match.length > 0){
if (match && match.length > 0) {
return new sequelizeErrors.ForeignKeyConstraintError({
fields: null,
index: match[1],
parent: err
});
}
return new sequelizeErrors.DatabaseError(err);
};
Query.prototype.isShowOrDescribeQuery = function() {
var result = false;
......@@ -223,10 +226,18 @@ module.exports = (function() {
if (this.callee) {
// add the inserted row id to the instance
var autoIncrementField = this.callee.Model.autoIncrementField
, autoIncrementFieldAlias = 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 || (metaData && metaData[this.getInsertIdField()]);
id = id || (results && results[0][autoIncrementField]);
id = id || (autoIncrementFieldAlias && results && results[0][autoIncrementFieldAlias]);
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!