Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Cvs P5ee > [svn:p5ee] r938...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 509 of 720
Post > Topic >>

[svn:p5ee] r9383 - p5ee/trunk/App-Repository/lib/App

by spadkins@[EMAIL PROTECTED] Apr 9, 2007 at 11:18 AM

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
@[EMAIL PROTECTED]
 -66,10 +66,10 @[EMAIL PROTECTED]
     $typedef      = $rep->get_type_def($typename);                 #
print "%$type\n";
     $tablenames   = $rep->get_table_names();                       #
print "@[EMAIL PROTECTED]
";
     $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 "@[EMAIL PROTECTED]
";
     $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
@[EMAIL PROTECTED]
 -558,7 +558,6 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $col, $value, $options) = @[EMAIL PROTECTED]
     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);
@[EMAIL PROTECTED]
 -603,9 +602,13 @[EMAIL PROTECTED]
 
     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});
@[EMAIL PROTECTED]
 -691,10 +694,14 @[EMAIL PROTECTED]
     my ($self, $table, $params, $cols, $row, $options) = @[EMAIL PROTECTED]
     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});
@[EMAIL PROTECTED]
 -711,14 +718,14 @[EMAIL PROTECTED]
             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;
             }
@[EMAIL PROTECTED]
 -894,21 +901,9 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table) = @[EMAIL PROTECTED]
     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);
@[EMAIL PROTECTED]
 -930,7 +925,8 @[EMAIL PROTECTED]
         $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.
@[EMAIL PROTECTED]
 -975,7 +971,8 @[EMAIL PROTECTED]
     my ($self, $table, $columns) = @[EMAIL PROTECTED]
     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}) {
@[EMAIL PROTECTED]
 -1164,18 +1161,13 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $options) = @[EMAIL PROTECTED]
     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);
@[EMAIL PROTECTED]
 -1187,7 +1179,7 @[EMAIL PROTECTED]
             $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) {
@[EMAIL PROTECTED]
 -1245,15 +1237,15 @[EMAIL PROTECTED]
 sub get_objects {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $options) = @[EMAIL PROTECTED]
    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 (@[EMAIL PROTECTED]
) {
         $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}) {
@[EMAIL PROTECTED]
 -1416,7 +1408,7 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $values, $options) = @[EMAIL PROTECTED]
     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);
 }
 
@[EMAIL PROTECTED]
 -1440,12 +1432,14 @[EMAIL PROTECTED]
     my ($self, $row, $table, $params, $cols, $options) = @[EMAIL PROTECTED]
 
     $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 = {};
@[EMAIL PROTECTED]
 -1460,7 +1454,6 @[EMAIL PROTECTED]
     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;
@[EMAIL PROTECTED]
 -1473,7 +1466,7 @[EMAIL PROTECTED]
             $repop = $2;
         }
 
-        if (!defined $tabledef->{column}{$column}) {
+        if (!defined $table_def->{column}{$column}) {
             if ($param =~ /^begin_(.*)/) {
                 $column = $1;
                 $repop = "ge";
@[EMAIL PROTECTED]
 -1483,7 +1476,7 @[EMAIL PROTECTED]
                 $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) {
@[EMAIL PROTECTED]
 -1497,7 +1490,7 @[EMAIL PROTECTED]
                 $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]*$/) {
@[EMAIL PROTECTED]
 -1567,10 +1560,10 @[EMAIL PROTECTED]
 
     my ($idx, $native_idx, $column, @[EMAIL PROTECTED]
);
     $#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;
     }
 
@[EMAIL PROTECTED]
 -1628,13 +1621,13 @[EMAIL PROTECTED]
     my ($self, $table, $params, $cols, $rows, $options) = @[EMAIL PROTECTED]
     $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, @[EMAIL PROTECTED]
 $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;
@[EMAIL PROTECTED]
 -1736,20 +1729,25 @[EMAIL PROTECTED]
 sub insert_row {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $row, $options) = @[EMAIL PROTECTED]
    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 (@[EMAIL PROTECTED]
>{columns}}) {
+            foreach my $col (@[EMAIL PROTECTED]
>{columns}}) {
                 if (exists $hash->{$col}) {
                     push(@[EMAIL PROTECTED]
 $col);
                 }
@[EMAIL PROTECTED]
 -1761,9 +1759,9 @[EMAIL PROTECTED]
                 $columns = $cols;
             }
             else {
-                my $tabledef = $self->get_table_def($table);
+                my $table_def = $self->{table}{$table};
                 $columns = [];
-                foreach my $col (@[EMAIL PROTECTED]
>{columns}}) {
+                foreach my $col (@[EMAIL PROTECTED]
>{columns}}) {
                     if (exists $hash->{$col}) {
                         push(@[EMAIL PROTECTED]
 $col);
                     }
@[EMAIL PROTECTED]
 -1809,7 +1807,7 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $row, $options) = @[EMAIL PROTECTED]
 
-    my $tabledef = $self->{table}{$table};
+    my $table_def = $self->get_table_def($table);
 
     my $ref = ref($cols);
     my ($object);
@[EMAIL PROTECTED]
 -1829,7 +1827,7 @[EMAIL PROTECTED]
         $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);
@[EMAIL PROTECTED]
 -1857,21 +1855,21 @[EMAIL PROTECTED]
 sub _check_default_and_required_fields {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $hash) = @[EMAIL PROTECTED]
    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) {
@[EMAIL PROTECTED]
 -1882,7 +1880,7 @[EMAIL PROTECTED]
         #    }
         #}
     }
-    my $alternate_keys = $tabledef->{alternate_key};
+    my $alternate_keys = $table_def->{alternate_key};
     if ($alternate_keys) {
         foreach my $alternate_key (@[EMAIL PROTECTED]
) {
             foreach my $column (@[EMAIL PROTECTED]
) {
@[EMAIL PROTECTED]
 -1905,18 +1903,23 @[EMAIL PROTECTED]
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $rows, $options) = @[EMAIL PROTECTED]
     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") {
@[EMAIL PROTECTED]
 -1926,8 +1929,8 @[EMAIL PROTECTED]
                 $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);
             }
         }
@[EMAIL PROTECTED]
 -1960,12 +1963,17 @[EMAIL PROTECTED]
     my ($self, $table, $params, $cols, $row, $options) = @[EMAIL PROTECTED]
     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);
@[EMAIL PROTECTED]
 -1977,12 +1985,17 @[EMAIL PROTECTED]
     my ($self, $table, $params, $cols, $row, $options) = @[EMAIL PROTECTED]
     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);
@[EMAIL PROTECTED]
 -2299,16 +2312,16 @[EMAIL PROTECTED]
 
 =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.
@[EMAIL PROTECTED]
 -2319,9 +2332,24 @[EMAIL PROTECTED]
 =cut
 
 sub get_table_def {
+    &App::sub_entry if ($App::trace);
     my ($self, $table) = @[EMAIL PROTECTED]
    $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);
 }
 

#############################################################################
@[EMAIL PROTECTED]
 -2347,8 +2375,9 @[EMAIL PROTECTED]
 
 sub get_column_names {
     my ($self, $table) = @[EMAIL PROTECTED]
    $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);
 }
 

#############################################################################
@[EMAIL PROTECTED]
 -2378,14 +2407,14 @[EMAIL PROTECTED]
 
 sub get_column_labels {
     my ($self, $table, $labelcolumn) = @[EMAIL PROTECTED]
    $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};
         }
@[EMAIL PROTECTED]
 -2399,17 +2428,17 @[EMAIL PROTECTED]
 
 =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.
@[EMAIL PROTECTED]
 -2423,8 +2452,9 @[EMAIL PROTECTED]
 # $column = $rep->get_column_def($tablename,$columnname); # print
"%$column\n";
 sub get_column_def {
     my ($self, $table, $column) = @[EMAIL PROTECTED]
    $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);
 }
 

#############################################################################
@[EMAIL PROTECTED]
 -2479,21 +2509,24 @[EMAIL PROTECTED]
     my $self = ****ft;
     my ($table, $rows, $rowidx, $rowchange, $change, $colref, $prikeyidx,
$nrows);
 
+    my ($table_def);
+
     $nrows = 0;
     foreach $table (@[EMAIL PROTECTED]
>{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];
@[EMAIL PROTECTED]
 -2863,7 +2896,8 @[EMAIL PROTECTED]
 
     $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};
@[EMAIL PROTECTED]
 -2899,7 +2933,7 @[EMAIL PROTECTED]
             if (!$columns) {
                  $columns = [ ];
                  foreach $column (sort keys %$row) {
-                     push(@[EMAIL PROTECTED]
 $column) if (defined
$column_def->{$column});
+                     push(@[EMAIL PROTECTED]
 $column) if (defined
$column_defs->{$column});
                  }
             }
             $hash_rows = $rows;
@[EMAIL PROTECTED]
 -2933,11 +2967,11 @[EMAIL PROTECTED]
         for ($i = 0; $i <= $#$columns; $i++) {
             $column = $columns->[$i];
             $value = $row->[$i];
-            if ($column_def->{$column}{expr}) {
+            if ($column_defs->{$column}{expr}) {
                 push(@[EMAIL PROTECTED]
 $i);
                 $contains_expr = 1;
             }
-            elsif ($column_def->{$column}{is_key}) {
+            elsif ($column_defs->{$column}{is_key}) {
                 # do nothing
             }
             elsif (defined $value && $value =~ /^-?[0-9\.]+$/) {
@[EMAIL PROTECTED]
 -3191,7 +3225,8 @[EMAIL PROTECTED]
     my ($self, $table, $params, $cols, $rows, $options) = @[EMAIL PROTECTED]
     $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 (@[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 $col, %colidx);
 
     for (my $i = 0; $i <= $#$cols; $i++) {
@[EMAIL PROTECTED]
 -3546,16 +3581,18 @[EMAIL PROTECTED]
         $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;
@[EMAIL PROTECTED]
 -3592,12 +3629,54 @[EMAIL PROTECTED]
    
######################################################################
     # 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 (@[EMAIL PROTECTED]
) {
+                $is_key{$column} = 1;
+            }
+        }
+    }
+
+    my $alternate_keys = $table_def->{alternate_key};
+    if ($alternate_keys && ref($alternate_keys) eq "ARRAY") {
+        foreach my $alternate_key (@[EMAIL PROTECTED]
) {
+            if (ref($alternate_key) eq "ARRAY") {
+                foreach $column (@[EMAIL PROTECTED]
) {
+                    $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);
 }
 




 1 Posts in Topic:
[svn:p5ee] r9383 - p5ee/trunk/App-Repository/lib/App
spadkins@[EMAIL PROTECTED  2007-04-09 11:18:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 20:09:17 CDT 2008.