Usually my code starts out as

my $results = $dbh->selectall_arrayref(<<'SQL',{ Slice => {} }, $place +holder1, ... ); SELECT foo , bar FROM mytable SQL

Then, while debugging, it morphs into

my $sql = <<'SQL'; SELECT foo , bar FROM mytable SQL $app->log($sql); my $results = $dbh->selectall_arrayref($sql,{ Slice => {} }, $placehol +der1, ... );

... and then it stays that way, because reverting the structure is only good until I need to re-debug things:

my $sql = <<'SQL'; SELECT foo , bar FROM mytable SQL # $app->log($sql); my $results = $dbh->selectall_arrayref($sql,{ Slice => {} }, $placehol +der1, ... );

For the placeholders I agree. Sometimes I keep the placeholders in an array to mention them in the same place as the SQL. In other situations, I use named placeholders like SQLite supports nowadays, and bind those in a loop:

while (my ($name,$value) = each %$parameter_names) { (my $perl_name) = ($name =~ m!(\w+)!); $perl_name = '$' . $perl_name; if( ! exists $parameters->{$perl_name}) { croak "Missing bind parameter '$perl_name'"; }; my $type = SQL_VARCHAR; # This is a horrible API, but so is using uplevel'ed variables if( my $r = ref $parameters->{$perl_name}) { if( $r eq 'SCALAR' ) { $type = SQL_INTEGER; # Clear out old variable binding: my $v = $parameters->{$perl_name}; delete $parameters->{$perl_name}; $parameters->{$perl_name} = $$v; } elsif( $r eq 'ARRAY' ) { $type = SQL_INTEGER; # Clear out old variable binding: my $v = $parameters->{$perl_name}; delete $parameters->{$perl_name}; $parameters->{$perl_name} = $v->[0]; $type = $v->[1]; } } $sth->bind_param($name => $parameters->{$perl_name}, $type) };

See Filesys::DB for a fancy but ultimately pointless idea of binding placeholder names to lexical variables in the Perl code. This was nice to use but in the end, the surrounding program does not pass SQL around but has function/method calls that do the right thing and the lexical binding does not provide anything helpful at the outside surface.


In reply to Re^3: DBI and JSON fields by Corion
in thread DBI and JSON fields by Bod

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.