Fix the column mapping
This commit is contained in:
parent
31473d3add
commit
e51bd2d360
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
36
lib/sql.js
36
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 {
|
||||
|
|
Loading…
Reference in New Issue