DBIx-Easy
view release on metacpan or search on metacpan
$column = shift; $value = shift;
push (@columns, $column);
push (@values, $value);
}
$flags = $self->typemap($table);
for (my $i = 0; $i <= $#values; $i++) {
if (ref ($values[$i]) eq 'SCALAR') {
$values[$i] = ${$values[$i]};
} elsif ($flags->{$columns[$i]} == DBI::SQL_INTEGER
|| $flags->{$columns[$i]} == DBI::SQL_SMALLINT
|| $flags->{$columns[$i]} == DBI::SQL_DECIMAL
|| $flags->{$columns[$i]} == DBI::SQL_FLOAT
|| $flags->{$columns[$i]} == DBI::SQL_REAL
|| $flags->{$columns[$i]} == DBI::SQL_DOUBLE
|| $flags->{$columns[$i]} == DBI::SQL_NUMERIC) {
# we don't need to quote numeric values, but
# we have to check for empty input
unless (defined $values[$i] && $values[$i] =~ /\S/) {
$values[$i] = 'NULL';
}
} elsif (defined $values[$i]) {
$values[$i] = $self -> quote ($values[$i]);
} else {
$values[$i] = 'NULL';
}
}
# now the statement
$statement = "INSERT INTO $table ("
. join (', ', @columns) . ") VALUES ("
. join (', ', @values) . ")";
# process it
$self -> {CONN} -> do ($statement)
|| $self -> fatal ("Couldn't execute statement \"$statement\"");
}
# ---------------------------------------------------------------
# METHOD: update TABLE CONDITIONS COLUMN VALUE [COLUMN VALUE] ...
#
# Updates the rows matching CONDITIONS with the given
# COLUMN/VALUE pairs and returns the number of the
# modified rows.
# ---------------------------------------------------------------
=over 4
=item update I<table> I<conditions> I<column> I<value> [I<column> I<value>] ...
$dbi_interface -> update ('components', "table='ram'", price => 100);
$dbi_interface -> update ('components', "table='ram'", price => \"price + 20");
Updates any row of I<table> which fulfill the I<conditions> by inserting the given I<column>/I<value> pairs. Scalar references can be used to embed strings without further quoting into the resulting SQL statement. Returns the number of rows modified.
=back
=cut
sub update
{
my $self = shift;
my $table = shift;
my $conditions = shift;
my (@columns);
my ($statement, $rv);
my ($column, $value);
# ensure that connection is established
return unless $self -> connect ();
while ($#_ >= 0) {
$column = shift; $value = shift;
# avoid Perl warning
if (defined $value) {
if (ref($value) eq 'SCALAR') {
$value = $$value;
} else {
$value = $self -> {CONN} -> quote ($value);
}
} else {
$value = 'NULL';
}
push (@columns, $column . ' = ' . $value);
}
# now the statement
$statement = "UPDATE $table SET "
. join (', ', @columns) . " WHERE $conditions";
# process it
$rv = $self -> {CONN} -> do ($statement);
if (defined $rv) {
# return the number of rows changed
$rv;
} else {
$self -> fatal ("Couldn't execute statement \"$statement\"");
}
}
# ---------------------------------------------------------------
# METHOD: put TABLE CONDITIONS COLUMN VALUE [COLUMN VALUE] ...
#
# Either updates the rows matching CONDITIONS with the given
# COLUMN/VALUE pairs or puts (inserts) them into TABLE.
# Returns the number of modified rows (1 in case of an insert).
# ---------------------------------------------------------------
=over 4
=item put I<table> I<conditions> I<column> I<value> [I<column> I<value>] ...
=back
=cut
sub put
{
my $self = shift;
my $table = shift;
( run in 1.473 second using v1.01-cache-2.11-cpan-7fcb06a456a )