cvsuser 04/11/02 08:45:53
Modified: App-Repository/lib/App Repository.pm
App-Repository/t DBI-insert.t
Log:
added insert hash support (and tests)
Revision Changes Path
1.14 +75 -7 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.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- Repository.pm 19 Oct 2004 15:31:03 -0000 1.13
+++ Repository.pm 2 Nov 2004 16:45:52 -0000 1.14
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: Repository.pm,v 1.13 2004/10/19 15:31:03 spadkins Exp $
+## $Id: Repository.pm,v 1.14 2004/11/02 16:45:52 spadkins Exp $
#############################################################################
package App::Repository;
@@ -1340,8 +1340,38 @@
# $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);
+ my ($self, $table, $cols, $row, $options) = @_;
+ my ($retval, $hash, $columns);
+ if (ref($cols) eq "HASH") {
+ $hash = $cols; # a hashref was passed in instead of cols/row
+ my $tabledef = $self->get_table_def($table);
+ $columns = $tabledef->{columns};
+ $columns = [ keys %$hash ] if (!$columns);
+ }
+ elsif (ref($row) eq "HASH") {
+ $hash = $row;
+ if (ref($cols) eq "ARRAY") {
+ $columns = $cols;
+ }
+ else {
+ my $tabledef = $self->get_table_def($table);
+ $columns = $tabledef->{columns};
+ $columns = [ keys %$hash ] if (!$columns);
+ }
+ }
+ 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;
}
@@ -1349,8 +1379,8 @@
# NOTE: insert() is a synonym for insert_row()
sub insert {
&App::sub_entry if ($App::trace);
- my ($self, $table, $cols, $row) = @_;
- my $retval = $self->_insert_row($table, $cols, $row);
+ my ($self, $table, $cols, $row, $options) = @_;
+ my $retval = $self->insert_row($table, $cols, $row, $options);
&App::sub_exit($retval) if ($App::trace);
$retval;
}
@@ -1358,8 +1388,46 @@
# $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);
+ my ($self, $table, $cols, $rows, $options) = @_;
+ my ($retval, $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 $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);
+ }
+ }
+ foreach $hash (@$hashes) {
+ $row = [];
+ foreach $col (@cols) {
+ push(@$row, $hash->{$col});
+ }
+ push(@rows, $row);
+ }
+ $retval = $self->_insert_rows($table, [EMAIL PROTECTED], [EMAIL PROTECTED],
$options);
+ }
+ else {
+ $retval = $self->_insert_rows($table, $cols, $rows, $options);
+ }
&App::sub_exit($retval) if ($App::trace);
$retval;
}
1.3 +44 -0 p5ee/App-Repository/t/DBI-insert.t
Index: DBI-insert.t
===================================================================
RCS file: /cvs/public/p5ee/App-Repository/t/DBI-insert.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DBI-insert.t 2 Sep 2004 20:59:16 -0000 1.2
+++ DBI-insert.t 2 Nov 2004 16:45:53 -0000 1.3
@@ -85,6 +85,50 @@
ok($db->_insert_row("test_person",
["person_id","age","first_name","gender","state"],
[7,39,"keith", "M","GA"]),
"insert again");
+ ok($db->insert("test_person", {
+ person_id => 8,
+ age => 35,
+ first_name => "alex",
+ gender => "M",
+ state => "GA",
+ }),
+ "insert hash");
+ eval {
+ $db->insert_row("test_person", {
+ person_id => 8,
+ age => 35,
+ first_name => "alex",
+ gender => "M",
+ state => "GA",
+ });
+ };
+ ok($@, "insert dup hash fails");
+ ok($db->insert("test_person", undef, {
+ person_id => 9,
+ age => 35,
+ first_name => "alex",
+ gender => "M",
+ state => "GA",
+ }),
+ "insert hash in 2nd pos");
+ ok($db->insert("test_person", ["age","first_name","gender","state"], {
+ person_id => 9,
+ age => 35,
+ first_name => "alex",
+ gender => "M",
+ state => "GA",
+ }),
+ "insert hash in 2nd pos w/ col spec");
+ eval {
+ $db->insert_row("test_person", undef, {
+ person_id => 9,
+ age => 35,
+ first_name => "alex",
+ gender => "M",
+ state => "GA",
+ });
+ };
+ ok($@, "insert dup hash in 2nd pos fails");
}
exit 0;