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 a2a21aae
authored
Sep 26, 2014
by
Joel Trost
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bulkinsert and ManytoMany support
1 parent
91b84530
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
44 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/sql-generator.js
test/include/find.test.js
lib/dialects/mssql/query-generator.js
View file @
a2a21aa
...
@@ -145,8 +145,20 @@ module.exports = (function() {
...
@@ -145,8 +145,20 @@ module.exports = (function() {
Parameters: table name + list of hashes of attribute-value-pairs.
Parameters: table name + list of hashes of attribute-value-pairs.
*/
*/
/* istanbul ignore next */
/* istanbul ignore next */
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
throwMethodUndefined
(
'bulkInsertQuery'
);
var
allAttributes
=
[],
ignoreKeys
=
options
.
fields
;
Utils
.
_
.
forEach
(
attrValueHashes
,
function
(
attrValueHash
,
i
)
{
Utils
.
_
.
forOwn
(
attrValueHash
,
function
(
value
,
key
,
hash
)
{
if
(
allAttributes
.
indexOf
(
key
)
===
-
1
)
allAttributes
.
push
(
key
);
if
(
value
!==
null
&&
ignoreKeys
.
indexOf
(
key
)
>
0
)
ignoreKeys
.
splice
(
ignoreKeys
.
indexOf
(
key
),
1
);
});
});
for
(
var
i
=
0
;
i
<
ignoreKeys
.
length
;
i
++
){
allAttributes
.
splice
(
allAttributes
.
indexOf
(
ignoreKeys
[
i
]),
1
);
}
return
SqlGenerator
.
bulkInsertSql
(
tableName
,
allAttributes
,
attrValueHashes
,
options
);
},
},
/*
/*
...
@@ -163,7 +175,7 @@ module.exports = (function() {
...
@@ -163,7 +175,7 @@ module.exports = (function() {
// if(where){
// if(where){
// throw new Error();
// throw new Error();
// }
// }
console
.
log
(
'here'
,
where
);
//
console.log('here', where);
var
query
=
[
var
query
=
[
SqlGenerator
.
updateSql
(
tableName
,
attrValueHash
,
attributes
),
SqlGenerator
.
updateSql
(
tableName
,
attrValueHash
,
attributes
),
this
.
getWhereConditions
(
where
)
this
.
getWhereConditions
(
where
)
...
...
lib/dialects/mssql/sql-generator.js
View file @
a2a21aa
...
@@ -17,6 +17,7 @@ var attributeMap = {
...
@@ -17,6 +17,7 @@ var attributeMap = {
defaultValue
:
"DEFAULT"
,
defaultValue
:
"DEFAULT"
,
unique
:
"UNIQUE"
,
unique
:
"UNIQUE"
,
primaryKey
:
"PRIMARY KEY"
,
primaryKey
:
"PRIMARY KEY"
,
foreignKey
:
"FOREIGN KEY"
,
comment
:
"COMMENT"
,
comment
:
"COMMENT"
,
references
:
"REFERENCES"
,
references
:
"REFERENCES"
,
onDelete
:
"ON DELETE"
,
onDelete
:
"ON DELETE"
,
...
@@ -104,14 +105,6 @@ function loadColumn(attributes){
...
@@ -104,14 +105,6 @@ function loadColumn(attributes){
}
}
return
attrStr
;
return
attrStr
;
}
}
function
loadColumnWithTypes
(
attributes
){
var
attrStr
=
[];
for
(
var
attr
in
attributes
)
{
var
dataType
=
attributes
[
attr
];
attrStr
.
push
(
quoteIdentifier
(
attr
)
+
" "
+
dataType
);
}
return
attrStr
;
}
function
addTableExistsWrapper
(
query
,
exists
){
function
addTableExistsWrapper
(
query
,
exists
){
return
[
return
[
"IF ("
,
"IF ("
,
...
@@ -188,7 +181,10 @@ module.exports = {
...
@@ -188,7 +181,10 @@ module.exports = {
return
dataType
.
indexOf
(
'PRIMARY KEY'
)
>=
0
;
return
dataType
.
indexOf
(
'PRIMARY KEY'
)
>=
0
;
}));
}));
attrStr
=
loadColumnWithTypes
(
attributes
);
for
(
var
attr
in
attributes
)
{
var
dataType
=
attributes
[
attr
];
attrStr
.
push
(
quoteIdentifier
(
attr
)
+
" "
+
dataType
);
}
var
values
=
{
var
values
=
{
unquotedTable
:
tableName
,
unquotedTable
:
tableName
,
...
@@ -201,7 +197,7 @@ module.exports = {
...
@@ -201,7 +197,7 @@ module.exports = {
alterTableSql
:
function
(
tableName
){
alterTableSql
:
function
(
tableName
){
var
query
=
'ALTER TABLE <%= tableName %>'
;
var
query
=
'ALTER TABLE <%= tableName %>'
;
var
value
=
{
var
value
=
{
tableName
:
tableName
tableName
:
quoteIdentifier
(
tableName
)
};
};
return
Utils
.
_
.
template
(
query
)(
value
);
return
Utils
.
_
.
template
(
query
)(
value
);
},
},
...
@@ -214,7 +210,29 @@ module.exports = {
...
@@ -214,7 +210,29 @@ module.exports = {
query
=
addTableExistsWrapper
(
query
,
true
);
query
=
addTableExistsWrapper
(
query
,
true
);
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
},
},
bulkInsertSql
:
function
(
tableName
,
attributeKeys
,
attributes
,
options
)
{
var
query
=
'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
,
tuples
=
[];
Utils
.
_
.
forEach
(
attributes
,
function
(
attrValueHash
,
i
)
{
tuples
.
push
(
'('
+
attributeKeys
.
map
(
function
(
key
)
{
return
escape
(
attrValueHash
[
key
]);
}.
bind
(
this
)).
join
(
','
)
+
')'
);
}.
bind
(
this
));
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' IGNORE'
:
''
,
table
:
quoteIdentifier
(
tableName
),
attributes
:
attributeKeys
.
map
(
function
(
attr
)
{
return
quoteIdentifier
(
attr
);
}.
bind
(
this
)).
join
(
','
),
tuples
:
tuples
};
return
Utils
.
_
.
template
(
query
)(
replacements
);
},
insertSql
:
function
(
tableName
,
valueHash
,
modelAttributeMap
)
{
insertSql
:
function
(
tableName
,
valueHash
,
modelAttributeMap
)
{
var
query
var
query
,
valueQuery
=
'INSERT INTO <%= tableName %> (<%= attributes %>)'
,
valueQuery
=
'INSERT INTO <%= tableName %> (<%= attributes %>)'
...
@@ -393,7 +411,11 @@ module.exports = {
...
@@ -393,7 +411,11 @@ module.exports = {
template
.
push
(
attribute
.
type
.
toString
());
template
.
push
(
attribute
.
type
.
toString
());
//a primary key
//a primary key
if
(
attribute
.
primaryKey
){
if
(
attribute
.
primaryKey
){
template
.
push
(
attributeMap
.
primaryKey
);
if
(
!
attribute
.
references
)
{
template
.
push
(
attributeMap
.
primaryKey
);
}
else
{
template
.
push
(
attributeMap
.
foreignKey
);
}
//allow null
//allow null
}
else
if
(
attribute
.
allowNull
===
false
){
}
else
if
(
attribute
.
allowNull
===
false
){
template
.
push
(
attributeMap
.
notNull
);
template
.
push
(
attributeMap
.
notNull
);
...
@@ -424,7 +446,8 @@ module.exports = {
...
@@ -424,7 +446,8 @@ module.exports = {
if
(
attribute
.
references
)
{
if
(
attribute
.
references
)
{
template
.
push
(
attributeMap
.
references
+
quoteIdentifier
(
attribute
.
references
));
template
.
push
(
attributeMap
.
references
);
template
.
push
(
quoteIdentifier
(
attribute
.
references
));
if
(
attribute
.
referencesKey
)
{
if
(
attribute
.
referencesKey
)
{
template
.
push
(
'('
+
quoteIdentifier
(
attribute
.
referencesKey
)
+
')'
);
template
.
push
(
'('
+
quoteIdentifier
(
attribute
.
referencesKey
)
+
')'
);
...
@@ -491,10 +514,15 @@ module.exports = {
...
@@ -491,10 +514,15 @@ module.exports = {
}
}
return
attrString
.
join
(
', '
);
return
attrString
.
join
(
', '
);
},
},
getTopClause
:
function
(
limit
){
return
"TOP("
+
limit
+
")"
;
},
getSelectorClause
:
function
(
model
,
options
){
getSelectorClause
:
function
(
model
,
options
){
var
query
=
[
'SELECT'
];
var
query
=
[
'SELECT'
];
//we have joins
//we have joins
if
(
options
.
limit
&&
!
options
.
offset
){
query
.
push
(
this
.
getTopClause
(
options
.
limit
));
}
//add join table
//add join table
if
(
options
.
include
){
if
(
options
.
include
){
query
.
push
(
loadFieldsWithName
(
model
.
rawAttributes
,
model
.
name
));
query
.
push
(
loadFieldsWithName
(
model
.
rawAttributes
,
model
.
name
));
...
@@ -520,13 +548,21 @@ module.exports = {
...
@@ -520,13 +548,21 @@ module.exports = {
return
query
.
join
(
' '
);
return
query
.
join
(
' '
);
},
},
getJoinClause
:
function
(
model
,
include
){
getJoinClause
:
function
(
model
,
include
){
var
primaryKey
=
''
;
//console.log(include.through);
for
(
var
key
in
model
.
rawAttributes
){
var
query
=
[];
if
(
model
.
rawAttributes
[
key
].
primaryKey
){
var
primaryKey
=
quoteIdentifier
(
model
.
primaryKeyAttribute
);
primaryKey
=
quoteIdentifier
(
key
);
var
joinType
=
include
.
required
?
'INNER JOIN'
:
'LEFT OUTER JOIN'
;
break
;
var
joinTable
=
include
.
as
?
include
.
as
:
include
.
model
.
name
;
}
var
manyJoinTable
=
joinTable
;
joinTable
=
quoteIdentifier
(
joinTable
);
var
tableName
=
quoteIdentifier
(
model
.
name
);
var
hasManyToMany
=
false
;
query
.
push
(
joinType
);
if
(
include
.
through
){
hasManyToMany
=
true
;
}
}
//this logic handles the join types
var
associationType
=
include
.
association
.
associationType
;
var
associationType
=
include
.
association
.
associationType
;
var
rootKey
,
joinKey
;
var
rootKey
,
joinKey
;
if
(
associationType
===
'BelongsTo'
){
if
(
associationType
===
'BelongsTo'
){
...
@@ -535,43 +571,72 @@ module.exports = {
...
@@ -535,43 +571,72 @@ module.exports = {
}
else
if
(
associationType
===
'HasMany'
){
}
else
if
(
associationType
===
'HasMany'
){
rootKey
=
primaryKey
;
rootKey
=
primaryKey
;
joinKey
=
quoteIdentifier
(
include
.
association
.
identifier
);
joinKey
=
quoteIdentifier
(
include
.
association
.
identifier
);
if
(
hasManyToMany
){
//indicates many to many
manyJoinTable
=
quoteIdentifier
(
joinTable
+
'.'
+
include
.
through
.
as
);
//console.log(include.through);
query
=
query
.
concat
([
"("
,
quoteIdentifier
(
include
.
through
.
model
.
name
)
,
"AS"
,
manyJoinTable
,
joinType
,
joinTable
,
"AS"
,
joinTable
,
"ON"
,
joinTable
+
'.'
+
quoteIdentifier
(
include
.
model
.
primaryKeyAttribute
),
"="
,
manyJoinTable
+
'.'
+
quoteIdentifier
(
include
.
through
.
model
.
primaryKeyAttribute
),
")"
]);
}
}
else
{
}
else
{
rootKey
=
primaryKey
;
rootKey
=
primaryKey
;
joinKey
=
quoteIdentifier
(
include
.
association
.
foreignKey
);
joinKey
=
quoteIdentifier
(
include
.
association
.
foreignKey
);
}
}
var
joinType
=
include
.
required
?
'INNER JOIN'
:
'LEFT OUTER JOIN'
;
//var key, joinKey;
if
(
!
hasManyToMany
){
//console.log(include.association.associationType);
query
.
push
(
quoteIdentifier
(
include
.
model
.
tableName
));
//if(include.association)
query
.
push
(
'AS'
);
var
joinTable
=
include
.
as
?
include
.
as
:
include
.
model
.
name
;
query
.
push
(
joinTable
);
joinTable
=
quoteIdentifier
(
joinTable
);
}
var
tableName
=
quoteIdentifier
(
model
.
name
);
query
=
query
.
concat
([
return
[
joinType
,
quoteIdentifier
(
include
.
model
.
tableName
),
'AS'
,
joinTable
,
'ON'
,
tableName
+
'.'
+
rootKey
,
'ON'
,
tableName
+
'.'
+
rootKey
,
'='
,
joinTable
+
'.'
+
joinKey
'='
,
manyJoinTable
+
'.'
+
joinKey
].
join
(
' '
);
]);
if
(
include
.
where
){
query
.
push
(
'AND'
);
query
.
push
(
this
.
formatWhereCondition
(
include
.
where
,
joinTable
));
}
return
query
.
join
(
' '
);
},
},
getWhereClause
:
function
(
where
,
tableName
){
formatWhereCondition
:
function
(
where
,
tableName
){
var
query
=
[
'WHERE'
];
var
query
=
[];
//console.log(where);
//console.log(tableName);
for
(
var
key
in
where
){
for
(
var
key
in
where
){
var
val
=
where
[
key
];
var
val
=
where
[
key
];
var
operator
=
'='
;
var
operator
=
'='
;
if
(
val
===
'NULL'
)
{
if
(
!
val
)
{
operator
=
'IS'
;
operator
=
'IS'
;
}
}
if
(
tableName
){
query
.
push
(
quoteIdentifier
(
tableName
)
+
'.'
+
quoteIdentifier
(
key
));
query
.
push
(
quoteIdentifier
(
tableName
)
+
'.'
+
quoteIdentifier
(
key
));
}
else
{
query
.
push
(
quoteIdentifier
(
key
));
}
console
.
log
(
'val'
,
typeof
val
);
query
.
push
(
operator
);
query
.
push
(
operator
);
query
.
push
(
val
);
if
(
!
val
){
query
.
push
(
'NULL'
);
}
else
if
(
typeof
val
===
'number'
){
query
.
push
(
val
);
}
else
{
query
.
push
(
wrapSingleQuote
(
val
));
}
}
}
return
query
.
join
(
' '
);
return
query
.
join
(
' '
);
},
getWhereClause
:
function
(
where
,
tableName
){
return
[
'WHERE'
,
this
.
formatWhereCondition
(
where
,
tableName
)
].
join
(
' '
);
}};
}};
...
...
test/include/find.test.js
View file @
a2a21aa
...
@@ -22,7 +22,6 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -22,7 +22,6 @@ describe(Support.getTestDialectTeaser("Include"), function () {
// Associations
// Associations
A
.
hasMany
(
B
);
A
.
hasMany
(
B
);
B
.
belongsTo
(
B
);
B
.
belongsTo
(
D
);
B
.
belongsTo
(
D
);
B
.
hasMany
(
C
,
{
B
.
hasMany
(
C
,
{
through
:
'BC'
,
through
:
'BC'
,
...
...
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