diff --git a/pg4wp/rewriters/AlterTableSQLRewriter.php b/pg4wp/rewriters/AlterTableSQLRewriter.php index db819e7..27a0c95 100644 --- a/pg4wp/rewriters/AlterTableSQLRewriter.php +++ b/pg4wp/rewriters/AlterTableSQLRewriter.php @@ -30,6 +30,7 @@ public function rewrite(): string $sql = $this->original(); $sql = $this->rewrite_numeric_type($sql); + $sql = $this->rewrite_columns_with_protected_names($sql); if (str_contains($sql, 'ADD INDEX') || str_contains($sql, 'ADD UNIQUE INDEX')) { $sql = $this->rewriteAddIndex($sql); @@ -262,4 +263,28 @@ private function rewrite_numeric_type($sql){ return $sql; } + + private function rewrite_columns_with_protected_names($sql) + { + // Splitting the SQL statement into parts before "(", inside "(", and after ")" + if (preg_match('/^(CREATE TABLE IF NOT EXISTS|CREATE TABLE|ALTER TABLE)\s+([^\s]+)\s*\((.*)\)(.*)$/is', $sql, $matches)) { + $prefix = $matches[1] . ' ' . $matches[2] . ' ('; + $columnsAndKeys = $matches[3]; + $suffix = ')' . $matches[4]; + + $regex = '/(?:^|\s*,\s*)(\b(?:timestamp|date|time|default)\b)\s*(?=\s+\w+)/i'; + + // Callback function to add quotes around protected column names + $callback = function($matches) { + $whitespace = str_replace($matches[1], "", $matches[0]); + return $whitespace . '"' . $matches[1] . '"'; + }; + + // Replace protected column names with quoted versions within columns and keys part + $columnsAndKeys = preg_replace_callback($regex, $callback, $columnsAndKeys, 1); + return $prefix . $columnsAndKeys . $suffix; + } + + return $sql; + } } diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index 8bd2b4f..eb4be6e 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -55,6 +55,7 @@ public function rewrite(): string ); $sql = $this->rewrite_numeric_type($sql); + $sql = $this->rewrite_columns_with_protected_names($sql); // Support for UNIQUE INDEX creation $pattern = '/,\s*(UNIQUE |)KEY\s+(`[^`]+`|\w+)\s+\(((?:[^()]|\([^)]*\))*)\)/'; @@ -133,4 +134,28 @@ private function rewrite_numeric_type($sql){ return $sql; } + + private function rewrite_columns_with_protected_names($sql) + { + // Splitting the SQL statement into parts before "(", inside "(", and after ")" + if (preg_match('/^(CREATE TABLE IF NOT EXISTS|CREATE TABLE|ALTER TABLE)\s+([^\s]+)\s*\((.*)\)(.*)$/is', $sql, $matches)) { + $prefix = $matches[1] . ' ' . $matches[2] . ' ('; + $columnsAndKeys = $matches[3]; + $suffix = ')' . $matches[4]; + + $regex = '/(?:^|\s*,\s*)(\b(?:timestamp|date|time|default)\b)\s*(?=\s+\w+)/i'; + + // Callback function to add quotes around protected column names + $callback = function($matches) { + $whitespace = str_replace($matches[1], "", $matches[0]); + return $whitespace . '"' . $matches[1] . '"'; + }; + + // Replace protected column names with quoted versions within columns and keys part + $columnsAndKeys = preg_replace_callback($regex, $callback, $columnsAndKeys, 1); + return $prefix . $columnsAndKeys . $suffix; + } + + return $sql; + } } diff --git a/tests/rewriteTest.php b/tests/rewriteTest.php index 118ade0..b2de577 100644 --- a/tests/rewriteTest.php +++ b/tests/rewriteTest.php @@ -530,6 +530,69 @@ public function test_it_handles_alter_tables_with_unique_indexes() $this->assertSame(trim($expected), trim($postgresql)); } + public function test_it_rewrites_protected_column_names() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } + + public function test_it_rewrites_advanced_protected_column_names() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } + + public function test_it_doesnt_remove_single_quotes() { $sql = <<