-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlora.c
8270 lines (6416 loc) · 224 KB
/
sqlora.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
/* $Id: sqlora.c 305 2005-02-26 14:58:20Z kpoitschke $ */
/**
* @file sqlora.c
* libsqlora8 Implementation
*
* @author Kai Poitschke
*
* Copyright (c) 1991-2004 Kai Poitschke (kai[_at_]poitschke.de)
*
* This file is part of the libsqlora8 package which can be found
* at http://www.poitschke.de/libsqlora8/
*/
/*
* Permission to use, copy, modify, and distribute this software for
* any purpose with or without fee is hereby granted, provided that
* the above copyright notice and this permission notice appear in all
* copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
const char * _sqlo_sqloraID="$Id: sqlora.c 305 2005-02-26 14:58:20Z kpoitschke $";
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H /* for my windows friends */
# include <unistd.h>
#endif
#include <ctype.h>
#include <assert.h>
#if defined(ENABLE_WINTHREADS)
#include <windows.h>
#else
# if defined(HAVE_PTHREAD_H)
#include <pthread.h>
# endif
#endif
#include "oci.h"
#include "sqlora.h"
/* If glib is enabled, we use the glib allocators */
#ifdef USE_GLIB_ALLOC
# include "glib.h"
# define MALLOC g_malloc
# define REALLOC g_realloc
# define FREE(_p) {void ** l_p= (void **)&(_p); if ( *l_p != NULL ) {g_free( *l_p); *l_p = NULL; } }
#else
# define MALLOC malloc
# define REALLOC realloc
# define FREE(_p) {void ** l_p= (void**)&(_p); if ( *l_p != NULL ) {free( *l_p); *l_p = NULL; } }
#endif
#if defined(WIN32)
#define inline __inline
#endif
#if defined (__STDC__) || defined (_AIX) || defined(PROTOTYPES) ||\
(defined (__mips) && defined (_SYSTYPE_SVR4)) ||\
defined(WIN32) || defined(__cplusplus)\
# define AND ,
# define DEFUN(name, arglist, args) name(args)
# define DEFUN_VOID(name) name(void)
/*
* Macro to use instead of "void" for arguments that must have
* type "void *" in ANSI C; maps them to type "char *" in
* non-ANSI systems.
*/
# define VOID void
#else
# define AND ;
# define DEFUN(name, arglist, args) name arglist args;
# define DEFUN_VOID(name) name()
# define VOID char
#endif
#ifndef NULL
# define NULL 0
#endif
/*#define TEST_WITH_SIZEOF_RETURNING_ULONG*/
#ifdef TEST_WITH_SIZEOF_RETURNING_ULONG
#define sizeof(_a) (unsigned long)sizeof(_a)
#endif
#if defined(ENABLE_PTHREADS) || defined(ENABLE_ORATHREADS) || defined(ENABLE_WINTHREADS)
/**
* Defines if the library was compiled with --enable-pthreads
*/
enum {THREADS_ENABLED = 1};
#else
enum {THREADS_ENABLED = 0};
#endif
#ifdef ENABLE_PTHREADS
typedef pthread_t sqlo_thread_t;
typedef pthread_mutex_t sqlo_mutex_t;
#else
# ifdef ENABLE_ORATHREADS
typedef OCIThreadMutex * sqlo_mutex_t;
typedef OCIThreadId * sqlo_thread_t;
# else
# ifdef ENABLE_WINTHREADS
typedef HANDLE sqlo_mutex_t;
typedef unsigned long sqlo_thread_t;
# else
typedef unsigned long sqlo_mutex_t; /* dummy */
typedef unsigned long sqlo_thread_t; /* dummy */
# endif
# endif
#endif
/**
* @def EXEC_WHEN_THREADING
* Executes _cmd if library was compiled with threading and initialized in threaded mode
* @param _cmd I - The code to execute
*/
#define EXEC_WHEN_THREADING(_cmd) \
if (THREADS_ENABLED && OCI_THREADED == _oci_init_mode) { _cmd }
/**
* @def UNLOCK_ALL
* Release all mutex locks
*/
#define UNLOCK_ALL EXEC_WHEN_THREADING(_dbv_unlock(); _env_unlock(); _init_unlock(); )
#define ENCODE_STH(_sth, _dbh) ((int)(_dbh << (sizeof(sqlo_stmt_handle_t)/2 * 8) | _sth))
#define DECODE_STH(_sth) ((ub4) _sth & 0x0000ffff)
#define DECODE_DBH(_sth) ((ub4) (_sth >> (sizeof(sqlo_stmt_handle_t)/2 * 8)) & 0x007fff)
/*-------------------------------------------------------------------------
* CONSTANTS
*-----------------------------------------------------------------------*/
/**
* @enum _sqlora_constants
*/
enum _sqlora_constants {
SQLO_MAX_DB = 0x00007fff, /**< max. number of allowed db connections */
SQLO_MAX_CURSORS = 0x0000ffff, /**< max. number of allowed cursors per db connection */
/* changed the MIN_ sizes for better testing (to make sure we do some reallocations) */
MIN_BINDP = 1 /*64*/, /**< Initial number of bind pointers
* that were allocated
*/
MIN_DEFNP = 1 /*32*/, /**< Initial number of define pointers
* that were allocated
*/
MIN_OBUF_SIZE = 1 /*32*/, /**< Minimum size of the output buffer for columns
* in the select list
*/
MIN_COL_NAME_LEN = 1 /*31*/, /**< Minimum size for a name or a column */
MIN_STMT_SIZE = 10 /* 1024*/, /**< Mininum allocation for sqlo_stmt_t.stmt */
DEF_PREFETCH_ROWS = 100, /**< The default number of prefetched rows */
SQLO_MAX_ERRMSG_LEN = 2047, /**< The maximum length of the error message buffer */
MAX_PATH_LEN = 512, /**< for the trace file */
MAX_VNAME_LEN = 255, /**< Max variable name len read from the environment */
MAX_LONG_SIZE = (1024*64), /**< Max size for LONG output variables */
#ifdef LIBSQLORA8_TRACE_ENABLED
TRACE_ENABLED = 1 /**< configured with enabled trace */
#else
TRACE_ENABLED = 0 /**< configured with disabled trace */
#endif
};
/*-------------------------------------------------------------------------
* MACROS
*-----------------------------------------------------------------------*/
#undef TRUE
#undef FALSE
/**
* Boolean TRUE
*/
#define TRUE ((1 == 1))
/**
* Boolean FALSE
*/
#define FALSE (!TRUE)
/**
* @def TRACE
* Execute cmd when the trace level of the library is >= the trace level
* passed to the macro.
* @param p_trace_level I - The min trace level where cmd should be executed
* @param p_cmd I - The commands to be executed in case of
* _trace_level >= p_trace_level.
*
* @par Example:
* TRACE(3, fprintf(g_ftp, "Calling foo()\n"););
*/
#define TRACE(p_trace_level, p_cmd) \
if ( TRACE_ENABLED && \
(NULL != _trace_fp) && \
(_trace_level >= p_trace_level) ) { \
{ p_cmd } \
(void) fflush(_trace_fp); \
}
/**
* @def ERRMALLOC
* Allocates _len bytes memory for _ptr
*
* If no memory could be allocated, an error message is
* put in p_dbp->errmsg, p_dbp->status is set to p_retcode and
* the macro returns p_retcode
* p_func should be a string identifying the
* location (function name) where the ERRMALLOC was called
* @param p_dbp I - The pointer to the database strucuture
* @param p_ptr O - The allocated memory
* @param p_len I - The number of bytes to allocate
* @param p_func I - The function name where this macro was called
* @param p_retcode I - The return code, the caller wants to return if the malloc fails.
*/
#define ERRMALLOC(p_dbp, p_ptr, p_len, p_func, p_retcode) \
{ \
sqlo_db_struct_ptr_t l_dbp = p_dbp; \
int l_len = p_len; \
CONST char * l_func = p_func; \
int l_retcode = p_retcode; \
(p_ptr) = MALLOC((l_len)); \
if ( !(p_ptr) ) { \
if ( l_dbp ) { \
sprintf( (l_dbp)->errmsg, \
"Cannot malloc %u bytes in %s\n", \
(unsigned int)(l_len), l_func); \
TRACE(1, (void) fputs((l_dbp)->errmsg, \
_get_trace_fp(l_dbp));); \
} \
l_dbp->status = l_retcode; \
/* make sure we release all locks */ \
UNLOCK_ALL; \
return l_retcode; \
} \
TRACE(4, fprintf( _get_trace_fp(l_dbp), \
"Allocated %u bytes in %s\n", \
(unsigned int) (l_len), l_func);); \
}
/**
* @def ERREALLOC
* Rellocates _len bytes memory at _ptr.
* If no memory could be allocated, an error message is
* put in p_dbp->errmsg, p_dbp->status is set to p_retcode
* and the macro returns p_retcode
* _func should be a string identifying the
* location (function name) where the ERRMALLOC was called
* @param p_dbp I - The pointer to the database strucuture
* @param p_ptr O - The allocated memory
* @param p_len I - The number of bytes to allocate
* @param p_func I - The function name where this macro was called
* @param p_retcode I - The return code, the caller wants to return if the malloc fails.
*/
#define ERREALLOC(p_dbp, p_ptr, p_len, p_func, p_retcode) \
{ \
sqlo_db_struct_ptr_t l_dbp = p_dbp; \
int l_len = p_len; \
CONST char * l_func = p_func; \
int l_retcode = p_retcode; \
(p_ptr) = REALLOC((p_ptr), (l_len)); \
if ( !(p_ptr) ) { \
if ( l_dbp ) { \
sprintf( (l_dbp)->errmsg, \
"Cannot realloc %u bytes in %s\n", \
(unsigned int)(l_len), l_func); \
TRACE( 1, (void) fputs((l_dbp)->errmsg, \
_get_trace_fp(l_dbp));); \
} \
/* make sure we release all locks */ \
l_dbp->status = p_retcode; \
UNLOCK_ALL; \
return l_retcode; \
} \
TRACE(4, fprintf(_get_trace_fp(l_dbp), \
"Reallocated %u bytes in %s\n", \
(unsigned int)(l_len), l_func);); \
}
/**
* @def CHECK_DBHANDLE
* Checks for a valid dbh and sets the dbp or returns with error.
* If the db handle is invalid, the macro calls return _errval.
* @param p_dbp O - The pointer to the _sqlo_db_t structure for the _dbh.
* @param p_dbh I - The database handle to check.
* @param p_func I - The callers function name. Used for error message.
* @param p_errval I - The value to return in case of an error.
*/
#define CHECK_DBHANDLE(p_dbp, p_dbh, p_func, p_errval) \
{ \
int l_dbh = p_dbh; \
CONST char * l_func = p_func; \
if ( !VALID_DBH_RANGE(l_dbh) || \
!_dbv[ l_dbh ] || \
!_dbv[ l_dbh ]->used) { \
TRACE(1, fprintf(_trace_fp, \
"Invalid Database handle %d in %s\n", \
l_dbh, l_func);); \
/* make sure we release all locks */ \
UNLOCK_ALL; \
return (p_errval); \
} \
p_dbp = _dbv[ l_dbh ]; \
}
/**
* @def CHECK_STHANDLE
* Checks for a valid sth and sets the stp or returns with error
* If the st handle is invalid, the macro calls return _errval.
* @param p_stp O - The pointer to the _sqlo_stmt_t structure for the _sth.
* @param p_sth I - The statement handle to check.
* @param p_func I - The callers function name. Used for error message.
* @param p_errval I - The value to return in case of an error.
*/
#define CHECK_STHANDLE(p_stp, p_sth, p_func, p_errval) \
{ \
if ( NULL == (p_stp = _sth2stp( p_sth, p_func ) ) || \
!p_stp->used ) { \
return p_errval; \
} \
}
/**
* @def CHECK_OCI_STATUS
* Checks and saves the OCI return status.
* If the status is != OCI_SUCCESS, the function _save_oci_status is called to
* save the error message in the database structure.
*
* @param p_dbp I - The pointer to the database structure.
* @param p_stat I - The status to check.
* @param p_action I - The action you executed causing this status
* @param p_object I - The object on which you did _action.
*/
#define CHECK_OCI_STATUS(p_dbp, p_stat, p_action, p_object) \
{ \
sqlo_db_struct_ptr_t l_dbp = p_dbp; \
int l_stat = p_stat; \
(l_dbp)->status = p_stat; \
TRACE(4, fprintf(_get_trace_fp(l_dbp), \
"CHECK_OCI_STATUS[%u]: %d at %d\n", \
l_dbp->dbh, l_stat, __LINE__);); \
if (OCI_SUCCESS != l_stat && \
OCI_STILL_EXECUTING != l_stat) { \
_save_oci_status(l_dbp, p_action, p_object, __LINE__); \
} \
}
/**
* @def CHECK_OCI_STATUS_RETURN
* Checks and saves the OCI status and returns the status on failure.
*
* Calls CHECK_OCI_STATUS and returns the status if it is != OCI_SUCCESS.
*
* @param p_dbp I - The pointer to the database structure.
* @param p_stat I - The status to check.
* @param p_action I - The action you executed causing this status
* @param p_object I - The object on which you did _action.
*/
#define CHECK_OCI_STATUS_RETURN(p_dbp, p_stat, p_action, p_object) \
{ \
sqlo_db_struct_ptr_t l_dbp2 = p_dbp; \
int l_stat2 = p_stat; \
CHECK_OCI_STATUS(l_dbp2, l_stat2, p_action, p_object); \
if (OCI_SUCCESS != l_stat2) { \
UNLOCK_ALL; \
return (l_stat2); \
} \
}
/**
* @def VALID_DBH_RANGE
* Expression resolves to true, if the passed dbh is in a valid range.
*
* @param _dbh I - The dbh to be checked.
*/
#define VALID_DBH_RANGE(_dbh) ( _dbh >= 0 && _dbh < (int)_dbv_size )
/* If we have usleep we wait this amount of microseconds in a
* OCI_STILL_EXECUTING loop
*/
#ifdef HAVE_USLEEP
# define SQLO_USLEEP usleep(1000)
#else
# define SQLO_USLEEP
#endif
/*-------------------------------------------------------------------------
* TYPES
*-----------------------------------------------------------------------*/
/**
* Boolean type
*/
typedef unsigned char bool_t;
/**
* Cursor types
*/
typedef enum {
DEFAULT = 0, /**< standard cursor */
REFCURSOR = 1, /**< ref cursor type */
NTABLE = 2 /**< nested table cursor */
} sqlo_cursor_type_e;
/**
* Stores information about the database connection.
*/
typedef struct _sqlo_db_struct {
struct _sqlo_stmt_struct *stmtv; /**< The statements (cursors) for this connection */
unsigned int stmtv_size; /**< max size of stmtv[] */
ub4 dbh; /**< The db handle used to address this entry */
OCIServer * srvhp; /**< OCI server handle */
OCIError * errhp; /**< OCI error handle */
OCISvcCtx * svchp; /**< OCI service context handle */
OCISession * authp; /**< OCI authorization session handle */
OCIEnv * envhp; /**< pointer to the OCI environment for the thread */
char * tnsname; /**< The TNS name of the database */
int status; /**< The last status code */
char errmsg[SQLO_MAX_ERRMSG_LEN+1]; /**< The last error message */
sb4 errcode; /**< The last oracle error code */
OCIStmt * stmthp; /**< @ref sqlo_exec stores its stmthp here.
This is not dangerous, because during a non-blocking
OCI call, you cannot open other stmts
*/
/* CONTROL FLAGS */
bool_t used; /**< Flag, if this one is in use or not */
bool_t attached; /**< not zero, if we are attached to the server */
bool_t session_created; /**< not zero, if a session was created */
FILE * trace_fp; /**< connection specific trace file */
sqlo_thread_t thread_id; /**< The thread id of the thread who opened this cursor */
ub4 exec_flags; /**< mode flags passed to OCIStmtExecute to facilitate OCI_COMMIT_ON_SUCCESS (@see sqlo_set_autocommit) */
} sqlo_db_struct_t, * sqlo_db_struct_ptr_t;
/**
* This pointer type defines a pointer to a const sqlo_db_struct_t.
*/
typedef const sqlo_db_struct_t * const_sqlo_db_struct_ptr_t;
/**
* Stores information about a column in the select list.
* This structure is not used if you bind the select list manually be
* @ref sqlo_define_by_pos or @ref sqlo_define_by_pos2.
*/
typedef struct _sqlo_col_struct {
unsigned pos; /**< The position in the select list (0 based).
* This variable can be used to address the right
* data or ind element in the stmt structure. */
ub2 dtype; /**< The datatype (@ref sqlo_data_types) */
char * col_name; /**< The column name */
unsigned col_name_size; /**< The max allocated length of col_name */
ub2 dbsize; /**< The size in the database */
ub1 prec; /**< The precision */
ub1 scale; /** The scale */
ub1 nullok; /**< Flag: Null allowed */
struct _sqlo_stmt_struct_t * stp; /**< link to the stmt structure (see @ref sqlo_stmt_struct_t) */
} sqlo_col_struct_t, * sqlo_col_struct_ptr_t;
/**
* Stores information about an sql statement.
* This structure stores all OCI handles plus the data buffers used to
* retrieve the data.
*/
typedef struct _sqlo_stmt_struct {
ub4 sth; /**< The own handle that identifies this entry. This
is the plain sth, not the encoded one. */
sqlo_db_struct_ptr_t dbp; /**< The link to the database connection (see @ref sqlo_db_struct_t). */
OCIStmt * stmthp; /**< The OCI statement handle pointer. */
char * stmt; /**< The sql statement. */
unsigned stmt_size; /**< The allocated size of stmt. */
/* INPUT */
OCIBind ** bindpv; /**< The vector of input bind variables. */
unsigned num_bindpv; /**< The number of used entries in bindpv[]. */
unsigned bindpv_size; /**< The current capacity of size of bindpv[] and indp[]. */
ub2 stype; /**< The OCI Statement type (see oci.h). */
short * indpv; /**< Indicator variable vector for input bind variables. */
/* OUTPUT */
sqlo_col_struct_t * ocolsv; /**< The output columns of the select list. */
OCIDefine ** defnpv; /**< The OCI define pointers. */
unsigned num_defnpv; /**< The current number of output variables. */
unsigned defnpv_size; /**< The current size of the arrays defnpv, outv,
outv_size, oindv, rlenv, ocol_namev_size and
ocol_namev_size.
*/
char ** outv; /**< The output columns as a vector.
See @ref sqlo_values.
*/
unsigned * outv_size; /**< The current size of an outv[i] element */
ub2 * oindv; /**< The indicator array for output variables */
ub2 * rlenv; /**< The actual length of a returned column. */
char ** ocol_namev; /**< Output column names.
The actual number of filled entries is num_defnpv.
*/
unsigned * ocol_namev_size; /**< The sizes of a colum name in ocol_namev[]. */
sqlo_cursor_type_e cursor_type; /**< The cursor type see @ref sqlo_cursor_type_e */
/* status flags */
bool_t opened; /**< 1 if the cursor is open, 0 if not. */
bool_t prepared; /**< 1 if the statement is prepared, 0 if not. */
bool_t used; /**< 1 indicates that this structure is in use, 0 if not. */
bool_t still_executing; /**< Is 1 in non-blocking mode and a call
to OCIStmtExecute returned OCI_STILL_EXECUTING
*/
unsigned num_executions; /**< number of times the statement was executed */
} sqlo_stmt_struct_t, * sqlo_stmt_struct_ptr_t;
/**
* This pointer type defines a pointer to a const sqlo_stmt_struct_t.
*/
typedef const sqlo_stmt_struct_t * const_sqlo_stmt_struct_ptr_t;
/**
* Structure to store parameter name, pointer to the variable
* and a optional trigger fct, that is executed when parameter is
* changed.
* @see g_params.
*/
typedef struct {
char * name; /**< parameter name */
enum {INTEGER, STRING} vtyp; /**< type of value */
VOID * value; /**< The address of the value */
int (*trigger_fct) __P((int)); /**< Function that handles this type */
} sqlora_param_t;
/*-------------------------------------------------------------------------
* EXPORTED GLOBALS
*-----------------------------------------------------------------------*/
/* define some variables where the user can check the version */
const unsigned sqlo_major_version = LIBSQLORA8_MAJOR_VERSION;
const unsigned sqlo_minor_version = LIBSQLORA8_MINOR_VERSION;
const unsigned sqlo_micro_version = LIBSQLORA8_MICRO_VERSION;
const unsigned sqlo_interface_age = LIBSQLORA8_INTERFACE_AGE;
const unsigned sqlo_binary_age = LIBSQLORA8_BINARY_AGE;
/* for backward compatibility */
const unsigned sqlora8_major_version = LIBSQLORA8_MAJOR_VERSION;
const unsigned sqlora8_minor_version = LIBSQLORA8_MINOR_VERSION;
const unsigned sqlora8_micro_version = LIBSQLORA8_MICRO_VERSION;
const unsigned sqlora8_interface_age = LIBSQLORA8_INTERFACE_AGE;
const unsigned sqlora8_binary_age = LIBSQLORA8_BINARY_AGE;
/*-------------------------------------------------------------------------
* MODULE GLOBAL VARIABLES
*-----------------------------------------------------------------------*/
/**
* @var _oci_init_mode
* The mode of the library.
* Either OCI_DEFAULT or OCI_THREADED.
*/
static int _oci_init_mode = OCI_DEFAULT; /* the mode we initialize the OCI lib */
/**
* @var _max_long_size
*
* The maxiumum length of a LONG result.
* Can be changed by setting the environment variable SQLORA_LONGSIZE.
* Default is @ref MAX_LONG_SIZE.
*/
static unsigned int _max_long_size = MAX_LONG_SIZE;
/**
* @var _dbv
* The array of @ref sqlo_db_struct_t pointers.
* The array is allocated in @ref sqlo_init with the size of the passed max_db
* parameter.
* The array is proteced by the mutex @ref _dbv_mux when compiled with threading
* enabled.
*/
static sqlo_db_struct_t ** _dbv = NULL; /* array for database connections */
/**
* @var _dbv_size
* The size of the @ref _dbv[].
*/
static unsigned int _dbv_size = 0; /* number of available entries in _dbv[] */
/**
* @var _max_cursors
* The maximum number of cursors for one database connection
*/
static unsigned int _max_cursors;
/**
* @var _env_mux
* A mutex to protect the call to OCIEnvCreate
*/
#ifdef ENABLE_THREADS
# ifdef ENABLE_PTHREADS
static pthread_mutex_t _env_mux = PTHREAD_MUTEX_INITIALIZER;
# else
# ifdef ENABLE_ORATHREADS
static OCIThreadMutex *_env_mux;
# else
# ifdef ENABLE_WINTHREADS
static HANDLE _env_mux;
# endif
# endif
# endif
#endif
/**
* @var _dbv_mux
* A mutex to protect @ref _dbv.
* @see _dbv_lock, _dbv_unlock
*/
#ifdef ENABLE_THREADS
# ifdef ENABLE_PTHREADS
static pthread_mutex_t _dbv_mux = PTHREAD_MUTEX_INITIALIZER;
# else
# ifdef ENABLE_ORATHREADS
static OCIThreadMutex *_dbv_mux;
# else
# ifdef ENABLE_WINTHREADS
static HANDLE _dbv_mux;
# endif
# endif
# endif
#endif
/**
* @var _init_mux
* A mutex to protect @ref _sqlo_init.
*/
#ifdef ENABLE_THREADS
# ifdef ENABLE_PTHREADS
static pthread_mutex_t _init_mux = PTHREAD_MUTEX_INITIALIZER;
# else
# ifdef ENABLE_ORATHREADS
static OCIThreadMutex *_init_mux;
# else
# ifdef ENABLE_WINTHREADS
static HANDLE _init_mux;
# endif
# endif
# endif
#endif
/**
* @var _init_mux_initialized
* Flag indicating if the _init_mux was initialized
*/
#ifdef ENABLE_THREADS
static int _init_mux_initialized = 0; /* Is set to 1 in _init_init_mux */
#endif
/**
* @var _num_prefetch_rows
* The number of rows Oracle should prefetch for us.
* The variable is initialized with @ref DEF_PREFETCH_ROWS, but can
* be changed by the environment variable SQLORA_PREFETCH_ROWS.
*/
static ub4 _num_prefetch_rows = DEF_PREFETCH_ROWS; /* out prefetch value */
/**
* @var _sqlo_init
* A flag indicating if the library was successfully initialized
*/
static int _sqlo_init = 0; /* Is set to 1 by sqlo_init */
/**
* @var _trace_level
*
* The trace level of the library.
* The level is initialized from the environment variable SQLORA_TRACE_LEVEL
*
* <ul>
* <li>0 : Trace disabled (default)
* <li>1 : Print errors to tracefile
* <li>2 : Print function calls to tracefile
* <li>3 : Print more detailed information to tracefile
* <li>4 : Print also malloc/realloc operations.
* </ul>
*/
static int _trace_level = 0;
/**
* @var _trace_file
* The name of the trace file.
* Default is sqlora8.trc, but can be changed by setting the environment
* variable SQLORA_TRACE_FILE
*/
static char _trace_file[MAX_PATH_LEN+1] = "";
/* These handles are needed by the Oracle OCIThread package.
* We cannot use the ones in _dbv, because this is the structure
* we want to protect.
*/
#ifdef ENABLE_ORATHREADS
static OCIEnv *_oci_envhp;
static OCIError *_oci_errhp;
#endif
/**
* @var DEFAULT_TRACE_FNAME
* The default trace filename.
*/
static const char * DEFAULT_TRACE_FNAME = "sqlora8.trc";
static FILE * _trace_fp = NULL; /**< The filepointer of the global tracefile */
static unsigned int _session_count = 0; /**< The nubmer of created sessions. Used to name the trace file */
static char _errmsg[SQLO_MAX_ERRMSG_LEN+1]; /**< The variable for error messages when no dbp->errmsg is available */
/*---------------------------------------------------------------------------
* PROTOTYPES
*--------------------------------------------------------------------------*/
/* functions needed in threaded mode */
static int _init_mutexes __P((void));
static int _dbv_lock __P((void));
static int _dbv_unlock __P((void));
static int _env_lock __P((void));
static int _env_unlock __P((void));
static int _init_lock __P((void));
static int _init_unlock __P((void));
#ifdef ENABLE_WINTHREADS
static int _winmutex_lock __P((sqlo_mutex_t mp));
static int _winmutex_unlock __P((sqlo_mutex_t mp));
#endif
static int _init_init_mux __P((void));
static int _sqlo_getenv __P((void));
static int _save_oci_status __P((sqlo_db_struct_ptr_t dbp,
const char *action,
const char *object,
int lineno));
static int _bind_argv __P(( sqlo_stmt_struct_ptr_t stp, unsigned int argc, const char ** argv));
static int _bind_by_pos __P((sqlo_stmt_struct_ptr_t stp, unsigned int param_pos, int param_type,
const void * param_addr, unsigned int param_size,
short * ind_addr, int is_array));
static int _bind_by_pos2 __P((sqlo_stmt_struct_ptr_t stp, unsigned int param_pos, int param_type,
const void * param_addr, unsigned int param_size,
short * ind_addr, unsigned short * rcode_addr,
unsigned int skip_size));
static void _strip_string __P((char *s, unsigned int len));
static int _define_ocol_by_pos __P((sqlo_stmt_struct_ptr_t stp, sqlo_col_struct_t *colp,
unsigned int pos));
static int _define_output __P((sqlo_stmt_struct_ptr_t stp));
static int _open_global_trace_file __P((void));
#if 0
static int _close_global_trace_file __P((void));
#endif
static int _open_session_trace_file __P((sqlo_db_struct_ptr_t dbp));
static int _close_session_trace_file __P((sqlo_db_struct_ptr_t dbp));
static sqlo_stmt_struct_ptr_t _get_stmt_ptr __P((const_sqlo_db_struct_ptr_t dbp));
static int _stmt_new __P((sqlo_db_struct_ptr_t dbp, const char * stmt, sqlo_stmt_struct_ptr_t *stpp));
static void _stmt_release __P((sqlo_stmt_struct_ptr_t stp));
static void _bindpv_reset __P((sqlo_stmt_struct_ptr_t stp));
static int _stmt_init __P((sqlo_stmt_struct_ptr_t stp, sqlo_db_struct_ptr_t dbp, const char *stmt));
static const char * _get_stmt_type_str __P((int stype));
static int _is_query __P((sqlo_stmt_struct_ptr_t stp));
static int _is_plsql __P((sqlo_stmt_struct_ptr_t stp));
static int _is_prepared __P((sqlo_stmt_struct_ptr_t stp));
static int _is_opened __P((sqlo_stmt_struct_ptr_t stp));
static const char * _get_data_type_str __P((int dtype));
static sqlo_db_struct_ptr_t _db_add __P((void));
static void _db_release __P((sqlo_db_struct_ptr_t dbp));
static int _define_by_pos __P((sqlo_stmt_struct_ptr_t stp, unsigned int value_pos,
int value_type, const void * value_addr,
unsigned int value_size, short * ind_addr,
ub2 * rlen_addr, ub2 * rcode_addr, int is_array));
static int _define_by_pos2 __P((sqlo_stmt_struct_ptr_t stp, unsigned int value_pos,
int value_type, const void * value_addr,
unsigned int value_size, short * ind_addr,
ub2 * rlen_addr, ub2 * rcode_addr,
unsigned int skip_size));
static int _calc_obuf_size __P(( unsigned int *bufsizep, unsigned int data_type,
int prec, int scale, unsigned int dbsize));
static int _get_blocking_mode __P((sqlo_db_struct_ptr_t dbp, unsigned * blocking));
static int _get_errcode __P(( sqlo_db_struct_ptr_t dbp));
static int _set_prefetch_rows __P((sqlo_stmt_struct_ptr_t stp, unsigned int nrows));
static const char * _get_stmt_string __P((sqlo_stmt_struct_ptr_t stp));
static int _get_stmt_state __P((sqlo_stmt_struct_ptr_t stp));
static int _alloc_definep __P((sqlo_stmt_struct_ptr_t stp, unsigned int size));
static void _dealloc_definep __P((sqlo_stmt_struct_ptr_t stp));
static int _alloc_bindp __P((sqlo_stmt_struct_ptr_t stp, unsigned int size));
static void _dealloc_bindp __P((sqlo_stmt_struct_ptr_t stp));
static void _close_all_db_cursors __P((const_sqlo_db_struct_ptr_t dbp));
static void _close_all_executing_cursors __P((const_sqlo_db_struct_ptr_t dbp));
static FILE * _get_trace_fp __P((const_sqlo_db_struct_ptr_t dbp));
static int _prepare __P((sqlo_stmt_struct_ptr_t stp, const char * stmt, ub2 * stmt_type));
static sqlo_thread_t _get_thread_id __P((void));
static bool_t _thread_id_equal __P(( sqlo_thread_t id1, sqlo_thread_t id2));
static sqlo_stmt_struct_ptr_t _sth2stp __P((int sth, const char *func_name ));
static int _sqlo_reopen __P(( sqlo_stmt_struct_ptr_t stp, int argc, const char ** argv));
static int _get_ocol_db_data_type __P((sqlo_stmt_struct_ptr_t stp, unsigned int pos, ub2 * dtypep));
static int _get_ocol_db_size __P((sqlo_stmt_struct_ptr_t stp, unsigned int pos, ub2 * sizep));
static int _get_ocol_db_prec __P((sqlo_stmt_struct_ptr_t stp, unsigned int pos, ub1 * precp));
static int _get_ocol_db_scale __P((sqlo_stmt_struct_ptr_t stp, unsigned int pos, ub1 * scalep));
static int _get_ocol_db_is_null __P((sqlo_stmt_struct_ptr_t stp, unsigned int pos, ub1 * is_nullp));
static int _set_ocol_name __P((sqlo_stmt_struct_ptr_t stp, sqlo_col_struct_t * colp, unsigned int pos));
static int _set_all_ocol_names __P((sqlo_stmt_struct_ptr_t stp));
static int _find_free_dbv_entry __P((unsigned int * free_idxp));
static int _db_alloc __P((unsigned int dbv_idx));
static int _stmtv_alloc __P((unsigned int dbv_idx));
static void _terminate_ocols __P((sqlo_stmt_struct_ptr_t stp,
int do_strip_string));