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
This commit is contained in:
@@ -156,7 +156,7 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
$db->dbExec("DROP TABLE test_meta");
|
$db->dbExec("DROP TABLE test_meta");
|
||||||
}
|
}
|
||||||
// uid is for internal reference tests
|
// uid is for internal reference tests
|
||||||
$base_table = <<<EOM
|
$base_table = <<<SQL
|
||||||
uid VARCHAR,
|
uid VARCHAR,
|
||||||
row_int INT,
|
row_int INT,
|
||||||
row_numeric NUMERIC,
|
row_numeric NUMERIC,
|
||||||
@@ -172,36 +172,36 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
row_array_varchar VARCHAR ARRAY
|
row_array_varchar VARCHAR ARRAY
|
||||||
)
|
)
|
||||||
WITHOUT OIDS
|
WITHOUT OIDS
|
||||||
EOM;
|
SQL;
|
||||||
// create the tables
|
// create the tables
|
||||||
$db->dbExec(
|
$db->dbExec(
|
||||||
// primary key name is table + '_id'
|
// primary key name is table + '_id'
|
||||||
<<<EOM
|
<<<SQL
|
||||||
CREATE TABLE table_with_primary_key (
|
CREATE TABLE table_with_primary_key (
|
||||||
table_with_primary_key_id SERIAL PRIMARY KEY,
|
table_with_primary_key_id SERIAL PRIMARY KEY,
|
||||||
$base_table
|
$base_table
|
||||||
EOM
|
SQL
|
||||||
/* "CREATE TABLE table_with_primary_key ("
|
/* "CREATE TABLE table_with_primary_key ("
|
||||||
// primary key name is table + '_id'
|
// primary key name is table + '_id'
|
||||||
. "table_with_primary_key_id SERIAL PRIMARY KEY, "
|
. "table_with_primary_key_id SERIAL PRIMARY KEY, "
|
||||||
. $base_table */
|
. $base_table */
|
||||||
);
|
);
|
||||||
$db->dbExec(
|
$db->dbExec(
|
||||||
<<<EOM
|
<<<SQL
|
||||||
CREATE TABLE table_without_primary_key (
|
CREATE TABLE table_without_primary_key (
|
||||||
$base_table
|
$base_table
|
||||||
EOM
|
SQL
|
||||||
/* "CREATE TABLE table_without_primary_key ("
|
/* "CREATE TABLE table_without_primary_key ("
|
||||||
. $base_table */
|
. $base_table */
|
||||||
);
|
);
|
||||||
// create simple table for meta test
|
// create simple table for meta test
|
||||||
$db->dbExec(
|
$db->dbExec(
|
||||||
<<<EOM
|
<<<SQL
|
||||||
CREATE TABLE test_meta (
|
CREATE TABLE test_meta (
|
||||||
row_1 VARCHAR,
|
row_1 VARCHAR,
|
||||||
row_2 INT
|
row_2 INT
|
||||||
) WITHOUT OIDS
|
) WITHOUT OIDS
|
||||||
EOM
|
SQL
|
||||||
/* "CREATE TABLE test_meta ("
|
/* "CREATE TABLE test_meta ("
|
||||||
. "row_1 VARCHAR, "
|
. "row_1 VARCHAR, "
|
||||||
. "row_2 INT"
|
. "row_2 INT"
|
||||||
@@ -1342,10 +1342,10 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
'has default' => false,
|
'has default' => false,
|
||||||
'array dims' => 0,
|
'array dims' => 0,
|
||||||
'is enum' => false,
|
'is enum' => false,
|
||||||
'is base' => 1,
|
'is base' => true,
|
||||||
'is composite' => false,
|
'is composite' => false,
|
||||||
'is pesudo' => false,
|
|
||||||
'description' => '',
|
'description' => '',
|
||||||
|
'is pseudo' => false
|
||||||
],
|
],
|
||||||
'row_2' => [
|
'row_2' => [
|
||||||
'num' => 2,
|
'num' => 2,
|
||||||
@@ -1355,10 +1355,10 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
'has default' => false,
|
'has default' => false,
|
||||||
'array dims' => 0,
|
'array dims' => 0,
|
||||||
'is enum' => false,
|
'is enum' => false,
|
||||||
'is base' => 1,
|
'is base' => true,
|
||||||
'is composite' => false,
|
'is composite' => false,
|
||||||
'is pesudo' => false,
|
|
||||||
'description' => '',
|
'description' => '',
|
||||||
|
'is pseudo' => false
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@@ -1374,10 +1374,10 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
'has default' => false,
|
'has default' => false,
|
||||||
'array dims' => 0,
|
'array dims' => 0,
|
||||||
'is enum' => false,
|
'is enum' => false,
|
||||||
'is base' => 1,
|
'is base' => true,
|
||||||
'is composite' => false,
|
'is composite' => false,
|
||||||
'is pesudo' => false,
|
|
||||||
'description' => '',
|
'description' => '',
|
||||||
|
'is pseudo' => false
|
||||||
],
|
],
|
||||||
'row_2' => [
|
'row_2' => [
|
||||||
'num' => 2,
|
'num' => 2,
|
||||||
@@ -1387,10 +1387,10 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
'has default' => false,
|
'has default' => false,
|
||||||
'array dims' => 0,
|
'array dims' => 0,
|
||||||
'is enum' => false,
|
'is enum' => false,
|
||||||
'is base' => 1,
|
'is base' => true,
|
||||||
'is composite' => false,
|
'is composite' => false,
|
||||||
'is pesudo' => false,
|
|
||||||
'description' => '',
|
'description' => '',
|
||||||
|
'is pseudo' => false
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@@ -4425,16 +4425,16 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
// same but as EOM
|
// same but as heredoc
|
||||||
'single insert (PK), EOM string' => [
|
'single insert (PK), heredoc string' => [
|
||||||
<<<EOM
|
<<<SQL
|
||||||
INSERT INTO table_with_primary_key (
|
INSERT INTO table_with_primary_key (
|
||||||
row_varchar, row_varchar_literal, row_int, row_date
|
row_varchar, row_varchar_literal, row_int, row_date
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'Text', 'Other', 123, '2022-03-01'
|
'Text', 'Other', 123, '2022-03-01'
|
||||||
)
|
)
|
||||||
RETURNING row_varchar, row_varchar_literal, row_int, row_date
|
RETURNING row_varchar, row_varchar_literal, row_int, row_date
|
||||||
EOM,
|
SQL,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
@@ -4529,16 +4529,16 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
// same as above but as EOM string
|
// same as above but as heredoc string
|
||||||
'single insert (No PK), EOM string' => [
|
'single insert (No PK), heredoc string' => [
|
||||||
<<<EOM
|
<<<SQL
|
||||||
INSERT INTO table_without_primary_key (
|
INSERT INTO table_without_primary_key (
|
||||||
row_varchar, row_varchar_literal, row_int, row_date
|
row_varchar, row_varchar_literal, row_int, row_date
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'Text', 'Other', 123, '2022-03-01'
|
'Text', 'Other', 123, '2022-03-01'
|
||||||
)
|
)
|
||||||
RETURNING row_varchar, row_varchar_literal, row_int, row_date
|
RETURNING row_varchar, row_varchar_literal, row_int, row_date
|
||||||
EOM,
|
SQL,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|||||||
@@ -141,14 +141,14 @@ var_dump($db->dbGetReturningExt());
|
|||||||
|
|
||||||
// same as above but use an EOM string
|
// same as above but use an EOM string
|
||||||
$some_time = time();
|
$some_time = time();
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
INSERT INTO test_foo (
|
INSERT INTO test_foo (
|
||||||
test, number_a
|
test, number_a
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'EOM FOO TEST $some_time', 1
|
'EOM FOO TEST $some_time', 1
|
||||||
)
|
)
|
||||||
RETURNING test, number_a
|
RETURNING test, number_a
|
||||||
EOM;
|
SQL;
|
||||||
$status = $db->dbExec($query);
|
$status = $db->dbExec($query);
|
||||||
print "EOM STRING DIRECT INSERT STATUS: " . Support::printToString($status) . " |<br>"
|
print "EOM STRING DIRECT INSERT STATUS: " . Support::printToString($status) . " |<br>"
|
||||||
. "QUERY: " . $db->dbGetQuery() . " |<br>"
|
. "QUERY: " . $db->dbGetQuery() . " |<br>"
|
||||||
@@ -167,21 +167,21 @@ print "DIRECT INSERT PREVIOUS INSERTED: "
|
|||||||
. print_r($db->dbReturnRow("SELECT test_foo_id, test FROM test_foo "
|
. print_r($db->dbReturnRow("SELECT test_foo_id, test FROM test_foo "
|
||||||
. "WHERE test_foo_id = " . (int)$last_insert_pk), true) . "<br>";
|
. "WHERE test_foo_id = " . (int)$last_insert_pk), true) . "<br>";
|
||||||
$__last_insert_pk = (int)$last_insert_pk;
|
$__last_insert_pk = (int)$last_insert_pk;
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
SELECT
|
SELECT
|
||||||
test_foo_id, test
|
test_foo_id, test
|
||||||
FROM test_foo
|
FROM test_foo
|
||||||
WHERE test_foo_id = $__last_insert_pk;
|
WHERE test_foo_id = $__last_insert_pk;
|
||||||
EOM;
|
SQL;
|
||||||
print "EOM READ OF PREVIOUS INSERTED: " . print_r($db->dbReturnRow($query), true) . "<br>";
|
print "EOM READ OF PREVIOUS INSERTED: " . print_r($db->dbReturnRow($query), true) . "<br>";
|
||||||
print "LAST ERROR: " . $db->dbGetLastError() . "<br>";
|
print "LAST ERROR: " . $db->dbGetLastError() . "<br>";
|
||||||
print "<br>";
|
print "<br>";
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
SELECT
|
SELECT
|
||||||
test_foo_id, test
|
test_foo_id, test
|
||||||
FROM test_foo
|
FROM test_foo
|
||||||
WHERE test_foo_id = $1;
|
WHERE test_foo_id = $1;
|
||||||
EOM;
|
SQL;
|
||||||
print "RETURN ROW PARAMS: " . print_r(
|
print "RETURN ROW PARAMS: " . print_r(
|
||||||
$db->dbReturnRowParams(
|
$db->dbReturnRowParams(
|
||||||
$query,
|
$query,
|
||||||
@@ -208,7 +208,7 @@ foreach (['pk_name', 'count', 'query', 'returning_id'] as $key) {
|
|||||||
print "KEY: " . $key . ': ' . $db->dbGetPrepareCursorValue('ins_test_foo', $key) . "<br>";
|
print "KEY: " . $key . ': ' . $db->dbGetPrepareCursorValue('ins_test_foo', $key) . "<br>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
test_foo
|
test_foo
|
||||||
(
|
(
|
||||||
@@ -217,7 +217,7 @@ INSERT INTO
|
|||||||
$1, '$2'
|
$1, '$2'
|
||||||
)
|
)
|
||||||
RETURNING test, string_a
|
RETURNING test, string_a
|
||||||
EOM;
|
SQL;
|
||||||
$db->dbPrepare("ins_test_foo_eom", $query);
|
$db->dbPrepare("ins_test_foo_eom", $query);
|
||||||
$status = $db->dbExecute("ins_test_foo_eom", ['EOM BAR TEST ' . time()]);
|
$status = $db->dbExecute("ins_test_foo_eom", ['EOM BAR TEST ' . time()]);
|
||||||
print "EOM STRING PREPARE INSERT[ins_test_foo_eom] STATUS: " . Support::printToString($status) . " |<br>"
|
print "EOM STRING PREPARE INSERT[ins_test_foo_eom] STATUS: " . Support::printToString($status) . " |<br>"
|
||||||
@@ -235,7 +235,7 @@ print "EOM STRING EXEC PARAMS INSERT STATUS: " . Support::printToString($status)
|
|||||||
. "RETURNING RETURN: " . print_r($db->dbGetReturningArray(), true) . "<br>";
|
. "RETURNING RETURN: " . print_r($db->dbGetReturningArray(), true) . "<br>";
|
||||||
|
|
||||||
// I/S Query
|
// I/S Query
|
||||||
$query_insert = <<<EOM
|
$query_insert = <<<SQL
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
test_foo
|
test_foo
|
||||||
(
|
(
|
||||||
@@ -246,8 +246,8 @@ INSERT INTO
|
|||||||
$6, $7, $8
|
$6, $7, $8
|
||||||
)
|
)
|
||||||
RETURNING test
|
RETURNING test
|
||||||
EOM;
|
SQL;
|
||||||
$query_select = <<<EOM
|
$query_select = <<<SQL
|
||||||
SELECT
|
SELECT
|
||||||
test, some_bool, string_a, number_a, number_a_numeric,
|
test, some_bool, string_a, number_a, number_a_numeric,
|
||||||
some_time, some_time, some_timestamp, json_string
|
some_time, some_time, some_timestamp, json_string
|
||||||
@@ -255,7 +255,7 @@ FROM
|
|||||||
test_foo
|
test_foo
|
||||||
WHERE
|
WHERE
|
||||||
test_foo_id = $1
|
test_foo_id = $1
|
||||||
EOM;
|
SQL;
|
||||||
// A
|
// A
|
||||||
$status = $db->dbExecParams(
|
$status = $db->dbExecParams(
|
||||||
$query_insert,
|
$query_insert,
|
||||||
@@ -313,18 +313,51 @@ print "EOM STRING EXEC RETURN TEST: " . print_r(
|
|||||||
[$__last_insert_id]
|
[$__last_insert_id]
|
||||||
)
|
)
|
||||||
) . "<br>";
|
) . "<br>";
|
||||||
|
// params > 10 for debug
|
||||||
|
// error catcher
|
||||||
|
$query_insert = <<<SQL
|
||||||
|
INSERT INTO many_columns (
|
||||||
|
col_01_int,
|
||||||
|
col_01, col_02, col_03, col_04, col_05, col_06, col_07, col_08, col_09,
|
||||||
|
col_10, col_11, col_12, col_02_int
|
||||||
|
) VALUES (
|
||||||
|
1,
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9,
|
||||||
|
$10, $11, $12, $13
|
||||||
|
)
|
||||||
|
RETURNING
|
||||||
|
many_columns_id,
|
||||||
|
col_01_int,
|
||||||
|
col_01, col_02, col_03, col_04, col_05, col_06, col_07, col_08, col_09,
|
||||||
|
col_10, col_11, col_12, col_02_int
|
||||||
|
SQL;
|
||||||
|
// will fail with "NULL" string on int
|
||||||
|
$query_params = [
|
||||||
|
'col 1', 'col 2', 'col 3', 'col 4', 'col 5', 'col 6', 'col 7', 'col 8',
|
||||||
|
'col 9',
|
||||||
|
'col 10', 'col 11', 'col 12', "NULL"
|
||||||
|
];
|
||||||
|
$status = $db->dbExecParams($query_insert, $query_params);
|
||||||
|
echo "<b>*</b><br>";
|
||||||
|
echo "EOM STRING WITH MORE THAN 10 PARAMETERS: "
|
||||||
|
. Support::printToString($query_params) . " |<br>"
|
||||||
|
. " |<br>"
|
||||||
|
. "PRIMARY KEY: " . Support::printToString($db->dbGetInsertPK()) . " | "
|
||||||
|
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
|
||||||
|
. "RETURNING RETURN: " . print_r($db->dbGetReturningArray(), true)
|
||||||
|
. "ERROR: " . $db->dbGetLastError(true) . "<br>";
|
||||||
echo "<hr>";
|
echo "<hr>";
|
||||||
// binary insert tests
|
// binary insert tests
|
||||||
$filename = $db->dbEscapeLiteral('class_test.db.php');
|
$filename = $db->dbEscapeLiteral('class_test.db.php');
|
||||||
$rand_bin_uid = $db->dbEscapeLiteral(\CoreLibs\Create\Uids::uniqIdShort());
|
$rand_bin_uid = $db->dbEscapeLiteral(\CoreLibs\Create\Uids::uniqIdShort());
|
||||||
$binary_data = $db->dbEscapeBytea(file_get_contents('class_test.db.php') ?: '');
|
$binary_data = $db->dbEscapeBytea(file_get_contents('class_test.db.php') ?: '');
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
INSERT INTO binary_test (
|
INSERT INTO binary_test (
|
||||||
filename, uid, binary_data
|
filename, uid, binary_data
|
||||||
) VALUES (
|
) VALUES (
|
||||||
$filename, $rand_bin_uid, '$binary_data'
|
$filename, $rand_bin_uid, '$binary_data'
|
||||||
)
|
)
|
||||||
EOM;
|
SQL;
|
||||||
$status = $db->dbExec($query);
|
$status = $db->dbExec($query);
|
||||||
$__last_insert_id = $db->dbGetInsertPK();
|
$__last_insert_id = $db->dbGetInsertPK();
|
||||||
print "BINARY DATA INSERT: "
|
print "BINARY DATA INSERT: "
|
||||||
@@ -336,13 +369,13 @@ print "BINARY DATA INSERT: "
|
|||||||
. "ERROR: " . $db->dbGetLastError(true) . "<br>";
|
. "ERROR: " . $db->dbGetLastError(true) . "<br>";
|
||||||
|
|
||||||
echo "<b>*</b><br>";
|
echo "<b>*</b><br>";
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
INSERT INTO binary_test (
|
INSERT INTO binary_test (
|
||||||
filename, uid, binary_data
|
filename, uid, binary_data
|
||||||
) VALUES (
|
) VALUES (
|
||||||
$1, $2, $3
|
$1, $2, $3
|
||||||
)
|
)
|
||||||
EOM;
|
SQL;
|
||||||
$status = $db->dbExecParams($query, [$filename, $rand_bin_uid, $binary_data]);
|
$status = $db->dbExecParams($query, [$filename, $rand_bin_uid, $binary_data]);
|
||||||
$__last_insert_id = $db->dbGetInsertPK();
|
$__last_insert_id = $db->dbGetInsertPK();
|
||||||
print "BINARY DATA INSERT PARAMS: "
|
print "BINARY DATA INSERT PARAMS: "
|
||||||
@@ -380,7 +413,7 @@ print "DIRECT MULTIPLE INSERT WITH RETURN STATUS: " . Support::printToString($st
|
|||||||
$t_1 = time();
|
$t_1 = time();
|
||||||
$t_2 = time();
|
$t_2 = time();
|
||||||
$t_3 = time();
|
$t_3 = time();
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
INSERT INTO test_foo (
|
INSERT INTO test_foo (
|
||||||
test
|
test
|
||||||
) VALUES
|
) VALUES
|
||||||
@@ -388,7 +421,7 @@ INSERT INTO test_foo (
|
|||||||
('EOM BAR 2 $t_2'),
|
('EOM BAR 2 $t_2'),
|
||||||
('EOM BAR 3 $t_3')
|
('EOM BAR 3 $t_3')
|
||||||
RETURNING test_foo_id, test
|
RETURNING test_foo_id, test
|
||||||
EOM;
|
SQL;
|
||||||
$status = $db->dbExec($query);
|
$status = $db->dbExec($query);
|
||||||
print "EOM STRING DIRECT MULTIPLE INSERT WITH RETURN STATUS: " . Support::printToString($status) . " |<br>"
|
print "EOM STRING DIRECT MULTIPLE INSERT WITH RETURN STATUS: " . Support::printToString($status) . " |<br>"
|
||||||
. "QUERY: " . $db->dbGetQuery() . " |<br>"
|
. "QUERY: " . $db->dbGetQuery() . " |<br>"
|
||||||
@@ -422,7 +455,7 @@ print "UPDATE WITH PK " . Support::printToString($last_insert_pk)
|
|||||||
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "<br>";
|
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "<br>";
|
||||||
// UPDATE BUT EOM STYLE
|
// UPDATE BUT EOM STYLE
|
||||||
$status = $db->dbExecParams(
|
$status = $db->dbExecParams(
|
||||||
<<<EOM
|
<<<SQL
|
||||||
UPDATE
|
UPDATE
|
||||||
test_foo
|
test_foo
|
||||||
SET
|
SET
|
||||||
@@ -432,7 +465,7 @@ $status = $db->dbExecParams(
|
|||||||
tset_foo_id = ?
|
tset_foo_id = ?
|
||||||
RETURNING
|
RETURNING
|
||||||
test_foo.test, string_a
|
test_foo.test, string_a
|
||||||
EOM,
|
SQL,
|
||||||
['SOMETHING DIFFERENT EOM', (string)rand(1, 100)]
|
['SOMETHING DIFFERENT EOM', (string)rand(1, 100)]
|
||||||
);
|
);
|
||||||
print "UPDATE EOM WITH PK " . Support::printToString($last_insert_pk)
|
print "UPDATE EOM WITH PK " . Support::printToString($last_insert_pk)
|
||||||
@@ -524,27 +557,27 @@ echo "<hr>";
|
|||||||
print "EOM STYLE STRINGS<br>";
|
print "EOM STYLE STRINGS<br>";
|
||||||
$test_bar = $db->dbEscapeLiteral('SOMETHING DIFFERENT');
|
$test_bar = $db->dbEscapeLiteral('SOMETHING DIFFERENT');
|
||||||
// Test EOM block
|
// Test EOM block
|
||||||
$q = <<<EOM
|
$q = <<<SQL
|
||||||
SELECT test_foo_id, test, some_bool, string_a, number_a,
|
SELECT test_foo_id, test, some_bool, string_a, number_a,
|
||||||
-- comment
|
-- comment
|
||||||
number_a_numeric, some_time
|
number_a_numeric, some_time
|
||||||
FROM test_foo
|
FROM test_foo
|
||||||
WHERE test = $test_bar
|
WHERE test = $test_bar
|
||||||
ORDER BY test_foo_id DESC LIMIT 5
|
ORDER BY test_foo_id DESC LIMIT 5
|
||||||
EOM;
|
SQL;
|
||||||
while (is_array($res = $db->dbReturn($q))) {
|
while (is_array($res = $db->dbReturn($q))) {
|
||||||
print "ROW: <pre>" . print_r($res, true) . "</pre><br>";
|
print "ROW: <pre>" . print_r($res, true) . "</pre><br>";
|
||||||
}
|
}
|
||||||
echo "<hr>";
|
echo "<hr>";
|
||||||
print "DB RETURN PARAMS<br>";
|
print "DB RETURN PARAMS<br>";
|
||||||
$q = <<<EOM
|
$q = <<<SQL
|
||||||
SELECT test_foo_id, test, some_bool, string_a, number_a,
|
SELECT test_foo_id, test, some_bool, string_a, number_a,
|
||||||
-- comment
|
-- comment
|
||||||
number_a_numeric, some_time
|
number_a_numeric, some_time
|
||||||
FROM test_foo
|
FROM test_foo
|
||||||
WHERE test = $1
|
WHERE test = $1
|
||||||
ORDER BY test_foo_id DESC LIMIT 5
|
ORDER BY test_foo_id DESC LIMIT 5
|
||||||
EOM;
|
SQL;
|
||||||
while (
|
while (
|
||||||
is_array($res = $db->dbReturnParams($q, ['SOMETHING DIFFERENT']))
|
is_array($res = $db->dbReturnParams($q, ['SOMETHING DIFFERENT']))
|
||||||
) {
|
) {
|
||||||
@@ -632,14 +665,14 @@ print "Wrote to DB tabel $table with data " . print_r($data, true) . " and got p
|
|||||||
$query = "SELECT type, sdate, integer FROM foobar";
|
$query = "SELECT type, sdate, integer FROM foobar";
|
||||||
$data = $db->dbReturnArray($query, true);
|
$data = $db->dbReturnArray($query, true);
|
||||||
print "RETURN ARRAY: " . $db->dbGetNumRows() . ", Full foobar list: <br><pre>" . print_r($data, true) . "</pre><br>";
|
print "RETURN ARRAY: " . $db->dbGetNumRows() . ", Full foobar list: <br><pre>" . print_r($data, true) . "</pre><br>";
|
||||||
$query = <<<EOM
|
$query = <<<SQL
|
||||||
SELECT
|
SELECT
|
||||||
type, sdate
|
type, sdate
|
||||||
FROM
|
FROM
|
||||||
foobar
|
foobar
|
||||||
WHERE
|
WHERE
|
||||||
type = $1
|
type = $1
|
||||||
EOM;
|
SQL;
|
||||||
$data = $db->dbReturnArrayParams($query, ['schmalz'], true);
|
$data = $db->dbReturnArrayParams($query, ['schmalz'], true);
|
||||||
print "RETURN ARRAY PARAMS: " . $db->dbGetNumRows() . ", Full foobar list: <br><pre>"
|
print "RETURN ARRAY PARAMS: " . $db->dbGetNumRows() . ", Full foobar list: <br><pre>"
|
||||||
. print_r($data, true) . "</pre><br>";
|
. print_r($data, true) . "</pre><br>";
|
||||||
|
|||||||
105
www/admin/class_test.db.single.php
Normal file
105
www/admin/class_test.db.single.php
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
<?php // phpcs:ignore warning
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phan-file-suppress PhanTypeSuspiciousStringExpression
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
// turn on all error reporting
|
||||||
|
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
// basic class test file
|
||||||
|
define('USE_DATABASE', true);
|
||||||
|
// sample config
|
||||||
|
require 'config.php';
|
||||||
|
// override ECHO ALL FALSE
|
||||||
|
$ECHO_ALL = true;
|
||||||
|
// define log file id
|
||||||
|
$LOG_FILE_ID = 'classTest-db-single';
|
||||||
|
ob_end_flush();
|
||||||
|
|
||||||
|
use CoreLibs\Debug\Support as DgS;
|
||||||
|
use CoreLibs\DB\IO as DbIo;
|
||||||
|
use CoreLibs\Debug\Support;
|
||||||
|
use CoreLibs\Convert\SetVarType;
|
||||||
|
|
||||||
|
$log = new CoreLibs\Debug\Logging([
|
||||||
|
'log_folder' => BASE . LOG,
|
||||||
|
'file_id' => $LOG_FILE_ID,
|
||||||
|
// add file date
|
||||||
|
'print_file_date' => true,
|
||||||
|
// set debug and print flags
|
||||||
|
'debug_all' => $DEBUG_ALL ?? true,
|
||||||
|
'echo_all' => $ECHO_ALL,
|
||||||
|
'print_all' => $PRINT_ALL ?? true,
|
||||||
|
]);
|
||||||
|
// db connection and attach logger
|
||||||
|
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
|
||||||
|
$db->log->debug('START', '=============================>');
|
||||||
|
|
||||||
|
$PAGE_NAME = 'TEST CLASS: DB SINGLE';
|
||||||
|
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.dbReturn.php">Class Test DB dbReturn</a></div>';
|
||||||
|
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||||
|
|
||||||
|
print "LOGFILE NAME: " . $db->log->getSetting('log_file_name') . "<br>";
|
||||||
|
print "LOGFILE ID: " . $db->log->getSetting('log_file_id') . "<br>";
|
||||||
|
print "DBINFO: " . $db->dbInfo() . "<br>";
|
||||||
|
// DB client encoding
|
||||||
|
print "DB client encoding: " . $db->dbGetEncoding() . "<br>";
|
||||||
|
print "DB search path: " . $db->dbGetSchema() . "<br>";
|
||||||
|
|
||||||
|
$to_db_version = '15.2';
|
||||||
|
print "VERSION DB: " . $db->dbVersion() . "<br>";
|
||||||
|
print "SERVER ENCODING: " . $db->dbVersionInfo('server_encoding') . "<br>";
|
||||||
|
if (($dbh = $db->dbGetDbh()) instanceof \PgSql\Connection) {
|
||||||
|
print "ALL OUTPUT [TEST]: <pre>" . print_r(pg_version($dbh), true) . "</pre><br>";
|
||||||
|
} else {
|
||||||
|
print "NO DB HANDLER<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// params > 10 for debug
|
||||||
|
// error catcher
|
||||||
|
$query_insert = <<<SQL
|
||||||
|
INSERT INTO many_columns (
|
||||||
|
col_01_int,
|
||||||
|
col_01, col_02, col_03, col_04, col_05, col_06, col_07, col_08, col_09,
|
||||||
|
col_10, col_11, col_12, col_02_int
|
||||||
|
) VALUES (
|
||||||
|
1,
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9,
|
||||||
|
$10, $11, $12, $13
|
||||||
|
)
|
||||||
|
RETURNING
|
||||||
|
many_columns_id,
|
||||||
|
col_01_int,
|
||||||
|
col_01, col_02, col_03, col_04, col_05, col_06, col_07, col_08, col_09,
|
||||||
|
col_10, col_11, col_12, col_02_int
|
||||||
|
SQL;
|
||||||
|
$query_params = [
|
||||||
|
'col 1', 'col 2', 'col 3', 'col 4', 'col 5', 'col 6', 'col 7', 'col 8',
|
||||||
|
'col 9', 'col 10', 'col 11', 'col 12', null
|
||||||
|
];
|
||||||
|
$status = $db->dbExecParams($query_insert, $query_params);
|
||||||
|
echo "<b>*</b><br>";
|
||||||
|
echo "EOM STRING WITH MORE THAN 10 PARAMETERS: "
|
||||||
|
. Support::printToString($query_params) . " |<br>"
|
||||||
|
. " |<br>"
|
||||||
|
. "PRIMARY KEY: " . Support::printToString($db->dbGetInsertPK()) . " | "
|
||||||
|
// . "RETURNING EXT: " . Support::printToString($db->dbGetReturningExt()) . " | "
|
||||||
|
. "RETURNING RETURN: " . Support::printToString($db->dbGetReturningArray())
|
||||||
|
. "ERROR: " . $db->dbGetLastError(true) . "<br>";
|
||||||
|
echo "<hr>";
|
||||||
|
|
||||||
|
// error message
|
||||||
|
print $log->printErrorMsg();
|
||||||
|
|
||||||
|
print "</body></html>";
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -73,6 +73,7 @@ print "<html><head><title>TEST CLASS</title><head>";
|
|||||||
print "<body>";
|
print "<body>";
|
||||||
|
|
||||||
print '<div><a href="class_test.db.php">Class Test: DB</a></div>';
|
print '<div><a href="class_test.db.php">Class Test: DB</a></div>';
|
||||||
|
print '<div><a href="class_test.db.single.php">Class Test: DB SINGLE</a></div>';
|
||||||
print '<div><a href="class_test.db.dbReturn.php">Class Test: DB dbReturn</a></div>';
|
print '<div><a href="class_test.db.dbReturn.php">Class Test: DB dbReturn</a></div>';
|
||||||
print '<div><a href="class_test.convert.colors.php">Class Test: CONVERT COLORS</a></div>';
|
print '<div><a href="class_test.convert.colors.php">Class Test: CONVERT COLORS</a></div>';
|
||||||
print '<div><a href="class_test.check.colors.php">Class Test: CHECK COLORS</a></div>';
|
print '<div><a href="class_test.check.colors.php">Class Test: CHECK COLORS</a></div>';
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ if (file_exists(BASE . CONFIGS . 'config.path.php')) {
|
|||||||
// live frontend pages
|
// live frontend pages
|
||||||
// ** missing live domains **
|
// ** missing live domains **
|
||||||
// get the name without the port
|
// get the name without the port
|
||||||
list($HOST_NAME) = array_pad(explode(':', $_SERVER['HTTP_HOST'], 2), 2, null);
|
[$HOST_NAME] = array_pad(explode(':', $_SERVER['HTTP_HOST'], 2), 2, null);
|
||||||
// set HOST name
|
// set HOST name
|
||||||
define('HOST_NAME', $HOST_NAME);
|
define('HOST_NAME', $HOST_NAME);
|
||||||
// BAIL ON MISSING MASTER SITE CONFIG
|
// BAIL ON MISSING MASTER SITE CONFIG
|
||||||
|
|||||||
@@ -1608,7 +1608,7 @@ class Login
|
|||||||
// TODO: submit or JS to set target page as ajax call
|
// TODO: submit or JS to set target page as ajax call
|
||||||
// NOTE: for the HTML block I ignore line lengths
|
// NOTE: for the HTML block I ignore line lengths
|
||||||
// phpcs:disable
|
// 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;">
|
<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>
|
<table>
|
||||||
<tr><td class="norm" align="center" colspan="2"><h3>{TITLE_PASSWORD_CHANGE}</h3></td></tr>
|
<tr><td class="norm" align="center" colspan="2"><h3>{TITLE_PASSWORD_CHANGE}</h3></td></tr>
|
||||||
@@ -1626,7 +1626,7 @@ class Login
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{PASSWORD_CHANGE_SHOW}
|
{PASSWORD_CHANGE_SHOW}
|
||||||
EOM;
|
HTML;
|
||||||
// phpcs:enable
|
// phpcs:enable
|
||||||
}
|
}
|
||||||
if ($this->password_forgot) {
|
if ($this->password_forgot) {
|
||||||
@@ -1650,7 +1650,7 @@ EOM;
|
|||||||
// now check templates
|
// now check templates
|
||||||
// TODO: submit or JS to set target page as ajax call
|
// TODO: submit or JS to set target page as ajax call
|
||||||
if (!$this->login_template['template']) {
|
if (!$this->login_template['template']) {
|
||||||
$this->login_template['template'] = <<<EOM
|
$this->login_template['template'] = <<<HTML
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{LANGUAGE}">
|
<html lang="{LANGUAGE}">
|
||||||
<head>
|
<head>
|
||||||
@@ -1712,7 +1712,7 @@ h3 { font-size: 18px; }
|
|||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
EOM;
|
HTML;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -735,7 +735,10 @@ class IO
|
|||||||
*/
|
*/
|
||||||
private function __dbErrorPreprocessor(\PgSql\Result|false $cursor = false): array
|
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,
|
// 1 = self/__dbErrorPreprocessor, 2 = __dbError, __dbWarning,
|
||||||
// 3+ == actual source
|
// 3+ == actual source
|
||||||
// loop until we get a null, build where called chain
|
// loop until we get a null, build where called chain
|
||||||
@@ -749,16 +752,31 @@ class IO
|
|||||||
if ($where_called === null) {
|
if ($where_called === null) {
|
||||||
$where_called = '[Unknown Method]';
|
$where_called = '[Unknown Method]';
|
||||||
}
|
}
|
||||||
|
[$db_prefix_last, $db_error_string_last] = $this->db_functions->__dbPrintLastError();
|
||||||
if ($cursor !== false) {
|
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')) {
|
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) {
|
// prefix the master if not the same
|
||||||
$this->__dbDebug('db', $pg_error_string, 'DB_ERROR', $where_called);
|
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,
|
// because the placeholders start with $ and at 1,
|
||||||
// we need to increase each key and prefix it with a $ char
|
// we need to increase each key and prefix it with a $ char
|
||||||
for ($i = 0, $iMax = count($keys); $i < $iMax; $i++) {
|
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
|
// 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]
|
'"NULL"' : (string)$data[$i]
|
||||||
);
|
);
|
||||||
|
// search part
|
||||||
|
$keys[$i] = '$' . ($keys[$i] + 1);
|
||||||
}
|
}
|
||||||
// simply replace the $1, $2, ... with the actual data and return it
|
// simply replace the $1, $2, ... with the actual data and return it
|
||||||
return str_replace(
|
return str_replace(
|
||||||
|
|||||||
@@ -209,10 +209,17 @@ interface SqlFunctions
|
|||||||
/**
|
/**
|
||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @param \PgSql\Result|false $cursor
|
* @return array{0:string,1:string}
|
||||||
* @return 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
|
* Undocumented function
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ class PgSQL implements Interface\SqlFunctions
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
private $last_error_query;
|
private $last_error_query;
|
||||||
/** @var \PgSql\Connection|false */
|
/** @var \PgSql\Connection|false */
|
||||||
private $dbh;
|
private $dbh = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* queries last error query and returns true or false if error was set
|
* 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;
|
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
|
* reads the last error for this cursor and returns
|
||||||
* html formatted string with error name
|
* html formatted string with error name
|
||||||
*
|
*
|
||||||
* @param \PgSql\Result|false $cursor cursor
|
* @param \PgSql\Result|false $cursor cursor
|
||||||
* or null
|
* or null
|
||||||
* @return string error string
|
* @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)) {
|
if (is_bool($this->dbh)) {
|
||||||
return '';
|
return ['', ''];
|
||||||
}
|
}
|
||||||
// run the query again for the error result here
|
// run the query again for the error result here
|
||||||
if ((is_bool($cursor)) && $this->last_error_query) {
|
if ((is_bool($cursor)) && $this->last_error_query) {
|
||||||
@@ -552,10 +571,12 @@ class PgSQL implements Interface\SqlFunctions
|
|||||||
$cursor = pg_get_result($this->dbh);
|
$cursor = pg_get_result($this->dbh);
|
||||||
}
|
}
|
||||||
if ($cursor && $error_str = pg_result_error($cursor)) {
|
if ($cursor && $error_str = pg_result_error($cursor)) {
|
||||||
return '-PostgreSQL-Error- '
|
return [
|
||||||
. $error_str;
|
'-PostgreSQL-Error-',
|
||||||
|
$error_str
|
||||||
|
];
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return ['', ''];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user