Permalink
Browse files

fix code add some tests

  • Loading branch information...
1 parent 73066d5 commit 4b34c8c08a2730a946fa352cf63902708fee4434 @ssinghi ssinghi committed Sep 4, 2010
Showing with 351 additions and 27 deletions.
  1. +22 −0 LICENSE
  2. +2 −7 example.js
  3. +27 −0 lib/connection.js~
  4. +4 −4 lib/query.js
  5. +156 −0 lib/query.js~
  6. +3 −0 lib/relationship.js~
  7. +15 −15 lib/table.js
  8. +60 −0 lib/table.js~
  9. +1 −1 lib/utils.js
  10. +14 −0 lib/utils.js~
  11. +8 −0 tests/fixtures/config.js
  12. +8 −0 tests/fixtures/config.js~
  13. +28 −0 tests/test-table.js
  14. +3 −0 tests/test-table.js~
View
@@ -0,0 +1,22 @@
+MIT License
+-----------
+
+Copyright (C) 2010 Kreeti Technologies
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -1,13 +1,8 @@
-config = require('./docs/config')
-
-RDX = require('./index').RDX;
-
-cp = new RDX.ConnectionPool(config.cfg)
-users = new RDX.Table(cp, 'users');
-collections = new RDX.Table(cp, 'collections');
users.has_many('collections', collections, 'user_id')
x = users.query().where('id = 675011 ').executeSync()[0];
console.log(x)
x.first_name = "S";
x.save()
+
+
View
@@ -0,0 +1,27 @@
+
+var mysql = require("../node-mysql-libmysqlclient/mysql_bindings");
+
+exports.ConnectionPool = function (config){
+ this.connections = [];
+ this.config = config;
+ this.establishConnection = function() {
+ this.connections.push(Connection(this.config));
+ return this;
+ };
+
+ this.getConnection = function() {
+ if(this.connections.length < 1) this.establishConnection();
+ var rand = parseInt(Math.random() * this.connections.length);
+ return this.connections[rand];
+ }
+}
+
+var Connection = exports.Connection = function(config)
+{
+ var conn = new mysql.MysqlConn;
+ conn.connect(config.host, config.user, config.password, config.database, config.port, config.socket);
+ // this.__proto__ = conn;
+ return conn;
+}
+
+
View
@@ -115,25 +115,25 @@ Query.prototype.count = function (field) {
Query.prototype.sum = function (field) {
var q = Utils.clone(this);
- q._select = q._select.concat(['sum(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ q._select = q._select.concat(['sum(' + ((field instanceof Field) ? field.toSql() : field.toString()) + ')']);
return q;
}
Query.prototype.min = function (field) {
var q = Utils.clone(this);
- q._select = q._select.concat(['min(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ q._select = q._select.concat(['min(' + ((field instanceof Field) ? field.toSql() : field.toString()) + ')']);
return q;
}
Query.prototype.max = function (field) {
var q = Utils.clone(this);
- q._select = q._select.concat(['max(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ q._select = q._select.concat(['max(' + ((field instanceof Field) ? field.toSql() : field.toString()) + ')']);
return q;
}
Query.prototype.avg = function (field) {
var q = Utils.clone(this);
- q._select = q._select.concat(['avg(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ q._select = q._select.concat(['avg(' + ((field instanceof Field) ? field.toSql() : field.toString()) + ')']);
return q;
}
View
@@ -0,0 +1,156 @@
+var Field = require('./field').Field;
+var Utils = require('./utils').Utils;
+
+
+var Query = exports.Query = function (){
+ this._select = [];
+ this._from = [];
+ this._where = [];
+ this._order = null;
+ this._joins = [];
+ this._group = null;
+ this._having = null;
+ this._limit = null;
+ this._offset = null;
+ this._connectionPool = null;
+ this._chain = null;
+ this._chained_object = null;
+};
+
+Query.prototype.toSql = function () {
+ var chain = this._chain;
+ var chained_object = this._chained_object;
+ var q = this;
+ while(chain) {
+ q = q.merge(chain.query(chained_object));
+ chained_object = q._chained_object = chain._chained_object;
+ chain = q._chain = chain._chain;
+ }
+ var sql = "select " + (this._select.map(function(f){return (field instanceof Field) ? field.toSql() : field.toString(); }).join(",") || "*");
+ sql += " from " + this._from.map(function(t){return t.table_name;}).join(", ");
+ sql += " " + this._joins.join(" ");
+
+ sql += this.whereStatement();
+
+ if(this._group)
+ sql += " group by " + this._group;
+ if(this._having)
+ sql += " order by " + this._having;
+ if(this._order)
+ sql += " order by " + this._order;
+ if(this._limit)
+ sql += " limit " + this._limit;
+ if(this._offset)
+ sql += " offset " + this._offset;
+
+ return sql;
+}
+
+Query.prototype.executeSync = function () {
+ var results = this._connectionPool.getConnection().querySync(this.toSql()).fetchAllSync();
+ var model = this._from[0];
+ if(results && model) {
+ return results.map(function(res) {
+ var m = new model();
+ for(f in res) {m[f] = res[f];}
+ return m;
+ });
+ }
+ return results;
+}
+
+Query.prototype.where = function (obj) {
+ var q = Utils.clone(this);
+ q._where = q._where.concat(obj);
+ return q;
+}
+
+Query.prototype.take = function (obj) {
+ var q = Utils.clone(this);
+ q._limit = obj;
+ return q;
+}
+
+Query.prototype.group = function (obj) {
+ var q = Utils.clone(this);
+ q._group = obj;
+ return q;
+}
+
+Query.prototype.having = function (obj) {
+ var q = Utils.clone(this);
+ q._having = obj;
+ return q;
+}
+
+Query.prototype.skip = function (obj) {
+ var q = Utils.clone(this);
+ q._offset = obj;
+ return q;
+}
+
+Query.prototype.join = function (join_query) {
+ var q = Utils.clone(this);
+ q._joins = q._joins.concat(join_query);
+ return q;
+}
+
+Query.prototype.select = function (select) {
+ var q = Utils.clone(this);
+ q._select = select;
+ return q;
+}
+
+Query.prototype.count = function (field) {
+ var q = Utils.clone(this);
+ var f = "1";
+ if(field instanceof Field) {
+ f = field.toSql();
+ } else if(field) {
+ f = field.toString();
+ }
+ q._select = q._select.concat(['count(' + f + ')']);
+ return q;
+}
+
+Query.prototype.sum = function (field) {
+ var q = Utils.clone(this);
+ q._select = q._select.concat(['sum(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ return q;
+}
+
+Query.prototype.min = function (field) {
+ var q = Utils.clone(this);
+ q._select = q._select.concat(['min(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ return q;
+}
+
+Query.prototype.max = function (field) {
+ var q = Utils.clone(this);
+ q._select = q._select.concat(['max(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ return q;
+}
+
+Query.prototype.avg = function (field) {
+ var q = Utils.clone(this);
+ q._select = q._select.concat(['avg(' + ((field instanceof Field) ? field.toSql() + field.toString()) + ')']);
+ return q;
+}
+
+Query.prototype.merge = function (other_query) {
+ var q = Utils.clone(this);
+
+ q._select = this._select.concat(other_query._select);
+ q._from = this._from.concat(other_query._from);
+ q._where = this._where.concat(other_query._where);
+ q._order = this._order.concat(other_query._order);
+
+ return q;
+}
+
+Query.prototype.whereStatement = function() {
+ if(this._where && this._where.length > 0)
+ return " where " + this._where.join(" and ");
+ return "";
+}
+
@@ -0,0 +1,3 @@
+exports.Relationship = function() {
+ this.name
+}
View
@@ -3,26 +3,27 @@ var Record = require('./record').Record;
var Field = require('./field').Field;
var Utils = require('./utils').Utils;
+var table_function = function() { return function(arg) { if(arg) { Utils.extend(this, arg); }; };};
+
+
var Table = exports.Table = Table = function(connection_pool, table_name) {
var me = this;
- this.table_name = table_name;
+ this.tableName = table_name;
this.connectionPool = connection_pool;
- this.primary_key = 'id';
+ this.primaryKey = 'id';
this.fields = this.connectionPool.getConnection().querySync('show fields from ' + table_name).fetchAllSync().map(function(f){
return new Field(me, f);});
this.relationships = [];
- var func = new Function(arg) {
- if(arg) {
- Utils.extend(this, arg);
- }
- };
+
+ var func = table_function;
Utils.extend(func.prototype, Record);
func.__proto__ = this;
return func;
};
+
Table.prototype.query = function() {
var q = new Query();
q._from = [this];
@@ -32,20 +33,19 @@ Table.prototype.query = function() {
return q;
}
-ToMany = function(primary_table, primary_key, join_table, join_key ){
+
+var relationship_function = function(t) { return function() { t._chained_object = this; return t; }};
+
+var ToMany = function(primary_table, primary_key, join_table, join_key ){
this.primary_table = primary_table;
this.primary_key = primary_key
this.join_table = this.join_table;
this.join_key = this.join_key;
var t = Utils.clone(join_table);
- var t._chain = this;
+ t._chain = this;
- var f = new function() {
- t._chained_object = this;
- return t;
- }
- return f;
+ return relationship_function(t);
};
ToMany.prototype.query = function(chained_object) {
@@ -57,4 +57,4 @@ ToMany.prototype.query = function(chained_object) {
this.join_table.table_name + "." + this.join_table.join_key];
}
return q;
-}
+};
View
@@ -0,0 +1,60 @@
+var Query = require('./query').Query;
+var Record = require('./record').Record;
+var Field = require('./field').Field;
+var Utils = require('./utils').Utils;
+
+var Table = exports.Table = Table = function(connection_pool, table_name) {
+ var me = this;
+ this.table_name = table_name;
+ this.connectionPool = connection_pool;
+ this.primary_key = 'id';
+ this.fields = this.connectionPool.getConnection().querySync('show fields from ' + table_name).fetchAllSync().map(function(f){
+ return new Field(me, f);});
+
+ this.relationships = [];
+ var func = new Function(arg) {
+ if(arg) {
+ Utils.extend(this, arg);
+ }
+ };
+ Utils.extend(func.prototype, Record);
+
+ func.__proto__ = this;
+ return func;
+};
+
+Table.prototype.query = function() {
+ var q = new Query();
+ q._from = [this];
+ q._connectionPool = this.connectionPool;
+ q._chain = this._chain;
+ q._chained_object = this._chained_object;
+ return q;
+}
+
+ToMany = function(primary_table, primary_key, join_table, join_key ){
+ this.primary_table = primary_table;
+ this.primary_key = primary_key
+ this.join_table = this.join_table;
+ this.join_key = this.join_key;
+
+ var t = Utils.clone(join_table);
+ var t._chain = this;
+
+ var f = new function() {
+ t._chained_object = this;
+ return t;
+ }
+ return f;
+};
+
+ToMany.prototype.query = function(chained_object) {
+ var q = new Query();
+ if(chained_object instanceof primary_table) {
+ q._where = [this.join_table.table_name + "." + this.join_table.join_key + '=' + chained_object[this.primary_key]];
+ } else {
+ q._join = ["INNER JOIN " + this.primary_table + " ON " + this.primary_table.table_name + "." + this.primary_key + " = " +
+ this.join_table.table_name + "." + this.join_table.join_key];
+ }
+ return q;
+}
View
@@ -8,7 +8,7 @@ Utils.clone = function (obj) {
};
Utils.extend = function(receiver, object) {
- for(var f in arg) {
+ for(var f in object) {
receiver[f] = object[f];
}
}
Oops, something went wrong.

0 comments on commit 4b34c8c

Please sign in to comment.