From e51bd2d360deec9038c0224fbe624a2d05788c20 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 22 May 2013 18:04:05 -0700 Subject: [PATCH] Fix the column mapping --- lib/datasource.js | 28 ++++++++++++++++------------ lib/sql.js | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index c0f4d070..3f6b000a 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -504,8 +504,11 @@ DataSource.prototype.tableName = function (modelName) { * @returns {String} columnName */ DataSource.prototype.columnName = function (modelName, propertyName) { + if(!propertyName) { + return propertyName; + } var property = this.definitions[modelName].properties[propertyName]; - if(property[this.adapter.name]) { + if(property && property[this.adapter.name]) { return property[this.adapter.name].columnName || propertyName; } else { return propertyName; @@ -535,22 +538,23 @@ DataSource.prototype.columnNames = function (modelName) { * @param modelName * @returns {String} columnName for ID */ -DataSource.prototype.getIDColumnName = function(modelName) { +DataSource.prototype.idColumnName = function(modelName) { + return this.columnName(modelName, this.idName(modelName)); +} + +/** + * Find the ID column name + * @param modelName + * @returns {String} columnName for ID + */ +DataSource.prototype.idName = function(modelName) { var props = this.definitions[modelName].properties; var self = this; Object.keys(props).forEach(function(key) { - var columnInfo = props[key][self.adapter.name]; - if(columnInfo && columnInfo.id) { - if(columnInfo.columnName) { - return columnInfo.columnName; - } else { - return key; - } + if(props[key].id) { + return key; } }); - if(props.id) { - return self.columnName(modelName, 'id'); - } return null; } diff --git a/lib/sql.js b/lib/sql.js index aa9b7b98..fd2b4c53 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -21,12 +21,35 @@ BaseSQL.prototype.queryOne = function (sql, callback) { }); }; +BaseSQL.prototype.dataSource = function(model) { + if(!model) { + console.log(new Error('model').stack); + } + return this._models[model].model.schema; +} + BaseSQL.prototype.table = function (model) { - return this._models[model].model.schema.tableName(model); + return this.dataSource(model).tableName(model); }; BaseSQL.prototype.column = function (model, property) { - return this._models[model].model.schema.columnName(model, property); + return this.dataSource(model).columnName(model, property); +}; + +BaseSQL.prototype.idName = function (model) { + return this.dataSource(model).idName(model); +}; + +BaseSQL.prototype.idColumn = function (model) { + return this.dataSource(model).idColumnName(model); +}; + +BaseSQL.prototype.idColumnEscaped = function (model) { + return this.escapeName(this.dataSource(model).idColumnName(model)); +}; + +BaseSQL.prototype.id = function (model, prop) { + return this._models[model].properties[prop].id; }; BaseSQL.prototype.escapeName = function (name) { @@ -51,7 +74,8 @@ BaseSQL.prototype.defineProperty = function (model, prop, params) { }; BaseSQL.prototype.save = function (model, data, callback) { - var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + this.toFields(model, data) + ' WHERE ' + this.escapeName('id') + ' = ' + Number(data.id); + var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + + this.toFields(model, data) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + Number(data.id); this.query(sql, function (err) { callback(err); @@ -61,7 +85,7 @@ BaseSQL.prototype.save = function (model, data, callback) { BaseSQL.prototype.exists = function (model, id, callback) { var sql = 'SELECT 1 FROM ' + - this.tableEscaped(model) + ' WHERE ' + this.escapeName('id') + ' = ' + Number(id) + ' LIMIT 1'; + this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + Number(id) + ' LIMIT 1'; this.query(sql, function (err, data) { if (err) return callback(err); @@ -75,7 +99,7 @@ BaseSQL.prototype.find = function find(model, id, callback) { callback(new Error('id is not a number')); } var sql = 'SELECT * FROM ' + - this.tableEscaped(model) + ' WHERE ' + this.escapeName('id') + ' = ' + idNumber + ' LIMIT 1'; + this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + idNumber + ' LIMIT 1'; this.query(sql, function (err, data) { if (data && data.length === 1) { @@ -118,7 +142,7 @@ BaseSQL.prototype.count = function count(model, callback, where) { function buildWhere(conds) { var cs = []; Object.keys(conds || {}).forEach(function (key) { - var keyEscaped = self.escapeName(key); + var keyEscaped = self.columnEscaped(key); if (conds[key] === null) { cs.push(keyEscaped + ' IS NULL'); } else {