This commit is contained in:
Raymond Feng 2013-05-23 22:40:33 -07:00
parent dea07f3a02
commit 98690a4881
1 changed files with 58 additions and 0 deletions

View File

@ -21,6 +21,11 @@ BaseSQL.prototype.queryOne = function (sql, callback) {
}); });
}; };
/**
* Look up the data source by model name
* @param model The model name
* @returns {DataSource} The data source
*/
BaseSQL.prototype.dataSource = function(model) { BaseSQL.prototype.dataSource = function(model) {
var m = this._models[model]; var m = this._models[model];
if(!m) { if(!m) {
@ -29,6 +34,11 @@ BaseSQL.prototype.dataSource = function(model) {
return m.model.schema; return m.model.schema;
} }
/**
* Get the table name for a given model
* @param model The model name
* @returns {String} The table name
*/
BaseSQL.prototype.table = function (model) { BaseSQL.prototype.table = function (model) {
var name = this.dataSource(model).tableName(model); var name = this.dataSource(model).tableName(model);
var dbName = this.dbName; var dbName = this.dbName;
@ -38,6 +48,12 @@ BaseSQL.prototype.table = function (model) {
return name; return name;
}; };
/**
* Get the column name for given model property
* @param model The model name
* @param property The property name
* @returns {String} The column name
*/
BaseSQL.prototype.column = function (model, property) { BaseSQL.prototype.column = function (model, property) {
var name = this.dataSource(model).columnName(model, property); var name = this.dataSource(model).columnName(model, property);
var dbName = this.dbName; var dbName = this.dbName;
@ -47,6 +63,12 @@ BaseSQL.prototype.column = function (model, property) {
return name; return name;
}; };
/**
* Get the corresponding property name for a given column name
* @param model The model name
* @param column The column name
* @returns {*}
*/
BaseSQL.prototype.propertyName = function (model, column) { BaseSQL.prototype.propertyName = function (model, column) {
var props = this._models[model].properties; var props = this._models[model].properties;
for(var p in props) { for(var p in props) {
@ -57,10 +79,20 @@ BaseSQL.prototype.propertyName = function (model, column) {
return null; return null;
}; };
/**
* Get the id property name
* @param model The model name
* @returns {String} The id property name
*/
BaseSQL.prototype.idName = function (model) { BaseSQL.prototype.idName = function (model) {
return this.dataSource(model).idName(model); return this.dataSource(model).idName(model);
}; };
/**
* Get the id column name
* @param model The model name
* @returns {String} The column name
*/
BaseSQL.prototype.idColumn = function (model) { BaseSQL.prototype.idColumn = function (model) {
var name = this.dataSource(model).idColumnName(model);; var name = this.dataSource(model).idColumnName(model);;
var dbName = this.dbName; var dbName = this.dbName;
@ -70,10 +102,21 @@ BaseSQL.prototype.idColumn = function (model) {
return name; return name;
}; };
/**
* Get the escaped id column name
* @param model The model name
* @returns {String} the escaped id column name
*/
BaseSQL.prototype.idColumnEscaped = function (model) { BaseSQL.prototype.idColumnEscaped = function (model) {
return this.escapeName(this.dataSource(model).idColumnName(model)); return this.escapeName(this.dataSource(model).idColumnName(model));
}; };
/**
* Get the id index (sequence number, starting from 1)
* @param model The model name
* @param prop The property name
* @returns {Number} The id index, undefined if the property is not part of the primary key
*/
BaseSQL.prototype.id = function (model, prop) { BaseSQL.prototype.id = function (model, prop) {
var p = this._models[model].properties[prop]; var p = this._models[model].properties[prop];
if(!p) { if(!p) {
@ -82,14 +125,29 @@ BaseSQL.prototype.id = function (model, prop) {
return p.id; return p.id;
}; };
/**
* Escape the name for the underlying database
* @param name The name
*/
BaseSQL.prototype.escapeName = function (name) { BaseSQL.prototype.escapeName = function (name) {
throw new Error('escapeName method should be declared in adapter'); throw new Error('escapeName method should be declared in adapter');
}; };
/**
* Get the escaped table name
* @param model The model name
* @returns {String} the escaped table name
*/
BaseSQL.prototype.tableEscaped = function (model) { BaseSQL.prototype.tableEscaped = function (model) {
return this.escapeName(this.table(model)); return this.escapeName(this.table(model));
}; };
/**
* Get the escaped column name for a given model property
* @param model The model name
* @param property The property name
* @returns {String} The escaped column name
*/
BaseSQL.prototype.columnEscaped = function (model, property) { BaseSQL.prototype.columnEscaped = function (model, property) {
return this.escapeName(this.column(model, property)); return this.escapeName(this.column(model, property));
}; };