Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit db45f14f
authored
Sep 30, 2014
by
Jason Jung
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added resolve / reject on connect
1 parent
0116710a
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
84 additions
and
25 deletions
lib/dialects/abstract/connection-manager.js
lib/dialects/mssql/connection-manager.js
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query.js
lib/transaction.js
test/model/and-or-where.test.js
lib/dialects/abstract/connection-manager.js
View file @
db45f14
...
...
@@ -182,6 +182,7 @@ ConnectionManager.prototype.initPools = function () {
ConnectionManager
.
prototype
.
getConnection
=
function
(
options
)
{
var
self
=
this
;
options
=
options
||
{};
self
.
pool
.
isTransaction
=
options
.
transaction
;
return
new
Promise
(
function
(
resolve
,
reject
)
{
self
.
pool
.
acquire
(
function
(
err
,
connection
)
{
...
...
@@ -200,7 +201,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
};
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
)
{
return
this
.
dialect
.
connectionManager
.
disconnect
(
connection
);
...
...
lib/dialects/mssql/connection-manager.js
View file @
db45f14
...
...
@@ -13,13 +13,13 @@ ConnectionManager = function(dialect, sequelize) {
try
{
this
.
lib
=
require
(
sequelize
.
config
.
dialectModulePath
||
'mssql'
);
}
catch
(
err
)
{
throw
new
Error
(
'Please install
seriate
package manually'
);
throw
new
Error
(
'Please install
mssql
package manually'
);
}
};
Utils
.
_
.
extend
(
ConnectionManager
.
prototype
,
AbstractConnectionManager
.
prototype
);
ConnectionManager
.
prototype
.
connect
=
function
(
config
)
{
ConnectionManager
.
prototype
.
connect
=
function
(
config
,
isTransaction
)
{
var
self
=
this
;
return
new
Promise
(
function
(
resolve
,
reject
)
{
...
...
@@ -43,24 +43,49 @@ ConnectionManager.prototype.connect = function(config) {
server
:
connectionConfig
.
host
,
database
:
connectionConfig
.
database
};
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
(
isTransaction
)
{
var
conn
=
new
self
.
lib
.
Connection
(
config
,
function
(
err
){
var
trans
=
new
self
.
lib
.
Transaction
(
conn
);
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 = {
// config: {
// user: connectionConfig.user,
// password: connectionConfig.password,
// server: connectionConfig.host,
// database: connectionConfig.database
// },
// },
// };
//connection = self.lib;
// connection.lib = self.lib.getPlainContext(connection.config);
resolve
(
self
.
lib
);
});
};
ConnectionManager
.
prototype
.
disconnect
=
function
(
connection
)
{
...
...
lib/dialects/mssql/query-generator.js
View file @
db45f14
...
...
@@ -359,7 +359,7 @@ module.exports = (function() {
if
(
identifiers
.
indexOf
(
'.'
)
!==
-
1
)
{
identifiers
=
identifiers
.
split
(
'.'
);
return
SqlGenerator
.
quoteIdentifier
(
identifiers
.
slice
(
0
,
identifiers
.
length
-
1
).
join
(
'.'
))
identifiers
.
slice
(
0
,
identifiers
.
length
-
1
).
join
(
'.'
))
+
'.'
+
SqlGenerator
.
quoteIdentifier
(
identifiers
[
identifiers
.
length
-
1
]);
}
else
{
return
SqlGenerator
.
quoteIdentifier
(
identifiers
);
...
...
@@ -472,7 +472,8 @@ module.exports = (function() {
//return 'SAVE TRANSACTION ' + SqlGenerator.quoteIdentifier(transaction.name) + ';';
}
return
'BEGIN TRANSACTION'
;
// return 'BEGIN TRANSACTION';
return
''
;
},
/**
* Returns a query that commits a transaction.
...
...
@@ -486,6 +487,7 @@ module.exports = (function() {
}
return
'COMMIT TRANSACTION;'
;
// return '';
},
/**
...
...
@@ -500,7 +502,8 @@ module.exports = (function() {
return
'ROLLBACK TRANSACTION '
+
this
.
quoteIdentifier
(
transaction
.
name
)
+
';'
;
}
return
'ROLLBACK TRANSACTION'
;
return
'ROLLBACK TRANSACTION;'
;
// return '';
},
addLimitAndOffset
:
function
(
options
,
query
)
{
...
...
lib/dialects/mssql/query.js
View file @
db45f14
...
...
@@ -36,6 +36,7 @@ module.exports = (function() {
}
var
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
console
.
log
(
self
.
sql
);
// self
...
...
@@ -52,15 +53,42 @@ module.exports = (function() {
// 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
)
{
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
connection
.
uuid
);
if
(
err
){
console
.
log
(
err
.
message
);
reject
(
self
.
formatError
(
err
));
}
else
{
resolve
(
self
.
formatResults
(
recordset
));
}
resolve
(
self
.
formatResults
(
recordset
));
});
});
return
promise
;
...
...
lib/transaction.js
View file @
db45f14
'use strict'
;
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()`.
...
...
@@ -114,11 +115,12 @@ Transaction.prototype.prepareEnvironment = function() {
var
self
=
this
;
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
)
{
self
.
connection
=
connection
;
self
.
connection
.
uuid
=
self
.
id
;
}).
then
(
function
()
{
})
.
then
(
function
()
{
return
self
.
begin
();
}).
then
(
function
()
{
return
self
.
setIsolationLevel
();
...
...
test/model/and-or-where.test.js
View file @
db45f14
...
...
@@ -60,7 +60,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it
(
'can handle objects'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
Sequelize
[
method
](
{
username
:
"foo"
,
intVal
:
2
},
{
secretValue
:
'bar'
}
)
}).
on
(
'sql'
,
function
(
sql
)
{
}).
on
(
'sql'
,
function
(
sql
)
{
var
expectation
=
({
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\')'
,
...
...
@@ -83,7 +83,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it
(
'can handle numbers'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
Sequelize
[
method
](
1
,
2
)
}).
on
(
'sql'
,
function
(
sql
)
{
}).
on
(
'sql'
,
function
(
sql
)
{
var
expectation
=
({
mysql
:
"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 () {
it
(
'allows nesting of Sequelize.or using object notation'
,
function
(
done
)
{
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
}})
)
}).
on
(
'sql'
,
function
(
sql
)
{
var
expectation
=
({
...
...
@@ -157,14 +157,14 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it
(
'allows nesting of Sequelize.and using object notation'
,
function
(
done
)
{
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
}})
)
}).
on
(
'sql'
,
function
(
sql
)
{
var
expectation
=
({
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"
,
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"
})[
Support
.
getTestDialect
()]
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment