Author: spadkins
Date: Thu Feb 26 14:36:32 2009
New Revision: 12549
Modified:
p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm
p5ee/trunk/App-Repository/lib/App/Repository/Oracle.pm
p5ee/trunk/App-Repository/t/DBI-insert-ora.t
Log:
implement insert/update on insert_rows
Modified: p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm Thu Feb 26 14:36:32 2009
@@ -2062,7 +2062,6 @@
}
};
if (my $e = $@) { # Log the error message with the SQL and rethrow
the exception
-#print STDERR "###### EXCEPTION: $e\n";
if ($self->is_duplicate_key_error($e)) {
if ($options->{update} || $options->{replace}) {
my ($key_idx);
@@ -2070,7 +2069,6 @@
if (ref($update_keys) eq "HASH") {
my @key_columns = (keys %$update_keys);
$key_idx = $self->_key_idx($table, $cols);
-#print STDERR "###### EXCEPTION: UPDATING col...@$cols] key...@key_columns] so
id...@$key_idx]\n";
}
else {
$key_idx = $self->_key_idx($table, $cols);
Modified: p5ee/trunk/App-Repository/lib/App/Repository/Oracle.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/Oracle.pm (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/Oracle.pm Thu Feb 26
14:36:32 2009
@@ -366,8 +366,10 @@
sub _insert_rows {
&App::sub_entry if ($App::trace);
my ($self, $table, $cols, $rows, $options) = @_;
+ $options = {} if (!$options);
$self->{error} = "";
my ($sql, $retval, $nrows_this_insert);
+ my $insert_method = $options->{insert_method} || $self->{insert_method} ||
"";
my $dbh = $self->{dbh};
return 0 if (!defined $dbh);
@@ -383,32 +385,50 @@
}
my $rows_ref = ref($rows);
if ($rows_ref eq "ARRAY") {
- my $maxrows = $options->{maxrows} || 100;
- my $rownum = 0;
- my (@current_rows, $rownum2);
- while ($rownum <= $#$rows) {
- $rownum2 = $rownum + $maxrows - 1;
- $rownum2 = $#$rows if ($rownum2 > $#$rows);
- @current_rows = @{$rows}[($rownum .. $rownum2)];
- $nrows_this_insert = $#current_rows + 1;
+ if ($insert_method eq "single") {
+ foreach my $row (@$rows_ref) {
+ $ok = $self->_insert_row($table, $cols, $rows, $options);
+ $nrows++ if ($ok);
+ }
+ }
+ else {
+ my $maxrows = $options->{maxrows} || 100;
+ my $rownum = 0;
+ my (@current_rows, $rownum2);
+ while ($rownum <= $#$rows) {
+ $rownum2 = $rownum + $maxrows - 1;
+ $rownum2 = $#$rows if ($rownum2 > $#$rows);
+ @current_rows = @{$rows}[($rownum .. $rownum2)];
+ $nrows_this_insert = $#current_rows + 1;
+
+ $sql = $self->_mk_insert_rows_sql($table, $cols,
\...@current_rows, $options);
+ if ($debug_sql) {
+ print $App::DEBUG_FILE "DEBUG_SQL: _insert_rows()\n";
+ print $App::DEBUG_FILE $sql;
+ }
+ ### TODO: make this work with regex for retry
+ eval {
+ $retval = $dbh->do($sql);
+ };
+ if ($debug_sql) {
+ print $App::DEBUG_FILE "DEBUG_SQL: retval [$retval]
$DBI::errstr\n";
+ print $App::DEBUG_FILE "\n";
+ }
+ if ($@) {
+ if ($options->{update} || $options->{replace}) {
+ foreach my $row (@current_rows) {
+ $ok = $self->_insert_row($table, $cols, $row,
$options);
+ $nrows++ if ($ok);
+ }
+ }
+ }
- $sql = $self->_mk_insert_rows_sql($table, $cols,
\...@current_rows, $options);
- if ($debug_sql) {
- print $App::DEBUG_FILE "DEBUG_SQL: _insert_rows()\n";
- print $App::DEBUG_FILE $sql;
+ $nrows += $retval;
+ $rownum += $maxrows;
}
- ### TODO: make this work with regex for retry
- $retval = $dbh->do($sql);
- if ($debug_sql) {
- print $App::DEBUG_FILE "DEBUG_SQL: retval [$retval]
$DBI::errstr\n";
- print $App::DEBUG_FILE "\n";
+ if ($nrows != $#$rows + 1) {
+ $ok = 0;
}
-
- $nrows += $retval;
- $rownum += $maxrows;
- }
- if ($nrows != $#$rows + 1) {
- $ok = 0;
}
$self->{numrows} = $nrows;
}
Modified: p5ee/trunk/App-Repository/t/DBI-insert-ora.t
==============================================================================
--- p5ee/trunk/App-Repository/t/DBI-insert-ora.t (original)
+++ p5ee/trunk/App-Repository/t/DBI-insert-ora.t Thu Feb 26 14:36:32 2009
@@ -251,8 +251,10 @@
is($nrows, 7, "insert_rows(): 7 rows, bulk insert");
#$nrows = $db->insert_rows("test_person",
["person_id","age","first_name","gender","state"], $dup_rows, { replace => 1 });
#is($nrows, 7, "insert_rows(): 7 rows, bulk replace");
- #$nrows = $db->insert_rows("test_person", ["person_id",
"age","first_name","gender","state"], $dup_rows, { update => 1 });
- #is($nrows, 7, "insert_rows(): 7 rows, bulk insert/update");
+
+ $nrows = $db->insert_rows("test_person", ["person_id",
"age","first_name","gender","state"], $dup_rows, { update => 1 });
+ is($nrows, 7, "insert_rows(): 7 rows, bulk insert/update");
+
#$nrows = $db->insert_rows("test_person",
["person_id","age","first_name","gender","state"], $dup_rows, { replace => 1,
maxrows => 4 });
#is($nrows, 7, "insert_rows(): 7 rows, bulk replace (4 at a time)");
#$nrows = $db->insert_rows("test_person", ["person_id",
"age","first_name","gender","state"], $dup_rows, { update => 1, maxrows => 4 });