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 e936f9f0
authored
Dec 04, 2014
by
Matt Broadstone
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove as much code from SqlGenerator as possible
1 parent
93afadd6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
92 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/sql-generator.js
test/instance.test.js
lib/dialects/mssql/query-generator.js
View file @
e936f9f
...
...
@@ -91,35 +91,49 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
},
addColumnQuery
:
function
(
table
Name
,
key
,
dataType
)
{
// FIXME: attributeToSQL SHOULD be using attributes in addColumn
Sql
addColumnQuery
:
function
(
table
,
key
,
dataType
)
{
// FIXME: attributeToSQL SHOULD be using attributes in addColumn
Query
// but instead we need to pass the key along as the field here
dataType
.
field
=
key
;
var
query
=
[
SqlGenerator
.
alterTableSql
(
tableName
),
SqlGenerator
.
addColumnSql
(
key
,
dataType
)
].
join
(
' '
)
+
';'
;
var
query
=
'ALTER TABLE <%= table %> ADD <%= attribute %>;'
,
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
})
});
return
query
;
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteTable
(
table
),
attribute
:
attribute
});
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
var
query
=
[
SqlGenerator
.
alterTableSql
(
tableName
),
SqlGenerator
.
dropSql
(
attributeName
)
].
join
(
' '
)
+
';'
;
return
query
;
var
query
=
'ALTER TABLE <%= tableName %> DROP <%= attributeName %>;'
;
return
Utils
.
_
.
template
(
query
)({
tableName
:
this
.
quoteTable
(
tableName
),
attributeName
:
this
.
quoteIdentifier
(
attributeName
)
})
;
},
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
[
SqlGenerator
.
alterTableSql
(
tableName
),
SqlGenerator
.
alterColumnSql
(),
SqlGenerator
.
alterAttributesSql
(
attributes
)
].
join
(
' '
)
+
';'
;
var
query
=
'ALTER TABLE <%= tableName %> ALTER COLUMN <%= attributes %>;'
;
var
attrString
=
[];
return
query
;
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
];
attrString
.
push
(
Utils
.
_
.
template
(
'<%= attrName %> <%= definition %>'
)({
attrName
:
this
.
quoteIdentifier
(
attrName
),
definition
:
definition
}));
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
this
.
quoteTable
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
});
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
...
...
@@ -188,19 +202,44 @@ module.exports = (function() {
};
var
generatedQuery
=
Utils
.
_
.
template
(
allQueries
.
join
(
';'
))(
replacements
);
if
(
needIdentityInsertWrapper
)
return
SqlGenerator
.
identityInsertWrapper
(
generatedQuery
,
tableName
);
if
(
needIdentityInsertWrapper
)
{
generatedQuery
=
[
'SET IDENTITY_INSERT'
,
this
.
quoteTable
(
tableName
),
'ON;'
,
generatedQuery
,
'SET IDENTITY_INSERT'
,
this
.
quoteTable
(
tableName
),
'OFF;'
,
].
join
(
' '
);
}
return
generatedQuery
;
},
deleteQuery
:
function
(
tableName
,
where
,
options
)
{
var
query
=
[
SqlGenerator
.
deleteSql
(
tableName
),
"WHERE"
,
this
.
getWhereConditions
(
where
),
";SELECT @@ROWCOUNT AS AFFECTEDROWS;"
].
join
(
' '
);
options
=
options
||
{};
return
query
;
var
table
=
this
.
quoteTable
(
tableName
);
if
(
options
.
truncate
===
true
)
{
// Truncate does not allow LIMIT and WHERE
return
'TRUNCATE '
+
table
;
}
where
=
this
.
getWhereConditions
(
where
);
var
limit
=
''
,
query
=
'DELETE<%= limit %> FROM <%= table %> WHERE <%= where %>; '
+
'SELECT @@ROWCOUNT AS AFFECTEDROWS;'
;
if
(
Utils
.
_
.
isUndefined
(
options
.
limit
))
{
options
.
limit
=
1
;
}
if
(
!!
options
.
limit
)
{
limit
=
' TOP('
+
this
.
escape
(
options
.
limit
)
+
')'
;
}
return
Utils
.
_
.
template
(
query
)({
limit
:
limit
,
table
:
table
,
where
:
where
,
});
},
showIndexQuery
:
function
(
tableName
,
options
)
{
...
...
@@ -228,6 +267,10 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
sql
)(
values
);
},
attributeToSQL
:
function
(
attribute
,
options
)
{
return
SqlGenerator
.
attributeToSQL
(
attribute
,
options
);
},
attributesToSQL
:
function
(
attributes
,
options
)
{
var
result
=
{}
,
key
...
...
@@ -331,10 +374,10 @@ module.exports = (function() {
},
dropForeignKeyQuery
:
function
(
tableName
,
foreignKey
)
{
return
[
SqlGenerator
.
alterTableSql
(
tableName
),
SqlGenerator
.
dropSql
(
foreignKey
)
].
join
(
' '
)
+
';'
;
return
Utils
.
_
.
template
(
'ALTER TABLE <%= table %> DROP <%= key %>'
)({
table
:
this
.
quoteTable
(
tableName
),
key
:
this
.
quoteIdentifier
(
foreignKey
)
})
;
},
setAutocommitQuery
:
function
(
value
)
{
...
...
lib/dialects/mssql/sql-generator.js
View file @
e936f9f
...
...
@@ -116,45 +116,6 @@ module.exports = {
return
table
;
},
identityInsertWrapper
:
function
(
query
,
table
){
return
[
'SET IDENTITY_INSERT'
,
quoteIdentifier
(
table
),
'ON;'
,
query
,
'SET IDENTITY_INSERT'
,
quoteIdentifier
(
table
),
'OFF;'
,
].
join
(
' '
);
},
alterTableSql
:
function
(
tableName
){
var
query
=
'ALTER TABLE <%= tableName %>'
;
var
value
=
{
tableName
:
quoteIdentifier
(
tableName
)
};
return
Utils
.
_
.
template
(
query
)(
value
);
},
deleteSql
:
function
(
tableName
)
{
var
query
=
"DELETE FROM <%= table %>"
;
var
replacements
=
{
table
:
quoteIdentifier
(
tableName
.
toString
())
};
return
Utils
.
_
.
template
(
query
)(
replacements
);
},
addColumnSql
:
function
(
key
,
dataType
){
var
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
})
});
return
'ADD '
+
attribute
;
},
alterColumnSql
:
function
(){
return
'ALTER COLUMN'
;
},
renameColumnSql
:
function
(
tableName
,
attrBefore
,
newColumnName
){
var
query
=
'EXEC SP_RENAME \'<%= tableName %>.<%= before %>\', \'<%= after %>\';'
;
var
attrString
=
[];
...
...
@@ -256,26 +217,6 @@ module.exports = {
}
return
template
.
join
(
' '
);
},
dropSql
:
function
(
val
){
return
[
'DROP'
,
quoteIdentifier
(
val
)
].
join
(
' '
);
},
alterAttributesSql
:
function
(
attributes
){
var
attrString
=
[];
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
];
attrString
.
push
(
Utils
.
_
.
template
(
'"<%= attrName %>" <%= definition %>'
)({
attrName
:
attrName
,
definition
:
definition
}));
}
return
attrString
.
join
(
', '
);
},
}
};
test/instance.test.js
View file @
e936f9f
...
...
@@ -1480,8 +1480,8 @@ describe(Support.getTestDialectTeaser("Instance"), function () {
}
else
if
(
Support
.
dialectIsMySQL
())
{
expect
(
sql
).
to
.
equal
(
"DELETE FROM `UserDestroys` WHERE `newId`='123ABC' LIMIT 1"
)
}
else
if
(
dialect
===
'mssql'
)
{
expect
(
sql
).
to
.
equal
(
'DELETE
FROM "UserDestroys" WHERE "newId"=\'123ABC\' ;
SELECT @@ROWCOUNT AS AFFECTEDROWS;'
)
}
else
if
(
dialect
===
'mssql'
)
{
expect
(
sql
).
to
.
equal
(
'DELETE
TOP(1) FROM "UserDestroys" WHERE "newId"=\'123ABC\';
SELECT @@ROWCOUNT AS AFFECTEDROWS;'
)
}
else
{
expect
(
sql
).
to
.
equal
(
"DELETE FROM `UserDestroys` WHERE `newId`='123ABC'"
)
}
...
...
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