Skip site navigation (1) Skip section navigation (2)

Fwd: psql include file using relative path

From: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
To: PGSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Fwd: psql include file using relative path
Date: 2011-02-24 23:21:39
Message-ID: AANLkTinVf1FwKKm61AWZLSTJ3Q8swOAxJXDHvWupBPdq@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-hackers
    psql has the ability to execute commands from a file, but if one wishes
to develop and provide a modularized set of sql files, then psql is not very
helpful because the \i command can open file paths either if they are
absolute paths or if they are palced correctly relative to psql's current
working directory.

Attached patch adds a new meta-command to psql, '\ir' that allows the user
to process files relative to currently processing file.

Also attached is a sample use case ir_sample. One can extract it _anywhere_
on the filesystem and invoke psql with the path to main.sql and the rest of
the files will be automatically included from that location.

Sample session:

[/tmp]$ psql -f ~/dev/ir_sample/main.sql
processing main.sql
BEGIN
processing subdir1/1.sql
processing subdir1/2.sql
processing subdir2/1.sql
processing subdir2/subdir2.1/1.sql
processing subdir2/2.sql
processing subdir2/3.sql
COMMIT

And here's what the sample's directory structure and files look like:

[ir_sample]$ find ./ -name "*.sql" | while read f; do echo === $f ====; cat
$f; done
=== ./main.sql ====
\echo processing main.sql
BEGIN;
\ir subdir1/1.sql
\ir subdir1/2.sql
\ir subdir2/1.sql
\ir subdir2/2.sql
\ir subdir2/3.sql
COMMIT;
=== ./subdir1/1.sql ====
\echo processing subdir1/1.sql
=== ./subdir1/2.sql ====
\echo processing subdir1/2.sql
=== ./subdir2/subdir2.1/1.sql ====
\echo processing subdir2/subdir2.1/1.sql
=== ./subdir2/1.sql ====
\echo processing subdir2/1.sql
\ir subdir2.1/1.sql
=== ./subdir2/2.sql ====
\echo processing subdir2/2.sql
=== ./subdir2/3.sql ====
\echo processing subdir2/3.sql

Regards,
-- 
Gurjeet Singh
EnterpriseDB Corporation <http://www.enterprisedb.com/>
The Enterprise PostgreSQL <http://www.postgresql.org/> Company

Attachment: ir_sample.tgz
Description: application/x-gzip (422 bytes)
Attachment: psql_ir.patch
Description: text/x-patch (4.3 KB)

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2011-02-24 23:36:03
Subject: Re: wCTE behaviour
Previous:From: Josh BerkusDate: 2011-02-24 20:42:39
Subject: Re: WIP: cross column correlation ...

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group