diff --git a/4dev/tests/CoreLibsDBIOTest.php b/4dev/tests/CoreLibsDBIOTest.php index 83c314bf..0994c5bf 100644 --- a/4dev/tests/CoreLibsDBIOTest.php +++ b/4dev/tests/CoreLibsDBIOTest.php @@ -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 diff --git a/www/admin/class_test.db.php b/www/admin/class_test.db.php index 370c702c..cc364d02 100644 --- a/www/admin/class_test.db.php +++ b/www/admin/class_test.db.php @@ -55,6 +55,23 @@ echo "DB_CONFIG_SET constant:
" . print_r(DB_CONFIG, true) . "

"; print "DB client encoding: " . $db->dbGetEncoding() . "
"; print "DB search path: " . $db->dbGetSchema() . "
"; +$to_db_version = '13.6'; +print "VERSION DB: " . $db->dbVersion() . "
"; +print "VERSION LONG DB: " . $db->dbVersionInfo('server', false) . "
"; +print "VERSION NUMERIC DB: " . $db->dbVersionNumeric() . "
"; +print "SERVER ENCODING: " . $db->dbVersionInfo('server_encoding') . "
"; +print "ALL PG VERSION PARAMETERS:
" . print_r($db->dbVersionInfoParameters(), true) . "

"; +print "ALL OUTPUT [TEST]:
" . print_r(pg_version($db->dbGetDbh()), true) . "

"; +print "DB Version smaller $to_db_version: " . $db->dbCompareVersion('<' . $to_db_version) . "
"; +print "DB Version smaller than $to_db_version: " . $db->dbCompareVersion('<=' . $to_db_version) . "
"; +print "DB Version equal $to_db_version: " . $db->dbCompareVersion('=' . $to_db_version) . "
"; +print "DB Version bigger than $to_db_version: " . $db->dbCompareVersion('>=' . $to_db_version) . "
"; +print "DB Version bigger $to_db_version: " . $db->dbCompareVersion('>' . $to_db_version) . "
"; + +$db->dbSetEncoding('SJIS'); +print "ENCODING TEST: " . $db->dbVersionInfo('client_encoding') . "/" . $db->dbGetEncoding() . "
"; +$db->dbResetEncoding(); + while (is_array($res = $db->dbReturn("SELECT * FROM max_test", DbIo::USE_CACHE, true))) { print "UUD/TIME: " . $res['uid'] . "/" . $res['time'] . "
"; } @@ -342,14 +359,6 @@ print "ASYNC PREVIOUS INSERTED: " ) . "
"; */ -$to_db_version = '9.1.9'; -print "VERSION DB: " . $db->dbVersion() . "
"; -print "DB Version smaller $to_db_version: " . $db->dbCompareVersion('<' . $to_db_version) . "
"; -print "DB Version smaller than $to_db_version: " . $db->dbCompareVersion('<=' . $to_db_version) . "
"; -print "DB Version equal $to_db_version: " . $db->dbCompareVersion('=' . $to_db_version) . "
"; -print "DB Version bigger than $to_db_version: " . $db->dbCompareVersion('>=' . $to_db_version) . "
"; -print "DB Version bigger $to_db_version: " . $db->dbCompareVersion('>' . $to_db_version) . "
"; - /* $q = "Select * from test_foo"; $test_foo = $db->dbExecAsync($q); diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 4c502ca2..311dc884 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -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 @@ -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; diff --git a/www/lib/CoreLibs/DB/SQL/PgSQL.php b/www/lib/CoreLibs/DB/SQL/PgSQL.php index 0d411827..fa368421 100644 --- a/www/lib/CoreLibs/DB/SQL/PgSQL.php +++ b/www/lib/CoreLibs/DB/SQL/PgSQL.php @@ -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