1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // cgwtest.c
4 // ---------
5 // Cego C wrapper test program
6 //
7 // Design and Implementation by Bjoern Lemke
8 //
9 // (C)opyright 2009 Bjoern Lemke
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; see the file COPYING.  If not, write to
23 // the Free Software Foundation, 59 Temple Place - Suite 330,
24 // Boston, MA 02111-1307, USA.
25 //
26 ///////////////////////////////////////////////////////////////////////////////
27 
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 
32 #include "cgwrap.h"
33 
34 void connect_test();
35 void create_test();
36 void drop_test();
37 void prepstmt1_test();
38 void prepstmt2_test();
39 void fetch_test();
40 void inout_test();
41 void blob_test();
42 
43 
44 char host[] = "localhost";
45 char tableset[] = "TS1";
46 char user[] = "lemke";
47 char passwd[] = "lemke";
48 char cgwlog[] = "cgwrap.log";
49 char prot[] = "serial";
50 
51 char proccreatestmt[] = "create procedure checkInOut ( inVal in string(30), outVal out string(30) ) return int \
52 begin \
53    insert into t1 values ( 1000, :inVal ); \
54    :outVal = 'Out-Out'; \
55    return 42; \
56 end;";
57 char tabcreatestmt[] = "create table t1 ( a int, b string(30));";
58 char procdropstmt[] = "drop procedure checkInOut;";
59 char tabdropstmt[] = "drop table t1;";
60 
61 
main(int argc,char ** argv)62 int main(int argc, char** argv)
63 {
64 
65     if ( argc > 1 )
66     {
67 	if ( strcmp (argv[1], "connect") == 0)
68 	{
69 	    connect_test();
70 	}
71 	else if ( strcmp (argv[1], "create") == 0)
72 	{
73 	    create_test();
74 	}
75 	else if ( strcmp (argv[1], "drop") == 0)
76 	{
77 	    drop_test();
78 	}
79 	else if ( strcmp (argv[1], "prepstmt1") == 0)
80 	{
81 	    prepstmt1_test();
82 	}
83 	else if ( strcmp (argv[1], "prepstmt2") == 0)
84 	{
85 	    prepstmt2_test();
86 	}
87 	else if ( strcmp (argv[1], "fetch") == 0)
88 	{
89 	    fetch_test();
90 	}
91 	else if ( strcmp (argv[1], "inout") == 0)
92 	{
93 	    inout_test();
94 	}
95 	else if ( strcmp (argv[1], "blob") == 0)
96 	{
97 	    blob_test();
98 	}
99     }
100 
101 }
102 
connect_test()103 void connect_test()
104 {
105 
106     CGDB *cgdb;
107 
108     printf("Connecting ...\n");
109 
110     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
111     {
112 	printf("Error : %s\n", cgerrmsg);
113 	exit(1);
114     }
115 
116     cego_modlog("ALL", CG_LOG_DEBUG);
117 
118     printf("Connect ok\n");
119 
120     cego_disconnect(cgdb);
121 }
122 
create_test()123 void create_test()
124 {
125 
126     CGDB *cgdb;
127 
128     printf("Connecting ...\n");
129 
130     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
131     {
132 	printf("Error : %s\n", cgerrmsg);
133 	exit(1);
134     }
135 
136     cego_modlog("ALL", CG_LOG_DEBUG);
137 
138 
139     printf("Executing query ...\n");
140     if ( cego_query (cgdb, tabcreatestmt, 0) != 0 )
141     {
142 	printf("Error : %s\n", cgerrmsg);
143 	exit(1);
144     }
145     printf("Query ok\n");
146 
147     if ( cego_query (cgdb, proccreatestmt, 0) != 0 )
148     {
149 	printf("Error : %s\n", cgerrmsg);
150 	exit(1);
151     }
152     printf("Query ok\n");
153 
154 
155     cego_disconnect(cgdb);
156 }
157 
drop_test()158 void drop_test()
159 {
160 
161     CGDB *cgdb;
162 
163     printf("Connecting ...\n");
164 
165     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
166     {
167 	printf("Error : %s\n", cgerrmsg);
168 	exit(1);
169     }
170 
171     cego_modlog("ALL", CG_LOG_DEBUG);
172 
173     printf("Executing query ...\n");
174     if ( cego_query (cgdb, tabdropstmt, 0) != 0 )
175     {
176 	printf("Error : %s\n", cgerrmsg);
177 	exit(1);
178     }
179     printf("Query ok\n");
180 
181     if ( cego_query (cgdb, procdropstmt, 0) != 0 )
182     {
183 	printf("Error : %s\n", cgerrmsg);
184 	exit(1);
185     }
186     printf("Query ok\n");
187 
188 
189     cego_disconnect(cgdb);
190 }
191 
192 
prepstmt1_test()193 void prepstmt1_test()
194 {
195 
196     CGDB *cgdb;
197 
198     printf("Connecting ...\n");
199 
200     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
201     {
202 	printf("Error : %s\n", cgerrmsg);
203 	exit(1);
204     }
205 
206     printf("Prepare stmt ...\n");
207     CGStmt *pStmt;
208     pStmt = cego_prepare("insert into t1 values ( ? , ? );");
209 
210     int *pI = (int*)malloc(sizeof(int));
211     *pI = 42;
212     CGVal *pV1 = (CGVal*)malloc(sizeof(CGVal));
213     pV1->type = CG_INT;
214     pV1->len = sizeof(int);
215     pV1->val = pI;
216 
217     char *s = (char*)malloc(20);
218     strcpy(s, "This is a X");
219     CGVal *pV2 = (CGVal*) malloc(sizeof(CGVal));
220 
221     pV2->type = CG_VARCHAR;
222     pV2->len = strlen(s);
223     pV2->val = s;
224 
225     printf("Bind stmt ...\n");
226     cego_bind_in(pStmt, pV1, 1);
227     cego_bind_in(pStmt, pV2, 2);
228 
229     printf("Execute stmt ...\n");
230     if ( cego_execute(cgdb, pStmt, 0) != 0)
231     {
232 	printf("Error : %s\n", cgerrmsg);
233     }
234     printf("Prepared Stmt ok\n");
235 
236     cego_free_stmt(pStmt);
237 
238     cego_disconnect(cgdb);
239 }
240 
241 
prepstmt2_test()242 void prepstmt2_test()
243 {
244 
245     CGDB *cgdb;
246 
247     printf("Connecting ...\n");
248 
249     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
250     {
251 	printf("Error : %s\n", cgerrmsg);
252 	exit(1);
253     }
254 
255     printf("Prepare stmt ...\n");
256     CGStmt *pStmt;
257     pStmt = cego_prepare("select a, b from t1;");
258 
259 
260     printf("Creating fetch handle ...\n");
261     CGFetch* cgfetch = cego_allocate_fetch();
262 
263     printf("Making query ...\n");
264     if ( cego_execute(cgdb, pStmt, cgfetch) != 0 )
265     {
266 	printf("Error : %s\n", cgerrmsg);
267 	exit(1);
268     }
269 
270     printf("Size=%d\n", cego_num_col(cgfetch));
271 
272     CGVal cgval[2];
273     cgval[0].val=0;
274     cgval[1].val=0;
275 
276     int colnum;
277     colnum = cego_fetch (cgfetch, cgval, 2);
278     cego_abort(cgfetch);
279     cego_disconnect(cgdb);
280 
281 
282     while ( ( colnum = cego_fetch (cgfetch, cgval, 2)) > 0 )
283     {
284 
285 	int i=0;
286 	while ( i < colnum )
287 	{
288 	    printf("Col %d\n", i);
289 	    if ( cgval[i].type == CG_VARCHAR )
290 		printf("%s ", (char*)cgval[i].val);
291 	    else if ( cgval[i].type == CG_INT )
292 		printf("%d ", *(int*)cgval[i].val);
293 	    i++;
294 	}
295 	printf("\n");
296 
297 	cego_abort(cgfetch);
298 	break;
299     }
300 
301     printf("Fetch ok\n");
302     cego_free_fetch(cgfetch);
303 
304     printf("Prepared Stmt ok\n");
305 
306     cego_disconnect(cgdb);
307 }
308 
309 
fetch_test()310 void fetch_test()
311 {
312 
313     CGDB *cgdb;
314 
315     printf("Connecting ...\n");
316 
317     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
318     {
319 	printf("Error : %s\n", cgerrmsg);
320 	exit(1);
321     }
322 
323     printf("Creating fetch handle ...\n");
324     CGFetch* cgfetch = cego_allocate_fetch();
325 
326     printf("Making query ...\n");
327     if ( cego_query(cgdb, "select a, b from t1;", cgfetch) != 0 )
328     {
329 	printf("Error : %s\n", cgerrmsg);
330 	exit(1);
331     }
332 
333     printf("Size=%d\n", cego_num_col(cgfetch));
334 
335     CGVal cgval[2];
336     cgval[0].val=0;
337     cgval[1].val=0;
338 
339     int colnum;
340     while ( ( colnum = cego_fetch (cgfetch, cgval, 2)) > 0 )
341     {
342 
343 	int i=0;
344 	while ( i < colnum )
345 	{
346 	    printf("Col %d, Name %s\n", i, cego_getcol(cgfetch, i));
347 	    if ( cgval[i].type == CG_VARCHAR )
348 		printf("%s ", (char*)cgval[i].val);
349 	    else if ( cgval[i].type == CG_INT )
350 		printf("%d ", *(int*)cgval[i].val);
351 	    i++;
352 	}
353 	printf("\n");
354     }
355 
356     printf("Fetch ok\n");
357 
358     cego_free_fetch(cgfetch);
359     cego_disconnect(cgdb);
360 }
361 
362 
inout_test()363 void inout_test()
364 {
365 
366     CGDB *cgdb;
367 
368     printf("Connecting ...\n");
369 
370     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
371     {
372         printf("Error : %s\n", cgerrmsg);
373         exit(1);
374     }
375 
376     printf("Prepare stmt ...\n");
377     CGStmt *pStmt;
378     pStmt = cego_prepare("? = call checkInOut( ? , ? );");
379 
380     CGVal v1;
381     v1.val = 0;
382 
383     char s[20] = "This is XXXX";
384     CGVal v2;
385 
386     v2.type = CG_VARCHAR;
387     v2.len = strlen(s);
388     v2.val = s;
389 
390     CGVal v3;
391     v3.val = 0;
392 
393     printf("Bind stmt ...\n");
394     cego_bind_out(pStmt, &v1, 1);
395     cego_bind_in(pStmt, &v2, 2);
396     cego_bind_out(pStmt, &v3, 3);
397 
398     printf("Execute stmt ...\n");
399     if ( cego_execute(cgdb, pStmt, 0) != 0)
400     {
401         printf("Error : %s\n", cgerrmsg);
402     }
403 
404     printf("P1 = %d\n", *(int*)v1.val);
405     printf("P2 = %s\n", (char*)v2.val);
406     printf("P3 = %s\n", (char*)v3.val);
407 
408     printf("InOut ok\n");
409 
410     cego_disconnect(cgdb);
411 }
412 
blob_test()413 void blob_test()
414 {
415 
416     CGDB *cgdb;
417 
418     printf("Connecting ...\n");
419 
420     if ( (cgdb = cego_connect(host, 2200, prot, tableset, user, passwd, cgwlog)) == NULL )
421     {
422 	printf("Error : %s\n", cgerrmsg);
423 	exit(1);
424     }
425 
426 
427 
428     printf("Creating blob table ...\n");
429 
430     CGStmt *pStmt;
431     pStmt = cego_prepare("create table blobtab ( a blob );");
432     printf("Execute stmt ...\n");
433     if ( cego_execute(cgdb, pStmt, 0) != 0)
434     {
435 	printf("Error : %s\n", cgerrmsg);
436 	exit(1);
437     }
438     cego_free_stmt(pStmt);
439 
440     // create blob ( putblob )
441 
442     CGBlob b;
443     int blobSize = 100;
444     b.buf = malloc(blobSize);
445     int i;
446     for ( i = 0 ; i < blobSize ; i++ )
447 	b.buf[i]='X';
448 
449     b.len = blobSize;
450     b.fileId = 0;
451     b.pageId = 0;
452 
453     cego_putblob (cgdb, &b);
454     printf("Created blob with ref : [%d,%d]\n", b.fileId, b.pageId);
455 
456     // insert into blobtab
457 
458     pStmt = cego_prepare("insert into blobtab values ( ? );");
459 
460     CGVal v1;
461     v1.type = CG_BLOB;
462     v1.len = 2 * sizeof(int);
463     v1.val = malloc ( 2 * sizeof(int));
464 
465     memcpy ( v1.val, &b.fileId, sizeof(int));
466     memcpy ( (int*)((long)v1.val + sizeof(int)), &b.pageId, sizeof(int));
467 
468     printf("Bind stmt ...\n");
469     cego_bind_in(pStmt, &v1, 1);
470 
471     printf("Execute stmt ...\n");
472     if ( cego_execute(cgdb, pStmt, 0) != 0)
473     {
474 	printf("Error : %s\n", cgerrmsg);
475     }
476     printf("Prepared Stmt ok\n");
477 
478     // blob retrievel
479     // cego_disconnect(cgdb);
480     // return;
481 
482     // select from blobtab
483 
484     printf("Creating fetch handle ...\n");
485     CGFetch* cgfetch = cego_allocate_fetch();
486 
487     printf("Creating fetch handle ...\n");
488     if ( cego_query(cgdb, "select a from blobtab;", cgfetch) != 0 )
489     {
490 	printf("Error : %s\n", cgerrmsg);
491 	exit(1);
492     }
493 
494     printf("NumCol=%d\n", cego_num_col(cgfetch));
495 
496     CGVal cgval[1];
497 
498     int fileId;
499     int pageId;
500 
501     int colnum;
502     while ( ( colnum = cego_fetch (cgfetch, cgval, 1)) > 0 )
503     {
504 	int i=0;
505 	while ( i < colnum )
506 	{
507 	    if ( cgval[i].type == CG_BLOB )
508 	    {
509 
510 		memcpy ( &fileId, (int*)cgval[i].val, sizeof (int));
511 		memcpy ( &pageId, (int*)( (long)cgval[i].val + sizeof(int)), sizeof (int));
512 
513 		printf("FileId = %d", fileId);
514 		printf("PageId = %d", pageId);
515 
516 	    }
517 	    i++;
518 	}
519 	printf("\n");
520     }
521 
522     printf("Fetch ok\n");
523     // cego_free_fetch(cgfetch);
524 
525     printf("Retrieving blob  : [%d,%d]\n", fileId, pageId);
526 
527     CGBlob b2;
528     b2.len = 0;
529     b2.buf = 0;
530 
531     b2.fileId = fileId;
532     b2.pageId = pageId;
533 
534     cego_getblob (cgdb, &b2);
535 
536     printf("Retrieved blob of size : %d ... %s\n", b2.len, b2.buf);
537 
538 
539     pStmt = cego_prepare("drop table blobtab;");
540     printf("Execute stmt ...\n");
541     if ( cego_execute(cgdb, pStmt, 0) != 0)
542     {
543 	printf("Error : %s\n", cgerrmsg);
544 	exit(1);
545     }
546     cego_free_stmt(pStmt);
547 
548     cego_disconnect(cgdb);
549 }
550 
551