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 cada814f
authored
Oct 06, 2014
by
Joel Trost
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit for fetch
1 parent
07257ee3
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
124 additions
and
97 deletions
lib/dialects/mssql/connection-manager.js
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query.js
lib/dialects/mssql/sql-generator.js
lib/sql-string.js
test/model/create.test.js
test/query-interface.test.js
test/sequelize.test.js
lib/dialects/mssql/connection-manager.js
View file @
cada814
...
...
@@ -40,7 +40,6 @@ ConnectionManager.prototype.connect = function(config) {
}
if
(
!
self
.
connection
){
//console.log('CONNECTION MADE', self.connection);
config
=
{
user
:
connectionConfig
.
user
,
password
:
connectionConfig
.
password
,
...
...
@@ -59,12 +58,6 @@ ConnectionManager.prototype.connect = function(config) {
self
.
lib
.
_transaction
=
null
;
var
conn
=
new
self
.
lib
.
Connection
(
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
;
...
...
@@ -76,18 +69,6 @@ ConnectionManager.prototype.connect = function(config) {
}
else
{
resolve
(
self
.
connection
);
}
//console.log(self.connection);
// 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);
});
};
ConnectionManager
.
prototype
.
getConnection
=
function
(
options
)
{
...
...
lib/dialects/mssql/query-generator.js
View file @
cada814
...
...
@@ -202,7 +202,6 @@ module.exports = (function() {
*/
/* istanbul ignore next */
deleteQuery
:
function
(
tableName
,
where
,
options
)
{
console
.
log
(
'where:'
,
where
);
var
query
=
SqlGenerator
.
deleteSql
(
tableName
,
where
);
...
...
@@ -474,8 +473,8 @@ module.exports = (function() {
return
''
;
//return 'SAVE TRANSACTION ' + SqlGenerator.quoteIdentifier(transaction.name) + ';';
}
//
return 'BEGIN TRANSACTION';
return
''
;
return
'BEGIN TRANSACTION'
;
//
return '';
},
/**
* Returns a query that commits a transaction.
...
...
lib/dialects/mssql/query.js
View file @
cada814
...
...
@@ -40,42 +40,57 @@ module.exports = (function() {
return
promise
;
}
var
request
,
transCommand
;
if
(
self
.
connection
.
lib
.
_transaction
)
{
request
=
new
self
.
connection
.
lib
.
Request
(
self
.
connection
.
lib
.
_transaction
);
if
(
self
.
sql
===
'COMMIT TRANSACTION;'
)
{
transCommand
=
'commit'
;
}
else
if
(
self
.
sql
===
'ROLLBACK TRANSACTION;'
)
{
transCommand
=
'rollback'
;
}
if
(
self
.
sql
===
'BEGIN TRANSACTION'
){
var
trans
=
new
self
.
connection
.
lib
.
Transaction
(
self
.
connection
.
context
);
trans
.
begin
(
function
(
err
){
if
(
err
)
{
console
.
log
(
err
.
message
);
reject
(
self
.
formatError
(
err
));
}
else
{
self
.
connection
.
lib
.
_transaction
=
trans
;
resolve
();
}
});
}
else
{
var
request
,
transCommand
;
if
(
self
.
connection
.
lib
.
_transaction
&&
self
.
connection
.
uuid
)
{
request
=
new
self
.
connection
.
lib
.
Request
(
self
.
connection
.
lib
.
_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
.
lib
.
_transaction
[
transCommand
](
function
(
err
,
result
)
{
if
(
err
)
{
console
.
log
(
err
.
message
);
reject
(
self
.
formatError
(
err
));
}
else
{
resolve
(
self
.
formatResults
(
result
));
}
});
return
promise
;
if
(
self
.
sql
===
'COMMIT TRANSACTION;'
||
self
.
sql
===
'ROLLBACK TRANSACTION;'
)
{
self
.
connection
.
lib
.
_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
.
lib
.
Request
(
self
.
connection
.
context
);
}
}
else
{
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
){
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
connection
.
uuid
);
}
if
(
err
){
console
.
log
(
err
.
message
);
reject
(
self
.
formatError
(
err
));
}
else
{
resolve
(
self
.
formatResults
(
recordset
));
}
});
});
}
});
return
promise
;
...
...
@@ -100,37 +115,34 @@ module.exports = (function() {
Query
.
prototype
.
formatResults
=
function
(
data
)
{
var
result
=
this
.
callee
;
//console.log(data);
if
(
data
){
if
(
this
.
isInsertQuery
(
data
))
{
this
.
handleInsertQuery
(
data
);
}
else
if
(
this
.
isShowTableQuery
())
{
result
=
this
.
handleShowTableQuery
(
data
);
}
else
if
(
this
.
isShowOrDescribeQuery
())
{
if
(
this
.
isInsertQuery
(
data
))
{
this
.
handleInsertQuery
(
data
);
}
else
if
(
this
.
isShowTableQuery
())
{
result
=
this
.
handleShowTableQuery
(
data
);
}
else
if
(
this
.
isShowOrDescribeQuery
())
{
result
=
data
;
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
"select c.name, t.name as 'type', c.is_nullable as isnull"
)
===
0
)
{
result
=
{};
data
.
forEach
(
function
(
_result
)
{
if
(
_result
.
Default
)
_result
.
Default
=
_result
.
Default
.
replace
(
'(\''
,
''
).
replace
(
'\')'
,
''
).
replace
(
/'/g
,
''
);
result
[
_result
.
Name
]
=
{
type
:
_result
.
Type
.
toUpperCase
(),
allowNull
:
_result
.
IsNull
,
defaultValue
:
_result
.
Default
};
});
}
else
if
(
this
.
isShowIndexesQuery
())
{
result
=
data
;
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
"select c.name, t.name as 'type', c.is_nullable as isnull"
)
===
0
)
{
result
=
{};
data
.
forEach
(
function
(
_result
)
{
if
(
_result
.
Default
)
_result
.
Default
=
_result
.
Default
.
replace
(
'(\''
,
''
).
replace
(
'\')'
,
''
).
replace
(
/'/g
,
''
);
result
[
_result
.
Name
]
=
{
type
:
_result
.
Type
.
toUpperCase
(),
allowNull
:
_result
.
IsNull
,
defaultValue
:
_result
.
Default
};
});
}
else
if
(
this
.
isShowIndexesQuery
())
{
result
=
data
;
}
}
else
if
(
this
.
isSelectQuery
())
{
result
=
this
.
handleSelectQuery
(
data
);
}
else
if
(
this
.
isCallQuery
())
{
result
=
data
[
0
];
}
else
if
(
this
.
isBulkUpdateQuery
()
||
this
.
isBulkDeleteQuery
())
{
result
=
data
.
affectedRows
;
}
}
else
{
result
=
null
;
}
else
if
(
this
.
isSelectQuery
())
{
result
=
this
.
handleSelectQuery
(
data
);
}
else
if
(
this
.
isCallQuery
())
{
result
=
data
[
0
];
}
else
if
(
this
.
isBulkUpdateQuery
()
||
this
.
isBulkDeleteQuery
())
{
result
=
data
.
affectedRows
;
}
return
result
;
};
...
...
@@ -220,6 +232,7 @@ module.exports = (function() {
,
id
=
null
;
id
=
id
||
(
results
&&
results
[
0
][
this
.
getInsertIdField
()]);
id
=
id
||
(
metaData
&&
metaData
[
this
.
getInsertIdField
()]);
id
=
id
||
(
results
&&
results
[
0
][
autoIncrementField
]);
this
.
callee
[
autoIncrementField
]
=
id
;
}
};
...
...
lib/dialects/mssql/sql-generator.js
View file @
cada814
...
...
@@ -4,7 +4,7 @@ var Utils = require('../../utils')
,
SqlString
=
require
(
'../../sql-string'
)
,
DataTypes
=
require
(
'./data-types'
)
,
_options
,
_dialect
,
_dialect
=
'mssql'
,
_sequelize
;
/*
...
...
@@ -82,12 +82,37 @@ function fieldsToSql(fields, singleQuote){
}
return
''
;
}
function
processValue
(
val
,
modelAttribute
){
var
sql
;
if
(
val
instanceof
Utils
.
cast
){
sql
=
'CAST('
;
if
(
val
.
val
.
_isSequelizeMethod
){
sql
+=
processValue
(
val
.
val
,
modelAttribute
)
+
' AS '
+
val
.
type
.
toUpperCase
();
}
else
{
sql
+=
escape
(
val
.
val
)
+
' AS '
+
val
.
type
.
toUpperCase
();
}
return
sql
+
')'
;
}
else
if
(
val
instanceof
Utils
.
literal
){
return
val
.
val
;
}
else
if
(
val
instanceof
Utils
.
fn
)
{
sql
=
val
.
fn
+
'('
+
val
.
args
.
map
(
function
(
arg
)
{
if
(
arg
.
_isSequelizeMethod
)
{
return
processValue
(
arg
,
modelAttribute
);
}
else
{
return
escape
(
arg
);
}
}).
join
(
', '
)
+
')'
;
return
sql
;
}
else
{
return
escape
(
val
,
modelAttribute
);
}
}
function
valuesToSql
(
fields
,
modelAttributeMap
){
var
values
=
[];
for
(
var
key
in
fields
)
{
if
(
fields
.
hasOwnProperty
(
key
))
{
var
value
=
fields
[
key
];
values
.
push
(
escape
(
value
,
(
modelAttributeMap
&&
modelAttributeMap
[
key
])
||
undefined
));
values
.
push
(
processValue
(
fields
[
key
],
(
modelAttributeMap
&&
modelAttributeMap
[
key
])
||
undefined
));
}
}
if
(
values
){
...
...
@@ -105,6 +130,13 @@ function loadColumn(attributes){
}
return
attrStr
;
}
function
identityInsertWrapper
(
query
,
table
){
return
[
'SET IDENTITY_INSERT'
,
quoteIdentifier
(
table
),
'ON;'
,
query
,
'SET IDENTITY_INSERT'
,
quoteIdentifier
(
table
),
'OFF;'
,
].
join
(
' '
);
}
function
addTableExistsWrapper
(
query
,
exists
){
return
[
"IF ("
,
...
...
@@ -250,11 +282,14 @@ module.exports = {
var
selFields
=
[];
var
insertKey
=
false
;
for
(
var
key
in
valueHash
)
{
for
(
var
key
in
modelAttributeMap
)
{
selFields
.
push
(
'INSERTED.'
+
quoteIdentifier
(
key
));
if
(
modelAttributeMap
[
key
].
autoIncrement
){
insertKey
=
true
;
if
(
modelAttributeMap
[
key
].
autoIncrement
&&
(
valueHash
[
key
]
===
'DEFAULT'
||
valueHash
[
key
]
===
null
)){
delete
valueHash
[
key
];
}
else
if
(
modelAttributeMap
[
key
].
autoIncrement
){
insertKey
=
true
;
}
}
...
...
@@ -267,9 +302,9 @@ module.exports = {
query
=
(
replacements
.
attributes
.
length
?
valueQuery
:
emptyQuery
)
+
';'
;
//
if(insertKey){
// query = identityInsertOnWrapper(query
);
//
}
if
(
insertKey
){
query
=
identityInsertWrapper
(
query
,
tableName
);
}
return
Utils
.
_
.
template
(
query
)(
replacements
);
},
updateSql
:
function
(
tableName
,
valueHash
,
where
,
options
,
attributes
){
...
...
@@ -632,8 +667,6 @@ module.exports = {
}
else
{
query
.
push
(
quoteIdentifier
(
key
));
}
console
.
log
(
'where'
,
where
);
console
.
log
(
'here'
,
val
);
query
.
push
(
operator
);
if
(
!
val
){
query
.
push
(
'NULL'
);
...
...
lib/sql-string.js
View file @
cada814
...
...
@@ -76,7 +76,7 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
}
}
if
(
dialect
===
'postgres'
||
dialect
===
'sqlite'
)
{
if
(
dialect
===
'postgres'
||
dialect
===
'sqlite'
||
dialect
===
'mssql'
)
{
// http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
// http://stackoverflow.com/q/603572/130598
val
=
val
.
replace
(
/'/g
,
"''"
);
...
...
test/model/create.test.js
View file @
cada814
...
...
@@ -377,7 +377,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
})
it
(
'is possible to use funtions when creating an instance'
,
function
(
done
)
{
it
(
'is possible to use fun
c
tions when creating an instance'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
secretValue
:
this
.
sequelize
.
fn
(
'upper'
,
'sequelize'
)
...
...
@@ -665,7 +665,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
})
it
(
'should only store the values passed in the whitelist'
,
function
(
done
)
{
it
.
only
(
'should only store the values passed in the whitelist'
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Peter'
,
secretValue
:
'42'
}
...
...
@@ -727,7 +727,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
})
it
(
'saves data with single quote'
,
function
(
done
)
{
it
.
only
(
'saves data with single quote'
,
function
(
done
)
{
var
quote
=
"single'quote"
,
self
=
this
...
...
@@ -1039,7 +1039,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
})
it
(
'saves data with single quote'
,
function
(
done
)
{
it
.
only
(
'saves data with single quote'
,
function
(
done
)
{
var
self
=
this
,
quote
=
"Single'Quote"
,
data
=
[{
username
:
'Peter'
,
data
:
quote
},
...
...
test/query-interface.test.js
View file @
cada814
...
...
@@ -99,7 +99,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
self
.
queryInterface
.
showIndex
(
'Group'
).
complete
(
function
(
err
,
indexes
)
{
expect
(
err
).
to
.
be
.
null
console
.
log
(
indexes
);
indexColumns
=
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
name
}))
expect
(
indexColumns
).
to
.
be
.
empty
...
...
test/sequelize.test.js
View file @
cada814
...
...
@@ -26,7 +26,7 @@ var qq = function(str) {
}
}
describe
.
only
(
Support
.
getTestDialectTeaser
(
"Sequelize"
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
"Sequelize"
),
function
()
{
describe
(
'constructor'
,
function
()
{
//MSSQL already pools, this test is not relevent
if
(
dialect
!==
'sqlite'
&&
dialect
!==
'mssql'
)
{
...
...
@@ -120,6 +120,8 @@ describe.only(Support.getTestDialectTeaser("Sequelize"), function () {
err
.
message
.
match
(
/connect ECONNREFUSED/
)
||
err
.
message
.
match
(
/invalid port number/
)
).
to
.
be
.
ok
}
else
if
(
dialect
===
'mssql'
){
expect
(
err
.
message
.
match
(
/ConnectionError: Login failed for user/
)).
to
.
be
.
ok
}
else
{
expect
(
err
.
message
).
to
.
match
(
/connect ECONNREFUSED/
)
}
...
...
@@ -1128,4 +1130,4 @@ describe.only(Support.getTestDialectTeaser("Sequelize"), function () {
});
});
});
});
\ No newline at end of file
});
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