Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbucci committed Feb 27, 2024
1 parent f602b49 commit cf88b4d
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 39 deletions.
10 changes: 5 additions & 5 deletions pg4wp/driver_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<SQL
SELECT a.attname
Expand Down Expand Up @@ -542,7 +542,7 @@ function wpsqli_query(&$connection, $query, $result_mode = 0)
$primaryKey = $this->get_primary_key_for_table($connection, $tableName);
$row = pg_fetch_assoc($result);

$GLOBALS['pg4wp_ins_id'] = $row[$primaryKey];
$GLOBALS['pg4wp_ins_id'] = $row[$primaryKey];
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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\"')";
Expand Down
21 changes: 11 additions & 10 deletions pg4wp/rewriters/AlterTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+\(((?:[^\(\)]+|\([^\(\)]+\))+)\)/';

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -258,7 +259,7 @@ private function rewrite_numeric_type($sql){
$sql = preg_replace($pattern, 'serial', $sql);
}
}

return $sql;
}
}
11 changes: 6 additions & 5 deletions pg4wp/rewriters/CreateTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -130,7 +131,7 @@ private function rewrite_numeric_type($sql){
$sql = preg_replace($pattern, 'serial', $sql);
}
}

return $sql;
}
}
23 changes: 11 additions & 12 deletions pg4wp/rewriters/InsertSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,30 @@ public function rewrite(): string
$sql = $sql_before_semicolon . ' RETURNING *' . $sql_after_semicolon;

} else {
$sql = $sql .=" RETURNING *";
$sql = $sql .= " RETURNING *";
}
}

return $sql;
}

// 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; $i<count($sqlAsArray); $i++) {
if(($sqlAsArray[$i] == '"' || $sqlAsArray[$i]=="'") && ($i == 0 || $sqlAsArray[$i-1]!='\\'))
$quoteOpened = !$quoteOpened;

else if($sqlAsArray[$i] == '(' && !$quoteOpened)
for($i = 0; $i < count($sqlAsArray); $i++) {
if(($sqlAsArray[$i] == '"' || $sqlAsArray[$i] == "'") && ($i == 0 || $sqlAsArray[$i - 1] != '\\')) {
$quoteOpened = !$quoteOpened;
} elseif($sqlAsArray[$i] == '(' && !$quoteOpened) {
$parenthesisDepth++;

else if($sqlAsArray[$i] == ')' && !$quoteOpened)
} elseif($sqlAsArray[$i] == ')' && !$quoteOpened) {
$parenthesisDepth--;

else if($sqlAsArray[$i] == ';' && !$quoteOpened && $parenthesisDepth == 0)
} elseif($sqlAsArray[$i] == ';' && !$quoteOpened && $parenthesisDepth == 0) {
return $i;
}
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions pg4wp/rewriters/ReplaceIntoSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function rewrite(): string
// Extract SQL components
$tableSection = trim(substr($statement, $insertIndex, $columnsStartIndex - $insertIndex));
$valuesSection = trim(substr($statement, $valuesIndex, strlen($statement) - $valuesIndex));
$columnsSection = trim(substr($statement, $columnsStartIndex, $columnsEndIndex - $columnsStartIndex + 1));
$columnsSection = trim(substr($statement, $columnsStartIndex, $columnsEndIndex - $columnsStartIndex + 1));

// Extract and clean up column names from the update section
$updateCols = explode(',', substr($columnsSection, 1, strlen($columnsSection) - 2));
$updateCols = array_map(function ($col) {
return trim($col);
return trim($col);
}, $updateCols);

// Choose a primary key for ON CONFLICT
Expand All @@ -91,7 +91,7 @@ public function rewrite(): string
}

// trim any preceding commas
$updateSection = ltrim($updateSection,", ");
$updateSection = ltrim($updateSection, ", ");

// Construct the PostgreSQL query
$postgresSQL = sprintf('%s %s %s ON CONFLICT (%s) DO UPDATE SET %s', $tableSection, $columnsSection, $valuesSection, $primaryKey, $updateSection);
Expand Down
4 changes: 2 additions & 2 deletions pg4wp/rewriters/SelectSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function rewrite(): string
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();
$sql = $this->postgresTableSizeRewrite();
return $sql;
}

Expand Down Expand Up @@ -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 = <<<SQL
Expand Down
2 changes: 1 addition & 1 deletion pg4wp/rewriters/ShowTableStatusSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ShowTableStatusSQLRewriter extends AbstractSQLRewriter
public function rewrite(): string
{
$sql = $this->original();
return $this->generatePostgresShowTableStatus();
return $this->generatePostgresShowTableStatus();
}


Expand Down
2 changes: 1 addition & 1 deletion pg4wp/rewriters/ShowVariablesSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\";";
}

Expand Down
Binary file added tests/tools/php-cs-fixer.phar
Binary file not shown.

0 comments on commit cf88b4d

Please sign in to comment.