diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index 184609f..8577915 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -466,7 +466,7 @@ function wpsqli_rollback(&$connection, $flags = 0, $name = null) pg_query($connection, "ROLLBACK"); } -function get_primary_key_for_table(&$connection, $table) +function get_primary_key_for_table(&$connection, $table) { $query = <<get_primary_key_for_table($connection, $tableName); $row = pg_fetch_assoc($result); - $GLOBALS['pg4wp_ins_id'] = $row[$primaryKey]; + $GLOBALS['pg4wp_ins_id'] = $row[$primaryKey]; } } @@ -1108,7 +1108,7 @@ function wpsqli_get_primary_sequence_for_table(&$connection, $table) } } - // we didn't find a sequence for this table. + // we didn't find a sequence for this table. return null; } @@ -1146,10 +1146,10 @@ function wpsqli_insert_id(&$connection = null) // PostgreSQL: Setting the value of the sequence based on the latest inserted ID. $GLOBALS['pg4wp_queued_query'] = "SELECT SETVAL('$seq',(SELECT MAX(\"ID\") FROM $table)+1);"; } elseif($GLOBALS['pg4wp_ins_id']) { - return $GLOBALS['pg4wp_ins_id']; + return $GLOBALS['pg4wp_ins_id']; } elseif(empty($sql)) { $sql = 'NO QUERY'; - $data = 0; + $data = 0; } else { // Double quoting is needed to prevent seq from being lowercased automatically $sql = "SELECT CURRVAL('\"$seq\"')"; diff --git a/pg4wp/rewriters/AlterTableSQLRewriter.php b/pg4wp/rewriters/AlterTableSQLRewriter.php index 191d099..f64e6d4 100644 --- a/pg4wp/rewriters/AlterTableSQLRewriter.php +++ b/pg4wp/rewriters/AlterTableSQLRewriter.php @@ -63,7 +63,7 @@ public function rewrite(): string return $sql; } - private function rewriteAddIndex(string $sql): string + private function rewriteAddIndex(string $sql): string { $pattern = '/ALTER TABLE\s+(\w+)\s+ADD (UNIQUE |)INDEX\s+([^\s]+)\s+\(((?:[^\(\)]+|\([^\(\)]+\))+)\)/'; @@ -72,18 +72,18 @@ private function rewriteAddIndex(string $sql): string $unique = $matches[2]; $index = $matches[3]; $columns = $matches[4]; - + // Remove prefix indexing // Rarely used and apparently unnecessary for current uses $columns = preg_replace('/\([^\)]*\)/', '', $columns); - + // Workaround for index name duplicate $index = $table . '_' . $index; - + // Add backticks around index name and column name, and include IF NOT EXISTS clause $sql = "CREATE {$unique}INDEX IF NOT EXISTS `{$index}` ON `{$table}` (`{$columns}`)"; } - + return $sql; } @@ -216,15 +216,16 @@ private function rewriteDropPrimaryKey(string $sql): string return $sql; } - private function rewrite_numeric_type($sql){ + private function rewrite_numeric_type($sql) + { // Numeric types in MySQL which need to be rewritten $numeric_types = ["bigint", "int", "integer", "smallint", "mediumint", "tinyint", "double", "decimal"]; $numeric_types_imploded = implode('|', $numeric_types); - + // Prepare regex pattern to match 'type(x)' $pattern = "/(" . $numeric_types_imploded . ")\(\d+\)/"; - - // Execute type find & replace + + // Execute type find & replace $sql = preg_replace_callback($pattern, function ($matches) { return $matches[1]; }, $sql); @@ -258,7 +259,7 @@ private function rewrite_numeric_type($sql){ $sql = preg_replace($pattern, 'serial', $sql); } } - + return $sql; } } diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index 8bd2b4f..fb8ec81 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -88,15 +88,16 @@ public function rewrite(): string return $sql; } - private function rewrite_numeric_type($sql){ + private function rewrite_numeric_type($sql) + { // Numeric types in MySQL which need to be rewritten $numeric_types = ["bigint", "int", "integer", "smallint", "mediumint", "tinyint", "double", "decimal"]; $numeric_types_imploded = implode('|', $numeric_types); - + // Prepare regex pattern to match 'type(x)' $pattern = "/(" . $numeric_types_imploded . ")\(\d+\)/"; - - // Execute type find & replace + + // Execute type find & replace $sql = preg_replace_callback($pattern, function ($matches) { return $matches[1]; }, $sql); @@ -130,7 +131,7 @@ private function rewrite_numeric_type($sql){ $sql = preg_replace($pattern, 'serial', $sql); } } - + return $sql; } } diff --git a/pg4wp/rewriters/InsertSQLRewriter.php b/pg4wp/rewriters/InsertSQLRewriter.php index e8461c0..50e9d59 100644 --- a/pg4wp/rewriters/InsertSQLRewriter.php +++ b/pg4wp/rewriters/InsertSQLRewriter.php @@ -130,7 +130,7 @@ public function rewrite(): string $sql = $sql_before_semicolon . ' RETURNING *' . $sql_after_semicolon; } else { - $sql = $sql .=" RETURNING *"; + $sql = $sql .= " RETURNING *"; } } @@ -138,23 +138,22 @@ public function rewrite(): string } // finds semicolons that aren't in variables - private function findSemicolon($sql) { + private function findSemicolon($sql) + { $quoteOpened = false; $parenthesisDepth = 0; - + $sqlAsArray = str_split($sql); - for($i=0; $ipostgresTableSizeRewrite(); + $sql = $this->postgresTableSizeRewrite(); return $sql; } @@ -360,7 +360,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 = 'public') { $sql = <<original(); - return $this->generatePostgresShowTableStatus(); + return $this->generatePostgresShowTableStatus(); } diff --git a/pg4wp/rewriters/ShowVariablesSQLRewriter.php b/pg4wp/rewriters/ShowVariablesSQLRewriter.php index 1eb45e8..8e944a5 100644 --- a/pg4wp/rewriters/ShowVariablesSQLRewriter.php +++ b/pg4wp/rewriters/ShowVariablesSQLRewriter.php @@ -38,7 +38,7 @@ public function generatePostgres($sql, $variableName) } if ($variableName == "max_allowed_packet") { - // Act like 1GB packet size, in practice this limit doesn't actually exist for postgres, we just want to fool WP + // Act like 1GB packet size, in practice this limit doesn't actually exist for postgres, we just want to fool WP return "SELECT '$variableName' AS \"Variable_name\", '1073741824' AS \"Value\";"; } diff --git a/tests/tools/php-cs-fixer.phar b/tests/tools/php-cs-fixer.phar new file mode 100644 index 0000000..54785df Binary files /dev/null and b/tests/tools/php-cs-fixer.phar differ