From a810cfb7dadd16014d636f3116f8cdc69c5d8809 Mon Sep 17 00:00:00 2001 From: Matthew Bucci Date: Tue, 7 Nov 2023 15:50:52 -0800 Subject: [PATCH] cleanup AlterTableSQLRewriter --- pg4wp/rewriters/AlterTableSQLRewriter.php | 130 +++++++++++++++------ pg4wp/rewriters/CreateTableSQLRewriter.php | 65 ++++++----- 2 files changed, 129 insertions(+), 66 deletions(-) diff --git a/pg4wp/rewriters/AlterTableSQLRewriter.php b/pg4wp/rewriters/AlterTableSQLRewriter.php index b077b2e..563d4e7 100644 --- a/pg4wp/rewriters/AlterTableSQLRewriter.php +++ b/pg4wp/rewriters/AlterTableSQLRewriter.php @@ -2,50 +2,77 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter { + private $stringReplacements = [ + 'bigint(20)' => 'bigint', + 'bigint(10)' => 'int', + 'int(11)' => 'int', + '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 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 utf8_general_ci' => '', + + // For flash-album-gallery plugin + 'tinyint' => 'smallint' + ]; + public function rewrite(): string { - // List of types translations (the key is the mysql one, the value is the text to use instead) - $typeTranslations = array( - 'bigint(20)' => 'bigint', - 'bigint(10)' => 'int', - 'int(11)' => 'int', - '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 utf8' => '', + $sql = $this->original(); - // WP 2.7.1 compatibility - 'int(4)' => 'smallint', + if (str_contains($sql, 'CHANGE COLUMN')) { + $sql = $this->rewriteChangeColumn($sql); + } + if (str_contains($sql, 'ALTER COLUMN')) { + $sql = $this->rewriteAlterColumn($sql); + } + if (str_contains($sql, 'ADD COLUMN')) { + $sql = $this->rewriteAddColumn($sql); + } + if (str_contains($sql, 'ADD KEY') || str_contains($sql, 'ADD UNIQUE KEY')) { + $sql = $this->rewriteAddKey($sql); + } + if (str_contains($sql, 'DROP INDEX')) { + $sql = $this->rewriteDropIndex($sql); + } + if (str_contains($sql, 'DROP PRIMARY KEY')) { + $sql = $this->rewriteDropPrimaryKey($sql); + } - // For WPMU (starting with WP 3.2) - 'tinyint(2)' => 'smallint', - 'tinyint(1)' => 'smallint', - "enum('0','1')" => 'smallint', - 'COLLATE utf8_general_ci' => '', + return $sql; + } - // For flash-album-gallery plugin - 'tinyint' => 'smallint', - ); + private function rewriteChangeColumn(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+CHANGE COLUMN\s+([^\s]+)\s+([^\s]+)\s+([^ ]+)( unsigned|)\s*(NOT NULL|)\s*(default (.+)|)/'; + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $col = $matches[2]; $newname = $matches[3]; $type = strtolower($matches[4]); - if(isset($typeTranslations[$type])) { - $type = $typeTranslations[$type]; + if(isset($this->stringReplacements[$type])) { + $type = $this->stringReplacements[$type]; } $unsigned = $matches[5]; $notnull = $matches[6]; $default = $matches[7]; $defval = $matches[8]; - if(isset($typeTranslations[$defval])) { - $defval = $typeTranslations[$defval]; + if(isset($this->stringReplacements[$defval])) { + $defval = $this->stringReplacements[$defval]; } $newq = "ALTER TABLE $table ALTER COLUMN $col TYPE $type"; if(!empty($notnull)) { @@ -59,29 +86,43 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter } $sql = $newq; } + + return $sql; + } + + private function rewriteAlterColumn(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+ALTER COLUMN\s+/'; + if(1 === preg_match($pattern, $sql)) { // Translate default values $sql = str_replace( - array_keys($typeTranslations), - array_values($typeTranslations), + array_keys($this->stringReplacements), + array_values($this->stringReplacements), $sql ); } + + return $sql; + } + + private function rewriteAddColumn(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+([^\s]+)\s+([^ ]+)( unsigned|)\s+(NOT NULL|)\s*(default (.+)|)/'; + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $col = $matches[2]; $type = strtolower($matches[3]); - if(isset($typeTranslations[$type])) { - $type = $typeTranslations[$type]; + if(isset($this->stringReplacements[$type])) { + $type = $this->stringReplacements[$type]; } $unsigned = $matches[4]; $notnull = $matches[5]; $default = $matches[6]; $defval = $matches[7]; - if(isset($typeTranslations[$defval])) { - $defval = $typeTranslations[$defval]; + if(isset($this->stringReplacements[$defval])) { + $defval = $this->stringReplacements[$defval]; } $newq = "ALTER TABLE $table ADD COLUMN $col $type"; if(!empty($default)) { @@ -92,7 +133,14 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter } $sql = $newq; } + + return $sql; + } + + private function rewriteAddKey(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+ADD (UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[^\(\)]+|\([^\(\)]+\))+)\)/'; + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $unique = $matches[2]; @@ -107,13 +155,27 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter $index = $table . '_' . $index; $sql = "CREATE {$unique}INDEX $index ON $table ($columns)"; } + + return $sql; + } + + private function rewriteDropIndex(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+DROP INDEX\s+([^\s]+)/'; + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $index = $matches[2]; $sql = "DROP INDEX ${table}_${index}"; } + + return $sql; + } + + private function rewriteDropPrimaryKey(string $sql): string + { $pattern = '/ALTER TABLE\s+(\w+)\s+DROP PRIMARY KEY/'; + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $sql = "ALTER TABLE ${table} DROP CONSTRAINT ${table}_pkey"; diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index ce0dc2f..fd31e4e 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -2,39 +2,40 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter { + private $stringReplacements = [ + 'bigint(20)' => 'bigint', + 'bigint(10)' => 'int', + 'int(11)' => 'int', + '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 { - // List of types translations (the key is the mysql one, the value is the text to use instead) - $typeTranslations = array( - 'bigint(20)' => 'bigint', - 'bigint(10)' => 'int', - 'int(11)' => 'int', - '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', - ); - $sql = $this->original(); + + $sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql); $pattern = '/CREATE TABLE [`]?(\w+)[`]?/'; preg_match($pattern, $sql, $matches); @@ -45,8 +46,8 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter // Translate types and some other replacements $sql = str_replace( - array_keys($typeTranslations), - array_values($typeTranslations), + array_keys($this->stringReplacements), + array_values($this->stringReplacements), $sql );