Author: spadkins
Date: Mon Apr  9 11:18:12 2007
New Revision: 9383

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

Log:
make all operations sensitive to repository redirection and table redirection

Modified: p5ee/trunk/App-Repository/lib/App/Repository.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository.pm     (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository.pm     Mon Apr  9 11:18:12 2007
@@ -66,10 +66,10 @@
     $typedef      = $rep->get_type_def($typename);                 # print 
"%$type\n";
     $tablenames   = $rep->get_table_names();                       # print 
"@$tablenames\n";
     $tablelabels  = $rep->get_table_labels();                      # print 
"%$tablelabels\n";
-    $tabledef     = $rep->get_table_def($tablename);               # print 
"%$table\n";
+    $table_def    = $rep->get_table_def($tablename);               # print 
"%$table\n";
     $columnnames  = $rep->get_column_names($tablename);            # print 
"@$columnnames\n";
     $columnlabels = $rep->get_column_labels($tablename);           # print 
"%$columnlabels\n";
-    $columndef    = $rep->get_column_def($tablename,$columnname);  # print 
"%$column\n";
+    $column_def   = $rep->get_column_def($tablename,$columnname);  # print 
"%$column\n";
 
     #################################################
     # RELATIONAL
@@ -558,7 +558,6 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $col, $value, $options) = @_;
     die "set(): params undefined" if (!defined $params);
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
     my ($nrows);
     if ($col && ref($col) eq "") {
         $nrows = $self->set_row($table, $params, [$col], [$value], $options);
@@ -603,9 +602,13 @@
 
     my ($row);
     my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $row = $rep->get_row($table, $params, $cols, $options);
+        $row = $rep->get_row($realtable, $params, $cols, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $row = $self->get_row($realtable, $params, $cols, $options);
     }
     else {
         $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
@@ -691,10 +694,14 @@
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "set_row(): params undefined" if (!defined $params);
     my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     my ($nrows);
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $nrows = $rep->set_row($table, $params, $cols, $row, $options);
+        $nrows = $rep->set_row($realtable, $params, $cols, $row, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $nrows = $self->set_row($realtable, $params, $cols, $row, $options);
     }
     else {
         $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
@@ -711,14 +718,14 @@
             my ($hash, $columns);
             if ($cols) {
                 $hash = $cols;
-                my $tabledef = $self->get_table_def($table);
-                $columns = $tabledef->{columns};
+                my $table_def = $self->{table}{$table};
+                $columns = $table_def->{columns};
                 $columns = [ keys %$hash ] if (!$columns);
             }
             else {
                 $hash = $params;     # a hashref was passed in instead of 
cols/row
-                my $tabledef = $self->get_table_def($table);
-                $columns = $tabledef->{columns};
+                my $table_def = $self->{table}{$table};
+                $columns = $table_def->{columns};
                 $columns = [ keys %$hash ] if (!$columns);
                 $params = undef;
             }
@@ -894,21 +901,9 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table) = @_;
     my ($columns);
-    my $table_def = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
     $columns = $table_def->{default_columns} || $table_def->{columns};
     $columns = $table_def->{columns} if ($columns eq "configured");
-    if (!$columns) {
-        my $table_def = $self->{table}{$table};
-        my $repname = $table_def->{repository};
-        my $realtable = $table_def->{table} || $table;
-        if (defined $repname && $repname ne $self->{name}) {
-            my $rep = $self->{context}->repository($repname);
-            $columns = $rep->_get_default_columns($realtable);
-        }
-        elsif (defined $realtable && $realtable ne $table) {
-            $columns = $self->_get_default_columns($realtable);
-        }
-    }
     die "Unknown default columns [$columns]" if (ref($columns) ne "ARRAY");
     &App::sub_exit($columns) if ($App::trace);
     return($columns);
@@ -930,7 +925,8 @@
         $colidx{$col} = $i;
     }
     # Search each {expr} column for what other columns it depends on.
-    my $column_defs = $self->{table}{$table}{column};
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
     for (my $i = 0; $i <= $#$cols; $i++) {
         $col = $cols->[$i];
         # The column may have an explicit definition of the columns it depends 
on.
@@ -975,7 +971,8 @@
     my ($self, $table, $columns) = @_;
     my $contains_expr = 0;
     my ($column);
-    my $column_defs = $self->{table}{$table}{column};
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
     for (my $i = 0; $i <= $#$columns; $i++) {
         $column = $columns->[$i];
         if ($column_defs->{$column}{expr}) {
@@ -1164,18 +1161,13 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $options) = @_;
     die "get_object(): params undefined" if (!defined $params);
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
 
     my ($object);
-    #if (ref($cols) eq "ARRAY" && $#$cols == -1 && !ref($params)) {
-    #    $object = {};
-    #}
-    #else {
-        $object = $self->get_hash($table, $params, $cols, $options);
-    #}
+    $object = $self->get_hash($table, $params, $cols, $options);
 
     if ($object) {
-        my $class = $tabledef->{class} || "App::RepositoryObject";
+        my $class = $table_def->{class} || "App::RepositoryObject";
         # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
         $class = $self->_get_qualified_class($class, $object) if (ref($class));
         App->use($class);
@@ -1187,7 +1179,7 @@
             $object->{_key} = $params;
         }
         else {
-            my $primary_key = $tabledef->{primary_key};
+            my $primary_key = $table_def->{primary_key};
             $primary_key = [$primary_key] if (ref($primary_key) eq "");
             my ($key);
             if ($primary_key) {
@@ -1245,15 +1237,15 @@
 sub get_objects {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $options) = @_;
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
     my $objects = $self->get_hashes($table, $params, $cols, $options);
-    my $primary_key = $tabledef->{primary_key};
+    my $primary_key = $table_def->{primary_key};
     $primary_key = [$primary_key] if (ref($primary_key) eq "");
     my ($key, $class, %used);
     foreach my $object (@$objects) {
         $object->{_repository} = $self;
         $object->{_table} = $table;
-        $class = $tabledef->{class} || "App::RepositoryObject";
+        $class = $table_def->{class} || "App::RepositoryObject";
         # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
         $class = $self->_get_qualified_class($class, $object) if (ref($class));
         if (!$used{$class}) {
@@ -1416,7 +1408,7 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $values, $options) = @_;
     die "set_hash(): params undefined" if (!defined $params);
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
+    die "set_hash(): not implemented";
     &App::sub_exit() if ($App::trace);
 }
 
@@ -1440,12 +1432,14 @@
     my ($self, $row, $table, $params, $cols, $options) = @_;
 
     $options = {} if (!$options);
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
 
-    my ($tabledef, $param, $column, $repop, $colidxs, $colidx, $colvalue, 
$paramvalue);
+    my ($param, $column, $repop, $colidxs, $colidx, $colvalue, $paramvalue);
 
     $colidxs = $options->{cache}{colidx};
     if (!defined $colidxs || ! %$colidxs) {
-        my $columns = $self->{table}{$table}{columns};
+        my $columns = $table_def->{columns};
         die "Columns not defined for table $table" if (!$columns);
         if (!defined $colidxs) {
             $colidxs = {};
@@ -1460,7 +1454,6 @@
     my ($all_params_match, $param_match);
     $all_params_match = 1;      # assume it matches
 
-    $tabledef = $self->{table}{$table};
     foreach $param (keys %$params) {
         $param_match = undef;
         $column = $param;
@@ -1473,7 +1466,7 @@
             $repop = $2;
         }
 
-        if (!defined $tabledef->{column}{$column}) {
+        if (!defined $table_def->{column}{$column}) {
             if ($param =~ /^begin_(.*)/) {
                 $column = $1;
                 $repop = "ge";
@@ -1483,7 +1476,7 @@
                 $repop = "le";
             }
         }
-        next if (!defined $tabledef->{column}{$column});  # skip if the column 
is unknown
+        next if (!defined $table_def->{column}{$column});  # skip if the 
column is unknown
 
         $paramvalue = $params->{$param};
         if (defined $paramvalue) {
@@ -1497,7 +1490,7 @@
                 $param_match = ($colvalue !~ /^$paramvalue$/);
             }
             elsif ($repop eq "in" || $repop eq "eq") {
-                if ($paramvalue =~ /,/ && ! 
$tabledef->{param}{$param}{no_auto_in_param}) {
+                if ($paramvalue =~ /,/ && ! 
$table_def->{param}{$param}{no_auto_in_param}) {
                     $param_match = (",$paramvalue," =~ /,$colvalue,/);
                 }
                 elsif ($paramvalue =~ /^-?[0-9]*\.?[0-9]*$/) {
@@ -1567,10 +1560,10 @@
 
     my ($idx, $native_idx, $column, @newrow);
     $#newrow = $#$cols;  # preallocate
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
     for ($idx = 0; $idx <= $#$cols; $idx++) {
         $column = $cols->[$idx];
-        $native_idx = $tabledef->{column}{$column}{idx};
+        $native_idx = $table_def->{column}{$column}{idx};
         $newrow[$idx] = (defined $native_idx) ? $row->[$native_idx] : undef;
     }
 
@@ -1628,13 +1621,13 @@
     my ($self, $table, $params, $cols, $rows, $options) = @_;
     $params = $self->_params_to_hashref($table, $params) if ($params && 
ref($params) ne "HASH");
 
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->{table}{$table};
 
     my ($primary_key, @keycolidx, $keypos, %keypos, $keys_supplied);
     my ($row, $colidx, $nrows, $success);
     $nrows = 0;
     if (! defined $params) {
-        $primary_key = $tabledef->{primary_key};
+        $primary_key = $table_def->{primary_key};
         $primary_key = [$primary_key] if (ref($primary_key) eq "");
         for ($keypos = 0; $keypos <= $#$primary_key; $keypos++) {
             $keypos{$primary_key->[$keypos]} = $keypos;
@@ -1736,20 +1729,25 @@
 sub insert_row {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $row, $options) = @_;
-    my $repname = $self->{table}{$table}{repository};
     my ($retval);
+    my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $retval = $rep->insert_row($table, $cols, $row, $options);
+        $retval = $rep->insert_row($realtable, $cols, $row, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $retval = $self->insert_row($realtable, $cols, $row, $options);
     }
     else {
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
         my ($hash, $columns);
         my $ref = ref($cols);
         if ($ref && $ref ne "ARRAY") {
             $hash = $cols;     # a hashref was passed in instead of cols/row
-            my $tabledef = $self->get_table_def($table);
+            my $table_def = $self->{table}{$table};
             $columns = [];
-            foreach my $col (@{$tabledef->{columns}}) {
+            foreach my $col (@{$table_def->{columns}}) {
                 if (exists $hash->{$col}) {
                     push(@$columns, $col);
                 }
@@ -1761,9 +1759,9 @@
                 $columns = $cols;
             }
             else {
-                my $tabledef = $self->get_table_def($table);
+                my $table_def = $self->{table}{$table};
                 $columns = [];
-                foreach my $col (@{$tabledef->{columns}}) {
+                foreach my $col (@{$table_def->{columns}}) {
                     if (exists $hash->{$col}) {
                         push(@$columns, $col);
                     }
@@ -1809,7 +1807,7 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $row, $options) = @_;
 
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
 
     my $ref = ref($cols);
     my ($object);
@@ -1829,7 +1827,7 @@
         $object = {};
     }
 
-    my $class = $tabledef->{class} || "App::RepositoryObject";
+    my $class = $table_def->{class} || "App::RepositoryObject";
     # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
     $class = $self->_get_qualified_class($class, $object) if (ref($class));
     App->use($class);
@@ -1857,21 +1855,21 @@
 sub _check_default_and_required_fields {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $hash) = @_;
-    my $tabledef = $self->{table}{$table};
-    my $columns = $tabledef->{column};
-    if ($columns) {
-        foreach my $column (keys %$columns) {
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
+    if ($column_defs) {
+        foreach my $column (keys %$column_defs) {
             if (!defined $hash->{$column}) {
-                if (defined $columns->{$column}{default}) {
-                    $hash->{$column} = $columns->{$column}{default};
+                if (defined $column_defs->{$column}{default}) {
+                    $hash->{$column} = $column_defs->{$column}{default};
                 }
-                elsif (defined $columns->{$column}{not_null}) {
+                elsif (defined $column_defs->{$column}{not_null}) {
                     die "Illegal object value for $table: $column cannot be 
NULL (i.e. undef)";
                 }
             }
         }
     }
-    my $primary_key = $tabledef->{primary_key};
+    my $primary_key = $table_def->{primary_key};
     if ($primary_key) {
         # Watch out for auto-generated primary keys. It's OK for them to be 
NULL.
         #if ($#$primary_key > 0) {
@@ -1882,7 +1880,7 @@
         #    }
         #}
     }
-    my $alternate_keys = $tabledef->{alternate_key};
+    my $alternate_keys = $table_def->{alternate_key};
     if ($alternate_keys) {
         foreach my $alternate_key (@$alternate_keys) {
             foreach my $column (@$alternate_key) {
@@ -1905,18 +1903,23 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $rows, $options) = @_;
     my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     my ($nrows);
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $nrows = $rep->insert_rows($table, $cols, $rows, $options);
+        $nrows = $rep->insert_rows($realtable, $cols, $rows, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $nrows = $self->insert_rows($realtable, $cols, $rows, $options);
     }
     else {
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
         my ($hashes, $hash, $columns);
         if (ref($cols) eq "ARRAY" && ref($cols->[0]) eq "HASH") {
             $hashes = $cols;     # an array of hashrefs was passed in instead 
of cols/rows
             $hash = $hashes->[0];
-            my $tabledef = $self->get_table_def($table);
-            $columns = $tabledef->{columns};
+            my $table_def = $self->{table}{$table};
+            $columns = $table_def->{columns};
             $columns = [ keys %$hash ] if (!$columns);
         }
         elsif (ref($rows) eq "ARRAY" && ref($rows->[0]) eq "HASH") {
@@ -1926,8 +1929,8 @@
                 $columns = $cols;
             }
             else {
-                my $tabledef = $self->get_table_def($table);
-                $columns = $tabledef->{columns};
+                my $table_def = $self->{table}{$table};
+                $columns = $table_def->{columns};
                 $columns = [ keys %$hash ] if (!$columns || $#$columns == -1);
             }
         }
@@ -1960,12 +1963,17 @@
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "delete(): params undefined" if (!defined $params);
     my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     my ($retval);
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $retval = $rep->delete($table, $cols, $row, $options);
+        $retval = $rep->delete($realtable, $cols, $row, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $retval = $self->delete($realtable, $cols, $row, $options);
     }
     else {
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
         $retval = $self->_delete($table,$params,$cols,$row,$options);
     }
     &App::sub_exit($retval) if ($App::trace);
@@ -1977,12 +1985,17 @@
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "update(): params undefined" if (!defined $params);
     my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
     my ($retval);
     if (defined $repname && $repname ne $self->{name}) {
         my $rep = $self->{context}->repository($repname);
-        $retval = $rep->update($table, $cols, $row, $options);
+        $retval = $rep->update($realtable, $cols, $row, $options);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $retval = $self->update($realtable, $cols, $row, $options);
     }
     else {
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
         $retval = $self->_update($table,$params,$cols,$row,$options);
     }
     &App::sub_exit($retval) if ($App::trace);
@@ -2299,16 +2312,16 @@
 
 =head2 get_table_def()
 
-    * Signature: $tabledef = $rep->get_table_def($tablename);
+    * Signature: $table_def = $rep->get_table_def($tablename);
     * Param:     $tablename         string
-    * Return:    $tabledef          {}
+    * Return:    $table_def          {}
     * Throws:    App::Exception::Repository
     * Since:     0.01
 
     Sample Usage: 
 
-    $tabledef = $rep->get_table_def($tablename);
-    print "$tabledef->{name} $tabledef->{label}\n";
+    $table_def = $rep->get_table_def($tablename);
+    print "$table_def->{name} $table_def->{label}\n";
 
 Gets a reference to a "table definition", which allows you to access all
 of the attributes of the requested table.
@@ -2319,9 +2332,24 @@
 =cut
 
 sub get_table_def {
+    &App::sub_entry if ($App::trace);
     my ($self, $table) = @_;
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
-    $self->{table}{$table};
+    my $repname = $self->{table}{$table}{repository};
+    my $realtable = $self->{table}{$table}{table} || $table;
+    my ($table_def);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $table_def = $rep->get_table_def($realtable);
+    }
+    elsif (defined $realtable && $realtable ne $table) {
+        $table_def = $self->get_table_def($realtable);
+    }
+    else {
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
+        $table_def = $self->{table}{$table};
+    }
+    &App::sub_exit($table_def) if ($App::trace);
+    return($table_def);
 }
 
 #############################################################################
@@ -2347,8 +2375,9 @@
 
 sub get_column_names {
     my ($self, $table) = @_;
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
-    $self->{table}{$table}{columns};
+    my $table_def = $self->get_table_def($table);
+    my $columns = $table_def->{columns};
+    return($columns);
 }
 
 #############################################################################
@@ -2378,14 +2407,14 @@
 
 sub get_column_labels {
     my ($self, $table, $labelcolumn) = @_;
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
+    my $table_def = $self->get_table_def($table);
     my ($labels);
     if (!$labelcolumn) {
-        $labels = $self->{table}{$table}{column_labels};
+        $labels = $table_def->{column_labels};
     }
     else {
         $labels = {};
-        my $column_defs = $self->{table}{$table}{column};
+        my $column_defs = $table_def->{column};
         foreach my $column (keys %$column_defs) {
             $labels->{$column} = $column_defs->{$column}{$labelcolumn};
         }
@@ -2399,17 +2428,17 @@
 
 =head2 get_column_def()
 
-    * Signature: $columndef = $rep->get_column_def($tablename,$columnname);
+    * Signature: $column_def = $rep->get_column_def($tablename,$columnname);
     * Param:     $tablename         string
     * Param:     $columnname        string
-    * Return:    $columndef         {}
+    * Return:    $column_def         {}
     * Throws:    App::Exception::Repository
     * Since:     0.01
 
     Sample Usage: 
 
-    $columndef = $rep->get_column_def($tablename,$columnname);
-    print "$columndef->{name} $columndef->{label} $columndef->{type}\n";
+    $column_def = $rep->get_column_def($tablename,$columnname);
+    print "$column_def->{name} $column_def->{label} $column_def->{type}\n";
 
 Gets a reference to a "column definition", which allows you to access all
 of the attributes of the requested column.
@@ -2423,8 +2452,9 @@
 # $column = $rep->get_column_def($tablename,$columnname); # print "%$column\n";
 sub get_column_def {
     my ($self, $table, $column) = @_;
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
-    $self->{table}{$table}{column}{$column};
+    my $table_def = $self->get_table_def($table);
+    my $column_def = $table_def->{column}{$column};
+    return($column_def);
 }
 
 #############################################################################
@@ -2479,21 +2509,24 @@
     my $self = shift;
     my ($table, $rows, $rowidx, $rowchange, $change, $colref, $prikeyidx, 
$nrows);
 
+    my ($table_def);
+
     $nrows = 0;
     foreach $table (@{$self->{tables}}) {
+        $table_def = $self->get_table_def($table);
 
-        $rowchange = $self->{table}{$table}{cache}{rowchange};
+        $rowchange = $table_def->{cache}{rowchange};
 
         if ($rowchange && $#$rowchange > -1) {
 
-            $prikeyidx = $self->{table}{$table}{prikeyidx};
+            $prikeyidx = $table_def->{prikeyidx};
             if (!$prikeyidx) {
                 $self->{context}->add_message("Table '$table' not configured 
for updating ('prikey' not set in commit())");
                 next;
             }
 
-            $rows      = $self->{table}{$table}{cache}{rows};
-            $colref    = $self->{table}{$table}{cache}{columns};
+            $rows      = $table_def->{cache}{rows};
+            $colref    = $table_def->{cache}{columns};
 
             for ($rowidx = 0; $rowidx <= $#$rows; $rowidx++) {
                 $change = $rowchange->[$rowidx];
@@ -2863,7 +2896,8 @@
 
     $summary_keys = [] if (!$summary_keys);
 
-    my $column_def = $self->{table}{$table}{column};
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
 
     my ($ext_summaries, $ext_column_summary, $ext_summary_columns, 
$ext_summary_functions);
     $ext_summaries = $options->{ext_summaries};
@@ -2899,7 +2933,7 @@
             if (!$columns) {
                  $columns = [ ];
                  foreach $column (sort keys %$row) {
-                     push(@$columns, $column) if (defined 
$column_def->{$column});
+                     push(@$columns, $column) if (defined 
$column_defs->{$column});
                  }
             }
             $hash_rows = $rows;
@@ -2933,11 +2967,11 @@
         for ($i = 0; $i <= $#$columns; $i++) {
             $column = $columns->[$i];
             $value = $row->[$i];
-            if ($column_def->{$column}{expr}) {
+            if ($column_defs->{$column}{expr}) {
                 push(@$expr_column_idx, $i);
                 $contains_expr = 1;
             }
-            elsif ($column_def->{$column}{is_key}) {
+            elsif ($column_defs->{$column}{is_key}) {
                 # do nothing
             }
             elsif (defined $value && $value =~ /^-?[0-9\.]+$/) {
@@ -3191,7 +3225,8 @@
     my ($self, $table, $params, $cols, $rows, $options) = @_;
     $options ||= {};
     my %options = %$options;
-    my $column_defs = $self->{table}{$table}{column};
+    my $table_def = $self->get_table_def($table);
+    my $column_defs = $table_def->{column};
     my (@expr_col_idx, @expr_col, $col, %colidx);
 
     for (my $i = 0; $i <= $#$cols; $i++) {
@@ -3546,16 +3581,18 @@
         $table_def->{columns} = $columns;
     }
 
+    my $column_defs = $table_def->{column};
+
     # for each column named in the configuration, give it a number up front
     for ($idx = 0; $idx <= $#$columns; $idx++) {
         $column = $columns->[$idx];
-        $table_def->{column}{$column}{idx} = $idx;
+        $column_defs->{$column}{idx} = $idx;
     }
 
     # for each column in the hash (random order), add them to the end
     my ($label);
-    foreach $column (keys %{$table_def->{column}}) {
-        $column_def = $table_def->{column}{$column};
+    foreach $column (keys %$column_defs) {
+        $column_def = $column_defs->{$column};
         $column_def->{name} = $column;
         if (! $column_def->{label}) {
             $label = $column;
@@ -3592,12 +3629,54 @@
     ######################################################################
     # primary key
     ######################################################################
-
     # if a non-reference scalar, assume it's a comma-separated list and split 
it
     if ($table_def->{primary_key} && ! ref($table_def->{primary_key})) {
         $table_def->{primary_key} = [ split(/ *, */, 
$table_def->{primary_key}) ];
     }
 
+    
####################################################################################
+    # Determine what columns would normally be considered keys
+    
####################################################################################
+    my (%is_key);
+    my $primary_key = $table_def->{primary_key} || $table_def->{prikey};
+    if ($primary_key) {
+        if (!ref($primary_key)) {
+            foreach $column (split(/,/,$primary_key)) {
+                $is_key{$column} = 1;
+            }
+        }
+        elsif (ref($primary_key) eq "ARRAY") {
+            foreach $column (@$primary_key) {
+                $is_key{$column} = 1;
+            }
+        }
+    }
+
+    my $alternate_keys = $table_def->{alternate_key};
+    if ($alternate_keys && ref($alternate_keys) eq "ARRAY") {
+        foreach my $alternate_key (@$alternate_keys) {
+            if (ref($alternate_key) eq "ARRAY") {
+                foreach $column (@$alternate_key) {
+                    $is_key{$column} = 1;
+                }
+            }
+        }
+    }
+
+    
####################################################################################
+    # Determine which columns *are* keys
+    
####################################################################################
+    foreach $column (keys %$column_defs) {
+        $column_def = $column_defs->{$column};
+        if (!defined $column_def->{is_key}) {
+            if ($is_key{$column}) {
+                $column_def->{is_key} = 1;
+            }
+            elsif ($column_def->{type} && $column_def->{type} eq "string") {
+                $column_def->{is_key} = 1;
+            }
+        }
+    }
     &App::sub_exit() if ($App::trace);
 }
 

Reply via email to