\input{../../generic-preamble/generic-preamble}

\def\subjnum{6.830}
\def\subjname{Database Systems}
\author{Dan Ports}
\authoremail{drkp@mit.edu}
\usepackage{longtable}
%
% SQL
%

% The psqlres environment pretty-prints tables copied verbatim from
% the output of the Postgres client.  Like the tabular environment, it
% takes a single argument that specifies the formatting of the columns.
% This format will be applied to the body of the table.  The column
% headers will always be centered.
\makeatletter
{
% Make TeX's scanner correctly parse the active characters used in the
% following definition
\catcode`\^^M=13
\catcode`|=13
\catcode`-=13
\gdef\psqlres#1{%
  % The | character separates columns.  In the header, the columns are
  % centered.  In the body, the columns will no longer be centered.
  \catcode`|=13%
  \def|{\psqlres@col}%
  \gdef\psqlres@col{\hfil&\hfil}%
  % Newlines separate rows.  In the header, we ignore newlines.  In
  % the body, a newline will be equivalent to \\.
  \catcode`\^^M=13%
  \def^^M{\psqlres@row}%
  \gdef\psqlres@row{}%
  % The - character separates the header and the body.
  \catcode`-=13%
  \def-{%
    % Finish up the header
    \hfil\cr\hline%
    % Ignore + and - for the rest of this line
    \catcode`+=9%
    \catcode`-=9%
    % Stop centering columns
    \gdef\psqlres@col{&}%
    % When we reach the end of the line, stop ignoring + and - and
    % start treating newline as a row separator.
    \gdef\psqlres@row{%
      \catcode`+=12 \catcode`-=12%
      \gdef\psqlres@row{\cr}%
    }%
  }%
  % Begin the table and center the first column
  \setlength\LTpre{0pt}
  \setlength\LTpost{0pt}
  \longtable{#1}%
  \multicolumn{\the\LT@cols}{c}{$\vdots$} \endfoot%
  \endlastfoot%
  \hfil}
\gdef\endpsqlres{\endlongtable}
}
\makeatother
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 

