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 43ed6755
authored
Jan 30, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix bug with limit: 1 and findAndCountAll
1 parent
f5fb6122
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
85 additions
and
27 deletions
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/utils.js
test/dao-factory/findAll.test.js
lib/dao-factory.js
View file @
43ed675
...
...
@@ -545,7 +545,9 @@ module.exports = (function() {
}
options
=
paranoidClause
.
call
(
this
,
options
)
options
.
limit
=
1
if
(
options
.
limit
===
undefined
)
{
options
.
limit
=
1
}
return
this
.
QueryInterface
.
select
(
this
,
this
.
getTableName
(),
options
,
Utils
.
_
.
defaults
({
plain
:
true
,
...
...
@@ -586,7 +588,9 @@ module.exports = (function() {
options
.
attributes
=
[
[
this
.
sequelize
.
fn
(
'COUNT'
,
this
.
sequelize
.
col
(
this
.
tableName
+
'.*'
)),
'count'
]
]
options
.
includeIgnoreAttributes
=
false
options
.
limit
=
null
this
.
find
(
options
,
{
raw
:
true
,
transaction
:
options
.
transaction
}).
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]}).
success
(
function
(
result
)
{
emitter
.
emit
(
'success'
,
parseInt
(
result
.
count
,
10
))
...
...
@@ -610,18 +614,18 @@ module.exports = (function() {
}
self
.
count
(
countOptions
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
count
)
{
if
(
count
===
0
)
{
return
emit
.
okay
(
count
)
// no records, no need for another query
}
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
count
)
{
if
(
count
===
0
)
{
return
emit
.
okay
(
count
)
// no records, no need for another query
}
self
.
findAll
(
findOptions
,
queryOptions
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
results
)
{
emit
.
okay
(
count
,
results
)
})
})
self
.
findAll
(
findOptions
,
queryOptions
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
.
success
(
function
(
results
)
{
emit
.
okay
(
count
,
results
)
})
})
}).
run
()
}
...
...
lib/dialects/abstract/query-generator.js
View file @
43ed675
...
...
@@ -473,8 +473,8 @@ module.exports = (function() {
,
mainQueryItems
=
[]
,
mainAttributes
=
options
.
attributes
,
mainJoinQueries
=
[]
// We'll use a subquery if
there's a limit, if we have hasMany associations and if any of them are filtered
,
subQuery
=
limit
&&
options
&&
options
.
hasIncludeWhere
&&
options
.
hasIncludeRequired
&&
options
.
hasMultiAssociation
// We'll use a subquery if
we have hasMany associations and a limit or a filtered/required association
,
subQuery
=
(
limit
||
options
.
hasIncludeWhere
||
options
.
hasIncludeRequired
)
&&
options
.
hasMultiAssociation
,
subQueryItems
=
[]
,
subQueryAttributes
=
null
,
subJoinQueries
=
[]
...
...
@@ -534,7 +534,9 @@ module.exports = (function() {
,
includeWhere
=
{}
,
whereOptions
=
Utils
.
_
.
clone
(
options
)
if
(
tableName
!==
parentTable
)
as
=
parentTable
+
'.'
+
include
.
as
if
(
tableName
!==
parentTable
)
{
as
=
parentTable
+
'.'
+
include
.
as
}
if
(
include
.
where
)
{
for
(
var
key
in
include
.
where
)
{
...
...
@@ -795,12 +797,12 @@ module.exports = (function() {
return
"ROLLBACK;"
},
addLimitAndOffset
:
function
(
options
,
query
){
addLimitAndOffset
:
function
(
options
,
query
)
{
query
=
query
||
""
if
(
options
.
offset
&&
!
options
.
limit
)
{
query
+=
" LIMIT "
+
options
.
offset
+
", "
+
10000000000000
;
}
else
if
(
options
.
limit
&&
!
(
options
.
include
&&
(
options
.
limit
===
1
))
)
{
}
else
if
(
options
.
limit
)
{
if
(
options
.
offset
)
{
query
+=
" LIMIT "
+
options
.
offset
+
", "
+
options
.
limit
}
else
{
...
...
lib/dialects/postgres/query-generator.js
View file @
43ed675
...
...
@@ -410,14 +410,12 @@ module.exports = (function() {
addLimitAndOffset
:
function
(
options
,
query
){
query
=
query
||
""
if
(
!
(
options
.
include
&&
(
options
.
limit
===
1
)))
{
if
(
options
.
limit
)
{
query
+=
" LIMIT "
+
options
.
limit
}
if
(
options
.
limit
)
{
query
+=
" LIMIT "
+
options
.
limit
}
if
(
options
.
offset
)
{
query
+=
" OFFSET "
+
options
.
offset
}
if
(
options
.
offset
)
{
query
+=
" OFFSET "
+
options
.
offset
}
return
query
;
...
...
lib/dialects/sqlite/query-generator.js
View file @
43ed675
...
...
@@ -153,7 +153,7 @@ module.exports = (function() {
query
=
query
||
""
if
(
options
.
offset
&&
!
options
.
limit
)
{
query
+=
" LIMIT "
+
options
.
offset
+
", "
+
10000000000000
;
}
else
if
(
options
.
limit
&&
!
(
options
.
include
&&
(
options
.
limit
===
1
))
)
{
}
else
if
(
options
.
limit
)
{
if
(
options
.
offset
)
{
query
+=
" LIMIT "
+
options
.
offset
+
", "
+
options
.
limit
}
else
{
...
...
lib/utils.js
View file @
43ed675
...
...
@@ -599,10 +599,12 @@ Utils.fn.prototype.toString = function(queryGenerator) {
}
Utils
.
col
.
prototype
.
toString
=
function
(
queryGenerator
)
{
if
(
this
.
col
.
indexOf
(
'*'
)
!==
-
1
)
return
'*'
if
(
this
.
col
.
indexOf
(
'*'
)
===
0
)
{
return
'*'
}
return
queryGenerator
.
quote
(
this
.
col
)
}
Utils
.
CustomEventEmitter
=
require
(
__dirname
+
"/emitters/custom-event-emitter"
)
Utils
.
QueryChainer
=
require
(
__dirname
+
"/query-chainer"
)
Utils
.
Lingo
=
require
(
"lingo"
)
Utils
.
Lingo
=
require
(
"lingo"
)
\ No newline at end of file
test/dao-factory/findAll.test.js
View file @
43ed675
...
...
@@ -957,6 +957,57 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it
(
"handles offset with includes"
,
function
(
done
)
{
var
Election
=
this
.
sequelize
.
define
(
'Election'
,
{
name
:
Sequelize
.
STRING
})
var
Citizen
=
this
.
sequelize
.
define
(
'Citizen'
,
{
name
:
Sequelize
.
STRING
})
// Associations
Election
.
belongsTo
(
Citizen
)
Election
.
hasMany
(
Citizen
,
{
as
:
'Voters'
,
through
:
'ElectionsVotes'
})
Citizen
.
hasMany
(
Election
)
Citizen
.
hasMany
(
Election
,
{
as
:
'Votes'
,
through
:
'ElectionsVotes'
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
be
.
ok
// Add some data
Citizen
.
create
({
name
:
'Alice'
}).
done
(
function
(
err
,
alice
)
{
expect
(
err
).
not
.
be
.
ok
Citizen
.
create
({
name
:
'Bob'
}).
done
(
function
(
err
,
bob
)
{
expect
(
err
).
not
.
be
.
ok
Election
.
create
({
name
:
'Some election'
}).
done
(
function
(
err
,
election
)
{
expect
(
err
).
not
.
be
.
ok
election
.
setCitizen
(
alice
).
done
(
function
(
err
)
{
expect
(
err
).
not
.
be
.
ok
election
.
setVoters
([
alice
,
bob
]).
done
(
function
(
err
)
{
expect
(
err
).
not
.
be
.
ok
var
criteria
=
{
offset
:
5
,
limit
:
1
,
include
:
[
Citizen
,
// Election creator
{
model
:
Citizen
,
as
:
'Voters'
}
// Election voters
]
}
Election
.
findAndCountAll
(
criteria
).
done
(
function
(
err
,
elections
)
{
expect
(
err
).
not
.
be
.
ok
expect
(
elections
.
count
).
to
.
equal
(
2
)
expect
(
elections
.
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
})
})
})
it
(
"handles attributes"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
where
:
"id != "
+
this
.
users
[
0
].
id
,
attributes
:
[
'data'
]}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
to
.
equal
(
2
)
...
...
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