Jifty-DBI
view release on metacpan or search on metacpan
lib/Jifty/DBI/Handle.pm view on Meta::CPAN
=cut
sub delete {
my ( $self, $table, @pairs ) = @_;
my @bind = ();
my $where = 'WHERE ';
while ( my $key = shift @pairs ) {
$where .= $key . "=?" . " AND ";
push( @bind, shift(@pairs) );
}
$where =~ s/AND $//;
my $query_string = "DELETE FROM " . $table . ' ' . $where;
$self->simple_query( $query_string, @bind );
}
=head2 insert $table_NAME @KEY_VALUE_PAIRS
Takes a table name and a set of key-value pairs in an array. splits the key value pairs, constructs an INSERT statement and performs the insert. Returns the row_id of this row.
=cut
sub insert {
my ( $self, $table, @pairs ) = @_;
my ( @cols, @vals, @bind );
#my %seen; #only the *first* value is used - allows drivers to specify default
while ( my $key = shift @pairs ) {
my $value = shift @pairs;
# next if $seen{$key}++;
push @cols, $key;
push @vals, '?';
push @bind, $value;
}
my $query_string
= "INSERT INTO $table ("
. CORE::join( ", ", @cols )
. ") VALUES " . "("
. CORE::join( ", ", @vals ) . ")";
my $sth = $self->simple_query( $query_string, @bind );
return ($sth);
}
=head2 update_record_value
Takes a hash with columns: C<table>, C<column>, C<value>, C<primary_keys>, and
C<is_sql_function>. The first two should be obvious; C<value> is where you
set the new value you want the column to have. The C<primary_keys> column should
be the lvalue of Jifty::DBI::Record::PrimaryKeys(). Finally ,
C<is_sql_function> is set when the Value is a SQL function. For example, you
might have C<< value => 'PASSWORD(string)' >>, by setting C<is_sql_function> to true,
that string will be inserted into the query directly rather then as a binding.
=cut
sub update_record_value {
my $self = shift;
my %args = (
table => undef,
column => undef,
is_sql_function => undef,
primary_keys => undef,
@_
);
return 1 unless grep {defined} values %{ $args{primary_keys} };
my @bind = ();
my $query = 'UPDATE ' . $args{'table'} . ' ';
$query .= 'SET ' . $args{'column'} . '=';
## Look and see if the column is being updated via a SQL function.
if ( $args{'is_sql_function'} ) {
$query .= $args{'value'} . ' ';
} else {
$query .= '? ';
push( @bind, $args{'value'} );
}
## Constructs the where clause.
my $where = 'WHERE ';
foreach my $key ( keys %{ $args{'primary_keys'} } ) {
$where .= $key . "=?" . " AND ";
push( @bind, $args{'primary_keys'}{$key} );
}
$where =~ s/AND\s$//;
my $query_str = $query . $where;
return ( $self->simple_query( $query_str, @bind ) );
}
=head2 update_table_value table COLUMN NEW_value RECORD_ID IS_SQL
Update column COLUMN of table table where the record id = RECORD_ID.
If IS_SQL is set, don't quote the NEW_VALUE.
=cut
sub update_table_value {
my $self = shift;
## This is just a wrapper to update_record_value().
my %args = ();
$args{'table'} = shift;
$args{'column'} = shift;
$args{'value'} = shift;
$args{'primary_keys'} = shift;
$args{'is_sql_function'} = shift;
return $self->update_record_value(%args);
}
=head2 simple_query QUERY_STRING, [ BIND_VALUE, ... ]
Execute the SQL string specified in QUERY_STRING
=cut
our $retry_simple_query = 1;
sub simple_query {
my $self = shift;
my $query_string = shift;
my @bind_values;
@bind_values = (@_) if (@_);
my $sth = $self->dbh->prepare($query_string);
unless ($sth) {
my $message = "$self couldn't prepare the query '$query_string': "
. $self->dbh->errstr;
if ($DEBUG) {
die "$message\n";
} else {
warn "$message\n";
my $ret = Class::ReturnValue->new();
$ret->as_error(
errno => '-1',
message => $message,
do_backtrace => undef
);
return ( $ret->return_value );
}
}
# Check @bind_values for HASH refs
for ( my $bind_idx = 0; $bind_idx < scalar @bind_values; $bind_idx++ ) {
if ( ref( $bind_values[$bind_idx] ) eq "HASH" ) {
my $bhash = $bind_values[$bind_idx];
$bind_values[$bind_idx] = $bhash->{'value'};
delete $bhash->{'value'};
$sth->bind_param( $bind_idx + 1, undef, $bhash );
}
# Some databases, such as Oracle fail to cope if it's a perl utf8
# string. they desperately want bytes.
Encode::_utf8_off( $bind_values[$bind_idx] );
}
my $basetime;
if ( $self->log_sql_statements ) {
( run in 1.364 second using v1.01-cache-2.11-cpan-4ac696b4eb4 )