I want to write a little wrapper function to the prepare() and execute() part of querying a database. For instance, here's a relatively simple version:
sub safe_query_execute { my ($db_handle, $query) = @_; my $query_handle = $db_handle->prepare($query) or confess "Error preparing query: $query\n" . "Error: $DBI::errstr\n"; eval { $query_handle->execute(); }; if ( $@ ) { confess "QUERY ERROR: $DBI::errstr\n" . "QUERY: $query\n"; } return $query_handle; }
Pretty basic, given a db handle and a query, prepare()'s and execute()'s it, returning the executed handle, doing error checking along the way.

Yes, i'm aware that i could accomplish this other ways, but I dont necessarily want to execute all queries this way in a script, some queries will want some of their own specialized error checking, like recovering from the error, etc. I wrote this because I write similar code often in my scripts, so i wrapped it.

Note, i used or confess on the prepare(), and eval{} on the execute() - opinions on that could be another thread.

Anyhow, i want to write a nice (similar) wrapper for queries with input bind params. The problem im running into is getting the query for error statements. I'd like the function to look something like:

sub bind_param_safe_query_execute { my ($query_handle, @params) = @_; eval { $query_handle->execute(@params); }; if ( $@ ) { confess "QUERY ERROR: $DBI::errstr\n" # somehow print the query } }
I dont want to have to pass in the query, sometimes they're created on the fly, and are out of scope when the handles are executed, but i cant find any way to get the query out of the handle itself. Is this possible?

Any other approaches to accomplish this same idea?

Note: someday i'll perhaps add stuff for giving SQL_TYPES to the bind params, perhaps with a Tie::IxHash instead of an array. Perhaps other options eventually too.


In reply to DBI - get info from statement handles by shemp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.