1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/ecpg-ref.sgml,v 1.17 2002/01/18 20:32:54 momjian Exp $
PostgreSQL documentation
-->
<refentry id="APP-ECPG">
<refmeta>
<refentrytitle id="app-ecpg-title">
<application>ecpg</application>
</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo>Application</refmiscinfo>
</refmeta>
<refnamediv>
<refname>
<application>ecpg</application>
</refname>
<refpurpose>
embedded SQL C preprocessor
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<refsynopsisdivinfo>
<date>1999-07-20</date>
</refsynopsisdivinfo>
<cmdsynopsis>
<command>ecpg</command>
<arg choice="opt">-v</arg>
<arg choice="opt">-t</arg>
<arg choice="opt">-I <replaceable>include-path</replaceable></arg>
<arg choice="opt">-o <replaceable>outfile</replaceable></arg>
<arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg>
</cmdsynopsis>
<refsect2 id="R2-APP-ECPG-1">
<refsect2info>
<date>1999-07-20</date>
</refsect2info>
<title>
Inputs
</title>
<para>
<application>ecpg</application> accepts the following command
line arguments:
<variablelist>
<varlistentry>
<term>-v</term>
<listitem>
<para>
Print version information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>-t</term>
<listitem>
<para>
Turn on auto-commit of transactions. In this mode, each query is
automatically committed unless it is inside an explicit
transaction block. In the default mode, queries are committed
only when <command>exec sql commit</command> is issued.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>-I <replaceable class="parameter">include-path</replaceable></term>
<listitem>
<para>
Specify an additional include path.
Defaults are <filename>.</filename> (current directory),
<filename>/usr/local/include</filename>, the
<productname>PostgreSQL</productname> include path which is
defined at compile time (default:
<filename>/usr/local/pgsql/include</filename>), and
<filename>/usr/include</filename>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>-o <replaceable>outfile</replaceable></term>
<listitem>
<para>
Specifies that <application>ecpg</application> should write all its output to outfile.
If no such option is given the output is written to
<filename><replaceable>name</replaceable>.c</filename>,
assuming the input file was
named <filename><replaceable>name</replaceable>.pgc</filename>.
If the input file does have the expected
<literal>.pgc</literal> suffix, then the output file will have
<literal>.pgc</literal> appended to the input file name.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">file</replaceable></term>
<listitem>
<para>
The files to be processed.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
<refsect2 id="R2-APP-ECPG-2">
<refsect2info>
<date>1998-11-05</date>
</refsect2info>
<title>
Outputs
</title>
<para>
<application>ecpg</application> will create a file or
write to <filename>stdout</filename>.
<variablelist>
<varlistentry>
<term>Return value</term>
<listitem>
<para>
<application>ecpg</application> returns 0 to the shell on successful completion, non-zero
for errors.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
</refsynopsisdiv>
<refsect1 id="R1-APP-ECPG-description">
<title>Description</title>
<para>
<application>ecpg</application>
is an embedded SQL preprocessor for the C language and the
<productname>PostgreSQL</productname>. It
enables development of C programs with embedded SQL code.
</para>
<para>
Linus Tolke (<email>linus@epact.se</email>) was the
original author of <application>ecpg</application> (up to version 0.2).
Michael Meskes (<email>meskes@debian.org</email>)
is the current author and maintainer of <application>ecpg</application>.
Thomas Good (<email>tomg@q8.nrnet.org</email>)
is the author of the last revision of the <application>ecpg</application> man page, on which
this document is based.
</para>
</refsect1>
<refsect1 id="R1-APP-ECPG-2">
<title>Usage</title>
<refsect2 id="R2-APP-ECPG-preprocessing">
<title>Preprocessing for Compilation</title>
<para>
An embedded SQL source file must be preprocessed before
compilation:
<synopsis>
ecpg [ -d ] [ -o <replaceable>file</replaceable> ] <replaceable>file</replaceable>.pgc
</synopsis>
where the optional <option>-d</option> flag turns on debugging.
The <literal>.pgc</literal> extension is an
arbitrary means of denoting <application>ecpg</application> source.
</para>
<para>
You may want to redirect the preprocessor output to a log file.
</para>
</refsect2>
<refsect2 id="R2-APP-ECPG-compiling">
<title>Compiling and Linking</title>
<para>
Assuming the <productname>PostgreSQL</productname> binaries are in
<filename>/usr/local/pgsql</filename>, you will need to compile
and link your preprocessed source file:
<synopsis>
gcc -g -I /usr/local/pgsql/include [ -o <replaceable>file</replaceable> ] <replaceable>file</replaceable>.c -L /usr/local/pgsql/lib -lecpg -lpq
</synopsis>
</para>
</refsect2>
</refsect1>
<refsect1 id="R1-APP-ECPG-grammar">
<title>Grammar</title>
<refsect2 id="R2-APP-ECPG-library">
<title>Libraries</title>
<para>
The preprocessor will prepend two directives to the source:
<programlisting>
#include <ecpgtype.h>
#include <ecpglib.h>
</programlisting>
</para>
</refsect2>
<refsect2 id="R2-APP-declaration">
<title>Variable Declaration</title>
<para>
Variables declared within <application>ecpg</application> source code must be prepended with:
<programlisting>
EXEC SQL BEGIN DECLARE SECTION;
</programlisting>
</para>
<para>
Similarly, variable declaration sections must terminate with:
<programlisting>
EXEC SQL END DECLARE SECTION;
</programlisting>
<note>
<para>
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:
<programlisting>
char foo[16], bar[16];
</programlisting>
</para>
</note>
</para>
</refsect2>
<refsect2 id="R2-APP-ECPG-errors">
<title>Error Handling</title>
<para>
The SQL communication area is defined with:
<programlisting>
EXEC SQL INCLUDE sqlca;
</programlisting>
</para>
<note>
<para>
The <literal>sqlca</literal> is in lowercase.
While SQL convention may be
followed, i.e., using uppercase to separate embedded SQL
from C statements, sqlca (which includes the <filename>sqlca.h</>
header file) <emphasis>must</> be lowercase. This is because the
EXEC SQL prefix indicates that this inclusion will be parsed by
<application>ecpg</application>.
<application>ecpg</application> observes case sensitivity
(<filename>SQLCA.h</> will not be found).
<command>EXEC SQL INCLUDE</command>
can be used to include other header files
as long as case sensitivity is observed.
</para>
</note>
<para>
The sqlprint command is used with the EXEC SQL WHENEVER
statement to turn on error handling throughout the
program:
<programlisting>
EXEC SQL WHENEVER sqlerror sqlprint;
</programlisting>
and
<programlisting>
EXEC SQL WHENEVER not found sqlprint;
</programlisting>
</para>
<note>
<para>
This is <emphasis>not</emphasis> an exhaustive example of usage for
the <command>EXEC SQL WHENEVER</command> statement.
Further examples of usage may
be found in SQL manuals (e.g., <citetitle>The LAN TIMES Guide to SQL</> by
Groff and Weinberg).
</para>
</note>
</refsect2>
<refsect2 id="R2-APP-ECPG-connecting">
<title>Connecting to the Database Server</title>
<para>
One connects to a database using the following:
<programlisting>
EXEC SQL CONNECT TO <replaceable>dbname</replaceable>;
</programlisting>
where the database name is not quoted. Prior to version 2.1.0, the
database name was required to be inside single quotes.
</para>
<para>
Specifying a server and port name in the connect statement is also
possible. The syntax is:
<synopsis>
<replaceable>dbname</replaceable>[@<replaceable>server</replaceable>][:<replaceable>port</replaceable>]
</synopsis>
or
<synopsis>
<tcp|unix>:postgresql://<replaceable>server</replaceable>[:<replaceable>port</replaceable>][/<replaceable>dbname</replaceable>][?<replaceable>options</replaceable>]
</synopsis>
</para>
</refsect2>
<refsect2 id="R2-APP-ECPG-queries">
<title>Queries</title>
<para>
In general, SQL queries acceptable to other applications such as
<application>psql</application> can be embedded into your C
code. Here are some examples of how to do that.
</para>
<para>
Create Table:
<programlisting>
EXEC SQL CREATE TABLE foo (number int4, ascii char(16));
EXEC SQL CREATE UNIQUE index num1 on foo(number);
EXEC SQL COMMIT;
</programlisting>
</para>
<para>
Insert:
<programlisting>
EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad');
EXEC SQL COMMIT;
</programlisting>
</para>
<para>
Delete:
<programlisting>
EXEC SQL DELETE FROM foo WHERE number = 9999;
EXEC SQL COMMIT;
</programlisting>
</para>
<para>
Singleton Select:
<programlisting>
EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = 'doodad';
</programlisting>
</para>
<para>
Select using Cursors:
<programlisting>
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;
</programlisting>
</para>
<para>
Updates:
<programlisting>
EXEC SQL UPDATE foo
SET ascii = 'foobar'
WHERE number = 9999;
EXEC SQL COMMIT;
</programlisting>
</para>
</refsect2>
</refsect1>
<refsect1 id="R1-APP-ECPG-notes">
<title>Notes</title>
<para>
The complete structure definition MUST be listed
inside the declare section.
</para>
<para>
See the <filename>TODO</filename> file in the source for some more
missing features.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->