Update DB\IO with auto query placeholder rewrite and better error logging

All errors have context that is used to add query, params, etc info
for logging into the DB.
Avoid double logging for PostGreSQL direct errors as those will be
logged now in context to the actual error log
Remove error: 16 missing/empty dbh has this is handled with error 14
in the connect method.

Auto convert ?, :named to $numbered, default off. Activate with
'db_convert_placeholder' flag or method dbSetConvertPlaceholder.
Converted result data for single queries in dbGetPlaceholderConverted
or in the cursor_ext array in placeholer_converted key

Do not auto translate debug queries with placeholder values in query
but keep them in the array in the context array. If needed
'db_debug_replace_placeholder' can be set to show prepared query
with placeholder replaced in the context

New methods:
public function dbSetConvertPlaceholder(bool $flag): void
public function dbGetConvertPlaceholder(): bool
public function dbSetConvertPlaceholderTarget(string $target): bool
public function dbGetConvertPlaceholderTarget(): string
public function dbSetDebugReplacePlaceholder(bool $flag): void
public function dbGetDebugReplacePlaceholder(): bool
public function dbGetPlaceholderConverted(): array

Chagned to public:
public function dbCheckQueryForSelect(string $query): bool
public function dbCheckQueryForInsert(string $query, bool $pure = false): bool
public function dbCheckQueryForUpdate(string $query): bool
This commit is contained in:
Clemens Schwaighofer
2023-10-16 14:43:55 +09:00
parent c46125aef1
commit 89e3888bf8
7 changed files with 947 additions and 398 deletions

View File

@@ -38,9 +38,10 @@ print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><a href="class_test.db.type.php">Class Test DB Types</a></div>';
print '<div><a href="class_test.db.type.php">Class Test DB row type convert to PHP type</a></div>';
print '<div><a href="class_test.db.query-placeholder.php">Class Test DB Query Placeholder convert</a></div>';
print '<div><a href="class_test.db.dbReturn.php">Class Test DB dbReturn</a></div>';
print '<div><a href="class_test.db.single.php">Class Test DB Single Aciont</a></div>';
print '<div><a href="class_test.db.single.php">Class Test DB Single Query tests</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
print "LOGFILE NAME: " . $db->log->getLogFile() . "<br>";
@@ -549,11 +550,13 @@ print "</pre>";
print "<b>PREPARE QUERIES</b><br>";
// READ PREPARE
$q_prep = "SELECT test_foo_id, test, some_bool, string_a, number_a, "
. "number_a_numeric, some_time "
. "FROM test_foo "
. "WHERE test = $1 "
. "ORDER BY test_foo_id DESC LIMIT 5";
$q_prep = <<<SQL
SELECT test_foo_id, test, some_bool, string_a, number_a,
number_a_numeric, some_time
FROM test_foo
WHERE test = $1
ORDER BY test_foo_id DESC LIMIT 5
SQL;
if ($db->dbPrepare('sel_test_foo', $q_prep) === false) {
print "Error in sel_test_foo prepare<br>";
} else {
@@ -681,16 +684,27 @@ echo "<hr>";
$db_pgb = new CoreLibs\DB\IO($DB_CONFIG['test_pgbouncer'] ?? [], $log);
print "[PGB] DBINFO: " . $db_pgb->dbInfo() . "<br>";
if ($db->dbPrepare('pgb_sel_test_foo', $q_prep) === false) {
print "[PGB] [1] Error in pgb_sel_test_foo prepare<br>";
print "[PGB] [1] Warning in pgb_sel_test_foo prepare<br>";
} else {
print "[PGB] [1] pgb_sel_test_foo prepare OK<br>";
}
// second prepare
if ($db->dbPrepare('pgb_sel_test_foo', $q_prep) === false) {
print "[PGB] [2] Error in pgb_sel_test_foo prepare<br>";
print "[PGB] [2] Warning in pgb_sel_test_foo prepare<br>";
} else {
print "[PGB] [2] pgb_sel_test_foo prepare OK<br>";
}
// same statment name, different query
if (
$db->dbPrepare('pgb_sel_test_foo', <<<SQL
SELECT * FROM test_foo WHERE test = $1
ORDER BY test_foo_id DESC LIMIT 5
SQL) === false
) {
print "[PGB] [3] Error in pgb_sel_test_foo prepare<br>";
} else {
print "[PGB] [3] pgb_sel_test_foo prepare OK<br>";
}
$db_pgb->dbClose();
# db write class test