Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
fixed query client issue
Browse files Browse the repository at this point in the history
  • Loading branch information
subsonic committed Dec 1, 2012
1 parent 1542eb1 commit a6a9612
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 238 deletions.
4 changes: 2 additions & 2 deletions examples/callbacks.js
Expand Up @@ -4,8 +4,8 @@ var _ = require("underscore")._;






//massive.connect("postgres://postgres@localhost/test", function(err,db) { massive.connect("postgres://postgres@localhost/test", function(err,db) {
massive.connect({user : "root", password : "", database : "test"}, function(err,db) { //massive.connect({user : "root", password : "", database : "test"}, function(err,db) {
console.log("err " + err) console.log("err " + err)
var dropProducts = db.dropTable("products").execute(function(err,data){ var dropProducts = db.dropTable("products").execute(function(err,data){
console.log("Products table dropped"); console.log("Products table dropped");
Expand Down
7 changes: 4 additions & 3 deletions examples/evented.js
Expand Up @@ -2,15 +2,16 @@ var massive = require("../index");
var util = require("util"); var util = require("util");
var _ = require("underscore")._; var _ = require("underscore")._;


//massive.connect("postgres://postgres@localhost/test", function(err, db){ massive.connect("postgres://postgres@localhost/test", function(err, db){
massive.connect({user : "root", password : "", database : "test"}, function(err,db) { //massive.connect({user : "rob", password : "", database : "test"}, function(err,db) {

var dropProducts = db.dropTable("products"); var dropProducts = db.dropTable("products");


var createProducts = db.createTable("products", { var createProducts = db.createTable("products", {
name : "string", name : "string",
price : "money", price : "money",
timestamps : true timestamps : true
}); }).execute();




var items = [ var items = [
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Expand Up @@ -92,7 +92,7 @@ var Query = function(sql, params, table) {
self.execute = function(callback) { self.execute = function(callback) {
self.db.execute(self.sql, self.params, function(err,result) { self.db.execute(self.sql, self.params, function(err,result) {
if(callback) { callback(err, result); } if(callback) { callback(err, result); }
self.emit("executed",client); self.emit("executed");
}); });
}; };


Expand Down
312 changes: 156 additions & 156 deletions test/mysql_queries_spec.coffee
@@ -1,157 +1,157 @@
massive = require("../index") # massive = require("../index")
should = require("should") # should = require("should")
util = require("util") # util = require("util")
helper = require('./mysql_helper') # helper = require('./mysql_helper')


describe "MySQL Queries", -> # describe "MySQL Queries", ->
db = null # db = null
before (done) -> # before (done) ->
massive.connect helper.connectionString(), (err,_db) -> # massive.connect helper.connectionString(), (err,_db) ->
db = _db # db = _db
done() # done()


describe "initialization", -> # describe "initialization", ->


it "sets the table name", -> # it "sets the table name", ->
db.products.name.should.equal("products") # db.products.name.should.equal("products")


it "defaults the pk to id", -> # it "defaults the pk to id", ->
db.products.pk.should.equal("id") # db.products.pk.should.equal("id")




describe "SELECT queries", -> # describe "SELECT queries", ->
it "runs a select *", -> # it "runs a select *", ->
query = db.products.find() # query = db.products.find()
query.sql.should.equal "SELECT * FROM products" # query.sql.should.equal "SELECT * FROM products"
query.params.length.should.equal 0 # query.params.length.should.equal 0


it "adds columns when specified", -> # it "adds columns when specified", ->
query = db.products.find ["name"] # query = db.products.find ["name"]
query.sql.should.equal "SELECT name FROM products" # query.sql.should.equal "SELECT name FROM products"




it "adds columns when specified in criteria", -> # it "adds columns when specified in criteria", ->
query = db.products.find {}, {columns : "name"} # query = db.products.find {}, {columns : "name"}
query.sql.should.equal "SELECT name FROM products" # query.sql.should.equal "SELECT name FROM products"
query.params.length.should.equal 0 # query.params.length.should.equal 0


it "adds a where when id is a number", -> # it "adds a where when id is a number", ->
query = db.products.find {id : 1} # query = db.products.find {id : 1}
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 1") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 1")
query.params.length.should.equal 0 # query.params.length.should.equal 0


it "adds a where as a primary key", -> # it "adds a where as a primary key", ->
query = db.products.find(5) # query = db.products.find(5)
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5")
query.params.length.should.equal 0 # query.params.length.should.equal 0




it "adds a LIMIT if specified", -> # it "adds a LIMIT if specified", ->
query = db.products.find(5).limit(1) # query = db.products.find(5).limit(1)
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT 1") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT 1")


it "adds a LIMIT with SKIP if specified", -> # it "adds a LIMIT with SKIP if specified", ->
query = db.products.find(5).limit(10,1) # query = db.products.find(5).limit(10,1)
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT(10,1)") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT(10,1)")


it "adds an ORDER if specified", -> # it "adds an ORDER if specified", ->
query = db.products.find(5).order("name") # query = db.products.find(5).order("name")
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nORDER BY name") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nORDER BY name")


it "handles greater than", -> # it "handles greater than", ->
query = db.products.find({"id >" : "steve"}) # query = db.products.find({"id >" : "steve"})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" > ?") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" > ?")
query.params.length.should.equal 1 # query.params.length.should.equal 1


it "handles less than", -> # it "handles less than", ->
query = db.products.find({"id <" : "steve"}) # query = db.products.find({"id <" : "steve"})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" < ?") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" < ?")
query.params.length.should.equal 1 # query.params.length.should.equal 1


it "handles bang equal", -> # it "handles bang equal", ->
query = db.products.find({"id !=" : "steve"}) # query = db.products.find({"id !=" : "steve"})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?")
query.params.length.should.equal 1 # query.params.length.should.equal 1


it "handles ineqaulity", -> # it "handles ineqaulity", ->
query = db.products.find({"id <>" : "steve"}) # query = db.products.find({"id <>" : "steve"})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?")
query.params.length.should.equal 1 # query.params.length.should.equal 1


it "handles IN", -> # it "handles IN", ->
query = db.products.find({id : ["steve","juice","pete"]}) # query = db.products.find({id : ["steve","juice","pete"]})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" IN (?, ?, ?)") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" IN (?, ?, ?)")
query.params.length.should.equal 3 # query.params.length.should.equal 3


it "handles NOT IN", -> # it "handles NOT IN", ->
query = db.products.find({"id != ": ["steve","juice","pete"]}) # query = db.products.find({"id != ": ["steve","juice","pete"]})
query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" NOT IN (?, ?, ?)") # query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" NOT IN (?, ?, ?)")
query.params.length.should.equal 3 # query.params.length.should.equal 3


it "handles inline goodness", -> # it "handles inline goodness", ->
query = db.run("select * from crazytown where id = ?", [1]) # query = db.run("select * from crazytown where id = ?", [1])
query.sql.should.equal("select * from crazytown where id = ?") # query.sql.should.equal("select * from crazytown where id = ?")
query.params.length.should.equal 1 # query.params.length.should.equal 1


describe "destroy", -> # describe "destroy", ->
it "creates a delete everything", -> # it "creates a delete everything", ->
query = db.products.destroy() # query = db.products.destroy()
query.sql.should.equal "DELETE FROM products" # query.sql.should.equal "DELETE FROM products"
query.params.length.should.equal 0 # query.params.length.should.equal 0


it "uses where when specified as an argument", -> # it "uses where when specified as an argument", ->
query = db.products.destroy({id : 1}) # query = db.products.destroy({id : 1})
query.sql.should.equal "DELETE FROM products \nWHERE \"id\" = 1" # query.sql.should.equal "DELETE FROM products \nWHERE \"id\" = 1"
query.params.length.should.equal 0 # query.params.length.should.equal 0




it "adds a where as a primary key", -> # it "adds a where as a primary key", ->
query = db.products.destroy(6) # query = db.products.destroy(6)
query.sql.should.equal("DELETE FROM products \nWHERE \"id\" = 6") # query.sql.should.equal("DELETE FROM products \nWHERE \"id\" = 6")
query.params.length.should.equal 0 # query.params.length.should.equal 0


describe "insert", -> # describe "insert", ->
it "creates a basic insert with returning", -> # it "creates a basic insert with returning", ->
query = db.products.insert({name : "steve", price : 12.00}) # query = db.products.insert({name : "steve", price : 12.00})
query.sql.should.equal "INSERT INTO products (name, price) VALUES\n(?, ?)" # query.sql.should.equal "INSERT INTO products (name, price) VALUES\n(?, ?)"
query.params.length.should.equal 2 # query.params.length.should.equal 2


it "creates a batch for item arrays", -> # it "creates a batch for item arrays", ->
items = [{title:"stuffy stuff", price: 12.00, desc : "bubble"},{title:"poofy poof", price: 24.00, desc : "glurp"}] # items = [{title:"stuffy stuff", price: 12.00, desc : "bubble"},{title:"poofy poof", price: 24.00, desc : "glurp"}]
query = db.products.insert(items) # query = db.products.insert(items)
query.sql.should.equal "INSERT INTO products (title, price, desc) VALUES\n(?, ?, ?),\n(?, ?, ?)" # query.sql.should.equal "INSERT INTO products (title, price, desc) VALUES\n(?, ?, ?),\n(?, ?, ?)"
query.params.length.should.equal 6 # query.params.length.should.equal 6


it "throws an error if no data was supplied", -> # it "throws an error if no data was supplied", ->
(-> db.products.insert().execute()).should.throw # (-> db.products.insert().execute()).should.throw


describe "updates", -> # describe "updates", ->
it "creates a basic update", -> # it "creates a basic update", ->
query = db.products.update({name:"pumpkin", price:1000}, 12) # query = db.products.update({name:"pumpkin", price:1000}, 12)
query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = 12") # query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = 12")
query.params.length.should.equal 2 # query.params.length.should.equal 2


it "creates a basic update with a string key", -> # it "creates a basic update with a string key", ->
query = db.products.update({name:"pumpkin", price:1000}, "12") # query = db.products.update({name:"pumpkin", price:1000}, "12")
query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = ?") # query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = ?")
query.params.length.should.equal 3 # query.params.length.should.equal 3


it "creates a basic update with multi result", -> # it "creates a basic update with multi result", ->
query = db.products.update({name:"pumpkin", price:1000}, {"id >": 12}) # query = db.products.update({name:"pumpkin", price:1000}, {"id >": 12})
query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" > 12") # query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" > 12")
query.params.length.should.equal 2 # query.params.length.should.equal 2


it "updates all rows", -> # it "updates all rows", ->
query = db.products.update({name:"leto", sand: true}) # query = db.products.update({name:"leto", sand: true})
query.sql.should.equal("UPDATE products SET name = ?, sand = ?") # query.sql.should.equal("UPDATE products SET name = ?, sand = ?")
query.params.length.should.equal 2 # query.params.length.should.equal 2




describe "aggregates", -> # describe "aggregates", ->
it "counts with SELECT COUNT", -> # it "counts with SELECT COUNT", ->
query = db.products.count() # query = db.products.count()
query.sql.should.equal("SELECT COUNT(1) FROM products") # query.sql.should.equal("SELECT COUNT(1) FROM products")
it "counts with SELECT COUNT and a WHERE", -> # it "counts with SELECT COUNT and a WHERE", ->
query = db.products.count({"id > " : 1}) # query = db.products.count({"id > " : 1})
query.sql.should.equal("SELECT COUNT(1) FROM products \nWHERE \"id\" > 1") # query.sql.should.equal("SELECT COUNT(1) FROM products \nWHERE \"id\" > 1")


0 comments on commit a6a9612

Please sign in to comment.