|
1 |
| -#include <stdio.h> |
2 |
| - |
3 |
| -exec sql include header_test; |
4 |
| - |
5 |
| -exec sql type str is varchar[10]; |
6 |
| - |
7 |
| -int |
8 |
| -main () |
9 |
| -{ |
10 |
| - typedef struct { long born; short age; } birthinfo; |
11 |
| - exec sql type birthinfo is struct { long born; short age; }; |
12 |
| -exec sql begin declare section; |
13 |
| - struct personal_struct { str name; |
14 |
| - birthinfo birth; |
15 |
| - } personal; |
16 |
| - struct personal_indicator { int ind_name; |
17 |
| - birthinfo ind_birth; |
18 |
| - } ind_personal; |
19 |
| - int *ind_married = NULL; |
20 |
| - int children; |
21 |
| - int ind_children; |
22 |
| - str *married = NULL; |
23 |
| - char *testname="Petra"; |
24 |
| - char *query="select name, born, age, married, children from meskes where name = :var1"; |
25 |
| -exec sql end declare section; |
26 |
| - |
27 |
| - exec sql declare cur cursor for |
28 |
| - select name, born, age, married, children from meskes; |
29 |
| - |
30 |
| - char msg[128], command[128]; |
31 |
| - FILE *dbgs; |
32 |
| - |
33 |
| - if ((dbgs = fopen("log", "w")) != NULL) |
34 |
| - ECPGdebug(1, dbgs); |
35 |
| - |
36 |
| - strcpy(msg, "connect"); |
37 |
| - exec sql connect to unix:postgresql://localhost:5432/mm; |
38 |
| - |
39 |
| - strcpy(msg, "create"); |
40 |
| - exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer); |
41 |
| - |
42 |
| - strcpy(msg, "insert"); |
43 |
| - exec sql insert into meskes(name, married, children) values ('Petra', '19900404', 3); |
44 |
| - exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 33, '19900404', 3); |
45 |
| - exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 8); |
46 |
| - exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 5); |
47 |
| - exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 1); |
48 |
| - |
49 |
| - strcpy(msg, "commit"); |
50 |
| - exec sql commit; |
51 |
| - |
52 |
| - strcpy(msg, "open"); |
53 |
| - exec sql open cur; |
54 |
| - |
55 |
| - exec sql whenever not found do break; |
56 |
| - |
57 |
| - while (1) { |
58 |
| - strcpy(msg, "fetch"); |
59 |
| - exec sql fetch in cur into :personal:ind_personal, :married:ind_married, :children:ind_children; |
60 |
| - printf("%8.8s", personal.name.arr); |
61 |
| - if (ind_personal.ind_birth.born >= 0) |
62 |
| - printf(", born %d", personal.birth.born); |
63 |
| - if (ind_personal.ind_birth.age >= 0) |
64 |
| - printf(", age = %d", personal.birth.age); |
65 |
| - if (ind_married >= 0) |
66 |
| - printf(", married %10.10s", married->arr); |
67 |
| - if (ind_children >= 0) |
68 |
| - printf(", children = %d", children); |
69 |
| - putchar('\n'); |
70 |
| - |
71 |
| - free(married); |
72 |
| - married = NULL; |
73 |
| - } |
74 |
| - |
75 |
| - strcpy(msg, "close"); |
76 |
| - exec sql close cur; |
77 |
| - |
78 |
| - /* and now the same query with prepare */ |
79 |
| - exec sql prepare MM from :query; |
80 |
| - exec sql declare prep cursor for MM; |
81 |
| - |
82 |
| - strcpy(msg, "open"); |
83 |
| - exec sql open prep using :testname; |
84 |
| - |
85 |
| - exec sql whenever not found do break; |
86 |
| - |
87 |
| - while (1) { |
88 |
| - strcpy(msg, "fetch"); |
89 |
| - exec sql fetch in prep into :personal:ind_personal, :married:ind_married, :children:ind_children; |
90 |
| - printf("%8.8s", personal.name.arr); |
91 |
| - if (ind_personal.ind_birth.born >= 0) |
92 |
| - printf(", born %d", personal.birth.born); |
93 |
| - if (ind_personal.ind_birth.age >= 0) |
94 |
| - printf(", age = %d", personal.birth.age); |
95 |
| - if (ind_married >= 0) |
96 |
| - printf(", married %10.10s", married->arr); |
97 |
| - if (ind_children >= 0) |
98 |
| - printf(", children = %d", children); |
99 |
| - putchar('\n'); |
100 |
| - } |
101 |
| - |
102 |
| - free(married); |
103 |
| - |
104 |
| - strcpy(msg, "close"); |
105 |
| - exec sql close prep; |
106 |
| - |
107 |
| - strcpy(msg, "drop"); |
108 |
| - exec sql drop table meskes; |
109 |
| - |
110 |
| - strcpy(msg, "commit"); |
111 |
| - exec sql commit; |
112 |
| - |
113 |
| - strcpy(msg, "disconnect"); |
114 |
| - |
115 |
| - exec sql disconnect; |
116 |
| - if (dbgs != NULL) |
117 |
| - fclose(dbgs); |
118 |
| - |
119 |
| - return (0); |
120 |
| -} |
121 |
| -#include <stdio.h> |
122 |
| - |
123 | 1 | exec sql include header_test;
|
124 | 2 |
|
125 | 3 | exec sql type str is varchar[10];
|
|
0 commit comments