Skip to content
Browse files

Add ordering

  • Loading branch information...
1 parent 69f172b commit b62b0276c509a2d3d1e731a663b4df1b8fd2043c @MrRio MrRio committed Feb 3, 2011
Showing with 19 additions and 3 deletions.
  1. +5 −1 index.html
  2. +14 −2 titaniorm.js
View
6 index.html
@@ -20,7 +20,11 @@
var output = table.find();
// Find by field
- var output = table.find(null, { id: 1 });
+ var output = table.find(null, { conditions: {id: 1} });
+
+ // Order
+ var output = table.find(null, { conditions: {id: 1}, order: 'species ASC' });
+
// Select just a few columns
var output = table.find(['species', 'weight']);
View
16 titaniorm.js
@@ -42,18 +42,30 @@ var db = function(database, table) {
sql += 'COMMIT;';
}
- var selectQuery = function(fields, conditions) {
+ var selectQuery = function(fields, params) {
+ var conditions;
+ var order = 'ID ASC';
+ if(params != undefined) {
+ if(params.conditions != undefined) {
+ conditions = params.conditions;
+ }
+ if(params.order != undefined) {
+ order = params.order;
+ }
+ }
+
var field_sql;
if(fields == 'all' || fields == null) {
field_sql = '*';
} else {
field_sql = fields.join(', ');
}
var sql = 'SELECT ' + field_sql + ' FROM ' + table + ' WHERE 1=1';
-
+ // @TODO: Use prepared queries on conditions to prevent injection
for(var condition in conditions) {
sql += ' AND ' + condition + ' = ' + conditions[condition];
}
+ sql += ' ORDER BY ' + order.toUpperCase();
var rows = execute(sql);
var output_rows = [];
while (rows.isValidRow()) {

0 comments on commit b62b027

Please sign in to comment.
Something went wrong with that request. Please try again.