sub setup_bindings {
# Produces strings useful for generating dynamic
# SQL statements as well as an array of matching
# binding values (because you use bind values, right?)
#Args:
# $_[0]: Ref to hash of values, keyed by column
# $_[1]: Ref to array of columns used
#Usage:
# my ( $column_string, $bindings_string, $bound_values )
# = setup_bindings( \%value_for_column, \@columns );
# my $statement = " INSERT into YOUR_TABLE ( $column_string )
# VALUES ( $bindings_string )";
# $dbhandle->do( $statement, undef, @$bound_values ); #Or whatever use you have- this is `do` from the DBI
my $values = shift;
my @columns = @{ shift };
my @set_columns = ();
my @set_bindings = ();
my @bound_values = ();
for my $col_2_bind ( @columns ) {
push @set_columns, $col_2_bind;
push @set_bindings, '?';
push @bound_values, $values->{$col_2_bind};
};
my $column_string = join ', ', @set_columns;
my $bindings_string = join ', ', @set_bindings;
return ($column_string, $bindings_string, \@bound_values);
}
use SQL::Abstract;
my $sa = SQL::Abstract->new;
my($sql, @bind) = $sa->insert('YOUR_TABLE', \%value_for_column);
$dbhandle->do($sql, {}, @bind);
"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden
"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden
Actually, it can be made to work by adding a + to the beginning: @{shift} treats "shift" as a string, and looks for a variable named @shift (which would—obligatory plug—be caught by use strict 'vars'), but @{+shift} forces it to use the keyword shift, which is what you were expecting it to do.
"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden
perlmonks.org content © perlmonks.org and blogical, ChemBoy, davidrw
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03