记录一次node单创建的性能问题批量组装sql一次发送

2021/10/16 19:09:30

本文主要是介绍记录一次node单创建的性能问题批量组装sql一次发送,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 DataHp.myBatchCreate = async (sql,values) => {
    return new Promise(function (resolve, reject) {
      DataHp.dataSource.connector.query(sql, function (err, data) {
        if (err) {
          reject(new FunctionError("QUERY_ERROR", "查询数据失败!"));
        }
        resolve(data);
      })
    })
  }
  function FindInsetSql(table,data,values){
    //INSERT INTO xx(1,2,3,4) VALUES (123,2,3,4),(2,3,4,5,6,)
    let str=`INSERT INTO ${table}(`
    let stra="";
    if(data==null){
      data=values[0];
    }
    for(let k in data){
      stra+=k+",";
    }
    stra= stra.substring(0,stra.length-1);
    str+=stra;
    str+=") VALUES ";
    let three="";
    for(let i=0;i<values.length;i++){
      let a="(";

      for(let k in data){
        let aaa=values[i][k];

        if(aaa){
          a+="'"+aaa+"',";
        }else{
          a+=aaa+",";
        }
      }
      a= a.substring(0,a.length-1);
      a+="),";
      three+=a;
    }
    three= three.substring(0,three.length-1);
    str+=three;
    return str;
  }
  DataHp.BatchCreate = async (table,data,values) => {
    if(values&&values.length>0){
      // console.log(values.length+"\t"+table);
      return  await DataHp.myBatchCreate( FindInsetSql(table,data,values));
    }
  }

调用

      await DataHp.BatchCreate("report",reportInputs[0],reportInputs);


这篇关于记录一次node单创建的性能问题批量组装sql一次发送的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程