Permalink
Browse files

Added MYSQL but its not working just yet

  • Loading branch information...
1 parent 00cb092 commit 1897ab0de1cb152e295a16c1f06c9c823707ab0a @subsonic subsonic committed May 4, 2012
Showing with 656 additions and 568 deletions.
  1. +60 −56 examples/callbacks.js
  2. +33 −33 examples/evented.js
  3. +17 −9 index.js
  4. +278 −47 lib/mysql.js
  5. +266 −71 lib/postgres.js
  6. +0 −350 lib/postgres_db.js
  7. +2 −2 test/pg_query_spec.coffee
View
@@ -1,78 +1,82 @@
-var massive = require("../lib/");
+var massive = require("../index");
var util = require("util");
var _ = require("underscore")._;
-massive.connect("postgres://postgres@localhost/test");
-//for MySQL - set the require statement in lib/index to ./lib/mysql
-//massive.connect({user : "root", password : "", database : "test"});
-var products = new massive.table("products", "id");
+massive.connect("postgres://postgres@localhost/test", function(err,db) {
+//massive.connect({user : "root", password : "", database : "test"}, function(err,db) {
-var dropProducts = massive.dropTable("products");
+ var dropProducts = db.dropTable("products").execute(function(err,data){
+ console.log("Products table dropped");
+ createProducts();
+ });
-dropProducts.execute(function(err,data){
- console.log("Products table dropped");
- createProducts();
-});
+ var createProducts = function(){
+ var createQuery = db.createTable("products", {
+ name : "string",
+ price : "money",
+ timestamps : true
+ });
+ createQuery.execute(function(err,data){
+ console.log("Products table created");
+ db.loadTables(function(err,_db){
+ db = _db;
+ insertProducts();
+ });
+
-var createProducts = function(){
- var createQuery = massive.createTable("products", {
- name : "string",
- price : "money",
- timestamps : true
- });
- createQuery.execute(function(err,data){
- console.log("Products table created");
- insertProducts();
- });
-}
+ });
+ }
-var insertProducts = function() {
+ var insertProducts = function() {
- var items = [
- {name:"stuffy stuff", price: 12.00},
- {name:"poofy poof", price: 24.00}
- ];
- products.insertBatch(items).execute(function(err,newProducts){
- console.log("HO")
- console.log("Added " + newProducts.rowCount + " to db");
- showProducts(function(){
- updatePrices();
+ var items = [
+ {name:"stuffy stuff", price: 12.00},
+ {name:"poofy poof", price: 24.00}
+ ];
+ db.products.insert(items).execute(function(err,newProducts){
+ console.log("Added " + newProducts.length + " products to db - here they are");
+ showProducts(function(){
+ updatePrices();
+ });
});
- });
-
-}
-var showProducts = function(callback){
+ }
- products.all().execute(function(err, products){
- _.each(products, function(p){
+ var showProducts = function(callback){
+ var query = db.products.find();
+ query.on("row", function(p){
console.log(p);
});
- if (callback) callback();
- });
-
-}
+ query.on("end", callback)
+ }
+
+ var updatePrices = function(){
+ console.log("Updating prices due to inflation");
+ db.products.update({price : 100.00}, {"id >" : 0}).execute(function(err,results){
+ console.log("Prices updated: " + results.rowCount);
+ showProducts(function(){
+ deleteAll();
+ });
+ });
+ }
-var updatePrices = function(){
- console.log("Updating prices due to inflation");
- products.update({price : 100.00}, {"id >" : 0}).execute(function(err,results){
- console.log("Prices updated: " + results.rowCount);
- showProducts(function(){
- deleteAll();
+ var deleteAll = function() {
+ console.log("Deleting everything, outta here!");
+ var query = db.products.destroy();
+ query.execute(function(err,results){
+ console.log("Everything toast!");
});
- });
-}
+ }
+
+
+
+});
+
+
-var deleteAll = function() {
- console.log("Deleting everything, outta here!");
- var query = products.destroy();
- query.execute(function(err,results){
- console.log("Everything toast!");
- });
-}
View
@@ -1,46 +1,46 @@
-var massive = require("../lib/");
+var massive = require("../index");
var util = require("util");
var _ = require("underscore")._;
-massive.connect("postgres://postgres@localhost/test");
+massive.connect("postgres://postgres@localhost/test", function(err, db){
+ var dropProducts = db.dropTable("products");
-var products = new massive.table("products");
-
-//this is a query
-var dropProducts = massive.dropTable("products");
+ var createProducts = db.createTable("products", {
+ name : "string",
+ price : "money",
+ timestamps : true
+ });
-var createProducts = massive.createTable("products", {
- name : "string",
- price : "money",
- timestamps : true
-});
+ var items = [
+ {name:"stuffy stuff", price: 12.00},
+ {name:"poofy poof", price: 24.00}
+ ];
-var items = [
- {name:"stuffy stuff", price: 12.00},
- {name:"poofy poof", price: 24.00}
-];
+ var batchInsert = db.products.insert(items);
+ var updatePrices = db.products.update({price : 100.00}, {"id >" : 0});
+ var select = db.products.find();
+ var destroyAll = db.products.destroy();
-var batchInsert = products.insertBatch(items);
-var updatePrices = products.update({price : 100.00}, {"id >" : 0});
-var select = products.all();
-var destroyAll = products.destroy();
+ destroyAll.once("executed", function(){
+ console.log("No more products!");
+ });
-destroyAll.once("executed", function(){
- console.log("No more products!");
-});
+ updatePrices.once("executed", function(){
+ select.on("row", function(product){
+ console.log(product);
+ });
-updatePrices.once("executed", function(){
- select.on("row", function(product){
- console.log(product);
});
- select.on("executed", function(){
- destroyAll.execute();
- })
-})
-batchInsert.once("executed", updatePrices.execute);
-createProducts.once("executed", batchInsert.execute);
-dropProducts.once("executed", createProducts.execute);
+
+ select.on("end", destroyAll.execute);
+ batchInsert.once("executed", updatePrices.execute);
+ createProducts.once("executed", batchInsert.execute);
+ dropProducts.once("executed", createProducts.execute);
-dropProducts.execute();
+ dropProducts.execute();
+
+
+});
+
View
@@ -1,17 +1,25 @@
-var pg = require("./lib/postgres_db");
+var pg = require("./lib/postgres");
+var mysql = require("./lib/mysql");
+var _ = require("underscore")._;
+
+var isPostgres = function(connection){
+ return !_.isObject(connection) && connection.indexOf("postgres://") > -1;
+};
+
+var isMySQL = function(connection){
+ return (_.isObject(connection));
+}
module.exports.connect = function(connection, callback) {
//console.log("Massive connection set to " + connection);
var db;
- try{
- if(connection.indexOf("postgres://") > -1){
- db = new pg(connection);
- }
- }catch(err){
- callback(err,null);
- return;
- }
+ if(isPostgres(connection)){
+ db = new pg(connection);
+ }else if (isMySQL(connection)) {
+ db = new mysql(connection);
+ }
+
db.loadTables(callback);
//_client.connect(connection);
};
Oops, something went wrong.

0 comments on commit 1897ab0

Please sign in to comment.