diff --git a/pg4wp/driver_pgsql_rewrite.php b/pg4wp/driver_pgsql_rewrite.php index 4b16556..8bd1a9d 100644 --- a/pg4wp/driver_pgsql_rewrite.php +++ b/pg4wp/driver_pgsql_rewrite.php @@ -12,7 +12,7 @@ spl_autoload_register(function ($className) { function createSQLRewriter(string $sql): AbstractSQLRewriter { $sql = trim($sql); - if (preg_match('/^(SELECT|INSERT|REPLACE INTO|UPDATE|DELETE|DESCRIBE|ALTER TABLE|CREATE TABLE|DROP TABLE|SHOW INDEX|SHOW VARIABLES|SHOW TABLES|OPTIMIZE TABLE|SET NAMES|SHOW FULL COLUMNS)\b/i', $sql, $matches)) { + if (preg_match('/^(SELECT|INSERT|REPLACE INTO|UPDATE|DELETE|DESCRIBE|ALTER TABLE|CREATE TABLE|DROP TABLE|SHOW INDEX|SHOW VARIABLES|SHOW TABLES|OPTIMIZE TABLE|SET NAMES|SHOW FULL COLUMNS|SHOW TABLE STATUS)\b/i', $sql, $matches)) { // Convert to a format suitable for class names (e.g., "SHOW TABLES" becomes "ShowTables") $type = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($matches[1])))); $className = $type . 'SQLRewriter'; diff --git a/pg4wp/rewriters/DescribeSQLRewriter.php b/pg4wp/rewriters/DescribeSQLRewriter.php index 6b841c1..98dfb8c 100644 --- a/pg4wp/rewriters/DescribeSQLRewriter.php +++ b/pg4wp/rewriters/DescribeSQLRewriter.php @@ -28,9 +28,10 @@ class DescribeSQLRewriter extends AbstractSQLRewriter * Generates a PostgreSQL-compatible SQL query to mimic MySQL's "DESCRIBE". * * @param string $tableName The table name + * @param string $schema The schema name * @return string The generated SQL query */ - public function generatePostgresDescribeTable($tableName) + public function generatePostgresDescribeTable($tableName, $schema = "public") { $sql = << 0 ORDER BY number SQL; diff --git a/pg4wp/rewriters/SelectSQLRewriter.php b/pg4wp/rewriters/SelectSQLRewriter.php index 19deb31..ea861d9 100644 --- a/pg4wp/rewriters/SelectSQLRewriter.php +++ b/pg4wp/rewriters/SelectSQLRewriter.php @@ -34,6 +34,16 @@ class SelectSQLRewriter extends AbstractSQLRewriter $sql = preg_replace('/SELECT\s+.*?\s+FROM\s+/is', 'SELECT COUNT(*) FROM ', $sql, 1); } + if(false !== strpos($sql, 'information_schema')) { + // WP Site Health rewrites + if (false !== strpos($sql, "SELECT TABLE_NAME AS 'table', TABLE_ROWS AS 'rows', SUM(data_length + index_length)")) { + $sql = $this->postgresTableSizeRewrite(); + return $sql; + } + + throw new Exception("Unsupported call to information_schema, this probably won't work correctly and needs to be specifically handled, open a github issue with the SQL"); + } + $sql = $this->ensureOrderByInSelect($sql); // Convert CONVERT to CAST @@ -350,4 +360,29 @@ class SelectSQLRewriter extends AbstractSQLRewriter return $sql; } + // This method is specifically to handle should_suggest_persistent_object_cache in wp site health + protected function postgresTableSizeRewrite($schema = 'public') + { + + $sql = <<original(); + return $this->generatePostgresShowTableStatus(); + } + + + /** + * Generates a PostgreSQL-compatible SQL query to mimic MySQL's "SHOW TABLE STATUS". + * + * @return string The generated SQL query + */ + public function generatePostgresShowTableStatus($schema = "public") + { + $sql = <<