From 36726124a4eee3fdf5d1f52e1f22a183e53af127 Mon Sep 17 00:00:00 2001 From: Matthew Bucci Date: Fri, 18 Oct 2024 23:07:05 -0700 Subject: [PATCH 1/2] swap hardcoded public values for DB_SCHEMA constant --- pg4wp/db.php | 5 +++++ pg4wp/rewriters/DescribeSQLRewriter.php | 4 ++-- pg4wp/rewriters/SelectSQLRewriter.php | 4 ++-- .../rewriters/ShowFullColumnsSQLRewriter.php | 4 ++-- .../rewriters/ShowTableStatusSQLRewriter.php | 4 ++-- pg4wp/rewriters/ShowTablesSQLRewriter.php | 2 +- readme.md | 19 +++++++++++++++++++ 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/pg4wp/db.php b/pg4wp/db.php index b5cca53..5dd9067 100644 --- a/pg4wp/db.php +++ b/pg4wp/db.php @@ -49,6 +49,11 @@ mkdir(PG4WP_LOG); } + // Default to public schema + if (!defined('DB_SCHEMA')) { + define('DB_SCHEMA', 'public'); + } + // Here happens all the magic require_once(PG4WP_ROOT . '/core.php'); } // Protection against multiple loading diff --git a/pg4wp/rewriters/DescribeSQLRewriter.php b/pg4wp/rewriters/DescribeSQLRewriter.php index 98dfb8c..5f39205 100644 --- a/pg4wp/rewriters/DescribeSQLRewriter.php +++ b/pg4wp/rewriters/DescribeSQLRewriter.php @@ -6,7 +6,7 @@ public function rewrite(): string { $sql = $this->original(); $table = $this->extractTableName($sql); - return $this->generatePostgresDescribeTable($table); + return $this->generatePostgresDescribeTable($table, DB_SCHEMA); } /** @@ -31,7 +31,7 @@ protected function extractTableName($sql) * @param string $schema The schema name * @return string The generated SQL query */ - public function generatePostgresDescribeTable($tableName, $schema = "public") + public function generatePostgresDescribeTable($tableName, $schema) { $sql = <<postgresTableSizeRewrite(); + $sql = $this->postgresTableSizeRewrite(DB_SCHEMA); return $sql; } @@ -373,7 +373,7 @@ protected function convertToPostgresLimitSyntax($sql) } // This method is specifically to handle should_suggest_persistent_object_cache in wp site health - protected function postgresTableSizeRewrite($schema = 'public') + protected function postgresTableSizeRewrite($schema) { $sql = <<original(); $table = $this->extractTableNameFromShowColumns($sql); - return $this->generatePostgresShowColumns($table); + return $this->generatePostgresShowColumns($table, DB_SCHEMA); } /** @@ -31,7 +31,7 @@ protected function extractTableNameFromShowColumns($sql) * @param string $schema The schema name * @return string The generated SQL query */ - public function generatePostgresShowColumns($tableName, $schema = "public") + public function generatePostgresShowColumns($tableName, $schema) { $sql = <<original(); - return $this->generatePostgresShowTableStatus(); + return $this->generatePostgresShowTableStatus(DB_SCHEMA); } @@ -14,7 +14,7 @@ public function rewrite(): string * * @return string The generated SQL query */ - public function generatePostgresShowTableStatus($schema = "public") + public function generatePostgresShowTableStatus($schema) { $sql = << Date: Fri, 18 Oct 2024 23:13:25 -0700 Subject: [PATCH 2/2] set default schema when connecting --- pg4wp/driver_pgsql.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index ac83d76..b44af89 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -105,6 +105,9 @@ function wpsqli_real_connect(&$connection, $hostname = null, $username = null, $ $pg_connstr = $GLOBALS['pg4wp_connstr'] . ' dbname=' . $dbname; $GLOBALS['pg4wp_conn'] = $connection = pg_connect($pg_connstr); + // Set the default schema + pg_query($connection, 'SET search_path TO ' . pg_escape_literal(DB_SCHEMA)); + return $connection; }