diff --git a/lib/dbwrap.js b/lib/dbwrap.js index 3e84451..3f943b7 100644 --- a/lib/dbwrap.js +++ b/lib/dbwrap.js @@ -100,24 +100,36 @@ function wrap(db, dir) { } DB.find = function (opts, params) { - var sql = 'SELECT * FROM ' + tablename + ' WHERE '; + var sql = 'SELECT * FROM \'' + tablename + '\' '; + var keys = opts && Object.keys(opts); - Object.keys(opts).forEach(function (key, i) { - if (i !== 0) { - sql += 'AND '; - } - sql += db.escape(snakeCase(key)) + " = '" + db.escape(opts[key]) + "'"; - }); + if (opts && keys.length) { + sql += 'WHERE '; + + keys.forEach(function (key, i) { + if (i !== 0) { + sql += 'AND '; + } + sql += db.escape(snakeCase(key)) + " = '" + db.escape(opts[key]) + "'"; + }); + } + else if (null !== opts || (params && !params.limit)) { + return PromiseA.reject(new Error("to find all you must explicitly specify find(null, { limit: <> })")); + } if (params) { if (params.orderBy) { - sql += ' ORDER BY ' + db.escape(snakeCase(params.orderBy)); + sql += " ORDER BY \"" + db.escape(snakeCase(params.orderBy) + "\" "); if (params.orderByDesc) { - sql += ' DESC'; + sql += 'DESC '; } } + if (params.limit) { + sql += " LIMIT " + parseInt(params.limit, 10); + } } + console.log('sql', sql); return db.allAsync(sql, []).then(simpleMap); };