~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to doc/src/sgml/ecpg.sgml

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
$PostgreSQL: pgsql/doc/src/sgml/ecpg.sgml,v 1.62.4.1 2005-01-22 23:05:47 momjian Exp $
 
3
-->
 
4
 
 
5
<chapter id="ecpg">
 
6
 <title><application>ECPG</application> - Embedded <acronym>SQL</acronym> in C</title>
 
7
 
 
8
 <indexterm zone="ecpg"><primary>embedded SQL</primary><secondary>in C</secondary></indexterm>
 
9
 <indexterm zone="ecpg"><primary>C</primary></indexterm>
 
10
 <indexterm zone="ecpg"><primary>ECPG</primary></indexterm>
 
11
 
 
12
 <para>
 
13
  This chapter describes the embedded <acronym>SQL</acronym> package
 
14
  for <productname>PostgreSQL</productname>. It was written by
 
15
  Linus Tolke (<email>linus@epact.se</email>) and Michael Meskes
 
16
  (<email>meskes@postgresql.org</email>). Originally it was written to work with
 
17
  <acronym>C</acronym>. It also works with <acronym>C++</acronym>, but
 
18
  it does not recognize all <acronym>C++</acronym> constructs yet. 
 
19
 </para>
 
20
 
 
21
 <para>
 
22
  This documentation is quite incomplete.  But since this
 
23
  interface is standardized, additional information can be found in
 
24
  many resources about SQL.
 
25
 </para>
 
26
 
 
27
 <sect1 id="ecpg-concept">
 
28
  <title>The Concept</title>
 
29
 
 
30
  <para>
 
31
   An embedded SQL program consists of code written in an ordinary
 
32
   programming language, in this case C, mixed with SQL commands in
 
33
   specially marked sections.  To build the program, the source code
 
34
   is first passed through the embedded SQL preprocessor, which converts it
 
35
   to an ordinary C program, and afterwards it can be processed by a C
 
36
   compiler.
 
37
  </para>
 
38
 
 
39
  <para>
 
40
   Embedded <acronym>SQL</acronym> has advantages over other methods
 
41
   for handling <acronym>SQL</acronym> commands from C code. First, it
 
42
   takes care of the tedious passing of information to and from
 
43
   variables in your <acronym>C</acronym> program.  Second, the SQL
 
44
   code in the program is checked at build time for syntactical
 
45
   correctness.  Third, embedded <acronym>SQL</acronym> in C is
 
46
   specified in the <acronym>SQL</acronym> standard and supported by
 
47
   many other <acronym>SQL</acronym> database systems.  The
 
48
   <productname>PostgreSQL</> implementation is designed to match this
 
49
   standard as much as possible, and it is usually possible to port
 
50
   embedded <acronym>SQL</acronym> programs written for other SQL
 
51
   databases to <productname>PostgreSQL</productname> with relative
 
52
   ease.
 
53
  </para>
 
54
 
 
55
  <para>
 
56
   As already stated, programs written for the embedded
 
57
   <acronym>SQL</acronym> interface are normal C programs with special
 
58
   code inserted to perform database-related actions.  This special
 
59
   code always has the form
 
60
<programlisting>
 
61
EXEC SQL ...;
 
62
</programlisting>
 
63
   These statements syntactically take the place of a C statement.
 
64
   Depending on the particular statement, they may appear at the
 
65
   global level or within a function.  Embedded
 
66
   <acronym>SQL</acronym> statements follow the case-sensitivity rules
 
67
   of normal <acronym>SQL</acronym> code, and not those of C.
 
68
  </para>
 
69
 
 
70
  <para>
 
71
   The following sections explain all the embedded SQL statements.
 
72
  </para>
 
73
 </sect1>
 
74
 
 
75
 <sect1 id="ecpg-connect">
 
76
  <title>Connecting to the Database Server</title>
 
77
 
 
78
  <para>
 
79
   One connects to a database using the following statement:
 
80
<programlisting>
 
81
EXEC SQL CONNECT TO <replaceable>target</replaceable> <optional>AS <replaceable>connection-name</replaceable></optional> <optional>USER <replaceable>user-name</replaceable></optional>;
 
82
</programlisting>
 
83
   The <replaceable>target</replaceable> can be specified in the
 
84
   following ways:
 
85
 
 
86
   <itemizedlist>
 
87
    <listitem>
 
88
     <simpara>
 
89
      <literal><replaceable>dbname</><optional>@<replaceable>hostname</></optional><optional>:<replaceable>port</></optional></literal>
 
90
     </simpara>
 
91
    </listitem>
 
92
 
 
93
    <listitem>
 
94
     <simpara>
 
95
      <literal>tcp:postgresql://<replaceable>hostname</><optional>:<replaceable>port</></optional><optional>/<replaceable>dbname</></optional><optional>?<replaceable>options</></optional></literal>
 
96
     </simpara>
 
97
    </listitem>
 
98
 
 
99
    <listitem>
 
100
     <simpara>
 
101
      <literal>unix:postgresql://<replaceable>hostname</><optional>:<replaceable>port</></optional><optional>/<replaceable>dbname</></optional><optional>?<replaceable>options</></optional></literal>
 
102
     </simpara>
 
103
    </listitem>
 
104
 
 
105
    <listitem>
 
106
     <simpara>
 
107
      an SQL string literal containing one of the above forms
 
108
     </simpara>
 
109
    </listitem>
 
110
 
 
111
    <listitem>
 
112
     <simpara>
 
113
      a reference to a character variable containing one of the above forms (see examples)
 
114
     </simpara>
 
115
    </listitem>
 
116
 
 
117
    <listitem>
 
118
     <simpara>
 
119
      <literal>DEFAULT</literal>
 
120
     </simpara>
 
121
    </listitem>
 
122
   </itemizedlist>
 
123
 
 
124
   If you specify the connection target literally (that is, not
 
125
   through a variable reference) and you don't quote the value, then
 
126
   the case-insensitivity rules of normal SQL are applied.  In that
 
127
   case you can also double-quote the individual parameters separately
 
128
   as needed.  In practice, it is probably less error-prone to use a
 
129
   (single-quoted) string literal or a variable reference.  The
 
130
   connection target <literal>DEFAULT</literal> initiates a connection
 
131
   to the default database under the default user name.  No separate
 
132
   user name or connection name may be specified in that case.
 
133
  </para>
 
134
 
 
135
  <para>
 
136
   There are also different ways to specify the user name:
 
137
 
 
138
   <itemizedlist>
 
139
    <listitem>
 
140
     <simpara>
 
141
      <literal><replaceable>username</replaceable></literal>
 
142
     </simpara>
 
143
    </listitem>
 
144
 
 
145
    <listitem>
 
146
     <simpara>
 
147
      <literal><replaceable>username</replaceable>/<replaceable>password</replaceable></literal>
 
148
     </simpara>
 
149
    </listitem>
 
150
 
 
151
    <listitem>
 
152
     <simpara>
 
153
      <literal><replaceable>username</replaceable> IDENTIFIED BY <replaceable>password</replaceable></literal>
 
154
     </simpara>
 
155
    </listitem>
 
156
 
 
157
    <listitem>
 
158
     <simpara>
 
159
      <literal><replaceable>username</replaceable> USING <replaceable>password</replaceable></literal>
 
160
     </simpara>
 
161
    </listitem>
 
162
   </itemizedlist>
 
163
 
 
164
   As above, the parameters <replaceable>username</replaceable> and
 
165
   <replaceable>password</replaceable> may be an SQL identifier, an
 
166
   SQL string literal, or a reference to a character variable.
 
167
  </para>
 
168
 
 
169
  <para>
 
170
   The <replaceable>connection-name</replaceable> is used to handle
 
171
   multiple connections in one program.  It can be omitted if a
 
172
   program uses only one connection.  The most recently opened
 
173
   connection becomes the current connection, which is used by default
 
174
   when an SQL statement is to be executed (see later in this
 
175
   chapter).
 
176
  </para>
 
177
 
 
178
  <para>
 
179
   Here are some examples of <command>CONNECT</command> statements:
 
180
<programlisting>
 
181
EXEC SQL CONNECT TO mydb@sql.mydomain.com;
 
182
 
 
183
EXEC SQL CONNECT TO 'unix:postgresql://sql.mydomain.com/mydb' AS myconnection USER john;
 
184
 
 
185
EXEC SQL BEGIN DECLARE SECTION;
 
186
const char *target = "mydb@sql.mydomain.com";
 
187
const char *user = "john";
 
188
EXEC SQL END DECLARE SECTION;
 
189
 ...
 
190
EXEC SQL CONNECT TO :target USER :user;
 
191
</programlisting>
 
192
   The last form makes use of the variant referred to above as
 
193
   character variable reference.  You will see in later sections how C
 
194
   variables can be used in SQL statements when you prefix them with a
 
195
   colon.
 
196
  </para>
 
197
 
 
198
  <para>
 
199
   Be advised that the format of the connection target is not
 
200
   specified in the SQL standard.  So if you want to develop portable
 
201
   applications, you might want to use something based on the last
 
202
   example above to encapsulate the connection target string
 
203
   somewhere.
 
204
  </para>
 
205
 </sect1>
 
206
 
 
207
 <sect1 id="ecpg-disconnect">
 
208
  <title>Closing a Connection</title>
 
209
 
 
210
  <para>
 
211
   To close a connection, use the following statement:
 
212
<programlisting>
 
213
EXEC SQL DISCONNECT <optional><replaceable>connection</replaceable></optional>;
 
214
</programlisting>
 
215
   The <replaceable>connection</replaceable> can be specified
 
216
   in the following ways:
 
217
 
 
218
   <itemizedlist>
 
219
    <listitem>
 
220
     <simpara>
 
221
      <literal><replaceable>connection-name</replaceable></literal>
 
222
     </simpara>
 
223
    </listitem>
 
224
 
 
225
    <listitem>
 
226
     <simpara>
 
227
      <literal>DEFAULT</literal>
 
228
     </simpara>
 
229
    </listitem>
 
230
 
 
231
    <listitem>
 
232
     <simpara>
 
233
      <literal>CURRENT</literal>
 
234
     </simpara>
 
235
    </listitem>
 
236
 
 
237
    <listitem>
 
238
     <simpara>
 
239
      <literal>ALL</literal>
 
240
     </simpara>
 
241
    </listitem>
 
242
   </itemizedlist>
 
243
 
 
244
   If no connection name is specified, the current connection is
 
245
   closed.
 
246
  </para>
 
247
 
 
248
  <para>
 
249
   It is good style that an application always explicitly disconnect
 
250
   from every connection it opened.
 
251
  </para>
 
252
 </sect1>
 
253
 
 
254
 <sect1 id="ecpg-commands">
 
255
  <title>Running SQL Commands</title>
 
256
 
 
257
  <para>
 
258
   Any SQL command can be run from within an embedded SQL application.
 
259
   Below are some examples of how to do that.
 
260
  </para>
 
261
 
 
262
  <para>
 
263
   Creating a table:
 
264
<programlisting>
 
265
EXEC SQL CREATE TABLE foo (number integer, ascii char(16));
 
266
EXEC SQL CREATE UNIQUE INDEX num1 ON foo(number);
 
267
EXEC SQL COMMIT;
 
268
</programlisting>
 
269
  </para>
 
270
 
 
271
  <para>
 
272
   Inserting rows:
 
273
<programlisting>
 
274
EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad');
 
275
EXEC SQL COMMIT;
 
276
</programlisting>
 
277
  </para>
 
278
 
 
279
  <para>
 
280
   Deleting rows:
 
281
<programlisting>
 
282
EXEC SQL DELETE FROM foo WHERE number = 9999;
 
283
EXEC SQL COMMIT;
 
284
</programlisting>
 
285
  </para>
 
286
 
 
287
  <para>
 
288
   Single-row select:
 
289
<programlisting>
 
290
EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = 'doodad';
 
291
</programlisting>
 
292
  </para>
 
293
 
 
294
  <para>
 
295
   Select using cursors:
 
296
<programlisting>
 
297
EXEC SQL DECLARE foo_bar CURSOR FOR
 
298
    SELECT number, ascii FROM foo
 
299
    ORDER BY ascii;
 
300
EXEC SQL OPEN foo_bar;
 
301
EXEC SQL FETCH foo_bar INTO :FooBar, DooDad;
 
302
...
 
303
EXEC SQL CLOSE foo_bar;
 
304
EXEC SQL COMMIT;
 
305
</programlisting>
 
306
  </para>
 
307
 
 
308
  <para>
 
309
   Updates:
 
310
<programlisting>
 
311
EXEC SQL UPDATE foo
 
312
    SET ascii = 'foobar'
 
313
    WHERE number = 9999;
 
314
EXEC SQL COMMIT;
 
315
</programlisting>
 
316
  </para>
 
317
 
 
318
  <para>
 
319
   The tokens of the form
 
320
   <literal>:<replaceable>something</replaceable></literal> are
 
321
   <firstterm>host variables</firstterm>, that is, they refer to
 
322
   variables in the C program.  They are explained in <xref
 
323
   linkend="ecpg-variables">.
 
324
  </para>
 
325
 
 
326
  <para>
 
327
   In the default mode, statements are committed only when
 
328
   <command>EXEC SQL COMMIT</command> is issued. The embedded SQL
 
329
   interface also supports autocommit of transactions (similar to
 
330
   <application>libpq</> behavior) via the <option>-t</option> command-line
 
331
   option to <command>ecpg</command> (see below) or via the <literal>EXEC SQL
 
332
   SET AUTOCOMMIT TO ON</literal> statement. In autocommit mode, each
 
333
   command is automatically committed unless it is inside an explicit
 
334
   transaction block. This mode can be explicitly turned off using
 
335
   <literal>EXEC SQL SET AUTOCOMMIT TO OFF</literal>.
 
336
  </para>
 
337
 </sect1>
 
338
 
 
339
 <sect1 id="ecpg-set-connection">
 
340
  <title>Choosing a Connection</title>
 
341
 
 
342
  <para>
 
343
   The SQL statements shown in the previous section are executed on
 
344
   the current connection, that is, the most recently opened one.  If
 
345
   an application needs to manage multiple connections, then there are
 
346
   two ways to handle this.
 
347
  </para>
 
348
 
 
349
  <para>
 
350
   The first option is to explicitly choose a connection for each SQL
 
351
   statement, for example
 
352
<programlisting>
 
353
EXEC SQL AT <replaceable>connection-name</replaceable> SELECT ...;
 
354
</programlisting>
 
355
   This option is particularly suitable if the application needs to
 
356
   use several connections in mixed order.
 
357
      </para>
 
358
 
 
359
      <para>
 
360
      If your application uses multiple threads of execution, they cannot share a
 
361
      connection concurrently. You must either explicitly control access to the connection
 
362
      (using mutexes) or use a connection for each thread. If each thread uses its own connection,
 
363
      you will need to use the AT clause to specify which connection the thread will use.
 
364
  </para>
 
365
 
 
366
  <para>
 
367
   The second option is to execute a statement to switch the current
 
368
   connection.  That statement is:
 
369
<programlisting>
 
370
EXEC SQL SET CONNECTION <replaceable>connection-name</replaceable>;
 
371
</programlisting>
 
372
   This option is particularly convenient if many statements are to be
 
373
   executed on the same connection.  It is not thread-aware.
 
374
  </para>
 
375
 </sect1>
 
376
 
 
377
 <sect1 id="ecpg-variables">
 
378
  <title>Using Host Variables</title>
 
379
 
 
380
  <para>
 
381
   In <xref linkend="ecpg-commands"> you saw how you can execute SQL
 
382
   statements from an embedded SQL program.  Some of those statements
 
383
   only used fixed values and did not provide a way to insert
 
384
   user-supplied values into statements or have the program process
 
385
   the values returned by the query.  Those kinds of statements are
 
386
   not really useful in real applications.  This section explains in
 
387
   detail how you can pass data between your C program and the
 
388
   embedded SQL statements using a simple mechanism called
 
389
   <firstterm>host variables</firstterm>.
 
390
  </para>
 
391
 
 
392
  <sect2>
 
393
   <title>Overview</title>
 
394
 
 
395
   <para>
 
396
    Passing data between the C program and the SQL statements is
 
397
    particularly simple in embedded SQL.  Instead of having the
 
398
    program paste the data into the statement, which entails various
 
399
    complications, such as properly quoting the value, you can simply
 
400
    write the name of a C variable into the SQL statement, prefixed by
 
401
    a colon.  For example:
 
402
<programlisting>
 
403
EXEC SQL INSERT INTO sometable VALUES (:v1, 'foo', :v2);
 
404
</programlisting>
 
405
    This statements refers to two C variables named
 
406
    <varname>v1</varname> and <varname>v2</varname> and also uses a
 
407
    regular SQL string literal, to illustrate that you are not
 
408
    restricted to use one kind of data or the other.
 
409
   </para>
 
410
 
 
411
   <para>
 
412
    This style of inserting C variables in SQL statements works
 
413
    anywhere a value expression is expected in an SQL statement.  In
 
414
    the SQL environment we call the references to C variables
 
415
    <firstterm>host variables</firstterm>.
 
416
   </para>
 
417
  </sect2>
 
418
 
 
419
  <sect2>
 
420
   <title>Declare Sections</title>
 
421
 
 
422
   <para>
 
423
    To pass data from the program to the database, for example as
 
424
    parameters in a query, or to pass data from the database back to
 
425
    the program, the C variables that are intended to contain this
 
426
    data need to be declared in specially marked sections, so the
 
427
    embedded SQL preprocessor is made aware of them.
 
428
   </para>
 
429
 
 
430
   <para>
 
431
    This section starts with
 
432
<programlisting>
 
433
EXEC SQL BEGIN DECLARE SECTION;
 
434
</programlisting>
 
435
    and ends with
 
436
<programlisting>
 
437
EXEC SQL END DECLARE SECTION;
 
438
</programlisting>
 
439
    Between those lines, there must be normal C variable declarations,
 
440
    such as
 
441
<programlisting>
 
442
int   x;
 
443
char  foo[16], bar[16];
 
444
</programlisting>
 
445
    You can have as many declare sections in a program as you like.
 
446
   </para>
 
447
 
 
448
   <para>
 
449
    The declarations are also echoed to the output file as a normal C
 
450
    variables, so there's no need to declare them again.  Variables
 
451
    that are not intended to be used in SQL commands can be declared
 
452
    normally outside these special sections.
 
453
   </para>
 
454
 
 
455
   <para>
 
456
    The definition of a structure or union also must be listed inside
 
457
    a <literal>DECLARE</> section. Otherwise the preprocessor cannot
 
458
    handle these types since it does not know the definition.
 
459
   </para>
 
460
 
 
461
   <para>
 
462
    The special type <type>VARCHAR</type> 
 
463
    is converted into a named <type>struct</> for every variable. A
 
464
    declaration like
 
465
<programlisting>
 
466
VARCHAR var[180];
 
467
</programlisting>
 
468
    is converted into
 
469
<programlisting>
 
470
struct varchar_var { int len; char arr[180]; } var;
 
471
</programlisting>
 
472
    This structure is suitable for interfacing with SQL datums of type
 
473
    <type>varchar</type>.
 
474
   </para>
 
475
  </sect2>
 
476
 
 
477
  <sect2>
 
478
   <title><command>SELECT INTO</command> and <command>FETCH INTO</command></title>
 
479
 
 
480
   <para>
 
481
    Now you should be able to pass data generated by your program into
 
482
    an SQL command.  But how do you retrieve the results of a query?
 
483
    For that purpose, embedded SQL provides special variants of the
 
484
    usual commands <command>SELECT</command> and
 
485
    <command>FETCH</command>.  These commands have a special
 
486
    <literal>INTO</literal> clause that specifies which host variables
 
487
    the retrieved values are to be stored in.
 
488
   </para>
 
489
 
 
490
   <para>
 
491
    Here is an example:
 
492
<programlisting>
 
493
/*
 
494
 * assume this table:
 
495
 * CREATE TABLE test1 (a int, b varchar(50));
 
496
 */
 
497
 
 
498
EXEC SQL BEGIN DECLARE SECTION;
 
499
int v1;
 
500
VARCHAR v2;
 
501
EXEC SQL END DECLARE SECTION;
 
502
 
 
503
 ...
 
504
 
 
505
EXEC SQL SELECT a, b INTO :v1, :v2 FROM test;
 
506
</programlisting>
 
507
    So the <literal>INTO</literal> clause appears between the select
 
508
    list and the <literal>FROM</literal> clause.  The number of
 
509
    elements in the select list and the list after
 
510
    <literal>INTO</literal> (also called the target list) must be
 
511
    equal.
 
512
   </para>
 
513
 
 
514
   <para>
 
515
    Here is an example using the command <command>FETCH</command>:
 
516
<programlisting>
 
517
EXEC SQL BEGIN DECLARE SECTION;
 
518
int v1;
 
519
VARCHAR v2;
 
520
EXEC SQL END DECLARE SECTION;
 
521
 
 
522
 ...
 
523
 
 
524
EXEC SQL DECLARE foo CURSOR FOR SELECT a, b FROM test;
 
525
 
 
526
 ...
 
527
 
 
528
do {
 
529
    ...
 
530
    EXEC SQL FETCH NEXT FROM foo INTO :v1, :v2;
 
531
    ...
 
532
} while (...);
 
533
</programlisting>
 
534
    Here the <literal>INTO</literal> clause appears after all the
 
535
    normal clauses.
 
536
   </para>
 
537
 
 
538
   <para>
 
539
    Both of these methods only allow retrieving one row at a time.  If
 
540
    you need to process result sets that potentially contain more than
 
541
    one row, you need to use a cursor, as shown in the second example.
 
542
   </para>
 
543
  </sect2>
 
544
 
 
545
  <sect2>
 
546
   <title>Indicators</title>
 
547
 
 
548
   <para>
 
549
    The examples above do not handle null values.  In fact, the
 
550
    retrieval examples will raise an error if they fetch a null value
 
551
    from the database.  To be able to pass null values to the database
 
552
    or retrieve null values from the database, you need to append a
 
553
    second host variable specification to each host variable that
 
554
    contains data.  This second host variable is called the
 
555
    <firstterm>indicator</firstterm> and contains a flag that tells
 
556
    whether the datum is null, in which case the value of the real
 
557
    host variable is ignored.  Here is an example that handles the
 
558
    retrieval of null values correctly:
 
559
<programlisting>
 
560
EXEC SQL BEGIN DECLARE SECTION;
 
561
VARCHAR val;
 
562
int val_ind;
 
563
EXEC SQL END DECLARE SECTION:
 
564
 
 
565
 ...
 
566
 
 
567
EXEC SQL SELECT b INTO :val :val_ind FROM test1;
 
568
</programlisting>
 
569
    The indicator variable <varname>val_ind</varname> will be zero if
 
570
    the value was not null, and it will be negative if the value was
 
571
    null.
 
572
   </para>
 
573
 
 
574
   <para>
 
575
    The indicator has another function: if the indicator value is
 
576
    positive, it means that the value is not null, but it was
 
577
    truncated when it was stored in the host variable.
 
578
   </para>
 
579
  </sect2>
 
580
 </sect1>
 
581
 
 
582
 <sect1 id="ecpg-dynamic">
 
583
  <title>Dynamic SQL</title>
 
584
 
 
585
  <para>
 
586
   In many cases, the particular SQL statements that an application
 
587
   has to execute are known at the time the application is written.
 
588
   In some cases, however, the SQL statements are composed at run time
 
589
   or provided by an external source.  In these cases you cannot embed
 
590
   the SQL statements directly into the C source code, but there is a
 
591
   facility that allows you to call arbitrary SQL statements that you
 
592
   provide in a string variable.
 
593
  </para>
 
594
 
 
595
  <para>
 
596
   The simplest way to execute an arbitrary SQL statement is to use
 
597
   the command <command>EXECUTE IMMEDIATE</command>.  For example:
 
598
<programlisting>
 
599
EXEC SQL BEGIN DECLARE SECTION;
 
600
const char *stmt = "CREATE TABLE test1 (...);";
 
601
EXEC SQL END DECLARE SECTION;
 
602
 
 
603
EXEC SQL EXECUTE IMMEDIATE :stmt;
 
604
</programlisting>
 
605
   You may not execute statements that retrieve data (e.g.,
 
606
   <command>SELECT</command>) this way.
 
607
  </para>
 
608
 
 
609
  <para>
 
610
   A more powerful way to execute arbitrary SQL statements is to
 
611
   prepare them once and execute the prepared statement as often as
 
612
   you like.  It is also possible to prepare a generalized version of
 
613
   a statement and then execute specific versions of it by
 
614
   substituting parameters.  When preparing the statement, write
 
615
   question marks where you want to substitute parameters later.  For
 
616
   example:
 
617
<programlisting>
 
618
EXEC SQL BEGIN DECLARE SECTION;
 
619
const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
 
620
EXEC SQL END DECLARE SECTION;
 
621
 
 
622
EXEC SQL PREPARE mystmt FROM :stmt;
 
623
 ...
 
624
EXEC SQL EXECUTE mystmt USING 42, 'foobar';
 
625
</programlisting>
 
626
   If the statement you are executing returns values, then add an
 
627
   <literal>INTO</literal> clause:
 
628
<programlisting>
 
629
EXEC SQL BEGIN DECLARE SECTION;
 
630
const char *stmt = "SELECT a, b, c FROM test1 WHERE a &gt; ?";
 
631
int v1, v2;
 
632
VARCHAR v3;
 
633
EXEC SQL END DECLARE SECTION;
 
634
 
 
635
EXEC SQL PREPARE mystmt FROM :stmt;
 
636
 ...
 
637
EXEC SQL EXECUTE mystmt INTO v1, v2, v3 USING 37;
 
638
</programlisting>
 
639
   An <command>EXECUTE</command> command may have an
 
640
   <literal>INTO</literal> clause, a <literal>USING</literal> clause,
 
641
   both, or neither.
 
642
  </para>
 
643
 
 
644
  <para>
 
645
   When you don't need the prepared statement anymore, you should
 
646
   deallocate it:
 
647
<programlisting>
 
648
EXEC SQL DEALLOCATE PREPARE <replaceable>name</replaceable>;
 
649
</programlisting>
 
650
  </para>
 
651
 </sect1>
 
652
 
 
653
 <sect1 id="ecpg-descriptors">
 
654
  <title>Using SQL Descriptor Areas</title>
 
655
 
 
656
  <para>
 
657
   An SQL descriptor area is a more sophisticated method for
 
658
   processing the result of a <command>SELECT</command> or
 
659
   <command>FETCH</command> statement.  An SQL descriptor area groups
 
660
   the data of one row of data together with metadata items into one
 
661
   data structure.  The metadata is particularly useful when executing
 
662
   dynamic SQL statements, where the nature of the result columns may
 
663
   not be known ahead of time.
 
664
  </para>
 
665
 
 
666
  <para>
 
667
   An SQL descriptor area consists of a header, which contains
 
668
   information concerning the entire descriptor, and one or more item
 
669
   descriptor areas, which basically each describe one column in the
 
670
   result row.
 
671
  </para>
 
672
 
 
673
  <para>
 
674
   Before you can use an SQL descriptor area, you need to allocate one:
 
675
<programlisting>
 
676
EXEC SQL ALLOCATE DESCRIPTOR <replaceable>identifier</replaceable>;
 
677
</programlisting>
 
678
   The identifier serves as the <quote>variable name</quote> of the
 
679
   descriptor area.  <remark>The scope of the allocated descriptor is WHAT?.</remark>
 
680
   When you don't need the descriptor anymore, you should deallocate
 
681
   it:
 
682
<programlisting>
 
683
EXEC SQL DEALLOCATE DESCRIPTOR <replaceable>identifier</replaceable>;
 
684
</programlisting>
 
685
  </para>
 
686
 
 
687
  <para>
 
688
   To use a descriptor area, specify it as the storage target in an
 
689
   <literal>INTO</literal> clause, instead of listing host variables:
 
690
<programlisting>
 
691
EXEC SQL FETCH NEXT FROM mycursor INTO DESCRIPTOR mydesc;
 
692
</programlisting>
 
693
  </para>
 
694
 
 
695
  <para>
 
696
   Now how do you get the data out of the descriptor area?  You can
 
697
   think of the descriptor area as a structure with named fields.  To
 
698
   retrieve the value of a field from the header and store it into a
 
699
   host variable, use the following command:
 
700
<programlisting>
 
701
EXEC SQL GET DESCRIPTOR <replaceable>name</replaceable> :<replaceable>hostvar</replaceable> = <replaceable>field</replaceable>;
 
702
</programlisting>
 
703
   Currently, there is only one header field defined:
 
704
   <replaceable>COUNT</replaceable>, which tells how many item
 
705
   descriptor areas exist (that is, how many columns are contained in
 
706
   the result).  The host variable needs to be of an integer type.  To
 
707
   get a field from the item descriptor area, use the following
 
708
   command:
 
709
<programlisting>
 
710
EXEC SQL GET DESCRIPTOR <replaceable>name</replaceable> VALUE <replaceable>num</replaceable> :<replaceable>hostvar</replaceable> = <replaceable>field</replaceable>;
 
711
</programlisting>
 
712
   <replaceable>num</replaceable> can be a literal integer or a host
 
713
   variable containing an integer. Possible fields are:
 
714
 
 
715
   <variablelist>
 
716
    <varlistentry>
 
717
     <term><literal>CARDINALITY</literal> (integer)</term>
 
718
     <listitem>
 
719
      <para>
 
720
       number of rows in the result set
 
721
      </para>
 
722
     </listitem>
 
723
    </varlistentry>
 
724
 
 
725
    <varlistentry>
 
726
     <term><literal>DATA</literal></term>
 
727
     <listitem>
 
728
      <para>
 
729
       actual data item (therefore, the data type of this field
 
730
       depends on the query)
 
731
      </para>
 
732
     </listitem>
 
733
    </varlistentry>
 
734
 
 
735
    <varlistentry>
 
736
     <term><literal>DATETIME_INTERVAL_CODE</literal> (integer)</term>
 
737
     <listitem>
 
738
      <para>
 
739
       ?
 
740
      </para>
 
741
     </listitem>
 
742
    </varlistentry>
 
743
 
 
744
    <varlistentry>
 
745
     <term><literal>DATETIME_INTERVAL_PRECISION</literal> (integer)</term>
 
746
     <listitem>
 
747
      <para>
 
748
       not implemented
 
749
      </para>
 
750
     </listitem>
 
751
    </varlistentry>
 
752
 
 
753
    <varlistentry>
 
754
     <term><literal>INDICATOR</literal> (integer)</term>
 
755
     <listitem>
 
756
      <para>
 
757
       the indicator (indicating a null value or a value truncation)
 
758
      </para>
 
759
     </listitem>
 
760
    </varlistentry>
 
761
 
 
762
    <varlistentry>
 
763
     <term><literal>KEY_MEMBER</literal> (integer)</term>
 
764
     <listitem>
 
765
      <para>
 
766
       not implemented
 
767
      </para>
 
768
     </listitem>
 
769
    </varlistentry>
 
770
 
 
771
    <varlistentry>
 
772
     <term><literal>LENGTH</literal> (integer)</term>
 
773
     <listitem>
 
774
      <para>
 
775
       length of the datum in characters
 
776
      </para>
 
777
     </listitem>
 
778
    </varlistentry>
 
779
 
 
780
    <varlistentry>
 
781
     <term><literal>NAME</literal> (string)</term>
 
782
     <listitem>
 
783
      <para>
 
784
       name of the column
 
785
      </para>
 
786
     </listitem>
 
787
    </varlistentry>
 
788
 
 
789
    <varlistentry>
 
790
     <term><literal>NULLABLE</literal> (integer)</term>
 
791
     <listitem>
 
792
      <para>
 
793
       not implemented
 
794
      </para>
 
795
     </listitem>
 
796
    </varlistentry>
 
797
 
 
798
    <varlistentry>
 
799
     <term><literal>OCTET_LENGTH</literal> (integer)</term>
 
800
     <listitem>
 
801
      <para>
 
802
       length of the character representation of the datum in bytes
 
803
      </para>
 
804
     </listitem>
 
805
    </varlistentry>
 
806
 
 
807
    <varlistentry>
 
808
     <term><literal>PRECISION</literal> (integer)</term>
 
809
     <listitem>
 
810
      <para>
 
811
       precision (for type <type>numeric</type>)
 
812
      </para>
 
813
     </listitem>
 
814
    </varlistentry>
 
815
 
 
816
    <varlistentry>
 
817
     <term><literal>RETURNED_LENGTH</literal> (integer)</term>
 
818
     <listitem>
 
819
      <para>
 
820
       length of the datum in characters
 
821
      </para>
 
822
     </listitem>
 
823
    </varlistentry>
 
824
 
 
825
    <varlistentry>
 
826
     <term><literal>RETURNED_OCTET_LENGTH</literal> (integer)</term>
 
827
     <listitem>
 
828
      <para>
 
829
       length of the character representation of the datum in bytes
 
830
      </para>
 
831
     </listitem>
 
832
    </varlistentry>
 
833
 
 
834
    <varlistentry>
 
835
     <term><literal>SCALE</literal> (integer)</term>
 
836
     <listitem>
 
837
      <para>
 
838
       scale (for type <type>numeric</type>)
 
839
      </para>
 
840
     </listitem>
 
841
    </varlistentry>
 
842
 
 
843
    <varlistentry>
 
844
     <term><literal>TYPE</literal> (integer)</term>
 
845
     <listitem>
 
846
      <para>
 
847
       numeric code of the data type of the column
 
848
      </para>
 
849
     </listitem>
 
850
    </varlistentry>
 
851
   </variablelist>
 
852
  </para>
 
853
 </sect1>
 
854
 
 
855
 <sect1 id="ecpg-errors">
 
856
  <title>Error Handling</title>
 
857
 
 
858
  <para>
 
859
   This section describes how you can handle exceptional conditions
 
860
   and warnings in an embedded SQL program.  There are several
 
861
   nonexclusive facilities for this.
 
862
  </para>
 
863
 
 
864
  <sect2>
 
865
   <title>Setting Callbacks</title>
 
866
 
 
867
   <para>
 
868
    One simple method to catch errors and warnings is to set a
 
869
    specific action to be executed whenever a particular condition
 
870
    occurs.  In general:
 
871
<programlisting>
 
872
EXEC SQL WHENEVER <replaceable>condition</replaceable> <replaceable>action</replaceable>;
 
873
</programlisting>
 
874
   </para>
 
875
 
 
876
   <para>
 
877
    <replaceable>condition</replaceable> can be one of the following:
 
878
 
 
879
    <variablelist>
 
880
     <varlistentry>
 
881
      <term><literal>SQLERROR</literal></term>
 
882
      <listitem>
 
883
       <para>
 
884
        The specified action is called whenever an error occurs during
 
885
        the execution of an SQL statement.
 
886
       </para>
 
887
      </listitem>
 
888
     </varlistentry>
 
889
 
 
890
     <varlistentry>
 
891
      <term><literal>SQLWARNING</literal></term>
 
892
      <listitem>
 
893
       <para>
 
894
        The specified action is called whenever a warning occurs
 
895
        during the execution of an SQL statement.
 
896
       </para>
 
897
      </listitem>
 
898
     </varlistentry>
 
899
 
 
900
     <varlistentry>
 
901
      <term><literal>NOT FOUND</literal></term>
 
902
      <listitem>
 
903
       <para>
 
904
        The specified action is called whenever an SQL statement
 
905
        retrieves or affects zero rows.  (This condition is not an
 
906
        error, but you might be interested in handling it specially.)
 
907
       </para>
 
908
      </listitem>
 
909
     </varlistentry>
 
910
    </variablelist>
 
911
   </para>
 
912
 
 
913
   <para>
 
914
    <replaceable>action</replaceable> can be one of the following:
 
915
 
 
916
    <variablelist>
 
917
     <varlistentry>
 
918
      <term><literal>CONTINUE</literal></term>
 
919
      <listitem>
 
920
       <para>
 
921
        This effectively means that the condition is ignored.  This is
 
922
        the default.
 
923
       </para>
 
924
      </listitem>
 
925
     </varlistentry>
 
926
 
 
927
     <varlistentry>
 
928
      <term><literal>GOTO <replaceable>label</replaceable></literal></term>
 
929
      <term><literal>GO TO <replaceable>label</replaceable></literal></term>
 
930
      <listitem>
 
931
       <para>
 
932
        Jump to the specified label (using a C <literal>goto</literal>
 
933
        statement).
 
934
       </para>
 
935
      </listitem>
 
936
     </varlistentry>
 
937
 
 
938
     <varlistentry>
 
939
      <term><literal>SQLPRINT</literal></term>
 
940
      <listitem>
 
941
       <para>
 
942
        Print a message to standard error.  This is useful for simple
 
943
        programs or during prototyping.  The details of the message
 
944
        cannot be configured.
 
945
       </para>
 
946
      </listitem>
 
947
     </varlistentry>
 
948
 
 
949
     <varlistentry>
 
950
      <term><literal>STOP</literal></term>
 
951
      <listitem>
 
952
       <para>
 
953
        Call <literal>exit(1)</literal>, which will terminate the
 
954
        program.
 
955
       </para>
 
956
      </listitem>
 
957
     </varlistentry>
 
958
 
 
959
     <varlistentry>
 
960
      <term><literal>BREAK</literal></term>
 
961
      <listitem>
 
962
       <para>
 
963
        Execute the C statement <literal>break</literal>.  This should
 
964
        only be used in loops or <literal>switch</literal> statements.
 
965
       </para>
 
966
      </listitem>
 
967
     </varlistentry>
 
968
 
 
969
     <varlistentry>
 
970
      <term><literal>CALL <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term>
 
971
      <term><literal>DO <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term>
 
972
      <listitem>
 
973
       <para>
 
974
        Call the specified C functions with the specified arguments.
 
975
       </para>
 
976
      </listitem>
 
977
     </varlistentry>
 
978
    </variablelist>
 
979
 
 
980
    The SQL standard only provides for the actions
 
981
    <literal>CONTINUE</literal> and <literal>GOTO</literal> (and
 
982
    <literal>GO TO</literal>).
 
983
   </para>
 
984
 
 
985
   <para>
 
986
    Here is an example that you might want to use in a simple program.
 
987
    It prints a simple message when a warning occurs and aborts the
 
988
    program when an error happens.
 
989
<programlisting>
 
990
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
 
991
EXEC SQL WHENEVER SQLERROR STOP;
 
992
</programlisting>
 
993
   </para>
 
994
 
 
995
   <para>
 
996
    The statement <literal>EXEC SQL WHENEVER</literal> is a directive
 
997
    of the SQL preprocessor, not a C statement.  The error or warning
 
998
    actions that it sets apply to all embedded SQL statements that
 
999
    appear below the point where the handler is set, unless a
 
1000
    different action was set for the same condition between the first
 
1001
    <literal>EXEC SQL WHENEVER</literal> and the SQL statement causing
 
1002
    the condition, regardless of the flow of control in the C program.
 
1003
    So neither of the two following C program excerpts will have the
 
1004
    desired effect.
 
1005
<programlisting>
 
1006
/*
 
1007
 * WRONG
 
1008
 */
 
1009
int main(int argc, char *argv[])
 
1010
{
 
1011
    ...
 
1012
    if (verbose) {
 
1013
        EXEC SQL WHENEVER SQLWARNING SQLPRINT;
 
1014
    }
 
1015
    ...
 
1016
    EXEC SQL SELECT ...;
 
1017
    ...
 
1018
}
 
1019
</programlisting>
 
1020
 
 
1021
<programlisting>
 
1022
/*
 
1023
 * WRONG
 
1024
 */
 
1025
int main(int argc, char *argv[])
 
1026
{
 
1027
    ...
 
1028
    set_error_handler();
 
1029
    ...
 
1030
    EXEC SQL SELECT ...;
 
1031
    ...
 
1032
}
 
1033
 
 
1034
static void set_error_handler(void)
 
1035
{
 
1036
    EXEC SQL WHENEVER SQLERROR STOP;
 
1037
}
 
1038
</programlisting>
 
1039
   </para>
 
1040
  </sect2>
 
1041
 
 
1042
  <sect2>
 
1043
   <title>sqlca</title>
 
1044
 
 
1045
   <para>
 
1046
    For more powerful error handling, the embedded SQL interface
 
1047
    provides a global variable with the name <varname>sqlca</varname>
 
1048
    that has the following structure:
 
1049
<programlisting>
 
1050
struct
 
1051
{
 
1052
    char sqlcaid[8];
 
1053
    long sqlabc;
 
1054
    long sqlcode;
 
1055
    struct
 
1056
    {
 
1057
        int sqlerrml;
 
1058
        char sqlerrmc[70];
 
1059
    } sqlerrm;
 
1060
    char sqlerrp[8];
 
1061
    long sqlerrd[6];
 
1062
    char sqlwarn[8];
 
1063
    char sqlstate[5];
 
1064
} sqlca;
 
1065
</programlisting>
 
1066
    (In a multithreaded program, every thread automatically gets its
 
1067
    own copy of <varname>sqlca</varname>.  This works similarly to the
 
1068
    handling of the standard C global variable
 
1069
    <varname>errno</varname>.)
 
1070
   </para>
 
1071
 
 
1072
   <para>
 
1073
    <varname>sqlca</varname> covers both warnings and errors.  If
 
1074
    multiple warnings or errors occur during the execution of a
 
1075
    statement, then <varname>sqlca</varname> will only contain
 
1076
    information about the last one.
 
1077
   </para>
 
1078
 
 
1079
   <para>
 
1080
    If no error occurred in the last <acronym>SQL</acronym> statement,
 
1081
    <literal>sqlca.sqlcode</literal> will be 0 and
 
1082
    <literal>sqlca.sqlstate</literal> will be
 
1083
    <literal>"00000"</literal>.  If a warning or error occurred, then
 
1084
    <literal>sqlca.sqlcode</literal> will be negative and
 
1085
    <literal>sqlca.sqlstate</literal> will be different from
 
1086
    <literal>"00000"</literal>.  A positive
 
1087
    <literal>sqlca.sqlcode</literal> indicates a harmless condition,
 
1088
    such as that the last query returned zero rows.
 
1089
    <literal>sqlcode</literal> and <literal>sqlstate</literal> are two
 
1090
    different error code schemes; details appear below.
 
1091
   </para>
 
1092
 
 
1093
   <para>
 
1094
    If the last SQL statement was successful, then
 
1095
    <literal>sqlca.sqlerrd[1]</literal> contains the OID of the
 
1096
    processed row, if applicable, and
 
1097
    <literal>sqlca.sqlerrd[2]</literal> contains the number of
 
1098
    processed or returned rows, if applicable to the command.
 
1099
   </para>
 
1100
 
 
1101
   <para>
 
1102
    In case of an error or warning,
 
1103
    <literal>sqlca.sqlerrm.sqlerrmc</literal> will contain a string
 
1104
    that describes the error.  The field
 
1105
    <literal>sqlca.sqlerrm.sqlerrml</literal> contains the length of
 
1106
    the error message that is stored in
 
1107
    <literal>sqlca.sqlerrm.sqlerrmc</literal> (the result of
 
1108
    <function>strlen()</function>, not really interesting for a C
 
1109
    programmer).  Note that some messages are too long to fit in the
 
1110
    fixed-size <literal>sqlerrmc</literal> array; they will be truncated.
 
1111
   </para>
 
1112
 
 
1113
   <para>
 
1114
    In case of a warning, <literal>sqlca.sqlwarn[2]</literal> is set
 
1115
    to <literal>W</literal>.  (In all other cases, it is set to
 
1116
    something different from <literal>W</literal>.)  If
 
1117
    <literal>sqlca.sqlwarn[1]</literal> is set to
 
1118
    <literal>W</literal>, then a value was truncated when it was
 
1119
    stored in a host variable.  <literal>sqlca.sqlwarn[0]</literal> is
 
1120
    set to <literal>W</literal> if any of the other elements are set
 
1121
    to indicate a warning.
 
1122
   </para>
 
1123
 
 
1124
   <para>
 
1125
    The fields <structfield>sqlcaid</structfield>,
 
1126
    <structfield>sqlcabc</structfield>,
 
1127
    <structfield>sqlerrp</structfield>, and the remaining elements of
 
1128
    <structfield>sqlerrd</structfield> and
 
1129
    <structfield>sqlwarn</structfield> currently contain no useful
 
1130
    information.
 
1131
   </para>
 
1132
 
 
1133
   <para>
 
1134
    The structure <varname>sqlca</varname> is not defined in the SQL
 
1135
    standard, but is implemented in several other SQL database
 
1136
    systems.  The definitions are similar at the core, but if you want
 
1137
    to write portable applications, then you should investigate the
 
1138
    different implementations carefully.
 
1139
   </para>
 
1140
  </sect2>
 
1141
 
 
1142
  <sect2>
 
1143
   <title><literal>SQLSTATE</literal> vs <literal>SQLCODE</literal></title>
 
1144
 
 
1145
   <para>
 
1146
    The fields <literal>sqlca.sqlstate</literal> and
 
1147
    <literal>sqlca.sqlcode</literal> are two different schemes that
 
1148
    provide error codes.  Both are specified in the SQL standard, but
 
1149
    <literal>SQLCODE</literal> has been marked deprecated in the 1992
 
1150
    edition of the standard and has been dropped in the 1999 edition.
 
1151
    Therefore, new applications are strongly encouraged to use
 
1152
    <literal>SQLSTATE</literal>.
 
1153
   </para>
 
1154
 
 
1155
   <para>
 
1156
    <literal>SQLSTATE</literal> is a five-character array.  The five
 
1157
    characters contain digits or upper-case letters that represent
 
1158
    codes of various error and warning conditions.
 
1159
    <literal>SQLSTATE</literal> has a hierarchical scheme: the first
 
1160
    two characters indicate the general class of the condition, the
 
1161
    last three characters indicate a subclass of the general
 
1162
    condition.  A successful state is indicated by the code
 
1163
    <literal>00000</literal>.  The <literal>SQLSTATE</literal> codes are for
 
1164
    the most part defined in the SQL standard.  The
 
1165
    <productname>PostgreSQL</productname> server natively supports
 
1166
    <literal>SQLSTATE</literal> error codes; therefore a high degree
 
1167
    of consistency can be achieved by using this error code scheme
 
1168
    throughout all applications.  For further information see
 
1169
    <xref linkend="errcodes-appendix">.
 
1170
   </para>
 
1171
 
 
1172
   <para>
 
1173
    <literal>SQLCODE</literal>, the deprecated error code scheme, is a
 
1174
    simple integer.  A value of 0 indicates success, a positive value
 
1175
    indicates success with additional information, a negative value
 
1176
    indicates an error.  The SQL standard only defines the positive
 
1177
    value +100, which indicates that the last command returned or
 
1178
    affected zero rows, and no specific negative values.  Therefore,
 
1179
    this scheme can only achieve poor portability and does not have a
 
1180
    hierarchical code assignment.  Historically, the embedded SQL
 
1181
    processor for <productname>PostgreSQL</productname> has assigned
 
1182
    some specific <literal>SQLCODE</literal> values for its use, which
 
1183
    are listed below with their numeric value and their symbolic name.
 
1184
    Remember that these are not portable to other SQL implementations.
 
1185
    To simplify the porting of applications to the
 
1186
    <literal>SQLSTATE</literal> scheme, the corresponding
 
1187
    <literal>SQLSTATE</literal> is also listed.  There is, however, no
 
1188
    one-to-one or one-to-many mapping between the two schemes (indeed
 
1189
    it is many-to-many), so you should consult the global
 
1190
    <literal>SQLSTATE</literal> listing in <xref linkend="errcodes-appendix">
 
1191
    in each case.
 
1192
   </para>
 
1193
 
 
1194
   <para>
 
1195
    These are the assigned <literal>SQLCODE</literal> values:
 
1196
 
 
1197
    <variablelist>
 
1198
     <varlistentry>
 
1199
      <term>-12 (<symbol>ECPG_OUT_OF_MEMORY</symbol>)</term>
 
1200
      <listitem>
 
1201
       <para>
 
1202
        Indicates that your virtual memory is exhausted. (SQLSTATE
 
1203
        YE001)
 
1204
      </para>
 
1205
     </listitem>
 
1206
    </varlistentry>
 
1207
 
 
1208
    <varlistentry>
 
1209
     <term>-200 (<symbol>ECPG_UNSUPPORTED</symbol>)</term>
 
1210
     <listitem>
 
1211
      <para>
 
1212
       Indicates the preprocessor has generated something that the
 
1213
       library does not know about.  Perhaps you are running
 
1214
       incompatible versions of the preprocessor and the
 
1215
       library. (SQLSTATE YE002)
 
1216
      </para>
 
1217
     </listitem>
 
1218
    </varlistentry>
 
1219
 
 
1220
    <varlistentry>
 
1221
     <term>-201 (<symbol>ECPG_TOO_MANY_ARGUMENTS</symbol>)</term>
 
1222
     <listitem>
 
1223
      <para>
 
1224
       This means that the command specified more host variables than
 
1225
       the command expected.  (SQLSTATE 07001 or 07002)
 
1226
      </para>
 
1227
     </listitem>
 
1228
    </varlistentry>
 
1229
 
 
1230
    <varlistentry>
 
1231
     <term>-202 (<symbol>ECPG_TOO_FEW_ARGUMENTS</symbol>)</term>
 
1232
     <listitem>
 
1233
      <para>
 
1234
       This means that the command specified fewer host variables than
 
1235
       the command expected.  (SQLSTATE 07001 or 07002)
 
1236
      </para>
 
1237
     </listitem>
 
1238
    </varlistentry>
 
1239
 
 
1240
    <varlistentry>
 
1241
     <term>-203 (<symbol>ECPG_TOO_MANY_MATCHES</symbol>)</term>
 
1242
     <listitem>
 
1243
      <para>
 
1244
       This means a query has returned multiple rows but the statement
 
1245
       was only prepared to store one result row (for example, because
 
1246
       the specified variables are not arrays).  (SQLSTATE 21000)
 
1247
      </para>
 
1248
     </listitem>
 
1249
    </varlistentry>
 
1250
 
 
1251
    <varlistentry>
 
1252
     <term>-204 (<symbol>ECPG_INT_FORMAT</symbol>)</term>
 
1253
     <listitem>
 
1254
      <para>
 
1255
       The host variable is of type <type>int</type> and the datum in
 
1256
       the database is of a different type and contains a value that
 
1257
       cannot be interpreted as an <type>int</type>.  The library uses
 
1258
       <function>strtol()</function> for this conversion.  (SQLSTATE
 
1259
       42804)
 
1260
      </para>
 
1261
     </listitem>
 
1262
    </varlistentry>
 
1263
 
 
1264
    <varlistentry>
 
1265
     <term>-205 (<symbol>ECPG_UINT_FORMAT</symbol>)</term>
 
1266
     <listitem>
 
1267
      <para>
 
1268
       The host variable is of type <type>unsigned int</type> and the
 
1269
       datum in the database is of a different type and contains a
 
1270
       value that cannot be interpreted as an <type>unsigned
 
1271
       int</type>.  The library uses <function>strtoul()</function>
 
1272
       for this conversion.  (SQLSTATE 42804)
 
1273
      </para>
 
1274
     </listitem>
 
1275
    </varlistentry>
 
1276
 
 
1277
    <varlistentry>
 
1278
     <term>-206 (<symbol>ECPG_FLOAT_FORMAT</symbol>)</term>
 
1279
     <listitem>
 
1280
      <para>
 
1281
       The host variable is of type <type>float</type> and the datum
 
1282
       in the database is of another type and contains a value that
 
1283
       cannot be interpreted as a <type>float</type>.  The library
 
1284
       uses <function>strtod()</function> for this conversion.
 
1285
       (SQLSTATE 42804)
 
1286
      </para>
 
1287
     </listitem>
 
1288
    </varlistentry>
 
1289
 
 
1290
    <varlistentry>
 
1291
     <term>-207 (<symbol>ECPG_CONVERT_BOOL</symbol>)</term>
 
1292
     <listitem>
 
1293
      <para>
 
1294
       This means the host variable is of type <type>bool</type> and
 
1295
       the datum in the database is neither <literal>'t'</> nor
 
1296
       <literal>'f'</>.  (SQLSTATE 42804)
 
1297
      </para>
 
1298
     </listitem>
 
1299
    </varlistentry>
 
1300
 
 
1301
    <varlistentry>
 
1302
     <term>-208 (<symbol>ECPG_EMPTY</symbol>)</term>
 
1303
     <listitem>
 
1304
      <para>
 
1305
       The statement sent to the <productname>PostgreSQL</productname>
 
1306
       server was empty.  (This cannot normally happen in an embedded
 
1307
       SQL program, so it may point to an internal error.)  (SQLSTATE
 
1308
       YE002)
 
1309
      </para>
 
1310
     </listitem>
 
1311
    </varlistentry>
 
1312
 
 
1313
    <varlistentry>
 
1314
     <term>-209 (<symbol>ECPG_MISSING_INDICATOR</symbol>)</term>
 
1315
     <listitem>
 
1316
      <para>
 
1317
       A null value was returned and no null indicator variable was
 
1318
       supplied.  (SQLSTATE 22002)
 
1319
      </para>
 
1320
     </listitem>
 
1321
    </varlistentry>
 
1322
 
 
1323
    <varlistentry>
 
1324
     <term>-210 (<symbol>ECPG_NO_ARRAY</symbol>)</term>
 
1325
     <listitem>
 
1326
      <para>
 
1327
       An ordinary variable was used in a place that requires an
 
1328
       array.  (SQLSTATE 42804)
 
1329
      </para>
 
1330
     </listitem>
 
1331
    </varlistentry>
 
1332
 
 
1333
    <varlistentry>
 
1334
     <term>-211 (<symbol>ECPG_DATA_NOT_ARRAY</symbol>)</term>
 
1335
     <listitem>
 
1336
      <para>
 
1337
       The database returned an ordinary variable in a place that
 
1338
       requires array value.  (SQLSTATE 42804)
 
1339
      </para>
 
1340
     </listitem>
 
1341
    </varlistentry>
 
1342
 
 
1343
    <varlistentry>
 
1344
     <term>-220 (<symbol>ECPG_NO_CONN</symbol>)</term>
 
1345
     <listitem>
 
1346
      <para>
 
1347
       The program tried to access a connection that does not exist.
 
1348
       (SQLSTATE 08003)
 
1349
      </para>
 
1350
     </listitem>
 
1351
    </varlistentry>
 
1352
 
 
1353
    <varlistentry>
 
1354
     <term>-221 (<symbol>ECPG_NOT_CONN</symbol>)</term>
 
1355
     <listitem>
 
1356
      <para>
 
1357
       The program tried to access a connection that does exist but is
 
1358
       not open.  (This is an internal error.)  (SQLSTATE YE002)
 
1359
      </para>
 
1360
     </listitem>
 
1361
    </varlistentry>
 
1362
 
 
1363
    <varlistentry>
 
1364
     <term>-230 (<symbol>ECPG_INVALID_STMT</symbol>)</term>
 
1365
     <listitem>
 
1366
      <para>
 
1367
       The statement you are trying to use has not been prepared.
 
1368
       (SQLSTATE 26000)
 
1369
      </para>
 
1370
     </listitem>
 
1371
    </varlistentry>
 
1372
 
 
1373
    <varlistentry>
 
1374
     <term>-240 (<symbol>ECPG_UNKNOWN_DESCRIPTOR</symbol>)</term>
 
1375
     <listitem>
 
1376
      <para>
 
1377
       The descriptor specified was not found.  The statement you are
 
1378
       trying to use has not been prepared.  (SQLSTATE 33000)
 
1379
      </para>
 
1380
     </listitem>
 
1381
    </varlistentry>
 
1382
 
 
1383
    <varlistentry>
 
1384
     <term>-241 (<symbol>ECPG_INVALID_DESCRIPTOR_INDEX</symbol>)</term>
 
1385
     <listitem>
 
1386
      <para>
 
1387
       The descriptor index specified was out of range.  (SQLSTATE
 
1388
       07009)
 
1389
      </para>
 
1390
     </listitem>
 
1391
    </varlistentry>
 
1392
 
 
1393
    <varlistentry>
 
1394
     <term>-242 (<symbol>ECPG_UNKNOWN_DESCRIPTOR_ITEM</symbol>)</term>
 
1395
     <listitem>
 
1396
      <para>
 
1397
       An invalid descriptor item was requested.  (This is an internal
 
1398
       error.)  (SQLSTATE YE002)
 
1399
      </para>
 
1400
     </listitem>
 
1401
    </varlistentry>
 
1402
 
 
1403
    <varlistentry>
 
1404
     <term>-243 (<symbol>ECPG_VAR_NOT_NUMERIC</symbol>)</term>
 
1405
     <listitem>
 
1406
      <para>
 
1407
       During the execution of a dynamic statement, the database
 
1408
       returned a numeric value and the host variable was not numeric.
 
1409
       (SQLSTATE 07006)
 
1410
      </para>
 
1411
     </listitem>
 
1412
    </varlistentry>
 
1413
 
 
1414
    <varlistentry>
 
1415
     <term>-244 (<symbol>ECPG_VAR_NOT_CHAR</symbol>)</term>
 
1416
     <listitem>
 
1417
      <para>
 
1418
       During the execution of a dynamic statement, the database
 
1419
       returned a non-numeric value and the host variable was numeric.
 
1420
       (SQLSTATE 07006)
 
1421
      </para>
 
1422
     </listitem>
 
1423
    </varlistentry>
 
1424
 
 
1425
    <varlistentry>
 
1426
     <term>-400 (<symbol>ECPG_PGSQL</symbol>)</term>
 
1427
     <listitem>
 
1428
      <para>
 
1429
       Some error caused by the <productname>PostgreSQL</productname>
 
1430
       server.  The message contains the error message from the
 
1431
       <productname>PostgreSQL</productname> server.
 
1432
      </para>
 
1433
     </listitem>
 
1434
    </varlistentry>
 
1435
 
 
1436
    <varlistentry>
 
1437
     <term>-401 (<symbol>ECPG_TRANS</symbol>)</term>
 
1438
     <listitem>
 
1439
      <para>
 
1440
       The <productname>PostgreSQL</productname> server signaled that
 
1441
       we cannot start, commit, or rollback the transaction.
 
1442
       (SQLSTATE 08007)
 
1443
      </para>
 
1444
     </listitem>
 
1445
    </varlistentry>
 
1446
 
 
1447
    <varlistentry>
 
1448
     <term>-402 (<symbol>ECPG_CONNECT</symbol>)</term>
 
1449
     <listitem>
 
1450
      <para>
 
1451
       The connection attempt to the database did not succeed.
 
1452
       (SQLSTATE 08001)
 
1453
      </para>
 
1454
     </listitem>
 
1455
    </varlistentry>
 
1456
 
 
1457
    <varlistentry>
 
1458
     <term>100 (<symbol>ECPG_NOT_FOUND</symbol>)</term>
 
1459
     <listitem>
 
1460
      <para>
 
1461
       This is a harmless condition indicating that the last command
 
1462
       retrieved or processed zero rows, or that you are at the end of
 
1463
       the cursor.  (SQLSTATE 02000)
 
1464
      </para>
 
1465
     </listitem>
 
1466
    </varlistentry>
 
1467
   </variablelist>
 
1468
  </para>
 
1469
  </sect2>
 
1470
 </sect1>
 
1471
 
 
1472
 <sect1 id="ecpg-include">
 
1473
  <title>Including Files</title>
 
1474
 
 
1475
  <para>
 
1476
   To include an external file into your embedded SQL program, use:
 
1477
<programlisting>
 
1478
EXEC SQL INCLUDE <replaceable>filename</replaceable>;
 
1479
</programlisting>
 
1480
   The embedded SQL preprocessor will look for a file named
 
1481
   <literal><replaceable>filename</replaceable>.h</literal>,
 
1482
   preprocess it, and include it in the resulting C output.  Thus,
 
1483
   embedded SQL statements in the included file are handled correctly.
 
1484
  </para>
 
1485
 
 
1486
  <para>
 
1487
   Note that this is <emphasis>not</emphasis> the same as
 
1488
<programlisting>
 
1489
#include &lt;<replaceable>filename</replaceable>.h&gt;
 
1490
</programlisting>
 
1491
   because this file would not be subject to SQL command preprocessing.
 
1492
   Naturally, you can continue to use the C
 
1493
   <literal>#include</literal> directive to include other header
 
1494
   files.
 
1495
  </para>
 
1496
 
 
1497
  <note>
 
1498
   <para>
 
1499
    The include file name is case-sensitive, even though the rest of
 
1500
    the <literal>EXEC SQL INCLUDE</literal> command follows the normal
 
1501
    SQL case-sensitivity rules.
 
1502
   </para>
 
1503
  </note>
 
1504
 </sect1>
 
1505
 
 
1506
 <sect1 id="ecpg-process">
 
1507
  <title>Processing Embedded SQL Programs</title>
 
1508
 
 
1509
  <para>
 
1510
   Now that you have an idea how to form embedded SQL C programs, you
 
1511
   probably want to know how to compile them.  Before compiling you
 
1512
   run the file through the embedded <acronym>SQL</acronym>
 
1513
   <acronym>C</acronym> preprocessor, which converts the
 
1514
   <acronym>SQL</acronym> statements you used to special function
 
1515
   calls.  After compiling, you must link with a special library that
 
1516
   contains the needed functions. These functions fetch information
 
1517
   from the arguments, perform the <acronym>SQL</acronym> command using
 
1518
   the <application>libpq</application> interface, and put the result
 
1519
   in the arguments specified for output.
 
1520
  </para>
 
1521
 
 
1522
  <para>
 
1523
   The preprocessor program is called <filename>ecpg</filename> and is
 
1524
   included in a normal <productname>PostgreSQL</> installation.
 
1525
   Embedded SQL programs are typically named with an extension
 
1526
   <filename>.pgc</filename>.  If you have a program file called
 
1527
   <filename>prog1.pgc</filename>, you can preprocess it by simply
 
1528
   calling
 
1529
<programlisting>
 
1530
ecpg prog1.pgc
 
1531
</programlisting>
 
1532
   This will create a file called <filename>prog1.c</filename>.  If
 
1533
   your input files do not follow the suggested naming pattern, you
 
1534
   can specify the output file explicitly using the
 
1535
   <option>-o</option> option.
 
1536
  </para>
 
1537
 
 
1538
  <para>
 
1539
   The preprocessed file can be compiled normally, for example:
 
1540
<programlisting>
 
1541
cc -c prog1.c
 
1542
</programlisting>
 
1543
   The generated C source files include header files from the
 
1544
   <productname>PostgreSQL</> installation, so if you installed
 
1545
   <productname>PostgreSQL</> in a location that is not searched by
 
1546
   default, you have to add an option such as
 
1547
   <literal>-I/usr/local/pgsql/include</literal> to the compilation
 
1548
   command line.
 
1549
  </para>
 
1550
 
 
1551
  <para>
 
1552
   To link an embedded SQL program, you need to include the
 
1553
   <filename>libecpg</filename> library, like so:
 
1554
<programlisting>
 
1555
cc -o myprog prog1.o prog2.o ... -lecpg
 
1556
</programlisting>
 
1557
   Again, you might have to add an option like
 
1558
   <literal>-L/usr/local/pgsql/lib</literal> to that command line.
 
1559
  </para>
 
1560
 
 
1561
  <para>
 
1562
   If you manage the build process of a larger project using
 
1563
   <application>make</application>, it may be convenient to include
 
1564
   the following implicit rule to your makefiles:
 
1565
<programlisting>
 
1566
ECPG = ecpg
 
1567
 
 
1568
%.c: %.pgc
 
1569
        $(ECPG) $<
 
1570
</programlisting>
 
1571
  </para>
 
1572
 
 
1573
  <para>
 
1574
   The complete syntax of the <command>ecpg</command> command is
 
1575
   detailed in <xref linkend="app-ecpg">.
 
1576
  </para>
 
1577
 
 
1578
  <para>
 
1579
   The <application>ecpg</application> library is thread-safe if it is built
 
1580
   using the <option>--enable-thread-safety</> command-line option to
 
1581
   <filename>configure</filename>.  (You might need to use other threading
 
1582
   command-line options to compile your client code.)
 
1583
  </para>
 
1584
 </sect1>
 
1585
 
 
1586
 <sect1 id="ecpg-library">
 
1587
  <title>Library Functions</title>
 
1588
 
 
1589
  <para>
 
1590
   The <filename>libecpg</filename> library primarily contains
 
1591
   <quote>hidden</quote> functions that are used to implement the
 
1592
   functionality expressed by the embedded SQL commands.  But there
 
1593
   are some functions that can usefully be called directly.  Note that
 
1594
   this makes your code unportable.
 
1595
  </para>
 
1596
 
 
1597
  <itemizedlist>
 
1598
   <listitem>
 
1599
    <para>
 
1600
     <function>ECPGdebug(int <replaceable>on</replaceable>, FILE
 
1601
     *<replaceable>stream</replaceable>)</function> turns on debug
 
1602
     logging if called with the first argument non-zero. Debug logging
 
1603
     is done on <replaceable>stream</replaceable>.  The log contains
 
1604
     all <acronym>SQL</acronym> statements with all the input
 
1605
     variables inserted, and the results from the
 
1606
     <productname>PostgreSQL</productname> server. This can be very
 
1607
     useful when searching for errors in your <acronym>SQL</acronym>
 
1608
     statements.
 
1609
    </para>
 
1610
   </listitem>
 
1611
 
 
1612
   <listitem>
 
1613
    <para>
 
1614
     <function>ECPGstatus(int <replaceable>lineno</replaceable>,
 
1615
     const char* <replaceable>connection_name</replaceable>)</function>
 
1616
     returns true if you are connected to a database and false if not.
 
1617
     <replaceable>connection_name</replaceable> can be <literal>NULL</> 
 
1618
     if a single connection is being used.
 
1619
    </para>
 
1620
   </listitem>
 
1621
  </itemizedlist>
 
1622
 </sect1>
 
1623
 
 
1624
 <sect1 id="ecpg-develop">
 
1625
  <title>Internals</title>
 
1626
 
 
1627
  <para>
 
1628
   This section explains how <application>ECPG</application> works
 
1629
   internally. This information can occasionally be useful to help
 
1630
   users understand how to use <application>ECPG</application>.
 
1631
  </para>
 
1632
 
 
1633
   <para>
 
1634
    The first four lines written by <command>ecpg</command> to the
 
1635
    output are fixed lines.  Two are comments and two are include
 
1636
    lines necessary to interface to the library.  Then the
 
1637
    preprocessor reads through the file and writes output.  Normally
 
1638
    it just echoes everything to the output.
 
1639
   </para>
 
1640
 
 
1641
   <para>
 
1642
    When it sees an <command>EXEC SQL</command> statement, it
 
1643
    intervenes and changes it. The command starts with <command>EXEC
 
1644
    SQL</command> and ends with <command>;</command>. Everything in
 
1645
    between is treated as an <acronym>SQL</acronym> statement and
 
1646
    parsed for variable substitution.
 
1647
   </para>
 
1648
 
 
1649
   <para>
 
1650
    Variable substitution occurs when a symbol starts with a colon
 
1651
    (<literal>:</literal>). The variable with that name is looked up
 
1652
    among the variables that were previously declared within a
 
1653
    <literal>EXEC SQL DECLARE</> section.
 
1654
   </para>
 
1655
 
 
1656
   <para>
 
1657
    The most important function in the library is
 
1658
    <function>ECPGdo</function>, which takes care of executing most
 
1659
    commands. It takes a variable number of arguments. This can easily
 
1660
    add up to 50 or so arguments, and we hope this will not be a
 
1661
    problem on any platform.
 
1662
   </para>
 
1663
 
 
1664
   <para>
 
1665
    The arguments are:
 
1666
 
 
1667
    <variablelist>
 
1668
     <varlistentry>
 
1669
      <term>A line number</term>
 
1670
      <listitem>
 
1671
       <para>
 
1672
        This is the line number of the original line; used in error
 
1673
        messages only.
 
1674
       </para>
 
1675
      </listitem>
 
1676
     </varlistentry>
 
1677
 
 
1678
     <varlistentry>
 
1679
      <term>A string</term>
 
1680
      <listitem>
 
1681
       <para>
 
1682
        This is the <acronym>SQL</acronym> command that is to be issued.
 
1683
        It is modified by the input variables, i.e., the variables that
 
1684
        where not known at compile time but are to be entered in the
 
1685
        command. Where the variables should go the string contains
 
1686
        <literal>?</literal>.
 
1687
       </para>
 
1688
      </listitem>
 
1689
     </varlistentry>
 
1690
 
 
1691
     <varlistentry>
 
1692
      <term>Input variables</term>
 
1693
      <listitem>
 
1694
       <para>
 
1695
        Every input variable causes ten arguments to be created.  (See below.)
 
1696
       </para>
 
1697
      </listitem>
 
1698
     </varlistentry>
 
1699
 
 
1700
     <varlistentry>
 
1701
      <term><parameter>ECPGt_EOIT</></term>
 
1702
      <listitem>
 
1703
       <para>
 
1704
        An <type>enum</> telling that there are no more input
 
1705
        variables.
 
1706
       </para>
 
1707
      </listitem>
 
1708
     </varlistentry>
 
1709
 
 
1710
     <varlistentry>
 
1711
      <term>Output variables</term>
 
1712
      <listitem>
 
1713
       <para>
 
1714
        Every output variable causes ten arguments to be created.
 
1715
        (See below.)  These variables are filled by the function.
 
1716
       </para>
 
1717
      </listitem>
 
1718
     </varlistentry>
 
1719
 
 
1720
      <varlistentry>
 
1721
       <term><parameter>ECPGt_EORT</></term>
 
1722
       <listitem>
 
1723
       <para>
 
1724
        An <type>enum</> telling that there are no more variables.
 
1725
       </para>
 
1726
      </listitem>
 
1727
     </varlistentry>
 
1728
    </variablelist>
 
1729
   </para>
 
1730
 
 
1731
   <para>
 
1732
    For every variable that is part of the <acronym>SQL</acronym>
 
1733
    command, the function gets ten arguments:
 
1734
 
 
1735
    <orderedlist>
 
1736
     <listitem>
 
1737
      <para>
 
1738
       The type as a special symbol.
 
1739
      </para>
 
1740
     </listitem>
 
1741
 
 
1742
     <listitem>
 
1743
      <para> 
 
1744
       A pointer to the value or a pointer to the pointer.
 
1745
      </para>
 
1746
     </listitem>
 
1747
 
 
1748
     <listitem>
 
1749
      <para>
 
1750
       The size of the variable if it is a <type>char</type> or <type>varchar</type>.
 
1751
      </para>
 
1752
     </listitem>
 
1753
 
 
1754
     <listitem>
 
1755
      <para>
 
1756
       The number of elements in the array (for array fetches).
 
1757
      </para>
 
1758
     </listitem>
 
1759
 
 
1760
     <listitem>
 
1761
      <para>
 
1762
       The offset to the next element in the array (for array fetches).
 
1763
      </para>
 
1764
     </listitem>
 
1765
 
 
1766
     <listitem>
 
1767
      <para>
 
1768
       The type of the indicator variable as a special symbol.
 
1769
      </para>
 
1770
     </listitem>
 
1771
 
 
1772
     <listitem>
 
1773
      <para>
 
1774
       A pointer to the indicator variable.
 
1775
      </para>
 
1776
     </listitem>
 
1777
 
 
1778
     <listitem>
 
1779
      <para>
 
1780
       0
 
1781
      </para>
 
1782
     </listitem>
 
1783
 
 
1784
     <listitem>
 
1785
      <para>
 
1786
       The number of elements in the indicator array (for array fetches).
 
1787
      </para>
 
1788
     </listitem>
 
1789
 
 
1790
     <listitem>
 
1791
      <para>
 
1792
       The offset to the next element in the indicator array (for
 
1793
       array fetches).
 
1794
      </para>
 
1795
     </listitem>
 
1796
    </orderedlist>
 
1797
   </para>
 
1798
 
 
1799
   <para>
 
1800
    Note that not all SQL commands are treated in this way.  For
 
1801
    instance, an open cursor statement like
 
1802
<programlisting>
 
1803
EXEC SQL OPEN <replaceable>cursor</replaceable>;
 
1804
</programlisting>
 
1805
    is not copied to the output. Instead, the cursor's
 
1806
    <command>DECLARE</> command is used at the position of the <command>OPEN</> command
 
1807
    because it indeed opens the cursor.
 
1808
   </para>
 
1809
 
 
1810
   <para>
 
1811
    Here is a complete example describing the output of the
 
1812
    preprocessor of a file <filename>foo.pgc</filename> (details may
 
1813
    change with each particular version of the preprocessor):
 
1814
<programlisting>
 
1815
EXEC SQL BEGIN DECLARE SECTION;
 
1816
int index;
 
1817
int result;
 
1818
EXEC SQL END DECLARE SECTION;
 
1819
...
 
1820
EXEC SQL SELECT res INTO :result FROM mytable WHERE index = :index;
 
1821
</programlisting>
 
1822
    is translated into:
 
1823
<programlisting>
 
1824
/* Processed by ecpg (2.6.0) */
 
1825
/* These two include files are added by the preprocessor */
 
1826
#include &lt;ecpgtype.h&gt;;
 
1827
#include &lt;ecpglib.h&gt;;
 
1828
 
 
1829
/* exec sql begin declare section */
 
1830
 
 
1831
#line 1 "foo.pgc"
 
1832
 
 
1833
 int index;
 
1834
 int result;
 
1835
/* exec sql end declare section */
 
1836
...
 
1837
ECPGdo(__LINE__, NULL, "SELECT res FROM mytable WHERE index = ?     ",
 
1838
        ECPGt_int,&amp;(index),1L,1L,sizeof(int),
 
1839
        ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
 
1840
        ECPGt_int,&amp;(result),1L,1L,sizeof(int),
 
1841
        ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
 
1842
#line 147 "foo.pgc"
 
1843
</programlisting>
 
1844
    (The indentation here is added for readability and not
 
1845
    something the preprocessor does.)
 
1846
   </para>
 
1847
 </sect1>
 
1848
</chapter>
 
1849
 
 
1850
<!-- Keep this comment at the end of the file
 
1851
Local variables:
 
1852
mode:sgml
 
1853
sgml-omittag:nil
 
1854
sgml-shorttag:t
 
1855
sgml-minimize-attributes:nil
 
1856
sgml-always-quote-attributes:t
 
1857
sgml-indent-step:1
 
1858
sgml-indent-data:t
 
1859
sgml-parent-document:nil
 
1860
sgml-default-dtd-file:"./reference.ced"
 
1861
sgml-exposed-tags:nil
 
1862
sgml-local-catalogs:("/usr/lib/sgml/catalog")
 
1863
sgml-local-ecat-files:nil
 
1864
End:
 
1865
-->