diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2025-01-12 23:14:20 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2025-01-29 12:42:44 +0100 |
commit | 9f8248cc9ced2b724516e876a52a0b78fa37e802 (patch) | |
tree | 717d6bd44f7dcd4d9e2f68044666733e79f05f6d /include/dba | |
parent | 71e4326606c416cff077009f49a63d134c458557 (diff) | |
download | volse-hubzilla-9f8248cc9ced2b724516e876a52a0b78fa37e802.tar.gz volse-hubzilla-9f8248cc9ced2b724516e876a52a0b78fa37e802.tar.bz2 volse-hubzilla-9f8248cc9ced2b724516e876a52a0b78fa37e802.zip |
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.
Diffstat (limited to 'include/dba')
-rw-r--r-- | include/dba/dba_pdo.php | 15 |
1 files changed, 12 insertions, 3 deletions
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)) { // |