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