From 54541332396c8a59c13f26b92be00f08fe189ea7 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 22 Nov 2024 17:25:22 +0900 Subject: [PATCH] Update SQL\PgSQL with param calls and heredoc, primary key search method update The primary key currval select is udpated to use proper calls so it works with serial and identity columns --- 4dev/tests/DB/CoreLibsDBIOTest.php | 7 +-- www/lib/CoreLibs/DB/SQL/PgSQL.php | 89 +++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/4dev/tests/DB/CoreLibsDBIOTest.php b/4dev/tests/DB/CoreLibsDBIOTest.php index d6fa9408..ec8a28e1 100644 --- a/4dev/tests/DB/CoreLibsDBIOTest.php +++ b/4dev/tests/DB/CoreLibsDBIOTest.php @@ -160,15 +160,12 @@ final class CoreLibsDBIOTest extends TestCase // create the tables $db->dbExec( // primary key name is table + '_id' + // table_with_primary_key_id SERIAL PRIMARY KEY, <<dbExec( <<__dbQuery($q)) { - if (is_array($res = $this->__dbFetchArray($q))) { + if ($cursor = $this->__dbQueryParams($q, [$table, $pk_name])) { + if (is_array($res = $this->__dbFetchArray($cursor))) { list($id) = $res; } else { return false; @@ -451,26 +447,67 @@ class PgSQL implements Interface\SqlFunctions $table_prefix = $schema . '.'; } } + $params = [$table_prefix . $table]; + $replace = ['', '']; // read from table the PK name // faster primary key get - $q = "SELECT pg_attribute.attname AS column_name, " - . "format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type " - . "FROM pg_index, pg_class, pg_attribute "; + /* $q = <<__dbQuery($q); + $q .= <<__dbQueryParams(str_replace( + ['{PG_NAMESPACE}', '{NSPNAME}'], + $replace, + $q + ), $params); if ($cursor !== false) { $__db_fetch_array = $this->__dbFetchArray($cursor); if (!is_array($__db_fetch_array)) { @@ -895,11 +932,13 @@ class PgSQL implements Interface\SqlFunctions public function __dbSetSchema(string $db_schema): int { // check if schema actually exists - $query = "SELECT EXISTS(" - . "SELECT 1 FROM information_schema.schemata " - . "WHERE schema_name = " . $this->__dbEscapeLiteral($db_schema) - . ")"; - $cursor = $this->__dbQuery($query); + $query = <<__dbQueryParams($query, [$db_schema]); // abort if execution fails if ($cursor === false) { return 1;