From 9f8248cc9ced2b724516e876a52a0b78fa37e802 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 12 Jan 2025 23:14:20 +0100 Subject: Use returning clause on dba_pdo::insert where supported By using the returning clause when inserting a new record, we get back the inserted record right away; thus saving us an extra roundtrip to the database. Both PostgreSQL and MariaDB supports this clause, but MySQL don't. In that case we fall back to manually fetching the last inserted row. --- include/dba/dba_pdo.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index b8b673727..7a1dfb48a 100644 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -146,10 +146,19 @@ class dba_pdo extends dba_driver { array_values($data) ); - $res = $this->q("INSERT INTO {$table} (" + $query = "INSERT INTO {$table} (" . implode(', ', $keys) . ') VALUES (' - . implode(', ', $values) . ')' - ); + . implode(', ', $values) . ')'; + + // MySQL is the only supported DB that don't support the returning + // clause. Since the driver type is 'mysql' also for MariaDB, we need + // to check the actual server version to be sure we only exclude actual + // MySQL systems. + if ($this->driver_dbtype !== 'mysql' || stripos($this->server_version, 'mariadb') !== false) { + $query .= ' RETURNING *'; + } + + $res = $this->q($query); if (is_a($res, PDOStatement::class)) { // -- cgit v1.2.3