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 eb305c3f
authored
Nov 22, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
min, insert, update via query interface
1 parent
1c7e685b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
58 deletions
lib/model-factory.js
lib/model.js
lib/query-interface.js
lib/model-factory.js
View file @
eb305c3
...
@@ -81,14 +81,10 @@ module.exports = (function() {
...
@@ -81,14 +81,10 @@ module.exports = (function() {
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'max'
)
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'max'
)
}
}
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'min('
+
field
+
')'
,
'min'
])
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'min'
)
query
.
call
(
self
,
self
.
QueryGenerator
.
minQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
success
(
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'min'
])
})
})
return
emitter
.
run
()
}
}
ModelFactory
.
prototype
.
findAll
=
function
(
options
)
{
ModelFactory
.
prototype
.
findAll
=
function
(
options
)
{
...
@@ -96,6 +92,11 @@ module.exports = (function() {
...
@@ -96,6 +92,11 @@ module.exports = (function() {
}
}
ModelFactory
.
prototype
.
find
=
function
(
options
)
{
ModelFactory
.
prototype
.
find
=
function
(
options
)
{
if
([
null
,
undefined
].
indexOf
(
options
)
>
-
1
)
{
var
NullEmitter
=
require
(
"./emitters/null-emitter"
)
return
new
NullEmitter
()
}
// options is not a hash but an id
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
options
=
{
where
:
options
}
...
@@ -109,15 +110,11 @@ module.exports = (function() {
...
@@ -109,15 +110,11 @@ module.exports = (function() {
})
})
options
=
{
where
:
where
}
options
=
{
where
:
where
}
}
else
if
((
options
==
null
)
||
(
options
==
undefined
))
{
var
NullEmitter
=
require
(
"./emitters/null-emitter"
)
return
new
NullEmitter
()
}
}
options
.
limit
=
1
options
.
limit
=
1
var
sql
=
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
)
return
this
.
QueryInterface
.
select
(
this
,
this
.
tableName
,
options
,
{
plain
:
true
})
return
query
.
call
(
this
,
sql
,
this
,
{
plain
:
true
})
}
}
ModelFactory
.
prototype
.
build
=
function
(
values
,
options
)
{
ModelFactory
.
prototype
.
build
=
function
(
values
,
options
)
{
...
...
lib/model.js
View file @
eb305c3
...
@@ -32,7 +32,7 @@ module.exports = (function() {
...
@@ -32,7 +32,7 @@ module.exports = (function() {
})
})
Model
.
prototype
.
__defineGetter__
(
'QueryInterface'
,
function
()
{
Model
.
prototype
.
__defineGetter__
(
'QueryInterface'
,
function
()
{
return
this
.
__definition
.
modelManager
.
QueryInterface
return
this
.
__definition
.
modelManager
.
sequelize
.
getQueryInterface
()
})
})
Model
.
prototype
.
save
=
function
()
{
Model
.
prototype
.
save
=
function
()
{
...
@@ -42,19 +42,10 @@ module.exports = (function() {
...
@@ -42,19 +42,10 @@ module.exports = (function() {
this
[
attr
]
=
new
Date
()
this
[
attr
]
=
new
Date
()
if
(
this
.
isNewRecord
)
{
if
(
this
.
isNewRecord
)
{
var
self
=
this
return
this
.
QueryInterface
.
insert
(
this
,
this
.
__definition
.
tableName
,
this
.
values
)
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
insertQuery
(
self
.
__definition
.
tableName
,
self
.
values
))
.
success
(
function
(
obj
)
{
obj
.
isNewRecord
=
false
eventEmitter
.
emit
(
'success'
,
obj
)
})
.
error
(
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
})
return
eventEmitter
.
run
()
}
else
{
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
return
query
.
call
(
this
,
this
.
QueryGenerator
.
updateQuery
(
this
.
__definition
.
tableName
,
this
.
values
,
identifier
)
)
return
this
.
QueryInterface
.
update
(
this
,
this
.
__definition
.
tableName
,
this
.
values
,
identifier
)
}
}
}
}
...
...
lib/query-interface.js
View file @
eb305c3
...
@@ -42,20 +42,32 @@ module.exports = (function() {
...
@@ -42,20 +42,32 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
insert
=
function
()
{
QueryInterface
.
prototype
.
insert
=
function
(
model
,
tableName
,
values
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
query
.
call
(
self
,
self
.
QueryGenerator
.
insertQuery
(
tableName
,
values
),
model
)
.
success
(
function
(
obj
)
{
obj
.
isNewRecord
=
false
emitter
.
emit
(
'success'
,
obj
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
}
QueryInterface
.
prototype
.
update
=
function
()
{
QueryInterface
.
prototype
.
update
=
function
(
model
,
tableName
,
values
,
identifier
)
{
var
sql
=
this
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
)
return
query
.
call
(
this
,
sql
,
model
)
}
}
QueryInterface
.
prototype
.
destroy
=
function
()
{
QueryInterface
.
prototype
.
destroy
=
function
()
{
}
}
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
)
{
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
,
queryOptions
)
{
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
),
factory
)
queryOptions
=
queryOptions
||
{}
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
),
factory
,
queryOptions
)
}
}
QueryInterface
.
prototype
.
rawSelect
=
function
(
tableName
,
options
,
attributeSelector
)
{
QueryInterface
.
prototype
.
rawSelect
=
function
(
tableName
,
options
,
attributeSelector
)
{
...
@@ -75,35 +87,6 @@ module.exports = (function() {
...
@@ -75,35 +87,6 @@ module.exports = (function() {
}).
run
()
}).
run
()
}
}
QueryInterface
.
prototype
.
count
=
function
(
tableName
,
options
)
{
var
self
=
this
,
countOption
=
"count(*) as count"
options
=
options
||
{}
options
.
attributes
=
options
.
attributes
||
[]
options
.
attributes
.
push
([
'count(*)'
,
'count'
])
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
query
.
call
(
self
,
sql
,
factory
,
{
plain
:
true
}).
success
(
function
(
data
)
{
emitter
.
emit
(
'success'
,
data
.
count
)
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
success
(
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'count(*)'
])
})
})
return
emitter
.
run
()
}
// private
// private
...
...
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