DB\IO move db related queries to db functions and add interface class
A new interface class for DB\SQL\* functions Also moved all PostgreSQL related SHOW/SET code to the DB\SQL\* functions class.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -52,7 +52,8 @@ print "DBINFO: " . $db->dbInfo() . "<br>";
|
|||||||
echo "DB_CONFIG_SET constant: <pre>" . print_r(DB_CONFIG, true) . "</pre><br>";
|
echo "DB_CONFIG_SET constant: <pre>" . print_r(DB_CONFIG, true) . "</pre><br>";
|
||||||
|
|
||||||
// DB client encoding
|
// DB client encoding
|
||||||
print "DB Client encoding: " . $db->dbGetEncoding() . "<br>";
|
print "DB client encoding: " . $db->dbGetEncoding() . "<br>";
|
||||||
|
print "DB search path: " . $db->dbGetSchema() . "<br>";
|
||||||
|
|
||||||
while (is_array($res = $db->dbReturn("SELECT * FROM max_test", DbIo::USE_CACHE, true))) {
|
while (is_array($res = $db->dbReturn("SELECT * FROM max_test", DbIo::USE_CACHE, true))) {
|
||||||
print "UUD/TIME: " . $res['uid'] . "/" . $res['time'] . "<br>";
|
print "UUD/TIME: " . $res['uid'] . "/" . $res['time'] . "<br>";
|
||||||
@@ -304,13 +305,14 @@ dbShowTableMetaData
|
|||||||
|
|
||||||
# async test queries
|
# async test queries
|
||||||
/*
|
/*
|
||||||
$db->dbExecAsync("SELECT test FROM test_foo, (SELECT pg_sleep(10)) as sub WHERE test_foo_id IN (27, 50, 67, 44, 10)");
|
$db->dbExecAsync(
|
||||||
|
"SELECT test FROM test_foo, (SELECT pg_sleep(10)) as sub "
|
||||||
|
. "WHERE test_foo_id IN (27, 50, 67, 44, 10)"
|
||||||
|
);
|
||||||
echo "WAITING FOR ASYNC: ";
|
echo "WAITING FOR ASYNC: ";
|
||||||
$chars = ['|', '/', '-', '\\'];
|
$chars = ['|', '/', '-', '\\'];
|
||||||
while (($ret = $db->dbCheckAsync()) === true)
|
while (($ret = $db->dbCheckAsync()) === true) {
|
||||||
{
|
if ((list($_, $char) = each($chars)) === FALSE) {
|
||||||
if ((list($_, $char) = each($chars)) === FALSE)
|
|
||||||
{
|
|
||||||
reset($chars);
|
reset($chars);
|
||||||
list($_, $char) = each($chars);
|
list($_, $char) = each($chars);
|
||||||
}
|
}
|
||||||
@@ -320,15 +322,13 @@ while (($ret = $db->dbCheckAsync()) === true)
|
|||||||
}
|
}
|
||||||
print "<br>END STATUS: " . $ret . "<br>";
|
print "<br>END STATUS: " . $ret . "<br>";
|
||||||
// while ($res = $db->dbFetchArray($ret))
|
// while ($res = $db->dbFetchArray($ret))
|
||||||
while ($res = $db->dbFetchArray())
|
while ($res = $db->dbFetchArray()) {
|
||||||
{
|
|
||||||
echo "RES: " . $res['test'] . "<br>";
|
echo "RES: " . $res['test'] . "<br>";
|
||||||
}
|
}
|
||||||
# test async insert
|
# test async insert
|
||||||
$db->dbExecAsync("INSERT INTO test_foo (Test) VALUES ('ASYNC TEST " . time() . "')");
|
$db->dbExecAsync("INSERT INTO test_foo (Test) VALUES ('ASYNC TEST " . time() . "')");
|
||||||
echo "WAITING FOR ASYNC INSERT: ";
|
echo "WAITING FOR ASYNC INSERT: ";
|
||||||
while (($ret = $db->dbCheckAsync()) === true)
|
while (($ret = $db->dbCheckAsync()) === true) {
|
||||||
{
|
|
||||||
print " . ";
|
print " . ";
|
||||||
sleep(1);
|
sleep(1);
|
||||||
flush();
|
flush();
|
||||||
|
|||||||
@@ -828,7 +828,7 @@ class IO
|
|||||||
// only do if array, else pass through row (can be false)
|
// only do if array, else pass through row (can be false)
|
||||||
if (
|
if (
|
||||||
!is_array($row) ||
|
!is_array($row) ||
|
||||||
empty($this->to_encoding) || empty($this->db_encoding)
|
empty($this->to_encoding)// || empty($this->db_encoding)
|
||||||
) {
|
) {
|
||||||
return $row;
|
return $row;
|
||||||
}
|
}
|
||||||
@@ -841,7 +841,11 @@ class IO
|
|||||||
$from_encoding != $this->to_encoding &&
|
$from_encoding != $this->to_encoding &&
|
||||||
$from_encoding != 'ASCII'
|
$from_encoding != 'ASCII'
|
||||||
) {
|
) {
|
||||||
$row[$key] = mb_convert_encoding($value, $this->to_encoding, $from_encoding);
|
$row[$key] = mb_convert_encoding(
|
||||||
|
$value,
|
||||||
|
$this->to_encoding,
|
||||||
|
$from_encoding
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $row;
|
return $row;
|
||||||
@@ -1216,6 +1220,9 @@ class IO
|
|||||||
public function dbClose(): void
|
public function dbClose(): void
|
||||||
{
|
{
|
||||||
if ($this->dbh) {
|
if ($this->dbh) {
|
||||||
|
// reset any client encodings set
|
||||||
|
$this->dbResetEncoding();
|
||||||
|
// calls db close
|
||||||
$this->db_functions->__dbClose();
|
$this->db_functions->__dbClose();
|
||||||
$this->dbh = null;
|
$this->dbh = null;
|
||||||
$this->db_connection_closed = true;
|
$this->db_connection_closed = true;
|
||||||
@@ -2365,6 +2372,15 @@ class IO
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current async running query hash
|
||||||
|
* @return string Current async running query hash
|
||||||
|
*/
|
||||||
|
public function dbGetAsyncRunning(): string
|
||||||
|
{
|
||||||
|
return $this->async_running;
|
||||||
|
}
|
||||||
|
|
||||||
// ***************************
|
// ***************************
|
||||||
// COMPLEX WRITE WITH CONFIG ARRAYS
|
// COMPLEX WRITE WITH CONFIG ARRAYS
|
||||||
// ***************************
|
// ***************************
|
||||||
@@ -2603,6 +2619,7 @@ class IO
|
|||||||
*/
|
*/
|
||||||
public function dbSetMaxQueryCall(?int $max_calls = null): bool
|
public function dbSetMaxQueryCall(?int $max_calls = null): bool
|
||||||
{
|
{
|
||||||
|
$this->__dbErrorReset();
|
||||||
// if null then reset to default
|
// if null then reset to default
|
||||||
if ($max_calls === null) {
|
if ($max_calls === null) {
|
||||||
$max_calls = self::DEFAULT_MAX_QUERY_CALL;
|
$max_calls = self::DEFAULT_MAX_QUERY_CALL;
|
||||||
@@ -2640,25 +2657,29 @@ class IO
|
|||||||
*/
|
*/
|
||||||
public function dbSetSchema(string $db_schema)
|
public function dbSetSchema(string $db_schema)
|
||||||
{
|
{
|
||||||
|
$this->__dbErrorReset();
|
||||||
if (empty($db_schema)) {
|
if (empty($db_schema)) {
|
||||||
$this->__dbError(70);
|
$this->__dbError(70);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// check if schema exists
|
$status = false;
|
||||||
$q = "SELECT EXISTS("
|
$set_value = $this->db_functions->__dbSetSchema($db_schema);
|
||||||
. "SELECT 1 FROM information_schema.schemata "
|
switch ($set_value) {
|
||||||
. "WHERE schema_name = " . $this->dbEscapeLiteral($db_schema)
|
// no problem
|
||||||
. ")";
|
case 0:
|
||||||
$row = $this->dbReturnRow($q);
|
$this->db_schema = $db_schema;
|
||||||
if (!$this->dbBoolean($row['exists'])) {
|
$status = true;
|
||||||
$this->__dbError(71);
|
break;
|
||||||
return false;
|
// cursor failed (1) or schema does not exists (2)
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
// setting schema failed (3)
|
||||||
|
case 3:
|
||||||
|
$this->__dbError(71);
|
||||||
|
$status = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
// set new schema
|
return $status;
|
||||||
$q = "SET search_path TO '" . $this->dbEscapeString($db_schema) . "'";
|
|
||||||
$this->dbExec($q);
|
|
||||||
$this->db_schema = $db_schema;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2673,11 +2694,7 @@ class IO
|
|||||||
if ($class_var === true) {
|
if ($class_var === true) {
|
||||||
return $this->db_schema;
|
return $this->db_schema;
|
||||||
}
|
}
|
||||||
$search_path = $this->dbReturnRow('SHOW search_path');
|
return $this->db_functions->__dbGetSchema();
|
||||||
if (!is_array($search_path)) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
return $search_path['search_path'] ?? '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2688,32 +2705,31 @@ class IO
|
|||||||
*/
|
*/
|
||||||
public function dbSetEncoding(string $db_encoding): bool
|
public function dbSetEncoding(string $db_encoding): bool
|
||||||
{
|
{
|
||||||
|
$this->__dbErrorReset();
|
||||||
|
// no encding, abort
|
||||||
if (empty($db_encoding)) {
|
if (empty($db_encoding)) {
|
||||||
$this->__dbError(80);
|
$this->__dbError(80);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// check if ecnoding is valid first
|
// set client encoding on database side
|
||||||
// does not take into account aliases
|
$status = false;
|
||||||
// TODO lookup with alisaes so eg ShiftJIS does not get a false
|
$set_value = $this->db_functions->__dbSetEncoding($db_encoding);
|
||||||
// $q = "SELECT EXISTS("
|
switch ($set_value) {
|
||||||
// . "SELECT pg_catalog.pg_encoding_to_char(conforencoding) "
|
// no problem
|
||||||
// . "FROM pg_catalog.pg_conversion "
|
case 0:
|
||||||
// . "WHERE pg_catalog.pg_encoding_to_char(conforencoding) = "
|
$this->db_encoding = $db_encoding;
|
||||||
// . $this->dbEscapeLiteral($db_encoding)
|
$status = true;
|
||||||
// . ")";
|
break;
|
||||||
// $row = $this->dbReturnRow($q);
|
// 1 & 2 are for encoding not found
|
||||||
// if (!$this->dbBoolean($row['exists'])) {
|
case 1:
|
||||||
// $this->__dbError(81);
|
case 2:
|
||||||
// return false;
|
// 3 is set failed
|
||||||
// }
|
case 3:
|
||||||
$q = "SET client_encoding TO '" . $this->dbEscapeString($db_encoding) . "'";
|
$this->__dbError(81);
|
||||||
// abort on error
|
$status = false;
|
||||||
if ($this->dbExec($q) === false) {
|
break;
|
||||||
$this->__dbError(81);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
$this->db_encoding = $db_encoding;
|
return $status;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2727,17 +2743,23 @@ class IO
|
|||||||
if ($class_var === true) {
|
if ($class_var === true) {
|
||||||
return $this->db_encoding;
|
return $this->db_encoding;
|
||||||
}
|
}
|
||||||
$client_encoding = $this->dbReturnRow('SHOW client_encoding');
|
return $this->db_functions->__dbGetEncoding();
|
||||||
if (!is_array($client_encoding)) {
|
}
|
||||||
return '';
|
|
||||||
}
|
/**
|
||||||
return $client_encoding['client_encoding'] ?? '';
|
* Resets client encodig back to databse encoding
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function dbResetEncoding(): void
|
||||||
|
{
|
||||||
|
$this->dbExec('RESET client_encoding');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the to_encoding var that will trigger on return of selected data
|
* Set the to_encoding var that will trigger on return of selected data
|
||||||
* on the fly PHP encoding conversion
|
* on the fly PHP encoding conversion
|
||||||
* Alternative use dbSetEcnoding to trigger encoding change on the DB side
|
* Alternative use dbSetEcnoding to trigger encoding change on the DB side
|
||||||
|
* Set to empty string to turn off
|
||||||
* @param string $encoding PHP Valid encoding to set
|
* @param string $encoding PHP Valid encoding to set
|
||||||
* @return string Current set encoding
|
* @return string Current set encoding
|
||||||
*/
|
*/
|
||||||
|
|||||||
297
www/lib/CoreLibs/DB/SQL/Interface/SqlFunctions.php
Normal file
297
www/lib/CoreLibs/DB/SQL/Interface/SqlFunctions.php
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intrface for all SQL\* functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\DB\SQL\Interface;
|
||||||
|
|
||||||
|
interface SqlFunctions
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __dbLastErrorQuery(): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbQuery(string $query);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
* @param array<mixed> $params
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbQueryParams(string $query, array $params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function __dbSendQuery(string $query): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbGetResult();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __dbClose(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param string $query
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbPrepare(string $name, string $query);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param array<mixed> $data
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbExecute(string $name, array $data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbNumRows($cursor): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbNumFields($cursor): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @param int $i
|
||||||
|
* @return string|bool
|
||||||
|
*/
|
||||||
|
public function __dbFieldName($cursor, int $i);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @param int $result_type
|
||||||
|
* @return array<mixed>|bool
|
||||||
|
*/
|
||||||
|
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param boolean $assoc_type
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbResultType(bool $assoc_type = true): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @return array<mixed>|bool
|
||||||
|
*/
|
||||||
|
public function __dbFetchAll($cursor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbAffectedRows($cursor): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
* @param string|null $pk_name
|
||||||
|
* @return string|integer|false
|
||||||
|
*/
|
||||||
|
public function __dbInsertId(string $query, ?string $pk_name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $table
|
||||||
|
* @param string $schema
|
||||||
|
* @return string|bool
|
||||||
|
*/
|
||||||
|
public function __dbPrimaryKey(string $table, string $schema = '');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $db_host
|
||||||
|
* @param string $db_user
|
||||||
|
* @param string $db_pass
|
||||||
|
* @param string $db_name
|
||||||
|
* @param integer $db_port
|
||||||
|
* @param string $db_ssl
|
||||||
|
* @return object|resource|bool
|
||||||
|
*/
|
||||||
|
public function __dbConnect(
|
||||||
|
string $db_host,
|
||||||
|
string $db_user,
|
||||||
|
string $db_pass,
|
||||||
|
string $db_name,
|
||||||
|
int $db_port,
|
||||||
|
string $db_ssl = 'allow'
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param object|resource|bool $cursor
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbPrintError($cursor = false): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $table
|
||||||
|
* @param boolean $extended
|
||||||
|
* @return array<mixed>|bool
|
||||||
|
*/
|
||||||
|
public function __dbMetaData(string $table, $extended = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string|int|float|bool $string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbEscapeString($string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string|int|float|bool $string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbEscapeLiteral($string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbEscapeIdentifier(string $string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $bytea
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbEscapeBytea(string $bytea): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function __dbConnectionBusy(): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param integer $timeout_seconds
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbVersion(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $array_text
|
||||||
|
* @param integer $start
|
||||||
|
* @param integer|null $end
|
||||||
|
* @return array<mixed>|null
|
||||||
|
*/
|
||||||
|
public function __dbArrayParse(
|
||||||
|
string $array_text,
|
||||||
|
int $start = 0,
|
||||||
|
?int &$end = null
|
||||||
|
): ?array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $show_string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbShow(string $show_string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $db_schema
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbSetSchema(string $db_schema): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbGetSchema(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $db_encoding
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function __dbSetEncoding(string $db_encoding): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __dbGetEncoding(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -51,7 +51,7 @@ namespace CoreLibs\DB\SQL;
|
|||||||
// as main system. Currently all @var sets are written as object
|
// as main system. Currently all @var sets are written as object
|
||||||
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
|
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
|
||||||
|
|
||||||
class PgSQL
|
class PgSQL implements Interface\SqlFunctions
|
||||||
{
|
{
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $last_error_query;
|
private $last_error_query;
|
||||||
@@ -62,15 +62,15 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* class constructor, empty does nothing
|
* class constructor, empty does nothing
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
// public function __construct()
|
||||||
{
|
// {
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* queries last error query and returns true or false if error was set
|
* queries last error query and returns true or false if error was set
|
||||||
* @return bool true/false if last error is set
|
* @return bool true/false if last error is set
|
||||||
*/
|
*/
|
||||||
public function __dbLastErrorQuery()
|
public function __dbLastErrorQuery(): bool
|
||||||
{
|
{
|
||||||
if ($this->last_error_query) {
|
if ($this->last_error_query) {
|
||||||
return true;
|
return true;
|
||||||
@@ -210,7 +210,7 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* wrapper for pg_num_rows
|
* wrapper for pg_num_rows
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @return int number of rows, -1 on error
|
* @return int number of rows, -1 on error
|
||||||
*/
|
*/
|
||||||
public function __dbNumRows($cursor): int
|
public function __dbNumRows($cursor): int
|
||||||
{
|
{
|
||||||
@@ -223,7 +223,7 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* wrapper for pg_num_fields
|
* wrapper for pg_num_fields
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @return int number for fields in result, -1 on error
|
* @return int number for fields in result, -1 on error
|
||||||
*/
|
*/
|
||||||
public function __dbNumFields($cursor): int
|
public function __dbNumFields($cursor): int
|
||||||
{
|
{
|
||||||
@@ -235,11 +235,11 @@ class PgSQL
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper for pg_field_name
|
* wrapper for pg_field_name
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @param int $i field position
|
* @param int $i field position
|
||||||
* @return string|bool name or false on error
|
* @return string|bool name or false on error
|
||||||
*/
|
*/
|
||||||
public function __dbFieldName($cursor, $i)
|
public function __dbFieldName($cursor, int $i)
|
||||||
{
|
{
|
||||||
if ($cursor === false || is_bool($cursor)) {
|
if ($cursor === false || is_bool($cursor)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -251,8 +251,8 @@ class PgSQL
|
|||||||
* wrapper for pg_fetch_array
|
* wrapper for pg_fetch_array
|
||||||
* if through/true false, use __dbResultType(true)
|
* if through/true false, use __dbResultType(true)
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @param int $result_type result type as int number
|
* @param int $result_type result type as int number
|
||||||
* @return array<mixed>|bool array result data or false on end/error
|
* @return array<mixed>|bool array result data or false on end/error
|
||||||
*/
|
*/
|
||||||
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH)
|
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH)
|
||||||
{
|
{
|
||||||
@@ -279,8 +279,8 @@ class PgSQL
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper for pg_fetch_all
|
* wrapper for pg_fetch_all
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @return array<mixed>|bool data array or false for end/error
|
* @return array<mixed>|bool data array or false for end/error
|
||||||
*/
|
*/
|
||||||
public function __dbFetchAll($cursor)
|
public function __dbFetchAll($cursor)
|
||||||
{
|
{
|
||||||
@@ -293,7 +293,7 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* wrapper for pg_affected_rows
|
* wrapper for pg_affected_rows
|
||||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||||
* @return int affected rows, 0 for none, -1 for error
|
* @return int affected rows, 0 for none, -1 for error
|
||||||
*/
|
*/
|
||||||
public function __dbAffectedRows($cursor): int
|
public function __dbAffectedRows($cursor): int
|
||||||
{
|
{
|
||||||
@@ -362,19 +362,11 @@ class PgSQL
|
|||||||
public function __dbPrimaryKey(string $table, string $schema = '')
|
public function __dbPrimaryKey(string $table, string $schema = '')
|
||||||
{
|
{
|
||||||
if ($table) {
|
if ($table) {
|
||||||
// check if schema set is different from schema given, only needed if schema is not empty
|
// check if schema set is different from schema given,
|
||||||
|
// only needed if schema is not empty
|
||||||
$table_prefix = '';
|
$table_prefix = '';
|
||||||
if ($schema) {
|
if ($schema) {
|
||||||
$q = "SHOW search_path";
|
$search_path = $this->__dbGetSchema();
|
||||||
$cursor = $this->__dbQuery($q);
|
|
||||||
if ($cursor === false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$__db_fetch_array = $this->__dbFetchArray($cursor);
|
|
||||||
if (!is_array($__db_fetch_array)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$search_path = $__db_fetch_array['search_path'] ?? '';
|
|
||||||
if ($search_path != $schema) {
|
if ($search_path != $schema) {
|
||||||
$table_prefix = $schema . '.';
|
$table_prefix = $schema . '.';
|
||||||
}
|
}
|
||||||
@@ -415,12 +407,12 @@ class PgSQL
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper for pg_connect, writes out failure to screen if error occurs (hidden var)
|
* wrapper for pg_connect, writes out failure to screen if error occurs (hidden var)
|
||||||
* @param string $db_host host name
|
* @param string $db_host host name
|
||||||
* @param string $db_user user name
|
* @param string $db_user user name
|
||||||
* @param string $db_pass password
|
* @param string $db_pass password
|
||||||
* @param string $db_name databse name
|
* @param string $db_name databse name
|
||||||
* @param integer $db_port port (int, 5432 is default)
|
* @param integer $db_port port (int, 5432 is default)
|
||||||
* @param string $db_ssl SSL (allow is default)
|
* @param string $db_ssl SSL (allow is default)
|
||||||
* @return object|resource|bool db handler PgSql\Connection or false on error
|
* @return object|resource|bool db handler PgSql\Connection or false on error
|
||||||
*/
|
*/
|
||||||
public function __dbConnect(
|
public function __dbConnect(
|
||||||
@@ -458,9 +450,6 @@ class PgSQL
|
|||||||
}
|
}
|
||||||
// connect
|
// connect
|
||||||
$this->dbh = pg_connect(join(' ', $connection_string));
|
$this->dbh = pg_connect(join(' ', $connection_string));
|
||||||
// if (!$this->dbh) {
|
|
||||||
// die("<!-- Can't connect to database //-->");
|
|
||||||
// }
|
|
||||||
return $this->dbh;
|
return $this->dbh;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,7 +458,7 @@ class PgSQL
|
|||||||
* html formatted string with error name
|
* html formatted string with error name
|
||||||
* @param bool|object|resource $cursor cursor PgSql\Result (former resource)
|
* @param bool|object|resource $cursor cursor PgSql\Result (former resource)
|
||||||
* or null
|
* or null
|
||||||
* @return string error string
|
* @return string error string
|
||||||
*/
|
*/
|
||||||
public function __dbPrintError($cursor = false): string
|
public function __dbPrintError($cursor = false): string
|
||||||
{
|
{
|
||||||
@@ -508,7 +497,7 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* wrapper for pg_escape_string
|
* wrapper for pg_escape_string
|
||||||
* @param string|int|float|bool $string any string/int/float/bool
|
* @param string|int|float|bool $string any string/int/float/bool
|
||||||
* @return string excaped string
|
* @return string excaped string
|
||||||
*/
|
*/
|
||||||
public function __dbEscapeString($string): string
|
public function __dbEscapeString($string): string
|
||||||
{
|
{
|
||||||
@@ -523,7 +512,7 @@ class PgSQL
|
|||||||
* difference to escape string is that this one adds quotes ('') around
|
* difference to escape string is that this one adds quotes ('') around
|
||||||
* the string too
|
* the string too
|
||||||
* @param string|int|float|bool $string any string/int/float/bool
|
* @param string|int|float|bool $string any string/int/float/bool
|
||||||
* @return string excaped string including quites
|
* @return string excaped string including quites
|
||||||
*/
|
*/
|
||||||
public function __dbEscapeLiteral($string): string
|
public function __dbEscapeLiteral($string): string
|
||||||
{
|
{
|
||||||
@@ -554,7 +543,7 @@ class PgSQL
|
|||||||
* @param string $bytea bytea data stream
|
* @param string $bytea bytea data stream
|
||||||
* @return string escaped bytea string
|
* @return string escaped bytea string
|
||||||
*/
|
*/
|
||||||
public function __dbEscapeBytea($bytea): string
|
public function __dbEscapeBytea(string $bytea): string
|
||||||
{
|
{
|
||||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||||
return '';
|
return '';
|
||||||
@@ -564,7 +553,7 @@ class PgSQL
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper for pg_connection_busy
|
* wrapper for pg_connection_busy
|
||||||
* @return bool true/false for busy connection
|
* @return bool True if connection is busy, False if not or no db connection at all
|
||||||
*/
|
*/
|
||||||
public function __dbConnectionBusy(): bool
|
public function __dbConnectionBusy(): bool
|
||||||
{
|
{
|
||||||
@@ -577,7 +566,8 @@ class PgSQL
|
|||||||
/**
|
/**
|
||||||
* Experimental wrapper with scoket timetout
|
* Experimental wrapper with scoket timetout
|
||||||
* @param integer $timeout_seconds Wait how many seconds on timeout
|
* @param integer $timeout_seconds Wait how many seconds on timeout
|
||||||
* @return boolean
|
* @return boolean True if connection is busy, or false on
|
||||||
|
* not busy or no db connection at all
|
||||||
*/
|
*/
|
||||||
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool
|
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool
|
||||||
{
|
{
|
||||||
@@ -619,10 +609,10 @@ class PgSQL
|
|||||||
* This is a fallback for old PostgreSQL versions
|
* This is a fallback for old PostgreSQL versions
|
||||||
* postgresql array to php array
|
* postgresql array to php array
|
||||||
* https://stackoverflow.com/a/27964420
|
* https://stackoverflow.com/a/27964420
|
||||||
* @param string $array_text Array text from PostgreSQL
|
* @param string $array_text Array text from PostgreSQL
|
||||||
* @param int $start Start string position
|
* @param int $start Start string position
|
||||||
* @param int|null $end End string position from recursive call
|
* @param int|null $end End string position from recursive call
|
||||||
* @return null|array<mixed> PHP type array
|
* @return array<mixed>|null PHP type array
|
||||||
*/
|
*/
|
||||||
public function __dbArrayParse(
|
public function __dbArrayParse(
|
||||||
string $array_text,
|
string $array_text,
|
||||||
@@ -673,6 +663,110 @@ class PgSQL
|
|||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wrapper for any SHOW data blocks
|
||||||
|
* eg search_path or client_encoding
|
||||||
|
* @param string $show_string Part to show, if invalid will return empty string
|
||||||
|
* @return string Found part as is
|
||||||
|
*/
|
||||||
|
public function __dbShow(string $show_string): string
|
||||||
|
{
|
||||||
|
// get search path
|
||||||
|
$cursor = $this->__dbQuery("SHOW " . $this->__dbEscapeIdentifier($show_string));
|
||||||
|
// abort on failure and return empty
|
||||||
|
if ($cursor === false) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
// get result
|
||||||
|
$db_schema = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||||
|
/** @phpstan-ignore-next-line Cannot access offset string on array|bool */
|
||||||
|
return $db_schema[$show_string] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a new database schema/search_path
|
||||||
|
* Checks if schema exits and if not aborts with error code 2
|
||||||
|
* @param string $db_schema Schema to set
|
||||||
|
* @return int Returns 0 if no error
|
||||||
|
* 1 for check query failed
|
||||||
|
* 2 for schema not found in database
|
||||||
|
*/
|
||||||
|
public function __dbSetSchema(string $db_schema): int
|
||||||
|
{
|
||||||
|
// check if schema actually exists
|
||||||
|
$query = "SELECT EXISTS("
|
||||||
|
. "SELECT 1 FROM information_schema.schemata "
|
||||||
|
. "WHERE schema_name = " . $this->__dbEscapeLiteral($db_schema)
|
||||||
|
. ")";
|
||||||
|
$cursor = $this->__dbQuery($query);
|
||||||
|
// abort if execution fails
|
||||||
|
if ($cursor === false) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// check if schema does not exists
|
||||||
|
$row = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||||
|
/** @phpstan-ignore-next-line */
|
||||||
|
if (empty($row['exists']) || $row['exists'] == 'f') {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
$query = "SET search_path TO " . $this->__dbEscapeLiteral($db_schema);
|
||||||
|
$this->__dbQuery($query);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns current set schema/search_path
|
||||||
|
* @return string Search Path as currently set in DB
|
||||||
|
*/
|
||||||
|
public function __dbGetSchema(): string
|
||||||
|
{
|
||||||
|
return $this->__dbShow('search_path');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the client encoding
|
||||||
|
* Returns 0 on set ok, or 3 if the client encoding could not be set
|
||||||
|
* @param string $db_encoding
|
||||||
|
* @return int Returns 0 for no error
|
||||||
|
* [not used] 1 for check query failed
|
||||||
|
* [not used] 2 for invalid client encoding
|
||||||
|
* 3 client encoding could not be set
|
||||||
|
*/
|
||||||
|
public function __dbSetEncoding(string $db_encoding): int
|
||||||
|
{
|
||||||
|
// check if ecnoding is valid first
|
||||||
|
// does not take into account aliases
|
||||||
|
// TODO lookup with alisaes so eg ShiftJIS does not get a false
|
||||||
|
// $query = "SELECT EXISTS("
|
||||||
|
// . "SELECT pg_catalog.pg_encoding_to_char(conforencoding) "
|
||||||
|
// . "FROM pg_catalog.pg_conversion "
|
||||||
|
// . "WHERE pg_catalog.pg_encoding_to_char(conforencoding) = "
|
||||||
|
// . $this->dbEscapeLiteral($db_encoding)
|
||||||
|
// . ")";
|
||||||
|
// $cursor = $this->__dbQuery($query);
|
||||||
|
// if ($cursor === false) {
|
||||||
|
// return 1;
|
||||||
|
// }
|
||||||
|
// $row = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||||
|
// if ($row['exists'] == 'f') {
|
||||||
|
// return 2;
|
||||||
|
// }
|
||||||
|
$query = "SET client_encoding TO " . $this->__dbEscapeLiteral($db_encoding);
|
||||||
|
if ($this->__dbQuery($query) === false) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns current set client encoding
|
||||||
|
* @return string Client encoding string, empty if not set
|
||||||
|
*/
|
||||||
|
public function __dbGetEncoding(): string
|
||||||
|
{
|
||||||
|
return $this->__dbShow('client_encoding');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
1
www/vendor/composer/autoload_classmap.php
vendored
1
www/vendor/composer/autoload_classmap.php
vendored
@@ -30,6 +30,7 @@ return array(
|
|||||||
'CoreLibs\\Create\\Uids' => $baseDir . '/lib/CoreLibs/Create/Uids.php',
|
'CoreLibs\\Create\\Uids' => $baseDir . '/lib/CoreLibs/Create/Uids.php',
|
||||||
'CoreLibs\\DB\\Extended\\ArrayIO' => $baseDir . '/lib/CoreLibs/DB/Extended/ArrayIO.php',
|
'CoreLibs\\DB\\Extended\\ArrayIO' => $baseDir . '/lib/CoreLibs/DB/Extended/ArrayIO.php',
|
||||||
'CoreLibs\\DB\\IO' => $baseDir . '/lib/CoreLibs/DB/IO.php',
|
'CoreLibs\\DB\\IO' => $baseDir . '/lib/CoreLibs/DB/IO.php',
|
||||||
|
'CoreLibs\\DB\\SQL\\Interface\\SqlFunctions' => $baseDir . '/lib/CoreLibs/DB/SQL/Interface/SqlFunctions.php',
|
||||||
'CoreLibs\\DB\\SQL\\PgSQL' => $baseDir . '/lib/CoreLibs/DB/SQL/PgSQL.php',
|
'CoreLibs\\DB\\SQL\\PgSQL' => $baseDir . '/lib/CoreLibs/DB/SQL/PgSQL.php',
|
||||||
'CoreLibs\\Debug\\FileWriter' => $baseDir . '/lib/CoreLibs/Debug/FileWriter.php',
|
'CoreLibs\\Debug\\FileWriter' => $baseDir . '/lib/CoreLibs/Debug/FileWriter.php',
|
||||||
'CoreLibs\\Debug\\Logging' => $baseDir . '/lib/CoreLibs/Debug/Logging.php',
|
'CoreLibs\\Debug\\Logging' => $baseDir . '/lib/CoreLibs/Debug/Logging.php',
|
||||||
|
|||||||
1
www/vendor/composer/autoload_static.php
vendored
1
www/vendor/composer/autoload_static.php
vendored
@@ -95,6 +95,7 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
|
|||||||
'CoreLibs\\Create\\Uids' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Uids.php',
|
'CoreLibs\\Create\\Uids' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Uids.php',
|
||||||
'CoreLibs\\DB\\Extended\\ArrayIO' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/Extended/ArrayIO.php',
|
'CoreLibs\\DB\\Extended\\ArrayIO' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/Extended/ArrayIO.php',
|
||||||
'CoreLibs\\DB\\IO' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/IO.php',
|
'CoreLibs\\DB\\IO' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/IO.php',
|
||||||
|
'CoreLibs\\DB\\SQL\\Interface\\SqlFunctions' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/SQL/Interface/SqlFunctions.php',
|
||||||
'CoreLibs\\DB\\SQL\\PgSQL' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/SQL/PgSQL.php',
|
'CoreLibs\\DB\\SQL\\PgSQL' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/SQL/PgSQL.php',
|
||||||
'CoreLibs\\Debug\\FileWriter' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/FileWriter.php',
|
'CoreLibs\\Debug\\FileWriter' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/FileWriter.php',
|
||||||
'CoreLibs\\Debug\\Logging' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/Logging.php',
|
'CoreLibs\\Debug\\Logging' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/Logging.php',
|
||||||
|
|||||||
Reference in New Issue
Block a user