Compare commits

...

6 Commits

Author SHA1 Message Date
Clemens Schwaighofer
e9c791c164 Add better error reporting to DB\IO for query with params
On error with query with params the query was sent to the server and
if ther query itself is ok but there is a problem with the parameters
a wrong error message ($1 not found) will be returned

Add pg_last_error reporting to catch this too.

Update both error reporting to return not string and prefix combined
but prefix + error string in array

In error return check that both strings are not equal, so we do not
return the same error string twice.

Also default set dbh variable in the PgSQL class to false so it will
skip last error report if there is no dbh set yet.

Bug fix for db query with params debug output. if there are more than 9
entries the $1 of eg $10 is replaced with $1 entry again. Changed to
'#' instead '$' to avoid this.

Other:
ACL\Login: replace EOM with HTML
config.master: replace list() with []
Add single DB tester where we can test single db calls without adding
more to the general test run
2023-05-18 15:20:36 +09:00
Clemens Schwaighofer
c7ec1300b7 Published: v8.3.1 2023-04-26 15:43:11 +09:00
Clemens Schwaighofer
064710324e Bug fix in arraySearchKey path reset 2023-04-26 15:41:56 +09:00
Clemens Schwaighofer
e0356dcadf Release: v8.3.0 2023-04-26 14:56:11 +09:00
Clemens Schwaighofer
62a5992e3a Array combined: new arraySearchKey method
Also update publish script and move URLS to .env file
2023-04-26 14:54:13 +09:00
Clemens Schwaighofer
6bb957fcb3 Publish v8.2.2 2023-04-11 11:04:41 +09:00
9 changed files with 388 additions and 54 deletions

View File

@@ -1 +1 @@
8.2.1
8.3.1

View File

@@ -20,7 +20,11 @@ fi;
# read in the .env.deploy file and we must have
# GITLAB_USER
# GITLAB_TOKEN
# GITLAB_URL
# GITEA_USER
# GITEA_DEPLOY_TOKEN
# GITEA_URL_DL
# GITEA_URL_PUSH
if [ ! -f "${BASE_FOLDER}.env.deploy" ]; then
echo "Deploy enviroment file .env.deploy is missing";
exit;
@@ -33,26 +37,27 @@ set +o allexport;
echo "[START]";
# gitea
if [ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then
if [ ! -z "${GITEA_URL_DL}" ] && [ ! -z "${GITEA_URL_PUSH}" ] &&
[ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then
curl -LJO \
--output-dir "${BASE_FOLDER}" \
https://git.egplusww.jp/Composer/CoreLibs-Composer-All/archive/v${VERSION}.zip;
${GITEA_URL_DL}/v${VERSION}.zip;
curl --user ${GITEA_USER}:${GITEA_TOKEN} \
--upload-file "${BASE_FOLDER}/CoreLibs-Composer-All-v${VERSION}.zip" \
https://git.egplusww.jp/api/packages/Composer/composer?version=${VERSION};
${GITEA_URL_PUSH}?version=${VERSION};
echo "${VERSION}" > "${file_last_published}";
else
echo "Missing either GITEA_USER or GITEA_TOKEN environment variable";
fi;
# gitlab
if [ ! -z "${GITLAB_DEPLOY_TOKEN}" ]; then
if [ ! -z "${GITLAB_URL}" ] && [ ! -z "${GITLAB_DEPLOY_TOKEN}" ]; then
curl --data tag=v${VERSION} \
--header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \
"https://gitlab-na.factory.tools/api/v4/projects/950/packages/composer";
"${GITLAB_URL}";
curl --data branch=master \
--header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \
"https://gitlab-na.factory.tools/api/v4/projects/950/packages/composer";
"${GITLAB_URL}";
echo "${VERSION}" > "${file_last_published}";
else
echo "Missing GITLAB_DEPLOY_TOKEN environment variable";

View File

@@ -1608,7 +1608,7 @@ class Login
// TODO: submit or JS to set target page as ajax call
// NOTE: for the HTML block I ignore line lengths
// phpcs:disable
$this->login_template['password_change'] = <<<EOM
$this->login_template['password_change'] = <<<HTML
<div id="pw_change_div" class="hidden" style="position: absolute; top: 30px; left: 50px; width: 400px; height: 220px; background-color: white; border: 1px solid black; padding: 25px;">
<table>
<tr><td class="norm" align="center" colspan="2"><h3>{TITLE_PASSWORD_CHANGE}</h3></td></tr>
@@ -1626,7 +1626,7 @@ class Login
</table>
</div>
{PASSWORD_CHANGE_SHOW}
EOM;
HTML;
// phpcs:enable
}
if ($this->password_forgot) {
@@ -1650,7 +1650,7 @@ EOM;
// now check templates
// TODO: submit or JS to set target page as ajax call
if (!$this->login_template['template']) {
$this->login_template['template'] = <<<EOM
$this->login_template['template'] = <<<HTML
<!DOCTYPE html>
<html lang="{LANGUAGE}">
<head>
@@ -1712,7 +1712,7 @@ h3 { font-size: 18px; }
</form>
</body>
</html>
EOM;
HTML;
}
}

View File

@@ -177,6 +177,65 @@ class ArrayHandler
return false;
}
/**
* search for one or many keys in array and return matching values
* If flat is set to true, return flat array with found values only
* If prefix is turned on each found group will be prefixed with the
* search key
*
* @param array<mixed> $array array to search in
* @param array<mixed> $needles keys to find in array
* @param bool $flat [false] Turn on flat output
* @param bool $prefix [false] Prefix found with needle key
* @return array<mixed> Found values
*/
public static function arraySearchKey(
array $array,
array $needles,
bool $flat = false,
bool $prefix = false
): array {
$iterator = new \RecursiveArrayIterator($array);
$recursive = new \RecursiveIteratorIterator(
$iterator,
\RecursiveIteratorIterator::SELF_FIRST
);
$hit_list = [];
if ($prefix === true) {
$hit_list = array_fill_keys($needles, []);
}
$key_path = [];
$prev_depth = 0;
foreach ($recursive as $key => $value) {
if ($prev_depth > $recursive->getDepth()) {
// remove all trailing to ne depth
$diff = $prev_depth - $recursive->getDepth();
array_splice($key_path, -$diff, $diff);
}
$prev_depth = $recursive->getDepth();
if ($flat === false) {
$key_path[$recursive->getDepth()] = $key;
}
if (in_array($key, $needles, true)) {
ksort($key_path);
if ($flat === true) {
$hit = $value;
} else {
$hit = [
'value' => $value,
'path' => $key_path
];
}
if ($prefix === true) {
$hit_list[$key][] = $hit;
} else {
$hit_list[] = $hit;
}
}
}
return $hit_list;
}
/**
* correctly recursive merges as an array as array_merge_recursive
* just glues things together

View File

@@ -735,7 +735,10 @@ class IO
*/
private function __dbErrorPreprocessor(\PgSql\Result|false $cursor = false): array
{
$pg_error_string = '';
$db_prefix = '';
$db_error_string = '';
$db_prefix_last = '';
$db_error_string_last = '';
// 1 = self/__dbErrorPreprocessor, 2 = __dbError, __dbWarning,
// 3+ == actual source
// loop until we get a null, build where called chain
@@ -749,16 +752,31 @@ class IO
if ($where_called === null) {
$where_called = '[Unknown Method]';
}
[$db_prefix_last, $db_error_string_last] = $this->db_functions->__dbPrintLastError();
if ($cursor !== false) {
$pg_error_string = $this->db_functions->__dbPrintError($cursor);
[$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError($cursor);
}
if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) {
$pg_error_string = $this->db_functions->__dbPrintError();
[$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError();
}
if ($pg_error_string) {
$this->__dbDebug('db', $pg_error_string, 'DB_ERROR', $where_called);
// prefix the master if not the same
if (
!empty($db_error_string_last) &&
trim($db_error_string) != trim($db_error_string_last)
) {
$db_error_string =
$db_prefix_last . ' ' . $db_error_string_last . ';'
. $db_prefix . ' ' . $db_error_string;
} elseif (!empty($db_error_string)) {
$db_error_string = $db_prefix . ' ' . $db_error_string;
}
return [$where_called, $pg_error_string];
if ($db_error_string) {
$this->__dbDebug('db', $db_error_string, 'DB_ERROR', $where_called);
}
return [
$where_called,
$db_error_string
];
}
/**
@@ -902,11 +920,14 @@ class IO
// because the placeholders start with $ and at 1,
// we need to increase each key and prefix it with a $ char
for ($i = 0, $iMax = count($keys); $i < $iMax; $i++) {
$keys[$i] = '$' . ($keys[$i] + 1);
// note: if I use $ here, the str_replace will
// replace it again. eg $11 '$1'1would be replaced with $1 again
// prefix data set with parameter pos
$data[$i] = $keys[$i] . ':' . ($data[$i] === null ?
$data[$i] = '#' . ($keys[$i] + 1) . ':' . ($data[$i] === null ?
'"NULL"' : (string)$data[$i]
);
// search part
$keys[$i] = '$' . ($keys[$i] + 1);
}
// simply replace the $1, $2, ... with the actual data and return it
return str_replace(

View File

@@ -209,10 +209,17 @@ interface SqlFunctions
/**
* Undocumented function
*
* @param \PgSql\Result|false $cursor
* @return string
* @return array{0:string,1:string}
*/
public function __dbPrintError(\PgSql\Result|false $cursor = false): string;
public function __dbPrintLastError(): array;
/**
* Undocumented function
*
* @param \PgSql\Result|false $cursor
* @return array{0:string,1:string}
*/
public function __dbPrintError(\PgSql\Result|false $cursor = false): array;
/**
* Undocumented function

View File

@@ -61,7 +61,7 @@ class PgSQL implements Interface\SqlFunctions
/** @var string */
private $last_error_query;
/** @var \PgSql\Connection|false */
private $dbh;
private $dbh = false;
/**
* queries last error query and returns true or false if error was set
@@ -532,18 +532,37 @@ class PgSQL implements Interface\SqlFunctions
return $this->dbh;
}
/**
* Returns last error for active cursor
*
* @return array{0:string,1:string} prefix, error string
*/
public function __dbPrintLastError(): array
{
if (is_bool($this->dbh)) {
return ['', ''];
}
if (!empty($error_message = pg_last_error($this->dbh))) {
return [
'-PostgreSQL-Error-Last-',
$error_message
];
}
return ['', ''];
}
/**
* reads the last error for this cursor and returns
* html formatted string with error name
*
* @param \PgSql\Result|false $cursor cursor
* or null
* @return string error string
* or null
* @return array{0:string,1:string} prefix, error string
*/
public function __dbPrintError(\PgSql\Result|false $cursor = false): string
public function __dbPrintError(\PgSql\Result|false $cursor = false): array
{
if (is_bool($this->dbh)) {
return '';
return ['', ''];
}
// run the query again for the error result here
if ((is_bool($cursor)) && $this->last_error_query) {
@@ -552,10 +571,12 @@ class PgSQL implements Interface\SqlFunctions
$cursor = pg_get_result($this->dbh);
}
if ($cursor && $error_str = pg_result_error($cursor)) {
return '-PostgreSQL-Error- '
. $error_str;
return [
'-PostgreSQL-Error-',
$error_str
];
} else {
return '';
return ['', ''];
}
}

View File

@@ -31,6 +31,7 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
4,
'b',
'c' => 'test',
'single' => 'single',
'same' => 'same',
'deep' => [
'sub' => [
@@ -288,6 +289,188 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
];
}
/**
* Undocumented function
*
* @return array
*/
public function arraySearchKeyProvider(): array
{
/*
0: search in array
1: search keys
2: flat flag
3: prefix flag
4: expected array
*/
return [
// single
'find single, standard' => [
0 => self::$array,
1 => ['single'],
2 => null,
3 => null,
4 => [
0 => [
'value' => 'single',
'path' => ['single'],
],
],
],
'find single, prefix' => [
0 => self::$array,
1 => ['single'],
2 => null,
3 => true,
4 => [
'single' => [
0 => [
'value' => 'single',
'path' => ['single'],
],
],
],
],
'find single, flat' => [
0 => self::$array,
1 => ['single'],
2 => true,
3 => null,
4 => [
'single',
],
],
'find single, flat, prefix' => [
0 => self::$array,
1 => ['single'],
2 => true,
3 => true,
4 => [
'single' => [
'single',
],
],
],
// not found
'not found, standard' => [
0 => self::$array,
1 => ['NOT FOUND'],
2 => null,
3 => null,
4 => [],
],
'not found, standard, prefix' => [
0 => self::$array,
1 => ['NOT FOUND'],
2 => null,
3 => true,
4 => [
'NOT FOUND' => [],
],
],
'not found, flat' => [
0 => self::$array,
1 => ['NOT FOUND'],
2 => true,
3 => null,
4 => [],
],
'not found, flat, prefix' => [
0 => self::$array,
1 => ['NOT FOUND'],
2 => true,
3 => true,
4 => [
'NOT FOUND' => [],
],
],
// multi
'multiple found, standard' => [
0 => self::$array,
1 => ['same'],
2 => null,
3 => null,
4 => [
[
'value' => 'same',
'path' => ['a', 'same', ],
],
[
'value' => 'same',
'path' => ['same', ],
],
[
'value' => 'same',
'path' => ['deep', 'sub', 'same', ],
],
]
],
'multiple found, flat' => [
0 => self::$array,
1 => ['same'],
2 => true,
3 => null,
4 => ['same', 'same', 'same', ],
],
// search with multiple
'search multiple, standard' => [
0 => self::$array,
1 => ['single', 'nested'],
2 => null,
3 => null,
4 => [
[
'value' => 'single',
'path' => ['single'],
],
[
'value' => 'bar',
'path' => ['deep', 'sub', 'nested', ],
],
],
],
'search multiple, prefix' => [
0 => self::$array,
1 => ['single', 'nested'],
2 => null,
3 => true,
4 => [
'single' => [
[
'value' => 'single',
'path' => ['single'],
],
],
'nested' => [
[
'value' => 'bar',
'path' => ['deep', 'sub', 'nested', ],
],
],
],
],
'search multiple, flat' => [
0 => self::$array,
1 => ['single', 'nested'],
2 => true,
3 => null,
4 => [
'single', 'bar',
],
],
'search multiple, flat, prefix' => [
0 => self::$array,
1 => ['single', 'nested'],
2 => true,
3 => true,
4 => [
'single' => ['single', ],
'nested' => ['bar', ],
],
],
];
}
/**
* provides array listing for the merge test
*
@@ -691,6 +874,44 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
);
}
/**
* Undocumented function
*
* @covers::arraySearchKey
* @dataProvider arraySearchKeyProvider
* @testdox arraySearchKey Search array with keys and flat: $flat, prefix: $prefix [$_dataName]
*
* @param array $input
* @param array $needles
* @param bool|null $flat
* @param bool|null $prefix
* @param array $expected
* @return void
*/
public function testArraySearchKey(
array $input,
array $needles,
?bool $flat,
?bool $prefix,
array $expected
): void {
if ($flat === null && $prefix === null) {
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles);
} elseif ($flat === null) {
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, prefix: $prefix);
} elseif ($prefix === null) {
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, flat: $flat);
} else {
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, $flat, $prefix);
}
// print "E: " . print_r($expected, true) . "\n";
// print "R: " . print_r($result, true) . "\n";
$this->assertEquals(
$expected,
$result
);
}
/**
* Undocumented function
*

View File

@@ -156,7 +156,7 @@ final class CoreLibsDBIOTest extends TestCase
$db->dbExec("DROP TABLE test_meta");
}
// uid is for internal reference tests
$base_table = <<<EOM
$base_table = <<<SQL
uid VARCHAR,
row_int INT,
row_numeric NUMERIC,
@@ -172,36 +172,36 @@ final class CoreLibsDBIOTest extends TestCase
row_array_varchar VARCHAR ARRAY
)
WITHOUT OIDS
EOM;
SQL;
// create the tables
$db->dbExec(
// primary key name is table + '_id'
<<<EOM
<<<SQL
CREATE TABLE table_with_primary_key (
table_with_primary_key_id SERIAL PRIMARY KEY,
$base_table
EOM
SQL
/* "CREATE TABLE table_with_primary_key ("
// primary key name is table + '_id'
. "table_with_primary_key_id SERIAL PRIMARY KEY, "
. $base_table */
);
$db->dbExec(
<<<EOM
<<<SQL
CREATE TABLE table_without_primary_key (
$base_table
EOM
SQL
/* "CREATE TABLE table_without_primary_key ("
. $base_table */
);
// create simple table for meta test
$db->dbExec(
<<<EOM
<<<SQL
CREATE TABLE test_meta (
row_1 VARCHAR,
row_2 INT
) WITHOUT OIDS
EOM
SQL
/* "CREATE TABLE test_meta ("
. "row_1 VARCHAR, "
. "row_2 INT"
@@ -1342,10 +1342,10 @@ final class CoreLibsDBIOTest extends TestCase
'has default' => false,
'array dims' => 0,
'is enum' => false,
'is base' => 1,
'is base' => true,
'is composite' => false,
'is pesudo' => false,
'description' => '',
'is pseudo' => false
],
'row_2' => [
'num' => 2,
@@ -1355,10 +1355,10 @@ final class CoreLibsDBIOTest extends TestCase
'has default' => false,
'array dims' => 0,
'is enum' => false,
'is base' => 1,
'is base' => true,
'is composite' => false,
'is pesudo' => false,
'description' => '',
'is pseudo' => false
]
]
],
@@ -1374,10 +1374,10 @@ final class CoreLibsDBIOTest extends TestCase
'has default' => false,
'array dims' => 0,
'is enum' => false,
'is base' => 1,
'is base' => true,
'is composite' => false,
'is pesudo' => false,
'description' => '',
'is pseudo' => false
],
'row_2' => [
'num' => 2,
@@ -1387,10 +1387,10 @@ final class CoreLibsDBIOTest extends TestCase
'has default' => false,
'array dims' => 0,
'is enum' => false,
'is base' => 1,
'is base' => true,
'is composite' => false,
'is pesudo' => false,
'description' => '',
'is pseudo' => false
]
]
],
@@ -4425,16 +4425,16 @@ final class CoreLibsDBIOTest extends TestCase
]
]
],
// same but as EOM
'single insert (PK), EOM string' => [
<<<EOM
// same but as heredoc
'single insert (PK), heredoc string' => [
<<<SQL
INSERT INTO table_with_primary_key (
row_varchar, row_varchar_literal, row_int, row_date
) VALUES (
'Text', 'Other', 123, '2022-03-01'
)
RETURNING row_varchar, row_varchar_literal, row_int, row_date
EOM,
SQL,
null,
null,
null,
@@ -4529,16 +4529,16 @@ final class CoreLibsDBIOTest extends TestCase
]
]
],
// same as above but as EOM string
'single insert (No PK), EOM string' => [
<<<EOM
// same as above but as heredoc string
'single insert (No PK), heredoc string' => [
<<<SQL
INSERT INTO table_without_primary_key (
row_varchar, row_varchar_literal, row_int, row_date
) VALUES (
'Text', 'Other', 123, '2022-03-01'
)
RETURNING row_varchar, row_varchar_literal, row_int, row_date
EOM,
SQL,
null,
null,
null,