Skip to content

Commit

Permalink
Merge pull request #23 from dirkmc/master
Browse files Browse the repository at this point in the history
Use local as default timezone, pass timezone to escapeVal()
  • Loading branch information
dresende committed Sep 6, 2013
2 parents ab2b7d2 + d03cf05 commit 718090a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/Dialects/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ exports.escapeVal = function (val, timeZone) {
}

if (Array.isArray(val)) {
return arrayToList(val, timeZone || "Z");
return arrayToList(val, timeZone || "local");
}

if (util.isDate(val)) {
val = dateToString(val, timeZone || "Z");
val = dateToString(val, timeZone || "local");
} else {
switch (typeof val) {
case 'boolean':
Expand Down
4 changes: 2 additions & 2 deletions lib/Insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Set = require("./Set");

exports.InsertQuery = InsertQuery;

function InsertQuery(Dialect) {
function InsertQuery(Dialect, opts) {
var sql = {};

return {
Expand All @@ -23,7 +23,7 @@ function InsertQuery(Dialect) {
if (sql.hasOwnProperty("set")) {
for (var k in sql.set) {
cols.push(Dialect.escapeId(k));
vals.push(Dialect.escapeVal(sql.set[k]));
vals.push(Dialect.escapeVal(sql.set[k], opts.timezone));
}
query.push("(" + cols.join(", ") + ")");
query.push("VALUES (" + vals.join(", ") + ")");
Expand Down
8 changes: 4 additions & 4 deletions lib/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ function Query(opts) {
escapeId : Dialect.escapeId.bind(Dialect),
escapeVal : Dialect.escapeVal.bind(Dialect),
select: function () {
return new SelectQuery(Dialect);
return new SelectQuery(Dialect, opts);
},
insert: function () {
return new InsertQuery(Dialect);
return new InsertQuery(Dialect, opts);
},
update: function () {
return new UpdateQuery(Dialect);
return new UpdateQuery(Dialect, opts);
},
remove: function () {
return new RemoveQuery(Dialect);
return new RemoveQuery(Dialect, opts);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Where = require("./Where");

exports.RemoveQuery = RemoveQuery;

function RemoveQuery(Dialect) {
function RemoveQuery(Dialect, opts) {
var sql = {
where : [],
order : []
Expand All @@ -28,7 +28,7 @@ function RemoveQuery(Dialect) {
query.push("DELETE FROM");
query.push(Dialect.escapeId(sql.table));

query = query.concat(Where.build(Dialect, sql.where));
query = query.concat(Where.build(Dialect, sql.where, opts));

// order
if (sql.order.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var aggregate_functions = [

exports.SelectQuery = SelectQuery;

function SelectQuery(Dialect) {
function SelectQuery(Dialect, opts) {
var sql = {
from : [],
where : [],
Expand Down Expand Up @@ -253,7 +253,7 @@ function SelectQuery(Dialect) {
if (typeof el.type == "function") {
switch (el.type()) {
case "text":
return Dialect.escapeVal(el.data);
return Dialect.escapeVal(el.data, opts.timezone);
default:
return el;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ function SelectQuery(Dialect) {
}
}

query = query.concat(Where.build(Dialect, sql.where));
query = query.concat(Where.build(Dialect, sql.where, opts));

if (sql.group_by !== null) {
query.push("GROUP BY " + sql.group_by.map(function (column) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports.build = function (Dialect, set) {
exports.build = function (Dialect, set, opts) {
opts = opts || {};

if (!set || set.length === 0) {
return [];
}
Expand All @@ -9,7 +11,7 @@ exports.build = function (Dialect, set) {
query.push(
Dialect.escapeId(k) +
" = " +
Dialect.escapeVal(set[k])
Dialect.escapeVal(set[k], opts.timezone)
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Where = require("./Where");

exports.UpdateQuery = UpdateQuery;

function UpdateQuery(Dialect) {
function UpdateQuery(Dialect, opts) {
var sql = {
where : []
};
Expand Down Expand Up @@ -32,8 +32,8 @@ function UpdateQuery(Dialect) {
query.push("UPDATE");
query.push(Dialect.escapeId(sql.table));

query = query.concat(Set.build(Dialect, sql.set));
query = query.concat(Where.build(Dialect, sql.where));
query = query.concat(Set.build(Dialect, sql.set, opts));
query = query.concat(Where.build(Dialect, sql.where, opts));

return query.join(" ");
}
Expand Down
30 changes: 16 additions & 14 deletions lib/Where.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var Helpers = require('./Helpers');

exports.build = function (Dialect, where) {
exports.build = function (Dialect, where, opts) {
if (where.length === 0) {
return [];
}

var query = [], subquery;

for (var i = 0; i < where.length; i++) {
subquery = buildOrGroup(Dialect, where[i]);
subquery = buildOrGroup(Dialect, where[i], opts);

if (subquery !== false) {
query.push(subquery);
Expand All @@ -24,7 +24,9 @@ exports.build = function (Dialect, where) {
return "WHERE (" + query.join(") AND (") + ")";
};

function buildOrGroup(Dialect, where) {
function buildOrGroup(Dialect, where, opts) {
opts = opts || {};

if (where.e) {
// EXISTS

Expand All @@ -41,7 +43,7 @@ function buildOrGroup(Dialect, where) {
"EXISTS (" +
"SELECT * FROM " + Dialect.escapeId(where.e.t) + " " +
"WHERE " + wheres.join(" AND ") + " " +
"AND " + buildOrGroup(Dialect, { t: null, w: where.w }) +
"AND " + buildOrGroup(Dialect, { t: null, w: where.w }, opts) +
")"
];
}
Expand All @@ -64,7 +66,7 @@ function buildOrGroup(Dialect, where) {
op = (k == "not" ? "and" : (k.indexOf("_") >= 0 ? k.substr(4) : k)).toUpperCase();

for (var j = 0; j < where.w[k].length; j++) {
q = buildOrGroup(Dialect, { t: where.t, w: where.w[k][j] });
q = buildOrGroup(Dialect, { t: where.t, w: where.w[k][j] }, opts);
if (q !== false) {
subquery.push(q);
}
Expand All @@ -84,25 +86,25 @@ function buildOrGroup(Dialect, where) {
query.push(
buildComparisonKey(Dialect, where.t, k) +
" BETWEEN " +
Dialect.escapeVal(where.w[k].from) +
Dialect.escapeVal(where.w[k].from, opts.timezone) +
" AND " +
Dialect.escapeVal(where.w[k].to)
Dialect.escapeVal(where.w[k].to, opts.timezone)
);
break;
case "not_between":
query.push(
buildComparisonKey(Dialect, where.t, k) +
" NOT BETWEEN " +
Dialect.escapeVal(where.w[k].from) +
Dialect.escapeVal(where.w[k].from, opts.timezone) +
" AND " +
Dialect.escapeVal(where.w[k].to)
Dialect.escapeVal(where.w[k].to, opts.timezone)
);
break;
case "like":
query.push(
buildComparisonKey(Dialect, where.t, k) +
" LIKE " +
Dialect.escapeVal(where.w[k].expr)
Dialect.escapeVal(where.w[k].expr, opts.timezone)
);
break;
case "eq":
Expand All @@ -122,7 +124,7 @@ function buildOrGroup(Dialect, where) {
query.push(
buildComparisonKey(Dialect, where.t, k) +
" " + op + " " +
Dialect.escapeVal(where.w[k].val)
Dialect.escapeVal(where.w[k].val, opts.timezone)
);
break;
case "sql":
Expand All @@ -138,7 +140,7 @@ function buildOrGroup(Dialect, where) {
return Dialect.escapeId(where.w[k].where.escapes.shift());
}
// ?:value
return Dialect.escapeVal(where.w[k].where.escapes.shift());
return Dialect.escapeVal(where.w[k].where.escapes.shift(), opts.timezone);
});

query.push(sql);
Expand All @@ -158,10 +160,10 @@ function buildOrGroup(Dialect, where) {
// #274: IN with empty arrays should be a false sentence
query.push("FALSE");
} else {
query.push(buildComparisonKey(Dialect, where.t, k) + " IN " + Dialect.escapeVal(where.w[k]));
query.push(buildComparisonKey(Dialect, where.t, k) + " IN " + Dialect.escapeVal(where.w[k], opts.timezone));
}
} else {
query.push(buildComparisonKey(Dialect, where.t, k) + " = " + Dialect.escapeVal(where.w[k]));
query.push(buildComparisonKey(Dialect, where.t, k) + " = " + Dialect.escapeVal(where.w[k], opts.timezone));
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/integration/test-dialect-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ assert.equal(
dialect.escapeVal(false),
"false"
);


// Dates and Timezones
var d = new Date(1378322111133);
var tzOffsetMillis = (d.getTimezoneOffset() * 60 * 1000);

assert.equal(
dialect.escapeVal(new Date(d.getTime() + tzOffsetMillis)),
"'2013-09-04 19:15:11'"
);
assert.equal(
dialect.escapeVal(new Date(d.getTime()), 'Z'),
"'2013-09-04 19:15:11'"
);
assert.equal(
dialect.escapeVal(new Date(d.getTime()), '-0000'),
"'2013-09-04 19:15:11'"
);
assert.equal(
dialect.escapeVal(new Date(d.getTime()), '-0400'),
"'2013-09-04 15:15:11'"
);
assert.equal(
dialect.escapeVal(new Date(d.getTime())),
dialect.escapeVal(new Date(d.getTime()), 'local')
);

0 comments on commit 718090a

Please sign in to comment.