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 da053191
authored
Sep 24, 2014
by
Joel Trost
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Destroyed the Select statement, now rebuilding
1 parent
5db3a650
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
222 additions
and
129 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query.js
lib/dialects/mssql/sql-generator.js
test/associations/alias.test.js
lib/dialects/abstract/query-generator.js
View file @
da05319
...
...
@@ -1259,7 +1259,7 @@ module.exports = (function() {
}
query
+=
';'
;
console
.
log
(
query
);
return
query
;
},
...
...
lib/dialects/mssql/query-generator.js
View file @
da05319
This diff is collapsed.
Click to expand it.
lib/dialects/mssql/query.js
View file @
da05319
...
...
@@ -31,7 +31,7 @@ module.exports = (function() {
}
var
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
console
.
log
(
self
.
sql
);
//
console.log(self.sql);
self
.
connection
...
...
lib/dialects/mssql/sql-generator.js
View file @
da05319
...
...
@@ -3,14 +3,13 @@
var
Utils
=
require
(
'../../utils'
)
,
SqlString
=
require
(
'../../sql-string'
)
,
DataTypes
=
require
(
'./data-types'
)
,
options
,
dialect
,
sequelize
;
,
_
options
,
_
dialect
,
_
sequelize
;
/*
Escape a value (e.g. a string, number or date)
*/
var
dialect
=
'mssql'
;
var
attributeMap
=
{
notNull
:
"NOT NULL"
,
allowNull
:
"NULL"
,
...
...
@@ -24,7 +23,13 @@ var attributeMap = {
onUpdate
:
"ON UPDATE"
,
default
:
"DEFAULT"
};
function
escape
(
value
,
field
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
value
.
toString
();
}
else
{
return
SqlString
.
escape
(
value
,
false
,
_options
.
timezone
,
_dialect
,
field
);
}
}
function
quoteIdentifier
(
identifier
,
force
)
{
if
(
identifier
===
'*'
)
return
identifier
;
return
Utils
.
addTicks
(
identifier
,
'"'
);
...
...
@@ -58,7 +63,39 @@ function nameIndexes(indexes, rawTablename) {
});
}
function
fieldsToSql
(
fields
,
singleQuote
){
var
fieldStr
=
[];
for
(
var
key
in
fields
)
{
if
(
fields
.
hasOwnProperty
(
key
))
{
if
(
singleQuote
){
fieldStr
.
push
(
wrapSingleQuote
(
key
));
}
else
{
fieldStr
.
push
(
quoteIdentifier
(
key
));
}
}
}
if
(
fieldStr
){
if
(
fieldStr
.
length
>
0
){
return
fieldStr
.
join
(
','
);
}
}
return
''
;
}
function
valuesToSql
(
fields
,
modelAttributeMap
){
var
values
=
[];
for
(
var
key
in
fields
)
{
if
(
fields
.
hasOwnProperty
(
key
))
{
var
value
=
fields
[
key
];
values
.
push
(
escape
(
value
,
(
modelAttributeMap
&&
modelAttributeMap
[
key
])
||
undefined
));
}
}
if
(
values
){
if
(
values
.
length
>
0
){
return
values
.
join
(
','
);
}
}
return
''
;
}
function
loadColumn
(
attributes
){
var
attrStr
=
[];
for
(
var
attr
in
attributes
)
{
...
...
@@ -75,7 +112,7 @@ function loadColumnWithTypes(attributes){
}
return
attrStr
;
}
function
addTableExistsWrapper
(
tableName
,
query
,
exists
){
function
addTableExistsWrapper
(
query
,
exists
){
return
[
"IF ("
,
(
exists
?
""
:
"NOT"
),
" EXISTS ("
,
...
...
@@ -86,34 +123,67 @@ function addTableExistsWrapper(tableName, query, exists){
"END"
].
join
(
" "
);
}
function
identityInsertOnWrapper
(
query
){
return
[
'SET IDENTITY_INSERT <%= tableName %> ON;'
,
query
,
'SET IDENTITY_INSERT <%= tableName %> OFF;'
].
join
(
' '
);
}
//select stuff
function
loadFields
(
attributes
){
var
attrStr
=
[];
for
(
var
attr
in
attributes
)
{
attrStr
.
push
(
quoteIdentifier
(
attr
));
}
return
attrStr
;
}
function
loadFields
(
attributes
,
tableName
){
var
attrStr
=
[];
for
(
var
attr
in
attributes
)
{
if
(
tableName
){
attrStr
.
push
(
quoteIdentifier
(
tableName
)
+
"."
+
quoteIdentifier
(
attr
));
}
else
{
attrStr
.
push
(
quoteIdentifier
(
attr
));
}
}
return
attrStr
.
join
(
','
);
}
function
joinFields
(
attributes
,
tableName
){
var
attrStr
=
[];
if
(
tableName
){
for
(
var
attr
in
attributes
)
{
attrStr
.
push
(
quoteIdentifier
(
tableName
)
+
"."
+
quoteIdentifier
(
attr
)
+
" AS "
+
quoteIdentifier
(
tableName
+
"."
+
attr
));
}
}
return
attrStr
.
join
(
','
);
}
module
.
exports
=
{
get
options
(){
return
options
;
return
_
options
;
},
set
options
(
opt
)
{
options
=
opt
;
_
options
=
opt
;
},
get
dialect
(){
return
dialect
;
return
_
dialect
;
},
set
dialect
(
dial
)
{
dialect
=
dial
;
_
dialect
=
dial
;
},
get
sequelize
(){
return
sequelize
;
return
_
sequelize
;
},
set
sequelize
(
seq
)
{
sequelize
=
seq
;
},
escape
:
function
(
value
,
field
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
value
.
toString
();
}
else
{
return
SqlString
.
escape
(
value
,
false
,
options
.
timezone
,
dialect
,
field
);
}
_sequelize
=
seq
;
},
showTableSql
:
function
(){
return
'SELECT name FROM sys.Tables;'
;
},
...
...
@@ -132,7 +202,7 @@ module.exports = {
tableName
:
quoteIdentifier
(
tableName
),
attributes
:
attrStr
.
join
(
", "
)
};
query
=
addTableExistsWrapper
(
tableName
,
query
);
query
=
addTableExistsWrapper
(
query
);
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
},
alterTableSql
:
function
(
tableName
){
...
...
@@ -148,55 +218,38 @@ module.exports = {
unquotedTable
:
tableName
,
tableName
:
quoteIdentifier
(
tableName
)
};
query
=
addTableExistsWrapper
(
tableName
,
query
,
true
);
query
=
addTableExistsWrapper
(
query
,
true
);
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
},
insertSql
:
function
(
table
,
valueHash
,
modelAttributes
,
options
)
{
options
=
options
||
{};
insertSql
:
function
(
tableName
,
valueHash
,
modelAttributeMap
)
{
var
query
,
valueQuery
=
'INSERT INTO <%= table %> (<%= attributes %>)'
,
emptyQuery
=
'INSERT INTO <%= table %>'
,
fields
=
[]
,
selFields
=
[]
,
values
=
[]
,
key
,
value
,
modelAttributeMap
=
{};
,
valueQuery
=
'INSERT INTO <%= tableName %> (<%= attributes %>)'
,
emptyQuery
=
'INSERT INTO <%= tableName %>'
;
valueQuery
+=
' OUTPUT <%= selFields %> VALUES (<%= values %>)'
;
emptyQuery
+=
' OUTPUT <%= selFields %> VALUES ()'
;
valueHash
=
Utils
.
removeNullValuesFromHash
(
valueHash
,
this
.
options
.
omitNull
);
for
(
key
in
valueHash
)
{
if
(
valueHash
.
hasOwnProperty
(
key
))
{
value
=
valueHash
[
key
];
// SERIALS' can't be NULL in postgresql, use DEFAULT where supported
if
(
modelAttributeMap
&&
modelAttributeMap
[
key
]
&&
modelAttributeMap
[
key
].
autoIncrement
&&
value
){
fields
.
push
(
quoteIdentifier
(
key
));
selFields
.
push
(
wrapSingleQuote
(
key
));
valueQuery
=
'SET IDENTITY_INSERT <%= table %> ON;'
+
valueQuery
+
'; SET IDENTITY_INSERT <%= table %> OFF'
;
values
.
push
(
this
.
escape
(
value
,
(
modelAttributeMap
&&
modelAttributeMap
[
key
])
||
undefined
));
}
else
if
(
value
)
{
fields
.
push
(
quoteIdentifier
(
key
));
selFields
.
push
(
wrapSingleQuote
(
key
));
values
.
push
(
this
.
escape
(
value
,
(
modelAttributeMap
&&
modelAttributeMap
[
key
])
||
undefined
));
}
emptyQuery
+=
' VALUES ()'
;
valueHash
=
Utils
.
removeNullValuesFromHash
(
valueHash
,
_options
.
omitNull
);
var
insertKey
=
false
;
for
(
var
key
in
valueHash
)
{
if
(
modelAttributeMap
[
key
].
autoIncrement
){
insertKey
=
true
;
}
}
var
replacements
=
{
table
:
quoteIdentifier
(
tabl
e
),
attributes
:
fields
.
join
(
','
),
selFields
:
selFields
.
join
(
','
),
values
:
values
.
join
(
','
)
table
Name
:
quoteIdentifier
(
tableNam
e
),
attributes
:
fields
ToSql
(
valueHash
,
false
),
selFields
:
fieldsToSql
(
valueHash
,
true
),
values
:
values
ToSql
(
valueHash
,
modelAttributeMap
)
};
query
=
(
replacements
.
attributes
.
length
?
valueQuery
:
emptyQuery
)
+
';'
;
if
(
insertKey
){
query
=
identityInsertOnWrapper
(
query
);
}
return
Utils
.
_
.
template
(
query
)(
replacements
);
},
...
...
@@ -288,7 +341,7 @@ module.exports = {
var
template
=
'VARCHAR(10) NOT NULL CHECK ("'
+
attribute
.
field
+
'" IN('
+
Utils
.
_
.
map
(
attribute
.
values
,
function
(
value
)
{
return
this
.
escape
(
value
);
return
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
'))'
;
return
template
;
},
...
...
@@ -327,7 +380,7 @@ module.exports = {
if
(
attribute
.
type
!==
'TEXT'
&&
attribute
.
type
.
_binary
===
false
&&
Utils
.
defaultValueSchemable
(
attribute
.
defaultValue
))
{
if
(
options
&&
this
.
escape
(
attribute
.
defaultValue
)){
if
(
options
&&
escape
(
attribute
.
defaultValue
)){
template
.
push
(
attributeMap
.
default
+
wrapSingleQuote
(
attribute
.
defaultValue
));
}
}
...
...
@@ -405,6 +458,46 @@ module.exports = {
}));
}
return
attrString
.
join
(
', '
);
},
getSelectorClause
:
function
(
model
,
options
){
var
query
=
[
'SELECT'
];
//we have joins
//add join table
if
(
options
.
include
){
query
.
push
(
loadFields
(
model
.
rawAttributes
,
model
.
name
));
for
(
var
i
=
0
;
i
<
options
.
include
.
length
;
i
++
){
if
(
options
.
include
[
i
].
as
)
{
query
.
push
(
joinFields
(
options
.
include
[
i
].
model
.
rawAttributes
,
options
.
include
[
i
].
as
));
}
}
}
else
{
query
.
push
(
loadFields
(
model
.
rawAttributes
));
}
return
query
.
join
(
' '
);
},
getFromClause
:
function
(
tableName
,
asValue
){
var
query
=
[
"FROM"
,
quoteIdentifier
(
tableName
)];
if
(
asValue
){
query
.
push
(
"AS"
);
query
.
push
(
quoteIdentifier
(
asValue
));
}
return
query
.
join
(
' '
);
},
getJoinClause
:
function
(
model
,
include
){
var
primaryKey
=
''
;
for
(
var
key
in
model
.
rawAttributes
){
if
(
model
.
rawAttributes
[
key
].
primaryKey
){
primaryKey
=
key
;
break
;
}
}
console
.
log
(
include
.
association
.
foreignKey
);
console
.
log
(
primaryKey
);
//for(var i = 0; i < )
var
joinType
=
include
.
required
?
' INNER JOIN '
:
' LEFT OUTER JOIN '
;
}
};
...
...
test/associations/alias.test.js
View file @
da05319
...
...
@@ -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