in reply to abstraction -- level up!
sub do_select { my $self = shift; my %params = @_; $params{params} ||= []; + my $results; eval { my $sth = $self->dbh->prepare_cached( $params{sql}, ); + $sth->execute( @{$params{params}}, ); + $results = $sth->fetchall_arrayref( {} ); }; if ($@) { warn $@, "\n$params{sql}\n@{$params{params}}\n"; return; } + if (UNIVERSAL::isa($params{inflater}, 'CODE')) { my @results = $params{inflater}->($results); + return ( wantarray ? @results : $results[0] ); } else { return $results; } }
The function takes three parameters, two of which are optional. The sql parameter is the SQL statement you want executed. params is an optional array reference of the parameters to that statement. (You are using placeholders, right?) If you pass an inflater parameter, it will call that function, passing in the returned value from the fetchrow_arrayref({}) call.
Assumptions:
$dbh = DBI->connect( $connect_string, $user, $password, { PrintError = +> 0, RaiseError => 1 } );
Now, the inflater is passed an arrayref of hashes. Do with it what you will. if you don't pass in an inflater, that AoH is passed back to the caller of do_select().
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
|
|---|