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 3b1bad08
authored
Jan 05, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1202 from mickhansen/findandcountall-include
Findandcountall include
2 parents
2461fb62
d76a3f23
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
68 additions
and
44 deletions
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/utils.js
test/include.test.js
lib/dao-factory.js
View file @
3b1bad0
...
@@ -66,6 +66,10 @@ module.exports = (function() {
...
@@ -66,6 +66,10 @@ module.exports = (function() {
}
}
})
})
Object
.
defineProperty
(
DAOFactory
.
prototype
,
'sequelize'
,
{
get
:
function
()
{
return
this
.
daoFactoryManager
.
sequelize
}
})
Object
.
defineProperty
(
DAOFactory
.
prototype
,
'QueryInterface'
,
{
Object
.
defineProperty
(
DAOFactory
.
prototype
,
'QueryInterface'
,
{
get
:
function
()
{
return
this
.
daoFactoryManager
.
sequelize
.
getQueryInterface
()
}
get
:
function
()
{
return
this
.
daoFactoryManager
.
sequelize
.
getQueryInterface
()
}
})
})
...
@@ -562,47 +566,46 @@ module.exports = (function() {
...
@@ -562,47 +566,46 @@ module.exports = (function() {
}
}
DAOFactory
.
prototype
.
count
=
function
(
options
)
{
DAOFactory
.
prototype
.
count
=
function
(
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
=
Utils
.
_
.
clone
(
options
||
{})
options
.
dataType
=
DataTypes
.
INTEGER
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
options
.
attributes
=
[
[
this
.
sequelize
.
fn
(
'COUNT'
,
this
.
sequelize
.
col
(
this
.
tableName
+
'.*'
)),
'count'
]
]
options
.
includeIgnoreAttributes
=
false
return
this
.
aggregate
(
'*'
,
'count'
,
options
)
this
.
find
(
options
,
{
raw
:
true
,
transaction
:
options
.
transaction
}).
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]}).
success
(
function
(
result
)
{
emitter
.
emit
(
'success'
,
parseInt
(
result
.
count
,
10
))
})
}.
bind
(
this
)).
run
()
}
}
DAOFactory
.
prototype
.
findAndCountAll
=
function
(
o
ptions
,
queryOptions
)
{
DAOFactory
.
prototype
.
findAndCountAll
=
function
(
findO
ptions
,
queryOptions
)
{
var
self
=
this
var
self
=
this
// no limit, offset, order, attributes
or include
for the options given to count()
// no limit, offset, order, attributes for the options given to count()
,
co
pts
=
Utils
.
_
.
omit
(
options
||
{},
[
'offset'
,
'limit'
,
'order'
,
'include
'
,
'attributes'
])
,
co
untOptions
=
Utils
.
_
.
omit
(
findOptions
||
{},
[
'offset'
,
'limit'
,
'order
'
,
'attributes'
])
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
emit
=
{
var
emit
=
{
err
:
function
(
e
)
{
// emit error
okay
:
function
(
count
,
results
)
{
// emit success
emitter
.
emit
(
'error'
,
e
);
emitter
.
emit
(
'success'
,
{
}
count
:
count
||
0
,
,
okay
:
function
(
c
,
r
)
{
// emit success
rows
:
(
results
&&
Array
.
isArray
(
results
)
?
results
:
[])
emitter
.
emit
(
'success'
,
{
})
count
:
c
||
0
,
rows
:
(
r
&&
Array
.
isArray
(
r
)
?
r
:
[])
});
}
,
sql
:
function
(
s
)
{
// emit SQL
emitter
.
emit
(
'sql'
,
s
);
}
}
}
}
self
.
count
(
copts
)
self
.
count
(
countOptions
)
.
on
(
'sql'
,
emit
.
sql
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
error
(
emit
.
err
)
.
success
(
function
(
count
)
{
.
success
(
function
(
cnt
)
{
if
(
count
===
0
)
{
if
(
cnt
===
0
)
{
return
emit
.
okay
(
count
)
// no records, no need for another query
return
emit
.
okay
(
cnt
)
// no records, no need for another query
}
}
self
.
findAll
(
options
,
queryOptions
)
self
.
findAll
(
findOptions
,
queryOptions
)
.
on
(
'sql'
,
emit
.
sql
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
error
(
emit
.
err
)
.
success
(
function
(
results
)
{
.
success
(
function
(
rows
)
{
emit
.
okay
(
count
,
results
)
emit
.
okay
(
cnt
,
rows
)
})
})
})
})
}).
run
()
}).
run
()
...
...
lib/dialects/abstract/query-generator.js
View file @
3b1bad0
...
@@ -469,13 +469,23 @@ module.exports = (function() {
...
@@ -469,13 +469,23 @@ module.exports = (function() {
options
.
table
=
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
t
)
{
return
this
.
quoteIdentifiers
(
t
)
}.
bind
(
this
)).
join
(
", "
)
:
this
.
quoteIdentifiers
(
tableName
)
options
.
table
=
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
t
)
{
return
this
.
quoteIdentifiers
(
t
)
}.
bind
(
this
)).
join
(
", "
)
:
this
.
quoteIdentifiers
(
tableName
)
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
var
addTable
=
true
if
(
attr
instanceof
Utils
.
fn
||
attr
instanceof
Utils
.
col
)
{
return
self
.
quote
(
attr
)
}
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
if
(
attr
[
0
]
instanceof
Utils
.
fn
||
attr
[
0
]
instanceof
Utils
.
col
)
{
attr
[
0
]
=
self
.
quote
(
attr
[
0
])
addTable
=
false
}
attr
=
[
attr
[
0
],
this
.
quoteIdentifier
(
attr
[
1
])].
join
(
' as '
)
attr
=
[
attr
[
0
],
this
.
quoteIdentifier
(
attr
[
1
])].
join
(
' as '
)
}
else
{
}
else
{
attr
=
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
&&
attr
.
indexOf
(
'"'
)
<
0
?
this
.
quoteIdentifiers
(
attr
)
:
attr
attr
=
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
&&
attr
.
indexOf
(
'"'
)
<
0
?
this
.
quoteIdentifiers
(
attr
)
:
attr
}
}
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
)
{
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
&&
addTable
)
{
attr
=
this
.
quoteIdentifier
(
options
.
table
)
+
'.'
+
attr
attr
=
this
.
quoteIdentifier
(
options
.
table
)
+
'.'
+
attr
}
}
...
@@ -494,7 +504,7 @@ module.exports = (function() {
...
@@ -494,7 +504,7 @@ module.exports = (function() {
,
association
=
include
.
association
,
association
=
include
.
association
,
through
=
include
.
through
,
through
=
include
.
through
,
joinType
=
include
.
required
?
' INNER JOIN '
:
' LEFT OUTER JOIN '
,
joinType
=
include
.
required
?
' INNER JOIN '
:
' LEFT OUTER JOIN '
,
where
=
{}
,
includeWhere
=
{}
,
whereOptions
=
Utils
.
_
.
clone
(
options
)
,
whereOptions
=
Utils
.
_
.
clone
(
options
)
if
(
tableName
!==
parentTable
)
as
=
parentTable
+
'.'
+
include
.
as
if
(
tableName
!==
parentTable
)
as
=
parentTable
+
'.'
+
include
.
as
...
@@ -502,19 +512,20 @@ module.exports = (function() {
...
@@ -502,19 +512,20 @@ module.exports = (function() {
if
(
include
.
where
)
{
if
(
include
.
where
)
{
for
(
var
key
in
include
.
where
)
{
for
(
var
key
in
include
.
where
)
{
if
(
include
.
where
.
hasOwnProperty
(
key
))
{
if
(
include
.
where
.
hasOwnProperty
(
key
))
{
w
here
[
self
.
quoteIdentifier
(
as
)
+
'.'
+
self
.
quoteIdentifiers
(
key
)]
=
include
.
where
[
key
]
includeW
here
[
self
.
quoteIdentifier
(
as
)
+
'.'
+
self
.
quoteIdentifiers
(
key
)]
=
include
.
where
[
key
]
}
}
}
}
include
.
where
=
where
whereOptions
.
keysEscaped
=
true
whereOptions
.
keysEscaped
=
true
}
}
attributes
=
include
.
attributes
.
map
(
function
(
attr
)
{
// includeIgnoreAttributes is used by aggregate functions
return
self
.
quoteIdentifier
(
as
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
as
+
"."
+
attr
)
if
(
options
.
includeIgnoreAttributes
!==
false
)
{
})
attributes
=
include
.
attributes
.
map
(
function
(
attr
)
{
return
self
.
quoteIdentifier
(
as
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
as
+
"."
+
attr
)
})
optAttributes
=
optAttributes
.
concat
(
attributes
)
optAttributes
=
optAttributes
.
concat
(
attributes
)
}
if
(
through
)
{
if
(
through
)
{
var
throughTable
=
through
.
daoFactory
.
tableName
var
throughTable
=
through
.
daoFactory
.
tableName
...
@@ -523,7 +534,7 @@ module.exports = (function() {
...
@@ -523,7 +534,7 @@ module.exports = (function() {
return
self
.
quoteIdentifier
(
throughAs
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
throughAs
+
"."
+
attr
)
return
self
.
quoteIdentifier
(
throughAs
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
throughAs
+
"."
+
attr
)
})
})
optAttributes
=
optAttributes
.
concat
(
throughAttributes
)
if
(
options
.
includeIgnoreAttributes
!==
false
)
optAttributes
=
optAttributes
.
concat
(
throughAttributes
)
var
primaryKeysSource
=
Object
.
keys
(
association
.
source
.
primaryKeys
)
var
primaryKeysSource
=
Object
.
keys
(
association
.
source
.
primaryKeys
)
,
tableSource
=
parentTable
,
tableSource
=
parentTable
...
@@ -544,7 +555,7 @@ module.exports = (function() {
...
@@ -544,7 +555,7 @@ module.exports = (function() {
joinQueryItem
+=
self
.
quoteIdentifier
(
throughAs
)
+
"."
+
self
.
quoteIdentifier
(
identTarget
)
joinQueryItem
+=
self
.
quoteIdentifier
(
throughAs
)
+
"."
+
self
.
quoteIdentifier
(
identTarget
)
if
(
include
.
where
)
{
if
(
include
.
where
)
{
joinQueryItem
+=
" AND "
+
self
.
hashToWhereConditions
(
include
.
w
here
,
include
.
daoFactory
,
whereOptions
)
joinQueryItem
+=
" AND "
+
self
.
hashToWhereConditions
(
include
W
here
,
include
.
daoFactory
,
whereOptions
)
}
}
}
else
{
}
else
{
var
primaryKeysLeft
=
((
association
.
associationType
===
'BelongsTo'
)
?
Object
.
keys
(
association
.
target
.
primaryKeys
)
:
Object
.
keys
(
include
.
association
.
source
.
primaryKeys
))
var
primaryKeysLeft
=
((
association
.
associationType
===
'BelongsTo'
)
?
Object
.
keys
(
association
.
target
.
primaryKeys
)
:
Object
.
keys
(
include
.
association
.
source
.
primaryKeys
))
...
@@ -558,7 +569,7 @@ module.exports = (function() {
...
@@ -558,7 +569,7 @@ module.exports = (function() {
joinQueryItem
+=
self
.
quoteIdentifier
(
tableRight
)
+
"."
+
self
.
quoteIdentifier
(
attrRight
)
joinQueryItem
+=
self
.
quoteIdentifier
(
tableRight
)
+
"."
+
self
.
quoteIdentifier
(
attrRight
)
if
(
include
.
where
)
{
if
(
include
.
where
)
{
joinQueryItem
+=
" AND "
+
self
.
hashToWhereConditions
(
include
.
w
here
,
include
.
daoFactory
,
whereOptions
)
joinQueryItem
+=
" AND "
+
self
.
hashToWhereConditions
(
include
W
here
,
include
.
daoFactory
,
whereOptions
)
}
}
}
}
...
@@ -852,6 +863,7 @@ module.exports = (function() {
...
@@ -852,6 +863,7 @@ module.exports = (function() {
,
_key
,
_key
,
_value
=
null
,
_value
=
null
if
(
options
.
keysEscaped
)
{
if
(
options
.
keysEscaped
)
{
_key
=
key
_key
=
key
}
else
{
}
else
{
...
...
lib/dialects/mysql/query-generator.js
View file @
3b1bad0
...
@@ -387,6 +387,7 @@ module.exports = (function() {
...
@@ -387,6 +387,7 @@ module.exports = (function() {
},
},
quoteIdentifier
:
function
(
identifier
,
force
)
{
quoteIdentifier
:
function
(
identifier
,
force
)
{
if
(
identifier
===
'*'
)
return
identifier
return
Utils
.
addTicks
(
identifier
,
"`"
)
return
Utils
.
addTicks
(
identifier
,
"`"
)
},
},
...
...
lib/dialects/postgres/query-generator.js
View file @
3b1bad0
...
@@ -774,6 +774,7 @@ module.exports = (function() {
...
@@ -774,6 +774,7 @@ module.exports = (function() {
},
},
quoteIdentifier
:
function
(
identifier
,
force
)
{
quoteIdentifier
:
function
(
identifier
,
force
)
{
if
(
identifier
===
'*'
)
return
identifier
if
(
!
force
&&
this
.
options
&&
this
.
options
.
quoteIdentifiers
===
false
)
{
// default is `true`
if
(
!
force
&&
this
.
options
&&
this
.
options
.
quoteIdentifiers
===
false
)
{
// default is `true`
// In Postgres, if tables or attributes are created double-quoted,
// In Postgres, if tables or attributes are created double-quoted,
// they are also case sensitive. If they contain any uppercase
// they are also case sensitive. If they contain any uppercase
...
...
lib/dialects/sqlite/query-generator.js
View file @
3b1bad0
...
@@ -380,6 +380,7 @@ module.exports = (function() {
...
@@ -380,6 +380,7 @@ module.exports = (function() {
},
},
quoteIdentifier
:
function
(
identifier
,
force
)
{
quoteIdentifier
:
function
(
identifier
,
force
)
{
if
(
identifier
===
'*'
)
return
identifier
return
Utils
.
addTicks
(
identifier
,
"`"
)
return
Utils
.
addTicks
(
identifier
,
"`"
)
},
},
...
...
lib/utils.js
View file @
3b1bad0
...
@@ -580,6 +580,7 @@ Utils.fn.prototype.toString = function(queryGenerator) {
...
@@ -580,6 +580,7 @@ Utils.fn.prototype.toString = function(queryGenerator) {
}
}
Utils
.
col
.
prototype
.
toString
=
function
(
queryGenerator
)
{
Utils
.
col
.
prototype
.
toString
=
function
(
queryGenerator
)
{
if
(
this
.
col
.
indexOf
(
'*'
)
!==
-
1
)
return
'*'
return
queryGenerator
.
quote
(
this
.
col
)
return
queryGenerator
.
quote
(
this
.
col
)
}
}
...
...
test/include.test.js
View file @
3b1bad0
...
@@ -1345,7 +1345,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -1345,7 +1345,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
describe
(
'findAndCountAll'
,
function
()
{
describe
(
'findAndCountAll'
,
function
()
{
x
it
(
'should include associations to findAndCountAll'
,
function
(
done
)
{
it
(
'should include associations to findAndCountAll'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{})
var
User
=
this
.
sequelize
.
define
(
'User'
,
{})
,
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
'test'
:
DataTypes
.
STRING
})
,
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
'test'
:
DataTypes
.
STRING
})
...
@@ -1381,12 +1381,17 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -1381,12 +1381,17 @@ describe(Support.getTestDialectTeaser("Include"), function () {
chainer
.
run
().
done
(
callback
)
chainer
.
run
().
done
(
callback
)
}]
}]
},
function
()
{
},
function
()
{
User
.
findAndCountAll
({
'where'
:
{
'item.test'
:
'def'
},
'include'
:
[
Item
]}).
done
(
function
(
err
,
result
)
{
User
.
findAndCountAll
({
include
:
[
{
model
:
Item
,
where
:
{
test
:
'def'
}}
]}).
done
(
function
(
err
,
result
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
result
.
count
).
to
.
eql
(
1
)
expect
(
result
.
count
).
to
.
eql
(
1
)
expect
(
result
.
rows
.
length
).
to
.
eql
(
1
)
expect
(
result
.
rows
.
length
).
to
.
eql
(
1
)
expect
(
result
.
rows
[
0
].
item
.
test
).
to
.
eql
(
'def'
)
expect
(
result
.
rows
[
0
].
item
.
test
).
to
.
eql
(
'def'
)
done
()
})
})
})
})
})
})
...
...
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