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 6287a15a
authored
Oct 06, 2012
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use abstract query parser instead of the mysql query one
1 parent
a74e5574
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
61 deletions
lib/dialects/abstract/query.js
lib/dialects/mysql/query.js
spec/buster-helpers.js
lib/dialects/abstract/query.js
View file @
6287a15
...
@@ -25,8 +25,52 @@ module.exports = (function() {
...
@@ -25,8 +25,52 @@ module.exports = (function() {
throw
new
Error
(
"The run method wasn't overwritten!"
)
throw
new
Error
(
"The run method wasn't overwritten!"
)
}
}
AbstractQuery
.
prototype
.
formatResults
=
function
()
{
/**
this
.
emit
(
'fnord'
,
1
)
* High level function that handles the results of a query execution.
*
*
* Example:
* query.formatResults([
* {
* UserWithNames: {
* name: 'barfooz',
* id: 1,
* createdAt: Sat Oct 06 2012 14:46:36 GMT+0200 (CEST),
* updatedAt: Sat Oct 06 2012 14:46:36 GMT+0200 (CEST)
* },
* Tasks: {
* title: 'task',
* id: 1,
* createdAt: Sat Oct 06 2012 14:46:36 GMT+0200 (CEST),
* updatedAt: Sat Oct 06 2012 14:46:36 GMT+0200 (CEST),
* UserWithNameId: 1
* }
* }
* ])
*
* @param {Array} data - The result of the query execution.
*/
AbstractQuery
.
prototype
.
formatResults
=
function
(
data
)
{
var
result
=
this
.
callee
/*if(queryResultHasJoin.call(this, data)) {
console.log(data)
console.log(this.sql)
}*/
if
(
isInsertQuery
.
call
(
this
,
data
))
{
handleInsertQuery
.
call
(
this
,
data
)
}
if
(
isSelectQuery
.
call
(
this
))
{
result
=
handleSelectQuery
.
call
(
this
,
data
)
}
else
if
(
isShowTableQuery
.
call
(
this
))
{
result
=
handleShowTableQuery
.
call
(
this
,
data
)
}
else
if
(
isShowOrDescribeQuery
.
call
(
this
))
{
result
=
data
}
return
result
}
}
/**
/**
...
@@ -62,5 +106,98 @@ module.exports = (function() {
...
@@ -62,5 +106,98 @@ module.exports = (function() {
return
this
return
this
}
}
var
queryResultHasJoin
=
function
(
results
)
{
var
hasJoin
=
!!
results
[
0
]
hasJoin
=
hasJoin
&&
(
Utils
.
_
.
keys
(
results
[
0
]).
length
>
1
)
hasJoin
=
hasJoin
&&
(
Utils
.
isHash
(
results
[
0
][
Utils
.
_
.
keys
(
results
[
0
])[
0
]]))
return
hasJoin
}
var
isInsertQuery
=
function
(
results
)
{
var
result
=
!!
this
.
callee
result
=
result
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
===
0
)
result
=
result
&&
results
.
hasOwnProperty
(
'insertId'
)
return
result
}
var
handleInsertQuery
=
function
(
results
)
{
// add the inserted row id to the instance
var
autoIncrementField
=
this
.
callee
.
__factory
.
autoIncrementField
this
.
callee
[
autoIncrementField
]
=
results
.
insertId
}
var
isShowTableQuery
=
function
()
{
return
(
this
.
sql
.
indexOf
(
'SHOW TABLES'
)
===
0
)
}
var
handleShowTableQuery
=
function
(
results
)
{
return
Utils
.
_
.
flatten
(
results
.
map
(
function
(
resultSet
)
{
return
Utils
.
_
.
values
(
resultSet
)
}))
}
var
isSelectQuery
=
function
()
{
return
(
this
.
sql
.
indexOf
(
'SELECT'
)
===
0
)
}
var
handleSelectQuery
=
function
(
results
)
{
var
result
=
null
if
(
this
.
options
.
raw
)
{
result
=
results
}
else
if
(
queryResultHasJoin
(
results
))
{
result
=
results
.
map
(
function
(
result
)
{
// let's build the actual dao instance first...
var
dao
=
this
.
callee
.
build
(
result
[
this
.
callee
.
tableName
],
{
isNewRecord
:
false
})
// ... and afterwards the prefetched associations
for
(
var
tableName
in
result
)
{
if
(
result
.
hasOwnProperty
(
tableName
)
&&
(
tableName
!==
this
.
callee
.
tableName
))
{
buildAssociatedDaoInstances
.
call
(
this
,
tableName
,
result
[
tableName
],
dao
)
}
}
return
dao
}.
bind
(
this
))
}
else
{
result
=
results
.
map
(
function
(
result
)
{
return
this
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
}.
bind
(
this
))
}
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
this
.
options
.
plain
)
{
result
=
(
result
.
length
===
0
)
?
null
:
result
[
0
]
}
return
result
}
var
buildAssociatedDaoInstances
=
function
(
tableName
,
associatedTableName
,
dao
)
{
var
associatedDao
=
this
.
sequelize
.
daoFactoryManager
.
getDAO
(
tableName
,
{
attribute
:
'tableName'
})
,
association
=
this
.
callee
.
getAssociation
(
associatedDao
)
,
accessor
=
Utils
.
_
.
camelize
(
associatedDao
.
tableName
)
,
daoInstance
=
associatedDao
.
build
(
associatedTableName
,
{
isNewRecord
:
false
})
// downcase the first char
accessor
=
accessor
.
slice
(
0
,
1
).
toLowerCase
()
+
accessor
.
slice
(
1
)
if
([
'BelongsTo'
,
'HasOne'
].
indexOf
(
association
.
associationType
)
>
-
1
)
{
accessor
=
Utils
.
singularize
(
accessor
)
dao
[
accessor
]
=
daoInstance
}
else
{
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
dao
[
accessor
].
push
(
daoInstance
)
}
}
var
isShowOrDescribeQuery
=
function
()
{
return
(
this
.
sql
.
indexOf
(
'SHOW'
)
===
0
)
||
(
this
.
sql
.
indexOf
(
'DESCRIBE'
)
===
0
)
}
return
AbstractQuery
return
AbstractQuery
})()
})()
lib/dialects/mysql/query.js
View file @
6287a15
...
@@ -68,65 +68,8 @@ module.exports = (function() {
...
@@ -68,65 +68,8 @@ module.exports = (function() {
}
}
var
onSuccess
=
function
(
results
,
fields
)
{
var
onSuccess
=
function
(
results
,
fields
)
{
var
result
=
this
.
callee
,
self
=
this
,
hasJoin
=
!!
results
[
0
]
&&
(
Utils
.
_
.
keys
(
results
[
0
]).
length
>
1
)
&&
(
Utils
.
isHash
(
results
[
0
][
Utils
.
_
.
keys
(
results
[
0
])[
0
]]))
// add the inserted row id to the instance
if
(
this
.
callee
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
===
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
{
this
.
callee
[
this
.
callee
.
__factory
.
autoIncrementField
]
=
results
.
insertId
}
if
(
this
.
sql
.
indexOf
(
'SELECT'
)
===
0
)
{
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
this
.
options
.
raw
)
{
result
=
results
}
else
if
(
hasJoin
)
{
result
=
results
.
map
(
function
(
result
)
{
var
dao
=
this
.
callee
.
build
(
result
[
this
.
callee
.
tableName
],
{
isNewRecord
:
false
})
for
(
var
tableName
in
result
)
{
if
(
result
.
hasOwnProperty
(
tableName
)
&&
(
tableName
!==
this
.
callee
.
tableName
))
{
var
associatedDao
=
this
.
sequelize
.
daoFactoryManager
.
getDAO
(
tableName
,
{
attribute
:
'tableName'
})
,
association
=
this
.
callee
.
associations
[
tableName
]
,
accessor
=
Utils
.
_
.
camelize
(
association
.
associationAccessor
)
,
daoInstance
=
associatedDao
.
build
(
result
[
tableName
],
{
isNewRecord
:
false
})
// downcase the first char
accessor
=
accessor
.
slice
(
0
,
1
).
toLowerCase
()
+
accessor
.
slice
(
1
)
if
([
'BelongsTo'
,
'HasOne'
].
indexOf
(
association
.
associationType
)
>
-
1
)
{
accessor
=
Utils
.
singularize
(
accessor
)
dao
[
accessor
]
=
daoInstance
}
else
{
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
dao
[
accessor
].
push
(
daoInstance
)
}
}
}
return
dao
}.
bind
(
this
))
}
else
{
result
=
results
.
map
(
function
(
result
)
{
return
self
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
})
}
if
(
this
.
options
.
plain
)
{
result
=
(
result
.
length
===
0
)
?
null
:
result
[
0
]
}
}
else
if
(
this
.
sql
.
indexOf
(
'SHOW TABLES'
)
===
0
)
{
result
=
Utils
.
_
.
flatten
(
results
.
map
(
function
(
resultSet
)
{
return
Utils
.
_
.
values
(
resultSet
)
}))
}
else
if
((
this
.
sql
.
indexOf
(
'SHOW'
)
===
0
)
||
(
this
.
sql
.
indexOf
(
'DESCRIBE'
)
===
0
))
{
result
=
results
}
unbindClient
.
call
(
this
)
unbindClient
.
call
(
this
)
this
.
emit
(
'success'
,
result
)
this
.
emit
(
'success'
,
this
.
formatResults
(
results
)
)
}
}
...
...
spec/buster-helpers.js
View file @
6287a15
...
@@ -46,7 +46,7 @@ var BusterHelpers = module.exports = {
...
@@ -46,7 +46,7 @@ var BusterHelpers = module.exports = {
getSupportedDialects
:
function
()
{
getSupportedDialects
:
function
()
{
return
fs
.
readdirSync
(
__dirname
+
'/../lib/dialects'
).
filter
(
function
(
file
)
{
return
fs
.
readdirSync
(
__dirname
+
'/../lib/dialects'
).
filter
(
function
(
file
)
{
return
(
file
.
indexOf
(
'.js'
)
===
-
1
)
return
(
(
file
.
indexOf
(
'.js'
)
===
-
1
)
&&
(
file
.
indexOf
(
'abstract'
)
===
-
1
)
)
})
})
},
},
...
...
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