-
Notifications
You must be signed in to change notification settings - Fork 9
/
odbc_util.h
276 lines (241 loc) · 10.8 KB
/
odbc_util.h
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
/*
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
*
*/
#ifndef __CUBRID_ODBC_UTIL_HEADER /* to avoid multiple inclusion */
#define __CUBRID_ODBC_UTIL_HEADER
#include "odbc_portable.h"
#define UT_ALLOC(size) ut_alloc(size)
#define UT_REALLOC(ptr, size) ut_realloc(ptr, size)
#define UT_FREE(ptr) ut_free(ptr)
#define UT_MAKE_STRING(ptr, length) ut_make_string(ptr, length)
#define UT_MAKE_BINARY(ptr, length) ut_make_binary(ptr, length)
#define UT_APPEND_STRING(str1, str2, len2) ut_append_string(str1, str2, len2)
#define UT_ALLOC_BSTR(size) ut_alloc_bstr(size)
#define UT_FREE_BSTR(ptr) ut_free_bstr(ptr)
#define UT_SET_DELIMITER ";;"
/* NC_FREE - NULL check free
* NA_FREE - NULL assign free
* MOVE_STRING - free & copy string
* NC_FREE_WH - NULL check free with handler
* NA_FREE_WH - NULL assign free with handler
* NC_FREE_WHO - NULL check free with handler, option
* NA_FREE_WHO - NULL assign free with handler, option
*/
#define NC_FREE(ptr) if ( ptr != NULL ) UT_FREE(ptr)
#define NA_FREE(ptr) \
do { \
if ( ptr != NULL ) { \
UT_FREE(ptr); \
ptr= NULL; \
} \
} while (0)
#define NC_FREE_WH(handle, ptr) if ( ptr != NULL ) handle(ptr)
#define NA_FREE_WH(handle, ptr) \
do { \
if ( ptr != NULL ) { \
handle(ptr); \
ptr = NULL; \
} \
} while (0) \
#define NC_FREE_WHO(handle, ptr, opt) if ( ptr != NULL ) handle(ptr, opt)
#define NA_FREE_WHO(handle, ptr, opt) \
do { \
if ( ptr != NULL ) { \
handle(ptr, opt); \
ptr = NULL; \
} \
} while (0) \
#define UT_COPY_STRING(target, value) \
NC_FREE(target); \
target = UT_MAKE_STRING(value, -1)
#define SET_OPTION(value, option) ((value) |= (option))
#define UNSET_OPTION(value, option) ((value) ^= (option))
#define IS_OPTION_SETTED(value, option) (( (value) & (option) ) == (option))
////////////////// debug log
#ifdef __DEBUG_LOG
#ifdef UNIX
#include <sys/time.h>
#include <stdlib.h>
#define DEBUG_FILE_KEY "DEBUG_TIMESTAMP_FILE"
#define DEBUG_TIMESTAMP(value) \
do { \
struct timeval now; \
FILE *fp = NULL; \
char *pt = NULL; \
pt = getenv(DEBUG_FILE_KEY) \
if ( pt != NULL ) { \
fp = fopen(pt, "a+"); \
gettimeofday(&now, NULL); \
fprintf(fp,#value " %ld.%07ld %s %d\n", now.tv_sec, now.tv_usec, __FILE__, __LINE__); \
fclose(fp); \
} \
} while(0)
#else
#include <sys/types.h>
#include <sys/timeb.h>
#define DEBUG_DETAIL(value) \
do {\
struct _timeb now; \
FILE *fp = NULL; \
fp = fopen(DEBUG_FILE_KEY, "a+"); \
_ftime(&now); \
fprintf(fp, "### [%d] %ld.%07ld %s %d\n", value, now.time, now.millitm, __FILE__, __LINE__); \
fclose(fp); \
} while (0)
#define DEBUG_FILE_KEY "c:\\temp\\time_log.txt"
#define DEBUG_TIMESTAMP(value)
#define DEBUG_TIMESTAMP(value) \
do { \
struct _timeb now; \
FILE *fp = NULL; \
fp = fopen(DEBUG_FILE_KEY, "a+"); \
_ftime(&now); \
fprintf(fp,#value " %ld.%07ld %s %d\n", now.time, now.millitm, __FILE__, __LINE__); \
fclose(fp); \
} while(0)
#endif
#else
#define DEBUG_TIMESTAMP(value)
#endif
#ifdef __DEBUG_LOG
#ifdef UNIX
#include <stdlib.h>
#define DEBUG_LOG_FILE_KEY "DEBUG_LOG_FILE"
#define DEBUG_LOG(value) \
do { \
FILE *fp = NULL; \
char *pt; \
pt = getenv(DEBUG_LOG_FILE_KEY); \
if ( pt != NULL ) { \
fp = fopen(pt, "a+"); \
fprintf(fp,"%s at %s %d\n", value, __FILE__, __LINE__); \
fclose(fp); \
} \
} while(0)
#else
#define DEBUG_LOG_FILE_KEY "c:\\odbc_log.txt"
#define DEBUG_LOG(value) \
do { \
FILE *fp = NULL; \
fp = fopen(DEBUG_LOG_FILE_KEY, "a+"); \
fprintf(fp,"%s at %s %d\n", value, __FILE__, __LINE__); \
fclose(fp); \
} while(0)
#endif
#else
#define DEBUG_LOG(value)
#endif
/* Dynamic string */
typedef struct __st_dynamic_string
{
char *value;
int totalSize;
int usedSize;
} D_STRING;
typedef struct __st_dynamic_binary
{
char *value;
int size;
} D_BINARY;
typedef struct tagST_LIST
{
void *key;
void *value;
struct tagST_LIST *next;
} ST_LIST;
PUBLIC void InitStr (D_STRING * str);
PUBLIC void FreeStr (D_STRING * str);
PUBLIC ERR_CODE MemcatImproved (D_STRING * dest, char *src, int srcSize);
PUBLIC void *ut_alloc (SQLLEN size);
PUBLIC void ut_free (void *ptr);
PUBLIC void *ut_realloc (void *ptr, int size);
PUBLIC char *ut_make_string (const char *src, int length);
PUBLIC char *ut_append_string (char *str1, char *str2, int len2);
PUBLIC char *ut_make_binary (const char *src, int length);
PUBLIC WCHAR* ut_alloc_bstr (SQLLEN size);
PUBLIC void ut_free_bstr (WCHAR *ptr);
PUBLIC int sqlwcharlen(const WCHAR *wstr);
PUBLIC int element_from_setstring (char **current, char *buf);
PUBLIC int add_element_to_setstring (char *setstring, char *element, int size);
PUBLIC char *odbc_trim (char *str);
PUBLIC RETCODE str_value_assign (const char *in_value,
char *out_buf,
SQLLEN out_buf_len, SQLLEN * val_len_ptr);
PUBLIC RETCODE bin_value_assign (const void *in_value,
SQLLEN in_val_len,
char *out_buf,
SQLLEN out_buf_len, SQLLEN * val_len_ptr);
PUBLIC short is_oidstr (char *str);
PUBLIC short is_oidstr_array (char **array, int size);
PUBLIC int replace_oid (char *sql_text, char **org_param_pos_pt,
char **oid_param_pos_pt, char **oid_param_val_pt);
PUBLIC char *remove_owner_name (char *tablename);
extern ERR_CODE ListTailAdd (ST_LIST * head, void *key, void *val,
ERR_CODE (*assignFunc) (ST_LIST *, void *,
void *));
extern void ListDelete (ST_LIST * head, void (*nodeDelete) (ST_LIST *));
extern ERR_CODE ListCreate (ST_LIST ** head);
extern ST_LIST *HeadNode (ST_LIST * dummyHead);
extern ST_LIST *NextNode (ST_LIST * node);
#ifdef _DEBUG
extern void ListPrint (ST_LIST * head, void (*nodePrint) (ST_LIST *));
#endif
/* node assign function */
extern ERR_CODE NodeAssign (ST_LIST * node, void *key, void *value);
/*------------- connection string util ------------------------*/
PUBLIC const char *next_element (const char *element_list);
PUBLIC const char *element_value (const char *element);
PUBLIC const char *element_value_by_key (const char *element_list,
const char *key);
PUBLIC void get_connect_attr (struct st_odbc_connection_attr* attr,
const char* ConnStrIn, char* buffer);
PUBLIC int wide_char_to_bytes (wchar_t *str,
int size,
char **target,
int* out_length,
char* characterset);
PUBLIC int bytes_to_wide_char (char *str,
int size,
wchar_t **buffer,
int buffer_length,
int* out_length,
char* characterset);
PUBLIC int get_wide_char_result (char *str,
int size,
wchar_t **buffer,
int buffer_length,
int* out_length,
char* characterset);
/*--------------------------------------------------------------*/
PUBLIC _BOOL_ is_odd_number(int num);
#ifdef CUBRID_ODBC_UNICODE
PUBLIC int check_if_even_number (SQLUSMALLINT info_type, SQLSMALLINT buffer_length);
PUBLIC int decide_info_value_length (SQLUSMALLINT info_type, int buffer_length, int info_value_length);
#endif
#endif /* ! __CUBRID_ODBC_UTIL_HEADER */