[eluser]Cyclops[/eluser]
Code:
/**
* js_database
*
* @author Michael Bahnmiller
* @author Ezekiel Messenger
*/
var js_database = function() {
this.records = new Array();
this.temprecords = new Array();
this.sortfield = '';
var me = this;
/**
* _select
*
* @param mixed criteria
*/
this._select = function(criteria)
{
var limit = criteria.limit;
var limit_start = (criteria.limit_start && typeof(criteria.limit_start) == 'number') ? criteria.limit_start : 0;
if ( typeof(criteria.records) == 'string' ){
if ( criteria.records != '*' )
return false;
if ( limit == 0 ){
return new Array();
}
this.__doSort(criteria);
if ( limit > 0 && limit < this.temprecords.length ) {
if ( limit_start > 0 )
this.temprecords.splice(0, limit_start);
this.temprecords.splice(limit, this.temprecords.length - limit);
return {"records" : this.temprecords, "count" : this.temprecords.length};
}
return {"records" : this.temprecords, "count" : this.temprecords.length};
}
var c = 0;
var d = 0;
var num_matches = 0;
var ret_data_Obj = {"records" : [], "count" : ""};
var len = 0;
if ( criteria.records) len += parseInt(criteria.records.length);
if ( criteria.records_gt) len += parseInt(criteria.records_gt.length);
if ( criteria.records_lt) len += parseInt(criteria.records_lt.length);
if ( criteria.records_ne) len += parseInt(criteria.records_ne.length);
this.__doSort(criteria);
for (i in this.temprecords){
c = 0;
for(j in criteria){
if ( j == 'records' || j == 'records_lt' || j == 'records_gt' || j == 'records_ne' ) {
for(k in criteria[j]) {
if ( j == 'records' ){
if ( this.temprecords[i][criteria.records[k].name] == criteria.records[k].value )
c++;
else break;
} else if ( j == 'records_lt' ){
if ( this.temprecords[i][criteria.records_lt[k].name] < criteria.records_lt[k].value )
c++;
else break;
} else if ( j == 'records_gt' ){
if ( this.temprecords[i][criteria.records_gt[k].name] > criteria.records_gt[k].value )
c++;
else break;
} else if ( j == 'records_ne' ){
if ( this.temprecords[i][criteria.records_ne[k].name] != criteria.records_ne[k].value )
c++;
else break;
}
if ( c == len ){
num_matches++;
if ( num_matches > limit_start ) {
d++;
ret_data_Obj.records.push(this.temprecords[i]);
}
if ( d == parseInt(limit) ){
ret_data_Obj.count = num_matches;
return ret_data_Obj;
}
}
}
}
}
}
ret_data_Obj.count = num_matches;
return ret_data_Obj;
};
/**
* _insert
*
* @param array rows
*/
this._insert = function(rows)
{
for(i in rows.records){
this.records[this.records.length] = rows.records[i];
}
};