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
|
* @returns {String} columnName
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.columnName = function (modelName, propertyName) {
|
DataSource.prototype.columnName = function (modelName, propertyName) {
|
||||||
|
if(!propertyName) {
|
||||||
|
return propertyName;
|
||||||
|
}
|
||||||
var property = this.definitions[modelName].properties[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;
|
return property[this.adapter.name].columnName || propertyName;
|
||||||
} else {
|
} else {
|
||||||
return propertyName;
|
return propertyName;
|
||||||
|
@ -535,22 +538,23 @@ DataSource.prototype.columnNames = function (modelName) {
|
||||||
* @param modelName
|
* @param modelName
|
||||||
* @returns {String} columnName for ID
|
* @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 props = this.definitions[modelName].properties;
|
||||||
var self = this;
|
var self = this;
|
||||||
Object.keys(props).forEach(function(key) {
|
Object.keys(props).forEach(function(key) {
|
||||||
var columnInfo = props[key][self.adapter.name];
|
if(props[key].id) {
|
||||||
if(columnInfo && columnInfo.id) {
|
|
||||||
if(columnInfo.columnName) {
|
|
||||||
return columnInfo.columnName;
|
|
||||||
} else {
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if(props.id) {
|
|
||||||
return self.columnName(modelName, 'id');
|
|
||||||
}
|
|
||||||
return null;
|
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) {
|
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) {
|
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) {
|
BaseSQL.prototype.escapeName = function (name) {
|
||||||
|
@ -51,7 +74,8 @@ BaseSQL.prototype.defineProperty = function (model, prop, params) {
|
||||||
};
|
};
|
||||||
|
|
||||||
BaseSQL.prototype.save = function (model, data, callback) {
|
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) {
|
this.query(sql, function (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
|
@ -61,7 +85,7 @@ BaseSQL.prototype.save = function (model, data, callback) {
|
||||||
|
|
||||||
BaseSQL.prototype.exists = function (model, id, callback) {
|
BaseSQL.prototype.exists = function (model, id, callback) {
|
||||||
var sql = 'SELECT 1 FROM ' +
|
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) {
|
this.query(sql, function (err, data) {
|
||||||
if (err) return callback(err);
|
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'));
|
callback(new Error('id is not a number'));
|
||||||
}
|
}
|
||||||
var sql = 'SELECT * FROM ' +
|
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) {
|
this.query(sql, function (err, data) {
|
||||||
if (data && data.length === 1) {
|
if (data && data.length === 1) {
|
||||||
|
@ -118,7 +142,7 @@ BaseSQL.prototype.count = function count(model, callback, where) {
|
||||||
function buildWhere(conds) {
|
function buildWhere(conds) {
|
||||||
var cs = [];
|
var cs = [];
|
||||||
Object.keys(conds || {}).forEach(function (key) {
|
Object.keys(conds || {}).forEach(function (key) {
|
||||||
var keyEscaped = self.escapeName(key);
|
var keyEscaped = self.columnEscaped(key);
|
||||||
if (conds[key] === null) {
|
if (conds[key] === null) {
|
||||||
cs.push(keyEscaped + ' IS NULL');
|
cs.push(keyEscaped + ' IS NULL');
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue