Tutorial

Prepared statements, sequences and RETURNING

Use prepared statements, the MariaDB-dialect sequences, OR REPLACE and RETURNING, and see how a driver's binary prepared statements differ from SQL-level PREPARE.

Prepared statements from a driver#

Every MySQL driver prepares statements through the binary protocol (COM_STMT_PREPARE, COM_STMT_EXECUTE). raftdb-mysql implements it for both dialects. Placeholders (?) are found by token, so a ? in a string, a comment or a name in backticks is not a placeholder; parameters are written into the statement as literals at the place of each ?, and a value can never change the structure of the statement (a test passes x'); DROP TABLE t; -- \' OR '1'='1 as a parameter and finds the table intact). Rows of a SELECT come back in the binary format.

A test program runs with a real JDBC driver (useServerPrepStmts=true) against MySQL 9.7 and Raft DB and compares the output: inserts with nine parameter types and generated keys, NULL parameters, batches, LIMIT ?, typed getters, IN (?, ?, ?), long data sent in pieces, and the errors of duplicate keys, NULL, too long values and bad syntax. Server-side cursors (COM_STMT_FETCH) are refused with error 1235; the driver reads the whole answer.

A session may hold at most 16,382 prepared statements (MySQL's default); the next one is error 1461.

SQL-level PREPARE (MariaDB dialect)#

Example:

SQL
DROP DATABASE IF EXISTS pr1;
CREATE DATABASE pr1;
USE pr1;
CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(10), qty INT);
PREPARE ins FROM 'INSERT INTO t VALUES (?, ?, ?)';
SET @a = 1, @b = 'one', @c = 10;
EXECUTE ins USING @a, @b, @c;
SET @a = 2, @b = 'two', @c = 20;
EXECUTE ins USING @a, @b, @c;
PREPARE sel FROM 'SELECT * FROM t WHERE qty > ? ORDER BY id';
SET @q = 5;
EXECUTE sel USING @q;
SET @sql = 'SELECT COUNT(*) AS n FROM t';
PREPARE cnt FROM @sql;
EXECUTE cnt;
DEALLOCATE PREPARE cnt;

EXECUTE sel USING @q with @q = 5 returns both rows. EXECUTE ins USING @a, @b with too few variables fails, as does EXECUTE of a name that was deallocated. The statement text may come from a user variable (PREPARE cnt FROM @sql). PREPARE, EXECUTE and DEALLOCATE PREPARE are verified in the MariaDB dialect only; for the MySQL dialect use the binary protocol (a driver) there.

Sequences (MariaDB dialect)#

Example:

SQL
CREATE SEQUENCE s1;
SELECT NEXTVAL(s1), NEXTVAL(s1), NEXT VALUE FOR s1;
SELECT LASTVAL(s1), PREVIOUS VALUE FOR s1;
SELECT * FROM s1;
SHOW CREATE SEQUENCE s1;
CREATE SEQUENCE s2 START WITH 100 INCREMENT BY 10 MINVALUE 50 MAXVALUE 140 CACHE 3 CYCLE;
SELECT NEXTVAL(s2), NEXTVAL(s2), NEXTVAL(s2), NEXTVAL(s2), NEXTVAL(s2), NEXTVAL(s2);
CREATE SEQUENCE s3 START WITH 5 INCREMENT BY -1 MINVALUE 1 MAXVALUE 5 NOCACHE NOCYCLE;
SELECT SETVAL(s1, 50), NEXTVAL(s1);

The first sequence answers 1, 2, 3 for the three calls; LASTVAL(s1) and PREVIOUS VALUE FOR s1 then give the last value this session took (3). s2 counts 100, 110, 120, 130, 140 and, because it is a CYCLE sequence, wraps to its minimum, 50. SETVAL(s1, 50) makes the next value 51. A sequence that has reached its limit without CYCLE gives an error (the script calls s3 six times).

The MySQL dialect has no sequences, as MySQL has none.

RETURNING and OR REPLACE (MariaDB dialect)#

Example:

SQL
CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10), qty INT DEFAULT 5, UNIQUE KEY un (name));
INSERT INTO t (name, qty) VALUES ('a', 1), ('b', 2), ('c', 3) RETURNING id, name;
INSERT INTO t (name) VALUES ('d') RETURNING *;
REPLACE INTO t (id, name, qty) VALUES (1, 'a', 100) RETURNING id, name, qty;
DELETE FROM t WHERE id IN (2, 3) RETURNING id, name;

CREATE OR REPLACE TABLE t (a INT, b INT);
CREATE OR REPLACE TABLE t2 AS SELECT 1 AS one, 'a' AS two;

INSERT ... RETURNING id, name answers a result set with the three new rows (1 a, 2 b, 3 c): you get the generated keys without a second query. CREATE OR REPLACE TABLE drops and re-creates the table in one statement.

What to do in the MySQL dialect for the same jobs#

  • Generated key: LAST_INSERT_ID() after the insert, or the driver's generated-keys API.
  • Replace a table: DROP TABLE IF EXISTS, then CREATE TABLE.

Where to go next#

The statement forms of every kind are in the SQL reference; operating a server is in Administration.