Fixed a lot of little things in modules, docs, etc. Bugs in sql_translator.pl.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Validator.pm
index 9b14928..41a5935 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Validator;
 
 # ----------------------------------------------------------------------
-# $Id: Validator.pm,v 1.1 2002-03-26 12:46:54 dlc Exp $
+# $Id: Validator.pm,v 1.4 2002-11-20 04:03:03 kycl4rk Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
 #                    darren chamberlain <darren@cpan.org>
@@ -23,7 +23,7 @@ package SQL::Translator::Validator;
 
 use strict;
 use vars qw($VERSION @EXPORT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
 
 use Exporter;
 use base qw(Exporter);
@@ -33,6 +33,9 @@ use Data::Dumper;
 
 sub by_context($$$) { ($_[0]) ? ($_[1], $_[2]) : $_[1]; }
 
+# XXX If called in scalar context, then validate should *not*
+# genertate or return $log.  It's a lot of extra work if we know we
+# are not going to use it.
 sub validate {
     my $data = shift;
     my $wa = wantarray;
@@ -63,7 +66,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'";
@@ -72,15 +75,15 @@ sub validate {
                 "not defined";
         }
 
-        # Indeces: array of hashes
-        unless (defined $table_data->{"indeces"} &&
-                UNIVERSAL::isa($table_data->{"indeces"}, "ARRAY")) {
-            return by_context $wa, 0, "Indeces is missing or is not an ARRAY";
+        # Indices: array of hashes
+        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"}};
-            $log .= "\n\tIndeces:";
-            if (@indeces) {
-                for my $index (@indeces) {
+            my @indices = @{$table_data->{"indices"}};
+            $log .= "\n\tIndices:";
+            if (@indices) {
+                for my $index (@indices) {
                     $log .= "\n\t\t" . ($index->{"name"} || "(unnamed)")
                          .  " on "
                          .  join ", ", @{$index->{"fields"}};
@@ -144,7 +147,7 @@ SQL::Translator::Validate - Validate that a data structure is correct
 When writing a parser module for SQL::Translator, it is helpful to
 have a tool to automatically check the return of your module, to make
 sure that it is returning the Right Thing.  While only a full Producer
-and the associated database can determine if you are producing valud
+and the associated database can determine if you are producing valid
 output, SQL::Translator::Validator can tell you if the basic format of
 the data structure is correct.  While this will not catch many errors,
 it will catch the basic ones.
@@ -161,6 +164,47 @@ trust:
 
       # continue...
 
+SQL::Translator::Validator can also be used as a reporting tool.  When
+B<validate> is called in a list context, the second value returned
+(assuming the data structure is well-formed) is a summary of the
+table's information.  For example, the following table definition
+(MySQL format):
+
+  CREATE TABLE random (
+    id  int(11) not null default 1,
+    seed char(32) not null default 1
+  );
+
+  CREATE TABLE session (
+    foo char(255),
+    id int(11) not null default 1 primary key
+  ) TYPE=HEAP;
+
+Produces the following summary:
+
+    Contains 2 tables.
+    Table 1: random
+            Type: not defined
+            Indices: none defined
+            Fields:
+                    id int (11)
+                            Default: 1
+                            Null: no
+                    seed char (32)
+                            Default: 1
+                            Null: no
+    Table 2: session
+            Type: HEAP
+            Indices:
+                    (unnamed) on id
+            Fields:
+                    foo char (255)
+                            Null: yes
+                    id int (11)
+                            Default: 1
+                            Null: no
+
+
 =head1 EXPORTED FUNCTIONS
 
 SQL::Translator::Validator exports a single function, called