-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathplsqlODBC.c
1073 lines (832 loc) · 32.4 KB
/
plsqlODBC.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Universal Permissive License v 1.0 as shown
* at http://oss.oracle.com/licenses/upl
*/
/*
NAME
plsqlODBC.c - ODBC demo that uses PLSQL
DESCRIPTION
This ODBC program does the following:
1. Uses a PLSQL package procedure
2. Uses a PLSQL package function
3. Uses a PLSQL anonymous block with host variables
4. Opens a Ref cursor in a PLSQL procedure
and processes the resultset in ODBC
EXPORT FUNCTION(S)
<external functions defined for use outside package - one-line descriptions>
connectDB() - Connect to TimesTen DB
givePayRaise - Use a PLSQL package procedure to randomly give one of the lowest paid workers a pay raise.
plsqlblock() - Use a PLSQL block to get some details on the worker who got the payraise
getEmpName - Use a PLSQL package function to get the name of an employee based on their empid
getSalesPeople - Open a Ref cursor and use ODBC to iterate over the result set.
INTERNAL FUNCTION(S)
<other external functions defined - one-line descriptions>
STATIC FUNCTION(S)
<static functions defined - one-line descriptions>
NOTES
<other useful comments, qualifications, etc.>
*/
/*
Use the TimesTen Quick Start for instructions to build and run this program.
*/
/* This source is best displayed with a tabstop of 4 */
#ifdef WIN32
#include <windows.h>
#else
#include <sqlunix.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "utils.h"
#include "tt_version.h"
#include "timesten.h"
#include "ttgetopt.h"
#define VERBOSE_NOMSGS 0
/* this value is used for results (and err msgs) only */
#define VERBOSE_RESULTS 1
/* this value is the default for the cmdline demo */
#define VERBOSE_DFLT 2
#define VERBOSE_ALL 3
#define DSNNAME "DSN=" DEMODSN
#define EMP_NAME_LEN 64
#define ERR_TEXT_LEN 255
static char usageStr[] =
"Usage:\t<CMD> {-h | -help | -V}\n"
"\t<CMD> [<DSN> | -connstr <connection-string>]\n\n"
" -h Prints this message and exits.\n"
" -help Same as -h.\n"
" -V Prints version number and exits.\n"
"The default if no DSN or connection-string is given is:\n"
" \"DSN=sampledb;UID=appuser\".\n\n";
/* Global parameters and flags (typically set by parse_args()) */
static char connstr[CONN_STR_LEN]; /* Connection string. */
static char dsn[CONN_STR_LEN]; /* DSN. */
static char cmdname[80]; /* Stripped command name. */
FILE *statusfp; /* File for status messages */
SQLHENV henv; /* Environment handle */
SQLHDBC hdbc;
int numEmps = 0; /* The number of lowest paid employees to choose from */
int empNo = 0; /* The employee ID of interest */
char luckyEmp[EMP_NAME_LEN + 1]; /* The employee who got a 10% payrise */
int errCode = 0;
char errText[ERR_TEXT_LEN + 1];
char jobtype[9+1];
char hired[20+1];
double salary;
int dept;
int worked_longer;
int higher_sal;
int total_in_dept;
char * exec_plsql_proc =
"begin \
emp_pkg.givePayRaise(:numEmps, :luckyEmp, :errCode, :errText); \
end;";
char * exec_plsql_fn =
"begin \
:luckyEmp := sample_pkg.getEmpName(:empNo, :errCode, :errText); \
end;";
char * exec_OpenSalesPeopleCursor =
"begin \
emp_pkg.OpenSalesPeopleCursor(:cmdRefCursor, :errCode, :errText); \
end;";
void givePayRaise( void );
void plsqlblock( void );
void getEmpName( void );
void getSalesPeople( void );
void connectDB( void );
void disconnectDB( void );
/*********************************************************************
*
* FUNCTION: parse_args
*
* DESCRIPTION: This function parses the command line arguments
* passed to main(), setting the appropriate global
* variables and issuing a usage message for
* invalid arguments.
*
* PARAMETERS: int argc # of arguments from main()
* char *argv[] arguments from main()
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
parse_args(int argc, char *argv[])
{
int ac;
char errbuf[80];
ttc_getcmdname(argv[0], cmdname, sizeof(cmdname));
ac = ttc_getoptions(argc, argv, TTC_OPT_NO_CONNSTR_OK,
errbuf, sizeof(errbuf),
"<HELP>", usageStr,
"<VERSION>", NULL,
"<CONNSTR>", connstr, sizeof(connstr),
"<DSN>", dsn, sizeof(dsn),
NULL);
if (ac == -1) {
fprintf(stderr, "%s\n", errbuf);
fprintf(stderr, "Type '%s -help' for more information.\n", cmdname);
exit(-1);
}
if (ac != argc) {
ttc_dump_help(stderr, cmdname, usageStr);
exit(-1);
}
/* check for connection string or DSN */
if (dsn[0] && connstr[0]) {
/* Got both a connection string and a DSN. */
fprintf(stderr, "%s: Both DSN and connection string were given.\n",
cmdname);
app_exit(-1);
} else if (dsn[0]) {
/* Got a DSN, turn it into a connection string. */
if (strlen(dsn)+5 >= sizeof(connstr)) {
fprintf(stderr, "%s: DSN '%s' too long.\n", cmdname, dsn);
app_exit(-1);
}
sprintf(connstr, "DSN=%s", dsn);
} else if (connstr[0]) {
/* Got a connection string, append some attributes. */
if (strlen(connstr)+1 >= sizeof(connstr)) {
fprintf(stderr, "%s: connection string '%s' too long.\n",
cmdname, connstr);
app_exit(-1);
}
} else {
/*
* No connection string or DSN given, use the default.
*
*/
sprintf(connstr, "%s;%s",
DSNNAME, UID);
}
}
/*********************************************************************************************/
/* Use a PLSQL package procedure to randomly give one of the lowest paid workers a payraise. */
/*********************************************************************************************/
void givePayRaise()
{
SQLHSTMT plsql_proc;
SQLRETURN rc;
SQLLEN luckyEmpLen;
SQLLEN errTextLen;
/* Initialize the emp_pkg.givePayRaise PLSQL procedure params */
errCode = 0;
memset(errText, 0, sizeof(errText));
memset(luckyEmp, 0, sizeof(luckyEmp));
numEmps = 10;
/* Allocate a statement for the PLSQL procedure */
rc = SQLAllocStmt(hdbc, &plsql_proc);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ABORT_DISCONNECT_EXIT,
"allocating plsql_proc statement handle",
__FILE__, __LINE__);
/* prepare the PLSQL procedure emp_pkg.givePayRaise */
rc = SQLPrepare(plsql_proc,
(SQLCHAR *) exec_plsql_proc,
SQL_NTS);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"preparing plsql proc",
__FILE__, __LINE__);
/* Bind the procedure IN and OUT parameters */
rc = SQLBindParameter(plsql_proc,
(SQLUSMALLINT) 1,
SQL_PARAM_INPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&numEmps,
0,
NULL);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_proc,
(SQLUSMALLINT) 2,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
EMP_NAME_LEN,
0,
luckyEmp,
EMP_NAME_LEN,
&luckyEmpLen);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_proc,
(SQLUSMALLINT) 3,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&errCode,
0,
NULL);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_proc,
(SQLUSMALLINT) 4,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
ERR_TEXT_LEN,
0,
errText,
ERR_TEXT_LEN,
&errTextLen);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
/* Execute the procedure */
rc = SQLExecute(plsql_proc);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"Error executing proc",
__FILE__, __LINE__);
printf("The employee who got the 10%% pay raise was %s\n", luckyEmp);
/* free the statement */
rc = SQLFreeStmt(plsql_proc, SQL_DROP);
handle_errors(hdbc, plsql_proc, rc, ABORT_DISCONNECT_EXIT,
"dropping the statement handle",
__FILE__, __LINE__);
} /* givePayRaise */
/**************************************************************************************/
/* Use a PLSQL anonymous block to get some details on the worker who got the payraise */
/**************************************************************************************/
void plsqlblock()
{
SQLHSTMT anon_block;
SQLRETURN rc;
SQLLEN luckyEmpLen;
SQLLEN errTextLen;
SQLLEN jobTypeLen;
SQLLEN hiredLen;
char anon_plsqlblock[2048];
/* Initialize the input and output parameters for the plsql block */
memset(anon_plsqlblock, 0, sizeof(anon_plsqlblock));
memset(jobtype, 0, sizeof(jobtype));
memset(hired, 0, sizeof(hired));
memset(errText, 0, sizeof(errText));
errCode = 0;
salary = 0.0;
dept = 0;
worked_longer = 0;
higher_sal = 0;
total_in_dept = 0;
/* Need to know the length of the IN param luckyEmp */
luckyEmpLen = strlen(luckyEmp);
/* Create the text for the PLSQL anonymous block */
strcpy(anon_plsqlblock, "BEGIN");
strcat(anon_plsqlblock, " SELECT job, hiredate, sal, deptno");
strcat(anon_plsqlblock, " INTO :jobtype, :hired, :salary, :dept");
strcat(anon_plsqlblock, " FROM emp");
strcat(anon_plsqlblock, " WHERE ename = UPPER(:luckyEmp);");
strcat(anon_plsqlblock, " SELECT count(*)");
strcat(anon_plsqlblock, " INTO :worked_longer");
strcat(anon_plsqlblock, " FROM emp");
strcat(anon_plsqlblock, " WHERE hiredate < :hired;");
strcat(anon_plsqlblock, " SELECT count(*)");
strcat(anon_plsqlblock, " INTO :higher_sal");
strcat(anon_plsqlblock, " FROM emp");
strcat(anon_plsqlblock, " WHERE sal > :salary;");
strcat(anon_plsqlblock, " SELECT count(*)");
strcat(anon_plsqlblock, " INTO :total_in_dept");
strcat(anon_plsqlblock, " FROM emp");
strcat(anon_plsqlblock, " WHERE deptno = :dept;");
strcat(anon_plsqlblock, " EXCEPTION");
strcat(anon_plsqlblock, " WHEN OTHERS THEN");
strcat(anon_plsqlblock, " :errCode := SQLCODE;");
strcat(anon_plsqlblock, " :errText := SUBSTR(SQLERRM, 1, 200);");
strcat(anon_plsqlblock, " END;");
/* Allocate a statement for the PLSQL block */
rc = SQLAllocStmt(hdbc, &anon_block);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ABORT_DISCONNECT_EXIT,
"allocating a statement handle",
__FILE__, __LINE__);
/* prepare the PLSQL statement */
rc = SQLPrepare(anon_block,
(SQLCHAR *) anon_plsqlblock,
SQL_NTS);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"preparing anon plsql block",
__FILE__, __LINE__);
/* Bind the IN and OUT parameters */
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 1,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
sizeof(jobtype),
0,
jobtype,
sizeof(jobtype),
&jobTypeLen);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter jobtype",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 2,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
sizeof(hired),
0,
hired,
sizeof(hired),
&hiredLen);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter hired",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 3,
SQL_PARAM_OUTPUT,
SQL_C_DOUBLE,
SQL_DOUBLE,
9,
2,
&salary,
sizeof(salary),
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter salary",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 4,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&dept,
0,
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter dept",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 5,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
EMP_NAME_LEN,
0,
luckyEmp,
EMP_NAME_LEN,
&luckyEmpLen);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter luckyEmp",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 6,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&worked_longer,
0,
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter worked_longer",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 7,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&higher_sal,
0,
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter higher_sal",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 8,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&total_in_dept,
0,
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter total_in_dept",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 9,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&errCode,
0,
NULL);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter errCode",
__FILE__, __LINE__);
rc = SQLBindParameter(anon_block,
(SQLUSMALLINT) 10,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
ERR_TEXT_LEN,
0,
errText,
ERR_TEXT_LEN,
&errTextLen);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"binding parameter errText",
__FILE__, __LINE__);
/* Execute the anon PLSQL block */
rc = SQLExecute(anon_block);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"Execute anaon plsql block",
__FILE__, __LINE__);
if (0 == errCode)
{
/* Display the employee's information */
printf("\n");
printf("Name: %s\n", luckyEmp);
printf("Job: %s\n", jobtype);
printf("Hired: %s\t(%2d people have served longer).\n", hired, worked_longer);
printf("Salary: $%.2f\t\t\t(%2d people have a higher salary).\n", salary, higher_sal);
printf("Department: %d\t\t\t\t(%2d people in the department).\n\n", dept, total_in_dept);
}
else
{
printf("A problem with plsqlblock:");
printf(" Error: %d\n", errCode);
printf(" Error Text: %s\n", errText);
}
/* Free the statement */
rc = SQLFreeStmt(anon_block, SQL_DROP);
handle_errors(hdbc, anon_block, rc, ABORT_DISCONNECT_EXIT,
"dropping the statement handle",
__FILE__, __LINE__);
} /* plsqlblock */
/************************************************************************************/
/* Use a PLSQL package function to get the name of an employee based on their empid */
/************************************************************************************/
void getEmpName()
{
SQLHSTMT plsql_func;
SQLRETURN rc;
SQLLEN luckyEmpLen;
SQLLEN errTextLen;
/* Initialize the PLSQL proc params */
memset(luckyEmp, 0, sizeof(luckyEmp));
memset(errText, 0, sizeof(errText));
errCode = 0;
empNo = 7839;
/* Allocate a statement for the PLSQL function */
rc = SQLAllocStmt(hdbc, &plsql_func);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ABORT_DISCONNECT_EXIT,
"allocating a statement handle",
__FILE__, __LINE__);
/* Prepare the PLSQL function sample_pkg.getEmpName */
rc = SQLPrepare(plsql_func,
(SQLCHAR *) exec_plsql_fn,
SQL_NTS);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"preparing plsql func",
__FILE__, __LINE__);
/* Bind the IN and OUT parameters */
rc = SQLBindParameter(plsql_func,
(SQLUSMALLINT) 1,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
EMP_NAME_LEN,
0,
luckyEmp,
EMP_NAME_LEN,
&luckyEmpLen);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_func,
(SQLUSMALLINT) 2,
SQL_PARAM_INPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&empNo,
0,
NULL);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_func,
(SQLUSMALLINT) 3,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&errCode,
0,
NULL);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_func,
(SQLUSMALLINT) 4,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
ERR_TEXT_LEN,
0,
errText,
ERR_TEXT_LEN,
&errTextLen);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"binding parameter",
__FILE__, __LINE__);
/* Execute the PLSQ function */
rc = SQLExecute(plsql_func);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"Error executing func",
__FILE__, __LINE__);
printf("The employee with empid %d was %s\n", empNo, luckyEmp);
/* Free the statement */
rc = SQLFreeStmt(plsql_func, SQL_DROP);
handle_errors(hdbc, plsql_func, rc, ABORT_DISCONNECT_EXIT,
"dropping the statement handle",
__FILE__, __LINE__);
} /* getEmpName */
/**************************************************************************/
/* Use a PLSQL Packge procedure to open a ref cursor for all sales people */
/* Use ODBC to iterate over of the resultset of the ref cursor */
/**************************************************************************/
void getSalesPeople()
{
SQLHSTMT plsql_OpenSalesPeopleCursor; /* call the PLSQL proc to open the ref cursor */
SQLHSTMT ref_cursor; /* Ref Cursor returned by PL/SQL */
//SQLHSTMT real_ref_cursor; /* Use the returned ref cursor in ODBC, uncomment when wish to try SQLGetInfo() code below */
SQLRETURN rc;
SQLLEN luckyEmpLen;
SQLLEN errTextLen;
/* Allocate the statement for the ODBC Ref Cursor */
rc = SQLAllocStmt(hdbc, &ref_cursor);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ABORT_DISCONNECT_EXIT,
"allocating a statement handle",
__FILE__, __LINE__);
/* Allocate the statement for the PLSQL procedure */
rc = SQLAllocStmt(hdbc, &plsql_OpenSalesPeopleCursor);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ABORT_DISCONNECT_EXIT,
"allocating a statement handle",
__FILE__, __LINE__);
/* Prepare the PLSQL statement */
rc = SQLPrepare(plsql_OpenSalesPeopleCursor,
(SQLCHAR *) exec_OpenSalesPeopleCursor,
SQL_NTS);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"preparing procedure OpenSalesPeopleCursor",
__FILE__, __LINE__);
/*
** A Driver Manager may wrap a hStmt into its own structure.
** Call SQLGetInfo() to get a handle to the real underlying hStmt (the one
** used by the actual ODBC driver).
** If there is no driver manager, then SQLGetInfo() just returns a pointer
** to the current hStmt. Uncomment the below lines as well the define code
** for real_ref_cursor above if you want to see the underlying handle returned
** by SQLGetInfo() in the driver manager.
*/
/*
real_ref_cursor = ref_cursor;
rc = SQLGetInfo(hdbc, SQL_DRIVER_HSTMT, &real_ref_cursor, sizeof(real_ref_cursor), 0);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"Calling SQLGetInfo()",
__FILE__, __LINE__);
printf("The pointer to hstmt handle returned by driver manager is:%i\n",real_ref_cursor);
printf("The pointer to hstmt handle used by the application is:%i\n", ref_cursor);
*/
/* Bind the PLSQL OUT parameters */
rc = SQLBindParameter(plsql_OpenSalesPeopleCursor,
(SQLUSMALLINT) 1,
SQL_PARAM_OUTPUT,
SQL_C_REFCURSOR,
SQL_REFCURSOR,
0,
0,
ref_cursor,
0,
NULL);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"binding parameter ref cursor",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_OpenSalesPeopleCursor,
(SQLUSMALLINT) 2,
SQL_PARAM_OUTPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&errCode,
0,
NULL);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"binding parameter errCode",
__FILE__, __LINE__);
rc = SQLBindParameter(plsql_OpenSalesPeopleCursor,
(SQLUSMALLINT) 3,
SQL_PARAM_OUTPUT,
SQL_C_CHAR,
SQL_VARCHAR,
ERR_TEXT_LEN,
0,
errText,
ERR_TEXT_LEN,
&errTextLen);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"binding parameter errText",
__FILE__, __LINE__);
/* Initialize the ref cursor params */
errCode = 0;
memset(errText, 0, sizeof(errText));
/* Execute the PLSQL procedure OpenSalesPeopleCursor */
rc = SQLExecute(plsql_OpenSalesPeopleCursor);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"Error executing plsql_OpenSalesPeopleCursor",
__FILE__, __LINE__);
if (0 == errCode) {
/* We know the number and type of the Ref Cursor columns so just bind them */
rc = SQLBindCol(ref_cursor, 1, SQL_C_SLONG, &empNo, 0, NULL);
handle_errors(hdbc, ref_cursor, rc, ABORT_DISCONNECT_EXIT,
"Error in SQLBindCol for empNo",
__FILE__, __LINE__);
rc = SQLBindCol(ref_cursor, 2, SQL_C_CHAR, luckyEmp, sizeof(luckyEmp), &luckyEmpLen);
handle_errors(hdbc, ref_cursor, rc, ABORT_DISCONNECT_EXIT,
"Error in SQLBindCol for luckyEmp",
__FILE__, __LINE__);
rc = SQLBindCol(ref_cursor, 3, SQL_C_DOUBLE, &salary, 0, NULL);
handle_errors(hdbc, ref_cursor, rc, ABORT_DISCONNECT_EXIT,
"Error in SQLBindCol for salary",
__FILE__, __LINE__);
/* Iterate over the ref cursors result set */
printf("\nThe sales people are:\n\n");
printf(" EMPNO ENAME SALARY\n");
printf(" ===== ========== ==========\n");
while((rc = SQLFetch(ref_cursor)) != SQL_NO_DATA_FOUND){
printf(" %5.0d %-10s %10.2f\n", empNo, luckyEmp, salary);
}
/* close the ref cursor */
rc = SQLFreeStmt(ref_cursor, SQL_CLOSE);
} else {
printf("Did not call ref cursor OK\n");
}
/* Drop the ref Cursor ODBC statment */
rc = SQLFreeStmt(ref_cursor, SQL_DROP);
handle_errors(hdbc, ref_cursor, rc, ABORT_DISCONNECT_EXIT,
"dropping the statement handle",
__FILE__, __LINE__);
/* Drop the PLSQL statment */
rc = SQLFreeStmt(plsql_OpenSalesPeopleCursor, SQL_DROP);
handle_errors(hdbc, plsql_OpenSalesPeopleCursor, rc, ABORT_DISCONNECT_EXIT,
"dropping the statement handle",
__FILE__, __LINE__);
} /* getSalesPeople */
/*****************************************/
/* Connect to the TimesTen DB using ODBC */
/*****************************************/
void connectDB( void )
{
/* variables for ODBC */
SQLRETURN rc;
/* variables used for setting up the tables */
char errstr[4096];
/* ODBC initialization */
rc = SQLAllocEnv(&henv);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
/* error occurred -- don't bother calling handle_errors, since handle
* is not valid so SQLError won't work */
err_msg3("ERROR in %s, line %d: %s\n",
__FILE__, __LINE__, "allocating an environment handle");
app_exit(1);
}
/* call this in case of warning */
handle_errors(SQL_NULL_HDBC, SQL_NULL_HSTMT, rc, NO_EXIT,
"allocating execution environment",
__FILE__, __LINE__);
rc = SQLAllocConnect(henv, &hdbc);
handle_errors(SQL_NULL_HDBC, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"allocating connection handle",
__FILE__, __LINE__);
/* Connect to data store */
printf("\nConnecting with connect string %s\n", connstr);
rc = SQLDriverConnect(hdbc, NULL, (SQLCHAR *) connstr, SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_COMPLETE);
sprintf(errstr, "connecting to driver (connect string %s)\n",
connstr);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ERROR_EXIT,
errstr, __FILE__, __LINE__);
printf("Connected\n\n");
/* Turn auto-commit off */
rc = SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, DISCONNECT_EXIT,
"switching off the AUTO_COMMIT option",
__FILE__, __LINE__);
/* commit transaction */
rc = SQLTransact(henv, hdbc, SQL_COMMIT);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"committing transaction",
__FILE__, __LINE__);
} /* connectDB */
/*****************************************/
/* Disconnect to the TimesTen DB using ODBC */
/*****************************************/
void disconnectDB( void )
{
/* variables for ODBC */
SQLRETURN rc;
printf("\nDisconnecting\n");
/* Disconnect and return */
/* commit transaction */
rc = SQLTransact(henv, hdbc, SQL_COMMIT);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"committing transaction",
__FILE__, __LINE__);
rc = SQLDisconnect(hdbc);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"disconnecting",
__FILE__, __LINE__);
rc = SQLFreeConnect(hdbc);
handle_errors(hdbc, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"freeing connection handle",
__FILE__, __LINE__);
rc = SQLFreeEnv(henv);
handle_errors(SQL_NULL_HDBC, SQL_NULL_HSTMT, rc, ERROR_EXIT,
"freeing environment handle",
__FILE__, __LINE__);