miridoo-js-orm
Version:
miridoo javascript orm for database
99 lines (77 loc) • 2.75 kB
JavaScript
module.exports = function(){
this.tableName = '';
this.tableAliasName = '';
this.expr = [];
var self = this;
this.getRefName = function(arguments){
return (typeof arguments == 'string')? this.tableAliasName+'.'+arguments : Array.from(arguments).map(function(value, index){
if(typeof value != 'string')
return value;
else if(!(/[^a-zA-Z0-9_$]/.test(value)))
return self.tableAliasName+'.'+value;
return '"'+(value.replace(/^\+/, ''))+'"';
});
};
this.append = function (str) {
this.expr.push(str);
return this;
};
this.sum = function(colname){
return this.append('SUM('+this.getRefName(colname)+')');
};
this.count = function(colname){
return this.append('COUNT('+this.getRefName(colname)+')');
};
this.min = function(colname){
return this.append('MIN('+this.getRefName(colname)+')');
};
this.max = function(colname){
return this.append('MAX('+this.getRefName(colname)+')');
};
this.avg = function(){
return this.append('AVG('+this.getRefName(arguments).join('+')+')');
};
this.concat = function(){
return this.append('CONCAT('+this.getRefName(arguments).join(', ')+')');
};
this.toUpperCase = function(colname){
return this.append('UPPER('+this.getRefName(colname)+')');
};
this.toLowerCase = function(colname){
return this.append('LOWER('+this.getRefName(colname)+')');
};
this.month = function(colname){
return this.append('MONTH('+this.getRefName(colname)+')');
};
this.year = function(colname){
return this.append('YEAR('+this.getRefName(colname)+')');
};
this.day = function(colname){
return this.append('DAY('+this.getRefName(colname)+')');
};
this.dayCount = function(colname){
return this.append('TO_DAYS('+this.getRefName(colname)+')');
};
this.datediff = function(colname){
return this.append('DATEDIFF('+this.getRefName(colname)+')');
};
this.hour = function(){
return this.append('HOUR('+this.getRefName(colname)+')');
};
this.timediff = function(colname){
return this.append('TIMEDIFF('+this.getRefName(colname)+')');
};
this.now = function(){
return this.append('NOW()');
};
this.as = function(name){
this.expr[this.expr.length-1] += ' AS '+name;
return this;
};
this.toString = function(){
return this.expr.join(', ');
};
this.toArray = function(){
return this.expr;
};
};