adding callbacks to Translator.pm to allow mangling of PK/FK/table names/package...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Validator.pm
index 71450bb..fe64ba7 100644 (file)
@@ -1,10 +1,11 @@
 package SQL::Translator::Validator;
 
 # ----------------------------------------------------------------------
-# $Id: Validator.pm,v 1.3 2002-06-11 12:09:13 dlc Exp $
+# $Id: Validator.pm,v 1.8 2003-04-17 13:42:44 dlc Exp $
 # ----------------------------------------------------------------------
-# Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
-#                    darren chamberlain <darren@cpan.org>
+# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
+#                    darren chamberlain <darren@cpan.org>,
+#                    Chris Mungall <cjm@fruitfly.org>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -23,7 +24,7 @@ package SQL::Translator::Validator;
 
 use strict;
 use vars qw($VERSION @EXPORT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
 
 use Exporter;
 use base qw(Exporter);
@@ -66,7 +67,7 @@ sub validate {
                 "Table `$table' is not a HASH reference";
         }
 
-        # Table must contain three elements: type, indeces, and fields
+        # Table must contain three elements: type, indices, and fields
         # XXX If there are other keys, is this an error?
         unless (exists $table_data->{"type"}) {
             return by_context $wa, 0, "Missing type for table `$table'";
@@ -76,17 +77,19 @@ sub validate {
         }
 
         # Indices: array of hashes
-        unless (defined $table_data->{"indeces"} &&
-                UNIVERSAL::isa($table_data->{"indeces"}, "ARRAY")) {
+        unless (defined $table_data->{"indices"} &&
+                UNIVERSAL::isa($table_data->{"indices"}, "ARRAY")) {
             return by_context $wa, 0, "Indices is missing or is not an ARRAY";
         } else {
-            my @indeces = @{$table_data->{"indeces"}};
+            my @indices = @{$table_data->{"indices"}};
             $log .= "\n\tIndices:";
-            if (@indeces) {
-                for my $index (@indeces) {
+            if (@indices) {
+                for my $index (@indices) {
+                    next unless ref($index) eq 'HASH';
+                    next unless scalar keys %$index;
                     $log .= "\n\t\t" . ($index->{"name"} || "(unnamed)")
                          .  " on "
-                         .  join ", ", @{$index->{"fields"}};
+                         .  join ", ", @{$index->{"fields"} ||= []};
                 }
             } else {
                 $log .= " none defined";
@@ -129,8 +132,7 @@ SQL::Translator::Validate - Validate that a data structure is correct
 
 =head1 SYNOPSIS
 
-  print "1..1\n";
-
+  use Test::More plan tests => 1;
   use SQL::Translator;
   use SQL::Translator::Validator;
 
@@ -139,8 +141,7 @@ SQL::Translator::Validate - Validate that a data structure is correct
   # Default producer passes the data structure through unchanged
   my $parsed = $tr->translate($datafile);
 
-  print "not " unless validate($parsed);
-  print "ok 1 # data structure looks OK\n";
+  ok(validate($parsed), "data structure conformance to definition");
 
 =head1 DESCRIPTION
 
@@ -157,12 +158,15 @@ testing tool (every SQL::Translator install will have this module),
 or, potentially, even as a runtime assertion for producers you don't
 trust:
 
-  $tr->producer(\&paranoid_producer);
+  $tr->producer(\&paranoid_producer, real_producer => "MySQL");
   sub paranoid_producer {
       my ($tr, $data) = @_;
-      return unless validate($data);
+      validate($data) or die "You gave me crap!" 
 
-      # continue...
+      # Load real producer, and execute it
+      $tr->producer($tr->producer_args->{'real_producer'});
+      return $tr->produce($data);
+  }
 
 SQL::Translator::Validator can also be used as a reporting tool.  When
 B<validate> is called in a list context, the second value returned