From: Fabien Wernli Date: Wed, 10 Feb 2010 18:16:08 +0000 (+0000) Subject: text -> varchar2(4000) instead of clob, get rid of reserved keywords, import oracle_v... X-Git-Tag: v0.11008~69 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=15861005302cd4158c36fadd162c5dac73d2176e;p=dbsrgits%2FSQL-Translator.git text -> varchar2(4000) instead of clob, get rid of reserved keywords, import oracle_version from DBD::Oracle --- diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index 9585c0f..8325328 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -103,27 +103,6 @@ To get this working we removed the slash in those statements in version 0.09002 of L when called in array context. In scalar context the slash will be still there to ensure compatibility with SQLPlus. -=head2 Quotes - -This producer will generate -DDL with or without quotes if L and/or -L are true. - -Quotes will be forced and names capitalised if C and/or C -for the following reserved keywords: - - ACCESS ADD ALL ALTER AND ANY AS ASC AUDIT BETWEEN BY CHAR CHECK - CLUSTER COLUMN COMMENT COMPRESS CONNECT CREATE CURRENT DATE DECIMAL - DEFAULT DELETE DESC DISTINCT DROP ELSE EXCLUSIVE EXISTS FILE FLOAT - FOR FROM GRANT GROUP HAVING IDENTIFIED IMMEDIATE IN INCREMENT - INDEX INITIAL INSERT INTEGER INTERSECT INTO IS LEVEL LIKE LOCK - LONG MAXEXTENTS MINUS MLSLABEL MODE MODIFY NOAUDIT NOCOMPRESS NOT - NOWAIT NULL NUMBER OF OFFLINE ON ONLINE OPTION OR ORDER PCTFREE - PRIOR PRIVILEGES PUBLIC RAW RENAME RESOURCE REVOKE ROW ROWID ROWNUM - ROWS SELECT SESSION SET SHARE SIZE SMALLINT START SUCCESSFUL SYNONYM - SYSDATE TABLE THEN TO TRIGGER UID UNION UNIQUE UPDATE USER VALIDATE - VALUES VARCHAR VARCHAR2 VIEW WHENEVER WHERE WITH - =cut use strict; @@ -154,7 +133,7 @@ my %translate = ( mediumblob => 'blob', longblob => 'blob', tinytext => 'varchar2', - text => 'clob', + text => [ 'varchar2', 4000 ], longtext => 'clob', mediumtext => 'clob', enum => 'varchar2', @@ -200,36 +179,6 @@ my %translate = ( ); # -# Oracle reserved words from: -# http://technet.oracle.com/docs/products/oracle8i/doc_library/\ -# 817_doc/server.817/a85397/ap_keywd.htm -# -my %ora_reserved = map { $_, 1 } qw( - ACCESS ADD ALL ALTER AND ANY AS ASC AUDIT - BETWEEN BY - CHAR CHECK CLUSTER COLUMN COMMENT COMPRESS CONNECT CREATE CURRENT - DATE DECIMAL DEFAULT DELETE DESC DISTINCT DROP - ELSE EXCLUSIVE EXISTS - FILE FLOAT FOR FROM - GRANT GROUP - HAVING - IDENTIFIED IMMEDIATE IN INCREMENT INDEX INITIAL INSERT - INTEGER INTERSECT INTO IS - LEVEL LIKE LOCK LONG - MAXEXTENTS MINUS MLSLABEL MODE MODIFY - NOAUDIT NOCOMPRESS NOT NOWAIT NULL NUMBER - OF OFFLINE ON ONLINE OPTION OR ORDER - PCTFREE PRIOR PRIVILEGES PUBLIC - RAW RENAME RESOURCE REVOKE ROW ROWID ROWNUM ROWS - SELECT SESSION SET SHARE SIZE SMALLINT START - SUCCESSFUL SYNONYM SYSDATE - TABLE THEN TO TRIGGER - UID UNION UNIQUE UPDATE USER - VALIDATE VALUES VARCHAR VARCHAR2 VIEW - WHENEVER WHERE WITH -); - -# # Oracle 8/9 max size of data types from: # http://www.ss64.com/orasyntax/datatypes.html # @@ -250,7 +199,6 @@ my %truncated; # Quote used to escape table, field, sequence and trigger names my $quote_char = '"'; -my $name_sep = '.'; # ------------------------------------------------------------------- sub produce { @@ -260,8 +208,7 @@ sub produce { my $no_comments = $translator->no_comments; my $add_drop_table = $translator->add_drop_table; my $schema = $translator->schema; - $quote_char = $translator->producer_args->{'quote_char'} ||= '"'; - $name_sep = $translator->producer_args->{'name_sep'} ||= '.'; + my $oracle_version = $translator->producer_args->{oracle_version} || 0; my $delay_constraints = $translator->producer_args->{delay_constraints}; my ($output, $create, @table_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs); @@ -406,7 +353,7 @@ sub create_table { for my $f ( $c->fields ) { my $field_def = $table->get_field( $f ) or next; - my $dtype = $translate{ $field_def->data_type } or next; + my $dtype = $translate{ ref $field_def->data_type eq "ARRAY" ? $field_def->data_type->[0] : $field_def->data_type} or next; if ( $WARN && $dtype =~ /clob/i ) { warn "Oracle will not allow UNIQUE constraints on " . "CLOB field '" . $field_def->table->name . '.' . @@ -623,10 +570,14 @@ sub create_field { $data_type = 'varchar2'; } else { - $data_type = defined $translate{ $data_type } ? - $translate{ $data_type } : - $data_type; - $data_type ||= 'varchar2'; + if (defined $translate{ $data_type }) { + if (ref $translate{ $data_type } eq "ARRAY") { + ($data_type,$size[0]) = @{$translate{ $data_type }}; + } else { + $data_type = $translate{ $data_type }; + } + } + $data_type ||= 'varchar2'; } # ensure size is not bigger than max size oracle allows for data type @@ -836,16 +787,7 @@ sub mk_name { # ------------------------------------------------------------------- sub quote { my ($name, $q) = @_; - if ( $q ) { - "$q$name$q"; - } elsif ($ora_reserved { uc $name }) { - # convert to upper case to be consistent with oracle - # when no quotes are being used - $name = uc $name; - "$quote_char$name$quote_char"; - } else { - $name; - } + $q ? "$q$name$q" : $name; } diff --git a/t/03mysql-to-oracle.t b/t/03mysql-to-oracle.t index 7fbe09b..885ab7a 100644 --- a/t/03mysql-to-oracle.t +++ b/t/03mysql-to-oracle.t @@ -25,7 +25,9 @@ BEGIN { my $tr = SQL::Translator->new( parser => "MySQL", - producer => "Oracle" + producer => "Oracle", + quote_table_names => 0, + quote_field_names => 0, ); my $output = $tr->translate(\$create); diff --git a/t/51-xml-to-oracle.t b/t/51-xml-to-oracle.t index 6b611a8..c00261d 100644 --- a/t/51-xml-to-oracle.t +++ b/t/51-xml-to-oracle.t @@ -19,6 +19,8 @@ my $xmlfile = "$Bin/data/xml/schema.xml"; my $sqlt; $sqlt = SQL::Translator->new( no_comments => 1, + quote_table_names => 0, + quote_field_names => 0, show_warnings => 0, add_drop_table => 1, ); @@ -44,7 +46,7 @@ my $want = [ 'CREATE TABLE Basic ( id number(10) NOT NULL, title varchar2(100) DEFAULT \'hello\' NOT NULL, - description clob DEFAULT \'\', + description varchar2(4000) DEFAULT \'\', email varchar2(500), explicitnulldef varchar2, explicitemptystring varchar2 DEFAULT \'\', @@ -108,7 +110,7 @@ CREATE SEQUENCE sq_Basic_id01; CREATE TABLE Basic ( id number(10) NOT NULL, title varchar2(100) DEFAULT 'hello' NOT NULL, - description clob DEFAULT '', + description varchar2(4000) DEFAULT '', email varchar2(500), explicitnulldef varchar2, explicitemptystring varchar2 DEFAULT '',