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 33bdb8f5
authored
Jun 30, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Plain Diff
merge
2 parents
c8066605
ac3acf58
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
199 additions
and
8 deletions
lib/associations/has-many-single-linked.js
lib/associations/has-one.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
lib/sequelize.js
lib/utils.js
spec-jasmine/dao.spec.js
spec-jasmine/mysql/query-generator.spec.js
spec-jasmine/postgres/query-generator.spec.js
spec-jasmine/sqlite/query-generator.spec.js
lib/associations/has-many-single-linked.js
View file @
33bdb8f
...
...
@@ -18,7 +18,7 @@ module.exports = (function() {
// clear the old associations
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
__factory
.
identifier
]
=
null
associatedObject
[
self
.
__factory
.
identifier
]
=
self
.
options
.
omitNull
?
''
:
null
associatedObject
.
save
()
})
...
...
lib/associations/has-one.js
View file @
33bdb8f
...
...
@@ -53,7 +53,7 @@ module.exports = (function() {
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
obj
[
self
.
accessors
.
get
]().
success
(
function
(
oldObj
)
{
if
(
oldObj
)
{
oldObj
[
self
.
identifier
]
=
null
oldObj
[
self
.
identifier
]
=
self
.
options
.
omitNull
?
''
:
null
;
oldObj
.
save
()
}
...
...
lib/dialects/mysql/query-generator.js
View file @
33bdb8f
...
...
@@ -129,6 +129,16 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
attrValueHash
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
attrValueHash
=
_attrValueHash
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
...
...
@@ -142,6 +152,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
values
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
values
=
_attrValueHash
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
lib/dialects/postgres/query-generator.js
View file @
33bdb8f
...
...
@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
options
:
{},
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
Utils
.
_
.
extend
({
},
options
||
{})
...
...
@@ -191,7 +193,18 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
attrValueHash
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
attrValueHash
=
_attrValueHash
}
var
returning
=
[]
Utils
.
_
.
forEach
(
attrValueHash
,
function
(
value
,
key
,
hash
)
{
if
(
tables
[
tableName
]
&&
tables
[
tableName
][
key
])
{
switch
(
tables
[
tableName
][
key
])
{
...
...
@@ -216,6 +229,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
values
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
values
=
_attrValueHash
}
var
replacements
=
{
table
:
addQuotes
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
lib/dialects/sqlite/query-generator.js
View file @
33bdb8f
...
...
@@ -15,6 +15,8 @@ var escape = function(str) {
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
options
:
{},
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
options
||
{}
...
...
@@ -44,6 +46,16 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
;
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
attrValueHash
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
attrValueHash
=
_attrValueHash
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
...
...
@@ -57,6 +69,16 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if
(
this
.
options
.
omitNull
)
{
_attrValueHash
=
{}
Utils
.
_
.
each
(
values
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_attrValueHash
[
key
]
=
val
;
}
})
values
=
_attrValueHash
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
lib/query-interface.js
View file @
33bdb8f
...
...
@@ -5,6 +5,7 @@ module.exports = (function() {
var
QueryInterface
=
function
(
sequelize
)
{
this
.
sequelize
=
sequelize
this
.
QueryGenerator
=
require
(
'./dialects/'
+
this
.
sequelize
.
options
.
dialect
+
'/query-generator'
)
this
.
QueryGenerator
.
options
=
this
.
sequelize
.
options
;
}
Utils
.
addEventEmitter
(
QueryInterface
)
...
...
lib/sequelize.js
View file @
33bdb8f
...
...
@@ -19,7 +19,8 @@ module.exports = (function() {
define
:
{},
query
:
{},
sync
:
{},
logging
:
console
.
log
logging
:
console
.
log
,
omitNull
:
false
},
options
||
{})
if
(
this
.
options
.
logging
===
true
)
{
...
...
@@ -49,7 +50,7 @@ module.exports = (function() {
Sequelize
.
Utils
.
_
.
map
(
DataTypes
,
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
})
Sequelize
.
prototype
.
getQueryInterface
=
function
()
{
this
.
queryInterface
=
this
.
queryInterface
||
new
QueryInterface
(
this
)
this
.
queryInterface
=
this
.
queryInterface
||
new
QueryInterface
(
this
)
return
this
.
queryInterface
}
...
...
@@ -67,6 +68,7 @@ module.exports = (function() {
if
(
this
.
options
.
define
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
options
.
omitNull
=
this
.
options
.
omitNull
var
factory
=
new
DAOFactory
(
daoName
,
attributes
,
options
)
...
...
lib/utils.js
View file @
33bdb8f
...
...
@@ -97,6 +97,37 @@ var Utils = module.exports = {
toDefaultValue
:
function
(
value
)
{
return
(
value
==
DataTypes
.
NOW
)
?
new
Date
()
:
value
},
setAttributes
:
function
(
hash
,
identifier
,
instance
,
prefix
)
{
prefix
=
prefix
||
''
if
(
this
.
isHash
(
identifier
))
{
this
.
_
.
each
(
identifier
,
function
(
elem
,
key
)
{
hash
[
prefix
+
key
]
=
Utils
.
_
.
isString
(
instance
)
?
instance
:
Utils
.
_
.
isObject
(
instance
)
?
instance
[
elem
.
key
||
elem
]
:
null
})
}
else
{
hash
[
prefix
+
identifier
]
=
Utils
.
_
.
isString
(
instance
)
?
instance
:
Utils
.
_
.
isObject
(
instance
)
?
instance
.
id
:
null
}
return
hash
},
removeNullValuesFromHash
:
function
(
hash
,
omitNull
)
{
var
result
=
hash
if
(
omitNull
)
{
var
_hash
=
{}
Utils
.
_
.
each
(
hash
,
function
(
val
,
key
)
{
if
(
val
!==
null
&&
val
!==
undefined
)
{
_hash
[
key
]
=
val
;
}
})
result
=
_hash
}
return
result
}
}
...
...
spec-jasmine/dao.spec.js
View file @
33bdb8f
spec-jasmine/mysql/query-generator.spec.js
View file @
33bdb8f
...
...
@@ -89,6 +89,21 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
undefined
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
omitNull
:
true
}}
}
],
...
...
@@ -105,6 +120,17 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
name
:
"foo';DROP TABLE myTable;"
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `name`='foo\\';DROP TABLE myTable;' WHERE `name`='foo'"
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'"
,
context
:
{
options
:
{
omitNull
:
true
}}
}
],
...
...
@@ -177,7 +203,10 @@ describe('QueryGenerator', function() {
tests
.
forEach
(
function
(
test
)
{
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
it
(
title
,
function
()
{
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
null
,
test
.
arguments
)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var
context
=
test
.
context
||
{
options
:
{}};
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
context
,
test
.
arguments
)
expect
(
conditions
).
toEqual
(
test
.
expectation
)
})
})
...
...
spec-jasmine/postgres/query-generator.spec.js
View file @
33bdb8f
...
...
@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
nullValue
:
null
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
nullValue
:
null
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
nullValue
:
null
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;"
,
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
nullValue
:
undefined
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;"
,
context
:
{
options
:
{
omitNull
:
true
}}
}
],
...
...
@@ -101,7 +116,22 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
name
:
"foo';DROP TABLE myTable;"
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"name\"='foo\\';DROP TABLE myTable;' WHERE \"name\"='foo'"
}
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'"
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'"
,
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
undefined
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'"
,
context
:
{
options
:
{
omitNull
:
true
}}
},
],
deleteQuery
:
[
...
...
@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
tests
.
forEach
(
function
(
test
)
{
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
it
(
title
,
function
()
{
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
null
,
test
.
arguments
)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var
context
=
test
.
context
||
{
options
:
{}};
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
context
,
test
.
arguments
)
expect
(
conditions
).
toEqual
(
test
.
expectation
)
})
})
...
...
spec-jasmine/sqlite/query-generator.spec.js
View file @
33bdb8f
...
...
@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
name
:
"foo"
,
value
:
false
}],
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
undefined
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
omitNull
:
true
}}
}
],
...
...
@@ -50,6 +65,17 @@ describe('QueryGenerator', function() {
},
{
arguments
:
[
'myTable'
,
{
flag
:
false
},
{
id
:
2
}],
expectation
:
"UPDATE `myTable` SET `flag`=0 WHERE `id`=2"
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
,
context
:
{
options
:
{
omitNull
:
false
}}
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'"
,
context
:
{
options
:
{
omitNull
:
true
}}
}
]
};
...
...
@@ -59,7 +85,10 @@ describe('QueryGenerator', function() {
tests
.
forEach
(
function
(
test
)
{
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
it
(
title
,
function
()
{
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
null
,
test
.
arguments
)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var
context
=
test
.
context
||
{
options
:
{}};
var
conditions
=
QueryGenerator
[
suiteTitle
].
apply
(
context
,
test
.
arguments
)
expect
(
conditions
).
toEqual
(
test
.
expectation
)
})
})
...
...
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