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 6acb13dd
authored
Sep 29, 2014
by
Joel Trost
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Passes "and-or-where" tests
1 parent
ba5e688e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
74 additions
and
39 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query.js
lib/dialects/mssql/sql-generator.js
test/model/and-or-where.test.js
test/query-interface.test.js
lib/dialects/mssql/query-generator.js
View file @
6acb13d
This diff is collapsed.
Click to expand it.
lib/dialects/mssql/query.js
View file @
6acb13d
...
...
@@ -28,7 +28,6 @@ module.exports = (function() {
};
Query
.
prototype
.
run
=
function
(
sql
)
{
console
.
log
(
sql
);
var
self
=
this
;
this
.
sql
=
sql
;
...
...
@@ -37,15 +36,22 @@ module.exports = (function() {
}
var
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
//
console.log(self.sql);
console
.
log
(
self
.
sql
);
self
.
connection
.
lib
.
execute
(
self
.
connection
.
config
,
{
query
:
self
.
sql
})
.
then
(
function
(
data
)
{
resolve
(
self
.
formatResults
(
data
.
result
));
}
//function (err) { reject(self.formatError(err)); }
function
(
data
)
{
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
connection
.
uuid
);
resolve
(
self
.
formatResults
(
data
.
result
));
},
function
(
err
)
{
console
.
log
(
'err:'
,
err
);
err
.
sql
=
sql
;
reject
(
self
.
formatError
(
err
));
}
);
});
...
...
@@ -71,34 +77,37 @@ module.exports = (function() {
Query
.
prototype
.
formatResults
=
function
(
data
)
{
var
result
=
this
.
callee
;
//console.log(data);
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
())
{
if
(
data
){
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
;
}
}
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
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
;
}
return
result
;
};
...
...
lib/dialects/mssql/sql-generator.js
View file @
6acb13d
...
...
@@ -169,7 +169,12 @@ module.exports = {
set
sequelize
(
seq
)
{
_sequelize
=
seq
;
},
quoteIdentifier
:
function
(
val
){
return
quoteIdentifier
(
val
);
},
escape
:
function
(
value
,
field
)
{
return
escape
(
value
,
field
);
},
showTableSql
:
function
(){
return
'SELECT name FROM sys.Tables;'
;
},
...
...
@@ -620,7 +625,8 @@ module.exports = {
}
else
{
query
.
push
(
quoteIdentifier
(
key
));
}
console
.
log
(
'val'
,
typeof
val
);
console
.
log
(
'where'
,
where
);
console
.
log
(
'here'
,
val
);
query
.
push
(
operator
);
if
(
!
val
){
query
.
push
(
'NULL'
);
...
...
test/model/and-or-where.test.js
View file @
6acb13d
...
...
@@ -35,7 +35,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this
.
User
.
find
({
where
:
Sequelize
[
method
](
"1=1"
,
"2=2"
)
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2) LIMIT 1"
)
if
(
dialect
===
'mssql'
){
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2)"
)
}
else
{
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2) LIMIT 1"
)
}
done
()
})
})
...
...
@@ -44,7 +48,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this
.
User
.
find
({
where
:
Sequelize
[
method
](
[
"1=?"
,
1
],
[
"2=?"
,
2
]
)
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2) LIMIT 1"
)
if
(
dialect
===
'mssql'
){
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2)"
)
}
else
{
expect
(
sql
).
to
.
contain
(
"WHERE (1=1 "
+
word
+
" 2=2) LIMIT 1"
)
}
done
()
})
})
...
...
@@ -52,9 +60,10 @@ 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\')'
,
sqlite
:
"WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 "
+
word
+
" `User`.`secretValue`='bar')"
,
postgres
:
'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 '
+
word
+
' "User"."secretValue"=\'bar\')'
,
mariadb
:
"WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 "
+
word
+
" `User`.`secretValue`='bar')"
...
...
@@ -74,11 +83,12 @@ 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)"
,
postgres
:
'WHERE ("User"."id"=1 '
+
word
+
' "User"."id"=2)'
,
mssql
:
'WHERE ("User"."id"=1 '
+
word
+
' "User"."id"=2)'
,
mariadb
:
"WHERE (`User`.`id`=1 "
+
word
+
" `User`.`id`=2)"
})[
Support
.
getTestDialect
()]
...
...
@@ -100,7 +110,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this
.
User
.
find
({
where
:
Sequelize
.
and
(
Sequelize
.
or
(
"1=1"
,
"2=2"
),
Sequelize
.
or
(
"3=3"
,
"4=4"
)
)
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4)) LIMIT 1"
)
if
(
dialect
===
'mssql'
){
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4))"
)
}
else
{
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4)) LIMIT 1"
)
}
done
()
})
})
...
...
@@ -114,6 +128,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
mysql
:
"WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1"
,
sqlite
:
"WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1"
,
postgres
:
'WHERE (("User"."username" = \'foo\' OR "User"."username" = \'bar\') AND ("User"."id" = 1 OR "User"."id" = 4)) LIMIT 1'
,
mssql
:
'WHERE (("User"."username" = \'foo\' OR "User"."username" = \'bar\') AND ("User"."id" = 1 OR "User"."id" = 4))'
,
mariadb
:
"WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1"
})[
Support
.
getTestDialect
()]
...
...
@@ -131,7 +146,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this
.
User
.
find
({
where
:
Sequelize
.
or
(
Sequelize
.
and
(
"1=1"
,
"2=2"
),
Sequelize
.
and
(
"3=3"
,
"4=4"
)
)
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4)) LIMIT 1"
)
if
(
dialect
===
'mssql'
){
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4))"
)
}
else
{
expect
(
sql
).
to
.
contain
(
"WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4)) LIMIT 1"
)
}
done
()
})
})
...
...
@@ -145,6 +164,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
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))'
,
mariadb
:
"WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1"
})[
Support
.
getTestDialect
()]
...
...
@@ -186,7 +206,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
)
]
}).
on
(
'sql'
,
function
(
sql
)
{
if
(
Support
.
getTestDialect
()
===
'postgres'
)
{
if
(
Support
.
getTestDialect
()
===
'postgres'
||
dialect
===
'mssql'
)
{
expect
(
sql
).
to
.
contain
(
'WHERE ('
+
[
'"User"."id"=42 AND 2=2 AND 1=1 AND "User"."username"=\'foo\' AND '
,
...
...
test/query-interface.test.js
View file @
6acb13d
...
...
@@ -426,7 +426,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
var
keys
=
Object
.
keys
(
fks
[
0
]),
keys2
=
Object
.
keys
(
fks
[
1
]),
keys3
=
Object
.
keys
(
fks
[
2
])
if
(
dialect
===
"postgres"
||
dialect
===
"postgres-native"
)
{
if
(
dialect
===
"postgres"
||
dialect
===
"postgres-native"
||
dialect
==
'mssql'
)
{
expect
(
keys
).
to
.
have
.
length
(
6
)
expect
(
keys2
).
to
.
have
.
length
(
7
)
expect
(
keys3
).
to
.
have
.
length
(
7
)
...
...
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