DBIx-Wrapper

 view release on metacpan or  search on metacpan

lib/DBIx/Wrapper.pm  view on Meta::CPAN

    }
    else {
        return $self->_quote_field_name($field);
    }
}

sub _quote_field_name {
    my $self = shift;
    my $field = shift;

    my $sep = $self->_get_catalog_separator;
    my $sf_sep = quotemeta($sep);
    my @parts = split(/$sf_sep/, $field);

    my $quote_char = $self->_get_identifier_quote_char;
    my $sf_quote_char = quotemeta($quote_char);

    foreach my $part (@parts) {
        $part =~ s/$sf_quote_char/$quote_char$quote_char/g;
        $part = $quote_char . $part . $quote_char;
    }

    return join($sep, @parts);
}

# E.g., turn test_db.test_table into `test_db`.`test_table`
sub _quote_table {
    my $self = shift;
    my $table = shift;

    my $sep = $self->_get_catalog_separator;

    my $parts;
    if (ref($table) eq 'ARRAY') {
        $parts = $table;
    }
    else {
        my $sf_sep = quotemeta($sep);
        $parts = [ split(/$sf_sep/, $table) ];
    }
    
    return join($sep, map { $self->_quote_field_name($_) } @$parts);
}

=pod

=head2 C<update($table, \%keys, \%data), update($table, \@keys, \%data)>

Update the table using the key/value pairs in C<%keys> to specify
the C<WHERE> clause of the query.  C<%data> contains the new values
for the row(s) in the database.  The keys parameter can
optionally be an array ref instead of a hashref.  E.g.,

     $db->update($table, [ key1 => $val1, key2 => $val2 ], \%data);

This is so that the order of the parameters in the C<WHERE> clause
are kept in the same order.  This is required to use the correct
multi field indexes in some databases.

=cut
sub update {
    my ($self, $table, $keys, $data) = @_;

    if (defined($keys)) {
        unless ((UNIVERSAL::isa($keys, 'HASH') or UNIVERSAL::isa($keys, 'ARRAY'))) {
            return $self->setErr(-1, 'DBIx::Wrapper: No keys passed to update()');
        }
        
    }

    unless ($data and UNIVERSAL::isa($data, 'HASH')) {
        return $self->setErr(-1, 'DBIx::Wrapper: No values passed to update()');
    }

    unless (%$data) {
        return "0E";
    }
        
    # my @fields;
    my @values;
    my @set;

    my $dbh = $self->_getDatabaseHandle;
    while (my ($field, $value) = each %$data) {
        # push @fields, $field;
        my $sf_field = $self->_quote_field_name($field);
        if (UNIVERSAL::isa($value, 'DBIx::Wrapper::SQLCommand')) {
            push @set, "$sf_field=" . $value->asString;
        } elsif (ref($value) eq 'SCALAR') {
            push @set, "$sf_field=" . $$value;
        } else {
            if ($self->_getNoPlaceholders) {
                if (defined($value)) {
                    push @set, "$sf_field=" . $dbh->quote($value);
                }
                else {
                    push @set, "$sf_field=NULL";
                }
            }
            else {
                push @set, "$sf_field=?";
                push @values, $value;
            }
        }
    }

    my @keys;
    if (ref($keys) eq 'ARRAY') {
        # allow this to maintain order in the WHERE clause in
        # order to use the right indexes
        my @copy = @$keys;
        while (my $key = shift @copy) {
            push @keys, $key;
            my $val = shift @copy; # shift off the value
        }
        $keys = { @$keys };
    }
    elsif (not defined($keys)) {
        # do nothing
    }
    else {



( run in 0.560 second using v1.01-cache-2.11-cpan-39bf76dae61 )