Compare commits

...

4 Commits

Author SHA1 Message Date
Clemens Schwaighofer
8bf694b2b2 ArrayHandler: find next or previous key in array and return it
get next or previous key in array
2023-11-02 14:01:47 +09:00
Clemens Schwaighofer
caf03421a7 Bug fix for DB\IO param prepare call with wrong param array check
The global param array in the class instead of the param array passed
to the method was checked
2023-10-31 10:18:44 +09:00
Clemens Schwaighofer
facf8adaf7 DB\IO move dbReturn params set before first abort 2023-10-31 10:05:43 +09:00
Clemens Schwaighofer
c8158c8224 Remove echo from phpunit test file 2023-10-23 17:11:06 +09:00
6 changed files with 176 additions and 6 deletions

View File

@@ -1098,16 +1098,109 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
* @testdox arrayFlatForKey array $input will be $expected [$_dataName]
*
* @param array $input
* @param string $search
* @param array $expected
* @return void
*/
public function testArrayFlatForKey(array $input, $search, array $expected): void
public function testArrayFlatForKey(array $input, string $search, array $expected): void
{
$this->assertEquals(
$expected,
\CoreLibs\Combined\ArrayHandler::arrayFlatForKey($input, $search)
);
}
/**
* Undocumented function
*
* @return array
*/
public function providerArrayGetNextPrevKey(): array
{
return [
'find, ok' => [
'input' => [
'a' => 'First',
'b' => 'Second',
'c' => 'Third',
],
'b',
'a',
'c'
],
'find, first' => [
'input' => [
'a' => 'First',
'b' => 'Second',
'c' => 'Third',
],
'a',
null,
'b'
],
'find, last' => [
'input' => [
'a' => 'First',
'b' => 'Second',
'c' => 'Third',
],
'c',
'b',
null
],
'find, not found' => [
'input' => [
'a' => 'First',
'b' => 'Second',
'c' => 'Third',
],
'z',
null,
null
],
'int, index' => [
'input' => [
'a',
'b',
'c'
],
1,
0,
2
]
];
}
/**
* Undocumented function
*
* @covers ::arrayGetPrevKey, ::arrayGetNextKey
* @dataProvider providerArrayGetNextPrevKey
* @testdox arrayGetNextPrevKey get next/prev key for $search wtih $expected_prev/$expected_next [$_dataName]
*
* @param array $input
* @param int|string $search
* @param int|string|null $expected_prev
* @param int|string|null $expected_next
* @return void
*/
public function testArrayGetNextPrevKey(
array $input,
int|string $search,
int|string|null $expected_prev,
int|string|null $expected_next
): void {
$this->assertEquals(
$expected_prev,
\CoreLibs\Combined\ArrayHandler::arrayGetPrevKey($input, $search),
'Find prev key in array'
);
$this->assertEquals(
$expected_next,
\CoreLibs\Combined\ArrayHandler::arrayGetNextKey($input, $search),
'Find next key in array'
);
}
}
// __END__

View File

@@ -467,7 +467,6 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds);
} else {
if (in_array('show_only_days', $params)) {
echo "FOO\n";
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds, show_only_days:true);
} elseif (in_array('truncate_after', $params)) {
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds, truncate_after: 'v');

View File

@@ -21,6 +21,7 @@ ob_end_flush();
use CoreLibs\Combined\ArrayHandler;
use CoreLibs\Debug\Support as DgS;
use CoreLibs\Convert\SetVarType;
use PHPUnit\Framework\Constraint\ArrayHasKey;
$log = new CoreLibs\Logging\Logging([
'log_folder' => BASE . LOG,
@@ -236,6 +237,22 @@ $flag = false;
$output = \CoreLibs\Combined\ArrayHandler::genAssocArray($db_array, $key, $value, $flag);
print "OUTPUT: " . \CoreLibs\Debug\Support::printAr($output) . "<br>";
print "<hr>";
$array = [
'a' => 'First',
'b' => 'Second',
'c' => 'Third',
];
foreach (array_keys($array) as $search) {
print "Result[" . $search . "]: "
. "next: " . DgS::printAr(ArrayHandler::arrayGetNextKey($array, $search)) . ", "
. "prev: " . DgS::printAr(ArrayHandler::arrayGetPrevKey($array, $search))
. "<br>";
}
print "Key not exists: " . DgS::printAr(ArrayHandler::arrayGetNextKey($array, 'z')) . "<br>";
print "</body></html>";
// __END__

View File

@@ -56,7 +56,9 @@ print "<b>dbReturn CACHE tests</b><br>";
$db->dbExec("DELETE FROM test_db_return");
$db->dbExec("INSERT INTO test_db_return (uid, data) VALUES ('A1', 'Test A'), ('B1', 'Test B')");
// read query to use
$q_db_ret = "SELECT * FROM test_db_return ORDER BY uid";
$q_db_ret = <<<SQL
SELECT * FROM test_db_return ORDER BY uid
SQL;
RunningTime::hrRunningTime();
@@ -157,4 +159,15 @@ $db->dbCacheReset($q_db_ret);
print "<br>";
print "Overall Run time: " . RunningTime::hrRunningTimeFromStart() . "<br>";
print "<br>";
print "PARAM TEST RUN<br>";
// PARAM
$q_db_ret = <<<SQL
SELECT * FROM test_db_return WHERE uid = $1
SQL;
while (is_array($res = $db->dbReturnParams($q_db_ret, ['A1'], $db::NO_CACHE, true))) {
print "ROW: " . Support::printAr($res) . "<br>";
}
// __END__

View File

@@ -236,6 +236,54 @@ class ArrayHandler
return $hit_list;
}
/**
* main wrapper function for next/prev key
*
* @param array<mixed> $array array to search in
* @param int|string $key key for next/prev
* @param bool $next [=true] if to search next or prev
* @return int|string|null Next/prev key or null for end/first
*/
private static function arrayGetKey(array $array, int|string $key, bool $next = true): int|string|null
{
$keys = array_keys($array);
if (($position = array_search($key, $keys, true)) === false) {
return null;
}
$next_position = $next ? $position + 1 : $position - 1;
if (!isset($keys[$next_position])) {
return null;
}
return $keys[$next_position];
}
/**
* Get previous array key from an array
* null on not found
*
* @param array<mixed> $array
* @param int|string $key
* @return int|string|null Next key, or null for not found
*/
public static function arrayGetPrevKey(array $array, int|string $key): int|string|null
{
return self::arrayGetKey($array, $key, false);
}
/**
* Get next array key from an array
* null on not found
*
* @param array<mixed> $array
* @param int|string $key
* @return int|string|null Next key, or null for not found
*/
public static function arrayGetNextKey(array $array, int|string $key): int|string|null
{
return self::arrayGetKey($array, $key, true);
}
/**
* correctly recursive merges as an array as array_merge_recursive
* just glues things together

View File

@@ -1186,7 +1186,7 @@ class IO
*/
private function __dbDebugPrepareContext(string $query, array $params = []): array
{
if ($this->params === []) {
if ($params === []) {
return [];
}
$error_data = [
@@ -2411,6 +2411,8 @@ class IO
// set the query
$this->cursor_ext[$query_hash]['query'] = $query;
// set the query parameters
$this->cursor_ext[$query_hash]['params'] = $params;
// before doing ANYTHING check if query is "SELECT ..." everything else does not work
if (!$this->dbCheckQueryForSelect($this->cursor_ext[$query_hash]['query'])) {
$this->__dbError(17, false, context: [
@@ -2420,8 +2422,6 @@ class IO
]);
return false;
}
// set the query parameters
$this->cursor_ext[$query_hash]['params'] = $params;
// QUERY PARAMS: run query params check and rewrite
if ($this->dbGetConvertPlaceholder() === true) {
try {