ecpg.sgml 35.6 KB
Newer Older
1
<!--
Tom Lane's avatar
Tom Lane committed
2
$Header: /cvsroot/pgsql/doc/src/sgml/ecpg.sgml,v 1.28 2001/11/01 04:48:00 tgl Exp $
3 4
-->

5
 <chapter id="ecpg">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
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
  <docinfo>
   <authorgroup>
    <author>
     <firstname>Linus</firstname>
     <surname>Tolke</surname>
    </author>
    <author>
     <firstname>Michael</firstname>
     <surname>Meskes</surname>
    </author>
   </authorgroup>
   <copyright>
    <year>1996-1997</year>
    <holder>Linus Tolke</holder>
   </copyright>
   <copyright>
    <year>1998</year>
    <holder>Michael Meskes</holder>
   </copyright>
   <date>Transcribed 1998-02-12</date>
  </docinfo>

  <title><application>ecpg</application> - Embedded <acronym>SQL</acronym> 
   in <acronym>C</acronym></title>

  <para>
Bruce Momjian's avatar
Bruce Momjian committed
32 33 34 35 36 37
   This describes the embedded <acronym>SQL</acronym> package for
   <productname>Postgres</productname>. It works with
   <acronym>C</acronym> and <acronym>C++</acronym>. It was written by
   Linus Tolke (<email>linus@epact.se</email>) and Michael Meskes
   (<email>meskes@debian.org</email>). The package is installed with the
   <productname>Postgres</> distribution, and carries a similar license.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
38
  </para>
39

40
  <sect1 id="ecpg-why">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
41 42 43
   <title>Why Embedded <acronym>SQL</acronym>?</title>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
44 45 46 47 48
    Embedded <acronym>SQL</acronym> has advantages over other methods
    for handling <acronym>SQL</acronym> queries. It takes care of
    the tedious passing of information to and from variables in your
    <acronym>C</acronym> or <acronym>C++</acronym> program. Many
    <acronym>RDBMS</acronym> packages support this embedded language.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
49 50
   </para>

Bruce Momjian's avatar
Bruce Momjian committed
51 52 53 54 55 56
   <para> 
    There is an ANSI standard describing how the embedded language
    should work. <application>ecpg</application> was designed to match
    this standard as much as possible. It is possible to port embedded
    <acronym>SQL</acronym> programs written for other
    <acronym>RDBMS</acronym> to <productname>Postgres</productname>.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
57 58 59
   </para>
  </sect1>

60
  <sect1 id="ecpg-concept">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
61 62 63
   <title>The Concept</title>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
64 65 66 67 68
    You write your program in <acronym>C/C++</acronym> with special
    <acronym>SQL</acronym> constructs. When declaring variables to be
    used in <acronym>SQL</acronym> statements, you need to put them in a
    special <command>declare</> section. You use a special syntax for the
    <acronym>SQL</acronym> queries.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
69 70 71
   </para>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
72 73 74 75 76
    Before compiling you run the file through the embedded
    <acronym>SQL</acronym> <acronym>C</acronym> preprocessor and it
    converts the <acronym>SQL</acronym> statements you used to function
    calls with the variables used as arguments. Both query input and
    result output variables are passed.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
77 78 79
   </para>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
80 81 82 83 84
    After compiling, you must link with a special library that contains
    needed functions. These functions fetch information from the
    arguments, perform the <acronym>SQL</acronym> query using the
    <filename>libpq</filename> interface, and put the result in the
    arguments specified for output.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
85 86 87
   </para>
  </sect1>

88
  <sect1 id="ecpg-use">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
89 90 91
   <title>How To Use <application>ecpg</application></title>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
92
    This section describes how to use <application>ecpg</application>.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
93 94 95 96 97 98
   </para>

   <sect2>
    <title>Preprocessor</title>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
99 100 101
     The preprocessor is called <application>ecpg</application>. After
     installation it resides in the <productname>Postgres</productname>
     <filename>bin/</filename> directory.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
102 103 104 105 106 107
    </para>
   </sect2>
   <sect2>
    <title>Library</title>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
108 109 110 111 112 113
     The <application>ecpg</application> library is called
     <filename>libecpg.a</filename> or <filename>libecpg.so</filename>.
     Additionally, the library uses the <filename>libpq</filename>
     library for communication to the
     <productname>Postgres</productname> server. You will have to link
     your program using <parameter>-lecpg -lpq</parameter>.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
114 115 116
    </para>

    <para>
117
     The library has some methods that are <quote>hidden</quote> but may prove
Bruce Momjian's avatar
Bruce Momjian committed
118
     useful.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
119 120 121 122

     <itemizedlist>
      <listitem>
       <para>
123 124 125 126 127
        <function>ECPGdebug(int <replaceable>on</replaceable>, FILE
        *<replaceable>stream</replaceable>)</function> turns on debug
        logging if called with the first argument non-zero. Debug
        logging is done on <replaceable>stream</replaceable>. Most
        <acronym>SQL</acronym> statement log their arguments and results.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
128 129 130
       </para>

       <para>
131 132 133 134 135 136
        The most important function , <function>ECPGdo</function>, logs
        all <acronym>SQL</acronym> statements with both the expanded
        string, i.e. the string with all the input variables inserted,
        and the result from the <productname>Postgres</productname>
        server. This can be very useful when searching for errors in
        your <acronym>SQL</acronym> statements.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
137 138 139 140 141
       </para>
      </listitem>

      <listitem>
       <para>
142 143
        <function>ECPGstatus()</function>
        This method returns TRUE if we are connected to a database and FALSE if not.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
144 145 146 147 148 149 150 151 152 153
       </para>
      </listitem>
     </itemizedlist>
    </para>
   </sect2>

   <sect2>
    <title>Error handling</title>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
154 155
     To detect errors from the <productname>Postgres</productname>
     server, include a line like: 
156
<programlisting>
Bruce Momjian's avatar
Bruce Momjian committed
157
exec sql include sqlca; 
158
</programlisting>
Bruce Momjian's avatar
Bruce Momjian committed
159 160
     in the include section of your file. This will define a struct and
     a variable with the name <parameter>sqlca</parameter> as follows:
161
<programlisting>
Michael Meskes's avatar
Michael Meskes committed
162 163 164 165 166 167 168 169 170 171 172 173 174
struct sqlca
{
 char sqlcaid[8];
 long sqlabc;
 long sqlcode;
 struct
 {
  int sqlerrml;
  char sqlerrmc[70];
 } sqlerrm;
 char sqlerrp[8];
 long sqlerrd[6];
 /* 0: empty                                         */
Michael Meskes's avatar
Michael Meskes committed
175
 /* 1: OID of processed tuple if applicable          */
Michael Meskes's avatar
Michael Meskes committed
176 177 178 179 180 181 182
 /* 2: number of rows processed in an INSERT, UPDATE */
 /*    or DELETE statement                           */
 /* 3: empty                                         */
 /* 4: empty                                         */
 /* 5: empty                                         */
 char sqlwarn[8];
 /* 0: set to 'W' if at least one other is 'W'       */
183
 /* 1: if 'W' at least one character string          */
Michael Meskes's avatar
Michael Meskes committed
184 185 186 187 188 189 190 191 192
 /*    value was truncated when it was               */
 /*    stored into a host variable.                  */
 /* 2: empty                                         */
 /* 3: empty                                         */
 /* 4: empty                                         */
 /* 5: empty                                         */
 /* 6: empty                                         */
 /* 7: empty                                         */
 char sqlext[8];
193
} sqlca;
194
</programlisting>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
195 196 197
    </para>

    <para>
198 199 200
     If an no error occurred in the last <acronym>SQL</acronym> statement.
     <parameter>sqlca.sqlcode</parameter> will be 0 (ECPG_NO_ERROR). If
     <parameter>sqlca.sqlcode</parameter> is less that zero, this is a
Bruce Momjian's avatar
Bruce Momjian committed
201
     serious error, like the database definition does not match the
202
     query. If it is greater than zero, it is a normal error like the
Bruce Momjian's avatar
Bruce Momjian committed
203
     table did not contain the requested row.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
204 205 206
    </para>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
207 208 209
     <parameter>sqlca.sqlerrm.sqlerrmc</parameter> will contain a string
     that describes the error. The string ends with the line number in
     the source file.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
210 211 212
    </para>

    <para>
213
     These are the errors that can occur:
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
214 215 216

     <variablelist>
      <varlistentry>
217
       <term><computeroutput>-12, Out of memory in line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
218
       <listitem>
219 220 221 222
        <para>
         Should not normally occur. This indicates your virtual memory is
         exhausted.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
223 224 225 226
       </listitem>
      </varlistentry>

      <varlistentry>
227
       <term><computeroutput>-200 (ECPG_UNSUPPORTED): Unsupported type %s on line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
228
       <listitem>
229 230 231 232 233 234
        <para>
         Should not normally occur. This indicates the preprocessor has
         generated something that the library does not know about.
         Perhaps you are running incompatible versions of the
         preprocessor and the library.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
235 236 237 238
       </listitem>
      </varlistentry>

      <varlistentry>
239
       <term><computeroutput>-201 (ECPG_TOO_MANY_ARGUMENTS): Too many arguments line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
240
       <listitem>
241 242 243 244 245 246
        <para>
         This means that <productname>Postgres</productname> has
         returned more arguments than we have matching variables.
         Perhaps you have forgotten a couple of the host variables in
         the <command>INTO :var1,:var2</command>-list.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
247 248 249 250
       </listitem>
      </varlistentry>

      <varlistentry>
251
       <term><computeroutput>-202 (ECPG_TOO_FEW_ARGUMENTS): Too few arguments line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
252
       <listitem>
253 254 255 256 257 258
        <para>
         This means that <productname>Postgres</productname> has
         returned fewer arguments than we have host variables. Perhaps
         you have too many host variables in the <command>INTO
         :var1,:var2</command>-list.
        </para> 
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
259 260 261 262
       </listitem>
      </varlistentry>

      <varlistentry>
263
       <term><computeroutput>-203 (ECPG_TOO_MANY_MATCHES): Too many matches line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
264
       <listitem>
265 266 267 268 269
        <para>
         This means the query has returned several rows but the
         variables specified are not arrays. The
         <command>SELECT</command> command was not unique.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
270 271 272 273
       </listitem>
      </varlistentry>

      <varlistentry>
274
       <term><computeroutput>-204 (ECPG_INT_FORMAT): Not correctly formatted int type: %s line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
275
       <listitem>
276 277 278 279 280 281 282
        <para>
         This means the host variable is of type <type>int</type> and
         the field in the <productname>Postgres</productname> database
         is of another type and contains a value that cannot be
         interpreted as an <type>int</type>. The library uses
         <function>strtol()</function> for this conversion.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
283 284 285 286
       </listitem>
      </varlistentry>

      <varlistentry>
287
       <term><computeroutput>-205 (ECPG_UINT_FORMAT): Not correctly formatted unsigned type: %s line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
288
       <listitem>
289 290 291 292 293 294 295 296
        <para>
         This means the host variable is of type <type>unsigned
         int</type> and the field in the
         <productname>Postgres</productname> database is of another type
         and contains a value that cannot be interpreted as an
         <type>unsigned int</type>. The library uses
         <function>strtoul()</function> for this conversion.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
297 298 299 300
       </listitem>
      </varlistentry>

      <varlistentry>
301
       <term><computeroutput>-206 (ECPG_FLOAT_FORMAT): Not correctly formatted floating point type: %s line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
302
       <listitem>
303 304 305 306 307 308 309
        <para>
         This means the host variable is of type <type>float</type> and
         the field in the <productname>Postgres</productname> database
         is of another type and contains a value that cannot be
         interpreted as a <type>float</type>. The library uses
         <function>strtod()</function> for this conversion.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
310 311 312 313
       </listitem>
      </varlistentry>

      <varlistentry>
314
       <term><computeroutput>-207 (ECPG_CONVERT_BOOL): Unable to convert %s to bool on line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
315
       <listitem>
316 317 318 319 320
        <para>
         This means the host variable is of type <type>bool</type> and
         the field in the <productname>Postgres</productname> database
         is neither <literal>'t'</> nor <literal>'f'</>.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
321 322 323 324
       </listitem>
      </varlistentry>

      <varlistentry>
325
       <term><computeroutput>-208 (ECPG_EMPTY): Empty query line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
326
       <listitem>
327 328 329 330
        <para>
         <productname>Postgres</productname> returned <symbol>PGRES_EMPTY_QUERY</symbol>, probably
         because the query indeed was empty.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
331 332 333
       </listitem>
      </varlistentry>

334
      <varlistentry>
335
       <term><computeroutput>-209 (ECPG_MISSING_INDICATOR): NULL value without indicator in line %d.</computeroutput></term>
336
       <listitem>
337 338 339 340
        <para>
         <productname>Postgres</productname> returned <symbol>ECPG_MISSING_INDICATOR</symbol>
         because a NULL was returned and no NULL indicator variable was supplied.
        </para>
341 342 343
       </listitem>
      </varlistentry>

Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
344
      <varlistentry>
345
       <term><computeroutput>-210 (ECPG_NO_ARRAY): Variable is not an array in line %d.</computeroutput></term>
346
       <listitem>
347 348 349 350 351
        <para>
         <productname>Postgres</productname> returned <symbol>ECPG_NO_ARRAY</symbol>
         because an ordinary variable was used in a place that requires
         an array.
        </para>
352 353 354 355
       </listitem>
      </varlistentry>

      <varlistentry>
356
       <term><computeroutput>-211 (ECPG_DATA_NOT_ARRAY): Data read from backend is not an array in line %d.</computeroutput></term>
357
       <listitem>
358 359 360 361 362
        <para>
         <productname>Postgres</productname> returned <symbol>ECPG_DATA_NOT_ARRAY</symbol>
         because the database returned an ordinary variable in a place
         that requires array value.
        </para>
363 364 365 366
       </listitem>
      </varlistentry>

      <varlistentry>
367
       <term><computeroutput>-220 (ECPG_NO_CONN): No such connection %s in line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
368
       <listitem>
369 370 371
        <para>
         The program tried to access a connection that does not exist.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
372 373 374 375
       </listitem>
      </varlistentry>

      <varlistentry>
376
       <term><computeroutput>-221 (ECPG_NOT_CONN): Not connected in line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
377
       <listitem>
378 379 380 381
        <para>
         The program tried to access a connection that does exist but is
         not open.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
382 383 384 385
       </listitem>
      </varlistentry>

      <varlistentry>
386
       <term><computeroutput>-230 (ECPG_INVALID_STMT): Invalid statement name %s in line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
387
       <listitem>
388 389 390
        <para>
         The statement you are trying to use has not been prepared.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
391 392 393 394
       </listitem>
      </varlistentry>

      <varlistentry>
395
       <term><computeroutput>-240 (ECPG_UNKNOWN_DESCRIPTOR): Descriptor %s not found in line %d.</computeroutput></term>
396
       <listitem>
397 398 399
        <para>
         The descriptor specified was not foundstatement you are trying to use has not been prepared.
        </para>
400 401 402 403
       </listitem>
      </varlistentry>

      <varlistentry>
404
       <term><computeroutput>-241 (ECPG_INVALID_DESCRIPTOR_INDEX): Descriptor index out of range in line %d.</computeroutput></term>
405
       <listitem>
406 407 408
        <para>
         The descriptor index specified was out of range.
        </para>
409 410 411 412
       </listitem>
      </varlistentry>

      <varlistentry>
413
       <term><computeroutput>-242 (ECPG_UNKNOWN_DESCRIPTOR_ITEM): Descriptor %s not found in line %d.</computeroutput></term>
414
       <listitem>
415 416 417
        <para>
         The descriptor specified was not foundstatement you are trying to use has not been prepared.
        </para>
418 419 420 421
       </listitem>
      </varlistentry>

      <varlistentry>
422
       <term><computeroutput>-243 (ECPG_VAR_NOT_NUMERIC): Variable is not a numeric type in line %d.</computeroutput></term>
423
       <listitem>
424 425 426 427
        <para>
         The database returned a numeric value and the variable was not
         numeric.
        </para>
428 429 430 431
       </listitem>
      </varlistentry>

      <varlistentry>
432
       <term><computeroutput>-244 (ECPG_VAR_NOT_CHAR): Variable is not a character type in line %d.</computeroutput></term>
433
       <listitem>
434 435 436 437
        <para>
         The database returned a non-numeric value and the variable was
         numeric.
        </para>
438 439 440 441
       </listitem>
      </varlistentry>

      <varlistentry>
442
       <term><computeroutput>-400 (ECPG_PGSQL): Postgres error: %s line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
443
       <listitem>
444 445 446 447 448
        <para>
         Some <productname>Postgres</productname> error. 
         The message contains the error message from the
         <productname>Postgres</productname> backend.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
449 450 451 452
       </listitem>
      </varlistentry>

      <varlistentry>
453
       <term><computeroutput>-401 (ECPG_TRANS): Error in transaction processing line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
454
       <listitem>
455 456 457 458
        <para>
         <productname>Postgres</productname> signaled that we cannot start,
         commit or rollback the transaction.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
459 460 461 462
       </listitem>
      </varlistentry>

      <varlistentry>
463
       <term><computeroutput>-402 (ECPG_CONNECT): Could not connect to database %s in line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
464
       <listitem>
465 466 467
        <para>
         The connect to the database did not work.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
468 469 470 471
       </listitem>
      </varlistentry>

      <varlistentry>
472
       <term><computeroutput>100 (ECPG_NOT_FOUND): Data not found line %d.</computeroutput></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
473
       <listitem>
474 475 476 477
        <para>
         This is a <quote>normal</quote> error that tells you that what you are querying cannot
         be found or you are at the end of the cursor.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
478 479 480 481 482 483 484 485
       </listitem>
      </varlistentry>

     </variablelist>
    </para>
   </sect2>
  </sect1>

486
  <sect1 id="ecpg-limitations">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
487 488 489
   <title>Limitations</title>

   <para>
490 491
    What will never be included and why it cannot be done:

Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
492 493
    <variablelist>
     <varlistentry>
Bruce Momjian's avatar
Bruce Momjian committed
494
      <term>Oracle's single tasking</term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
495 496
      <listitem>
       <para>
497 498 499 500 501 502
        Oracle version 7.0 on <systemitem class="osname">AIX</> 3 uses OS-supported locks in shared
        memory that allow an application designer to link an application
        in a <quote>single tasking</quote> way. Instead of starting one client
        process per application process, both the database part and the
        application part run in the same process. In later versions of
        Oracle this is no longer supported.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
503 504 505
       </para>

       <para>
506 507 508
        This would require a total redesign of the
        <productname>Postgres</productname> access model and the
        performance gain does not justify the effort.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
509 510 511 512 513 514 515
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </sect1>

516
  <sect1 id="ecpg-porting">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
517 518 519
   <title>Porting From Other <acronym>RDBMS</acronym> Packages</title>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
520 521 522 523 524 525
    The design of <application>ecpg</application> follows the SQL
    standard. Porting from a standard RDBMS should not be a problem.
    Unfortunately there is no such thing as a standard RDBMS. Therefore
    <application>ecpg</application> tries to understand syntax
    extensions as long as they do not create conflicts with the
    standard.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
526 527 528
   </para>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
529 530 531 532 533 534 535
    The following list shows all the known incompatibilities. If you
    find one not listed please notify <ulink
    url="meskes@debian.org">Michael Meskes</ulink>. Note, however, that
    we list only incompatibilities from a precompiler of another RDBMS
    to <application>ecpg</application> and not
    <application>ecpg</application> features that these RDBMS do not
    support.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
536 537 538 539 540
   </para>

   <para>
    <variablelist>
     <varlistentry>
Bruce Momjian's avatar
Bruce Momjian committed
541
      <term>Syntax of FETCH</term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
542 543
      <listitem>
       <para>
544
        The standard syntax for FETCH is:
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
545 546
       </para>
       <para>
547
        FETCH [direction] [amount] IN|FROM <replaceable>cursor</replaceable>.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
548 549
       </para>
       <para>
550 551 552
        <application>ORACLE</application>, however, does not use the keywords IN
        or FROM. This feature cannot be added since it would create parsing
        conflicts.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
553 554 555 556 557 558 559
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </sect1>

560
  <sect1 id="ecpg-develop">
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
561 562 563
   <title>For the Developer</title>

   <para>
Bruce Momjian's avatar
Bruce Momjian committed
564 565 566
    This section explain how <application>ecpg</application>
    works internally. It contains valuable information to help users
    understand how to use <application>ecpg</application>.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
567 568 569 570 571 572
   </para>

   <sect2>
    <title>ToDo List</title>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
573
     This version of the preprocessor has some flaws:
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
574 575 576 577 578

     <variablelist>
      <varlistentry>
       <term>Library functions</term>
       <listitem>
579 580 581 582 583
        <para>
         to_date et al. does not exist. However,
         <productname>Postgres</productname> has some good conversion
         routines so you probably won't miss them.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
584 585 586 587
       </listitem>
      </varlistentry>

      <varlistentry>
Bruce Momjian's avatar
Bruce Momjian committed
588
       <term>Structures and unions</term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
589
       <listitem>
590 591 592 593
        <para>
         Structures and unions have to be defined in the
         <command>declare</> section.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
594 595 596 597 598 599
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Missing statements</term>
       <listitem>
600 601 602 603 604 605 606 607 608 609 610 611
        <para>
         The following statements are not implemented thus far:

         <itemizedlist>
          <listitem>
           <para>
            <literal>exec sql allocate</>
           </para>
          </listitem>

          <listitem>
           <para>
Tom Lane's avatar
Tom Lane committed
612
            <literal>exec sql deallocate</>
613 614 615 616 617
           </para>
          </listitem>
 
          <listitem>
           <para>
Tom Lane's avatar
Tom Lane committed
618
            <literal>SQLSTATE</>
619 620 621 622
           </para>
          </listitem>
         </itemizedlist>
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
623 624 625 626 627 628
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>message 'no data found'</term>
       <listitem>
629 630 631
        <para>
         The error message for <quote>no data</quote> in:
         <programlisting>
Bruce Momjian's avatar
Bruce Momjian committed
632
exec sql insert select from statement
633 634 635
        </programlisting>
         has to be <literal>100</>.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
636 637 638 639
       </listitem>
      </varlistentry>

      <varlistentry>
640
       <term><literal>sqlwarn[6]</literal></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
641
       <listitem>
642 643 644 645 646
        <para>
         <literal>sqlwarn[6]</literal> should be <literal>W</> if the <command>PRECISION</>
         or <command>SCALE</> value specified in a <command>SET
         DESCRIPTOR</> statement was ignored.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
647 648 649 650 651 652 653 654 655 656
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </sect2>

   <sect2>
    <title>The Preprocessor</title>

    <para>
657
     The first four lines written by <command>ecpg</command> to the output are fixed lines.
Bruce Momjian's avatar
Bruce Momjian committed
658 659
     Two are comments and two are include lines necessary to interface
     to the library.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
660 661 662
    </para>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
663 664
     Then the preprocessor reads through the file and writes output.
     Normally it just echoes everything to the output.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
665 666 667
    </para>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
668 669 670
     When it sees an <command>EXEC SQL</command> statement, it
     intervenes and changes it. The <command>EXEC SQL</command>
     statement can be one of these:
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
671 672 673 674 675

     <variablelist>
      <varlistentry>
       <term>Declare sections</term>
       <listitem>
676 677 678
        <para>
         <command>Declare</> sections begin with:
<programlisting>
679
exec sql begin declare section;
680 681 682
</programlisting>
         and end with:
<programlisting>
683
exec sql end declare section;
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
</programlisting>
         In this section only variable declarations are allowed. Every
         variable declared within this section is stored in a list
         of variables indexed by name together with its corresponding
         type.
        </para>

        <para>
         In particular the definition of a structure or union also must
         be listed inside a <command>declare</> section. Otherwise
         <application>ecpg</application> cannot handle these types since
         it does not know the definition.
        </para>

        <para>
         The declaration is also echoed to the file to make it a normal
         C variable.
        </para>

        <para>
         The special types <type>VARCHAR</type> and <type>VARCHAR2</type> are converted into a named struct
         for every variable. A declaration like:
<programlisting>
707
VARCHAR var[180];
708 709 710
</programlisting>
         is converted into:
<programlisting>
711
struct varchar_var { int len; char arr[180]; } var;
712 713
</programlisting>
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
714 715 716 717 718 719
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Include statements</term>
       <listitem>
720 721 722
        <para>
         An include statement looks like:
<programlisting>
723
exec sql include filename;
724 725 726
</programlisting>
         Note that this is NOT the same as:
<programlisting>
727
#include &lt;filename.h&gt;
728 729 730 731 732 733 734 735 736
</programlisting>
        </para>

        <para>
         Instead the file specified is parsed by
         <application>ecpg</application> so the contents of the file are
         included in the resulting C code. This way you are able to
         specify EXEC SQL commands in an include file.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
737 738 739 740 741 742
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Connect statement</term>
       <listitem>
743 744 745
        <para>
         A connect statement looks like:
<programlisting>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
746
exec sql connect to <replaceable>connection target</replaceable>;
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
</programlisting>
         It creates a connection to the specified database.
        </para>

        <para>
         The <replaceable>connection target</replaceable> can be specified in the
         following ways:
         <itemizedlist>
          <listitem>
           <simpara>
            <literal>dbname[@server][:port][as <replaceable>connection
            name</replaceable>][user <replaceable>user name</replaceable>]</literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
            <literal>tcp:postgresql://server[:port][/dbname][as
            <replaceable>connection name</replaceable>][user <replaceable>user name</replaceable>]</literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
            <literal>unix:postgresql://server[:port][/dbname][as
            <replaceable>connection name</replaceable>][user <replaceable>user name</replaceable>]</literal>
           </simpara>
          </listitem>
 
          <listitem>
           <simpara>
            <literal><replaceable>character variable</replaceable>[as
            <replaceable>connection name</replaceable>][user <replaceable>user name</replaceable>]</literal>
           </simpara>
          </listitem>
 
          <listitem>
           <simpara>
            <literal><replaceable>character string</replaceable>[as
            <replaceable>connection name</replaceable>][<replaceable>user</replaceable>]</literal>
           </simpara>
          </listitem>
 
          <listitem>
           <simpara>
            <literal>default</literal>
           </simpara>
          </listitem>
 
          <listitem>
           <simpara>
            <literal>user</literal>
           </simpara>
          </listitem>
         </itemizedlist>
        </para>

        <para>
         There are also different ways to specify the user name:

         <itemizedlist>
          <listitem>
           <simpara>
            <literal><replaceable>userid</replaceable></literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
            <literal><replaceable>userid</replaceable>/<replaceable>password</replaceable></literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
            <literal><replaceable>userid</replaceable> identified by <replaceable>password</replaceable></literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
            <literal><replaceable>userid</replaceable> using <replaceable>password</replaceable></literal>
           </simpara>
          </listitem>
         </itemizedlist>
        </para>

        <para> 
         Finally, the <replaceable>userid</replaceable> and <replaceable>password</replaceable> may be a constant text, a
         character variable, or a character string.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
838 839 840 841 842 843
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Disconnect statements</term>
       <listitem>
844 845 846
        <para>
         A disconnect statement looks like:
         <programlisting>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
847
exec sql disconnect [<replaceable>connection target</replaceable>];
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
         </programlisting>
         It closes the connection to the specified database.
        </para>

        <para>
         The <replaceable>connection target</replaceable> can be specified in the
         following ways:

         <itemizedlist>
          <listitem>
           <simpara>
            <literal><replaceable>connection name</replaceable></literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
           <literal>default</literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
           <literal>current</literal>
           </simpara>
          </listitem>

          <listitem>
           <simpara>
Tom Lane's avatar
Tom Lane committed
877
           <literal>all</literal>
878 879 880 881
           </simpara>
          </listitem>
         </itemizedlist>
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
882 883 884
       </listitem>
      </varlistentry>

885 886 887
      <varlistentry>
       <term>Open cursor statement</term>
       <listitem>
888 889 890
        <para>
         An open cursor statement looks like:
<programlisting>
891
exec sql open <replaceable>cursor</replaceable>;
892 893 894 895 896
</programlisting>
         and is not copied to the output. Instead, the cursor's
         <command>DECLARE</> command is used because it opens the cursor
         as well.
        </para>
897 898 899 900 901 902
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Commit statement</term>
       <listitem>
903 904 905
        <para>
         A commit statement looks like:
<programlisting>
906
exec sql commit;
907 908
</programlisting>
        </para>
909 910 911 912 913 914
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Rollback statement</term>
       <listitem>
915 916 917
        <para>
         A rollback statement looks like:
<programlisting>
918
exec sql rollback;
919 920
</programlisting>
        </para>
921 922 923
       </listitem>
      </varlistentry>

Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
924 925 926
      <varlistentry>
       <term>Other statements</term>
       <listitem>
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
        <para>
         Other <acronym>SQL</acronym> statements are used by
         starting with <command>exec sql</command> and ending with
         <command>;</command>. Everything in between is treated as an
         <acronym>SQL</acronym> statement and parsed for variable
         substitution.
        </para>

        <para>
         Variable substitution occurs when a symbol starts with a colon
         (<command>:</command>). The variable with that name is looked
         up among the variables that were previously declared within a
         <command>declare</> section. Depending on whether the variable is
         being use for input or output, a pointer to the variable is
         output to allow access by the function.
        </para>

        <para>
         For every variable that is part of the <acronym>SQL</acronym>
         query, the function gets other arguments:

         <itemizedlist>
          <listitem>
           <para>
            The type as a special symbol.
           </para>
          </listitem>

          <listitem>
           <para> 
            A pointer to the value or a pointer to the pointer.
           </para>
          </listitem>

          <listitem>
           <para>
            The size of the variable if it is a <type>char</type> or <type>varchar</type>.
           </para>
          </listitem>

          <listitem>
           <para>
            The number of elements in the array (for array fetches).
           </para>
          </listitem>

          <listitem>
           <para>
            The offset to the next element in the array (for array fetches).
           </para>
          </listitem>

          <listitem>
           <para>
            The type of the indicator variable as a special symbol.
           </para>
          </listitem>

          <listitem>
           <para>
            A pointer to the value of the indicator variable or a pointer to the pointer of the indicator variable.
           </para>
          </listitem>

          <listitem>
           <para>
            0.
           </para>
          </listitem>

          <listitem>
           <para>
            Number of elements in the indicator array (for array fetches).
           </para>
          </listitem>

          <listitem>
           <para>
            The offset to the next element in the indicator array (for
            array fetches).
           </para>
          </listitem>
         </itemizedlist>
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </sect2>

   <sect2>
    <title>A Complete Example</title>

    <para>
     Here is a complete example describing the output of the preprocessor of a
1023
     file <filename>foo.pgc</filename>:
1024
<programlisting>
1025 1026 1027 1028 1029
exec sql begin declare section;
int index;
int result;
exec sql end declare section;
...
Michael Meskes's avatar
Michael Meskes committed
1030
exec sql select res into :result from mytable where index = :index;
1031
</programlisting>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1032
     is translated into:
1033
<programlisting>
Michael Meskes's avatar
Michael Meskes committed
1034
/* Processed by ecpg (2.6.0) */
1035
/* These two include files are added by the preprocessor */
Michael Meskes's avatar
Michael Meskes committed
1036 1037 1038
#include &lt;ecpgtype.h&gt;;
#include &lt;ecpglib.h&gt;;

1039 1040
/* exec sql begin declare section */

Michael Meskes's avatar
Michael Meskes committed
1041 1042
#line 1 "foo.pgc"

1043 1044 1045 1046
 int index;
 int result;
/* exec sql end declare section */
...
Michael Meskes's avatar
Michael Meskes committed
1047
ECPGdo(__LINE__, NULL, "select  res  from mytable where index = ?     ",
1048
        ECPGt_int,&amp;(index),1L,1L,sizeof(int),
Michael Meskes's avatar
Michael Meskes committed
1049 1050 1051 1052
        ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
        ECPGt_int,&amp;(result),1L,1L,sizeof(int),
        ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
#line 147 "foo.pgc"
1053
</programlisting>
Bruce Momjian's avatar
Bruce Momjian committed
1054 1055
     (The indentation in this manual is added for readability and not
     something the preprocessor does.)
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1056 1057 1058 1059 1060 1061 1062
    </para>
   </sect2>

   <sect2>
    <title>The Library</title>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
1063 1064 1065
     The most important function in the library is
     <function>ECPGdo</function>. It takes a variable number of
     arguments. Hopefully there are no computers that limit the
1066
     number of variables that can be accepted by a <function>varargs()</function> function. This
Bruce Momjian's avatar
Bruce Momjian committed
1067
     can easily add up to 50 or so arguments.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1068 1069 1070 1071 1072 1073 1074 1075 1076
    </para>

    <para>
     The arguments are:

     <variablelist>
      <varlistentry>
       <term>A line number</term>
       <listitem>
1077 1078 1079
        <para>
         This is a line number of the original line; used in error messages only.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1080 1081 1082 1083 1084 1085
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>A string</term>
       <listitem>
1086 1087 1088 1089 1090 1091 1092
        <para>
         This is the <acronym>SQL</acronym> query that is to be issued.
         It is modified by the input variables, i.e. the variables that
         where not known at compile time but are to be entered in the
         query. Where the variables should go the string contains
         <literal>?</literal>.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1093 1094 1095 1096 1097 1098
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Input variables</term>
       <listitem>
1099 1100 1101 1102
        <para>
         As described in the section about the preprocessor, every input variable
         gets ten arguments.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1103 1104 1105 1106
       </listitem>
      </varlistentry>

      <varlistentry>
1107
       <term><literal>ECPGt_EOIT</literal></term>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1108
       <listitem>
1109 1110 1111
        <para>
         An enum telling that there are no more input variables.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1112 1113 1114 1115 1116 1117
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>Output variables</term>
       <listitem>
1118 1119 1120 1121
        <para>
         As described in the section about the preprocessor, every input variable
         gets ten arguments. These variables are filled by the function.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1122 1123 1124 1125 1126 1127
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>ECPGt_EORT</term>
       <listitem>
1128 1129 1130
        <para>
         An enum telling that there are no more variables.
        </para>
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1131 1132 1133 1134 1135 1136
       </listitem>
      </varlistentry>
     </variablelist>
    </para>

    <para>
Bruce Momjian's avatar
Bruce Momjian committed
1137 1138 1139 1140 1141 1142
     All <acronym>SQL</acronym> statements are performed in one
     transaction unless you issue a commit transaction. To accomplish
     this auto-transaction behavior, the first statement and the first
     statement after a commit or rollback always begins a new transaction. To
     disable this feature, use the <option>-t</option> command-line
     option.
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    </para>

   </sect2>
  </sect1>
 </chapter>

<!-- 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
1161
sgml-local-catalogs:("/usr/lib/sgml/catalog")
Thomas G. Lockhart's avatar
Thomas G. Lockhart committed
1162 1163 1164
sgml-local-ecat-files:nil
End:
-->