cvsuser     03/06/18 07:56:20

  Modified:    App-Repository/lib/App/Repository DBI.pm
  Log:
  added delete()
  
  Revision  Changes    Path
  1.8       +121 -3    p5ee/App-Repository/lib/App/Repository/DBI.pm
  
  Index: DBI.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/Repository/DBI.pm,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -w -r1.7 -r1.8
  --- DBI.pm    13 Jun 2003 17:28:59 -0000      1.7
  +++ DBI.pm    18 Jun 2003 14:56:20 -0000      1.8
  @@ -1,13 +1,13 @@
   
   ######################################################################
  -## File: $Id: DBI.pm,v 1.7 2003/06/13 17:28:59 spadkins Exp $
  +## File: $Id: DBI.pm,v 1.8 2003/06/18 14:56:20 spadkins Exp $
   ######################################################################
   
   use App;
   use App::Repository;
   
   package App::Repository::DBI;
  -$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" );
   
  @@ -970,7 +970,7 @@
                   elsif ($repop eq "matches") {
                       $value =~ s/\*/%/g;
                       $value =~ s/'/\\'/g;
  -                    $value = "'%$value%'";
  +                    $value = "'$value'";
                   }
                   elsif ($sqlop eq "in" || $sqlop eq "=") {
                       if ($quoted) {
  @@ -1885,6 +1885,105 @@
       $sql;
   }
   
  +# $delete_sql = $rep->_mk_delete_sql($table, \%params,                   \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, \%params,    undef,  undef, \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, [EMAIL PROTECTED], [EMAIL PROTECTED], 
[EMAIL PROTECTED], \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, [EMAIL PROTECTED], [EMAIL PROTECTED], 
[EMAIL PROTECTED], \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, $key,                       \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, $key,        undef,  undef, \%options);
  +# $delete_sql = $rep->_mk_delete_sql($table, undef,       [EMAIL PROTECTED], [EMAIL 
PROTECTED], \%options);
  +sub _mk_delete_sql {
  +    &App::sub_entry if ($App::trace_subs);
  +    my ($self, $table, $params, $cols, $row, $options) = @_;
  +
  +    $self->_load_table_metadata($table) if (!defined 
$self->{table}{$table}{loaded});
  +
  +    my $tabcols = $self->{table}{$table}{column};
  +    my $by_expression = $options->{by_expression};
  +
  +    my $where = "";
  +
  +    my ($colidx, $col, $value, $quoted);
  +    if (!defined $params) {
  +        $params = $self->{table}{$table}{primary_key};
  +        die "_mk_delete_sql() can't delete with undef params because 
{table}{$table}{primary_key} not defined"
  +            if (!defined $params);
  +        $params = [ $params ] if (!ref($params));
  +    }
  +
  +    if (!ref($params)) {  # delete by key!
  +        $value = $params;
  +        $col = $self->{table}{$table}{primary_key};
  +        die "_mk_delete_sql() can't delete with key because 
{table}{$table}{primary_key} not defined"
  +            if (!defined $col);
  +        if (!ref($col)) {
  +            $quoted = (defined $tabcols->{$col}{quoted}) ? 
($tabcols->{$col}{quoted}) : ($value !~ /^-?[0-9.]+$/);
  +            if ($quoted && !$by_expression) {
  +                $value =~ s/'/\\'/g;
  +                $value = "'$value'";
  +            }
  +            $where = "where $col = $value\n";
  +        }
  +        else {
  +            $params = $col;   # it wasn't a column, but an array of them
  +            my @where = ();
  +            my @values = $self->_key_to_values($value);
  +            for ($colidx = 0; $colidx <= $#$params; $colidx++) {
  +                $col = $params->[$colidx];
  +                $value = $values[$colidx];
  +                $quoted = (defined $tabcols->{$col}{quoted}) ? 
($tabcols->{$col}{quoted}) : ($value !~ /^-?[0-9.]+$/);
  +                if ($quoted && !$by_expression) {
  +                    $value =~ s/'/\\'/g;
  +                    $value = "'$value'";
  +                }
  +                push(@where, "$col = $value");
  +            }
  +            $where = "where " . join("\n  and ",@where) . "\n";
  +        }
  +    }
  +    elsif (ref($params) eq "HASH") {
  +        $where = $self->_mk_where_clause($table, $params);
  +    }
  +    elsif (ref($params) eq "ARRAY") {
  +        die "_mk_delete_sql() can't delete with no indexes/columns in params" if 
($#$params == -1);
  +        my @where = ();
  +        if ($params->[0] =~ /^[0-9]+$/) {  # an array of indexes
  +            my $keycolidx = $params;  # @$params represents a set of array indices
  +            for (my $i = 0; $i <= $#$keycolidx; $i++) {
  +                $colidx = $keycolidx->[$i];
  +                $col = $cols->[$colidx];
  +                if (!defined $row || $#$row == -1) {
  +                    $value = "?";
  +                }
  +                else {
  +                    $value = $row->[$colidx];
  +                    if (!defined $value) {
  +                        $value = "NULL";
  +                    }
  +                    else {
  +                        $quoted = (defined 
$tabcols->{$col}{quoted})?($tabcols->{$col}{quoted}):($value !~ /^-?[0-9.]+$/);
  +                        if ($quoted) {
  +                            $value =~ s/'/\\'/g;
  +                            $value = "'$value'";
  +                        }
  +                    }
  +                }
  +                push(@where, "$col = $value");
  +            }
  +        }
  +        else {   # an array of column names
  +        }
  +        $where = "where " . join("\n  and ",@where) . "\n" if ($#where > -1);
  +    }
  +    else {
  +        die "_mk_delete_sql() unrecognized params type";
  +    }
  +
  +    my $sql = "delete from $table\n$where";
  +    &App::sub_exit($sql) if ($App::trace_subs);
  +    $sql;
  +}
  +
   # $delete_sql = $rep->_mk_delete_row_sql ($table, [EMAIL PROTECTED], [EMAIL 
PROTECTED], [EMAIL PROTECTED]);
   sub _mk_delete_row_sql {
       &App::sub_entry if ($App::trace_subs);
  @@ -2069,6 +2168,25 @@
       $self->{numrows} = $nrows;
       &App::sub_exit($ok) if ($App::trace_subs);
       return($ok);
  +}
  +
  +sub delete {
  +    &App::sub_entry if ($App::trace_subs);
  +    my ($self, $table, $params, $cols, $row, $options) = @_;
  +    my $retval = $self->_delete($table,$params,$cols,$row,$options);
  +    &App::sub_exit($retval) if ($App::trace_subs);
  +    return($retval);
  +}
  +
  +sub _delete {
  +    &App::sub_entry if ($App::trace_subs);
  +    my ($self, $table, $params, $cols, $row, $options) = @_;
  +    $self->{error} = "";
  +    my $sql = $self->_mk_delete_sql($table, $params, $cols, $row, $options);
  +    $self->{sql} = $sql;
  +    my $retval = $self->{dbh}->do($sql);
  +    &App::sub_exit($retval) if ($App::trace_subs);
  +    return($retval);
   }
   
   # $nrows = $rep->_update($table, \%params,    [EMAIL PROTECTED], [EMAIL PROTECTED], 
\%options);
  
  
  

Reply via email to