improve primary key detection to use first column found if no primary key found
This commit is contained in:
@ -469,18 +469,30 @@ function wpsqli_rollback(&$connection, $flags = 0, $name = null)
|
|||||||
function get_primary_key_for_table(&$connection, $table)
|
function get_primary_key_for_table(&$connection, $table)
|
||||||
{
|
{
|
||||||
$query = <<<SQL
|
$query = <<<SQL
|
||||||
SELECT a.attname
|
SELECT a.attname, i.indisprimary
|
||||||
FROM pg_index i
|
FROM pg_index i
|
||||||
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
|
||||||
AND a.attnum = ANY(i.indkey)
|
|
||||||
WHERE i.indrelid = '$table'::regclass
|
WHERE i.indrelid = '$table'::regclass
|
||||||
AND i.indisprimary
|
|
||||||
SQL;
|
SQL;
|
||||||
|
|
||||||
$result = pg_query($connection, $query);
|
$result = pg_query($connection, $query);
|
||||||
|
if (!$result) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$row = pg_fetch_row($result);
|
$firstRow = null;
|
||||||
return $row[0];
|
while ($row = pg_fetch_row($result)) {
|
||||||
|
if ($firstRow === null) {
|
||||||
|
$firstRow = $row; // Save the first row in case no match is found
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($row[1] == true) {
|
||||||
|
return $row[0]; // Return the first row where $row[1] == true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no row where $row[1] == true was found, return the first row encountered
|
||||||
|
return $firstRow ? $firstRow[0] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user