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 9419770e
authored
Oct 11, 2012
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed formatting of join query results
1 parent
0afc6f94
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
6 deletions
lib/dialects/abstract/query.js
lib/dialects/abstract/query.js
View file @
9419770
...
@@ -122,13 +122,37 @@ module.exports = (function() {
...
@@ -122,13 +122,37 @@ module.exports = (function() {
// private //
// private //
/////////////
/////////////
var
findTableNameInAttribute
=
function
(
attribute
)
{
var
tableName
=
null
this
.
sequelize
.
daoFactoryManager
.
daos
.
forEach
(
function
(
daoFactory
)
{
if
(
!!
tableName
)
{
return
}
else
{
var
regExp
=
new
RegExp
(
"^"
+
daoFactory
.
tableName
+
"\\."
)
,
match
=
attribute
.
match
(
regExp
)
if
(
!!
match
)
{
tableName
=
daoFactory
.
tableName
}
}
})
return
tableName
}
var
queryResultHasJoin
=
function
(
results
)
{
var
queryResultHasJoin
=
function
(
results
)
{
var
hasJoin
=
!!
results
[
0
]
if
(
!!
results
[
0
])
{
var
keys
=
Utils
.
_
.
keys
(
results
[
0
])
hasJoin
=
hasJoin
&&
(
Utils
.
_
.
keys
(
results
[
0
]).
length
>
1
)
for
(
var
i
=
0
;
i
<
keys
.
length
;
i
++
)
{
hasJoin
=
hasJoin
&&
(
Utils
.
isHash
(
results
[
0
][
Utils
.
_
.
keys
(
results
[
0
])[
0
]]))
if
(
!!
findTableNameInAttribute
.
call
(
this
,
keys
[
i
]))
{
return
true
}
}
}
return
hasJoin
return
false
}
}
var
isInsertQuery
=
function
(
results
)
{
var
isInsertQuery
=
function
(
results
)
{
...
@@ -169,8 +193,9 @@ module.exports = (function() {
...
@@ -169,8 +193,9 @@ module.exports = (function() {
if
(
this
.
options
.
raw
)
{
if
(
this
.
options
.
raw
)
{
result
=
results
result
=
results
}
else
if
(
queryResultHasJoin
(
results
))
{
}
else
if
(
queryResultHasJoin
.
call
(
this
,
results
))
{
result
=
groupDataByCalleeFactory
.
call
(
this
,
results
).
map
(
function
(
result
)
{
result
=
prepareJoinData
.
call
(
this
,
results
)
result
=
groupDataByCalleeFactory
.
call
(
this
,
result
).
map
(
function
(
result
)
{
// let's build the actual dao instance first...
// let's build the actual dao instance first...
var
dao
=
this
.
callee
.
build
(
result
[
this
.
callee
.
tableName
],
{
isNewRecord
:
false
})
var
dao
=
this
.
callee
.
build
(
result
[
this
.
callee
.
tableName
],
{
isNewRecord
:
false
})
...
@@ -287,5 +312,36 @@ module.exports = (function() {
...
@@ -287,5 +312,36 @@ module.exports = (function() {
return
result
return
result
}
}
/**
* This function will prepare the result of select queries with joins.
*
* @param {Array} data This array contains objects.
* @return {Array} The array will have the needed format for groupDataByCalleeFactory.
*/
var
prepareJoinData
=
function
(
data
)
{
var
result
=
data
.
map
(
function
(
row
)
{
var
nestedRow
=
{}
for
(
var
key
in
row
)
{
if
(
row
.
hasOwnProperty
(
key
))
{
var
tableName
=
findTableNameInAttribute
.
call
(
this
,
key
)
if
(
!!
tableName
)
{
nestedRow
[
tableName
]
=
nestedRow
[
tableName
]
||
{}
nestedRow
[
tableName
][
key
.
replace
(
tableName
+
'.'
,
''
)]
=
row
[
key
]
}
else
{
nestedRow
[
this
.
callee
.
tableName
]
=
nestedRow
[
this
.
callee
.
tableName
]
||
{}
nestedRow
[
this
.
callee
.
tableName
][
key
]
=
row
[
key
]
}
}
}
return
nestedRow
}.
bind
(
this
))
return
result
}
return
AbstractQuery
return
AbstractQuery
})()
})()
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