Skip to content

Commit

Permalink
Updated Db-PostgreSQL to be PHP 8.1 compatibable
Browse files Browse the repository at this point in the history
  • Loading branch information
tinoest committed Jan 3, 2022
1 parent d9e5ddd commit 5eec56d
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions sources/database/Db-postgresql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ public function fetch_row($result, $counter = false)
return pg_fetch_row($result, $counter);

// Reset the row counter...
if (!isset($db_row_count[(int) $result]))
$db_row_count[(int) $result] = 0;
if (!isset($db_row_count[$this->seekCounter($result)]))
$db_row_count[$this->seekCounter($result)] = 0;

// Return the right row.
return @pg_fetch_row($result, $db_row_count[(int) $result]++);
return @pg_fetch_row($result, $db_row_count[$this->seekCounter($result)]++);
}

/**
Expand All @@ -408,6 +408,13 @@ public function fetch_row($result, $counter = false)
*/
public function free_result($result)
{
global $db_row_count;

$id = $this->seekCounter($result);
// Reset the row counter...
if (isset($db_row_count[$id]))
unset($db_row_count[$id]);

// Just delegate to the native function
pg_free_result($result);
}
Expand Down Expand Up @@ -443,7 +450,7 @@ public function data_seek($request, $counter)
{
global $db_row_count;

$db_row_count[(int) $request] = $counter;
$db_row_count[$this->seekCounter($request)] = $counter;

return true;
}
Expand Down Expand Up @@ -485,7 +492,7 @@ public function last_error($connection = null)
// Decide which connection to use
$connection = $connection === null ? $this->_connection : $connection;

if (is_resource($connection))
if (is_resource($connection) || $connection instanceof \PgSql\Connection)
return pg_last_error($connection);
}

Expand Down Expand Up @@ -987,7 +994,7 @@ protected function _replaceIdentifier($replacement)
*/
public function escape_string($string)
{
return pg_escape_string($string);
return pg_escape_string($this->_connection, $string);
}

/**
Expand All @@ -1004,11 +1011,11 @@ public function fetch_assoc($request, $counter = false)
return pg_fetch_assoc($request, $counter);

// Reset the row counter...
if (!isset($db_row_count[(int) $request]))
$db_row_count[(int) $request] = 0;
if (!isset($db_row_count[$this->seekCounter($request)]))
$db_row_count[$this->seekCounter($request)] = 0;

// Return the right row.
return @pg_fetch_assoc($request, $db_row_count[(int) $request]++);
return @pg_fetch_assoc($request, $db_row_count[$this->seekCounter($request)]++);
}

/**
Expand Down Expand Up @@ -1064,6 +1071,20 @@ public static function db()
*/
public function validConnection($connection = null)
{
return is_resource($connection);
return (is_resource($connection) || $connection instanceof \PgSql\Connection);
}

public function seekCounter($resource)
{
global $db_row_count;
static $prev_id = null;

if(!is_resource($resource) && ($resource instanceof \PgSql\Result)) {
$id = spl_object_id($resource);
} else {
$id = (int)$resource;
}

return $id;
}
}

0 comments on commit 5eec56d

Please sign in to comment.