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

Commit db45f14f by Jason Jung Committed by Matt Broadstone

added resolve / reject on connect

1 parent 0116710a
...@@ -182,6 +182,7 @@ ConnectionManager.prototype.initPools = function () { ...@@ -182,6 +182,7 @@ ConnectionManager.prototype.initPools = function () {
ConnectionManager.prototype.getConnection = function(options) { ConnectionManager.prototype.getConnection = function(options) {
var self = this; var self = this;
options = options || {}; options = options || {};
self.pool.isTransaction = options.transaction;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
self.pool.acquire(function(err, connection) { self.pool.acquire(function(err, connection) {
...@@ -200,7 +201,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) { ...@@ -200,7 +201,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
}; };
ConnectionManager.prototype.$connect = function(config) { ConnectionManager.prototype.$connect = function(config) {
return this.dialect.connectionManager.connect(config); return this.dialect.connectionManager.connect(config, this.pool.isTransaction);
}; };
ConnectionManager.prototype.$disconnect = function(connection) { ConnectionManager.prototype.$disconnect = function(connection) {
return this.dialect.connectionManager.disconnect(connection); return this.dialect.connectionManager.disconnect(connection);
......
...@@ -13,13 +13,13 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -13,13 +13,13 @@ ConnectionManager = function(dialect, sequelize) {
try { try {
this.lib = require(sequelize.config.dialectModulePath || 'mssql'); this.lib = require(sequelize.config.dialectModulePath || 'mssql');
} catch (err) { } catch (err) {
throw new Error('Please install seriate package manually'); throw new Error('Please install mssql package manually');
} }
}; };
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype); Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
ConnectionManager.prototype.connect = function(config) { ConnectionManager.prototype.connect = function(config, isTransaction) {
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
...@@ -43,24 +43,49 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -43,24 +43,49 @@ ConnectionManager.prototype.connect = function(config) {
server: connectionConfig.host, server: connectionConfig.host,
database: connectionConfig.database database: connectionConfig.database
}; };
self.lib.connect(config, function(err) {
// var request = new self.lib.Request(); if (isTransaction) {
// request.query('select 1 as number', function(err, recordset) { var conn = new self.lib.Connection(config, function (err){
// console.log('err2', err); var trans = new self.lib.Transaction(conn);
// console.log(recordset);
// }); self.lib._transaction = trans;
}); trans.begin(function(err) {
if (err) {
reject(err);
return;
}
resolve(self.lib);
});
});
} else {
self.lib.connect(config, function(err) {
// var request = new self.lib.Request();
// request.query('select 1 as number', function(err, recordset) {
// console.log('err2', err);
// console.log(recordset);
// });
if (err) {
reject(err);
return;
}
resolve(self.lib);
});
}
// var connection = { // var connection = {
// config: { // config: {
// user: connectionConfig.user, // user: connectionConfig.user,
// password: connectionConfig.password, // password: connectionConfig.password,
// server: connectionConfig.host, // server: connectionConfig.host,
// database: connectionConfig.database // database: connectionConfig.database
// }, // },
// }; // };
//connection = self.lib; //connection = self.lib;
// connection.lib = self.lib.getPlainContext(connection.config); // connection.lib = self.lib.getPlainContext(connection.config);
resolve(self.lib);
}); });
}; };
ConnectionManager.prototype.disconnect = function(connection) { ConnectionManager.prototype.disconnect = function(connection) {
......
...@@ -359,7 +359,7 @@ module.exports = (function() { ...@@ -359,7 +359,7 @@ module.exports = (function() {
if (identifiers.indexOf('.') !== -1) { if (identifiers.indexOf('.') !== -1) {
identifiers = identifiers.split('.'); identifiers = identifiers.split('.');
return SqlGenerator.quoteIdentifier( return SqlGenerator.quoteIdentifier(
identifiers.slice(0, identifiers.length - 1).join('.')) identifiers.slice(0, identifiers.length - 1).join('.'))
+ '.' + SqlGenerator.quoteIdentifier(identifiers[identifiers.length - 1]); + '.' + SqlGenerator.quoteIdentifier(identifiers[identifiers.length - 1]);
} else { } else {
return SqlGenerator.quoteIdentifier(identifiers); return SqlGenerator.quoteIdentifier(identifiers);
...@@ -472,7 +472,8 @@ module.exports = (function() { ...@@ -472,7 +472,8 @@ module.exports = (function() {
//return 'SAVE TRANSACTION ' + SqlGenerator.quoteIdentifier(transaction.name) + ';'; //return 'SAVE TRANSACTION ' + SqlGenerator.quoteIdentifier(transaction.name) + ';';
} }
return 'BEGIN TRANSACTION'; // return 'BEGIN TRANSACTION';
return '';
}, },
/** /**
* Returns a query that commits a transaction. * Returns a query that commits a transaction.
...@@ -486,6 +487,7 @@ module.exports = (function() { ...@@ -486,6 +487,7 @@ module.exports = (function() {
} }
return 'COMMIT TRANSACTION;'; return 'COMMIT TRANSACTION;';
// return '';
}, },
/** /**
...@@ -500,7 +502,8 @@ module.exports = (function() { ...@@ -500,7 +502,8 @@ module.exports = (function() {
return 'ROLLBACK TRANSACTION ' + this.quoteIdentifier(transaction.name) + ';'; return 'ROLLBACK TRANSACTION ' + this.quoteIdentifier(transaction.name) + ';';
} }
return 'ROLLBACK TRANSACTION'; return 'ROLLBACK TRANSACTION;';
// return '';
}, },
addLimitAndOffset: function(options, query) { addLimitAndOffset: function(options, query) {
......
...@@ -36,6 +36,7 @@ module.exports = (function() { ...@@ -36,6 +36,7 @@ module.exports = (function() {
} }
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
console.log(self.sql); console.log(self.sql);
// self // self
...@@ -52,15 +53,42 @@ module.exports = (function() { ...@@ -52,15 +53,42 @@ module.exports = (function() {
// resolve(self.formatResults(result.query)); // resolve(self.formatResults(result.query));
// }); // });
// }); // });
var request = new self.connection.Request();
var request, transCommand;
if (self.connection._transaction) {
request = new self.connection.Request(self.connection._transaction);
if (self.sql === 'COMMIT TRANSACTION;') {
transCommand = 'commit';
} else if (self.sql === 'ROLLBACK TRANSACTION;') {
transCommand = 'rollback';
}
if (self.sql === 'COMMIT TRANSACTION;' ||
self.sql === 'ROLLBACK TRANSACTION;') {
self.connection._transaction[transCommand](function (err, result) {
if (err) {
console.log(err.message);
reject(self.formatError(err));
} else {
resolve(self.formatResults(result));
}
});
return promise;
}
} else {
request = new self.connection.Request();
}
request.query(self.sql, function(err, recordset) { request.query(self.sql, function(err, recordset) {
promise.emit('sql', self.sql, self.connection.uuid); promise.emit('sql', self.sql, self.connection.uuid);
if(err){ if(err){
console.log(err.message); console.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
} else {
resolve(self.formatResults(recordset));
} }
resolve(self.formatResults(recordset));
}); });
}); });
return promise; return promise;
......
'use strict'; 'use strict';
var Utils = require('./utils') var Utils = require('./utils')
, util = require('util'); , util = require('util')
, Promise = require('./promise');
/** /**
* The transaction object is used to identify a running transaction. It is created by calling `Sequelize.transaction()`. * The transaction object is used to identify a running transaction. It is created by calling `Sequelize.transaction()`.
...@@ -114,11 +115,12 @@ Transaction.prototype.prepareEnvironment = function() { ...@@ -114,11 +115,12 @@ Transaction.prototype.prepareEnvironment = function() {
var self = this; var self = this;
return Utils.Promise.resolve( return Utils.Promise.resolve(
self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id }) self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id, transaction: true })
).then(function (connection) { ).then(function (connection) {
self.connection = connection; self.connection = connection;
self.connection.uuid = self.id; self.connection.uuid = self.id;
}).then(function () { })
.then(function () {
return self.begin(); return self.begin();
}).then(function () { }).then(function () {
return self.setIsolationLevel(); return self.setIsolationLevel();
......
...@@ -60,7 +60,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -60,7 +60,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('can handle objects', function(done) { it('can handle objects', function(done) {
this.User.find({ this.User.find({
where: Sequelize[method]( { username: "foo", intVal: 2 }, { secretValue: 'bar' } ) where: Sequelize[method]( { username: "foo", intVal: 2 }, { secretValue: 'bar' } )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
mysql: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')", mysql: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')",
mssql: 'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 ' + word + ' "User"."secretValue"=\'bar\')', mssql: 'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 ' + word + ' "User"."secretValue"=\'bar\')',
...@@ -83,7 +83,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -83,7 +83,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('can handle numbers', function(done) { it('can handle numbers', function(done) {
this.User.find({ this.User.find({
where: Sequelize[method]( 1, 2 ) where: Sequelize[method]( 1, 2 )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
mysql: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)", mysql: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)",
sqlite: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)", sqlite: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)",
...@@ -121,7 +121,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -121,7 +121,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('allows nesting of Sequelize.or using object notation', function(done) { it('allows nesting of Sequelize.or using object notation', function(done) {
this.User.find({ this.User.find({
where: Sequelize.and( Sequelize.or({username: {eq: "foo"}}, {username: {eq:"bar"}}), where: Sequelize.and( Sequelize.or({username: {eq: "foo"}}, {username: {eq:"bar"}}),
Sequelize.or({id: {eq: 1}}, {id: {eq:4}}) ) Sequelize.or({id: {eq: 1}}, {id: {eq:4}}) )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
...@@ -157,14 +157,14 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -157,14 +157,14 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('allows nesting of Sequelize.and using object notation', function(done) { it('allows nesting of Sequelize.and using object notation', function(done) {
this.User.find({ this.User.find({
where: Sequelize.or( Sequelize.and({username: {eq: "foo"}}, {username: {eq:"bar"}}), where: Sequelize.or( Sequelize.and({username: {eq: "foo"}}, {username: {eq:"bar"}}),
Sequelize.and({id: {eq: 1}}, {id: {eq:4}}) ) Sequelize.and({id: {eq: 1}}, {id: {eq:4}}) )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
mysql: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1", mysql: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1",
sqlite: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1", sqlite: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1",
postgres: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4)) LIMIT 1', postgres: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4)) LIMIT 1',
mssql: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4))', mssql: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4))',
mariadb: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1" mariadb: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1"
})[Support.getTestDialect()] })[Support.getTestDialect()]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!