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/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; } 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 = <<