Re: Using q() to define a query



On 2008-01-12 11:51:42 +0100, Dr.Ruud wrote:
Colin Wetherbee schreef:
my $sql = q(SELECT departure_date, eq.name AS equipment,
dp.full_city AS departure_city, ap.full_city AS arrival_city,
ca.name AS carrier_name, number
FROM jsjourneys
FULL OUTER JOIN jscarriers AS ca ON jsjourneys.carrier = ca.id
FULL OUTER JOIN jsequipment AS eq ON jsjourneys.equipment = eq.id
JOIN jsports AS dp ON jsjourneys.departure_port = dp.id
JOIN jsports AS ap ON jsjourneys.arrival_port = ap.id
ORDER BY departure_date);

(As an aside, how do you guys quote your queries? I find that for
anything longer than about 60 characters, q() and '' and everything
else start to look horribly inelegant.)


my $sql = <<'SQL';

SELECT
jo.departure_date AS departure
, eq.name AS equipment
, dp.full_city AS departure_city
, ap.full_city AS arrival_city
, ca.name AS carrier_name
, jo.number

FROM
jsjourneys AS jo
[...]
SQL

I almost never use here documents because they cannot be properly
indented:


sub foo {
some;
code;
here;
if (bla) {
more;
code;
here;
my $sql = <<'SQL';
SELECT
jo.departure_date AS departure
, eq.name AS equipment
, dp.full_city AS departure_city
, ap.full_city AS arrival_city
, ca.name AS carrier_name
, jo.number
[...]
SQL
even;
more;
}
code;
here;
}

just looks terrible. Of course with SQL leading whitespace doesn't
matter so you can just indent the whole statement and just have the
dangling terminator at the left edge[1] but that doesn't work for
multiline strings in general. Putting here documents in a function of
their own as proposed by Stephen helps, but the indentation is still
inconsistent.

Since initial whitespace doesn't matter in SQL, I'd just write that as:

sub foo {
some;
code;
here;
if (bla) {
more;
code;
here;
my $sql = q{
SELECT
jo.departure_date AS departure
, eq.name AS equipment
, dp.full_city AS departure_city
, ap.full_city AS arrival_city
, ca.name AS carrier_name
, jo.number
[...]
};
even;
more;
}
code;
here;
}

(actually, I'd put the commas at the end of the lines)

As an aside, the SPL programming language[2] allows the terminator of a
here document to be indented and to strip off everything up to and
including some character from each line, so that could be written like
this:

function foo() {
some;
code;
here;
if (bla) {
more;
code;
here;
var sql = >>SQL|
|SELECT
| jo.departure_date AS departure
|, eq.name AS equipment
|, dp.full_city AS departure_city
|, ap.full_city AS arrival_city
|, ca.name AS carrier_name
|, jo.number
|[...]
SQL;
even;
more;
}
code;
here;
}

BTW, some editors recognize certain heredoc-delimiters (like SQL) and
switch language for color coding, autocompletion, etc.

That's a neat feature of course and an argument for using
here-documents.

hp


[1] No, I don't think »my $sql = <<' SQL';« is a good idea.

[2] http://www.clifford.at/spl


--
_ | Peter J. Holzer | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR | I'd be programming in Java.
| | | hjp@xxxxxxxxx | I don't, and I'm not.
__/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users

Attachment: pgpI2ebokGlgY.pgp
Description: PGP signature



Relevant Pages

  • Re: Performance degradation with view and "left outer join" vs. "from x, outer y"
    ... Turned Every Which Way But Loose", I reminded the attendees that the ANSI SQL '92 parsing rules REQUIRE that filters in the WHERE clause of a query MUST be processed POST-JOIN in order for an RDBMS to be compliant. ... That means that your filter, c.custid = "AB1234", is applied after all rows of the customer table have been LEFT OUTER JOINed to the my_view VIEW with the results stored into a temp table. ... Now if I use the informix extended outer join syntax: ...
    (comp.databases.informix)
  • Re: Performance degradation with view and "left outer join" vs. "from x, outer y"
    ... "SQL Turned Every Which Way But Loose", I reminded the attendees that the ANSI SQL '92 parsing rules REQUIRE that filters in the WHERE clause of a query MUST be processed POST-JOIN in order for an RDBMS to be compliant. ... Even if I were to bypass the Hibernate generated SQL and roll my own, that did not work at all, it returned all rows unfiltered. ... Now if I use the informix extended outer join syntax: ...
    (comp.databases.informix)
  • Re: How to query with both JOIN and aliases in Access?
    ... Access SQL recognizes Inner Join, LEFT JOIN, and RIGHT JOIN. ... It does not recognize LEFT OUTER JOIN and JOIN. ... LEFT JOIN (SELECT empID, max(datum) as LastDayWorked ... This query performs in SQL Server without any problem. ...
    (microsoft.public.access.queries)
  • Re: Difference in Left Join, Right Join
    ... RIGHT syntax exist. ... If you write SQL such that what you feel is the driving table is at ... The above is Oracle outer join syntax, ...
    (comp.databases.oracle.misc)
  • Re: Query with left outer join all of a sudden wont work?
    ... just doesn't work if it is a left or right outer join. ... rather than on the top most query calling it). ... SELECT * FROM [qryFarmer Contracts] ... can you post the whole SQL statement? ...
    (microsoft.public.access.queries)