cvsuser 04/09/02 14:00:18
Modified: App-Repository/lib/App Repository.pm
Log:
added support for RepositoryObject
Revision Changes Path
1.11 +210 -78 p5ee/App-Repository/lib/App/Repository.pm
Index: Repository.pm
===================================================================
RCS file: /cvs/public/p5ee/App-Repository/lib/App/Repository.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -w -r1.10 -r1.11
--- Repository.pm 12 Mar 2004 03:10:39 -0000 1.10
+++ Repository.pm 2 Sep 2004 21:00:17 -0000 1.11
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: Repository.pm,v 1.10 2004/03/12 03:10:39 spadkins Exp $
+## $Id: Repository.pm,v 1.11 2004/09/02 21:00:17 spadkins Exp $
#############################################################################
package App::Repository;
@@ -12,6 +12,7 @@
use strict;
use Date::Format;
+use App::RepositoryObject;
=head1 NAME
@@ -577,15 +578,13 @@
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $options) = @_;
- my ($row, $repname, $rep);
- if (defined $self->{table}{$table}{repository}) {
- $repname = $self->{table}{$table}{repository};
- if ($repname ne $self->{name}) {
- $rep = $self->{context}->repository($repname);
+ my ($row);
+ my $repname = $self->{table}{$table}{repository};
+ if (defined $repname && $repname ne $self->{name}) {
+ my $rep = $self->{context}->repository($repname);
$row = $rep->get_row($table, $params, $cols, $options);
}
- }
- if (!$row) {
+ else {
$self->_load_table_metadata($table) if (! defined
$self->{table}{$table}{loaded});
if (!defined $cols) {
$cols = $self->{table}{$table}{columns};
@@ -705,15 +704,13 @@
sub get_rows {
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $options) = @_;
- my ($rows, $repname, $rep);
- if (defined $self->{table}{$table}{repository}) {
- $repname = $self->{table}{$table}{repository};
- if ($repname ne $self->{name}) {
- $rep = $self->{context}->repository($repname);
+ my ($rows);
+ my $repname = $self->{table}{$table}{repository};
+ if (defined $repname && $repname ne $self->{name}) {
+ my $rep = $self->{context}->repository($repname);
$rows = $rep->get_rows($table, $params, $cols, $options);
}
- }
- if (!$rows) {
+ else {
$self->_load_table_metadata($table) if (! defined
$self->{table}{$table}{loaded});
if (!defined $cols) {
$cols = $self->{table}{$table}{columns};
@@ -862,6 +859,145 @@
}
#############################################################################
+# get_object()
+#############################################################################
+
+=head2 get_object()
+
+ * Signature: $object = $rep->get_object ($table, $key, $cols, $options);
+ * Signature: $object = $rep->get_object ($table, $params, $cols, $options);
+ * Param: $table string
+ * Param: $cols ARRAY,undef
+ * Param: $key string
+ * Param: $params undef,HASH
+ * Param: $options undef,HASH
+ * Return: $object App::RepositoryObject
+ * Throws: App::Exception::Repository
+ * Since: 0.50
+
+ Sample Usage:
+
+ $object = $rep->get_object ($table, $key, [EMAIL PROTECTED], \%options);
+ $object = $rep->get_object ($table, \%params, [EMAIL PROTECTED], \%options);
+ $object = $rep->get_object ($table, $key, undef, \%options);
+ $object = $rep->get_object ($table, \%params, undef, \%options);
+
+tbd.
+
+=cut
+
+sub get_object {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $params, $cols, $options) = @_;
+ my $tabledef = $self->{table}{$table};
+ my $class = $tabledef->{class} || "App::RepositoryObject";
+ App->use($class);
+ my ($object);
+ if (ref($cols) eq "ARRAY" && $#$cols == -1 && !ref($params)) {
+ $object = {};
+ }
+ else {
+ $object = $self->get_hash($table, $params, $cols, $options);
+ }
+ $object->{_repository} = $self;
+ $object->{_table} = $table;
+ bless $object, $class;
+ if (!ref($params)) {
+ $object->{_key} = $params;
+ }
+ else {
+ my $primary_key = $tabledef->{primary_key};
+ $primary_key = [$primary_key] if (ref($primary_key) eq "");
+ my ($key);
+ if ($primary_key) {
+ $key = undef;
+ foreach my $column (@$primary_key) {
+ if (defined $object->{$column}) {
+ if (defined $key) {
+ $key .= "," . $object->{$column};
+ }
+ else {
+ $key = $object->{$column};
+ }
+ }
+ else {
+ $key = undef;
+ last;
+ }
+ }
+ $object->{_key} = $key if (defined $key);
+ }
+ }
+ &App::sub_exit($object) if ($App::trace);
+ return($object);
+}
+
+#############################################################################
+# get_objects()
+#############################################################################
+
+=head2 get_objects()
+
+ * Signature: $objects = $rep->get_objects ($table, $key, $cols, $options);
+ * Signature: $objects = $rep->get_objects ($table, $params, $cols, $options);
+ * Param: $table string
+ * Param: $cols ARRAY,undef
+ * Param: $key string
+ * Param: $params undef,HASH
+ * Param: $options undef,HASH
+ * Return: $objects ARRAY
+ * Throws: App::Exception::Repository
+ * Since: 0.50
+
+ Sample Usage:
+
+ $objects = $rep->get_objects ($table, $key, [EMAIL PROTECTED], \%options);
+ $objects = $rep->get_objects ($table, \%params, [EMAIL PROTECTED], \%options);
+ $objects = $rep->get_objects ($table, $key, undef, \%options);
+ $objects = $rep->get_objects ($table, \%params, undef, \%options);
+
+tbd.
+
+=cut
+
+sub get_objects {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $params, $cols, $options) = @_;
+ my $tabledef = $self->{table}{$table};
+ my $class = $tabledef->{class} || "App::RepositoryObject";
+ App->use($class);
+ my $primary_key = $tabledef->{primary_key};
+ $primary_key = [$primary_key] if (ref($primary_key) eq "");
+ my ($key);
+ my $objects = $self->get_hashes($table, $params, $cols, $options);
+ foreach my $object (@$objects) {
+ $object->{_repository} = $self;
+ $object->{_table} = $table;
+ bless $object, $class;
+ if ($primary_key) {
+ $key = undef;
+ foreach my $column (@$primary_key) {
+ if (defined $object->{$column}) {
+ if (defined $key) {
+ $key .= "," . $object->{$column};
+ }
+ else {
+ $key = $object->{$column};
+ }
+ }
+ else {
+ $key = undef;
+ last;
+ }
+ }
+ $object->{_key} = $key if (defined $key);
+ }
+ }
+ &App::sub_exit($objects) if ($App::trace);
+ return($objects);
+}
+
+#############################################################################
# set_hash()
#############################################################################
@@ -1143,8 +1279,8 @@
sub _set_row {
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $row, $options) = @_;
- $params = $self->_params_to_hashref($table, $params) if (ref($params) ne
"HASH");
+ $params = $self->_params_to_hashref($table, $params) if (ref($params) ne
"HASH");
my $nrows = $self->_update($table, $params, $cols, $row, $options);
&App::sub_exit($nrows) if ($App::trace);
@@ -1201,6 +1337,24 @@
return(\%params);
}
+# $ok = $rep->insert_row ($table, [EMAIL PROTECTED], [EMAIL PROTECTED]);
+sub insert_row {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $cols, $row) = @_;
+ my $retval = $self->_insert_row($table, $cols, $row);
+ &App::sub_exit($retval) if ($App::trace);
+ $retval;
+}
+
+# $ok = $rep->insert_rows ($table, [EMAIL PROTECTED], [EMAIL PROTECTED]);
+sub insert_rows {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $cols, $rows) = @_;
+ my $retval = $self->_insert_rows($table, $cols, $rows);
+ &App::sub_exit($retval) if ($App::trace);
+ $retval;
+}
+
sub delete {
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $row, $options) = @_;
@@ -1209,6 +1363,34 @@
return($retval);
}
+sub update {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $params, $cols, $row, $options) = @_;
+ my $retval = $self->_update($table,$params,$cols,$row,$options);
+ &App::sub_exit($retval) if ($App::trace);
+ return($retval);
+}
+
+sub _insert_row {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $params, $cols, $row, $options) = @_;
+ $self->{error} = "";
+ my $retval = 0;
+ die "_insert_row(): not yet implemented";
+ &App::sub_exit($retval) if ($App::trace);
+ return($retval);
+}
+
+sub _insert_rows {
+ &App::sub_entry if ($App::trace);
+ my ($self, $table, $params, $cols, $row, $options) = @_;
+ $self->{error} = "";
+ my $retval = 0;
+ die "_insert_rows(): not yet implemented";
+ &App::sub_exit($retval) if ($App::trace);
+ return($retval);
+}
+
sub _delete {
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $row, $options) = @_;
@@ -1713,66 +1895,6 @@
# METHODS
#############################################################################
-=head1 Methods: Repository Objects
-
-=cut
-
-#############################################################################
-# object()
-#############################################################################
-
-=head2 object()
-
- * Signature: $obj = $rep->object($table, $key);
- * Signature: $obj = $rep->object($table);
- * Param: $table $string
- * Param: $key $string
- * Return: $obj App::RepositoryObject
- * Throws: App::Exception::Repository
- * Since: 0.01
-
- Sample Usage:
-
- $obj = $rep->object($table, $key);
- $obj = $rep->object($table);
-
-A RepositoryObject is very lightweight to instantiate.
-It merely contains the "table" and "key" attributes
-and references to its Repository and its Context.
-This allows it to get its data from the Repository
-or put its data in the Repository whenever requested.
-
-The second form (without a $key) creates an object
-which cannot get() or set() attributes. This "anonymous"
-object is more like a "class" because "class methods"
-may be called such as finding out the relationships
-between classes of objects.
-
-=cut
-
-sub object {
- &App::sub_entry if ($App::trace);
- my ($self, $table, $key) = @_;
- my $class = $self->{table}{$table}{object_class} || "App::RepositoryObject";
- if (! $self->{used}{$class}) {
- App->use($class);
- $self->{used}{$class} = 1;
- }
- my $object = {
- repository => $self,
- context => $self->{context},
- table => $table,
- key => $key,
- };
- bless $object, $class;
- &App::sub_exit($object) if ($App::trace);
- return $object;
-}
-
-#############################################################################
-# METHODS
-#############################################################################
-
=head1 Methods: Locking (Concurrency Management)
=cut
@@ -2267,6 +2389,16 @@
$table_def->{column_labels}{$column} = $column_def->{label};
}
+
+ ######################################################################
+ # 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}) ];
+ }
+
&App::sub_entry if ($App::trace);
}