I need to pass a sql query string to obtain the column aliases. For instance
select the_date as "date", round(months_between(first_date,second_date),0) months_old ,product,extract(year from the_date) year ,case when a=b then 'c' else 'd' end tough_one from ... where...
The result I am looking for is a string like:
date,months_old,product,year,tough_one

I have tried a couple of different techniques like loading the sql statement into a string and spliting the data by ",", but this gets honked up with the months between and round functions. The other issue is, that the sql statement is generated by different people, all of whom have their own unique way of aliasing the columns. Some use "as xyz", others just use a space ".. xyz" and some actually quote it, '... as "xyz"'. Further compounding the issue is that sometimes each "column" is on its own line, other times it is not. Other times the whole column takes up more than one line by itself. Thus the attempted ugliness of the example above.

My other thought was to use a regular expression to get rid of the commas within each "column". For example, get rid of the commas in the months_between and round function, leaving me with a string that I could split by comma and take the last piece of. However, I stink at regular expressions and I continously got either a syntax error or a bad result. I thought I could do something as simple as
$sql=/(*,*)/(*)/;
Surely you must be chuckling out loud at such foley.

Any thoughts of an efficient method of obtaining these results? Regards

In reply to parse a query string by ctaustin

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.