Author: spadkins
Date: Mon Mar 12 12:56:04 2007
New Revision: 9230

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

Log:
enhance repository redirection for more methods than just get_rows() et.al.

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 Mar 12 12:56:04 2007
@@ -690,64 +690,72 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "set_row(): params undefined" if (!defined $params);
-    $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
-
-    my ($nrows, $key_defined);
-    if ($row) {
-        my $ref = ref($row);
-        if ($ref && $ref ne "ARRAY") {
-            $row = [ @[EMAIL PROTECTED] ];
-        }
-        $nrows = $self->_set_row($table, $params, $cols, $row, $options);
+    my $repname = $self->{table}{$table}{repository};
+    my ($nrows);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $nrows = $rep->set_row($table, $params, $cols, $row, $options);
     }
     else {
-        my ($hash, $columns);
-        if ($cols) {
-            $hash = $cols;
-            my $tabledef = $self->get_table_def($table);
-            $columns = $tabledef->{columns};
-            $columns = [ keys %$hash ] if (!$columns);
+        $self->_load_table_metadata($table) if (! defined 
$self->{table}{$table}{loaded});
+
+        my ($key_defined);
+        if ($row) {
+            my $ref = ref($row);
+            if ($ref && $ref ne "ARRAY") {
+                $row = [ @[EMAIL PROTECTED] ];
+            }
+            $nrows = $self->_set_row($table, $params, $cols, $row, $options);
         }
         else {
-            $hash = $params;     # a hashref was passed in instead of cols/row
-            my $tabledef = $self->get_table_def($table);
-            $columns = $tabledef->{columns};
-            $columns = [ keys %$hash ] if (!$columns);
-            $params = undef;
-        }
+            my ($hash, $columns);
+            if ($cols) {
+                $hash = $cols;
+                my $tabledef = $self->get_table_def($table);
+                $columns = $tabledef->{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};
+                $columns = [ keys %$hash ] if (!$columns);
+                $params = undef;
+            }
 
-        my (@cols, @row);
-        foreach my $col (@$columns) {
-            if (exists $hash->{$col}) {
-                push(@cols, $col);
-                push(@row, $hash->{$col});
+            my (@cols, @row);
+            foreach my $col (@$columns) {
+                if (exists $hash->{$col}) {
+                    push(@cols, $col);
+                    push(@row, $hash->{$col});
+                }
             }
-        }
 
-        $key_defined = 1;
+            $key_defined = 1;
 
-        if (!defined $params) {
-            my $primary_key = $self->{table}{$table}{primary_key};
-            $primary_key = [$primary_key] if (ref($primary_key) eq "");
-            $params = {};
-            my ($col);
-            for (my $keypos = 0; $keypos <= $#$primary_key; $keypos++) {
-                $col = $primary_key->[$keypos];
-                if (defined $hash->{$col}) {
-                    $params->{$col} = $hash->{$col};
-                }
-                else {
-                    $key_defined = 0;
-                    last;
+            if (!defined $params) {
+                my $primary_key = $self->{table}{$table}{primary_key};
+                $primary_key = [$primary_key] if (ref($primary_key) eq "");
+                $params = {};
+                my ($col);
+                for (my $keypos = 0; $keypos <= $#$primary_key; $keypos++) {
+                    $col = $primary_key->[$keypos];
+                    if (defined $hash->{$col}) {
+                        $params->{$col} = $hash->{$col};
+                    }
+                    else {
+                        $key_defined = 0;
+                        last;
+                    }
                 }
             }
-        }
 
-        if ($key_defined) {
-            $nrows = $self->_set_row($table, $params, [EMAIL PROTECTED], 
[EMAIL PROTECTED], $options);
-        }
-        else {
-            $nrows = 0;
+            if ($key_defined) {
+                $nrows = $self->_set_row($table, $params, [EMAIL PROTECTED], 
[EMAIL PROTECTED], $options);
+            }
+            else {
+                $nrows = 0;
+            }
         }
     }
 
@@ -885,9 +893,22 @@
 sub _get_default_columns {
     &App::sub_entry if ($App::trace);
     my ($self, $table) = @_;
+    my ($columns);
     my $table_def = $self->{table}{$table};
-    my $columns = $table_def->{default_columns} || $table_def->{columns};
+    $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);
@@ -1715,24 +1736,17 @@
 sub insert_row {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $row, $options) = @_;
-    my ($retval, $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);
-        $columns = [];
-        foreach my $col (@{$tabledef->{columns}}) {
-            if (exists $hash->{$col}) {
-                push(@$columns, $col);
-            }
-        }
+    my $repname = $self->{table}{$table}{repository};
+    my ($retval);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $retval = $rep->insert_row($table, $cols, $row, $options);
     }
-    elsif (ref($row) eq "HASH") {
-        $hash = $row;
-        if (ref($cols) eq "ARRAY") {
-            $columns = $cols;
-        }
-        else {
+    else {
+        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);
             $columns = [];
             foreach my $col (@{$tabledef->{columns}}) {
@@ -1741,19 +1755,34 @@
                 }
             }
         }
-    }
-    if ($hash) {
-        my (@cols, @row);
-        foreach my $col (@$columns) {
-            if (exists $hash->{$col}) {
-                push(@cols, $col);
-                push(@row, $hash->{$col});
+        elsif (ref($row) eq "HASH") {
+            $hash = $row;
+            if (ref($cols) eq "ARRAY") {
+                $columns = $cols;
+            }
+            else {
+                my $tabledef = $self->get_table_def($table);
+                $columns = [];
+                foreach my $col (@{$tabledef->{columns}}) {
+                    if (exists $hash->{$col}) {
+                        push(@$columns, $col);
+                    }
+                }
             }
         }
-        $retval = $self->_insert_row($table, [EMAIL PROTECTED], [EMAIL 
PROTECTED], $options);
-    }
-    else {
-        $retval = $self->_insert_row($table, $cols, $row, $options);
+        if ($hash) {
+            my (@cols, @row);
+            foreach my $col (@$columns) {
+                if (exists $hash->{$col}) {
+                    push(@cols, $col);
+                    push(@row, $hash->{$col});
+                }
+            }
+            $retval = $self->_insert_row($table, [EMAIL PROTECTED], [EMAIL 
PROTECTED], $options);
+        }
+        else {
+            $retval = $self->_insert_row($table, $cols, $row, $options);
+        }
     }
     &App::sub_exit($retval) if ($App::trace);
     $retval;
@@ -1875,44 +1904,52 @@
 sub insert_rows {
     &App::sub_entry if ($App::trace);
     my ($self, $table, $cols, $rows, $options) = @_;
-    my ($nrows, $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};
-        $columns = [ keys %$hash ] if (!$columns);
-    }
-    elsif (ref($rows) eq "ARRAY" && ref($rows->[0]) eq "HASH") {
-        $hashes = $rows;
-        $hash = $hashes->[0];
-        if (ref($cols) eq "ARRAY") {
-            $columns = $cols;
-        }
-        else {
+    my $repname = $self->{table}{$table}{repository};
+    my ($nrows);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $nrows = $rep->insert_rows($table, $cols, $rows, $options);
+    }
+    else {
+        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};
             $columns = [ keys %$hash ] if (!$columns);
         }
-    }
-    if ($hashes) {
-        my (@cols, @rows, $col, $row);
-        foreach $col (@$columns) {
-            if (exists $hash->{$col}) {
-                push(@cols, $col);
+        elsif (ref($rows) eq "ARRAY" && ref($rows->[0]) eq "HASH") {
+            $hashes = $rows;
+            $hash = $hashes->[0];
+            if (ref($cols) eq "ARRAY") {
+                $columns = $cols;
+            }
+            else {
+                my $tabledef = $self->get_table_def($table);
+                $columns = $tabledef->{columns};
+                $columns = [ keys %$hash ] if (!$columns || $#$columns == -1);
             }
         }
-        foreach $hash (@$hashes) {
-            $row = [];
-            foreach $col (@cols) {
-                push(@$row, $hash->{$col});
+        if ($hashes) {
+            my (@cols, @rows, $col, $row);
+            foreach $col (@$columns) {
+                if (exists $hash->{$col}) {
+                    push(@cols, $col);
+                }
             }
-            push(@rows, $row);
+            foreach $hash (@$hashes) {
+                $row = [];
+                foreach $col (@cols) {
+                    push(@$row, $hash->{$col});
+                }
+                push(@rows, $row);
+            }
+            $nrows = $self->_insert_rows($table, [EMAIL PROTECTED], [EMAIL 
PROTECTED], $options);
+        }
+        else {
+            $nrows = $self->_insert_rows($table, $cols, $rows, $options);
         }
-        $nrows = $self->_insert_rows($table, [EMAIL PROTECTED], [EMAIL 
PROTECTED], $options);
-    }
-    else {
-        $nrows = $self->_insert_rows($table, $cols, $rows, $options);
     }
     &App::sub_exit($nrows) if ($App::trace);
     $nrows;
@@ -1922,7 +1959,15 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "delete(): params undefined" if (!defined $params);
-    my $retval = $self->_delete($table,$params,$cols,$row,$options);
+    my $repname = $self->{table}{$table}{repository};
+    my ($retval);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $retval = $rep->delete($table, $cols, $row, $options);
+    }
+    else {
+        $retval = $self->_delete($table,$params,$cols,$row,$options);
+    }
     &App::sub_exit($retval) if ($App::trace);
     return($retval);
 }
@@ -1931,7 +1976,15 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $row, $options) = @_;
     die "update(): params undefined" if (!defined $params);
-    my $retval = $self->_update($table,$params,$cols,$row,$options);
+    my $repname = $self->{table}{$table}{repository};
+    my ($retval);
+    if (defined $repname && $repname ne $self->{name}) {
+        my $rep = $self->{context}->repository($repname);
+        $retval = $rep->update($table, $cols, $row, $options);
+    }
+    else {
+        $retval = $self->_update($table,$params,$cols,$row,$options);
+    }
     &App::sub_exit($retval) if ($App::trace);
     return($retval);
 }
@@ -3471,6 +3524,17 @@
     my ($table_def, $columns, $column, $column_def, $idx, $native_column);
 
     $table_def = $self->{table}{$table};
+    if (!$table_def) {
+        my $options = $self->{options};
+        my $prefix = $options->{prefix};
+        my $conf_type = $options->{conf_type} || "pl";
+        my $table_file = 
"$prefix/etc/app/Repository/$self->{name}/$table.$conf_type";
+        if (-r $table_file) {
+            $table_def = App::Conf::File->create({ conf_file => $table_file });
+            $self->{table}{$table} = $table_def;
+        }
+    }
+
     return if (!defined $table_def);
 
     # load up all additional information from the native metadata

Reply via email to