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 6307f1dc
authored
Sep 26, 2014
by
Joel Trost
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes to where clause generation
1 parent
089cedc8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
73 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/sql-generator.js
test/associations/alias.test.js
lib/dialects/mssql/query-generator.js
View file @
6307f1d
...
...
@@ -391,10 +391,9 @@ module.exports = (function() {
// Enter and change at your own peril -- Mick Hansen
//console.log('tablename', tableName);
//console.log('options', options);
//console.log('model', model);
//console.log('model', model
.name
);
//console.log(options.where);
options
=
options
||
{};
var
query
=
[
SqlGenerator
.
getSelectorClause
(
model
,
options
),
SqlGenerator
.
getFromClause
(
model
.
tableName
,
model
.
name
)
...
...
@@ -501,7 +500,9 @@ module.exports = (function() {
getWhereConditions
:
function
(
where
,
tableName
,
model
,
options
,
prepend
)
{
//console.log('where:', model);
console
.
log
(
'logic'
,
where
);
//console.log('logic', where);
//console.log('options', options);
if
(
where
){
return
SqlGenerator
.
getWhereClause
(
where
,
tableName
);
}
else
{
...
...
lib/dialects/mssql/sql-generator.js
View file @
6307f1d
...
...
@@ -486,8 +486,8 @@ module.exports = {
//we have joins
//add join table
query
.
push
(
loadFieldsWithName
(
model
.
rawAttributes
,
model
.
name
));
if
(
options
.
include
){
query
.
push
(
loadFieldsWithName
(
model
.
rawAttributes
,
model
.
name
));
query
.
push
(
','
);
for
(
var
i
=
0
;
i
<
options
.
include
.
length
;
i
++
){
if
(
options
.
include
[
i
].
as
)
{
...
...
@@ -495,6 +495,8 @@ module.exports = {
,
options
.
include
[
i
].
as
));
}
}
}
else
{
query
.
push
(
loadFieldsWithName
(
model
.
rawAttributes
));
}
return
query
.
join
(
' '
);
},
...
...
@@ -517,6 +519,7 @@ module.exports = {
}
var
associationType
=
include
.
association
.
associationType
;
var
rootKey
=
primaryKey
;
console
.
log
(
include
.
association
);
var
joinKey
=
quoteIdentifier
(
include
.
association
.
foreignKey
);
if
(
associationType
===
'BelongsTo'
){
rootKey
=
quoteIdentifier
(
include
.
association
.
foreignKey
);
...
...
@@ -530,7 +533,6 @@ module.exports = {
var
joinTable
=
include
.
as
?
include
.
as
:
include
.
model
.
name
;
joinTable
=
quoteIdentifier
(
joinTable
);
var
tableName
=
quoteIdentifier
(
model
.
name
);
//console.log(include);
return
[
joinType
,
quoteIdentifier
(
include
.
model
.
tableName
),
...
...
@@ -541,6 +543,8 @@ module.exports = {
},
getWhereClause
:
function
(
where
,
tableName
){
var
query
=
[
'WHERE'
];
//console.log(where);
//console.log(tableName);
for
(
var
key
in
where
){
if
(
tableName
){
query
.
push
(
quoteIdentifier
(
tableName
)
+
'.'
+
quoteIdentifier
(
key
));
...
...
test/associations/alias.test.js
View file @
6307f1d
...
...
@@ -38,72 +38,72 @@ describe(Support.getTestDialectTeaser("Alias"), function() {
});
});
//
it('shouldnt touch the passed alias', function () {
//
var User = this.sequelize.define('user', {})
//
, Task = this.sequelize.define('task', {});
//
User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' });
//
Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' });
//
return this.sequelize.sync({ force: true }).then(function () {
//
return User.create({ id: 1 });
//
}).then(function (user){
//
expect(user.getASSIGNMENTS).to.be.ok;
//
return Task.create({ id: 1, userId: 1 });
//
}).then(function (task) {
//
expect(task.getOWNER).to.be.ok;
//
return Promise.all([
//
User.find({ where: { id: 1 }, include: [{model: Task, as: 'ASSIGNMENTS'}] }),
//
Task.find({ where: { id: 1 }, include: [{model: User, as: 'OWNER'}] }),
//
]);
//
}).spread(function (user, task) {
//
expect(user.ASSIGNMENTS).to.be.ok;
//
expect(task.OWNER).to.be.ok;
//
});
//
});
//
it('should allow me to pass my own plural and singular forms to hasMany', function () {
//
var User = this.sequelize.define('user', {})
//
, Task = this.sequelize.define('task', {});
//
User.hasMany(Task, { as: { singular: 'task', plural: 'taskz'} });
//
return this.sequelize.sync({ force: true }).then(function () {
//
return User.create({ id: 1 });
//
}).then(function (user) {
//
expect(user.getTaskz).to.be.ok;
//
expect(user.addTask).to.be.ok;
//
expect(user.addTaskz).to.be.ok;
//
}).then(function () {
//
return User.find({ where: { id: 1 }, include: [{model: Task, as: 'taskz'}] });
//
}).then(function (user) {
//
expect(user.taskz).to.be.ok;
//
});
//
});
//
it('should allow me to define plural and singular forms on the model', function () {
//
var User = this.sequelize.define('user', {})
//
, Task = this.sequelize.define('task', {}, {
//
name: {
//
singular: 'assignment',
//
plural: 'assignments'
//
}
//
});
//
User.hasMany(Task);
//
return this.sequelize.sync({ force: true }).then(function () {
//
return User.create({ id: 1 });
//
}).then(function (user) {
//
expect(user.getAssignments).to.be.ok;
//
expect(user.addAssignment).to.be.ok;
//
expect(user.addAssignments).to.be.ok;
//
}).then(function () {
//
return User.find({ where: { id: 1 }, include: [Task] });
//
}).then(function (user) {
//
expect(user.assignments).to.be.ok;
//
});
//
});
it
(
'shouldnt touch the passed alias'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{})
,
Task
=
this
.
sequelize
.
define
(
'task'
,
{});
User
.
hasMany
(
Task
,
{
as
:
'ASSIGNMENTS'
,
foreignKey
:
'userId'
});
Task
.
belongsTo
(
User
,
{
as
:
'OWNER'
,
foreignKey
:
'userId'
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
id
:
1
});
}).
then
(
function
(
user
){
expect
(
user
.
getASSIGNMENTS
).
to
.
be
.
ok
;
return
Task
.
create
({
id
:
1
,
userId
:
1
});
}).
then
(
function
(
task
)
{
expect
(
task
.
getOWNER
).
to
.
be
.
ok
;
return
Promise
.
all
([
User
.
find
({
where
:
{
id
:
1
},
include
:
[{
model
:
Task
,
as
:
'ASSIGNMENTS'
}]
}),
Task
.
find
({
where
:
{
id
:
1
},
include
:
[{
model
:
User
,
as
:
'OWNER'
}]
}),
]);
}).
spread
(
function
(
user
,
task
)
{
expect
(
user
.
ASSIGNMENTS
).
to
.
be
.
ok
;
expect
(
task
.
OWNER
).
to
.
be
.
ok
;
});
});
it
(
'should allow me to pass my own plural and singular forms to hasMany'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{})
,
Task
=
this
.
sequelize
.
define
(
'task'
,
{});
User
.
hasMany
(
Task
,
{
as
:
{
singular
:
'task'
,
plural
:
'taskz'
}
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
id
:
1
});
}).
then
(
function
(
user
)
{
expect
(
user
.
getTaskz
).
to
.
be
.
ok
;
expect
(
user
.
addTask
).
to
.
be
.
ok
;
expect
(
user
.
addTaskz
).
to
.
be
.
ok
;
}).
then
(
function
()
{
return
User
.
find
({
where
:
{
id
:
1
},
include
:
[{
model
:
Task
,
as
:
'taskz'
}]
});
}).
then
(
function
(
user
)
{
expect
(
user
.
taskz
).
to
.
be
.
ok
;
});
});
it
(
'should allow me to define plural and singular forms on the model'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{})
,
Task
=
this
.
sequelize
.
define
(
'task'
,
{},
{
name
:
{
singular
:
'assignment'
,
plural
:
'assignments'
}
});
User
.
hasMany
(
Task
);
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
id
:
1
});
}).
then
(
function
(
user
)
{
expect
(
user
.
getAssignments
).
to
.
be
.
ok
;
expect
(
user
.
addAssignment
).
to
.
be
.
ok
;
expect
(
user
.
addAssignments
).
to
.
be
.
ok
;
}).
then
(
function
()
{
return
User
.
find
({
where
:
{
id
:
1
},
include
:
[
Task
]
});
}).
then
(
function
(
user
)
{
expect
(
user
.
assignments
).
to
.
be
.
ok
;
});
});
});
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