]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-variables.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-variables.txt
1
2 34.4. Using Host Variables #
3
4    34.4.1. Overview
5    34.4.2. Declare Sections
6    34.4.3. Retrieving Query Results
7    34.4.4. Type Mapping
8    34.4.5. Handling Nonprimitive SQL Data Types
9    34.4.6. Indicators
10
11    In Section 34.3 you saw how you can execute SQL statements from an
12    embedded SQL program. Some of those statements only used fixed values
13    and did not provide a way to insert user-supplied values into
14    statements or have the program process the values returned by the
15    query. Those kinds of statements are not really useful in real
16    applications. This section explains in detail how you can pass data
17    between your C program and the embedded SQL statements using a simple
18    mechanism called host variables. In an embedded SQL program we consider
19    the SQL statements to be guests in the C program code which is the host
20    language. Therefore the variables of the C program are called host
21    variables.
22
23    Another way to exchange values between PostgreSQL backends and ECPG
24    applications is the use of SQL descriptors, described in Section 34.7.
25
26 34.4.1. Overview #
27
28    Passing data between the C program and the SQL statements is
29    particularly simple in embedded SQL. Instead of having the program
30    paste the data into the statement, which entails various complications,
31    such as properly quoting the value, you can simply write the name of a
32    C variable into the SQL statement, prefixed by a colon. For example:
33 EXEC SQL INSERT INTO sometable VALUES (:v1, 'foo', :v2);
34
35    This statement refers to two C variables named v1 and v2 and also uses
36    a regular SQL string literal, to illustrate that you are not restricted
37    to use one kind of data or the other.
38
39    This style of inserting C variables in SQL statements works anywhere a
40    value expression is expected in an SQL statement.
41
42 34.4.2. Declare Sections #
43
44    To pass data from the program to the database, for example as
45    parameters in a query, or to pass data from the database back to the
46    program, the C variables that are intended to contain this data need to
47    be declared in specially marked sections, so the embedded SQL
48    preprocessor is made aware of them.
49
50    This section starts with:
51 EXEC SQL BEGIN DECLARE SECTION;
52
53    and ends with:
54 EXEC SQL END DECLARE SECTION;
55
56    Between those lines, there must be normal C variable declarations, such
57    as:
58 int   x = 4;
59 char  foo[16], bar[16];
60
61    As you can see, you can optionally assign an initial value to the
62    variable. The variable's scope is determined by the location of its
63    declaring section within the program. You can also declare variables
64    with the following syntax which implicitly creates a declare section:
65 EXEC SQL int i = 4;
66
67    You can have as many declare sections in a program as you like.
68
69    The declarations are also echoed to the output file as normal C
70    variables, so there's no need to declare them again. Variables that are
71    not intended to be used in SQL commands can be declared normally
72    outside these special sections.
73
74    The definition of a structure or union also must be listed inside a
75    DECLARE section. Otherwise the preprocessor cannot handle these types
76    since it does not know the definition.
77
78 34.4.3. Retrieving Query Results #
79
80    Now you should be able to pass data generated by your program into an
81    SQL command. But how do you retrieve the results of a query? For that
82    purpose, embedded SQL provides special variants of the usual commands
83    SELECT and FETCH. These commands have a special INTO clause that
84    specifies which host variables the retrieved values are to be stored
85    in. SELECT is used for a query that returns only single row, and FETCH
86    is used for a query that returns multiple rows, using a cursor.
87
88    Here is an example:
89 /*
90  * assume this table:
91  * CREATE TABLE test1 (a int, b varchar(50));
92  */
93
94 EXEC SQL BEGIN DECLARE SECTION;
95 int v1;
96 VARCHAR v2;
97 EXEC SQL END DECLARE SECTION;
98
99  ...
100
101 EXEC SQL SELECT a, b INTO :v1, :v2 FROM test;
102
103    So the INTO clause appears between the select list and the FROM clause.
104    The number of elements in the select list and the list after INTO (also
105    called the target list) must be equal.
106
107    Here is an example using the command FETCH:
108 EXEC SQL BEGIN DECLARE SECTION;
109 int v1;
110 VARCHAR v2;
111 EXEC SQL END DECLARE SECTION;
112
113  ...
114
115 EXEC SQL DECLARE foo CURSOR FOR SELECT a, b FROM test;
116
117  ...
118
119 do
120 {
121     ...
122     EXEC SQL FETCH NEXT FROM foo INTO :v1, :v2;
123     ...
124 } while (...);
125
126    Here the INTO clause appears after all the normal clauses.
127
128 34.4.4. Type Mapping #
129
130    When ECPG applications exchange values between the PostgreSQL server
131    and the C application, such as when retrieving query results from the
132    server or executing SQL statements with input parameters, the values
133    need to be converted between PostgreSQL data types and host language
134    variable types (C language data types, concretely). One of the main
135    points of ECPG is that it takes care of this automatically in most
136    cases.
137
138    In this respect, there are two kinds of data types: Some simple
139    PostgreSQL data types, such as integer and text, can be read and
140    written by the application directly. Other PostgreSQL data types, such
141    as timestamp and numeric can only be accessed through special library
142    functions; see Section 34.4.4.2.
143
144    Table 34.1 shows which PostgreSQL data types correspond to which C data
145    types. When you wish to send or receive a value of a given PostgreSQL
146    data type, you should declare a C variable of the corresponding C data
147    type in the declare section.
148
149    Table 34.1. Mapping Between PostgreSQL Data Types and C Variable Types
150         PostgreSQL data type        Host variable type
151    smallint                       short
152    integer                        int
153    bigint                         long long int
154    decimal                        decimal^[a]
155    numeric                        numeric^[a]
156    real                           float
157    double precision               double
158    smallserial                    short
159    serial                         int
160    bigserial                      long long int
161    oid                            unsigned int
162    character(n), varchar(n), text char[n+1], VARCHAR[n+1]
163    name                           char[NAMEDATALEN]
164    timestamp                      timestamp^[a]
165    interval                       interval^[a]
166    date                           date^[a]
167    boolean                        bool^[b]
168    bytea                          char *, bytea[n]
169
170    ^[a] This type can only be accessed through special library functions;
171    see Section 34.4.4.2.
172
173    ^[b] declared in ecpglib.h if not native
174
175 34.4.4.1. Handling Character Strings #
176
177    To handle SQL character string data types, such as varchar and text,
178    there are two possible ways to declare the host variables.
179
180    One way is using char[], an array of char, which is the most common way
181    to handle character data in C.
182 EXEC SQL BEGIN DECLARE SECTION;
183     char str[50];
184 EXEC SQL END DECLARE SECTION;
185
186    Note that you have to take care of the length yourself. If you use this
187    host variable as the target variable of a query which returns a string
188    with more than 49 characters, a buffer overflow occurs.
189
190    The other way is using the VARCHAR type, which is a special type
191    provided by ECPG. The definition on an array of type VARCHAR is
192    converted into a named struct for every variable. A declaration like:
193 VARCHAR var[180];
194
195    is converted into:
196 struct varchar_var { int len; char arr[180]; } var;
197
198    The member arr hosts the string including a terminating zero byte.
199    Thus, to store a string in a VARCHAR host variable, the host variable
200    has to be declared with the length including the zero byte terminator.
201    The member len holds the length of the string stored in the arr without
202    the terminating zero byte. When a host variable is used as input for a
203    query, if strlen(arr) and len are different, the shorter one is used.
204
205    VARCHAR can be written in upper or lower case, but not in mixed case.
206
207    char and VARCHAR host variables can also hold values of other SQL
208    types, which will be stored in their string forms.
209
210 34.4.4.2. Accessing Special Data Types #
211
212    ECPG contains some special types that help you to interact easily with
213    some special data types from the PostgreSQL server. In particular, it
214    has implemented support for the numeric, decimal, date, timestamp, and
215    interval types. These data types cannot usefully be mapped to primitive
216    host variable types (such as int, long long int, or char[]), because
217    they have a complex internal structure. Applications deal with these
218    types by declaring host variables in special types and accessing them
219    using functions in the pgtypes library. The pgtypes library, described
220    in detail in Section 34.6 contains basic functions to deal with those
221    types, such that you do not need to send a query to the SQL server just
222    for adding an interval to a time stamp for example.
223
224    The follow subsections describe these special data types. For more
225    details about pgtypes library functions, see Section 34.6.
226
227 34.4.4.2.1. timestamp, date #
228
229    Here is a pattern for handling timestamp variables in the ECPG host
230    application.
231
232    First, the program has to include the header file for the timestamp
233    type:
234 #include <pgtypes_timestamp.h>
235
236    Next, declare a host variable as type timestamp in the declare section:
237 EXEC SQL BEGIN DECLARE SECTION;
238 timestamp ts;
239 EXEC SQL END DECLARE SECTION;
240
241    And after reading a value into the host variable, process it using
242    pgtypes library functions. In following example, the timestamp value is
243    converted into text (ASCII) form with the PGTYPEStimestamp_to_asc()
244    function:
245 EXEC SQL SELECT now()::timestamp INTO :ts;
246
247 printf("ts = %s\n", PGTYPEStimestamp_to_asc(ts));
248
249    This example will show some result like following:
250 ts = 2010-06-27 18:03:56.949343
251
252    In addition, the DATE type can be handled in the same way. The program
253    has to include pgtypes_date.h, declare a host variable as the date type
254    and convert a DATE value into a text form using PGTYPESdate_to_asc()
255    function. For more details about the pgtypes library functions, see
256    Section 34.6.
257
258 34.4.4.2.2. interval #
259
260    The handling of the interval type is also similar to the timestamp and
261    date types. It is required, however, to allocate memory for an interval
262    type value explicitly. In other words, the memory space for the
263    variable has to be allocated in the heap memory, not in the stack
264    memory.
265
266    Here is an example program:
267 #include <stdio.h>
268 #include <stdlib.h>
269 #include <pgtypes_interval.h>
270
271 int
272 main(void)
273 {
274 EXEC SQL BEGIN DECLARE SECTION;
275     interval *in;
276 EXEC SQL END DECLARE SECTION;
277
278     EXEC SQL CONNECT TO testdb;
279     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
280 MMIT;
281
282     in = PGTYPESinterval_new();
283     EXEC SQL SELECT '1 min'::interval INTO :in;
284     printf("interval = %s\n", PGTYPESinterval_to_asc(in));
285     PGTYPESinterval_free(in);
286
287     EXEC SQL COMMIT;
288     EXEC SQL DISCONNECT ALL;
289     return 0;
290 }
291
292 34.4.4.2.3. numeric, decimal #
293
294    The handling of the numeric and decimal types is similar to the
295    interval type: It requires defining a pointer, allocating some memory
296    space on the heap, and accessing the variable using the pgtypes library
297    functions. For more details about the pgtypes library functions, see
298    Section 34.6.
299
300    No functions are provided specifically for the decimal type. An
301    application has to convert it to a numeric variable using a pgtypes
302    library function to do further processing.
303
304    Here is an example program handling numeric and decimal type variables.
305 #include <stdio.h>
306 #include <stdlib.h>
307 #include <pgtypes_numeric.h>
308
309 EXEC SQL WHENEVER SQLERROR STOP;
310
311 int
312 main(void)
313 {
314 EXEC SQL BEGIN DECLARE SECTION;
315     numeric *num;
316     numeric *num2;
317     decimal *dec;
318 EXEC SQL END DECLARE SECTION;
319
320     EXEC SQL CONNECT TO testdb;
321     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
322 MMIT;
323
324     num = PGTYPESnumeric_new();
325     dec = PGTYPESdecimal_new();
326
327     EXEC SQL SELECT 12.345::numeric(4,2), 23.456::decimal(4,2) INTO :num, :dec;
328
329     printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 0));
330     printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 1));
331     printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 2));
332
333     /* Convert decimal to numeric to show a decimal value. */
334     num2 = PGTYPESnumeric_new();
335     PGTYPESnumeric_from_decimal(dec, num2);
336
337     printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 0));
338     printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 1));
339     printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 2));
340
341     PGTYPESnumeric_free(num2);
342     PGTYPESdecimal_free(dec);
343     PGTYPESnumeric_free(num);
344
345     EXEC SQL COMMIT;
346     EXEC SQL DISCONNECT ALL;
347     return 0;
348 }
349
350 34.4.4.2.4. bytea #
351
352    The handling of the bytea type is similar to that of VARCHAR. The
353    definition on an array of type bytea is converted into a named struct
354    for every variable. A declaration like:
355 bytea var[180];
356
357    is converted into:
358 struct bytea_var { int len; char arr[180]; } var;
359
360    The member arr hosts binary format data. It can also handle '\0' as
361    part of data, unlike VARCHAR. The data is converted from/to hex format
362    and sent/received by ecpglib.
363
364 Note
365
366    bytea variable can be used only when bytea_output is set to hex.
367
368 34.4.4.3. Host Variables with Nonprimitive Types #
369
370    As a host variable you can also use arrays, typedefs, structs, and
371    pointers.
372
373 34.4.4.3.1. Arrays #
374
375    There are two use cases for arrays as host variables. The first is a
376    way to store some text string in char[] or VARCHAR[], as explained in
377    Section 34.4.4.1. The second use case is to retrieve multiple rows from
378    a query result without using a cursor. Without an array, to process a
379    query result consisting of multiple rows, it is required to use a
380    cursor and the FETCH command. But with array host variables, multiple
381    rows can be received at once. The length of the array has to be defined
382    to be able to accommodate all rows, otherwise a buffer overflow will
383    likely occur.
384
385    Following example scans the pg_database system table and shows all OIDs
386    and names of the available databases:
387 int
388 main(void)
389 {
390 EXEC SQL BEGIN DECLARE SECTION;
391     int dbid[8];
392     char dbname[8][16];
393     int i;
394 EXEC SQL END DECLARE SECTION;
395
396     memset(dbname, 0, sizeof(char)* 16 * 8);
397     memset(dbid, 0, sizeof(int) * 8);
398
399     EXEC SQL CONNECT TO testdb;
400     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
401 MMIT;
402
403     /* Retrieve multiple rows into arrays at once. */
404     EXEC SQL SELECT oid,datname INTO :dbid, :dbname FROM pg_database;
405
406     for (i = 0; i < 8; i++)
407         printf("oid=%d, dbname=%s\n", dbid[i], dbname[i]);
408
409     EXEC SQL COMMIT;
410     EXEC SQL DISCONNECT ALL;
411     return 0;
412 }
413
414    This example shows following result. (The exact values depend on local
415    circumstances.)
416 oid=1, dbname=template1
417 oid=11510, dbname=template0
418 oid=11511, dbname=postgres
419 oid=313780, dbname=testdb
420 oid=0, dbname=
421 oid=0, dbname=
422 oid=0, dbname=
423
424 34.4.4.3.2. Structures #
425
426    A structure whose member names match the column names of a query
427    result, can be used to retrieve multiple columns at once. The structure
428    enables handling multiple column values in a single host variable.
429
430    The following example retrieves OIDs, names, and sizes of the available
431    databases from the pg_database system table and using the
432    pg_database_size() function. In this example, a structure variable
433    dbinfo_t with members whose names match each column in the SELECT
434    result is used to retrieve one result row without putting multiple host
435    variables in the FETCH statement.
436 EXEC SQL BEGIN DECLARE SECTION;
437     typedef struct
438     {
439        int oid;
440        char datname[65];
441        long long int size;
442     } dbinfo_t;
443
444     dbinfo_t dbval;
445 EXEC SQL END DECLARE SECTION;
446
447     memset(&dbval, 0, sizeof(dbinfo_t));
448
449     EXEC SQL DECLARE cur1 CURSOR FOR SELECT oid, datname, pg_database_size(oid)
450 AS size FROM pg_database;
451     EXEC SQL OPEN cur1;
452
453     /* when end of result set reached, break out of while loop */
454     EXEC SQL WHENEVER NOT FOUND DO BREAK;
455
456     while (1)
457     {
458         /* Fetch multiple columns into one structure. */
459         EXEC SQL FETCH FROM cur1 INTO :dbval;
460
461         /* Print members of the structure. */
462         printf("oid=%d, datname=%s, size=%lld\n", dbval.oid, dbval.datname, dbva
463 l.size);
464     }
465
466     EXEC SQL CLOSE cur1;
467
468    This example shows following result. (The exact values depend on local
469    circumstances.)
470 oid=1, datname=template1, size=4324580
471 oid=11510, datname=template0, size=4243460
472 oid=11511, datname=postgres, size=4324580
473 oid=313780, datname=testdb, size=8183012
474
475    Structure host variables “absorb” as many columns as the structure as
476    fields. Additional columns can be assigned to other host variables. For
477    example, the above program could also be restructured like this, with
478    the size variable outside the structure:
479 EXEC SQL BEGIN DECLARE SECTION;
480     typedef struct
481     {
482        int oid;
483        char datname[65];
484     } dbinfo_t;
485
486     dbinfo_t dbval;
487     long long int size;
488 EXEC SQL END DECLARE SECTION;
489
490     memset(&dbval, 0, sizeof(dbinfo_t));
491
492     EXEC SQL DECLARE cur1 CURSOR FOR SELECT oid, datname, pg_database_size(oid)
493 AS size FROM pg_database;
494     EXEC SQL OPEN cur1;
495
496     /* when end of result set reached, break out of while loop */
497     EXEC SQL WHENEVER NOT FOUND DO BREAK;
498
499     while (1)
500     {
501         /* Fetch multiple columns into one structure. */
502         EXEC SQL FETCH FROM cur1 INTO :dbval, :size;
503
504         /* Print members of the structure. */
505         printf("oid=%d, datname=%s, size=%lld\n", dbval.oid, dbval.datname, size
506 );
507     }
508
509     EXEC SQL CLOSE cur1;
510
511 34.4.4.3.3. Typedefs #
512
513    Use the typedef keyword to map new types to already existing types.
514 EXEC SQL BEGIN DECLARE SECTION;
515     typedef char mychartype[40];
516     typedef long serial_t;
517 EXEC SQL END DECLARE SECTION;
518
519    Note that you could also use:
520 EXEC SQL TYPE serial_t IS long;
521
522    This declaration does not need to be part of a declare section; that
523    is, you can also write typedefs as normal C statements.
524
525    Any word you declare as a typedef cannot be used as an SQL keyword in
526    EXEC SQL commands later in the same program. For example, this won't
527    work:
528 EXEC SQL BEGIN DECLARE SECTION;
529     typedef int start;
530 EXEC SQL END DECLARE SECTION;
531 ...
532 EXEC SQL START TRANSACTION;
533
534    ECPG will report a syntax error for START TRANSACTION, because it no
535    longer recognizes START as an SQL keyword, only as a typedef. (If you
536    have such a conflict, and renaming the typedef seems impractical, you
537    could write the SQL command using dynamic SQL.)
538
539 Note
540
541    In PostgreSQL releases before v16, use of SQL keywords as typedef names
542    was likely to result in syntax errors associated with use of the
543    typedef itself, rather than use of the name as an SQL keyword. The new
544    behavior is less likely to cause problems when an existing ECPG
545    application is recompiled in a new PostgreSQL release with new
546    keywords.
547
548 34.4.4.3.4. Pointers #
549
550    You can declare pointers to the most common types. Note however that
551    you cannot use pointers as target variables of queries without
552    auto-allocation. See Section 34.7 for more information on
553    auto-allocation.
554
555 EXEC SQL BEGIN DECLARE SECTION;
556     int   *intp;
557     char **charp;
558 EXEC SQL END DECLARE SECTION;
559
560 34.4.5. Handling Nonprimitive SQL Data Types #
561
562    This section contains information on how to handle nonscalar and
563    user-defined SQL-level data types in ECPG applications. Note that this
564    is distinct from the handling of host variables of nonprimitive types,
565    described in the previous section.
566
567 34.4.5.1. Arrays #
568
569    Multi-dimensional SQL-level arrays are not directly supported in ECPG.
570    One-dimensional SQL-level arrays can be mapped into C array host
571    variables and vice-versa. However, when creating a statement ecpg does
572    not know the types of the columns, so that it cannot check if a C array
573    is input into a corresponding SQL-level array. When processing the
574    output of an SQL statement, ecpg has the necessary information and thus
575    checks if both are arrays.
576
577    If a query accesses elements of an array separately, then this avoids
578    the use of arrays in ECPG. Then, a host variable with a type that can
579    be mapped to the element type should be used. For example, if a column
580    type is array of integer, a host variable of type int can be used. Also
581    if the element type is varchar or text, a host variable of type char[]
582    or VARCHAR[] can be used.
583
584    Here is an example. Assume the following table:
585 CREATE TABLE t3 (
586     ii integer[]
587 );
588
589 testdb=> SELECT * FROM t3;
590      ii
591 -------------
592  {1,2,3,4,5}
593 (1 row)
594
595    The following example program retrieves the 4th element of the array
596    and stores it into a host variable of type int:
597 EXEC SQL BEGIN DECLARE SECTION;
598 int ii;
599 EXEC SQL END DECLARE SECTION;
600
601 EXEC SQL DECLARE cur1 CURSOR FOR SELECT ii[4] FROM t3;
602 EXEC SQL OPEN cur1;
603
604 EXEC SQL WHENEVER NOT FOUND DO BREAK;
605
606 while (1)
607 {
608     EXEC SQL FETCH FROM cur1 INTO :ii ;
609     printf("ii=%d\n", ii);
610 }
611
612 EXEC SQL CLOSE cur1;
613
614    This example shows the following result:
615 ii=4
616
617    To map multiple array elements to the multiple elements in an array
618    type host variables each element of array column and each element of
619    the host variable array have to be managed separately, for example:
620 EXEC SQL BEGIN DECLARE SECTION;
621 int ii_a[8];
622 EXEC SQL END DECLARE SECTION;
623
624 EXEC SQL DECLARE cur1 CURSOR FOR SELECT ii[1], ii[2], ii[3], ii[4] FROM t3;
625 EXEC SQL OPEN cur1;
626
627 EXEC SQL WHENEVER NOT FOUND DO BREAK;
628
629 while (1)
630 {
631     EXEC SQL FETCH FROM cur1 INTO :ii_a[0], :ii_a[1], :ii_a[2], :ii_a[3];
632     ...
633 }
634
635    Note again that
636 EXEC SQL BEGIN DECLARE SECTION;
637 int ii_a[8];
638 EXEC SQL END DECLARE SECTION;
639
640 EXEC SQL DECLARE cur1 CURSOR FOR SELECT ii FROM t3;
641 EXEC SQL OPEN cur1;
642
643 EXEC SQL WHENEVER NOT FOUND DO BREAK;
644
645 while (1)
646 {
647     /* WRONG */
648     EXEC SQL FETCH FROM cur1 INTO :ii_a;
649     ...
650 }
651
652    would not work correctly in this case, because you cannot map an array
653    type column to an array host variable directly.
654
655    Another workaround is to store arrays in their external string
656    representation in host variables of type char[] or VARCHAR[]. For more
657    details about this representation, see Section 8.15.2. Note that this
658    means that the array cannot be accessed naturally as an array in the
659    host program (without further processing that parses the text
660    representation).
661
662 34.4.5.2. Composite Types #
663
664    Composite types are not directly supported in ECPG, but an easy
665    workaround is possible. The available workarounds are similar to the
666    ones described for arrays above: Either access each attribute
667    separately or use the external string representation.
668
669    For the following examples, assume the following type and table:
670 CREATE TYPE comp_t AS (intval integer, textval varchar(32));
671 CREATE TABLE t4 (compval comp_t);
672 INSERT INTO t4 VALUES ( (256, 'PostgreSQL') );
673
674    The most obvious solution is to access each attribute separately. The
675    following program retrieves data from the example table by selecting
676    each attribute of the type comp_t separately:
677 EXEC SQL BEGIN DECLARE SECTION;
678 int intval;
679 varchar textval[33];
680 EXEC SQL END DECLARE SECTION;
681
682 /* Put each element of the composite type column in the SELECT list. */
683 EXEC SQL DECLARE cur1 CURSOR FOR SELECT (compval).intval, (compval).textval FROM
684  t4;
685 EXEC SQL OPEN cur1;
686
687 EXEC SQL WHENEVER NOT FOUND DO BREAK;
688
689 while (1)
690 {
691     /* Fetch each element of the composite type column into host variables. */
692     EXEC SQL FETCH FROM cur1 INTO :intval, :textval;
693
694     printf("intval=%d, textval=%s\n", intval, textval.arr);
695 }
696
697 EXEC SQL CLOSE cur1;
698
699    To enhance this example, the host variables to store values in the
700    FETCH command can be gathered into one structure. For more details
701    about the host variable in the structure form, see Section 34.4.4.3.2.
702    To switch to the structure, the example can be modified as below. The
703    two host variables, intval and textval, become members of the comp_t
704    structure, and the structure is specified on the FETCH command.
705 EXEC SQL BEGIN DECLARE SECTION;
706 typedef struct
707 {
708     int intval;
709     varchar textval[33];
710 } comp_t;
711
712 comp_t compval;
713 EXEC SQL END DECLARE SECTION;
714
715 /* Put each element of the composite type column in the SELECT list. */
716 EXEC SQL DECLARE cur1 CURSOR FOR SELECT (compval).intval, (compval).textval FROM
717  t4;
718 EXEC SQL OPEN cur1;
719
720 EXEC SQL WHENEVER NOT FOUND DO BREAK;
721
722 while (1)
723 {
724     /* Put all values in the SELECT list into one structure. */
725     EXEC SQL FETCH FROM cur1 INTO :compval;
726
727     printf("intval=%d, textval=%s\n", compval.intval, compval.textval.arr);
728 }
729
730 EXEC SQL CLOSE cur1;
731
732    Although a structure is used in the FETCH command, the attribute names
733    in the SELECT clause are specified one by one. This can be enhanced by
734    using a * to ask for all attributes of the composite type value.
735 ...
736 EXEC SQL DECLARE cur1 CURSOR FOR SELECT (compval).* FROM t4;
737 EXEC SQL OPEN cur1;
738
739 EXEC SQL WHENEVER NOT FOUND DO BREAK;
740
741 while (1)
742 {
743     /* Put all values in the SELECT list into one structure. */
744     EXEC SQL FETCH FROM cur1 INTO :compval;
745
746     printf("intval=%d, textval=%s\n", compval.intval, compval.textval.arr);
747 }
748 ...
749
750    This way, composite types can be mapped into structures almost
751    seamlessly, even though ECPG does not understand the composite type
752    itself.
753
754    Finally, it is also possible to store composite type values in their
755    external string representation in host variables of type char[] or
756    VARCHAR[]. But that way, it is not easily possible to access the fields
757    of the value from the host program.
758
759 34.4.5.3. User-Defined Base Types #
760
761    New user-defined base types are not directly supported by ECPG. You can
762    use the external string representation and host variables of type
763    char[] or VARCHAR[], and this solution is indeed appropriate and
764    sufficient for many types.
765
766    Here is an example using the data type complex from the example in
767    Section 36.13. The external string representation of that type is
768    (%f,%f), which is defined in the functions complex_in() and
769    complex_out() functions in Section 36.13. The following example inserts
770    the complex type values (1,1) and (3,3) into the columns a and b, and
771    select them from the table after that.
772 EXEC SQL BEGIN DECLARE SECTION;
773     varchar a[64];
774     varchar b[64];
775 EXEC SQL END DECLARE SECTION;
776
777     EXEC SQL INSERT INTO test_complex VALUES ('(1,1)', '(3,3)');
778
779     EXEC SQL DECLARE cur1 CURSOR FOR SELECT a, b FROM test_complex;
780     EXEC SQL OPEN cur1;
781
782     EXEC SQL WHENEVER NOT FOUND DO BREAK;
783
784     while (1)
785     {
786         EXEC SQL FETCH FROM cur1 INTO :a, :b;
787         printf("a=%s, b=%s\n", a.arr, b.arr);
788     }
789
790     EXEC SQL CLOSE cur1;
791
792    This example shows following result:
793 a=(1,1), b=(3,3)
794
795    Another workaround is avoiding the direct use of the user-defined types
796    in ECPG and instead create a function or cast that converts between the
797    user-defined type and a primitive type that ECPG can handle. Note,
798    however, that type casts, especially implicit ones, should be
799    introduced into the type system very carefully.
800
801    For example,
802 CREATE FUNCTION create_complex(r double, i double) RETURNS complex
803 LANGUAGE SQL
804 IMMUTABLE
805 AS $$ SELECT $1 * complex '(1,0')' + $2 * complex '(0,1)' $$;
806
807    After this definition, the following
808 EXEC SQL BEGIN DECLARE SECTION;
809 double a, b, c, d;
810 EXEC SQL END DECLARE SECTION;
811
812 a = 1;
813 b = 2;
814 c = 3;
815 d = 4;
816
817 EXEC SQL INSERT INTO test_complex VALUES (create_complex(:a, :b), create_complex
818 (:c, :d));
819
820    has the same effect as
821 EXEC SQL INSERT INTO test_complex VALUES ('(1,2)', '(3,4)');
822
823 34.4.6. Indicators #
824
825    The examples above do not handle null values. In fact, the retrieval
826    examples will raise an error if they fetch a null value from the
827    database. To be able to pass null values to the database or retrieve
828    null values from the database, you need to append a second host
829    variable specification to each host variable that contains data. This
830    second host variable is called the indicator and contains a flag that
831    tells whether the datum is null, in which case the value of the real
832    host variable is ignored. Here is an example that handles the retrieval
833    of null values correctly:
834 EXEC SQL BEGIN DECLARE SECTION;
835 VARCHAR val;
836 int val_ind;
837 EXEC SQL END DECLARE SECTION:
838
839  ...
840
841 EXEC SQL SELECT b INTO :val :val_ind FROM test1;
842
843    The indicator variable val_ind will be zero if the value was not null,
844    and it will be negative if the value was null. (See Section 34.16 to
845    enable Oracle-specific behavior.)
846
847    The indicator has another function: if the indicator value is positive,
848    it means that the value is not null, but it was truncated when it was
849    stored in the host variable.
850
851    If the argument -r no_indicator is passed to the preprocessor ecpg, it
852    works in “no-indicator” mode. In no-indicator mode, if no indicator
853    variable is specified, null values are signaled (on input and output)
854    for character string types as empty string and for integer types as the
855    lowest possible value for type (for example, INT_MIN for int).