Improve trigger 'scope' attribute support (RT#119997)
Dagfinn Ilmari Mannsåker [Thu, 26 Jan 2017 17:42:30 +0000 (17:42 +0000)]
- JSON, YAML and XML producers
- XML and SQLite parsers

TODO: Unify with the DB2 parser/producer's 'granularity' extra attribute

13 files changed:
Changes
lib/SQL/Translator/Parser/SQLite.pm
lib/SQL/Translator/Parser/XML/SQLFairy.pm
lib/SQL/Translator/Producer/JSON.pm
lib/SQL/Translator/Producer/XML/SQLFairy.pm
lib/SQL/Translator/Producer/YAML.pm
lib/Test/SQL/Translator.pm
t/16xml-parser.t
t/17sqlfxml-producer.t
t/23json.t
t/24yaml.t
t/46xml-to-pg.t
t/data/xml/schema.xml

diff --git a/Changes b/Changes
index 1bcc13a..d9e4ba4 100644 (file)
--- a/Changes
+++ b/Changes
@@ -8,6 +8,8 @@ Changes for SQL::Translator
  * Add support for USING and WHERE on indexes in PostgreSQL producer
    and parser (RT#63814, GH#52)
  * Improve add_trigger consistency between producers (GH#48)
+ * Add trigger 'scope' attribute support to JSON, YAML and XML producers,
+   and XML and SQLite parsers (RT#119997)
  * Declare dependencies in deterministic order (RT#102859)
  * Multiple speedups of naive internal debugging mechanism (GH#54)
  * Remove dependency on List::MoreUtils ( http://is.gd/lmu_cac_debacle )
index 4787302..251c97b 100644 (file)
@@ -730,6 +730,7 @@ sub parse {
             database_events     => $def->{'db_events'},
             action              => $def->{'action'},
             on_table            => $def->{'on_table'},
+            scope               => 'row', # SQLite only supports row triggers
         );
     }
 
index 4e6e874..14d98b1 100644 (file)
@@ -216,7 +216,7 @@ sub parse {
     foreach (@nodes) {
         my %data = get_tagfields($xp, $_, "sqlf:", qw/
             name perform_action_when database_event database_events fields
-            on_table action order extra
+            on_table action order extra scope
         /);
 
         # back compat
index 890a958..d057d8e 100644 (file)
@@ -149,6 +149,7 @@ sub view_trigger {
         'fields'              => scalar $trigger->fields,
         'on_table'            => scalar $trigger->on_table,
         'action'              => scalar $trigger->action,
+        'scope'               => scalar $trigger->scope,
         keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (),
     };
 }
index 73411f6..ce9e151 100644 (file)
@@ -271,7 +271,7 @@ sub produce {
     xml_obj_children( $xml, $schema,
         tag    => 'trigger',
         methods => [qw/name database_events action on_table perform_action_when
-            fields order extra/],
+            fields order extra scope/],
     );
 
     #
index 49d7029..c5055ff 100644 (file)
@@ -148,6 +148,7 @@ sub view_trigger {
         'fields'              => scalar $trigger->fields,
         'on_table'            => scalar $trigger->on_table,
         'action'              => scalar $trigger->action,
+        'scope'               => scalar $trigger->scope,
         keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (),
     };
 }
index 5803536..9856350 100644 (file)
@@ -284,6 +284,9 @@ sub trigger_ok {
     is( $obj->on_table, $test->{on_table},
         "$t_name    on_table is '$test->{on_table}'" );
 
+    is( $obj->scope, $test->{scope}, "$t_name    scope is '$test->{scope}'" )
+        if exists $test->{scope};
+
     is( $obj->action, $test->{action}, "$t_name    action is '$test->{action}'" );
 
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
index b8fad41..165d11f 100644 (file)
@@ -27,7 +27,7 @@ use constant DEBUG => (exists $opt{d} ? 1 : 0);
 #=============================================================================
 
 BEGIN {
-    maybe_plan(238, 'SQL::Translator::Parser::XML::SQLFairy');
+    maybe_plan(undef, 'SQL::Translator::Parser::XML::SQLFairy');
 }
 
 my $testschema = "$Bin/data/xml/schema.xml";
@@ -235,6 +235,7 @@ schema_ok( $scma, {
             database_events     => 'insert',
             on_table            => 'Basic',
             action              => 'update modified=timestamp();',
+            scope               => 'row',
             extra => {
                 foo => "bar",
                 hello => "world",
@@ -247,6 +248,7 @@ schema_ok( $scma, {
             database_events     => 'insert,update',
             on_table            => 'Basic',
             action              => 'update modified2=timestamp();',
+            scope               => 'row',
             extra => {
                 hello => "aliens",
             },
@@ -269,3 +271,5 @@ schema_ok( $scma, {
     ],
 
 }); # end schema
+
+done_testing;
index cded24b..7989e35 100644 (file)
@@ -193,7 +193,7 @@ $ans = <<EOXML;
   </tables>
   <views></views>
   <triggers>
-    <trigger name="foo_trigger" database_events="insert" on_table="Basic" perform_action_when="after" order="1">
+    <trigger name="foo_trigger" database_events="insert" on_table="Basic" perform_action_when="after" order="1" scope="row">
       <action>update modified=timestamp();</action>
       <extra hello="world" />
     </trigger>
@@ -222,6 +222,7 @@ EOXML
         database_events     => [$database_event],
         table               => $table,
         action              => $action,
+        scope               => 'row',
         extra               => { hello => "world" },
     ) or die $s->error;
 
index cfc721c..0f96e47 100644 (file)
@@ -260,7 +260,8 @@ my $json = to_json(from_json(<<JSON), { canonical => 1, pretty => 1 });
             "name" : "pet_trig",
             "on_table" : "pet",
             "order" : "1",
-            "perform_action_when" : "after"
+            "perform_action_when" : "after",
+            "scope": "row"
          }
       },
       "views" : {
index e177907..89d4cbb 100644 (file)
@@ -213,6 +213,7 @@ schema:
       on_table: pet
       order: 1
       perform_action_when: after
+      scope: row
   views:
     person_pet:
       fields: []
index 8d8f8a6..d9dd327 100644 (file)
@@ -66,11 +66,11 @@ CREATE VIEW "email_list" ( "email" ) AS
 
 DROP TRIGGER IF EXISTS "foo_trigger";
 
-CREATE TRIGGER "foo_trigger" after insert ON "Basic" update modified=timestamp();;
+CREATE TRIGGER "foo_trigger" after insert ON "Basic" FOR EACH row update modified=timestamp();;
 
 DROP TRIGGER IF EXISTS "bar_trigger";
 
-CREATE TRIGGER "bar_trigger" before insert OR update ON "Basic" update modified2=timestamp();;
+CREATE TRIGGER "bar_trigger" before insert OR update ON "Basic" FOR EACH row update modified2=timestamp();;
 
 ALTER TABLE "Basic" ADD FOREIGN KEY ("another_id")
   REFERENCES "Another" ("id") DEFERRABLE;
index b57a4de..048d857 100644 (file)
@@ -99,12 +99,12 @@ Created on Fri Aug 15 15:08:18 2003
 
   <triggers>
       <trigger name="foo_trigger" database_event="insert" on_table="Basic"
-          perform_action_when="after" order="1">
+          perform_action_when="after" order="1" scope="row">
           <action>update modified=timestamp();</action>
           <extra foo="bar" hello="world" bar="baz" />
       </trigger>
       <trigger name="bar_trigger" database_events="insert , update" on_table="Basic"
-          perform_action_when="before" order="1">
+          perform_action_when="before" order="1" scope="row">
           <action>update modified2=timestamp();</action>
           <extra hello="aliens" />
       </trigger>