[Bast-commits] r3813 - in DBIx-Class/0.08/trunk: . lib/DBIx/Class lib/SQL/Translator/Parser/DBIx t t/lib/DBICTest/Schema

ash at dev.catalyst.perl.org ash at dev.catalyst.perl.org
Fri Oct 12 11:26:55 GMT 2007


Author: ash
Date: 2007-10-12 11:26:55 +0100 (Fri, 12 Oct 2007)
New Revision: 3813

Modified:
   DBIx-Class/0.08/trunk/Changes
   DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSource.pm
   DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSourceProxy.pm
   DBIx-Class/0.08/trunk/lib/SQL/Translator/Parser/DBIx/Class.pm
   DBIx-Class/0.08/trunk/t/86sqlt.t
   DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/Artist.pm
Log:
Add an add_index method on ResultSource (and proxy classes)

Modified: DBIx-Class/0.08/trunk/Changes
===================================================================
--- DBIx-Class/0.08/trunk/Changes	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/Changes	2007-10-12 10:26:55 UTC (rev 3813)
@@ -8,6 +8,8 @@
           clash
         - InflateColumn::DateTime now accepts an extra parameter of timezone
           to set timezone on the DT object (thanks Sergio Salvi)
+        - ResultSource now has an add_index method to add indices for when
+          using SQL::Translator to create tables/SQL.
 
 0.08007 2007-09-04 19:36:00
         - patch for Oracle datetime inflation (abram at arin.net)

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSource.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSource.pm	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSource.pm	2007-10-12 10:26:55 UTC (rev 3813)
@@ -13,7 +13,7 @@
 __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns
   _columns _primaries _unique_constraints name resultset_attributes
   schema from _relationships column_info_from_storage source_info
-  source_name/);
+  source_name _indices/);
 
 __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class
   result_class/);
@@ -55,6 +55,7 @@
   $new->{_relationships} = { %{$new->{_relationships}||{}} };
   $new->{name} ||= "!!NAME NOT SET!!";
   $new->{_columns_info_loaded} ||= 0;
+  $new->_indices([]) unless $new->_indices;
   return $new;
 }
 
@@ -449,6 +450,30 @@
 
 sub storage { shift->schema->storage; }
 
+=head2 add_index
+
+Add an index to the result source. This has no effect for DBIx::Class - it is
+just used for creating SQL with L<SQL::Translator>. Takes the same arguments
+as L<SQL::Translator::Schema::Table::add_index>.
+
+=cut
+
+sub add_index {
+  my ($self, $idx) = @_;
+
+  push @{ $self->_indices }, $idx;
+}
+
+=head2 indicies
+
+Returns list of secondary (i.e. non unique) indicies created on this table.
+
+=cut
+
+sub indices {
+  return @{ shift->_indices };
+}
+
 =head2 add_relationship
 
   $source->add_relationship('relname', 'related_source', $cond, $attrs);

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSourceProxy.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSourceProxy.pm	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSourceProxy.pm	2007-10-12 10:26:55 UTC (rev 3813)
@@ -104,4 +104,12 @@
   shift->result_source_instance->relationship_info(@_);
 }
 
+sub add_index {
+  shift->result_source_instance->add_index(@_);
+}
+
+sub indices {
+  shift->result_source_instance->indices(@_);
+}
+
 1;

Modified: DBIx-Class/0.08/trunk/lib/SQL/Translator/Parser/DBIx/Class.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/SQL/Translator/Parser/DBIx/Class.pm	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/lib/SQL/Translator/Parser/DBIx/Class.pm	2007-10-12 10:26:55 UTC (rev 3813)
@@ -5,6 +5,8 @@
 
 # Some mistakes the fault of Matt S Trout
 
+# Others the fault of Ash Berlin
+
 use strict;
 use warnings;
 use vars qw($DEBUG $VERSION @EXPORT_OK);
@@ -107,6 +109,11 @@
             }
         }
 
+        foreach my $idx ( $source->indices ) {
+            my $ret = $table->add_index(%$idx)
+              or die $table->error;
+        }
+
         my @rels = $source->relationships();
 
         my %created_FK_rels;

Modified: DBIx-Class/0.08/trunk/t/86sqlt.t
===================================================================
--- DBIx-Class/0.08/trunk/t/86sqlt.t	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/t/86sqlt.t	2007-10-12 10:26:55 UTC (rev 3813)
@@ -10,7 +10,7 @@
 
 my $schema = DBICTest->init_schema;
 
-plan tests => 54;
+plan tests => 55;
 
 my $translator = SQL::Translator->new( 
   parser_args => {
@@ -28,6 +28,7 @@
 ok($output, "SQLT produced someoutput")
   or diag($translator->error);
 
+
 # Note that the constraints listed here are the only ones that are tested -- if
 # more exist in the Schema than are listed here and all listed constraints are
 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
@@ -213,6 +214,14 @@
 #  ],
 );
 
+my %indices = (
+  artist => [
+    {
+      'fields' => ['name']
+    },
+  ]
+);
+
 my $tschema = $translator->schema();
 
 # Test that nonexistent constraints are not found
@@ -244,6 +253,13 @@
   }
 }
 
+for my $table_index (keys %indices) {
+  for my $expected_index ( @{ $indices{$table_index} } ) {
+
+    ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
+  }
+}
+
 # Returns the Constraint object for the specified constraint type, table and
 # columns from the SQL::Translator schema, or undef if no matching constraint
 # is found.
@@ -293,6 +309,34 @@
   return undef; # didn't find a matching constraint
 }
 
+sub get_index {
+  my ($table_name, $index) = @_;
+
+  my $table = $tschema->get_table($table_name);
+
+ CAND_INDEX:
+  for my $cand_index ( $table->get_indices ) {
+   
+    next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
+                    || $index->{type} && $cand_index->type ne $index->{type};
+
+    my %idx_fields = map { $_ => 1 } $cand_index->fields;
+
+    for my $field ( @{ $index->{fields} } ) {
+      next CAND_INDEX unless $idx_fields{$field};
+    }
+
+    %idx_fields = map { $_ => 1 } @{$index->{fields}};
+    for my $field ( $cand_index->fields) {
+      next CAND_INDEX unless $idx_fields{$field};
+    }
+
+    return $cand_index;
+  }
+
+  return undef; # No matching idx
+}
+
 # Test parameters in a FOREIGN KEY constraint other than columns
 sub test_fk {
   my ($expected, $got) = @_;

Modified: DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/Artist.pm
===================================================================
--- DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/Artist.pm	2007-10-10 16:54:45 UTC (rev 3812)
+++ DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/Artist.pm	2007-10-12 10:26:55 UTC (rev 3813)
@@ -41,4 +41,6 @@
   { cascade_copy => 0 } # this would *so* not make sense
 );
 
+__PACKAGE__->add_index({ name => 'artist_name', fields => ['name'],});
+
 1;




More information about the Bast-commits mailing list