Comparing version 0.5.3 to 0.5.4
@@ -124,3 +124,3 @@ // Generated by CoffeeScript 1.6.2 | ||
_buildWhere = function(schema, conditions, conjunction) { | ||
var key, keys, obj, subs; | ||
var after_count, before_count, key, keys, obj, subs; | ||
@@ -165,2 +165,5 @@ if (conjunction == null) { | ||
if (conjunction === '$and') { | ||
before_count = _.reduce(subs, (function(memo, sub) { | ||
return memo + Object.keys(sub).length; | ||
}), 0); | ||
subs.unshift({}); | ||
@@ -170,3 +173,4 @@ obj = _.extend.apply(_, subs); | ||
keys = Object.keys(obj); | ||
if (!_.some(keys, function(key) { | ||
after_count = keys.length; | ||
if (before_count === after_count && !_.some(keys, function(key) { | ||
return key.substr(0, 1) === '$'; | ||
@@ -173,0 +177,0 @@ })) { |
@@ -336,11 +336,7 @@ // Generated by CoffeeScript 1.6.2 | ||
MySQLAdapter.prototype.findById = function(model, id, options, callback) { | ||
var selects, sql, | ||
var select, sql, | ||
_this = this; | ||
if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
} else { | ||
selects = '*'; | ||
} | ||
sql = "SELECT " + selects + " FROM " + (tableize(model)) + " WHERE id=? LIMIT 1"; | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
sql = "SELECT " + select + " FROM " + (tableize(model)) + " WHERE id=? LIMIT 1"; | ||
return this._query(sql, id, function(error, result) { | ||
@@ -361,11 +357,9 @@ if (error) { | ||
MySQLAdapter.prototype.find = function(model, conditions, options, callback) { | ||
var field, location, order_by, orders, params, selects, sql, | ||
var field, location, order_by, orders, params, select, sql, | ||
_this = this; | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
select = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else { | ||
selects = '*'; | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
} | ||
@@ -375,6 +369,6 @@ if ((options.near != null) && (field = Object.keys(options.near)[0])) { | ||
location = options.near[field]; | ||
selects += ",GLENGTH(LINESTRING(" + field + ",POINT(" + location[0] + "," + location[1] + "))) AS " + field + "_distance"; | ||
select += ",GLENGTH(LINESTRING(" + field + ",POINT(" + location[0] + "," + location[1] + "))) AS " + field + "_distance"; | ||
} | ||
params = []; | ||
sql = "SELECT " + selects + " FROM " + (tableize(model)); | ||
sql = "SELECT " + select + " FROM " + (tableize(model)); | ||
if (conditions.length > 0) { | ||
@@ -381,0 +375,0 @@ try { |
@@ -333,12 +333,8 @@ // Generated by CoffeeScript 1.6.2 | ||
PostgreSQLAdapter.prototype.findById = function(model, id, options, callback) { | ||
var selects, table, | ||
var select, table, | ||
_this = this; | ||
if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
} else { | ||
selects = '*'; | ||
} | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
table = tableize(model); | ||
return this._query("SELECT " + selects + " FROM " + table + " WHERE id=$1 LIMIT 1", [id], function(error, result) { | ||
return this._query("SELECT " + select + " FROM " + table + " WHERE id=$1 LIMIT 1", [id], function(error, result) { | ||
var rows; | ||
@@ -361,14 +357,12 @@ | ||
PostgreSQLAdapter.prototype.find = function(model, conditions, options, callback) { | ||
var orders, params, selects, sql, | ||
var orders, params, select, sql, | ||
_this = this; | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
select = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else { | ||
selects = '*'; | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
} | ||
params = []; | ||
sql = "SELECT " + selects + " FROM " + (tableize(model)); | ||
sql = "SELECT " + select + " FROM " + (tableize(model)); | ||
if (conditions.length > 0) { | ||
@@ -375,0 +369,0 @@ try { |
@@ -159,2 +159,20 @@ // Generated by CoffeeScript 1.6.2 | ||
SQLAdapterBase.prototype._buildSelect = function(model_class, select) { | ||
var schema; | ||
if (select) { | ||
if (select.length > 0) { | ||
schema = model_class._schema; | ||
select = select.map(function(column) { | ||
return schema[column]._dbname; | ||
}); | ||
return 'id,' + select.join(','); | ||
} else { | ||
return 'id'; | ||
} | ||
} else { | ||
return '*'; | ||
} | ||
}; | ||
return SQLAdapterBase; | ||
@@ -161,0 +179,0 @@ |
@@ -278,12 +278,8 @@ // Generated by CoffeeScript 1.6.2 | ||
SQLite3Adapter.prototype.findById = function(model, id, options, callback) { | ||
var selects, table, | ||
var select, table, | ||
_this = this; | ||
if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
} else { | ||
selects = '*'; | ||
} | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
table = tableize(model); | ||
return this._query('all', "SELECT " + selects + " FROM " + table + " WHERE id=? LIMIT 1", id, function(error, result) { | ||
return this._query('all', "SELECT " + select + " FROM " + table + " WHERE id=? LIMIT 1", id, function(error, result) { | ||
if (error) { | ||
@@ -303,14 +299,12 @@ return callback(SQLite3Adapter.wrapError('unknown error', error)); | ||
SQLite3Adapter.prototype.find = function(model, conditions, options, callback) { | ||
var orders, params, selects, sql, | ||
var orders, params, select, sql, | ||
_this = this; | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
select = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else { | ||
selects = '*'; | ||
select = this._buildSelect(this._connection.models[model], options.select); | ||
} | ||
params = []; | ||
sql = "SELECT " + selects + " FROM " + (tableize(model)); | ||
sql = "SELECT " + select + " FROM " + (tableize(model)); | ||
if (conditions.length > 0) { | ||
@@ -317,0 +311,0 @@ try { |
@@ -64,3 +64,3 @@ // Generated by CoffeeScript 1.6.2 | ||
Query.prototype.select = function(columns) { | ||
var schema_columns; | ||
var schema_columns, select; | ||
@@ -70,6 +70,16 @@ this._options.select = null; | ||
if (typeof columns === 'string') { | ||
columns = columns.split(/\s+/).filter(function(column) { | ||
return schema_columns.indexOf(column) >= 0; | ||
select = []; | ||
columns.split(/\s+/).forEach(function(column) { | ||
if (schema_columns.indexOf(column) >= 0) { | ||
return select.push(column); | ||
} else { | ||
column += '.'; | ||
return schema_columns.forEach(function(sc) { | ||
if (sc.indexOf(column) === 0) { | ||
return select.push(sc); | ||
} | ||
}); | ||
} | ||
}); | ||
this._options.select = columns; | ||
this._options.select = select; | ||
} | ||
@@ -76,0 +86,0 @@ return this; |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "orm", |
179863
4694