不要怂,就是干,撸起袖子干!

Commit e40f9b6e by Jan Aagaard Meier

Clean up the sqlstring escapedate function

1 parent 6baf8c77
...@@ -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);
} }
}, },
......
...@@ -75,9 +75,9 @@ module.exports = (function() { ...@@ -75,9 +75,9 @@ module.exports = (function() {
var val = result[name]; var val = result[name];
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
} }
......
...@@ -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(date); date = moment(date).zone(timeZone);
// 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;
}
...@@ -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('datetime', 'now')) : self.sequelize.fn('NOW')
user.set({ user.set({
d: now, d: now,
...@@ -292,7 +292,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -292,7 +292,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
var Contact = this.sequelize.define('Contact', { var Contact = this.sequelize.define('Contact', {
first: { type: Sequelize.STRING }, first: { type: Sequelize.STRING },
last: { type: Sequelize.STRING }, last: { type: Sequelize.STRING },
tags: { tags: {
type: Sequelize.STRING, type: Sequelize.STRING,
get: function(field) { get: function(field) {
var val = this.getDataValue(field); var val = this.getDataValue(field);
...@@ -402,10 +402,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -402,10 +402,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
name: 'Jan Meier' name: 'Jan Meier'
}) })
user.set('name', 'Mick Hansen') user.set('name', 'Mick Hansen')
expect(user.previous('name')).to.equal('Jan Meier') expect(user.previous('name')).to.equal('Jan Meier')
expect(user.get('name')).to.equal('Mick Hansen') expect(user.get('name')).to.equal('Mick Hansen')
}) })
}) })
}) })
}) })
\ No newline at end of file
...@@ -978,4 +978,4 @@ if (dialect.match(/^postgres/)) { ...@@ -978,4 +978,4 @@ if (dialect.match(/^postgres/)) {
}) })
}) })
}) })
} }
\ No newline at end of file
...@@ -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,
......
...@@ -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);
}); });
}); });
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!