Files
postgresql-for-wordpress/pg4wp/rewriters/CreateTableSQLRewriter.php

109 lines
3.7 KiB
PHP
Raw Normal View History

<?php
class CreateTableSQLRewriter extends AbstractSQLRewriter
{
2023-11-07 15:50:52 -08:00
private $stringReplacements = [
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
2023-11-26 01:34:10 -08:00
'int(1)' => 'smallint',
2023-11-07 15:50:52 -08:00
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
'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',
'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' => '',
// For flash-album-gallery plugin
'tinyint' => 'smallint'
];
public function rewrite(): string
{
$sql = $this->original();
2023-11-07 15:50:52 -08:00
$sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
preg_match($pattern, $sql, $matches);
$table = $matches[1];
// Remove trailing spaces
$sql = trim($sql) . ';';
// Translate types and some other replacements
2023-11-26 01:34:10 -08:00
$sql = str_ireplace(
2023-11-07 15:50:52 -08:00
array_keys($this->stringReplacements),
array_values($this->stringReplacements),
$sql
);
2023-11-26 15:07:33 -08:00
// bigint
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
preg_match($pattern, $sql, $matches);
if($matches) {
2023-11-26 15:07:33 -08:00
$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);
}
}
2023-11-26 01:34:10 -08:00
// Support for UNIQUE INDEX creation
$pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
$unique = $match[1];
$index = $match[2];
$columns = $match[3];
$columns = preg_replace('/\(\d+\)/', '', $columns);
// Workaround for index name duplicate
$index = $table . '_' . $index;
$sql .= "\nCREATE {$unique}INDEX $index ON $table ($columns);";
}
}
// Now remove handled indexes
$sql = preg_replace($pattern, '', $sql);
2023-11-26 15:17:26 -08:00
$pattern = "/(,\s*)?UNIQUE KEY\s+[a-zA-Z0-9_]+\s+(\([a-zA-Z0-9_,\s]+\))/";
$replacement = "$1UNIQUE $2";
$sql = preg_replace($pattern, $replacement, $sql);
2023-11-26 01:34:10 -08:00
return $sql;
}
}