diff --git a/src/DB/SQL/Interface/SqlFunctions.php b/src/DB/SQL/Interface/SqlFunctions.php index 7e81a16..8ccfa9a 100644 --- a/src/DB/SQL/Interface/SqlFunctions.php +++ b/src/DB/SQL/Interface/SqlFunctions.php @@ -285,6 +285,22 @@ interface SqlFunctions */ public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool; + /** + * Undocumented function + * + * @param string $parameter + * @param bool $strip + * @return string + */ + public function __dbVersionInfo(string $parameter, bool $strip = true): string; + + /** + * Undocumented function + * + * @return array + */ + public function __dbVersionInfoParameterList(): array; + /** * Undocumented function * @@ -292,6 +308,13 @@ interface SqlFunctions */ public function __dbVersion(): string; + /** + * Undocumented function + * + * @return int + */ + public function __dbVersionNumeric(): int; + /** * Undocumented function * @@ -306,6 +329,14 @@ interface SqlFunctions ?int &$end = null ): ?array; + /** + * Undocumented function + * + * @param string $parameter + * @return string|bool + */ + public function __dbParameter(string $parameter): string|bool; + /** * Undocumented function * @@ -343,6 +374,14 @@ interface SqlFunctions * @return string */ public function __dbGetEncoding(): string; + + /** + * Undocumented function + * + * @param string $query + * @return int + */ + public function __dbCountQueryParams(string $query): int; } // __END__ diff --git a/src/DB/SQL/PgSQL.php b/src/DB/SQL/PgSQL.php index 53b9725..a343c8b 100644 --- a/src/DB/SQL/PgSQL.php +++ b/src/DB/SQL/PgSQL.php @@ -407,17 +407,13 @@ class PgSQL implements Interface\SqlFunctions } // no PK name given at all if (empty($pk_name)) { - // if name is plurar, make it singular - // if (preg_match("/.*s$/i", $table)) - // $table = substr($table, 0, -1); // set pk_name to "id" $pk_name = $table . "_id"; } - $seq = ($schema ? $schema . '.' : '') . $table . "_" . $pk_name . "_seq"; - $q = "SELECT CURRVAL('$seq') AS insert_id"; + $q = "SELECT CURRVAL(pg_get_serial_sequence($1, $2)) AS insert_id"; // I have to do manually or I overwrite the original insert internal vars ... - if ($q = $this->__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,36 @@ 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); + $cursor = $this->__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 +901,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; diff --git a/test/phpunit/DB/CoreLibsDBIOTest.php b/test/phpunit/DB/CoreLibsDBIOTest.php index d6fa940..ec8a28e 100644 --- a/test/phpunit/DB/CoreLibsDBIOTest.php +++ b/test/phpunit/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( <<