|
@@ -45,6 +45,16 @@ export class MysqlDB{
|
|
|
});
|
|
|
})
|
|
|
}
|
|
|
+ async count(tablename:string,whereObj?:any){
|
|
|
+ let whereRule="";
|
|
|
+ if(whereObj){
|
|
|
+ whereRule = ` where ${MysqlUtil.objToKVString(whereObj,' and ')}`;
|
|
|
+ }
|
|
|
+ const sqlStr = `select count(1) as total from ${tablename} ${whereRule}`;
|
|
|
+ const ret = await this.querySync(sqlStr) as MysqlResult;
|
|
|
+ if(ret.err || ret.data.length==0) return 0;
|
|
|
+ return ret.data[0].total;
|
|
|
+ }
|
|
|
async update(tablename:string,updateObj:any,whereObj:any){
|
|
|
if(!tablename || !updateObj) return;
|
|
|
let whereRule="";
|
|
@@ -62,7 +72,10 @@ export class MysqlDB{
|
|
|
return {err:err,data:null};
|
|
|
}
|
|
|
let queryStr = "*",whereStr = "";
|
|
|
- if(queryField) queryStr = queryField.join(",");
|
|
|
+ if(queryField){
|
|
|
+ queryField = queryField.map(v=>`\`${v}\``);
|
|
|
+ queryStr = queryField.join(",");
|
|
|
+ }
|
|
|
if(whereObj) whereStr = ` where ${MysqlUtil.objToKVString(whereObj,' and ')}`;
|
|
|
|
|
|
const sql = `select ${queryStr} from ${tablename} ${whereStr}`;
|
|
@@ -86,16 +99,19 @@ export class MysqlDB{
|
|
|
const fields = Object.keys(insertData[0]);
|
|
|
const values = [];
|
|
|
for(const data of insertData){
|
|
|
+ const rawData = JSON.parse(JSON.stringify(data));
|
|
|
const valueArr = [];
|
|
|
for(const key of fields){
|
|
|
- if(data[key] != null || data[key] != undefined){
|
|
|
- if(data[key].constructor == String){
|
|
|
- data[key] = `"${data[key]}"`;
|
|
|
- }else if(data[key].constructor == Number){
|
|
|
- data[key] = Number(data[key]);
|
|
|
+ if(rawData[key] != null || rawData[key] != undefined){
|
|
|
+ if(rawData[key].constructor == String){
|
|
|
+ rawData[key] = `"${rawData[key]}"`;
|
|
|
+ }else if(rawData[key].constructor == Number){
|
|
|
+ rawData[key] = Number(rawData[key]);
|
|
|
}
|
|
|
+ }else{
|
|
|
+ rawData[key] = null;
|
|
|
}
|
|
|
- valueArr.push(data[key]);
|
|
|
+ valueArr.push(rawData[key]);
|
|
|
}
|
|
|
values.push(`(${valueArr.join(',')})`);
|
|
|
}
|
|
@@ -117,20 +133,22 @@ export class MysqlUtil{
|
|
|
**/
|
|
|
public static objToKVString(obj:any,joinStr?:string,keyPrefix?:string){
|
|
|
const strArr:Array<string> = [];
|
|
|
-
|
|
|
- const keys = Object.keys(obj);
|
|
|
+ const dupObj = JSON.parse(JSON.stringify(obj));
|
|
|
+ const keys = Object.keys(dupObj);
|
|
|
let _extraSql = '',_extraLink = '';
|
|
|
keyPrefix = keyPrefix?`${keyPrefix}.`:'';
|
|
|
if(keys.length>0){
|
|
|
keys.forEach(key =>{
|
|
|
- let v = obj[key];
|
|
|
+ let v = dupObj[key];
|
|
|
if(key=='_extra'){
|
|
|
if(Array.isArray(v)){
|
|
|
_extraSql = v.join(joinStr);
|
|
|
}else if(v.constructor == Object){
|
|
|
if(v.sql){
|
|
|
_extraSql = v.sql;
|
|
|
- _extraLink = v.link||'and';
|
|
|
+ if(v.link!=null){
|
|
|
+ _extraLink = v.link||'and';
|
|
|
+ }
|
|
|
}
|
|
|
}else{
|
|
|
_extraSql = v;
|
|
@@ -193,7 +211,13 @@ export class MysqlUtil{
|
|
|
if(module[key].reverseKey && reverse) key = module[key].reverseKey;
|
|
|
outData[key] = value;
|
|
|
}else{
|
|
|
- if(!filter) outData[key] = data[key];
|
|
|
+ if(!filter){
|
|
|
+ if(data[key]==null){
|
|
|
+ outData[key] = String(data[key]);
|
|
|
+ }else{
|
|
|
+ outData[key] = data[key];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
return outData;
|