Re: proposal \gcsv - Mailing list pgsql-hackers

From Daniel Verite
Subject Re: proposal \gcsv
Date
Msg-id db912388-f97a-4c97-a2aa-37fa1df3a5c7@manitou-mail.org
Whole thread Raw
In response to Re: proposal \gcsv  (Alvaro Herrera <alvherre@2ndquadrant.com>)
List pgsql-hackers
    Alvaro Herrera wrote:

> Can we fix that by adding some syntax to allow command aliases?
> So you could add to your .psqlrc something like
>
> \alias \gcsv \pset push all \; \cbuf; \; \pset pop
>
> where the \cbuf is a hypothetical "function" that expands to the current
> query buffer.  This needs some refining I guess, but it'd allow you to
> create your own shortcuts for the most common features you want without
> excessive typing effort.

Since variables can contain metacommands, they can be abused
as macros. For instance I think a declaration like this would work:

\set gcsv '\\pset push all  \\pset format csv \\g \\pset pop'

or with another pset with embedded single quotes:

\set gcsv '\\pset push all  \\pset format csv \\pset csv_fieldsep ''\\t'' \\g
\\pset pop'

This kind of usage is not mentioned explicitly in the doc, so it might be
hard to discover, but without the push/pop feature that doesn't exist,
we can already do that:

test=> \set gcsv '\\pset format csv \\pset csv_fieldsep ''\\t'' \\g'

test=> select 1,2 :gcsv | (echo "START OF OUTPUT"; cat)
Output format is csv.
Field separator for CSV is "    ".
START OF OUTPUT
?column?    ?column?
1    2


Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-mail.org
Twitter: @DanielVerite



pgsql-hackers by date:

Previous
From: John Naylor
Date:
Subject: Re: truncating timestamps on arbitrary intervals
Next
From: Alexey Bashtanov
Date:
Subject: Re: control max length of parameter values logged