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

@@ -203,7 +203,147 @@ final class CoreLibsDBIOTest extends TestCase
}
// - connected version test
// dbVerions, dbCompareVersion
// dbVerions, dbVersionNum, dbVersionInfo, dbVersionInfoParameters,
// dbCompareVersion
/**
* Just checks that the return value of dbVersion matches basic regex
*
* @covers ::dbVersion
* @testdox test db version string return matching retex
*
* @return void
*/
public function testDbVersion(): void
{
// self::$log->setLogLevelAll('debug', true);
// self::$log->setLogLevelAll('print', true);
$db = new \CoreLibs\DB\IO(
self::$db_config['valid'],
self::$log
);
$this->assertMatchesRegularExpression(
"/^\d+\.\d+(\.\d+)?$/",
$db->dbVersion()
);
$db->dbClose();
}
/**
* Undocumented function
*
* @covers ::dbVersionNumeric
* @testdox test db version numeric return matching retex
*
* @return void
*/
public function testDbVersionNumeric(): void
{
// self::$log->setLogLevelAll('debug', true);
// self::$log->setLogLevelAll('print', true);
$db = new \CoreLibs\DB\IO(
self::$db_config['valid'],
self::$log
);
$version = $db->dbVersionNumeric();
// must be int
$this->assertIsInt($version);
// assume 90606 or 130006
// should this change, the regex below must change
$this->assertMatchesRegularExpression(
"/^\d{5,6}?$/",
(string)$version
);
$db->dbClose();
}
/**
* Undocumented function
*
* @covers ::dbVersionInfoParameters
* @testdox test db version parameters are returned as array
*
* @return void
*/
public function testDbVersionInfoParameters(): void
{
// self::$log->setLogLevelAll('debug', true);
// self::$log->setLogLevelAll('print', true);
$db = new \CoreLibs\DB\IO(
self::$db_config['valid'],
self::$log
);
$parameters = $db->dbVersionInfoParameters();
$this->assertIsArray($parameters);
$this->assertGreaterThan(
1,
count($parameters)
);
// must have at least this
$this->assertContains(
'server',
$parameters
);
$db->dbClose();
}
/**
* Undocumented function
*
* @return array
*/
public function versionInfoProvider(): array
{
return [
'client' => [
'client',
"/^\d+\.\d+/"
],
'session authorization' => [
'session_authorization',
"/^\w+$/"
],
'test non existing' => [
'non_existing',
'/^$/'
],
];
}
/**
* Undocumented function
*
* @covers ::dbVersionInfo
* @dataProvider versionInfoProvider
* @testdox Version Info $parameter matches as $expected [$_dataName]
*
* @param string $parameter
* @param string $expected
* @return void
*/
public function testDbVersionInfo(string $parameter, string $expected): void
{
// self::$log->setLogLevelAll('debug', true);
// self::$log->setLogLevelAll('print', true);
$db = new \CoreLibs\DB\IO(
self::$db_config['valid'],
self::$log
);
$this->assertMatchesRegularExpression(
$expected,
$db->dbVersionInfo($parameter)
);
$db->dbClose();
}
/**
* Returns test list for dbCompareVersion check
@@ -212,6 +352,8 @@ final class CoreLibsDBIOTest extends TestCase
*/
public function versionProvider(): array
{
// 1: vesion to compare to
// 2: expected outcome
return [
'compare = ok' => [ '=13.6.0', true ],
'compare = bad' => [ '=9.2.0', false ],
@@ -2664,10 +2806,16 @@ final class CoreLibsDBIOTest extends TestCase
// avoid bubbling up error
@$db->dbSetEncoding($set_encoding)
);
// show query
$this->assertEquals(
$expected_get_encoding,
$db->dbGetEncoding()
);
// pg_version
$this->assertEquals(
$expected_get_encoding,
$db->dbVersionInfo('client_encoding')
);
$db->dbClose();
}
@@ -2840,7 +2988,6 @@ final class CoreLibsDBIOTest extends TestCase
'table_with_primary_key',
'table_with_primary_key_id',
],
// TODO other tests
];
}
@@ -3244,15 +3391,13 @@ final class CoreLibsDBIOTest extends TestCase
$db->dbClose();
}
// TODO implement below checks
// - complex write sets
// dbWriteData, dbWriteDataExt
// - data debug
// dbDumpData
// - deprecated tests [no need to test perhaps]
// getInsertReturn, getReturning, getInsertPK, getReturningExt,
// getCursorExt, getNumRows
// ASYNC at the end because it has 3s timeout
// ASYNC at the end because it has 1s timeout
// - asynchronous executions
// dbExecAsync, dbCheckAsync

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