Comparing version 0.0.5 to 0.0.6
@@ -8,2 +8,3 @@ var SelectQuery = require("./Select").SelectQuery; | ||
exports.Query = Query; | ||
exports.Comparators = Object.keys(Comparators); | ||
@@ -24,2 +25,4 @@ for (var comparator in Comparators) { | ||
return { | ||
escapeId: Dialect.escapeId.bind(Dialect), | ||
escapeVal: Dialect.escapeVal.bind(Dialect), | ||
select: function () { | ||
@@ -26,0 +29,0 @@ return new SelectQuery(Dialect); |
@@ -8,3 +8,4 @@ var Where = require("./Where"); | ||
from : [], | ||
where : [] | ||
where : [], | ||
order : [] | ||
}; | ||
@@ -21,4 +22,6 @@ var get_table_alias = function (table) { | ||
return { | ||
select: function () { | ||
sql.from[sql.from.length - 1].select = Array.prototype.slice.apply(arguments); | ||
select: function (fields) { | ||
if (fields) { | ||
sql.from[sql.from.length - 1].select = (Array.isArray(fields) ? fields : Array.prototype.slice.apply(arguments)); | ||
} | ||
return this; | ||
@@ -83,2 +86,17 @@ }, | ||
}, | ||
offset: function (offset) { | ||
sql.offset = offset; | ||
return this; | ||
}, | ||
limit: function (limit) { | ||
sql.limit = limit; | ||
return this; | ||
}, | ||
order: function (column, dir) { | ||
sql.order.push({ | ||
c : column, | ||
d : (dir == "Z" ? "DESC" : "ASC") | ||
}); | ||
return this; | ||
}, | ||
build: function () { | ||
@@ -177,2 +195,25 @@ var query = [], tmp, i, j, select_all = true; | ||
// order | ||
if (sql.order.length > 0) { | ||
tmp = []; | ||
for (i = 0; i < sql.order.length; i++) { | ||
tmp.push(Dialect.escapeId(sql.order[i].c) + " " + sql.order[i].d); | ||
} | ||
if (tmp.length > 0) { | ||
query.push("ORDER BY " + tmp.join(", ")); | ||
} | ||
} | ||
// limit | ||
if (sql.hasOwnProperty("limit")) { | ||
if (sql.hasOwnProperty("offset")) { | ||
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset); | ||
} else { | ||
query.push("LIMIT " + sql.limit); | ||
} | ||
} else if (sql.hasOwnProperty("offset")) { | ||
query.push("OFFSET " + sql.offset); | ||
} | ||
return query.join(" "); | ||
@@ -179,0 +220,0 @@ } |
@@ -12,2 +12,6 @@ exports.build = function (Dialect, where) { | ||
if (query.length === 0) { | ||
return []; | ||
} | ||
return "WHERE (" + query.join(") OR (") + ")"; | ||
@@ -14,0 +18,0 @@ }; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"license": "MIT", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -15,2 +15,12 @@ var common = require('../common'); | ||
assert.equal( | ||
common.Select().from('table1').select([ 'id', 'name' ]).build(), | ||
"SELECT `id`, `name` FROM `table1`" | ||
); | ||
assert.equal( | ||
common.Select().from('table1').select().build(), | ||
"SELECT * FROM `table1`" | ||
); | ||
assert.equal( | ||
common.Select().from('table1').select('id1', 'name') | ||
@@ -17,0 +27,0 @@ .from('table2', 'id2', 'id1').select('id2').build(), |
26258
29
945