Permalink
Browse files

fixed bug with empty predicate not leaving off where clause

  • Loading branch information...
1 parent d3f2adb commit 7d7c3fdee6d5ba4e78968c5566287c0d83db0ae0 @mrjjwright committed Mar 7, 2010
Showing with 714 additions and 11 deletions.
  1. +1 −4 Cakefile
  2. +2 −4 README.md
  3. +1 −1 nosqlite.coffee
  4. +7 −1 sql.coffee
  5. +7 −1 sql.js
  6. +2 −0 test/test_sql.coffee
  7. +2 −0 test/test_sql.js
  8. +692 −0 underscore.js
View
@@ -1,6 +1,4 @@
-require.paths.unshift "/Users/johnw/js/mumblr_machine"
-require.paths.unshift "/Users/johnw/js/mumblr_machine/external"
-require.paths.unshift "/Users/johnw/js/mumblr_machine/db"
+require.paths.unshift "."
fs: require 'fs'
coffee: require 'coffee-script'
@@ -9,7 +7,6 @@ require "underscore"
task 'test', 'run the Mumblr Machine test suite', ->
process.mixin require 'assert'
process.mixin require 'sys'
- TestSuite: require('async_testing').TestSuite
passed_count: 0
failed_count: 0
View
@@ -7,7 +7,7 @@ The goal of NoSQLite is to exploit that even more than other SQLite libraries.
* zero-config - No "create table", "alter table".
* zero-schema - Just store my JS object in an intelligent way.
-* simple DSL - just find stuff, save stuff, or find_or_save stuff (ok remove stuff too).
+* zero-SQL - just a simple DSL to find stuff, save stuff, or find_or_save stuff (ok remove stuff too).
Other goals
---------------
@@ -26,6 +26,4 @@ Currently Requires
* [node](http://nodejs.org)
* [CoffeeScript](http://jashkenas.github.com/coffee-script/) - fun, clean way to write JavaScript. Includes Cake to run the Cakefile and tests.
-* [node-sqlite](http://github.com/grumdrig/node-sqlite) - I am working on rewriting this to be async and be more HTML 5 compatible.
-* [underscore.js](http://github.com/documentcloud/underscore) - this needs to be in your node require path as well
-
+* [node-sqlite](http://github.com/grumdrig/node-sqlite) - I am working on rewriting this to be async and be more HTML 5 compatible. You will have to get it and compile the node bindings and put it in your node require path
View
@@ -3,7 +3,7 @@ fs: require "fs"
require "underscore"
assert: require "assert"
sql: require "./sql"
-sqlite: require("sqlite")
+sqlite: require "sqlite"
assert: require "assert"
# A library to make it as easy as possible to store and retrieve JS objects
View
@@ -11,7 +11,13 @@ class SQL
select: (table, predicate) ->
- sql: "select rowid, * from " + table + " where"
+ sql: "select rowid, * from " + table
+ if (not predicate?) or _.isEmpty(predicate)
+ @escaped: sql
+ @placeholder: sql
+ return this
+
+ sql += " where"
predicates: []
#allow the user to pass in a single object or multiple objects
View
8 sql.js
@@ -13,7 +13,13 @@
};
SQL.prototype.select = function select(table, predicate) {
var _a, _b, _c, _d, ands_escaped, ands_placeholder, key, key_sql, predicates, sql;
- sql = "select rowid, * from " + table + " where";
+ sql = "select rowid, * from " + table;
+ if ((!(typeof predicate !== "undefined" && predicate !== null)) || _.isEmpty(predicate)) {
+ this.escaped = sql;
+ this.placeholder = sql;
+ return this;
+ }
+ sql += " where";
predicates = [];
//allow the user to pass in a single object or multiple objects
!_.isArray(predicate) ? predicates.push(predicate) : (predicates = predicate);
View
@@ -21,6 +21,8 @@ ok sql.select("log", {"external_id": true}).escaped, "select rowid, * from log w
ok sql.select("log", {"external_id": false}).escaped, "select rowid, * from log where(external_id = 'false')", "should handle predicate value = false"
ok sql.select("log", {"external_id": false}).placeholder, "select rowid, * from log where(external_id = ?)", "placeholder property should be set"
ok sql.select("log", {"external_id": false}).values.length, 1, "values property should be array of right size"
+ok sql.select("log", {}).escaped, "select rowid, * from log", "empty object predicate should leave off where clause"
+ok sql.select("log", []).escaped, "select rowid, * from log", "empty array predicate should leave off where clause"
puts "testing insert"
ok sql.insert("log", {text: "hello", log_type: "mumble"}).placeholder, "insert or replace into log(text,log_type) values (?,?)", "insert placeholder should be correct"
View
@@ -33,6 +33,8 @@
ok(sql.select("log", {
"external_id": false
}).values.length, 1, "values property should be array of right size");
+ ok(sql.select("log", {}).escaped, "select rowid, * from log", "empty object predicate should leave off where clause");
+ ok(sql.select("log", []).escaped, "select rowid, * from log", "empty array predicate should leave off where clause");
puts("testing insert");
ok(sql.insert("log", {
text: "hello",
Oops, something went wrong.

0 comments on commit 7d7c3fd

Please sign in to comment.