From: Peter Rabbitson Date: Sun, 15 Feb 2009 22:38:03 +0000 (+0000) Subject: Support for mysql fully qualified table names (db.table) by Debolaz X-Git-Tag: v0.11008~225 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8d693a858511dc5ff4c19ded853954f909b8bd77;p=dbsrgits%2FSQL-Translator.git Support for mysql fully qualified table names (db.table) by Debolaz --- diff --git a/Changes b/Changes index 60aa9f0..f38dd30 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,9 @@ # ---------------------------------------------------------- +# x.xxxxx xxxx-xx-xx +# ---------------------------------------------------------- +* Properly quote absolute table names in the MySQL producer + +# ---------------------------------------------------------- # 0.09004 2009-02-13 # ---------------------------------------------------------- * Add support for temporary tables in Pg (nachos) diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index c6af310..1ded991 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -369,7 +369,7 @@ sub create_table my $qt = $options->{quote_table_names} || ''; my $qf = $options->{quote_field_names} || ''; - my $table_name = $table->name; + my $table_name = quote_table_name($table->name, $qt); debug("PKG: Looking at table '$table_name'\n"); # @@ -377,9 +377,9 @@ sub create_table # my $create = ''; my $drop; - $create .= "--\n-- Table: $qt$table_name$qt\n--\n" unless $options->{no_comments}; - $drop = qq[DROP TABLE IF EXISTS $qt$table_name$qt] if $options->{add_drop_table}; - $create .= "CREATE TABLE $qt$table_name$qt (\n"; + $create .= "--\n-- Table: $table_name\n--\n" unless $options->{no_comments}; + $drop = qq[DROP TABLE IF EXISTS $table_name] if $options->{add_drop_table}; + $create .= "CREATE TABLE $table_name (\n"; # # Fields @@ -428,6 +428,14 @@ sub create_table return $drop ? ($drop,$create) : $create; } +sub quote_table_name { + my ($table_name, $qt) = @_; + + $table_name =~ s/\./$qt.$qt/g; + + return "$qt$table_name$qt"; +} + sub generate_table_options { my ($table, $options) = @_; diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index fbd58e8..ae2c64d 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -74,7 +74,7 @@ schema: name: idx_unique_name thing2: - name: thing2 + name: some.thing2 extra: order: 2 fields: @@ -142,8 +142,8 @@ my @stmts = ( UNIQUE `idx_unique_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci", -"DROP TABLE IF EXISTS `thing2`", -"CREATE TABLE `thing2` ( +"DROP TABLE IF EXISTS `some`.`thing2`", +"CREATE TABLE `some`.`thing2` ( `id` integer, `foo` integer, `foo2` integer,