-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.pgc
469 lines (368 loc) · 13.5 KB
/
test.pgc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sqlda.h>
#include <pgtypes_date.h>
#include <pgtypes_numeric.h>
sqlda_t *sqlda1;
sqlda_t *sqlda2;
EXEC SQL BEGIN DECLARE SECTION;
char query[20000],x[20000];
char c,*ans;
const char *password, *username, *name, *search;
int i=0,intval,cnt=0;
long long int longlongval;
float realval;
date dateval;
numeric numericval;
short shortval;
EXEC SQL END DECLARE SECTION;
EXEC SQL WHENEVER SQLERROR CALL print_sqlca();
void print_sqlca()
{
fprintf(stderr, "==== sqlca ====\n");
fprintf(stderr, "sqlcode: %ld\n", sqlca.sqlcode);
fprintf(stderr, "sqlerrm.sqlerrml: %d\n", sqlca.sqlerrm.sqlerrml);
fprintf(stderr, "sqlerrm.sqlerrmc: %s\n", sqlca.sqlerrm.sqlerrmc);
fprintf(stderr, "sqlerrd: %ld %ld %ld %ld %ld %ld\n", sqlca.sqlerrd[0],sqlca.sqlerrd[1],sqlca.sqlerrd[2],
sqlca.sqlerrd[3],sqlca.sqlerrd[4],sqlca.sqlerrd[5]);
fprintf(stderr, "sqlwarn: %d %d %d %d %d %d %d %d\n", sqlca.sqlwarn[0], sqlca.sqlwarn[1], sqlca.sqlwarn[2],
sqlca.sqlwarn[3], sqlca.sqlwarn[4], sqlca.sqlwarn[5],
sqlca.sqlwarn[6], sqlca.sqlwarn[7]);
fprintf(stderr, "sqlstate: %5s\n", sqlca.sqlstate);
fprintf(stderr, "===============\n");
}
void executequery(){
printf("\nQuery : ");
scanf(" %[^\n]",query);
int firstTime = 1;
EXEC SQL PREPARE eq FROM :query;
EXEC SQL EXECUTE eq;
if(sqlca.sqlcode==0)
{
fprintf(stderr, "sucessful\n");
}
EXEC SQL COMMIT;
}
void executequery1(){
printf("\nQuery : ");
scanf(" %[^\n]",query);
int firstTime = 1;
printf("\nPrice per\tCity\t\tArea\t\tSales Type\n");
printf("square feet\n");
EXEC SQL PREPARE eq FROM :query;
EXEC SQL DECLARE cur1 CURSOR FOR eq;
EXEC SQL OPEN cur1;
while(sqlca.sqlcode==0){
EXEC SQL FETCH NEXT FROM cur1 INTO DESCRIPTOR sqlda1;
sqlda_t *cur_sqlda;
for (cur_sqlda = sqlda1;cur_sqlda != NULL;cur_sqlda = cur_sqlda->desc_next){
int i;
char name_buf[1024];
char var_buf[1024];
if(sqlca.sqlcode != 0)
break;
point: for (i = 0; i < cur_sqlda->sqld; i++){
sqlvar_t v = cur_sqlda->sqlvar[i];
char *sqldata = v.sqldata;
short sqllen = v.sqllen;
strncpy(name_buf, v.sqlname.data, v.sqlname.length);
name_buf[v.sqlname.length] = '\0';
switch (v.sqltype){
case ECPGt_char:
memset(&var_buf, 0, sizeof(var_buf));
memcpy(&var_buf, sqldata, (sizeof(var_buf)<=sqllen ? sizeof(var_buf)-1 : sqllen) );
break;
case ECPGt_int: /* integer */
memcpy(&intval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", intval);
break;
case ECPGt_long_long: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
case ECPGt_float: /* real */
memcpy(&realval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%.2f", realval);
break;
case ECPGt_short: /* sort */
memcpy(&shortval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", shortval);
break;
case 7: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
default:
{
int i;
memset(var_buf, 0, sizeof(var_buf));
for (i = 0; i < sqllen; i++){
char tmpbuf[16];
snprintf(tmpbuf, sizeof(tmpbuf), "%02x ", (unsigned char) sqldata[i]);
strncat(var_buf, tmpbuf, sizeof(var_buf));
}
}
break;
}
printf("%s \t",var_buf);
}
printf("\n");
}
}
EXEC SQL CLOSE cur1;
EXEC SQL COMMIT;
}
void executequery2(){
printf("\nQuery : ");
scanf(" %[^\n]",query);
int firstTime = 1;
printf("\nSellerId\tNo.ofBuyer\tCustomers \tPercentage\n");
EXEC SQL PREPARE eq FROM :query;
EXEC SQL DECLARE cur2 CURSOR FOR eq;
EXEC SQL OPEN cur2;
while(sqlca.sqlcode==0){
EXEC SQL FETCH NEXT FROM cur2 INTO DESCRIPTOR sqlda1;
sqlda_t *cur_sqlda;
for (cur_sqlda = sqlda1;cur_sqlda != NULL;cur_sqlda = cur_sqlda->desc_next){
int i;
char name_buf[1024];
char var_buf[1024];
if(sqlca.sqlcode != 0)
break;
point: for (i = 0; i < cur_sqlda->sqld; i++){
sqlvar_t v = cur_sqlda->sqlvar[i];
char *sqldata = v.sqldata;
short sqllen = v.sqllen;
strncpy(name_buf, v.sqlname.data, v.sqlname.length);
name_buf[v.sqlname.length] = '\0';
switch (v.sqltype){
case ECPGt_char:
memset(&var_buf, 0, sizeof(var_buf));
memcpy(&var_buf, sqldata, (sizeof(var_buf)<=sqllen ? sizeof(var_buf)-1 : sqllen) );
break;
case ECPGt_int: /* integer */
memcpy(&intval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", intval);
break;
case ECPGt_long_long: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
case ECPGt_float: /* real */
memcpy(&realval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%.2f", realval);
break;
case ECPGt_short: /* sort */
memcpy(&shortval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", shortval);
break;
case 7: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
default:
{
int i;
memset(var_buf, 0, sizeof(var_buf));
for (i = 0; i < sqllen; i++){
char tmpbuf[16];
snprintf(tmpbuf, sizeof(tmpbuf), "%02x ", (unsigned char) sqldata[i]);
strncat(var_buf, tmpbuf, sizeof(var_buf));
}
}
break;
}
printf("%s \t\t",var_buf);
}
printf("\n");
}
}
EXEC SQL CLOSE cur2;
EXEC SQL COMMIT;
}
void executequery3(){
printf("\nQuery : ");
scanf(" %[^\n]",query);
int firstTime = 1;
printf("\nSellerId\tMin Age \tMax Age\n");
EXEC SQL PREPARE eq FROM :query;
EXEC SQL DECLARE cur3 CURSOR FOR eq;
EXEC SQL OPEN cur3;
while(sqlca.sqlcode==0){
EXEC SQL FETCH NEXT FROM cur3 INTO DESCRIPTOR sqlda1;
sqlda_t *cur_sqlda;
for (cur_sqlda = sqlda1;cur_sqlda != NULL;cur_sqlda = cur_sqlda->desc_next){
int i;
char name_buf[1024];
char var_buf[1024];
if(sqlca.sqlcode != 0)
break;
point: for (i = 0; i < cur_sqlda->sqld; i++){
sqlvar_t v = cur_sqlda->sqlvar[i];
char *sqldata = v.sqldata;
short sqllen = v.sqllen;
strncpy(name_buf, v.sqlname.data, v.sqlname.length);
name_buf[v.sqlname.length] = '\0';
switch (v.sqltype){
case ECPGt_char:
memset(&var_buf, 0, sizeof(var_buf));
memcpy(&var_buf, sqldata, (sizeof(var_buf)<=sqllen ? sizeof(var_buf)-1 : sqllen) );
break;
case ECPGt_int: /* integer */
memcpy(&intval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", intval);
break;
case ECPGt_long_long: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
case ECPGt_float: /* real */
memcpy(&realval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%.2f", realval);
break;
case ECPGt_short: /* sort */
memcpy(&shortval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", shortval);
break;
case 7: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
default:
{
int i;
memset(var_buf, 0, sizeof(var_buf));
for (i = 0; i < sqllen; i++){
char tmpbuf[16];
snprintf(tmpbuf, sizeof(tmpbuf), "%02x ", (unsigned char) sqldata[i]);
strncat(var_buf, tmpbuf, sizeof(var_buf));
}
}
break;
}
printf("%s\t \t",var_buf);
}
printf("\n");
}
}
EXEC SQL CLOSE cur3;
EXEC SQL COMMIT;
}
void executequery4(){
printf("\nQuery : ");
scanf(" %[^\n]",query);
int firstTime = 1;
printf("\nDifference\tDifference\n");
printf("from min value\tfrom max value\n");
EXEC SQL PREPARE eq FROM :query;
EXEC SQL DECLARE cur4 CURSOR FOR eq;
EXEC SQL OPEN cur4;
while(sqlca.sqlcode==0){
EXEC SQL FETCH NEXT FROM cur4 INTO DESCRIPTOR sqlda1;
sqlda_t *cur_sqlda;
for (cur_sqlda = sqlda1;cur_sqlda != NULL;cur_sqlda = cur_sqlda->desc_next){
int i;
char name_buf[1024];
char var_buf[1024];
if(sqlca.sqlcode != 0)
break;
point: for (i = 0; i < cur_sqlda->sqld; i++){
sqlvar_t v = cur_sqlda->sqlvar[i];
char *sqldata = v.sqldata;
short sqllen = v.sqllen;
strncpy(name_buf, v.sqlname.data, v.sqlname.length);
name_buf[v.sqlname.length] = '\0';
switch (v.sqltype){
case ECPGt_char:
memset(&var_buf, 0, sizeof(var_buf));
memcpy(&var_buf, sqldata, (sizeof(var_buf)<=sqllen ? sizeof(var_buf)-1 : sqllen) );
break;
case ECPGt_int: /* integer */
memcpy(&intval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", intval);
break;
case ECPGt_long_long: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
case ECPGt_float: /* real */
memcpy(&realval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%.2f", realval);
break;
case ECPGt_short: /* sort */
memcpy(&shortval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%d", shortval);
break;
case 7: /* bigint */
memcpy(&longlongval, sqldata, sqllen);
snprintf(var_buf, sizeof(var_buf), "%lld", longlongval);
break;
default:
{
int i;
memset(var_buf, 0, sizeof(var_buf));
for (i = 0; i < sqllen; i++){
char tmpbuf[16];
snprintf(tmpbuf, sizeof(tmpbuf), "%02x ", (unsigned char) sqldata[i]);
strncat(var_buf, tmpbuf, sizeof(var_buf));
}
}
break;
}
printf("%s\t",var_buf);
}
printf("\n");
}
}
EXEC SQL CLOSE cur4;
EXEC SQL COMMIT;
}
int main()
{
username = "201601218";
name = "201601218@10.100.71.21";
password = "yash";
search = "realestate1";
EXEC SQL CONNECT TO :name USER :username USING :password;
if(sqlca.sqlcode==0)
fprintf(stderr, "Connection Successful\n");
else
{
fprintf(stderr,"Connection couldn't be established\n");
exit(1);
}
EXEC SQL set search_path to :search;
if(sqlca.sqlcode==0)
fprintf(stderr,"Search_path Successfully set\n");
else
{
fprintf(stderr,"No such path available\n");
exit(1);
}
while(1)
{
printf("1. Price per sq ft. in your area\n2. Find the number of buyers whose budget is greater than the average value of seller's properties \n3. Find your target age group \n4. Find the price range in your city according to your budget\n5. Query\n6. Exit\n");
scanf("%d",&cnt);
if(cnt==1){
executequery1();
}
else if(cnt==2){
executequery2();
}
else if(cnt==3){
executequery3();
}
else if(cnt==4){
executequery4();
}
else if(cnt==5)
{
executequery();
}
else{
break;
}
}
}