Including files

Поиск
Список
Период
Сортировка
От Dean Gibson (DB Administrator)
Тема Including files
Дата
Msg-id 5.1.0.14.2.20030508120123.00adeab8@dba-sql.ultimeth.net
обсуждение исходный текст
Список pgsql-general
I'm using PostgreSQL 7.3.2 (from RPM on this site) on RH Linux 7.

On several of the TABLEs I have, there are fields that contain codes.  For example, one such field is a two-character state code (eg, 'CA' for 'California').  In order to convert these codes to their full names, I have a lookup TABLE:

CREATE TABLE states(
    state_id     CHAR( 2 ),
    state_text  CHAR(15)
);

In a query, I have the following SQL:

SELECT
    ...
    address,
    city,
    (SELECT state_id || ' - ' || state_text::TEXT
        FROM  states
        WHERE state_id = state
        LIMIT 1)
      AS state,
    CASE WHEN substr( zip, 6 ) != '    ' THEN
            substr( zip, 1, 5 ) || '-' || substr( zip, 6 )
        ELSE
            zip
        END
      AS zip,
    ...;

All this works fine!  However, I would like to put the above (and many other code lookups) in VIEWs, and then JOIN the VIEWs.  Unfortunately, the TABLE lookups are not deferred until after any JOINs (by contrast, the CASE statement above IS apparently deferred until after JOINs);  this means that SELECTs based on such JOINed VIEWs can take a very long time.

So, this means that I need to put the above inner SELECT in a query or VIEW that does the JOIN, rather than in VIEWs that participate in the JOINs.  This is turn means that I have proliferate the inner SELECT into quite a few VIEWs that do the various JOINS I do, rather than in just one VIEW.

OK, so I'd like to put the inner SELECT into another file, and simply include it as follows:

SELECT
    ...
    address,
    city,
/i  stateSelect.sql
      ,
    CASE WHEN substr( zip, 6 ) != '    ' THEN
            substr( zip, 1, 5 ) || '-' || substr( zip, 6 )
        ELSE
            zip
        END
      AS zip,
    ...;

That means I have only one file to maintain for each such lookup table.  Unfortunately, this does not work.  While the documentation says, "Reads input from the file filename and executes it as though it had been typed on the keyboard.", this is not true.  Apparently "\i" includes are not processed through the query buffer:  in effect, the first part of the above select is stored in the query buffer, then the "\i stateSelect.sql" is read AND EXECUTED (giving an error), and then the remainder of the SELECT statement is read into the query buffer, and the SELECT statement is then executed without the included lines.

Which brings me to my question:  is there any way to include files into the query buffer?  Every other include facility that I've ever worked with, acts as though the included lines were typed AT THE POINT OF INCLUSION (like the docs infer).

My workaround is to manually copy the included portion into each required VIEW definition, but this creates quite a maintenance headache.  Any suggestions (other than an awk script)?

Sincerely, Dean

В списке pgsql-general по дате отправления:

Предыдущее
От: Dennis Gearon
Дата:
Сообщение: Re: resequencing a user defined integer column
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: Disk usage