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
This commit is contained in:
@@ -160,15 +160,12 @@ final class CoreLibsDBIOTest extends TestCase
|
|||||||
// create the tables
|
// create the tables
|
||||||
$db->dbExec(
|
$db->dbExec(
|
||||||
// primary key name is table + '_id'
|
// primary key name is table + '_id'
|
||||||
|
// table_with_primary_key_id SERIAL PRIMARY KEY,
|
||||||
<<<SQL
|
<<<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 INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||||
$base_table
|
$base_table
|
||||||
SQL
|
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(
|
$db->dbExec(
|
||||||
<<<SQL
|
<<<SQL
|
||||||
|
|||||||
@@ -407,17 +407,13 @@ class PgSQL implements Interface\SqlFunctions
|
|||||||
}
|
}
|
||||||
// no PK name given at all
|
// no PK name given at all
|
||||||
if (empty($pk_name)) {
|
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"
|
// set pk_name to "id"
|
||||||
$pk_name = $table . "_id";
|
$pk_name = $table . "_id";
|
||||||
}
|
}
|
||||||
$seq = ($schema ? $schema . '.' : '') . $table . "_" . $pk_name . "_seq";
|
$q = "SELECT CURRVAL(pg_get_serial_sequence($1, $2)) AS insert_id";
|
||||||
$q = "SELECT CURRVAL('$seq') AS insert_id";
|
|
||||||
// I have to do manually or I overwrite the original insert internal vars ...
|
// I have to do manually or I overwrite the original insert internal vars ...
|
||||||
if ($q = $this->__dbQuery($q)) {
|
if ($cursor = $this->__dbQueryParams($q, [$table, $pk_name])) {
|
||||||
if (is_array($res = $this->__dbFetchArray($q))) {
|
if (is_array($res = $this->__dbFetchArray($cursor))) {
|
||||||
list($id) = $res;
|
list($id) = $res;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
@@ -451,26 +447,67 @@ class PgSQL implements Interface\SqlFunctions
|
|||||||
$table_prefix = $schema . '.';
|
$table_prefix = $schema . '.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$params = [$table_prefix . $table];
|
||||||
|
$replace = ['', ''];
|
||||||
// read from table the PK name
|
// read from table the PK name
|
||||||
// faster primary key get
|
// faster primary key get
|
||||||
$q = "SELECT pg_attribute.attname AS column_name, "
|
/* $q = <<<SQL
|
||||||
. "format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type "
|
SELECT
|
||||||
. "FROM pg_index, pg_class, pg_attribute ";
|
pg_attribute.attname AS column_name,
|
||||||
|
format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type
|
||||||
|
FROM pg_index, pg_class, pg_attribute
|
||||||
|
|
||||||
|
SQL;
|
||||||
if ($schema) {
|
if ($schema) {
|
||||||
$q .= ", pg_namespace ";
|
$q .= ", pg_namespace ";
|
||||||
}
|
}
|
||||||
$q .= "WHERE "
|
$q .= <<<SQL
|
||||||
// regclass translates the OID to the name
|
WHERE
|
||||||
. "pg_class.oid = '" . $table_prefix . $table . "'::regclass AND "
|
-- regclass translates the OID to the name
|
||||||
. "indrelid = pg_class.oid AND ";
|
pg_class.oid = $1::regclass AND
|
||||||
|
indrelid = pg_class.oid AND
|
||||||
|
|
||||||
|
SQL;
|
||||||
if ($schema) {
|
if ($schema) {
|
||||||
$q .= "nspname = '" . $schema . "' AND "
|
$params[] = $schema;
|
||||||
. "pg_class.relnamespace = pg_namespace.oid AND ";
|
$q .= <<<SQL
|
||||||
|
nspname = $2 AND
|
||||||
|
pg_class.relnamespace = pg_namespace.oid AND
|
||||||
|
|
||||||
|
SQL;
|
||||||
}
|
}
|
||||||
$q .= "pg_attribute.attrelid = pg_class.oid AND "
|
$q .= <<<SQL
|
||||||
. "pg_attribute.attnum = any(pg_index.indkey) "
|
pg_attribute.attrelid = pg_class.oid AND
|
||||||
. "AND indisprimary";
|
pg_attribute.attnum = any(pg_index.indkey)
|
||||||
$cursor = $this->__dbQuery($q);
|
AND indisprimary
|
||||||
|
SQL; */
|
||||||
|
|
||||||
|
$q = <<<SQL
|
||||||
|
SELECT
|
||||||
|
pg_attribute.attname AS column_name,
|
||||||
|
format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type
|
||||||
|
FROM pg_index, pg_class, pg_attribute{PG_NAMESPACE}
|
||||||
|
WHERE
|
||||||
|
-- regclass translates the OID to the name
|
||||||
|
pg_class.oid = $1::regclass AND
|
||||||
|
indrelid = pg_class.oid AND
|
||||||
|
pg_attribute.attrelid = pg_class.oid AND
|
||||||
|
pg_attribute.attnum = any(pg_index.indkey) AND
|
||||||
|
indisprimary
|
||||||
|
{NSPNAME}
|
||||||
|
SQL;
|
||||||
|
if ($schema) {
|
||||||
|
$params[] = $schema;
|
||||||
|
$replace = [
|
||||||
|
", pg_namespace",
|
||||||
|
"AND pg_class.relnamespace = pg_namespace.oid AND nspname = $2"
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$cursor = $this->__dbQueryParams(str_replace(
|
||||||
|
['{PG_NAMESPACE}', '{NSPNAME}'],
|
||||||
|
$replace,
|
||||||
|
$q
|
||||||
|
), $params);
|
||||||
if ($cursor !== false) {
|
if ($cursor !== false) {
|
||||||
$__db_fetch_array = $this->__dbFetchArray($cursor);
|
$__db_fetch_array = $this->__dbFetchArray($cursor);
|
||||||
if (!is_array($__db_fetch_array)) {
|
if (!is_array($__db_fetch_array)) {
|
||||||
@@ -895,11 +932,13 @@ class PgSQL implements Interface\SqlFunctions
|
|||||||
public function __dbSetSchema(string $db_schema): int
|
public function __dbSetSchema(string $db_schema): int
|
||||||
{
|
{
|
||||||
// check if schema actually exists
|
// check if schema actually exists
|
||||||
$query = "SELECT EXISTS("
|
$query = <<<SQL
|
||||||
. "SELECT 1 FROM information_schema.schemata "
|
SELECT EXISTS (
|
||||||
. "WHERE schema_name = " . $this->__dbEscapeLiteral($db_schema)
|
SELECT 1 FROM information_schema.schemata
|
||||||
. ")";
|
WHERE schema_name = $1
|
||||||
$cursor = $this->__dbQuery($query);
|
)
|
||||||
|
SQL;
|
||||||
|
$cursor = $this->__dbQueryParams($query, [$db_schema]);
|
||||||
// abort if execution fails
|
// abort if execution fails
|
||||||
if ($cursor === false) {
|
if ($cursor === false) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user