I'm using DBI to talk to a MySQL server.

When preparing SQL statements to send to the server, DBI circumvents the problem of different SQL syntaxes across all the different SQL servers (Oracle, Sybase, MySQL etc) by simply taking the statement as a raw string and letting the SQL server tell you whether the syntax is valid or not.

The problem with this is that you don't find out until deep into runtime whether there is a problem with your SQL - even something simple such as a typo.

I appreciate that it would be really hard to write a layer over SQL that was general across all servers, but I'm really only interested in MySQL's specific syntax to get my job done.

With that as a given, I think it should be possible to write something (perhaps like a CGI.pm style interface to HTML) that will allow you to compile-time check MySQL specific SQL syntax, and perhaps even check if you are refering to real databases and tables.

I was thinking something like:

$sth->prepare( table{users}->select( all, where( like{name}->quote('%john%') ) ) )

would be equivilant to:

$sth->prepare( << 'EOS' ); select * from users where name like '%john%' EOS

Obviously for this simple example the extra layer is not worth the overhead for clarity's sake - but for a more complex real world situation the compile time syntax checking would be worth it.

Has anyone got any thoughts further to this?


In reply to SQL/DBI by tomazos

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.