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 7f99d9de
authored
Sep 23, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #928 from janmeier/updateWithColumn
Update with column
2 parents
2ba78f18
1e208eeb
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
175 additions
and
17 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
test/dao-factory.test.js
test/dao.test.js
test/mysql/query-generator.test.js
test/postgres/query-generator.test.js
test/sqlite/query-generator.test.js
lib/dialects/abstract/query-generator.js
View file @
7f99d9d
var
Utils
=
require
(
"../../utils"
)
var
Utils
=
require
(
"../../utils"
)
,
SqlString
=
require
(
"../../sql-string"
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
...
@@ -318,8 +319,12 @@ module.exports = (function() {
...
@@ -318,8 +319,12 @@ module.exports = (function() {
/*
/*
Escape a value (e.g. a string, number or date)
Escape a value (e.g. a string, number or date)
*/
*/
escape
:
function
(
value
)
{
escape
:
function
(
value
,
field
)
{
throwMethodUndefined
(
'escape'
)
if
(
value
instanceof
Utils
.
fn
||
value
instanceof
Utils
.
col
)
{
return
value
.
toString
(
this
)
}
else
{
return
SqlString
.
escape
(
value
,
false
,
null
,
this
.
dialect
,
field
)
}
}
}
}
}
...
...
lib/dialects/mysql/query-generator.js
View file @
7f99d9d
...
@@ -605,12 +605,7 @@ module.exports = (function() {
...
@@ -605,12 +605,7 @@ module.exports = (function() {
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
escape
:
function
(
value
)
{
return
SqlString
.
escape
(
value
,
false
,
null
,
"mysql"
)
}
}
}
}
return
Utils
.
_
.
extend
(
Utils
.
_
.
clone
(
require
(
"../abstract/query-generator"
)),
QueryGenerator
)
return
Utils
.
_
.
extend
(
Utils
.
_
.
clone
(
require
(
"../abstract/query-generator"
)),
QueryGenerator
)
...
...
lib/dialects/postgres/query-generator.js
View file @
7f99d9d
...
@@ -858,12 +858,7 @@ module.exports = (function() {
...
@@ -858,12 +858,7 @@ module.exports = (function() {
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
return
identifiers
.
split
(
'.'
).
map
(
function
(
t
)
{
return
this
.
quoteIdentifier
(
t
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
return
identifiers
.
split
(
'.'
).
map
(
function
(
t
)
{
return
this
.
quoteIdentifier
(
t
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
escape
:
function
(
value
,
field
)
{
return
SqlString
.
escape
(
value
,
false
,
null
,
"postgres"
,
field
)
}
}
}
}
// Private
// Private
...
...
lib/dialects/sqlite/query-generator.js
View file @
7f99d9d
...
@@ -486,12 +486,7 @@ module.exports = (function() {
...
@@ -486,12 +486,7 @@ module.exports = (function() {
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
quoteIdentifiers
:
function
(
identifiers
,
force
)
{
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
escape
:
function
(
value
)
{
return
SqlString
.
escape
(
value
,
false
,
null
,
"sqlite"
)
}
}
}
}
return
Utils
.
_
.
extend
({},
MySqlQueryGenerator
,
QueryGenerator
)
return
Utils
.
_
.
extend
({},
MySqlQueryGenerator
,
QueryGenerator
)
...
...
test/dao-factory.test.js
View file @
7f99d9d
...
@@ -333,6 +333,68 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -333,6 +333,68 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
describe
(
'create'
,
function
()
{
describe
(
'create'
,
function
()
{
it
(
'is possible to use funtions when creating an instance'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
secretValue
:
this
.
sequelize
.
fn
(
'upper'
,
'sequelize'
)
}).
success
(
function
(
user
)
{
self
.
User
.
find
(
user
.
id
).
success
(
function
(
user
)
{
expect
(
user
.
secretValue
).
to
.
equal
(
'SEQUELIZE'
)
done
()
})
})
})
it
(
'is possible to use functions as default values'
,
function
(
done
)
{
var
self
=
this
,
userWithDefaults
if
(
dialect
.
indexOf
(
'postgres'
)
===
0
)
{
this
.
sequelize
.
query
(
'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
).
success
(
function
()
{
userWithDefaults
=
self
.
sequelize
.
define
(
'userWithDefaults'
,
{
uuid
:
{
type
:
'UUID'
,
defaultValue
:
self
.
sequelize
.
fn
(
'uuid_generate_v4'
)
}
})
userWithDefaults
.
sync
({
force
:
true
}).
success
(
function
()
{
userWithDefaults
.
create
({}).
success
(
function
(
user
)
{
// uuid validation regex taken from http://stackoverflow.com/a/13653180/800016
expect
(
user
.
uuid
).
to
.
match
(
/^
[
0-9a-f
]{8}
-
[
0-9a-f
]{4}
-
[
1-5
][
0-9a-f
]{3}
-
[
89ab
][
0-9a-f
]{3}
-
[
0-9a-f
]{12}
$/i
)
done
()
})
})
})
}
else
if
(
dialect
===
'sqlite'
)
{
// The definition here is a bit hacky. sqlite expects () around the expression for default values, so we call a function without a name
// to enclose the date function in (). http://www.sqlite.org/syntaxdiagrams.html#column-constraint
userWithDefaults
=
self
.
sequelize
.
define
(
'userWithDefaults'
,
{
year
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
self
.
sequelize
.
fn
(
''
,
self
.
sequelize
.
fn
(
'date'
,
'now'
))
}
})
userWithDefaults
.
sync
({
force
:
true
}).
success
(
function
()
{
userWithDefaults
.
create
({}).
success
(
function
(
user
)
{
userWithDefaults
.
find
(
user
.
id
).
success
(
function
(
user
)
{
var
now
=
new
Date
()
,
pad
=
function
(
number
)
{
if
(
number
>
9
)
{
return
number
}
return
'0'
+
number
}
expect
(
user
.
year
).
to
.
equal
(
now
.
getFullYear
()
+
'-'
+
pad
(
now
.
getMonth
()
+
1
)
+
'-'
+
pad
(
now
.
getDate
()))
done
()
})
})
})
}
else
{
// functions as default values are not supported in mysql, see http://stackoverflow.com/a/270338/800016
done
()
}
})
it
(
"casts empty arrays correctly for postgresql insert"
,
function
(
done
)
{
it
(
"casts empty arrays correctly for postgresql insert"
,
function
(
done
)
{
if
(
dialect
!==
"postgres"
&&
dialect
!==
"postgresql-native"
)
{
if
(
dialect
!==
"postgres"
&&
dialect
!==
"postgresql-native"
)
{
expect
(
''
).
to
.
equal
(
''
)
expect
(
''
).
to
.
equal
(
''
)
...
@@ -955,6 +1017,21 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -955,6 +1017,21 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
(
'updates with function and column value'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'John'
}).
success
(
function
(
user
)
{
self
.
User
.
update
({
username
:
self
.
sequelize
.
fn
(
'upper'
,
self
.
sequelize
.
col
(
'username'
))},
{
username
:
'John'
}).
success
(
function
()
{
self
.
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'JOHN'
)
done
()
})
})
})
})
it
(
'sets updatedAt to the current timestamp'
,
function
(
done
)
{
it
(
'sets updatedAt to the current timestamp'
,
function
(
done
)
{
var
self
=
this
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
...
...
test/dao.test.js
View file @
7f99d9d
...
@@ -641,6 +641,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -641,6 +641,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
},
1000
)
},
1000
)
})
})
it
(
'updates with function and column value'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
aNumber
:
42
}).
success
(
function
(
user
)
{
user
.
bNumber
=
self
.
sequelize
.
col
(
'aNumber'
)
user
.
username
=
self
.
sequelize
.
fn
(
'upper'
,
'sequelize'
)
user
.
save
().
success
(
function
(){
self
.
User
.
find
(
user
.
id
).
success
(
function
(
user2
)
{
expect
(
user2
.
username
).
to
.
equal
(
'SEQUELIZE'
)
expect
(
user2
.
bNumber
).
to
.
equal
(
42
)
done
()
})
})
})
})
describe
(
'without timestamps option'
,
function
()
{
describe
(
'without timestamps option'
,
function
()
{
it
(
"doesn't update the updatedAt column"
,
function
(
done
)
{
it
(
"doesn't update the updatedAt column"
,
function
(
done
)
{
var
User2
=
this
.
sequelize
.
define
(
'User2'
,
{
var
User2
=
this
.
sequelize
.
define
(
'User2'
,
{
...
...
test/mysql/query-generator.test.js
View file @
7f99d9d
...
@@ -308,6 +308,14 @@ if (dialect.match(/^mysql/)) {
...
@@ -308,6 +308,14 @@ if (dialect.match(/^mysql/)) {
},
{
},
{
arguments
:
[
'myTable'
,
{
foo
:
true
}],
arguments
:
[
'myTable'
,
{
foo
:
true
}],
expectation
:
"INSERT INTO `myTable` (`foo`) VALUES (true);"
expectation
:
"INSERT INTO `myTable` (`foo`) VALUES (true);"
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
foo
:
sequelize
.
fn
(
'NOW'
)
}
}],
expectation
:
"INSERT INTO `myTable` (`foo`) VALUES (NOW());"
,
needsSequelize
:
true
}
}
],
],
...
@@ -375,6 +383,22 @@ if (dialect.match(/^mysql/)) {
...
@@ -375,6 +383,22 @@ if (dialect.match(/^mysql/)) {
},
{
},
{
arguments
:
[
'myTable'
,
{
bar
:
true
},
{
name
:
'foo'
}],
arguments
:
[
'myTable'
,
{
bar
:
true
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=true WHERE `name`='foo'"
expectation
:
"UPDATE `myTable` SET `bar`=true WHERE `name`='foo'"
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
fn
(
'NOW'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=NOW() WHERE `name`='foo'"
,
needsSequelize
:
true
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
col
(
'foo'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=`foo` WHERE `name`='foo'"
,
needsSequelize
:
true
}
}
],
],
...
...
test/postgres/query-generator.test.js
View file @
7f99d9d
...
@@ -451,6 +451,14 @@ if (dialect.match(/^postgres/)) {
...
@@ -451,6 +451,14 @@ if (dialect.match(/^postgres/)) {
},
{
},
{
arguments
:
[
'mySchema.myTable'
,
{
name
:
"foo';DROP TABLE mySchema.myTable;"
}],
arguments
:
[
'mySchema.myTable'
,
{
name
:
"foo';DROP TABLE mySchema.myTable;"
}],
expectation
:
"INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo'';DROP TABLE mySchema.myTable;') RETURNING *;"
expectation
:
"INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo'';DROP TABLE mySchema.myTable;') RETURNING *;"
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
foo
:
sequelize
.
fn
(
'NOW'
)
}
}],
expectation
:
"INSERT INTO \"myTable\" (\"foo\") VALUES (NOW()) RETURNING *;"
,
needsSequelize
:
true
},
},
// Variants when quoteIdentifiers is false
// Variants when quoteIdentifiers is false
...
@@ -630,6 +638,22 @@ if (dialect.match(/^postgres/)) {
...
@@ -630,6 +638,22 @@ if (dialect.match(/^postgres/)) {
},
{
},
{
arguments
:
[
'mySchema.myTable'
,
{
name
:
"foo';DROP TABLE mySchema.myTable;"
},
{
name
:
'foo'
}],
arguments
:
[
'mySchema.myTable'
,
{
name
:
"foo';DROP TABLE mySchema.myTable;"
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo'';DROP TABLE mySchema.myTable;' WHERE \"name\"='foo' RETURNING *"
expectation
:
"UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo'';DROP TABLE mySchema.myTable;' WHERE \"name\"='foo' RETURNING *"
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
fn
(
'NOW'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=NOW() WHERE \"name\"='foo' RETURNING *"
,
needsSequelize
:
true
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
col
(
'foo'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE \"myTable\" SET \"bar\"=\"foo\" WHERE \"name\"='foo' RETURNING *"
,
needsSequelize
:
true
},
},
// Variants when quoteIdentifiers is false
// Variants when quoteIdentifiers is false
...
...
test/sqlite/query-generator.test.js
View file @
7f99d9d
...
@@ -294,6 +294,14 @@ if (dialect === 'sqlite') {
...
@@ -294,6 +294,14 @@ if (dialect === 'sqlite') {
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
undefined
}],
arguments
:
[
'myTable'
,
{
name
:
'foo'
,
foo
:
1
,
nullValue
:
undefined
}],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
expectation
:
"INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
,
context
:
{
options
:
{
omitNull
:
true
}}
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
foo
:
sequelize
.
fn
(
'NOW'
)
}
}],
expectation
:
"INSERT INTO `myTable` (`foo`) VALUES (NOW());"
,
needsSequelize
:
true
}
}
],
],
...
@@ -373,6 +381,22 @@ if (dialect === 'sqlite') {
...
@@ -373,6 +381,22 @@ if (dialect === 'sqlite') {
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
:
{
omitNull
:
true
}}
context
:
{
options
:
{
omitNull
:
true
}}
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
fn
(
'NOW'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=NOW() WHERE `name`='foo'"
,
needsSequelize
:
true
},
{
arguments
:
[
'myTable'
,
function
(
sequelize
)
{
return
{
bar
:
sequelize
.
col
(
'foo'
)
}
},
{
name
:
'foo'
}],
expectation
:
"UPDATE `myTable` SET `bar`=`foo` WHERE `name`='foo'"
,
needsSequelize
:
true
}
}
],
],
...
...
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