in reply to Re: SQL Query
in thread SQL Query

Why type all that extra here-doc stuff?
$sql = ' CREATE TABLE colors ( color varchar(60) default NULL ) TYPE=MyISAM ';
And this looks even better if you don't need to store the SQL for later:
$dbh->do(' CREATE TABLE colors ( color varchar(60) default NULL ) TYPE=MyISAM ');
Here docs are nice, but overkill in this situation since the database could care less how it 'appears'. Just so long as it is valid syntax.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

Replies are listed 'Best First'.
Re: (jeffa) 2Re: SQL Query
by thor (Priest) on Jan 11, 2002 at 10:01 UTC
    I think that the here doc is a good solution in this case. If not, then I can think of absolutely no situation where it would be a good solution. You avoid quoting issues (both ' and " are valid quotes in sql, so why restict yourself to just one because you chose to open your string with one or the other?) and save yourself a headache when trying to debug.

    thor
      I didn't say that a here doc is a _bad_ solution, just too much of a solution. Because i use placeholders ( see bind values), i don't need inner quotes, but there is nothing stopping you from using placeholders inside a here document, either. It really is just a matter of style.

      A good use for here docs is for user display, and i don't mean debugging. But since i discovered the wonderful world of templating, i haven't used a here doc in a long, long time.

      I rather like this quick and dirty method of encapsulating a SQL query, i really think that here docs add line noise:

      sub get_artists { my $artist = shift() . '%'; return $dbh->selectcol_arrayref(' select name from artist where name like ? ',undef,$artist); }
      All i need is a pair of single quotes. But, this week taught me a pretty important lesson - there really are CPAN modules that take away having to even worry about such issues ... check out the replies by princepawn and autarch at Save MP3 Tag's to Database with SPOPS. (please note that SPOPS itself is meant for much more than what i presented.)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
Re: (jeffa) 3: SQL Query
by dmmiller2k (Chaplain) on Jan 11, 2002 at 08:05 UTC

    HERE docs neatly avoid any quoting issues. And XEmacs colors 'em different so they REALLY stand out :)

    dmm

      No they don't. BIND VARS neatly takes care of quoting issues:
      my $sth = $dbh->prepare(' insert into foo(bar,baz) values (?,?) '); $sth->execute($foo,$bar);

      Here docs would clutter this. Syntax highlighters should treat here docs and strings the same, my gvim highlighter does ...

      NOT! (update) Sorry, the gvim syntax highlighter tends to treat the characters inside a here document as perl code, while strings are one color except for interpolated variables and escaped characters. But anyhow...

      Save yourself the carpel tunnel. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      

      <update>

      Thanks gmax! I have 'posted' the instructions at my perlmonks site: http://jeffa.perlmonk.org/vi/highlight_embed.html.

      </update>

        Just a quick addition. Here documents have their pro and cons, and I take your point. However, one of the reasons for having a here doc is that you can set a different syntax highlighting. In Vim (or gvim) you can edit the perl.vim syntax file and impose a different set of colors for your SQL code. The only hairy thing is that you need to define a unique separator.
        In my case, I have defined two areas where I want this particular double syntax, inside a qq{} string (without spaces before the brace. If I want normal perl syntax, I use qq {}) and inside here documents starting with <<SQL. This way I can decide which one to use, whether or not I am using hash variables inside my SQL string. I use bind vars inside my SQL, and they don't interfere with my double syntax highlighting.

        The stylistic issue can be seen differently here, if we consider that we can have a separate syntax highlighting for embedded SQL (or HTML, XML, or whatever thing we are building from our Perl script).
        More than style, I would say it is convenience, since syntax highlighting will catch typing mistakes before I close my editor. Since I produce and maintain several Perl/DBI scripts, I found a balance between how much to write and how much help I want to get from my editor. Besides, if I catch the error earlier, it's one time less that I have to fire the editor and modify the code.
        OTOH, this benefit is lost when I send my code to somebody who doesn't benefit from this feature.
        I just wanted to share my experience. A mix of personal taste and style.
        _ _ _ _ (_|| | |(_|>< _|

        Ah! No wonder. Say no more. I just checked your Perl Geek Code.

        Ee-- indeed :)

        dmm