nuintun / node-adodb

A node.js javascript client implementing the ADODB protocol on windows.
https://nuintun.github.io/node-adodb
MIT License
185 stars 51 forks source link

node-adodb variable multiple queries #58

Closed Falym closed 5 years ago

Falym commented 6 years ago

Hello Nuintun,

How to do to execute multiple queries with nested query.

I've try this code :

var produits_test = [{id:1,ref:'40'},{id:2,ref:'32'},{id:15,ref:'29'},{id:17,ref:'152'}];
for(var i=0;i<produits_test.length;i++){
    var sql = "update t_produits_test set ref='"+produits_test[i].ref+"' where id_produit = "+produits_test[i].id;
    var produits_temp=produits_test[i];
    console.log(produits_test[i]);//OK
    console.log(sql);//OK
connection
    .execute(sql)
    .then(function(){
        console.log(produits_test[i]);//Undefined
        console.log(produits_temp);//always the last item {id:17,ref:'152'} executed
        sql = "insert into log(id, ref) values ("+produits_temp.id+",'"+produits_temp.ref+"')";
        console.log(sql);
    connection
        .execute(sql)
        .then(function(){
            //
        })
        .catch(error => {
            displayWarning(error);
        });
    })
    .catch(error => {
        displayWarning(error);
    });
}

I can't execute each item prefectely.

Could you have an idea ? By the way, is there a solution to execute my queries with synchronous mode ?

Thanks.

Falym commented 6 years ago

I have to process separately my queries like this :

var produits_test = [{id:1,ref:'40'},{id:2,ref:'32'},{id:15,ref:'29'},{id:17,ref:'152'}];
for(var i=0;i<produits_test.length;i++){
    var sql = "update t_produits_test set ref='"+produits_test[i].ref+"' where id_produit = "+produits_test[i].id;
connection
    .execute(sql)
    .then(function(){
    })
    .catch(error => {
        displayWarning(error);
    });
}
for(var i=0;i<produits_test.length;i++){
    var sql = "insert into log(id, ref) values ("+produits_test[i].id+",'"+produits_test[i].ref+"')";
    console.log(sql);//OK
connection
    .execute(sql)
    .then(function(){
    })
    .catch(error => {
        displayWarning(error);
    });
}

But if I want my 2nd query depends on value returned by the first query, this issue isn't the good solution.

Could you help me please ?

Best regards,

nuintun commented 6 years ago
const produits = [
  { id: 1, ref: '40' },
  { id: 2, ref: '32' },
  { id: 15, ref: '29' },
  { id: 17, ref: '152' }
];

async function multiple() {
  for (let i = 0, length = produits.length; i < length; i++) {
    const { ref, id } = produits[i];
    const sql = `update t_produits_test set ref="${ref}" where id_produit=${id}`;

    await connection
      .execute(sql)
      .then(console.log)
      .catch(error => displayWarning(error));
  }
}

multiple();