Skip to content

Commit

Permalink
Fix: Use a binding for the table name when getting the primary key info
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed Sep 18, 2023
1 parent b609325 commit 8a9c3a7
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions Database/Driver/PostgresQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected function _prepare($sql)
$this->database()->debugInfo($sql, $this->_bindings);

$resource = $this->database()->resource();
$pkey = $this->pkey();

// Add a RETURNING command to postgres insert queries so we can get the
// pkey value from the query reliably
Expand All @@ -92,17 +93,14 @@ protected function _prepare($sql)

// Get the pkey field name
$pkRes = $resource->prepare(
"SELECT
pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = '{$table[0]}'::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary"
"SELECT a.attname
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = (:tableName)::regclass
AND i.indisprimary"
);
$pkRes->bindValue('tableName', $table[0]);
$pkRes->execute();
$row = $pkRes->fetch();

Expand Down

0 comments on commit 8a9c3a7

Please sign in to comment.