cvsuser 04/11/22 11:57:03
Modified: App-Repository/lib/App Repository.pm
App-Repository/t DBI-getset.t
Log:
support set()/set_rows() on a hash
Revision Changes Path
1.15 +66 -5 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.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- Repository.pm 2 Nov 2004 16:45:52 -0000 1.14
+++ Repository.pm 22 Nov 2004 19:57:03 -0000 1.15
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: Repository.pm,v 1.14 2004/11/02 16:45:52 spadkins Exp $
+## $Id: Repository.pm,v 1.15 2004/11/22 19:57:03 spadkins Exp $
#############################################################################
package App::Repository;
@@ -538,7 +538,7 @@
my ($self, $table, $params, $col, $value, $options) = @_;
$self->_load_table_metadata($table) if (! defined
$self->{table}{$table}{loaded});
my ($nrows);
- if (ref($col) eq "") {
+ if ($col && ref($col) eq "") {
$nrows = $self->set_row($table, $params, [$col], [$value], $options);
}
else {
@@ -609,10 +609,13 @@
* Signature: $nrows = $rep->set_row($table, $key, $cols, $row,
$options);
* Signature: $nrows = $rep->set_row($table, $params, $cols, $row,
$options);
+ * Signature: $nrows = $rep->set_row($table, $hash, undef,
undef,$options);
+ * Signature: $nrows = $rep->set_row($table, $params, $hash,
undef,$options);
* Param: $table string
* Param: $cols ARRAY
* Param: $row ARRAY
* Param: $key string
+ * Param: $hash HASH
* Param: $params undef,HASH
* Param: $options undef,HASH
* Return: $nrows integer
@@ -633,7 +636,62 @@
&App::sub_entry if ($App::trace);
my ($self, $table, $params, $cols, $row, $options) = @_;
$self->_load_table_metadata($table) if (! defined
$self->{table}{$table}{loaded});
- my $nrows = $self->_set_row($table, $params, $cols, $row, $options);
+
+ my ($nrows, $key_defined);
+ if ($row) {
+ $nrows = $self->_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);
+ }
+ 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});
+ }
+ }
+
+ $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 ($key_defined) {
+ $nrows = $self->_set_row($table, $params, [EMAIL PROTECTED],
[EMAIL PROTECTED], $options);
+ }
+ else {
+ $nrows = 0;
+ }
+ }
+
&App::sub_exit($nrows) if ($App::trace);
return($nrows);
}
@@ -1280,8 +1338,11 @@
&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 ($params &&
ref($params) ne "HASH");
my $nrows = $self->_update($table, $params, $cols, $row, $options);
+ if ($nrows == 0) {
+ $nrows = $self->_insert_row($table, $cols, $row, $options);
+ }
&App::sub_exit($nrows) if ($App::trace);
return($nrows);
@@ -1450,7 +1511,7 @@
sub _insert_row {
&App::sub_entry if ($App::trace);
- my ($self, $table, $params, $cols, $row, $options) = @_;
+ my ($self, $table, $cols, $row, $options) = @_;
$self->{error} = "";
my $retval = 0;
die "_insert_row(): not yet implemented";
1.4 +17 -3 p5ee/App-Repository/t/DBI-getset.t
Index: DBI-getset.t
===================================================================
RCS file: /cvs/public/p5ee/App-Repository/t/DBI-getset.t,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DBI-getset.t 2 Sep 2004 20:59:16 -0000 1.3
+++ DBI-getset.t 22 Nov 2004 19:57:03 -0000 1.4
@@ -29,6 +29,7 @@
},
},
},
+ #debug_sql => 1,
);
my $rep = $context->repository();
@@ -116,27 +117,40 @@
is($person_id, 4, "get_row() 3 values w/ %crit (checking 3 of 3)");
my ($hashes, $hash);
+ok($rep->set("test_person", {person_id => 1, age => 40}),
"set(table,%hash)");
$hash = $rep->get_hash("test_person");
is($hash->{person_id}, 1, "get_hash() person_id");
-is($hash->{age}, 39, "get_hash() age");
+is($hash->{age}, 40, "get_hash() age");
is($hash->{first_name}, "steve", "get_hash() first_name");
is($hash->{gender}, "M", "get_hash() gender");
is($hash->{state}, "GA", "get_hash() state");
+ok($rep->set("test_person", 1, {person_id => 1, age => 41}),
"set(table,\$key,\%hash)");
$hash = $rep->get_hash("test_person", 1);
is($hash->{person_id}, 1, "get_hash(1) person_id");
-is($hash->{age}, 39, "get_hash(1) age");
+is($hash->{age}, 41, "get_hash(1) age");
is($hash->{first_name}, "steve", "get_hash(1) first_name");
is($hash->{gender}, "M", "get_hash(1) gender");
is($hash->{state}, "GA", "get_hash(1) state");
+ok($rep->set("test_person", {first_name => "steve"}, {person_id => 1, age =>
41}), "set(table,\$params,\%hash)");
+ok($rep->set("test_person", {person_id => 8, age => 37, first_name =>
"nick", gender => "M", state => "NY"}),
+ "set(table,\$params,\%hash) : insert");
+is($rep->set("test_person", {gender => "F", age => 41}), 0,
+ "set(table,\$params,\%hash) : fails if key not supplied");
$hashes = $rep->get_hashes("test_person");
$hash = $hashes->[0];
is($hash->{person_id}, 1, "get_hashes()->[0] person_id");
-is($hash->{age}, 39, "get_hashes()->[0] age");
+is($hash->{age}, 41, "get_hashes()->[0] age");
is($hash->{first_name}, "steve", "get_hashes()->[0] first_name");
is($hash->{gender}, "M", "get_hashes()->[0] gender");
is($hash->{state}, "GA", "get_hashes()->[0] state");
+$hash = $hashes->[$#$hashes];
+is($hash->{person_id}, 8, "get_hashes()->[n] person_id");
+is($hash->{age}, 37, "get_hashes()->[n] age");
+is($hash->{first_name}, "nick", "get_hashes()->[n] first_name");
+is($hash->{gender}, "M", "get_hashes()->[n] gender");
+is($hash->{state}, "NY", "get_hashes()->[n] state");
exit(0);
#####################################################################