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 1c7e685b
authored
Nov 22, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored count and max method
1 parent
36d748f4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
9 deletions
lib/model-factory.js
lib/query-interface.js
lib/query.js
lib/model-factory.js
View file @
1c7e685
...
@@ -68,16 +68,17 @@ module.exports = (function() {
...
@@ -68,16 +68,17 @@ module.exports = (function() {
}
}
ModelFactory
.
prototype
.
count
=
function
(
options
)
{
ModelFactory
.
prototype
.
count
=
function
(
options
)
{
return
this
.
QueryInterface
.
count
(
this
,
this
.
tableName
,
options
)
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'count(*)'
,
'count'
])
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'count'
)
}
}
ModelFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
ModelFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
query
.
call
(
self
,
self
.
QueryGenerator
.
maxQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
success
(
function
(
obj
)
{
options
.
attributes
.
push
([
'max('
+
field
+
')'
,
'max'
])
emitter
.
emit
(
'success'
,
obj
[
'max'
])
})
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'max'
)
})
return
emitter
.
run
()
}
}
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
var
self
=
this
...
...
lib/query-interface.js
View file @
1c7e685
...
@@ -58,7 +58,25 @@ module.exports = (function() {
...
@@ -58,7 +58,25 @@ module.exports = (function() {
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
),
factory
)
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
),
factory
)
}
}
QueryInterface
.
prototype
.
count
=
function
(
factory
,
tableName
,
options
)
{
QueryInterface
.
prototype
.
rawSelect
=
function
(
tableName
,
options
,
attributeSelector
)
{
var
self
=
this
if
(
attributeSelector
==
undefined
)
throw
new
Error
(
'Please pass an attribute selector!'
)
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
query
.
call
(
self
,
sql
,
null
,
{
plain
:
true
,
raw
:
true
}).
success
(
function
(
data
)
{
emitter
.
emit
(
'success'
,
data
[
attributeSelector
])
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
QueryInterface
.
prototype
.
count
=
function
(
tableName
,
options
)
{
var
self
=
this
var
self
=
this
,
countOption
=
"count(*) as count"
,
countOption
=
"count(*) as count"
...
...
lib/query.js
View file @
1c7e685
...
@@ -62,7 +62,13 @@ module.exports = (function() {
...
@@ -62,7 +62,13 @@ module.exports = (function() {
// transform results into real model instances
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
this
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
if
(
this
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
result
=
results
.
map
(
function
(
result
)
{
return
self
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
})
if
(
this
.
options
.
raw
)
{
result
=
results
}
else
{
result
=
results
.
map
(
function
(
result
)
{
return
self
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
})
}
if
(
this
.
options
.
plain
)
if
(
this
.
options
.
plain
)
result
=
(
result
.
length
==
0
)
?
null
:
result
[
0
]
result
=
(
result
.
length
==
0
)
?
null
:
result
[
0
]
...
...
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