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 e40f9b6e
authored
Jul 08, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up the sqlstring escapedate function
1 parent
6baf8c77
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
17 additions
and
47 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/sqlite/query.js
lib/sql-string.js
test/dao/values.test.js
test/postgres/query-generator.test.js
test/support.js
test/timezone.test.js
lib/dialects/abstract/query-generator.js
View file @
e40f9b6
...
@@ -555,7 +555,7 @@ module.exports = (function() {
...
@@ -555,7 +555,7 @@ module.exports = (function() {
if
(
value
&&
value
.
_isSequelizeMethod
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
value
.
toString
(
this
);
return
value
.
toString
(
this
);
}
else
{
}
else
{
return
SqlString
.
escape
(
value
,
false
,
null
,
this
.
dialect
,
field
);
return
SqlString
.
escape
(
value
,
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
}
}
},
},
...
...
lib/dialects/sqlite/query.js
View file @
e40f9b6
...
@@ -77,7 +77,7 @@ module.exports = (function() {
...
@@ -77,7 +77,7 @@ module.exports = (function() {
if
(
val
!==
null
)
{
if
(
val
!==
null
)
{
if
(
val
.
indexOf
(
'+'
)
===
-
1
)
{
if
(
val
.
indexOf
(
'+'
)
===
-
1
)
{
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
result
[
name
]
=
new
Date
(
val
+
'Z'
);
// Z means UTC
result
[
name
]
=
new
Date
(
val
+
self
.
sequelize
.
options
.
timezone
);
}
else
{
}
else
{
result
[
name
]
=
new
Date
(
val
);
// We already have a timezone stored in the string
result
[
name
]
=
new
Date
(
val
);
// We already have a timezone stored in the string
}
}
...
...
lib/sql-string.js
View file @
e40f9b6
...
@@ -159,36 +159,18 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
...
@@ -159,36 +159,18 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
};
};
SqlString
.
dateToString
=
function
(
date
,
timeZone
,
dialect
)
{
SqlString
.
dateToString
=
function
(
date
,
timeZone
,
dialect
)
{
var
dt
=
new
Date
(
dat
e
);
date
=
moment
(
date
).
zone
(
timeZon
e
);
// TODO: Ideally all dialects would work a bit more like this
if
(
dialect
===
'mysql'
||
dialect
===
'mariadb'
)
{
if
(
dialect
===
'postgres'
||
dialect
===
'sqlite'
)
{
return
date
.
format
(
'YYYY-MM-DD HH:mm:ss'
);
return
moment
(
dt
).
format
(
'YYYY-MM-DD HH:mm:ss.SSS'
+
timeZone
);
}
else
{
}
// ZZ here means current timezone, _not_ UTC
return
date
.
format
(
'YYYY-MM-DD HH:mm:ss.SSS Z'
);
if
(
timeZone
!==
'local'
)
{
var
tz
=
convertTimezone
(
timeZone
);
dt
.
setTime
(
dt
.
getTime
()
+
(
dt
.
getTimezoneOffset
()
*
60000
));
if
(
tz
!==
false
)
{
dt
.
setTime
(
dt
.
getTime
()
+
(
tz
*
60000
));
}
}
}
return
moment
(
dt
).
format
(
'YYYY-MM-DD HH:mm:ss'
);
};
};
SqlString
.
bufferToString
=
function
(
buffer
,
dialect
)
{
SqlString
.
bufferToString
=
function
(
buffer
,
dialect
)
{
var
hex
=
''
;
var
hex
=
buffer
.
toString
(
'hex'
);
try
{
hex
=
buffer
.
toString
(
'hex'
);
}
catch
(
err
)
{
// node v0.4.x does not support hex / throws unknown encoding error
for
(
var
i
=
0
;
i
<
buffer
.
length
;
i
++
)
{
var
byte
=
buffer
[
i
];
hex
+=
zeroPad
(
byte
.
toString
(
16
));
}
}
if
(
dialect
===
'postgres'
)
{
if
(
dialect
===
'postgres'
)
{
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
...
@@ -214,15 +196,3 @@ SqlString.objectToValues = function(object, timeZone) {
...
@@ -214,15 +196,3 @@ SqlString.objectToValues = function(object, timeZone) {
function
zeroPad
(
number
)
{
function
zeroPad
(
number
)
{
return
(
number
<
10
)
?
'0'
+
number
:
number
;
return
(
number
<
10
)
?
'0'
+
number
:
number
;
}
}
function
convertTimezone
(
tz
)
{
if
(
tz
===
'Z'
)
{
return
0
;
}
var
m
=
tz
.
match
(
/
([\+\-\s])(\d\d)
:
?(\d\d)?
/
);
if
(
m
)
{
return
(
m
[
1
]
===
'-'
?
-
1
:
1
)
*
(
parseInt
(
m
[
2
],
10
)
+
((
m
[
3
]
?
parseInt
(
m
[
3
],
10
)
:
0
)
/
60
))
*
60
;
}
return
false
;
}
test/dao/values.test.js
View file @
e40f9b6
...
@@ -119,7 +119,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -119,7 +119,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
return
User
.
create
({}).
then
(
function
(
user
)
{
return
User
.
create
({}).
then
(
function
(
user
)
{
// Create the user first to set the proper default values. PG does not support column references in insert,
// 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
// 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'
)
var
now
=
dialect
===
'sqlite'
?
self
.
sequelize
.
fn
(
''
,
self
.
sequelize
.
fn
(
'date
time
'
,
'now'
))
:
self
.
sequelize
.
fn
(
'NOW'
)
user
.
set
({
user
.
set
({
d
:
now
,
d
:
now
,
...
...
test/postgres/query-generator.test.js
View file @
e40f9b6
test/support.js
View file @
e40f9b6
...
@@ -63,7 +63,7 @@ var Support = {
...
@@ -63,7 +63,7 @@ var Support = {
var
sequelizeOptions
=
_
.
defaults
(
options
,
{
var
sequelizeOptions
=
_
.
defaults
(
options
,
{
host
:
options
.
host
||
config
.
host
,
host
:
options
.
host
||
config
.
host
,
//
logging: false,
logging
:
false
,
dialect
:
options
.
dialect
,
dialect
:
options
.
dialect
,
port
:
options
.
port
||
process
.
env
.
SEQ_PORT
||
config
.
port
,
port
:
options
.
port
||
process
.
env
.
SEQ_PORT
||
config
.
port
,
pool
:
config
.
pool
,
pool
:
config
.
pool
,
...
...
test/timezone.test.js
View file @
e40f9b6
...
@@ -12,7 +12,7 @@ var chai = require('chai')
...
@@ -12,7 +12,7 @@ var chai = require('chai')
if
(
dialect
!==
'sqlite'
)
{
if
(
dialect
!==
'sqlite'
)
{
// Sqlite does not support setting timezone
// Sqlite does not support setting timezone
describe
.
only
(
Support
.
getTestDialectTeaser
(
'Timezone'
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
'Timezone'
),
function
()
{
beforeEach
(
function
()
{
beforeEach
(
function
()
{
this
.
sequelizeWithTimezone
=
Support
.
createSequelizeInstance
({
this
.
sequelizeWithTimezone
=
Support
.
createSequelizeInstance
({
timezone
:
'+07:00'
,
timezone
:
'+07:00'
,
...
@@ -36,15 +36,15 @@ if (dialect !== 'sqlite') {
...
@@ -36,15 +36,15 @@ if (dialect !== 'sqlite') {
,
TimezonedUser
=
this
.
sequelizeWithTimezone
.
define
(
'user'
,
{});
,
TimezonedUser
=
this
.
sequelizeWithTimezone
.
define
(
'user'
,
{});
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
NormalUser
.
create
({});
return
TimezonedUser
.
create
({});
}).
then
(
function
(
normalUser
)
{
this
.
normalUser
=
normalUser
;
return
TimezonedUser
.
find
(
normalUser
.
id
);
}).
then
(
function
(
timezonedUser
)
{
}).
then
(
function
(
timezonedUser
)
{
this
.
timezonedUser
=
timezonedUser
;
return
NormalUser
.
find
(
timezonedUser
.
id
);
}).
then
(
function
(
normalUser
)
{
// Expect 7 hours difference, in milliseconds.
// Expect 7 hours difference, in milliseconds.
// This difference is expected since two instances, configured for each their timezone is trying to read the same timestamp
// This difference is expected since two instances, configured for each their timezone is trying to read the same timestamp
// this test does not apply to PG, since it stores the timezone along with the timestamp.
// this test does not apply to PG, since it stores the timezone along with the timestamp.
expect
(
this
.
normalUser
.
createdAt
.
getTime
()
-
timezonedUser
.
createdAt
.
getTime
()
).
to
.
be
.
closeTo
(
60
*
60
*
7
*
1000
,
50
);
expect
(
normalUser
.
createdAt
.
getTime
()
-
this
.
timezonedUser
.
createdAt
.
getTime
()
).
to
.
be
.
closeTo
(
60
*
60
*
7
*
1000
,
50
);
});
});
});
});
}
}
...
...
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