DB\IO updates for vesion info calls

dbVersion only returns nn.n version without any additional info
added new dbVersionNumeric that follows the nnNNnn type of format, eg
90605 or 130006
Added new dbVersionInfo where all the pg_version returned array elements
can be checked. Has a strip override for 'server' version to no strip
additional info
dbVersionInfoParameters returns an array of all possible parameters that
can be called.
Not that if the whole info block is needed pg_version($db->dbGetDbh) is
recommended for now.

Also dbCompareVersion does not call the dbVersion but calls the
functions __dbVersion
This commit is contained in:
Clemens Schwaighofer
2022-03-14 10:38:47 +09:00
parent a833b26322
commit 40edbe271d
4 changed files with 280 additions and 20 deletions

View File

@@ -55,6 +55,23 @@ echo "DB_CONFIG_SET constant: <pre>" . print_r(DB_CONFIG, true) . "</pre><br>";
print "DB client encoding: " . $db->dbGetEncoding() . "<br>";
print "DB search path: " . $db->dbGetSchema() . "<br>";
$to_db_version = '13.6';
print "VERSION DB: " . $db->dbVersion() . "<br>";
print "VERSION LONG DB: " . $db->dbVersionInfo('server', false) . "<br>";
print "VERSION NUMERIC DB: " . $db->dbVersionNumeric() . "<br>";
print "SERVER ENCODING: " . $db->dbVersionInfo('server_encoding') . "<br>";
print "ALL PG VERSION PARAMETERS: <pre>" . print_r($db->dbVersionInfoParameters(), true) . "</pre><br>";
print "ALL OUTPUT [TEST]: <pre>" . print_r(pg_version($db->dbGetDbh()), true) . "</pre><br>";
print "DB Version smaller $to_db_version: " . $db->dbCompareVersion('<' . $to_db_version) . "<br>";
print "DB Version smaller than $to_db_version: " . $db->dbCompareVersion('<=' . $to_db_version) . "<br>";
print "DB Version equal $to_db_version: " . $db->dbCompareVersion('=' . $to_db_version) . "<br>";
print "DB Version bigger than $to_db_version: " . $db->dbCompareVersion('>=' . $to_db_version) . "<br>";
print "DB Version bigger $to_db_version: " . $db->dbCompareVersion('>' . $to_db_version) . "<br>";
$db->dbSetEncoding('SJIS');
print "ENCODING TEST: " . $db->dbVersionInfo('client_encoding') . "/" . $db->dbGetEncoding() . "<br>";
$db->dbResetEncoding();
while (is_array($res = $db->dbReturn("SELECT * FROM max_test", DbIo::USE_CACHE, true))) {
print "UUD/TIME: " . $res['uid'] . "/" . $res['time'] . "<br>";
}
@@ -342,14 +359,6 @@ print "ASYNC PREVIOUS INSERTED: "
) . "<br>";
*/
$to_db_version = '9.1.9';
print "VERSION DB: " . $db->dbVersion() . "<br>";
print "DB Version smaller $to_db_version: " . $db->dbCompareVersion('<' . $to_db_version) . "<br>";
print "DB Version smaller than $to_db_version: " . $db->dbCompareVersion('<=' . $to_db_version) . "<br>";
print "DB Version equal $to_db_version: " . $db->dbCompareVersion('=' . $to_db_version) . "<br>";
print "DB Version bigger than $to_db_version: " . $db->dbCompareVersion('>=' . $to_db_version) . "<br>";
print "DB Version bigger $to_db_version: " . $db->dbCompareVersion('>' . $to_db_version) . "<br>";
/*
$q = "Select * from test_foo";
$test_foo = $db->dbExecAsync($q);

View File

@@ -1325,7 +1325,16 @@ class IO
}
/**
* return current database version
* Server version as integer value
* @return integer Version as integer
*/
public function dbVersionNumeric(): int
{
return $this->db_functions->__dbVersionNumeric();
}
/**
* return current database version (server side) as string
* @return string database version as string
*/
public function dbVersion(): string
@@ -1333,6 +1342,28 @@ class IO
return $this->db_functions->__dbVersion();
}
/**
* extended version info, can access all additional information data
* @param string $parameter Array parameter name, if not valid returns
* empty string
* @param boolean $strip Strip extended server info string, default true
* eg nn.n (other info) will only return nn.n
* @return string Parameter value
*/
public function dbVersionInfo(string $parameter, bool $strip = true): string
{
return $this->db_functions->__dbVersionInfo($parameter, $strip);
}
/**
* All possible parameter names for dbVersionInfo
* @return array List of all parameter names
*/
public function dbVersionInfoParameters(): array
{
return $this->db_functions->__dbVersionInfoParameterList();
}
/**
* returns boolean true or false if the string matches the database version
* @param string $compare string to match in type =X.Y, >X.Y, <X.Y, <=X.Y, >=X.Y
@@ -1346,6 +1377,7 @@ class IO
$compare = $matches[1];
$to_master = $matches[2];
$to_minor = $matches[3];
$to_version = '';
if (!$compare || !strlen($to_master) || !strlen($to_minor)) {
return false;
} else {
@@ -1353,7 +1385,11 @@ class IO
}
// db_version can return X.Y.Z
// we only compare the first two
preg_match("/^(\d{1,})\.(\d{1,})\.?(\d{1,})?/", $this->dbVersion(), $matches);
preg_match(
"/^(\d{1,})\.(\d{1,})\.?(\d{1,})?/",
$this->db_functions->__dbVersion(),
$matches
);
$master = $matches[1];
$minor = $matches[2];
$version = $master . ($minor < 10 ? '0' : '') . $minor;

View File

@@ -586,6 +586,47 @@ class PgSQL implements Interface\SqlFunctions
return $busy;
}
/**
* extended wrapper for pg_version
* can return any setting in this array block
* If no connection, return empty string,
* if not in array return empty string
* On default 'version' will be stripped of any space attached info
* eg 13.5 (other info) will return only 13.5
* @param string $parameter Parameter string to extract from array
* @param boolean $strip If parameter is server strip out on default
* Set to false to get original string AS is
* @return string The parameter value
*/
public function __dbVersionInfo(string $parameter, bool $strip = true): string
{
if ($this->dbh === false || is_bool($this->dbh)) {
return '';
}
// extract element
$return_string = pg_version($this->dbh)[$parameter] ?? '';
// for version, strip if requested
if (
in_array($parameter, ['server']) &&
$strip === true
) {
$return_string = explode(' ', $return_string, 2)[0] ?? '';
}
return $return_string;
}
/**
* Returns all parameters that are possible from the db_version
* @return array Parameter key names from pg_version
*/
public function __dbVersionInfoParameterList(): array
{
if ($this->dbh === false || is_bool($this->dbh)) {
return [];
}
return array_keys(pg_version($this->dbh));
}
/**
* wrapper for pg_version
* Note: this only returns server version
@@ -597,10 +638,22 @@ class PgSQL implements Interface\SqlFunctions
if ($this->dbh === false || is_bool($this->dbh)) {
return '';
}
// array has client, protocol, server
// we just need the server
$v = pg_version($this->dbh);
return $v['server'];
// array has client, protocol, server, we just return server stripped
return $this->__dbVersionInfo('server', true);
}
/**
* Returns a numeric version eg 90506 or 130006, etc
* Note that this calls a show command on the server
* Note:
* Old version is 9.5.6 where 9.5 is the major version
* Newer Postgresql (10 on) have only one major version so eg 13.5
* is returned as 130005
* @return integer Server version
*/
public function __dbVersionNumeric(): int
{
return (int)$this->__dbShow('server_version_num');
}
/**
@@ -664,6 +717,23 @@ class PgSQL implements Interface\SqlFunctions
return $return;
}
/**
* Returns any server setting, if no connection or empty parameter returns
* empty string
* @param string $parameter Parameter to query
* @return string Settings value as string
*/
public function __dbParameter(string $parameter): string
{
if ($this->dbh === false || is_bool($this->dbh)) {
return '';
}
if (empty($parameter)) {
return '';
}
return pg_parameter_status($this->dbh, $parameter);
}
/**
* wrapper for any SHOW data blocks
* eg search_path or client_encoding