cvsuser 02/08/02 12:33:17
Modified: P5EEx/Blue/P5EEx/Blue Repository.pm
Log:
added select_values() and some debugging
Revision Changes Path
1.21 +96 -7 p5ee/P5EEx/Blue/P5EEx/Blue/Repository.pm
Index: Repository.pm
===================================================================
RCS file: /cvs/public/p5ee/P5EEx/Blue/P5EEx/Blue/Repository.pm,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -w -r1.20 -r1.21
--- Repository.pm 1 Aug 2002 18:17:35 -0000 1.20
+++ Repository.pm 2 Aug 2002 19:33:17 -0000 1.21
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: Repository.pm,v 1.20 2002/08/01 18:17:35 spadkins Exp $
+## $Id: Repository.pm,v 1.21 2002/08/02 19:33:17 spadkins Exp $
#############################################################################
package P5EEx::Blue::Repository;
@@ -75,6 +75,7 @@
# MEDIUM-LEVEL
$row = $rep->select_row ($table, \@cols, \@params, \%paramvalues);
$rows = $rep->select_rows($table, \@cols, \@params, \%paramvalues,
\@ordercols, $startrow, $endrow);
+ @values = $rep->select_values($table, \@cols, \@params, \%paramvalues);
$ok = $rep->insert_row ($table, \@cols, \@row);
$ok = $rep->insert_rows($table, \@cols, \@rows);
$ok = $rep->update_row ($table, \@cols, \@row, \@keycolidx);
@@ -948,6 +949,53 @@
}
#############################################################################
+# select_values()
+#############################################################################
+
+=head2 select_values()
+
+ * Signature: @values = $rep->select_values ($table, $cols);
+ * Signature: @values = $rep->select_values ($table, $cols, undef, $paramvalues);
+ * Signature: @values = $rep->select_values ($table, $cols, $params,
$paramvalues);
+ * Param: $table string
+ * Param: $cols []
+ * Param: $params []
+ * Param: $paramvalues {}
+ * Return: @values ()
+ * Throws: P5EEx::Blue::Exception::Repository
+ * Since: 0.01
+
+ Sample Usage:
+
+ @values = $rep->select_values (
+ "customer",
+ [ "id", "first_name", "last_name"],
+ undef,
+ { "last_name.contains" => "Smith" }
+ );
+ print join(",", @values), "\n";
+
+select_values() retrieves a single row of data from a table in the repository,
+with the specified columns of data, and restricted by the parameter values
+specified. If more than one row in the table satisfy the criteria
+specified by the parameter values, the first row is returned.
+
+If called in an array context, all values are returned.
+If called in a scalar context, only the first value is returned.
+
+=cut
+
+# @values = $rep->select_values ($table, \@cols, \@params, \%paramvalues);
+sub select_values {
+ my ($self, $table, $cols, $params, $paramvalues) = @_;
+ my $row = $self->select_row($table, $cols, $params, $paramvalues);
+ return undef if (!$row);
+ return undef if ($#$row == -1);
+ return @$row if (wantarray);
+ return $row->[0];
+}
+
+#############################################################################
# insert_row()
#############################################################################
@@ -1339,10 +1387,10 @@
$cacherowidx = $self->{table}{$table}{cache}{rowidx};
# if cache is empty, preload it with rows as hinted
- if ($#$cacherows == -1) {
- $cacheparamvalues = $self->{table}{$table}{cache}{paramvalues};
- $self->get_rows($table, undef, $cacheparamvalues);
- }
+ #if ($#$cacherows == -1) {
+ # $cacheparamvalues = $self->{table}{$table}{cache}{paramvalues};
+ # $self->get_rows($table, undef, $cacheparamvalues);
+ #}
$rowidx = $cacherowidx->{$key}; # look in cache
if (defined $rowidx) { # if found...
@@ -1361,6 +1409,14 @@
$row = $self->select_row($table, $cachecolumns, undef, \%paramvalues);
+ if ($P5EEx::Blue::DEBUG && $self->{context}->dbg(7)) {
+ my $context = $self->{context};
+ $context->dbgprint("rep->get_row(): [$self->{sql}]");
+ if ($row) {
+ $context->dbgprint(" [", join("|",@$row), "]");
+ }
+ }
+
$key = join(",", @{$row}[@$keycolidxref]);
push(@$cacherows, $row);
$cacherowidx->{$key} = $#$cacherows;
@@ -1419,10 +1475,19 @@
#print "rep->get_rows(", ($cachecolumns ? join(",", @$cachecolumns) : "undef"),
")\n";
$rows = $self->select_rows($table, $cachecolumns, undef, $paramvalueshashref);
+ if ($P5EEx::Blue::DEBUG && $self->{context}->dbg(7)) {
+ my $context = $self->{context};
+ $context->dbgprint("rep->get_rows(): [$self->{sql}]");
+ if ($rows) {
+ foreach (@$rows) {
+ $context->dbgprint(" [", join("|",@{$_}), "]");
+ }
+ }
+ }
+
$cacherows = $self->{table}{$table}{cache}{rows};
$cacherowidx = $self->{table}{$table}{cache}{rowidx};
-
if (defined $keycolidxref) { # can't cache the rows by key unless we know the
key
foreach $row (@$rows) {
$key = join(",", @{$row}[@$keycolidxref]);
@@ -2127,8 +2192,9 @@
sub commit {
my $self = shift;
- my ($table, $rows, $rowidx, $rowchange, $change, $colref, $prikeyidx);
+ my ($table, $rows, $rowidx, $rowchange, $change, $colref, $prikeyidx, $nrows);
+ $nrows = 0;
foreach $table (@{$self->{tables}}) {
$rowchange = $self->{table}{$table}{cache}{rowchange};
@@ -2150,15 +2216,24 @@
if ($change eq "U") {
$self->update_row($table, $colref, $rows->[$rowidx],
$prikeyidx);
$rowchange->[$rowidx] = "";
+ $nrows++;
}
elsif ($change eq "I") {
$self->insert_row($table, $colref, $rows->[$rowidx]);
$rowchange->[$rowidx] = "";
+ $nrows++;
}
+ if ($P5EEx::Blue::DEBUG && $self->{context}->dbg(7)) {
+ my $context = $self->{context};
+ $context->dbgprint("rep->commit(): [$self->{sql}]");
+ $context->dbgprint(" [", join("|",@{$rows->[$rowidx]}), "]");
}
}
}
}
+ $self->{context}->dbgprint("rep->commit(): nrows=$nrows")
+ if ($P5EEx::Blue::DEBUG && $self->{context}->dbg(2));
+}
#############################################################################
# rollback()
@@ -2197,6 +2272,7 @@
=head2 object()
* Signature: $obj = $rep->object($table, $key);
+ * Signature: $obj = $rep->object($table);
* Param: $table $string
* Param: $key $string
* Return: $obj P5EEx::Blue::RepositoryObject
@@ -2206,6 +2282,19 @@
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