Classify SQL errors in a Starlark procedure¶
Send the procedure body as one complete DDL statement; the client must not split it on internal lines or semicolons. First run the source error classification test to check mo.errno() and mo.sqlstate(). This minimal procedure intentionally causes a duplicate key:
CREATE DATABASE cookbook_starlark;
CREATE TABLE cookbook_starlark.items(id INT PRIMARY KEY);
INSERT INTO cookbook_starlark.items VALUES(1);
CREATE PROCEDURE cookbook_starlark.classify(out code INT) LANGUAGE 'starlark'
$$
rows, err = mo.sql("insert into cookbook_starlark.items values(1)")
out_code = mo.errno() if err != None else 0
$$;
SET @code = 0;
CALL cookbook_starlark.classify(@code);
SELECT @code AS mysql_error_code;
SELECT COUNT(*) AS row_count FROM cookbook_starlark.items;
DROP DATABASE cookbook_starlark;
Expect mysql_error_code=1062 and row_count=1. For scheduled work, invoke a retry-safe business procedure from a SQL Task. A task retry does not by itself make the whole multi-statement task body atomic.