RE: Translate between DBI and SQL
P.S. This may also help: http://www.dispersiondesign.com/articles/perl/perl_escape_characters Short answer: Use single-quoted strings whenever possible - they have the fewest characters to escape - only backslash and single quote - and you can escape both by preceding with a backslash. Use dot operator and double-quoted strings for special characters and new-lines. Use the dot and dot-equals operators for multi-line sql. something like this: my($my_sql_string); $my_sql_string = 'SELECT \'column name with a space\' ' . "\n"; $my_sql_string .= 'FROM table_b' ; Ref the docs for anything really fancy (like non-english / non-printable-ascii Unicode or unusual character encodings). Brian Fennell The information contained in this electronic mail transmission is intended only for the use of the individual or entity named in this transmission. If you are not the intended recipient of this transmission, you are hereby notified that any disclosure, copying or distribution of the contents of this transmission is strictly prohibited and that you should delete the contents of this transmission from your system immediately. Any comments or statements contained in this transmission do not necessarily reflect the views or position of Radial or its subsidiaries and/or affiliates.
Re: Translate between DBI and SQL
On Friday 08 February 2019 22:37:17 Mike Martin wrote: > Has anyone done any work on converting SQL queries between RDBMS and perl? > > My particular interest is DBD::Pg but anything would be of use > > It would be very useful when I am testing complex SQL, it's very easy to > miss a \ or quote between the two > > Thanks > Mike Hi! Maybe following module could be interesting: https://metacpan.org/pod/DBIx::Perlish
RE: Translate between DBI and SQL
P.P.S. My last answer used single-quote for a column with a space in it in PostgreSQL - this is wrong, it should have been a double-quote for the name of the column with a space and a single quote for a quoted string literal in PostgreSQL. Better example: (Adapted from answer to question here https://dba.stackexchange.com/questions/118059/quoting-columns-with-spaces-in-postgresql ) $my_perl_sql_string= 'SELECT' . "\n"; $my_perl_sql_string.= ' tab."This IS My Column EXACTLY" AS col' . "\n"; $my_perl_sql_string.= 'FROM "My TabLE Name Contains Spaces Too!" tab' . "\n"; $my_perl_sql_string.= 'WHERE tab."ANOTHER UGLY COLUMN name" = \'MyFilterString\'' I don't know if PostgreSQL requires a trailing semi-colon when used in DBI/DBD or not. I know that Oracle SQL requires a trailing semicolon in sqlplus but not in DBI/DBD. I don't have a PostgreSQL database to try it out on. Try it both ways, and the one that works is the one to use. Brian Fennell The information contained in this electronic mail transmission is intended only for the use of the individual or entity named in this transmission. If you are not the intended recipient of this transmission, you are hereby notified that any disclosure, copying or distribution of the contents of this transmission is strictly prohibited and that you should delete the contents of this transmission from your system immediately. Any comments or statements contained in this transmission do not necessarily reflect the views or position of Radial or its subsidiaries and/or affiliates.
RE: Translate between DBI and SQL
Mike, If you have a complete example of what you are starting with and what you want to end up with your question would be clearer. If I understand you correctly: You start with raw SQL such as you might enter into a Postgres command line tool (for example psql). What you want is the equivalent Perl source code to define the identical SQL as a string literal. Please look at the docs here: https://metacpan.org/pod/perlop https://metacpan.org/pod/perlop#Quote-and-Quote-like-Operators https://metacpan.org/pod/perlop#Gory-details-of-parsing-quoted-constructs https://metacpan.org/pod/perlop#Additive-Operators(Especially the “.” Operator) https://metacpan.org/pod/perlop#Assignment-Operators(Especially the “.=” Assignment Operator) https://metacpan.org/pod/perlfunc#ord-EXPR https://metacpan.org/pod/perlfunc#chr-NUMBER And see if that helps. The information contained in this electronic mail transmission is intended only for the use of the individual or entity named in this transmission. If you are not the intended recipient of this transmission, you are hereby notified that any disclosure, copying or distribution of the contents of this transmission is strictly prohibited and that you should delete the contents of this transmission from your system immediately. Any comments or statements contained in this transmission do not necessarily reflect the views or position of Radial or its subsidiaries and/or affiliates.