| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include <limits.h> |
| 4 |
|
| 5 |
exec sql include ../regression; |
| 6 |
|
| 7 |
exec sql include sqlda.h; |
| 8 |
exec sql include sqltypes.h; |
| 9 |
|
| 10 |
exec sql whenever sqlerror stop; |
| 11 |
|
| 12 |
/* These shouldn't be under DECLARE SECTION */ |
| 13 |
sqlda_t *inp_sqlda, *outp_sqlda; |
| 14 |
|
| 15 |
static void |
| 16 |
dump_sqlda(sqlda_t *sqlda) |
| 17 |
{ |
| 18 |
int i; |
| 19 |
|
| 20 |
if (sqlda == NULL) |
| 21 |
{ |
| 22 |
printf("dump_sqlda called with NULL sqlda\n"); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
for (i = 0; i < sqlda->sqld; i++) |
| 27 |
{ |
| 28 |
if (sqlda->sqlvar[i].sqlind && *(sqlda->sqlvar[i].sqlind) == -1) |
| 29 |
printf("name sqlda descriptor: '%s' value NULL'\n", sqlda->sqlvar[i].sqlname); |
| 30 |
else |
| 31 |
switch (sqlda->sqlvar[i].sqltype) |
| 32 |
{ |
| 33 |
case SQLCHAR: |
| 34 |
printf("name sqlda descriptor: '%s' value '%s'\n", sqlda->sqlvar[i].sqlname, sqlda->sqlvar[i].sqldata); |
| 35 |
break; |
| 36 |
case SQLINT: |
| 37 |
printf("name sqlda descriptor: '%s' value %d\n", sqlda->sqlvar[i].sqlname, *(int *)sqlda->sqlvar[i].sqldata); |
| 38 |
break; |
| 39 |
case SQLFLOAT: |
| 40 |
printf("name sqlda descriptor: '%s' value %f\n", sqlda->sqlvar[i].sqlname, *(double *)sqlda->sqlvar[i].sqldata); |
| 41 |
break; |
| 42 |
case SQLDECIMAL: |
| 43 |
{ |
| 44 |
char val[64]; |
| 45 |
dectoasc((dec_t *)sqlda->sqlvar[i].sqldata, val, 64, -1); |
| 46 |
printf("name sqlda descriptor: '%s' value DECIMAL '%s'\n", sqlda->sqlvar[i].sqlname, val); |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
int |
| 54 |
main (void) |
| 55 |
{ |
| 56 |
exec sql begin declare section; |
| 57 |
char *stmt1 = "SELECT * FROM t1"; |
| 58 |
char *stmt2 = "SELECT * FROM t1 WHERE id = ?"; |
| 59 |
int rec; |
| 60 |
int id; |
| 61 |
exec sql end declare section; |
| 62 |
|
| 63 |
char msg[128]; |
| 64 |
|
| 65 |
ECPGdebug(1, stderr); |
| 66 |
|
| 67 |
strcpy(msg, "connect"); |
| 68 |
exec sql connect to REGRESSDB1 as regress1; |
| 69 |
|
| 70 |
strcpy(msg, "set"); |
| 71 |
exec sql set datestyle to iso; |
| 72 |
|
| 73 |
strcpy(msg, "create"); |
| 74 |
exec sql create table t1( |
| 75 |
id integer, |
| 76 |
t text, |
| 77 |
d1 numeric, |
| 78 |
d2 float8, |
| 79 |
c char(10)); |
| 80 |
|
| 81 |
strcpy(msg, "insert"); |
| 82 |
exec sql insert into t1 values |
| 83 |
(1, 'a', 1.0, 1, 'a'), |
| 84 |
(2, null, null, null, null), |
| 85 |
(4, 'd', 4.0, 4, 'd'); |
| 86 |
|
| 87 |
strcpy(msg, "commit"); |
| 88 |
exec sql commit; |
| 89 |
|
| 90 |
/* SQLDA test for getting all records from a table */ |
| 91 |
|
| 92 |
outp_sqlda = NULL; |
| 93 |
|
| 94 |
strcpy(msg, "prepare"); |
| 95 |
exec sql prepare st_id1 from :stmt1; |
| 96 |
|
| 97 |
strcpy(msg, "declare"); |
| 98 |
exec sql declare mycur1 cursor for st_id1; |
| 99 |
|
| 100 |
strcpy(msg, "open"); |
| 101 |
exec sql open mycur1; |
| 102 |
|
| 103 |
exec sql whenever not found do break; |
| 104 |
|
| 105 |
rec = 0; |
| 106 |
while (1) |
| 107 |
{ |
| 108 |
strcpy(msg, "fetch"); |
| 109 |
exec sql fetch 1 from mycur1 into descriptor outp_sqlda; |
| 110 |
|
| 111 |
printf("FETCH RECORD %d\n", ++rec); |
| 112 |
dump_sqlda(outp_sqlda); |
| 113 |
} |
| 114 |
|
| 115 |
exec sql whenever not found continue; |
| 116 |
|
| 117 |
strcpy(msg, "close"); |
| 118 |
exec sql close mycur1; |
| 119 |
|
| 120 |
strcpy(msg, "deallocate"); |
| 121 |
exec sql deallocate prepare st_id1; |
| 122 |
|
| 123 |
free(outp_sqlda); |
| 124 |
|
| 125 |
/* SQLDA test for getting all records from a table |
| 126 |
using the Informix-specific FETCH ... USING DESCRIPTOR |
| 127 |
*/ |
| 128 |
|
| 129 |
outp_sqlda = NULL; |
| 130 |
|
| 131 |
strcpy(msg, "prepare"); |
| 132 |
exec sql prepare st_id2 from :stmt1; |
| 133 |
|
| 134 |
strcpy(msg, "declare"); |
| 135 |
exec sql declare mycur2 cursor for st_id2; |
| 136 |
|
| 137 |
strcpy(msg, "open"); |
| 138 |
exec sql open mycur2; |
| 139 |
|
| 140 |
exec sql whenever not found do break; |
| 141 |
|
| 142 |
rec = 0; |
| 143 |
while (1) |
| 144 |
{ |
| 145 |
strcpy(msg, "fetch"); |
| 146 |
exec sql fetch from mycur2 using descriptor outp_sqlda; |
| 147 |
|
| 148 |
printf("FETCH RECORD %d\n", ++rec); |
| 149 |
dump_sqlda(outp_sqlda); |
| 150 |
} |
| 151 |
|
| 152 |
exec sql whenever not found continue; |
| 153 |
|
| 154 |
strcpy(msg, "close"); |
| 155 |
exec sql close mycur2; |
| 156 |
|
| 157 |
strcpy(msg, "deallocate"); |
| 158 |
exec sql deallocate prepare st_id2; |
| 159 |
|
| 160 |
free(outp_sqlda); |
| 161 |
|
| 162 |
/* SQLDA test for getting one record using an input descriptor */ |
| 163 |
|
| 164 |
/* Input sqlda has to be built manually */ |
| 165 |
inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t)); |
| 166 |
memset(inp_sqlda, 0, sizeof(sqlda_t)); |
| 167 |
inp_sqlda->sqld = 1; |
| 168 |
inp_sqlda->sqlvar = malloc(sizeof(sqlvar_t)); |
| 169 |
memset(inp_sqlda->sqlvar, 0, sizeof(sqlvar_t)); |
| 170 |
|
| 171 |
inp_sqlda->sqlvar[0].sqltype = SQLINT; |
| 172 |
inp_sqlda->sqlvar[0].sqldata = (char *)&id; |
| 173 |
|
| 174 |
printf("EXECUTE RECORD 4\n"); |
| 175 |
|
| 176 |
id = 4; |
| 177 |
|
| 178 |
outp_sqlda = NULL; |
| 179 |
|
| 180 |
strcpy(msg, "prepare"); |
| 181 |
exec sql prepare st_id3 FROM :stmt2; |
| 182 |
|
| 183 |
strcpy(msg, "execute"); |
| 184 |
exec sql execute st_id3 using descriptor inp_sqlda into descriptor outp_sqlda; |
| 185 |
|
| 186 |
dump_sqlda(outp_sqlda); |
| 187 |
|
| 188 |
strcpy(msg, "deallocate"); |
| 189 |
exec sql deallocate prepare st_id3; |
| 190 |
|
| 191 |
free(inp_sqlda->sqlvar); |
| 192 |
free(inp_sqlda); |
| 193 |
free(outp_sqlda); |
| 194 |
|
| 195 |
/* SQLDA test for getting one record using an input descriptor |
| 196 |
* on a named connection |
| 197 |
*/ |
| 198 |
|
| 199 |
exec sql connect to REGRESSDB1 as con2; |
| 200 |
|
| 201 |
/* Input sqlda has to be built manually */ |
| 202 |
inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t)); |
| 203 |
memset(inp_sqlda, 0, sizeof(sqlda_t)); |
| 204 |
inp_sqlda->sqld = 1; |
| 205 |
inp_sqlda->sqlvar = malloc(sizeof(sqlvar_t)); |
| 206 |
memset(inp_sqlda->sqlvar, 0, sizeof(sqlvar_t)); |
| 207 |
|
| 208 |
inp_sqlda->sqlvar[0].sqltype = SQLINT; |
| 209 |
inp_sqlda->sqlvar[0].sqldata = (char *)&id; |
| 210 |
|
| 211 |
printf("EXECUTE RECORD 4\n"); |
| 212 |
|
| 213 |
id = 4; |
| 214 |
|
| 215 |
outp_sqlda = NULL; |
| 216 |
|
| 217 |
strcpy(msg, "prepare"); |
| 218 |
exec sql at con2 prepare st_id4 FROM :stmt2; |
| 219 |
|
| 220 |
strcpy(msg, "execute"); |
| 221 |
exec sql at con2 execute st_id4 using descriptor inp_sqlda into descriptor outp_sqlda; |
| 222 |
|
| 223 |
dump_sqlda(outp_sqlda); |
| 224 |
|
| 225 |
strcpy(msg, "commit"); |
| 226 |
exec sql at con2 commit; |
| 227 |
|
| 228 |
strcpy(msg, "deallocate"); |
| 229 |
exec sql deallocate prepare st_id4; |
| 230 |
|
| 231 |
free(inp_sqlda->sqlvar); |
| 232 |
free(inp_sqlda); |
| 233 |
free(outp_sqlda); |
| 234 |
|
| 235 |
strcpy(msg, "disconnect"); |
| 236 |
exec sql disconnect con2; |
| 237 |
|
| 238 |
/* End test */ |
| 239 |
|
| 240 |
strcpy(msg, "drop"); |
| 241 |
exec sql drop table t1; |
| 242 |
|
| 243 |
strcpy(msg, "commit"); |
| 244 |
exec sql commit; |
| 245 |
|
| 246 |
strcpy(msg, "disconnect"); |
| 247 |
exec sql disconnect; |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |