Is there a way to run multiple statements/transaction/a sql script ? #2913
Replies: 20 comments
-
At protocol level it's "only one command at a time" and query command can contain multiple statements - see https://github.com/mysqljs/mysql#multiple-statement-queries |
Beta Was this translation helpful? Give feedback.
-
but the tricky thing is, that flag does not work when the delimiter change :-) |
Beta Was this translation helpful? Give feedback.
-
In your case it's one single statement, and you don't need to set delimiter. See example here - node-mysql2/test/integration/connection/test-binary-multiple-results.js Lines 100 to 104 in 0c422dd |
Beta Was this translation helpful? Give feedback.
-
But what if I read it from a sql script ? drop table if exists foo
create table foo (
......
drop procedure if exists bar;
delimiter $$
create procedure bar
(......
begin
-- a lot of statements --
end $$
delimiter ; |
Beta Was this translation helpful? Give feedback.
-
afaik you just use part without delimiter: conn.query('drop procedure if exists bar');
conn.query(`
create procedure bar
(......
begin
-- a lot of statements --
end
`); |
Beta Was this translation helpful? Give feedback.
-
I inherited code that does something like Promise.all( connection.query(), connection.query() ) The SQL statements don't do anything tricky like commit or rollback, so as long as connection.query() "locks" the connection until the result set is loaded into memory from the server, I guess this will work. The code is written this way because the Promise collection is generated by passing a callback to Array.map() that returns a Promise. Therefore, multiple connection.query()'s run at the same time, more or less. So far we have not seen any issues but is this supported usage? Is it advantageous any way to await connection.query(...); |
Beta Was this translation helpful? Give feedback.
-
no, they'll be executed sequentially anyway. There might be a benefit of doing await Promise.all([pool.query(...), pool.query(...)])
|
Beta Was this translation helpful? Give feedback.
-
Thanks! For our multiuser app, we need database connections to be available for incoming requests and therefore can't allow one request to hog connections not to mention the entire mysql server with a swarm of work. Also, for one request, there can only be one database transaction that is rolled back upon failure. Therefore we use synchronous SQL commands for individual API requests. Just giving mysql2 users something to think about when they stumble across this conversation. |
Beta Was this translation helpful? Give feedback.
-
I am at a loss. I have tried every which way to make this work but cant.
If I remove the DELIMITER statements then the ";" cause the connection to think there are multiple statements. If I remove the ";" then the statement isn't valid SQL. Any suggestions? |
Beta Was this translation helpful? Give feedback.
-
Creating stored procedures with mysql2 is atypical. Have you considered db-migrate? Be sure to omit DELIMITER statements in db-migrate scripts. This might be helpful: https://stackoverflow.com/questions/47369349/how-does-one-create-a-stored-procedure-in-node-js-using-the-mysql-library |
Beta Was this translation helpful? Give feedback.
-
If you are executing a script from file and have to stick to the sql script syntax, like what I did, you may need to handle delimiter lines and process the script manually. It is like, first split the script by ';', then search in each statement for delimiter lines, then replace the delimiter to ';' and remove delimiter line. |
Beta Was this translation helpful? Give feedback.
-
@terrisgit what do you mean by atypical? mysql2 is a relatively low level driver and only supports syntax that mysql server understands. I guess most confusion comes from differences that mysql cli adds by itself on the client before sending to the server |
Beta Was this translation helpful? Give feedback.
-
@sidorares I meant that creating a stored procedure as part of a one-time migration is orders of magnitude more common than creating them during normal application activity. That said, yes, you could implement that using mysql2. I think @drwharris needs to disable the multipleStatements option when connecting (and of course not using the DELIMITER statements). It's unclear how you are creating the connection. |
Beta Was this translation helpful? Give feedback.
-
I can set the statement to be as follows (removing the delimiter lines) but it then complains about the semi colons (It thinks its multi-statement?). If I remove the semi colons its complains about invalid SQL at the 2nd declare statement.
|
Beta Was this translation helpful? Give feedback.
-
Would it not be possible to have a "raw" option to just pass a statement to the DB and let it to its job without client side parsing, etc? |
Beta Was this translation helpful? Give feedback.
-
"without client side parsing" |
Beta Was this translation helpful? Give feedback.
-
OK I think i solved it. You need to terminate each line with a \r\n |
Beta Was this translation helpful? Give feedback.
-
@drwharris could you please provide minimal example? |
Beta Was this translation helpful? Give feedback.
-
4y on? One of my comments had exactly what I was trying to do. |
Beta Was this translation helpful? Give feedback.
-
Yes, this is still an issue. |
Beta Was this translation helpful? Give feedback.
-
I know according to the protocol mysql only accept one statement a time.
But is there a way to work around it ?
I tried
It usually works until I encountered sth. like
So, do I really need to find a way to handle this ?
Beta Was this translation helpful? Give feedback.
All reactions