数据库
首页 > 数据库> > 记录一次node单创建的性能问题批量组装sql一次发送

记录一次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,一次,table,three,values,sql,let,stra,data
来源: https://blog.csdn.net/YangjulongTrue/article/details/120802186