From: Wallace Reis Date: Thu, 23 Apr 2009 15:37:12 +0000 (+0000) Subject: Added CREATE VIEW subrules for mysql parser X-Git-Tag: v0.11008~184 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=22b9814ae49dd04433213e7f3c3cab8f1f9640df;p=dbsrgits%2FSQL-Translator.git Added CREATE VIEW subrules for mysql parser --- diff --git a/Changes b/Changes index a8c17e7..9a50894 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,7 @@ # ---------------------------------------------------------- * Properly quote absolute table names in the MySQL producer * Added semi-colon for (DROP|CREATE) TYPE statements in the Pg producer (wreis) +* Added CREATE VIEW subrules for mysql parser (wreis) # ---------------------------------------------------------- # 0.09004 2009-02-13 diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index c4d5aec..5698f74 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -343,16 +343,12 @@ create : CREATE PROCEDURE NAME not_delimiter "$delimiter" PROCEDURE : /procedure/i | /function/i -create : CREATE algorithm(?) /view/i NAME not_delimiter "$delimiter" +create : CREATE replace(?) algorithm(?) /view/i NAME not_delimiter "$delimiter" { @table_comments = (); - my $view_name = $item[4]; - my $sql; - if (scalar(@{$item[2]}) == 1) { - $sql = "$item[1] $item[2][0] $item[3] $item[4] $item[5]"; - } else { - $sql = "$item[1] $item[3] $item[4] $item[5]"; - } + my $view_name = $item[5]; + my $sql = join(q{ }, grep { length } $item[1], $item[2]->[0], $item[3]->[0]) + . " $item[4] $item[5] $item[6]"; # Hack to strip database from function calls in SQL $sql =~ s#`\w+`\.(`\w+`\()##g; @@ -362,6 +358,8 @@ create : CREATE algorithm(?) /view/i NAME not_delimiter "$delimiter" $views{ $view_name }{'sql'} = $sql; } +replace : /or replace/i + algorithm : /algorithm/i /=/ WORD { $return = "$item[1]=$item[3]"; diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index 972a095..919de25 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -11,7 +11,7 @@ use SQL::Translator::Utils qw//; use Test::SQL::Translator qw(maybe_plan); BEGIN { - maybe_plan(293, "SQL::Translator::Parser::MySQL"); + maybe_plan(295, "SQL::Translator::Parser::MySQL"); SQL::Translator::Parser::MySQL->import('parse'); } @@ -510,6 +510,32 @@ BEGIN { `m`.`user_id` AS `user_access` from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */; DELIMITER ;; + /*!50001 CREATE */ + /*! VIEW `vs_asset2` AS + select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`, + `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`, + `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`, + `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`, + `a`.`foreign_asset_id` AS `foreign_asset_id`, + `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`, + `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`, + `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`, + `m`.`user_id` AS `user_access` + from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */; + DELIMITER ;; + /*!50001 CREATE OR REPLACE */ + /*! VIEW `vs_asset3` AS + select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`, + `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`, + `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`, + `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`, + `a`.`foreign_asset_id` AS `foreign_asset_id`, + `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`, + `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`, + `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`, + `m`.`user_id` AS `user_access` + from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */; + DELIMITER ;; /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 FUNCTION `ap_from_millitime_nullable`( millis_since_1970 BIGINT ) RETURNS timestamp DETERMINISTIC BEGIN @@ -592,9 +618,11 @@ BEGIN { is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' ); my @views = $schema->get_views; - is( scalar @views, 1, 'Right number of views (1)' ); - my $view1 = shift @views; + is( scalar @views, 3, 'Right number of views (3)' ); + my ($view3, $view1, $view2) = @views; is( $view1->name, 'vs_asset', 'Found "vs_asset" view' ); + is( $view2->name, 'vs_asset2', 'Found "vs_asset2" view' ); + is( $view3->name, 'vs_asset3', 'Found "vs_asset3" view' ); like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm"); like($view1->sql, qr/vs_asset/, "Detected view vs_asset"); unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");