mirror of
https://github.com/PostgreSQL-For-Wordpress/postgresql-for-wordpress.git
synced 2025-06-25 09:21:44 +02:00
formatting
This commit is contained in:
@ -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
|
||||
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
@ -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\"')";
|
||||
|
@ -63,7 +63,7 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
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 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
$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 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
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 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
@ -88,15 +88,16 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
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 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ class InsertSQLRewriter extends AbstractSQLRewriter
|
||||
$sql = $sql_before_semicolon . ' RETURNING *' . $sql_after_semicolon;
|
||||
|
||||
} else {
|
||||
$sql = $sql .=" RETURNING *";
|
||||
$sql = $sql .= " RETURNING *";
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,23 +138,22 @@ class InsertSQLRewriter extends AbstractSQLRewriter
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
@ -61,12 +61,12 @@ class ReplaceIntoSQLRewriter extends AbstractSQLRewriter
|
||||
// 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
|
||||
@ -91,7 +91,7 @@ class ReplaceIntoSQLRewriter extends AbstractSQLRewriter
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
@ -37,7 +37,7 @@ class SelectSQLRewriter extends AbstractSQLRewriter
|
||||
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;
|
||||
}
|
||||
|
||||
@ -360,7 +360,7 @@ class SelectSQLRewriter extends AbstractSQLRewriter
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -5,7 +5,7 @@ class ShowTableStatusSQLRewriter extends AbstractSQLRewriter
|
||||
public function rewrite(): string
|
||||
{
|
||||
$sql = $this->original();
|
||||
return $this->generatePostgresShowTableStatus();
|
||||
return $this->generatePostgresShowTableStatus();
|
||||
}
|
||||
|
||||
|
||||
|
@ -38,7 +38,7 @@ class ShowVariablesSQLRewriter extends AbstractSQLRewriter
|
||||
}
|
||||
|
||||
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\";";
|
||||
}
|
||||
|
||||
|
BIN
tests/tools/php-cs-fixer.phar
Normal file
BIN
tests/tools/php-cs-fixer.phar
Normal file
Binary file not shown.
Reference in New Issue
Block a user