dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

How to write multiple queries in the raw query? #566

Open cynosureabu opened 9 years ago

cynosureabu commented 9 years ago

Hi, I am trying to execute query like: set @result:=0;
update mytable
set status = case
when status != 'ended' then if(@result:=1,'ended', status)
else status end
where id = 49;
select @result;

I have to use the raw query here, but seems that I cannon call db.driver.execQuery with the above query? I received syntax error.

Now i am just using something like: db.driver.execQuery('set @result:=0; ', function(err) { if (err) { return cb(err); }

        db.driver.execQuery('update mytable   \
            set status = case   \
            when status != \'ended\' then if(@result:=1,\'ended\', status)  \
            else status   end  \
            where id = ' + id, function(err) {
            if (err) {
                callback(err, null);
            } else {
                db.driver.execQuery('select @result;', function(err, result) {
                    callback(null, result);
                });
            }
        });
    });

but its pretty ugly..

any help appreciated!

varsis commented 9 years ago

You could try setting the multipleStatements options in the connection string. See here: https://github.com/felixge/node-mysql Another option is use a library like async and execute in series.