ECPG(UNIX)

NAME
ecpg - embedded SQL preprocessor for C / PostgreSQL

SYNOPSIS
ecpg [-v ] [-t] [-I include-path ] [-o outfile ] file1 [ file2 ] [ ... ]

DESCRIPTION
ecpg is an embedded SQL preprocessor for C / PostgreSQL. It enables development of C programs with embedded SQL code.

ecpg is ultimately intended to be as compliant as possible with the ANSI SQL-2 standard and existing commercial ESQL/C packages.

OPTIONS
ecpg interprets the following flags when it is invoked on the command line:

-v  Print version information.

-t  Turn off auto-transactin mode.

-I include-path
    Specify additional include path. Defaults are ., /usr/local/include, the PostgreSQL include path which is defined at compile time (default: /usr/local/pgsql/lib), /usr/include

-o  Specifies that ecpg should write all its output to outfile. If no such option is given the output is written to foo.c (if the input file was named foo.pgc.) If the input file was named foo.bar the output file will be named foo.bar.c.

file1, file2...
    The files to be processed.

INSTALLATION
The ecpg preprocessor is built during the PostgreSQL installation. Binaries and libraries are installed into the PGBASE (i.e., /usr/local/pgsql/... ) subdirectories.

PREPROCESSING FOR COMPILATION
ecpg (-o file) file.pgc

The .pgc extension is an arbitrary means of denoting ecpg source.

COMPILING AND LINKING
Assuming the PostgreSQL binaries are in /usr/local/pgsql:

gcc -g -i /usr/local/pgsql/include (-o file) file.c -L /usr/local/pgsql/lib -lecpg -lpq

ECPG GRAMMAR

LIBRARIES

The preprocessor will prepend two directives to the source:

#include <ecpgtype.h> and #include <ecpglib.h>

VARIABLE DECLARATION
Variables declared within ecpg source code must be prepended with:

EXEC SQL BEGIN DECLARE SECTION;

Similarly, variable declaration sections must terminate with:

EXEC SQL END DECLARE SECTION;

NOTE: prior to version 2.1.0, each variable had to be declared on a separate line. As of version 2.1.0 multiple variables may be declared on a single line:

char foo(16), bar(16);

ERROR HANDLING
The SQL communication area is defined with:

EXEC SQL INCLUDE sqlca;

NOTE: the lowercase `sqlca'. While SQL convention may be followed, i.e., using uppercase to separate embedded SQL from C statements, sqlca (which includes the sqlca.h header file) MUST be lowercase. This is because the EXEC SQL prefix indicates that this INCLUDE will be parsed by ecpg. ecpg observes case sensitivity (SQLCA.h will not be found.) EXEC SQL INCLUDE can be used to include other header files as long as case sensitivity is observed.

The sqlprint command is used with the EXEC SQL WHENEVER statement to turn on error handling throughout the program:

EXEC SQL WHENEVER sqlerror sqlprint;

EXEC SQL WHENEVER not found sqlprint;

PLEASE NOTE: this is *not* an exhaustive example of usage for the EXEC SQL WHENEVER statement. Further examples of usage may be found in SQL manuals (e.g., `The LAN TIMES Guide to SQL' by Groff and Weinberg.)

CONNECTING TO THE DATABASE SERVER
Prior to version 2.1.0 the database name was single quoted:
    EXEC SQL CONNECT 'test1';

As of version 2.1.0, the syntax has been simplified:

    EXEC SQL CONNECT test1;
(The database name is no longer quoted.)

Specifying a server and port name in the connect statement is also possible as of version 6.4. of PostgreSQL. The syntax is:

    dbname[@server][:port]

or

    <tcp|unix>:postgresql://server[:port][/dbname][?options]

QUERIES

  Create Table:

EXEC SQL CREATE TABLE foo (number int4, ascii char(16));
    EXEC SQL CREATE UNIQUE index num1 on foo(number);
EXEC SQL COMMIT;

  Insert:

EXEC SQL INSERT INTO foo (number, ascii)
    VALUES (9999, 'doodad');
EXEC SQL COMMIT;

  Delete:

EXEC SQL DELETE FROM foo
    WHERE number = 9999;
EXEC SQL COMMIT;

  Singleton Select:

EXEC SQL SELECT foo INTO :FooBar FROM table1
    WHERE ascii = 'doodad';

  Select using Cursors:

EXEC SQL DECLARE foo_bar CURSOR FOR
    SELECT number, ascii FROM foo
        ORDER BY ascii;
EXEC SQL FETCH foo_bar INTO :FooBar, DooDad;

EXEC SQL CLOSE foo_bar;
    EXEC SQL COMMIT;

  Updates

EXEC SQL UPDATE foo
    SET ascii = 'foobar'
    WHERE number = 9999;
EXEC SQL COMMIT;

BUGS

The is no EXEC SQL PREPARE statement.

The complete structure definition MUST be listed inside the declare section.

See the TODO file in the source for some more missing features.

RETURN VALUE

ecpg returns 0 to the shell on successful completion, -1 for errors.

SEE ALSO
cc(1), pgintro(l), commit(l), delete(l)
fetch(l), select(l), sql(l) , update(l)

FILES
/usr/src/pgsql/postgresql-${ver}/src/interfaces...
     ./ecpg/include.......source for ecpg header files.
     ./ecpg/lib...........source for ecpg libraries.
     ./ecpg/preproc.......source for ecpg header files.
     ./ecpg/test..........source for ecpg libraries.
     (test contains examples of syntax for ecpg SQL-C.)

/usr/local/pgsql/bin
    PostgreSQL binaries including ecpg.

/usr/local/pgsql/include
    PostgreSQL headers including ecpglib.h ecpgtype.h and sqlca.h.

/usr/local/pgsql/lib
    PostgreSQL libraries including libecpg.a and libecpg.so.

AUTHORS
Linus Tolke <linus@epact.se> - original author of ECPG (up to version 0.2).

Michael Meskes <meskes@debian.org> - actual author and maintainer of ECPG.

Thomas Good <tomg@q8.nrnet.org> - author of this revision of the ecpg man page.