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 054c7569
authored
Jun 22, 2012
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
based omitNull on master
1 parent
31f6bd8e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
138 additions
and
7 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
spec-jasmine/mysql/query-generator.spec.js
spec-jasmine/postgres/query-generator.spec.js
spec-jasmine/sqlite/query-generator.spec.js
lib/dialects/mysql/query-generator.js
View file @
054c756
...
@@ -129,6 +129,16 @@ module.exports = (function() {
...
@@ -129,6 +129,16 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
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
=
{
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
...
@@ -142,6 +152,17 @@ module.exports = (function() {
...
@@ -142,6 +152,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= 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
=
{
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
lib/dialects/postgres/query-generator.js
View file @
054c756
...
@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
...
@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
options
:
{},
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
Utils
.
_
.
extend
({
options
=
Utils
.
_
.
extend
({
},
options
||
{})
},
options
||
{})
...
@@ -191,6 +193,16 @@ module.exports = (function() {
...
@@ -191,6 +193,16 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
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
}
returning
=
[]
returning
=
[]
Utils
.
_
.
forEach
(
attrValueHash
,
function
(
value
,
key
,
hash
)
{
Utils
.
_
.
forEach
(
attrValueHash
,
function
(
value
,
key
,
hash
)
{
if
(
tables
[
tableName
]
&&
tables
[
tableName
][
key
])
{
if
(
tables
[
tableName
]
&&
tables
[
tableName
][
key
])
{
...
@@ -216,6 +228,17 @@ module.exports = (function() {
...
@@ -216,6 +228,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= 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
=
{
var
replacements
=
{
table
:
addQuotes
(
tableName
),
table
:
addQuotes
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
lib/dialects/sqlite/query-generator.js
View file @
054c756
...
@@ -15,6 +15,8 @@ var escape = function(str) {
...
@@ -15,6 +15,8 @@ var escape = function(str) {
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
options
:
{},
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
options
||
{}
options
=
options
||
{}
...
@@ -44,6 +46,16 @@ module.exports = (function() {
...
@@ -44,6 +46,16 @@ module.exports = (function() {
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
;
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
=
{
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
...
@@ -57,6 +69,16 @@ module.exports = (function() {
...
@@ -57,6 +69,16 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
values
,
where
)
{
updateQuery
:
function
(
tableName
,
values
,
where
)
{
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= 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
=
{
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
...
...
spec-jasmine/mysql/query-generator.spec.js
View file @
054c756
...
@@ -95,11 +95,15 @@ describe('QueryGenerator', function() {
...
@@ -95,11 +95,15 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
,
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
,
context
:
{
options
:
{
ignore
Null
:
false
}}
context
:
{
options
:
{
omit
Null
:
false
}}
},
{
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
ignoreNull
:
true
}}
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
}}
}
}
],
],
...
@@ -122,11 +126,11 @@ describe('QueryGenerator', function() {
...
@@ -122,11 +126,11 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
,
expectation
:
"UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
,
context
:
{
options
:
{
ignore
Null
:
false
}}
context
:
{
options
:
{
omit
Null
:
false
}}
},
{
},
{
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
arguments
:
[
'myTable'
,
{
bar
:
2
,
nullValue
:
null
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'"
,
expectation
:
"UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'"
,
context
:
{
options
:
{
ignore
Null
:
true
}}
context
:
{
options
:
{
omit
Null
:
true
}}
}
}
],
],
...
...
spec-jasmine/postgres/query-generator.spec.js
View file @
054c756
...
@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
...
@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
}],
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
}],
expectation
:
"INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;"
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() {
...
@@ -101,7 +116,22 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
name
:
"foo';DROP TABLE myTable;"
},
{
name
:
'foo'
}],
arguments
:
[
'myTable'
,
{
name
:
"foo';DROP TABLE myTable;"
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"name\"='foo\\';DROP TABLE myTable;' WHERE \"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
:
[
deleteQuery
:
[
...
@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
...
@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
tests
.
forEach
(
function
(
test
)
{
tests
.
forEach
(
function
(
test
)
{
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
it
(
title
,
function
()
{
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
)
expect
(
conditions
).
toEqual
(
test
.
expectation
)
})
})
})
})
...
...
spec-jasmine/sqlite/query-generator.spec.js
View file @
054c756
...
@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
...
@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
name
:
"foo"
,
value
:
false
}],
arguments
:
[
'myTable'
,
{
name
:
"foo"
,
value
:
false
}],
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);"
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() {
...
@@ -50,6 +65,17 @@ describe('QueryGenerator', function() {
},
{
},
{
arguments
:
[
'myTable'
,
{
flag
:
false
},
{
id
:
2
}],
arguments
:
[
'myTable'
,
{
flag
:
false
},
{
id
:
2
}],
expectation
:
"UPDATE `myTable` SET `flag`=0 WHERE `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() {
...
@@ -59,7 +85,10 @@ describe('QueryGenerator', function() {
tests
.
forEach
(
function
(
test
)
{
tests
.
forEach
(
function
(
test
)
{
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
var
title
=
test
.
title
||
'correctly returns '
+
test
.
expectation
+
' for '
+
util
.
inspect
(
test
.
arguments
)
it
(
title
,
function
()
{
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
)
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