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 23f0460a
authored
May 09, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix use of sequelize.fn etc in date and boolean fields. Closes #1471
1 parent
bb40f280
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
51 additions
and
15 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/query-generator.js
lib/instance.js
lib/model.js
lib/utils.js
test/dao/values.test.js
lib/dialects/abstract/query-generator.js
View file @
23f0460
...
...
@@ -437,7 +437,7 @@ module.exports = (function() {
,
len
=
obj
.
length
for
(
var
i
=
0
;
i
<
len
-
1
;
i
++
)
{
var
item
=
obj
[
i
]
if
(
item
.
_modelAttribute
||
Utils
.
_
.
isString
(
item
)
||
item
instanceof
Utils
.
fn
||
item
instanceof
Utils
.
col
||
item
instanceof
Utils
.
literal
||
item
instanceof
Utils
.
cast
||
'raw'
in
item
)
{
if
(
item
.
_modelAttribute
||
Utils
.
_
.
isString
(
item
)
||
item
.
_isSequelizeMethod
||
'raw'
in
item
)
{
break
}
...
...
@@ -476,7 +476,7 @@ module.exports = (function() {
return
sql
}
else
if
(
obj
.
_modelAttribute
)
{
return
this
.
quoteTable
(
obj
.
Model
.
name
)
+
'.'
+
obj
.
fieldName
}
else
if
(
obj
instanceof
Utils
.
fn
||
obj
instanceof
Utils
.
col
||
obj
instanceof
Utils
.
literal
||
obj
instanceof
Utils
.
cast
)
{
}
else
if
(
obj
.
_isSequelizeMethod
)
{
return
obj
.
toString
(
this
)
}
else
if
(
Utils
.
_
.
isObject
(
obj
)
&&
'raw'
in
obj
)
{
return
obj
.
raw
...
...
@@ -551,7 +551,7 @@ module.exports = (function() {
Escape a value (e.g. a string, number or date)
*/
escape
:
function
(
value
,
field
)
{
if
(
value
instanceof
Utils
.
fn
||
value
instanceof
Utils
.
col
||
value
instanceof
Utils
.
literal
||
value
instanceof
Utils
.
cast
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
value
.
toString
(
this
)
}
else
{
return
SqlString
.
escape
(
value
,
false
,
null
,
this
.
dialect
,
field
)
...
...
lib/dialects/postgres/query-generator.js
View file @
23f0460
...
...
@@ -807,7 +807,7 @@ module.exports = (function() {
Escape a value (e.g. a string, number or date)
*/
escape
:
function
(
value
,
field
)
{
if
(
value
instanceof
Utils
.
fn
||
value
instanceof
Utils
.
col
||
value
instanceof
Utils
.
literal
||
value
instanceof
Utils
.
cast
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
value
.
toString
(
this
)
}
else
{
if
(
field
&&
field
.
type
&&
field
.
type
.
toString
()
===
DataTypes
.
HSTORE
.
type
&&
Utils
.
_
.
isObject
(
value
))
{
...
...
lib/instance.js
View file @
23f0460
...
...
@@ -295,13 +295,13 @@ module.exports = (function() {
}
// Convert date fields to real date objects
if
(
this
.
Model
.
_hasDateAttributes
&&
this
.
Model
.
_isDateAttribute
(
key
)
&&
value
!==
null
&&
!
(
value
instanceof
Date
))
{
if
(
this
.
Model
.
_hasDateAttributes
&&
this
.
Model
.
_isDateAttribute
(
key
)
&&
value
!==
null
&&
!
(
value
instanceof
Date
)
&&
!
value
.
_isSequelizeMethod
)
{
value
=
new
Date
(
value
)
}
}
// Convert boolean-ish values to booleans
if
(
this
.
Model
.
_hasBooleanAttributes
&&
this
.
Model
.
_isBooleanAttribute
(
key
)
&&
value
!==
null
&&
value
!==
undefined
)
{
if
(
this
.
Model
.
_hasBooleanAttributes
&&
this
.
Model
.
_isBooleanAttribute
(
key
)
&&
value
!==
null
&&
value
!==
undefined
&&
!
value
.
_isSequelizeMethod
)
{
value
=
!!
value
}
...
...
lib/model.js
View file @
23f0460
...
...
@@ -1921,14 +1921,12 @@ module.exports = (function() {
var
optClone
=
function
(
options
)
{
return
Utils
.
_
.
cloneDeep
(
options
,
function
(
elem
)
{
// The InstanceFactories used for include are pass by ref, so don't clone them.
if
(
elem
instanceof
Model
||
elem
instanceof
Utils
.
col
||
elem
instanceof
Utils
.
literal
||
elem
instanceof
Utils
.
cast
||
elem
instanceof
Utils
.
fn
||
elem
instanceof
Utils
.
and
||
elem
instanceof
Utils
.
or
||
elem
instanceof
Transaction
if
(
elem
&&
(
elem
.
_isSequelizeMethod
||
elem
instanceof
Model
||
elem
instanceof
Transaction
)
)
{
return
elem
}
...
...
lib/utils.js
View file @
23f0460
...
...
@@ -557,6 +557,8 @@ var Utils = module.exports = {
Utils
.
condition
=
Utils
.
where
Utils
.
and
.
prototype
.
_isSequelizeMethod
=
Utils
.
or
.
prototype
.
_isSequelizeMethod
=
Utils
.
where
.
prototype
.
_isSequelizeMethod
=
Utils
.
literal
.
prototype
.
_isSequelizeMethod
=
Utils
.
cast
.
prototype
.
_isSequelizeMethod
=
...
...
@@ -576,7 +578,7 @@ Utils.cast.prototype.toString = function(queryGenerator) {
this
.
val
=
queryGenerator
.
escape
(
this
.
val
)
}
else
{
this
.
val
=
this
.
val
.
toString
(
queryGenerator
)
}
}
return
'CAST('
+
this
.
val
+
' AS '
+
this
.
type
.
toUpperCase
()
+
')'
}
...
...
test/dao/values.test.js
View file @
23f0460
...
...
@@ -3,6 +3,7 @@ var chai = require('chai')
,
Sequelize
=
require
(
'../../index'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
dialect
=
Support
.
getTestDialect
()
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
datetime
=
require
(
'chai-datetime'
)
...
...
@@ -103,6 +104,41 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
expect
(
user
.
dataValues
.
email
).
not
.
to
.
be
.
ok
})
it
(
'allows use of sequelize.fn and sequelize.col in date and bool fields'
,
function
()
{
var
self
=
this
,
User
=
this
.
sequelize
.
define
(
'User'
,
{
d
:
DataTypes
.
DATE
,
b
:
DataTypes
.
BOOLEAN
,
always_false
:
{
type
:
DataTypes
.
BOOLEAN
,
defaultValue
:
false
}
},
{
timestamps
:
false
})
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({}).
then
(
function
(
user
)
{
// Create the user first to set the proper default values. PG does not support column references in insert,
// so we must create a record with the right value for always_false, then reference it in an update
var
now
=
dialect
===
'sqlite'
?
self
.
sequelize
.
fn
(
''
,
self
.
sequelize
.
fn
(
'date'
,
'now'
))
:
self
.
sequelize
.
fn
(
'NOW'
)
user
.
set
({
d
:
now
,
b
:
self
.
sequelize
.
col
(
'always_false'
)
})
expect
(
user
.
get
(
'd'
)).
to
.
be
.
instanceof
(
self
.
sequelize
.
Utils
.
fn
)
expect
(
user
.
get
(
'b'
)).
to
.
be
.
instanceof
(
self
.
sequelize
.
Utils
.
col
)
return
user
.
save
().
then
(
function
()
{
return
user
.
reload
().
then
(
function
()
{
expect
(
user
.
d
).
to
.
equalDate
(
new
Date
())
expect
(
user
.
b
).
to
.
equal
(
false
)
})
})
})
})
})
describe
(
'includes'
,
function
()
{
it
(
'should support basic includes'
,
function
()
{
var
Product
=
this
.
sequelize
.
define
(
'Product'
,
{
...
...
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