One of the annoying limitations of MySQL/MariaDB stored procedures is the lack of a FOREACH construct, which loops on each row returned by a query.
In practice, this forces users to write a lot of code just to tell MySQL how to fetch rows and exit properly. Nesting 2 loops of this kind simply results in unmaintenable code (don’t trust me, just try).
Now, I’m writing a library of views and procedures that I’ll share as open source in the next days, and I decided to finally write my foreach. Well, sort of. It is impossible to use the current stored procedures language to write a flexible foreach, because to loop rows you need a cursor. And cursors are based on a hard-coded query. In this old post I proposed a verbose, ugly, but working solution, but it has a limitation: the number of columns returned by the query must still be fixed. So, I used that technique (based on views) in my procedure, but I had to write different procedures: foreach_1(), foreach_2(), foreach_3(). If you need to read more rows you can modify the code easily, but I think that for an open source library 3 columns is reasonable.
Here I decided to share an experimental prototype. If you have a better idea on how to achieve a similar result, I’ll be glad to trash this hack and use your idea instead.
Also note that for this procedure I used MariaDB 10.2 improved PREPARE statement. If you want to run it on MySQL or older MariaDB versions, you’ll have to make some easy changes and test the procedure.
CREATE PROCEDURE foreach_2(IN in_sql TEXT, IN in_callback_type VARCHAR(9), IN in_callback_body TEXT) NOT DETERMINISTIC READS SQL DATA BEGIN DECLARE p_sql TEXT DEFAULT NULL; DECLARE p_eof BOOL DEFAULT FALSE; DECLARE v_p1, v_p2 TEXT DEFAULT NULL; DECLARE crs_foreach CURSOR FOR SELECT p1, p2 FROM vw_foreach; DECLARE CONTINUE HANDLER FOR NOT FOUND BEGIN SET p_eof := TRUE; END; SET in_callback_type := UPPER(in_callback_type); IF NOT (in_callback_type IN ('SQL', 'PROCEDURE')) THEN SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = '[foreach_2] Invalid in_callback_type'; END IF; DO GET_LOCK('crs_foreach', 1); SET p_sql := CONCAT('CREATE OR REPLACE VIEW vw_foreach AS ', in_sql, ';'); PREPARE stmt_foreach_2 FROM p_sql; EXECUTE stmt_foreach_2; OPEN crs_foreach; DO RELEASE_LOCK('crs_foreach'); lp_while: WHILE TRUE DO FETCH crs_foreach INTO v_p1, v_p2; IF p_eof THEN LEAVE lp_while; END IF; IF in_callback_type IN ('SQL') THEN SET @p1 := v_p1; SET @p2 := v_p2; PREPARE stmt_foreach_2 FROM in_callback_body; ELSE PREPARE stmt_foreach_2 FROM CONCAT('CALL `', in_callback_body, '`(', QUOTE(v_p1), ', ', QUOTE(v_p2), ');'); END IF; EXECUTE stmt_foreach_2; END WHILE; CLOSE crs_foreach; DEALLOCATE PREPARE stmt_foreach_2; END
Now, the usage. There are 2 ways to use this procedure.
Using a callback procedure
First, create a callback procedure which will handle the rows returned by your query:
CREATE PROCEDURE p_echo(IN in_p1 TEXT, IN in_p2 TEXT) BEGIN SELECT CONCAT_WS('.', in_p1, in_p2) AS r; END
As you can see, the procedure must accept 2 values (foreach_2). The names don’t really matter, but p1 and p2 are clear, at least in this example.
Now, invoke foreach_2():
CALL foreach_2( 'SELECT TABLE_SCHEMA AS p1, TABLE_NAME AS p2 FROM information_schema.TABLES LIMIT 3;', 'PROCEDURE', 'p_echo' );
We passed the query producing the results we want to loop. Then we told foeach_2() that we want to use a callback procedure, and specified its name.
Using a callback SQL statement
Writing a procedure to perform a simple tasks would be annoying. That’s why I decided to also support callback statements:
CALL foreach_2( 'SELECT TABLE_SCHEMA AS p1, TABLE_NAME AS p2 FROM information_schema.TABLES LIMIT 3;', 'SQL', 'SELECT @p1 AS p1, @p2 AS p2;' );
As you can see, the callback query can access the values using user variables: @p1 and @p2.
These variables exist at session level, so it is possible that we are overwriting something. But I think there are no reasonable ways to avoid these collisions.