Style: SQL For Web Nerds Philip Greenspun
Style: SQL For Web Nerds Philip Greenspun
Sty le
Style
part of SQL for Web Nerds by Philip Greenspun
If your code isn't properly indented then you will never be able to debug it. How can we justify using the word "properly"? After all, the SQL parser doesn't take extra spaces or newlines into account. Software is indented properly when the structure of the software is revealed and when the indentation style is familiar to a community of programmers.
10/20/12
Sty le
If it doesn't fit nicely on one line, give each clause a separate line:
slc * eet fo nw rm es weessae>eprto_ae hr ydt xiaindt adapoe_ ='' n prvdp t odrb rlaedt ds,ceto_aeds re y ees_ae ec raindt ec
If the stuff for a particular clause won't fit on one line, put a newline immediately after the keyword that opens the clause. Then indent the items underneath. Here's an example from the ArsDigita Community System's static .html page administration section. We're querying the s a i _ a e table, which holds a copy of any .html files in the Unix file system: ttcpgs
slc eet t_hrcut*,9999999999)a npgs oca(on()'9G9G9G9G9' s _ae, t_hrsmdm_o.elnt(aebd),9999999999)a nbts oca(u(bslbgteghpg_oy)'9G9G9G9G9' s _ye fo sai_ae; rm ttcpgs
In this query, there are two items in the select list, a count of all the rows and a sum of the bytes in the p g _ o ycolumn (of type CLOB, aebd hence the requirement to use d m _ o . e l n t rather than simply l n t ). We want Oracle to format these numbers with separation bslbgtegh egh characters between every three digits. For this, we have to use the t _ h rfunction and a mask of ' 9 G 9 G 9 G 9 G 9 '(the "G" tells oca 9999999999 Oracle to use the appropriate character depending on the country where it is installed, e.g., comma in the U.S. and period in Europe). Then we have to give the results correlation names so that they will be easy to use as Tcl variables. By the time we're done with all of this, it would be confusing to put both items on the same line. Here's another example, this time from the top-level comment administation page for the ArsDigita Community System. We're going to get back a single row with a count of each type of user-submitted comment:
slc eet cut* a nttl on() s _oa, smdcd(omn_ye'lentv_esetv'10)a natraieprpcie, u(eoecmettp,atraieprpcie,,) s _lentv_esetvs smdcd(omn_ye'aig,,) a nrtns u(eoecmettp,rtn'10) s _aig, smdcd(omn_ye'nnwrdqeto'10)a nuasee_usin, u(eoecmettp,uasee_usin,,) s _nnwrdqetos smdcd(omn_ye'rvt_esg_opg_uhr'10)a npiaemsae u(eoecmettp,piaemsaet_aeatos,,) s _rvt_esgs
philip.greenspun.com/sql/sty le.html 2/4
fo cmet rm omns
Notice the use of s m d c d to count the number of each type of comment. This gives us similar information to what we'd get from a u(eoe GROUP BY, but we get a sum total as well as category totals. Also, the numbers come out with the column names of our choice. Of course, this kind of query only works when you know in advance the possible values of c m e t t p . omn_ye The Oracle docs on DECODE for an explanation of the number formatting wizardry, see Oracle8 Server SQL Reference section on format conversion
Reader's Comments
The where clause is in two lines in the example above though the text suggests one line: "If it doesn't fit nicely on one line, give each clause a separate line: select * from news where sysdate > expiration_date and approved_p = 't' order by release_date desc, creation_date desc" In this case, one line would be the better, of course.
10/20/12
Sty le
philip.greenspun.com/sql/sty le.html
4/4