Author: spadkins
Date: Sun Feb 22 18:05:10 2009
New Revision: 12529

Modified:
   p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm

Log:
read indexes from database using the old method in case your DBD::mysql is 
really old (ca. 3.008)

Modified: p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       Sun Feb 22 
18:05:10 2009
@@ -781,6 +781,10 @@
     my ($self, $table) = @_;
     my $dbh = $self->{dbh};
     my $primary_key = [ map {lc} $dbh->primary_key(undef, $self->{dbname}, 
$table) ];
+    if (!$primary_key || $#$primary_key == -1) {
+        $self->_set_keys_from_source($table);
+        $primary_key = $self->{table}{$table}{primary_key};
+    }
     &App::sub_exit($primary_key) if ($App::trace);
     return($primary_key);
 }
@@ -813,6 +817,56 @@
     return($alternate_keys);
 }
 
+sub _set_keys_from_source {
+    &App::sub_entry if ($App::trace);
+    my ($self, $table) = @_;
+
+    return if (! $table);
+    my $table_def = $self->{table}{$table};
+    return if (! $table_def);
+    my $dbh = $self->{dbh};
+
+    # if not defined at all, try to get it from the database
+    my (@primary_key, @alternate_key, @index, @key, $key_name, $non_unique);
+    if ($table_def->{phys_table} && (! defined $table_def->{primary_key} || ! 
defined $table_def->{alternate_key})) {
+        local $dbh->{FetchHashKeyName} = 'NAME_lc';
+        my $sth = $dbh->prepare("SHOW INDEX FROM $table");
+        my $hashes = $dbh->selectall_arrayref($sth, { Columns=>{} });
+        foreach my $hash (@$hashes) {
+             if ($key_name && $hash->{key_name} ne $key_name) {
+                 if ($key_name eq 'PRIMARY') {
+                     @primary_key = @key;
+                 }                          
+                 elsif ($non_unique) {
+                     push(@index, [...@key]);
+                 }                          
+                 else {
+                     push(@alternate_key, [...@key]);
+                 }                          
+                 @key = ();
+             }
+             $non_unique = $hash->{non_unique};
+             $key_name = $hash->{key_name};
+             push(@key, $hash->{column_name});
+         }
+         if ($key_name) {
+             if ($key_name eq 'PRIMARY') {
+                 @primary_key = @key;
+             }                          
+             elsif ($non_unique) {
+                 push(@index, [...@key]);
+             }                          
+             else {
+                 push(@alternate_key, [...@key]);
+             }                          
+         }
+        
+         $table_def->{primary_key} = \...@primary_key if 
(!$table_def->{primary_key});
+         $table_def->{alternate_key} = \...@alternate_key if 
(!$table_def->{alternate_key} && $#alternate_key > -1);
+    }
+    &App::sub_exit() if ($App::trace);
+}
+
 sub _column_metadata_specifiers {
     &App::sub_entry if ($App::trace);
     my ($self, $table) = @_;

Reply via email to