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 878e22cc
authored
Oct 10, 2013
by
Rafael Martins
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First steps to eager loading
1 parent
4794fd37
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
7 deletions
lib/dialects/sqlite/query-generator.js
test/associations/multiple-level-filters.test.js
lib/dialects/sqlite/query-generator.js
View file @
878e22c
...
...
@@ -225,19 +225,61 @@ module.exports = (function() {
var
query
=
"SELECT "
+
options
.
attributes
+
" FROM "
+
options
.
table
query
+=
joinQuery
var
innerQuery
=
"SELECT "
+
options
.
table
+
'.*\r\nFROM '
+
options
.
table
var
whereClauses
=
'1=1'
;
if
(
options
.
hasOwnProperty
(
'where'
))
{
options
.
where
=
this
.
getWhereConditions
(
options
.
where
,
tableName
,
factory
)
query
+=
" WHERE "
+
options
.
where
var
self
=
this
;
(
function
(){
var
keys
=
Object
.
keys
(
options
.
where
)
,
dao
=
factory
;
keys
.
forEach
(
function
(
key
)
{
var
filterValue
=
options
.
where
[
key
];
key
.
split
(
'.'
).
forEach
(
function
(
attribute
){
var
isAssociation
=
false
;
Object
.
keys
(
dao
.
associations
).
forEach
(
function
(
associationName
){
var
accessor
=
dao
.
associations
[
associationName
].
associationAccessor
accessor
=
Utils
.
singularize
(
accessor
[
0
].
toLowerCase
()
+
accessor
.
slice
(
1
));
if
(
attribute
===
accessor
){
var
targetTableName
=
dao
.
associations
[
associationName
].
target
.
tableName
,
targetColumnName
=
dao
.
associations
[
associationName
].
target
.
autoIncrementField
,
sourceTableName
=
dao
.
tableName
,
sourceColumnName
=
dao
.
associations
[
associationName
].
identifier
innerQuery
+=
'\r\n\r\nLEFT JOIN '
+
self
.
quoteIdentifier
(
targetTableName
)
innerQuery
+=
'\r\nON '
+
self
.
quoteIdentifier
(
targetTableName
)
+
'.'
+
self
.
quoteIdentifier
(
targetColumnName
)
+
' = '
+
self
.
quoteIdentifier
(
sourceTableName
)
+
'.'
+
self
.
quoteIdentifier
(
sourceColumnName
)
dao
=
dao
.
associations
[
associationName
].
target
;
isAssociation
=
true
;
}
});
if
(
!
isAssociation
){
var
smth
,
result
,
hash
=
{};
hash
[
attribute
]
=
filterValue
;
smth
=
Utils
.
prependTableNameToHash
(
dao
.
tableName
,
hash
)
result
=
self
.
hashToWhereConditions
(
smth
)
whereClauses
+=
' AND '
+
result
}
})
});
})();
innerQuery
+=
" WHERE "
+
whereClauses
}
if
(
options
.
group
)
{
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
group
q
uery
+=
" GROUP BY "
+
options
.
group
innerQ
uery
+=
" GROUP BY "
+
options
.
group
}
if
(
options
.
order
)
{
options
.
order
=
Array
.
isArray
(
options
.
order
)
?
options
.
order
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
order
q
uery
+=
" ORDER BY "
+
options
.
order
innerQ
uery
+=
" ORDER BY "
+
options
.
order
}
if
(
options
.
offset
&&
!
options
.
limit
)
{
...
...
@@ -245,15 +287,20 @@ module.exports = (function() {
* If no limit is defined, our best bet is to use the max number of rows in a table. From the SQLite docs:
* A 140 terabytes database can hold no more than approximately 1e+13 rows
*/
q
uery
+=
" LIMIT "
+
options
.
offset
+
", "
+
10000000000000
;
innerQ
uery
+=
" LIMIT "
+
options
.
offset
+
", "
+
10000000000000
;
}
else
if
(
options
.
limit
&&
!
(
options
.
include
&&
(
options
.
limit
===
1
)))
{
if
(
options
.
offset
)
{
q
uery
+=
" LIMIT "
+
options
.
offset
+
", "
+
options
.
limit
innerQ
uery
+=
" LIMIT "
+
options
.
offset
+
", "
+
options
.
limit
}
else
{
q
uery
+=
" LIMIT "
+
options
.
limit
innerQ
uery
+=
" LIMIT "
+
options
.
limit
}
}
query
=
'SELECT * FROM (\r\n'
+
innerQuery
+
') as '
+
options
.
table
+
'\r\n'
+
joinQuery
query
+=
";"
return
query
...
...
test/associations/multiple-level-filters.test.js
0 → 100644
View file @
878e22c
/* jshint camelcase: false, expr: true */
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
chai
.
Assertion
.
includeStack
=
true
describe
(
Support
.
getTestDialectTeaser
(
"Multiple Level Filters"
),
function
()
{
it
(
'can filter through relations'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
,
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
title
:
DataTypes
.
STRING
})
Project
.
belongsTo
(
User
);
//User.hasMany(Project)
Task
.
belongsTo
(
Project
);
//Project.hasMany(Task);
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([{
id
:
101
,
username
:
'leia'
},
{
id
:
102
,
username
:
'vader'
}]).
success
(
function
()
{
Project
.
bulkCreate
([{
id
:
201
,
UserId
:
101
,
title
:
'republic'
},{
id
:
202
,
UserId
:
102
,
title
:
'empire'
}]).
success
(
function
()
{
Task
.
bulkCreate
([{
id
:
301
,
ProjectId
:
201
,
title
:
'fight empire'
},{
id
:
302
,
ProjectId
:
201
,
title
:
'stablish republic'
},{
id
:
303
,
ProjectId
:
202
,
title
:
'destroy rebel alliance'
},{
id
:
304
,
ProjectId
:
202
,
title
:
'rule everything'
}]).
success
(
function
()
{
Task
.
findAll
({
where
:
{
'project.user.username'
:
'leia'
}
}).
success
(
function
(
tasks
){
try
{
expect
(
tasks
.
length
).
to
.
be
.
equal
(
2
);
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
'fight empire'
);
expect
(
tasks
[
1
].
title
).
to
.
be
.
equal
(
'stablish republic'
);
done
();
}
catch
(
e
){
done
(
e
);
}
})
});
});
});
})
})
})
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