// 格式化字符串
String.prototype.format = function(args){
var result = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp ("({["+i+"]})","g");
result = result.replace(reg, arguments[i]);
}
return result;
}
var db = (function(){
var _sql;
return{
insert:function(TableName, fields){
var ArrFieldsName = [];
var ArrFieldsValue = [];
for(var v in fields){
if(typeof fields[v] !== 'number')
ArrFieldsValue.push("'{0}'".format(fields[v]));
else
ArrFieldsValue.push(fields[v]);
ArrFieldsName.push(v);
}
ArrFieldsName.reverse();
ArrFieldsValue.reverse();
_sql = "insert into {0} ({1}) values ({2})".format(TableName,
ArrFieldsName.join(","),ArrFieldsValue.join(","));
},
get:function(){
return _sql;
},
update:function(TableName, fields, condition){
var ArrFields = [];
var ArrCondition = [];
for(var v in fields){
if(typeof fields[v] !== 'number')
ArrFields.push("{0}='{1}'".format(v, fields[v]));
else
ArrFields.push("{0}={1}".format(v, fields[v]));
}
for(var v1 in condition){
if(typeof fields[v] !== 'number')
ArrCondition.push("{0}='{1}'".format(v1, condition[v1]));
else
ArrCondition.push("{0}={1}".format(v1, condition[v1]));
}
ArrFields.reverse();
ArrCondition.reverse();
_sql = "UPDATE {0} SET {1} WHERE {2}".format(TableName,
ArrFields.join(","), ArrCondition.join(" AND "));
},
delete:function(TableName, condition){
var ArrCondition = [];
for(var v in condition){
if(typeof condition[v] !== 'number')
ArrCondition.push("{0}='{1}'".format(v, condition[v]));
else
ArrCondition.push("{0}={1}".format(v, condition[v]));
}
_sql = "DELETE FROM {0} WHERE {1}".format(TableName, ArrCondition.join(" AND "));
}
};
})();