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 d07f3187
authored
Oct 22, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored query chaining
1 parent
f7aea3b0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
32 deletions
examples/ChainQueries/app.js
lib/sequelize/QueryChainer.js
lib/sequelize/Sequelize.js
test/Table/associations.js
examples/ChainQueries/app.js
0 → 100644
View file @
d07f318
/*
Title: Using the chainQueries function
This example demonstrates the use of chainQueries.
*/
var
Sequelize
=
require
(
__dirname
+
"/../../lib/sequelize/Sequelize"
).
Sequelize
var
sys
=
require
(
"sys"
)
var
Class
=
function
(){
sys
.
log
(
"You've just created a new instance!"
)
}
Class
.
prototype
.
add
=
function
(
a
,
b
,
callback
){
this
.
result
=
a
+
b
sys
.
log
(
"The result: "
+
this
.
result
)
callback
(
this
.
result
)
}
sys
.
log
(
"First of all the old and obsolete way:"
)
Sequelize
.
chainQueries
([
{
add
:
(
new
Class
()),
params
:
[
1
,
2
]},
{
add
:
(
new
Class
()),
params
:
[
2
,
3
]}
],
function
()
{
sys
.
log
(
"And we did it!"
)
})
sys
.
puts
(
""
)
sys
.
log
(
"The new fashioned way is about removing the array and pass an arbitrary amount of parameters!"
)
sys
.
log
(
"Just pass as many hashes as you want, but at the end a function as callback!"
)
Sequelize
.
chainQueries
(
{
add
:
new
Class
(),
params
:
[
1
,
2
]},
{
add
:
new
Class
(),
params
:
[
2
,
3
]},
function
()
{
sys
.
log
(
"And we did it! Great!"
)
}
)
sys
.
puts
(
""
)
sys
.
log
(
"As you see we add some values two times."
)
sys
.
log
(
"Let's say you want to call a method on multiple objects with the same or no parameters!"
)
Sequelize
.
chainQueries
(
{
add
:
[
new
Class
(),
new
Class
()],
params
:
[
1
,
2
]},
function
()
{
sys
.
log
(
"And we did it! Great!"
)
}
)
\ No newline at end of file
lib/sequelize/QueryChainer.js
0 → 100644
View file @
d07f318
var
sys
=
require
(
"sys"
)
var
QueryChainer
=
function
(
Helper
)
{
this
.
Helper
=
Helper
}
var
instanceMethods
=
{
splitQueryHash
:
function
(
queryHash
)
{
var
method
=
this
.
Helper
.
Array
.
without
(
this
.
Helper
.
Hash
.
keys
(
queryHash
),
"params"
)[
0
],
object
=
queryHash
[
method
]
return
{
method
:
method
,
object
:
object
}
},
splitArguments
:
function
()
{
var
result
=
{
queries
:
[],
callback
:
null
}
for
(
var
i
=
0
;
i
<
arguments
.
length
;
i
++
)
{
var
arg
=
arguments
[
i
]
if
(
typeof
arg
==
'function'
)
result
.
callback
=
arg
else
if
(
Array
.
isArray
(
arg
))
arg
.
forEach
(
function
(
o
)
{
result
.
queries
.
push
(
o
)
})
else
result
.
queries
.
push
(
arg
)
}
return
result
},
expandMultiQueries
:
function
(
queries
)
{
var
self
=
this
,
result
=
[],
multiQueries
=
[]
queries
.
forEach
(
function
(
queryHash
)
{
var
splittedQueryHash
=
self
.
splitQueryHash
(
queryHash
),
method
=
splittedQueryHash
.
method
,
object
=
splittedQueryHash
.
object
if
(
!
Array
.
isArray
(
object
))
result
.
push
(
queryHash
)
else
if
(
object
.
length
>
0
)
{
for
(
var
i
=
0
;
i
<
object
.
length
;
i
++
)
{
var
newQueryHash
=
{
params
:
queryHash
.
params
}
newQueryHash
[
method
]
=
object
[
i
]
result
.
push
(
newQueryHash
)
}
}
})
return
result
},
executeQuery
:
function
(
queries
,
index
,
callback
)
{
var
self
=
this
,
queryHash
=
queries
[
index
],
splittedQueryHash
=
this
.
splitQueryHash
(
queryHash
),
method
=
splittedQueryHash
.
method
,
object
=
splittedQueryHash
.
object
var
iterator
=
function
()
{
if
(
queries
.
length
>
(
index
+
1
))
self
.
executeQuery
(
queries
,
index
+
1
,
callback
)
else
if
(
callback
)
callback
()
}
object
[
method
].
apply
(
object
,
this
.
Helper
.
Array
.
join
(
queryHash
.
params
||
[],
[
iterator
]))
},
chain
:
function
()
{
var
self
=
this
var
args
=
this
.
splitArguments
.
apply
(
this
,
arguments
),
queries
=
args
.
queries
,
callback
=
args
.
callback
var
expandedQueries
=
this
.
expandMultiQueries
(
queries
)
if
(
queries
.
length
>
0
)
this
.
executeQuery
(
expandedQueries
,
0
,
callback
)
else
if
(
callback
)
callback
()
}
}
for
(
var
methodName
in
instanceMethods
)
QueryChainer
.
prototype
[
methodName
]
=
instanceMethods
[
methodName
]
module
.
exports
.
QueryChainer
=
QueryChainer
\ No newline at end of file
lib/sequelize/Sequelize.js
View file @
d07f318
...
...
@@ -61,39 +61,11 @@ var classMethods = {
return
Sequelize
.
Helper
.
evaluateTemplate
(
query
,
values
)
},
chainQueries
:
function
(
)
{
// queries = [{method: object}, {method: object, params: [1,2,3]}, {method: object}]
// example: Sequelize.chainQueries({save: a}, {destroy: b}, callback)
// example: Sequelize.chainQueries({save: [a, b]}, callback)
chainQueries
:
function
()
{
var
QueryChainer
=
require
(
"./QueryChainer"
).
QueryChainer
,
queryChainer
=
new
QueryChainer
(
Sequelize
.
Helper
)
var
queries
=
[],
callback
=
null
for
(
var
i
=
0
;
i
<
arguments
.
length
;
i
++
)
{
var
arg
=
arguments
[
i
]
if
(
typeof
arg
==
'function'
)
callback
=
arg
else
if
(
Array
.
isArray
(
arg
))
arg
.
forEach
(
function
(
o
)
{
queries
.
push
(
o
)
})
else
queries
.
push
(
arg
)
}
var
executeQuery
=
function
(
index
)
{
var
queryHash
=
queries
[
index
],
method
=
Sequelize
.
Helper
.
Array
.
without
(
Sequelize
.
Helper
.
Hash
.
keys
(
queryHash
),
"params"
)[
0
],
object
=
queryHash
[
method
],
iterator
=
function
()
{
if
(
queries
.
length
>
(
index
+
1
))
executeQuery
(
index
+
1
)
else
if
(
callback
)
callback
()
}
if
(
!
Array
.
isArray
(
object
))
object
=
[
object
]
object
.
forEach
(
function
(
o
)
{
o
[
method
].
apply
(
o
,
Sequelize
.
Helper
.
Array
.
join
(
queryHash
.
params
||
[],
[
iterator
]))
})
}
if
(
queries
.
length
>
0
)
executeQuery
(
0
)
else
if
(
callback
)
callback
()
queryChainer
.
chain
.
apply
(
queryChainer
,
arguments
)
}
}
...
...
test/Table/associations.js
View file @
d07f318
...
...
@@ -32,6 +32,7 @@ module.exports = {
})
beforeExit
(
function
()
{
assert
.
match
(
1
,
2
)
assert
.
isNotNull
(
assoc
)
assert
.
equal
(
assoc
.
length
,
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