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

Commit e40f9b6e by Jan Aagaard Meier

Clean up the sqlstring escapedate function

1 parent 6baf8c77
......@@ -555,7 +555,7 @@ module.exports = (function() {
if (value && value._isSequelizeMethod) {
return value.toString(this);
} else {
return SqlString.escape(value, false, null, this.dialect, field);
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
}
},
......
......@@ -77,7 +77,7 @@ module.exports = (function() {
if (val !== null) {
if (val.indexOf('+') === -1) {
// 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 {
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) {
};
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 === 'postgres' || dialect === 'sqlite') {
return moment(dt).format('YYYY-MM-DD HH:mm:ss.SSS' + timeZone);
}
if (timeZone !== 'local') {
var tz = convertTimezone(timeZone);
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
if (tz !== false) {
dt.setTime(dt.getTime() + (tz * 60000));
}
if (dialect === 'mysql' || dialect === 'mariadb') {
return date.format('YYYY-MM-DD HH:mm:ss');
} else {
// ZZ here means current timezone, _not_ UTC
return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
}
return moment(dt).format('YYYY-MM-DD HH:mm:ss');
};
SqlString.bufferToString = function(buffer, dialect) {
var 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));
}
}
var hex = buffer.toString('hex');
if (dialect === 'postgres') {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
......@@ -214,15 +196,3 @@ SqlString.objectToValues = function(object, timeZone) {
function zeroPad(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 () {
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')
var now = dialect === 'sqlite' ? self.sequelize.fn('', self.sequelize.fn('datetime', 'now')) : self.sequelize.fn('NOW')
user.set({
d: now,
......
......@@ -63,7 +63,7 @@ var Support = {
var sequelizeOptions = _.defaults(options, {
host: options.host || config.host,
// logging: false,
logging: false,
dialect: options.dialect,
port: options.port || process.env.SEQ_PORT || config.port,
pool: config.pool,
......
......@@ -12,7 +12,7 @@ var chai = require('chai')
if (dialect !== 'sqlite') {
// Sqlite does not support setting timezone
describe.only(Support.getTestDialectTeaser('Timezone'), function () {
describe(Support.getTestDialectTeaser('Timezone'), function () {
beforeEach(function () {
this.sequelizeWithTimezone = Support.createSequelizeInstance({
timezone: '+07:00',
......@@ -36,15 +36,15 @@ if (dialect !== 'sqlite') {
, TimezonedUser = this.sequelizeWithTimezone.define('user', {});
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return NormalUser.create({});
}).then(function (normalUser) {
this.normalUser = normalUser;
return TimezonedUser.find(normalUser.id);
return TimezonedUser.create({});
}).then(function (timezonedUser) {
this.timezonedUser = timezonedUser;
return NormalUser.find(timezonedUser.id);
}).then(function (normalUser) {
// 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 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!