]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-sql-type.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-sql-type.txt
1
2 TYPE
3
4    TYPE — define a new data type
5
6 Synopsis
7
8 TYPE type_name IS ctype
9
10 Description
11
12    The TYPE command defines a new C type. It is equivalent to putting a
13    typedef into a declare section.
14
15    This command is only recognized when ecpg is run with the -c option.
16
17 Parameters
18
19    type_name #
20           The name for the new type. It must be a valid C type name.
21
22    ctype #
23           A C type specification.
24
25 Examples
26
27 EXEC SQL TYPE customer IS
28     struct
29     {
30         varchar name[50];
31         int     phone;
32     };
33
34 EXEC SQL TYPE cust_ind IS
35     struct ind
36     {
37         short   name_ind;
38         short   phone_ind;
39     };
40
41 EXEC SQL TYPE c IS char reference;
42 EXEC SQL TYPE ind IS union { int integer; short smallint; };
43 EXEC SQL TYPE intarray IS int[AMOUNT];
44 EXEC SQL TYPE str IS varchar[BUFFERSIZ];
45 EXEC SQL TYPE string IS char[11];
46
47    Here is an example program that uses EXEC SQL TYPE:
48 EXEC SQL WHENEVER SQLERROR SQLPRINT;
49
50 EXEC SQL TYPE tt IS
51     struct
52     {
53         varchar v[256];
54         int     i;
55     };
56
57 EXEC SQL TYPE tt_ind IS
58     struct ind {
59         short   v_ind;
60         short   i_ind;
61     };
62
63 int
64 main(void)
65 {
66 EXEC SQL BEGIN DECLARE SECTION;
67     tt t;
68     tt_ind t_ind;
69 EXEC SQL END DECLARE SECTION;
70
71     EXEC SQL CONNECT TO testdb AS con1;
72     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
73 MMIT;
74
75     EXEC SQL SELECT current_database(), 256 INTO :t:t_ind LIMIT 1;
76
77     printf("t.v = %s\n", t.v.arr);
78     printf("t.i = %d\n", t.i);
79
80     printf("t_ind.v_ind = %d\n", t_ind.v_ind);
81     printf("t_ind.i_ind = %d\n", t_ind.i_ind);
82
83     EXEC SQL DISCONNECT con1;
84
85     return 0;
86 }
87
88    The output from this program looks like this:
89 t.v = testdb
90 t.i = 256
91 t_ind.v_ind = 0
92 t_ind.i_ind = 0
93
94 Compatibility
95
96    The TYPE command is a PostgreSQL extension.