cvsuser     03/08/05 10:50:47

  Modified:    App-Repository/lib/App/Repository MySQL2.pm
  Log:
  took out locking from store_rows()
  
  Revision  Changes    Path
  1.8       +85 -44    p5ee/App-Repository/lib/App/Repository/MySQL2.pm
  
  Index: MySQL2.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/Repository/MySQL2.pm,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -w -r1.7 -r1.8
  --- MySQL2.pm 1 Jul 2003 20:54:58 -0000       1.7
  +++ MySQL2.pm 5 Aug 2003 17:50:47 -0000       1.8
  @@ -1,12 +1,12 @@
   
   ######################################################################
  -## File: $Id: MySQL2.pm,v 1.7 2003/07/01 20:54:58 spadkins Exp $
  +## File: $Id: MySQL2.pm,v 1.8 2003/08/05 17:50:47 spadkins Exp $
   ######################################################################
   
   use App::Repository::MySQL;
   
   package App::Repository::MySQL2;
  -$VERSION = do { my @r=(q$Revision: 1.7 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  +$VERSION = do { my @r=(q$Revision: 1.8 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
   
   @ISA = ( "App::Repository::MySQL" );
   
  @@ -18,13 +18,12 @@
       my ($row, $ok, $nrows, $rownum, $colnum, $column, $tabledef);
       my ($pk_column, $pk_column_idx, @insert_cols, @insert_cols_ih);
       my ($id, %paramvalues, $oldrow, $oldrows, %oldrows, $different, $key);
  -    my ($table_ih, $auto_id, $current_datetime, $context, $error, $sql);
  +    my ($table_ih, $auto_id, $current_datetime, $context, $errmsg, $sql);
       my ($column_obsolete_dttm, $column_change_dttm, %colidx, %nodiff_column);
   
       $self->{error} = "";
       $context = $self->{context};
   
  -    $self->_lock_table($table);
       eval {
           $tabledef = $self->{table}{$table};
           $table_ih = $tabledef->{table_ih} || "";
  @@ -61,7 +60,7 @@
           if (($auto_id && $pk_column) ||
               ($column_change_dttm && $column_obsolete_dttm)) {
       
  -            if ($pk_column && ! defined $colidx{$pk_column}) {
  +            if ($pk_column && ! defined $pk_column_idx) {
                   push(@insert_cols, $pk_column);
                   push(@insert_cols_ih, $pk_column);
                   $colidx{$pk_column} = $#insert_cols;
  @@ -119,8 +118,8 @@
               $current_datetime = $self->current_datetime() if (%oldrows && 
!$current_datetime);
       
               if ($App::DEBUG && $context->dbg(1)) {
  -                $error = $self->error();
  -                $context->dbgprint("store_rows($table): replacing ", $#$tmprows + 
1, " $error");
  +                $errmsg = $self->error();
  +                $context->dbgprint("store_rows($table): replacing ", $#$tmprows + 
1, " $errmsg");
               }
           }
           else {
  @@ -182,83 +181,125 @@
                   # only if it is considered different do we try to physically store 
the row
                   if ($different) {
                       # substitute any defaults (todo)
  -                    if ($table_ih) {
  +                    $ok = $self->_update($table,$keycolidx,$cols,$row);
  +                    if ($ok && $table_ih) {
                           if ($column_obsolete_dttm && $column_change_dttm) {
                               $oldrow->[$colidx{$column_obsolete_dttm}] = 
$row->[$colidx{$column_change_dttm}];
                           }
  +                        $ok = 0;
  +                        $errmsg = "";
  +                        eval {
                           $ok = $self->_insert_row($table_ih,[EMAIL 
PROTECTED],$oldrow);
  -                        $ok = 1 if (!$ok && $self->error() =~ /duplicate/i);
  +                            $errmsg = $self->error();
  +                        };
  +                        $errmsg = $@ if ($@);
  +                        if (!$ok && $errmsg =~ /duplicate/i) {
  +                            $ok = 1;
  +                            print STDERR "store_rows() DUP: 
insert_row($table_ih,...)\n";
  +                            print STDERR "   [", join("|", @insert_cols_ih), "]\n";
  +                            print STDERR "   [", join("|", @$oldrow), "]\n";
  +                        }
  +                        elsif ($errmsg) {
  +                            print STDERR "store_rows() ERR: 
insert_row($table_ih,...) $errmsg\n";
  +                            print STDERR "   [", join("|", @insert_cols_ih), "]\n";
  +                            print STDERR "   [", join("|", @$oldrow), "]\n";
  +                        }
                       }
  -                    $ok = $self->_update($table,$keycolidx,$cols,$row) if ($ok);
                   }
       
                   if ($App::DEBUG && $context->dbg(1)) {
                       $context->dbgprint("store_rows() old=[", join(",",@$oldrow), 
"]");
                       $context->dbgprint("             new=[", join(",",@$row), "]");
  -                    $error = $self->error();
  -                    $sql = $error ? $self->{sql} : "";
  -                    $context->dbgprint("             diff=$different ok=$ok 
err=$error $sql");
  +                    $errmsg = $self->error();
  +                    $sql = $errmsg ? $self->{sql} : "";
  +                    $context->dbgprint("             diff=$different ok=$ok 
err=$errmsg $sql");
                   }
                   $nrows++ if ($ok);
               }
               else {
                   # next if (does not have permission to insert);  (todo)
       
  +                if (! $row->[$pk_column_idx]) {
                   # if we need to automatically allocate an ID ...
  -                if ($auto_id && ! $row->[$pk_column_idx]) {
  -                    $id = $self->_next_id($table, $id);
  +                    if ($auto_id) {
  +                        $id = $self->next_id($table, $id);
                       $row->[$pk_column_idx] = $id;
                   }
  +                    else {   # give it 0 and let the DB assign an ID
  +                        $row->[$pk_column_idx] = 0;
  +                    }
  +                }
  +
  +                $ok = 0;
  +                $errmsg = "";
  +                eval {
                   $ok = $self->_insert_row($table,[EMAIL PROTECTED],$row);
  -                $error = $self->error();
  +                    $errmsg = $self->error();
  +                };
  +                $errmsg = $@ if ($@);
  +                if (!$ok && $errmsg =~ /duplicate/i) {
  +                    $ok = 1;
  +                    print STDERR "store_rows() DUP: insert_row($table,...)\n";
  +                    print STDERR "   [", join("|", @insert_cols), "]\n";
  +                    print STDERR "   [", join("|", @$row), "]\n";
  +                }
  +                elsif ($errmsg) {
  +                    print STDERR "store_rows() ERR: insert_row($table,...) 
$errmsg\n";
  +                    print STDERR "   [", join("|", @insert_cols), "]\n";
  +                    print STDERR "   [", join("|", @$row), "]\n";
  +                }
       
                   if ($App::DEBUG && $context->dbg(1)) {
                       $context->dbgprint("store_rows() new=[", join(",",@$row), "]");
  -                    $sql = $error ? $self->{sql} : "";
  -                    $context->dbgprint("                   ok=$ok err=$error $sql");
  -                }
  -                if ($ok) {
  -                    $nrows++;
  -                }
  -                elsif ($error =~ /duplicate/i) {
  -                    $ok = 1;
  +                    $sql = $errmsg ? $self->{sql} : "";
  +                    $context->dbgprint("                   ok=$ok err=$errmsg 
$sql");
                   }
  +                $nrows++ if ($ok);
               }
  -    
  -            last if (!$ok);
           }
       
           if (defined $params && %oldrows) {
               foreach $key (keys %oldrows) {
                   $oldrow = $oldrows{$key};
  -                $ok = 1;
  -                if ($table_ih) {
  +                $ok = $self->_delete_row($table,$cols,$oldrow,$keycolidx);
  +                if ($ok && $table_ih) {
                       if ($column_obsolete_dttm) {
                           $current_datetime = $self->current_datetime() if 
(!$current_datetime);
                           $oldrow->[$colidx{$column_obsolete_dttm}] = 
$current_datetime;
                       }
  -                    $ok = $self->_insert_row($table_ih,[EMAIL PROTECTED],$oldrow);
  -                    $ok = 1 if (!$ok && $self->error() =~ /duplicate/i);
  +                    $ok = 0;
  +                    $errmsg = "";
  +                    eval {
  +                        $ok = $self->insert_row($table_ih,[EMAIL 
PROTECTED],$oldrow);
  +                        $errmsg = $self->error();
  +                    };
  +                    $errmsg = $@ if ($@);
  +                    if (!$ok && $errmsg =~ /duplicate/i) {
  +                        $ok = 1;
  +                        print STDERR "store_rows() DUP: 
insert_row($table_ih,...)\n";
  +                        print STDERR "   [", join("|", @insert_cols_ih), "]\n";
  +                        print STDERR "   [", join("|", @$oldrow), "]\n";
  +                    }
  +                    elsif ($errmsg) {
  +                        print STDERR "store_rows() ERR: insert_row($table_ih,...) 
$errmsg\n";
  +                        print STDERR "   [", join("|", @insert_cols_ih), "]\n";
  +                        print STDERR "   [", join("|", @$oldrow), "]\n";
  +                    }
                   }
  -                $ok = $self->_delete_row($table,$cols,$oldrow,$keycolidx) if ($ok);
       
                   if ($App::DEBUG && $context->dbg(1)) {
                       $context->dbgprint("store_rows() del=[", join(",",@$oldrow), 
"]");
  -                    $error = $self->error();
  -                    $sql = $error ? $self->{sql} : "";
  -                    $context->dbgprint("                   ok=$ok err=$error $sql");
  +                    $errmsg = $self->error();
  +                    $sql = $errmsg ? $self->{sql} : "";
  +                    $context->dbgprint("                   ok=$ok err=$errmsg 
$sql");
                   }
  -    
  -                last if (!$ok);
               }
           }
       };
       if ($@) {
  -        my $errmsg = $@;
  -        $self->_unlock_table($table);
  +        $errmsg = $@;
           die "Exception in store_rows(): $errmsg";
       }
  -    $self->_unlock_table($table);
       $self->{numrows} = $nrows;
       &App::sub_exit($nrows) if ($App::trace_subs);
       return($nrows);
  
  
  

Reply via email to