mirror of
https://github.com/PostgreSQL-For-Wordpress/postgresql-for-wordpress.git
synced 2025-07-29 17:27:13 +02:00
Merge branch 'v3' into site-health
This commit is contained in:
@ -12,7 +12,7 @@ spl_autoload_register(function ($className) {
|
||||
function createSQLRewriter(string $sql): AbstractSQLRewriter
|
||||
{
|
||||
$sql = trim($sql);
|
||||
if (preg_match('/^(SELECT|INSERT|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)) {
|
||||
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';
|
||||
|
@ -3,11 +3,6 @@
|
||||
class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
{
|
||||
private $stringReplacements = [
|
||||
' bigint(40)' => ' bigint',
|
||||
' bigint(20)' => ' bigint',
|
||||
' bigint(10)' => ' int',
|
||||
' int(11)' => ' int',
|
||||
' int(10)' => ' int',
|
||||
' tinytext' => ' text',
|
||||
' mediumtext' => ' text',
|
||||
' longtext' => ' text',
|
||||
@ -16,16 +11,15 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
|
||||
'\'0000-00-00 00:00:00\'' => 'now()',
|
||||
' datetime' => ' timestamp',
|
||||
' DEFAULT CHARACTER SET utf8mb4' => '',
|
||||
' DEFAULT CHARACTER SET utf8' => '',
|
||||
|
||||
// WP 2.7.1 compatibility
|
||||
' int(4)' => ' smallint',
|
||||
|
||||
// For WPMU (starting with WP 3.2)
|
||||
' tinyint(2)' => ' smallint',
|
||||
' tinyint(1)' => ' smallint',
|
||||
" enum('0','1')" => ' smallint',
|
||||
' COLLATE utf8mb4_unicode_520_ci' => '',
|
||||
' COLLATE utf8_general_ci' => '',
|
||||
' CHARACTER SET utf8' => '',
|
||||
' DEFAULT CHARSET=utf8' => '',
|
||||
|
||||
// For flash-album-gallery plugin
|
||||
' tinyint' => ' smallint'
|
||||
@ -35,6 +29,8 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
{
|
||||
$sql = $this->original();
|
||||
|
||||
$sql = $this->rewrite_numeric_type($sql);
|
||||
|
||||
if (str_contains($sql, 'CHANGE COLUMN')) {
|
||||
$sql = $this->rewriteChangeColumn($sql);
|
||||
}
|
||||
@ -185,4 +181,50 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter
|
||||
|
||||
return $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
|
||||
$sql = preg_replace_callback($pattern, function ($matches) {
|
||||
return $matches[1];
|
||||
}, $sql);
|
||||
|
||||
// bigint
|
||||
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'bigserial', $sql);
|
||||
}
|
||||
|
||||
// int
|
||||
$pattern = '/int(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
|
||||
// smallint
|
||||
$pattern = '/smallint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'smallserial', $sql);
|
||||
}
|
||||
|
||||
// Handle for numeric and decimal -- being replaced with serial
|
||||
$numeric_patterns = ['/numeric(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i', '/decimal(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i'];
|
||||
foreach($numeric_patterns as $pattern) {
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,6 @@
|
||||
class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
{
|
||||
private $stringReplacements = [
|
||||
' bigint(40)' => ' bigint',
|
||||
' bigint(20)' => ' bigint',
|
||||
' bigint(10)' => ' int',
|
||||
' int(11)' => ' int',
|
||||
' int(10)' => ' int',
|
||||
' int(1)' => ' smallint',
|
||||
' tinytext' => ' text',
|
||||
' mediumtext' => ' text',
|
||||
' longtext' => ' text',
|
||||
@ -16,16 +10,11 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
|
||||
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
|
||||
'\'0000-00-00 00:00:00\'' => 'now()',
|
||||
'datetime' => 'timestamp',
|
||||
' datetime' => ' timestamp',
|
||||
' DEFAULT CHARACTER SET utf8mb4' => '',
|
||||
' DEFAULT CHARACTER SET utf8' => '',
|
||||
|
||||
// WP 2.7.1 compatibility
|
||||
' int(4)' => ' smallint',
|
||||
|
||||
// For WPMU (starting with WP 3.2)
|
||||
' tinyint(2)' => ' smallint',
|
||||
' tinyint(1)' => ' smallint',
|
||||
" enum('0','1')" => ' smallint',
|
||||
' COLLATE utf8mb4_unicode_520_ci' => '',
|
||||
' COLLATE utf8_general_ci' => '',
|
||||
@ -40,7 +29,6 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
{
|
||||
$sql = $this->original();
|
||||
|
||||
|
||||
$tableSQL = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
|
||||
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
|
||||
preg_match($pattern, $tableSQL, $matches);
|
||||
@ -66,35 +54,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
$sql
|
||||
);
|
||||
|
||||
// bigint
|
||||
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'bigserial', $sql);
|
||||
}
|
||||
|
||||
// int
|
||||
$pattern = '/int(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
|
||||
// smallint
|
||||
$pattern = '/smallint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'smallserial', $sql);
|
||||
}
|
||||
|
||||
// Handle for numeric and decimal -- being replaced with serial
|
||||
$numeric_patterns = ['/numeric(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i', '/decimal(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i'];
|
||||
foreach($numeric_patterns as $pattern) {
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
$sql = $this->rewrite_numeric_type($sql);
|
||||
|
||||
// Support for UNIQUE INDEX creation
|
||||
$pattern = '/,\s*(UNIQUE |)KEY\s+(`[^`]+`|\w+)\s+\(((?:[^()]|\([^)]*\))*)\)/';
|
||||
@ -127,4 +87,50 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
|
||||
return $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
|
||||
$sql = preg_replace_callback($pattern, function ($matches) {
|
||||
return $matches[1];
|
||||
}, $sql);
|
||||
|
||||
// bigint
|
||||
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'bigserial', $sql);
|
||||
}
|
||||
|
||||
// int
|
||||
$pattern = '/int(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
|
||||
// smallint
|
||||
$pattern = '/smallint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'smallserial', $sql);
|
||||
}
|
||||
|
||||
// Handle for numeric and decimal -- being replaced with serial
|
||||
$numeric_patterns = ['/numeric(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i', '/decimal(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i'];
|
||||
foreach($numeric_patterns as $pattern) {
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
107
pg4wp/rewriters/ReplaceIntoSQLRewriter.php
Normal file
107
pg4wp/rewriters/ReplaceIntoSQLRewriter.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
class ReplaceIntoSQLRewriter extends AbstractSQLRewriter
|
||||
{
|
||||
public function rewrite(): string
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$sql = $this->original();
|
||||
|
||||
$splitStatements = function (string $sql): array {
|
||||
$statements = [];
|
||||
$buffer = '';
|
||||
$quote = null;
|
||||
|
||||
for ($i = 0, $len = strlen($sql); $i < $len; $i++) {
|
||||
$char = $sql[$i];
|
||||
|
||||
if ($quote) {
|
||||
if ($char === $quote && $sql[$i - 1] !== '\\') {
|
||||
$quote = null;
|
||||
}
|
||||
} elseif ($char === '"' || $char === "'") {
|
||||
$quote = $char;
|
||||
} elseif ($char === ';') {
|
||||
$statements[] = $buffer . ';';
|
||||
$buffer = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
$buffer .= $char;
|
||||
}
|
||||
|
||||
if (!empty($buffer)) {
|
||||
$statements[] = $buffer;
|
||||
}
|
||||
|
||||
return $statements;
|
||||
};
|
||||
|
||||
$statements = $splitStatements($sql);
|
||||
|
||||
foreach ($statements as $statement) {
|
||||
$statement = trim($statement);
|
||||
|
||||
// Skip empty statements
|
||||
if (empty($statement)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Replace backticks with double quotes for PostgreSQL compatibility
|
||||
$statement = str_replace('`', '"', $statement);
|
||||
|
||||
// Find index positions for the SQL components
|
||||
$insertIndex = strpos($statement, 'REPLACE INTO');
|
||||
$columnsStartIndex = strpos($statement, "(");
|
||||
$columnsEndIndex = strpos($statement, ")");
|
||||
$valuesIndex = strpos($statement, 'VALUES');
|
||||
$onDuplicateKeyIndex = strpos($statement, 'ON DUPLICATE KEY UPDATE');
|
||||
|
||||
// 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));
|
||||
|
||||
// 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);
|
||||
}, $updateCols);
|
||||
|
||||
// Choose a primary key for ON CONFLICT
|
||||
$primaryKey = 'option_name';
|
||||
if (!in_array($primaryKey, $updateCols)) {
|
||||
$primaryKey = 'meta_name';
|
||||
if (!in_array($primaryKey, $updateCols)) {
|
||||
$primaryKey = $updateCols[0] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
// SWAP REPLACE INTO for INSERT INTO
|
||||
$tableSection = str_replace("REPLACE INTO", "INSERT INTO", $tableSection);
|
||||
|
||||
// Construct the PostgreSQL ON CONFLICT DO UPDATE section
|
||||
$updateSection = "";
|
||||
foreach($updateCols as $col) {
|
||||
if ($col !== $primaryKey) {
|
||||
$updateSection .= ", ";
|
||||
$updateSection .= "$col = EXCLUDED.$col";
|
||||
}
|
||||
}
|
||||
|
||||
// trim any preceding commas
|
||||
$updateSection = ltrim($updateSection,", ");
|
||||
|
||||
// Construct the PostgreSQL query
|
||||
$postgresSQL = sprintf('%s %s %s ON CONFLICT (%s) DO UPDATE SET %s', $tableSection, $columnsSection, $valuesSection, $primaryKey, $updateSection);
|
||||
|
||||
// Append to the converted statements list
|
||||
$convertedStatements[] = $postgresSQL;
|
||||
}
|
||||
|
||||
$sql = implode('; ', $convertedStatements);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
@ -24,6 +24,9 @@ class SelectSQLRewriter extends AbstractSQLRewriter
|
||||
// Remove the LIMIT clause if it exists
|
||||
$sql = preg_replace('/\s+LIMIT\s+\d+(\s*,\s*\d+)?/i', '', $sql);
|
||||
|
||||
// Remove the ORDER BY containing case / end clause if it exists
|
||||
$sql = preg_replace('/\s+ORDER\s+BY\s+.+END\),[^)]+/is', '', $sql);
|
||||
|
||||
// Remove the ORDER BY clause if it exists
|
||||
$sql = preg_replace('/\s+ORDER\s+BY\s+[^)]+/i', '', $sql);
|
||||
|
||||
|
@ -64,7 +64,7 @@ final class rewriteTest extends TestCase
|
||||
lockout_host varchar(40),
|
||||
lockout_user bigint ,
|
||||
lockout_username varchar(60),
|
||||
lockout_active smallint NOT NULL DEFAULT 1,
|
||||
lockout_active int NOT NULL DEFAULT 1,
|
||||
lockout_context TEXT,
|
||||
PRIMARY KEY (lockout_id)
|
||||
);
|
||||
@ -382,6 +382,51 @@ final class rewriteTest extends TestCase
|
||||
$this->assertSame(trim($expected), trim($postgresql));
|
||||
}
|
||||
|
||||
public function test_it_will_handle_found_rows_on_queries_with_order_by_case()
|
||||
{
|
||||
$GLOBALS['pg4wp_numrows_query'] = <<<SQL
|
||||
SELECT wp_posts.ID
|
||||
FROM wp_posts
|
||||
WHERE 1=1 AND
|
||||
(((wp_posts.post_title LIKE '%Hello%') OR (wp_posts.post_excerpt LIKE '%Hello%') OR (wp_posts.post_content LIKE '%Hello%')) AND
|
||||
((wp_posts.post_title LIKE '%world%') OR (wp_posts.post_excerpt LIKE '%world%') OR (wp_posts.post_content LIKE '%world%'))) AND
|
||||
((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')))
|
||||
ORDER BY (CASE
|
||||
WHEN wp_posts.post_title LIKE '%Hello world%' THEN 1
|
||||
WHEN wp_posts.post_title LIKE '%Hello%' AND wp_posts.post_title LIKE '%world%' THEN 2
|
||||
WHEN wp_posts.post_title LIKE '%Hello%' OR wp_posts.post_title LIKE '%world%' THEN 3
|
||||
WHEN wp_posts.post_excerpt LIKE '%Hello world%' THEN 4
|
||||
WHEN wp_posts.post_content LIKE '%Hello world%' THEN 5 ELSE 6 END), wp_posts.post_date
|
||||
DESC
|
||||
LIMIT 0, 20
|
||||
SQL;
|
||||
|
||||
$sql = "SELECT FOUND_ROWS()";
|
||||
|
||||
$expected = <<<SQL
|
||||
SELECT COUNT(*) FROM wp_posts
|
||||
WHERE 1=1 AND
|
||||
(((wp_posts.post_title ILIKE '%Hello%') OR (wp_posts.post_excerpt ILIKE '%Hello%') OR (wp_posts.post_content ILIKE '%Hello%')) AND
|
||||
((wp_posts.post_title ILIKE '%world%') OR (wp_posts.post_excerpt ILIKE '%world%') OR (wp_posts.post_content ILIKE '%world%'))) AND
|
||||
((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')))
|
||||
SQL;
|
||||
|
||||
$postgresql = pg4wp_rewrite($sql);
|
||||
$this->assertSame(trim($expected), trim($postgresql));
|
||||
}
|
||||
|
||||
|
||||
public function test_it_can_handle_replacement_sql()
|
||||
{
|
||||
$sql = "REPLACE INTO test2 (column1, column2, column3) VALUES (1, 'Old', '2014-08-20 18:47:00')";
|
||||
$expected = "INSERT INTO test2 (column1, column2, column3) VALUES (1, 'Old', '2014-08-20 18:47:00') ON CONFLICT (column1) DO UPDATE SET column2 = EXCLUDED.column2, column3 = EXCLUDED.column3";
|
||||
|
||||
$postgresql = pg4wp_rewrite($sql);
|
||||
$this->assertSame(trim($expected), trim($postgresql));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected function setUp(): void
|
||||
|
Reference in New Issue
Block a user