DB\IO fix primary key return

Fixed table lookup if tables are in quotes (")
If no primary key name is set return empty.
If no primary key ID is returned, return null.
This commit is contained in:
Clemens Schwaighofer
2022-03-08 06:14:41 +09:00
parent cf49bf72fa
commit 3881a2a83d
2 changed files with 183 additions and 28 deletions

View File

@@ -878,11 +878,11 @@ class IO
{
$matches = [];
if (preg_match("/^SELECT /i", $query)) {
preg_match("/ (FROM) (([\w_]+)\.)?([\w_]+) /i", $query, $matches);
preg_match("/ (FROM) \"?(([\w_]+)\.)?([\w_]+)\"? /i", $query, $matches);
} else {
preg_match("/(INSERT INTO|DELETE FROM|UPDATE) (([\w_]+)\.)?([\w_]+) /i", $query, $matches);
preg_match("/(INSERT INTO|DELETE FROM|UPDATE) \"?(([\w_]+)\.)?([\w_]+)\"? /i", $query, $matches);
}
return [$matches[3], $matches[4]];
return [$matches[3] ?? '', $matches[4] ?? ''];
}
/**
@@ -1000,7 +1000,9 @@ class IO
if (!array_key_exists($table, $this->pk_name_table) || !$this->pk_name_table[$table]) {
$this->pk_name_table[$table] = $this->db_functions->__dbPrimaryKey($table, $schema);
}
$this->pk_name = $this->pk_name_table[$table] ? $this->pk_name_table[$table] : 'NULL';
$this->pk_name =
$this->pk_name_table[$table] ?
$this->pk_name_table[$table] : 'NULL';
}
if (
!preg_match("/ returning /i", $this->query) &&
@@ -2824,19 +2826,6 @@ class IO
// INTERNAL VARIABLES READ POST QUERY RUN
// ***************************
/**
* return current set insert_id as is
* @return array<mixed>|string|int|bool|null Primary key value, most likely int
* Array for multiple return set
* Empty string for unset
* Null for error
* @deprecated Use ->dbGetInsertPK();
*/
public function dbGetReturning()
{
return $this->dbGetInsertPK();
}
/**
* returns current set primary key name for last run query
* Is empty string if not setable
@@ -2845,7 +2834,7 @@ class IO
*/
public function dbGetInsertPKName(): string
{
return $this->insert_id_pk_name;
return $this->insert_id_pk_name ?? '';
}
/**
@@ -2857,6 +2846,9 @@ class IO
*/
public function dbGetInsertPK()
{
if (empty($this->insert_id_pk_name)) {
return null;
}
return $this->dbGetReturningExt($this->insert_id_pk_name);
}
@@ -3020,6 +3012,19 @@ class IO
// all call below are no longer in use and throw deprecated errors
// ***************************
/**
* return current set insert_id as is
* @return array<mixed>|string|int|bool|null Primary key value, most likely int
* Array for multiple return set
* Empty string for unset
* Null for error
* @deprecated Use ->dbGetInsertPK();
*/
public function dbGetReturning()
{
return $this->dbGetInsertPK();
}
/**
* returns the db init error
* if failed to connect it is set to true