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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. 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. |
| |
| 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. |
+----------------------------------------------------------------------------+
*/
#include "gate/data/odbc_adapter.h"
#include "gate/results.h"
#include "gate/libraries.h"
#include "gate/arrays.h"
#include "gate/debugging.h"
#include "gate/guids.h"
#include "gate/times.h"
#include "gate/data/adapter_base.h"
#if defined(GATE_DATA_ODBC_ENABLED)
# if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WINSTORE) && !defined(GATE_SYS_ANDROID) && !defined(GATE_SYS_DOS) && !defined(GATE_SYS_EFI) && !defined(GATE_SYS_OPENBSD) && !defined(GATE_SYS_WASM) && !defined(GATE_SYS_BEOS)
# define GATE_DATA_ODBC_IMPL
# endif
#endif
#ifdef GATE_DATA_ODBC_IMPL
#include "gate/platforms.h"
#include <sql.h>
#include <sqlext.h>
#if defined(GATE_COMPILER_MSVC98)
# ifdef _WIN64
typedef INT64 SQLLEN;
typedef UINT64 SQLULEN;
# else
# define SQLLEN SQLINTEGER
# define SQLULEN SQLUINTEGER
# endif
#endif
typedef struct gate_odbc_functions
{
SQLRETURN(SQL_API* AllocConnect)(SQLHENV EnvironmentHandle, SQLHDBC* ConnectionHandle);
SQLRETURN(SQL_API* FreeEnv)(SQLHENV EnvironmentHandle);
SQLRETURN(SQL_API* Connect)(SQLHDBC ConnectionHandle, SQLTCHAR* ServerName, SQLSMALLINT NameLength1,
SQLTCHAR* UserName, SQLSMALLINT NameLength2, SQLTCHAR* Authentication, SQLSMALLINT NameLength3);
SQLRETURN(SQL_API* FreeConnect)(SQLHDBC ConnectionHandle);
SQLRETURN(SQL_API* Disconnect)(SQLHDBC ConnectionHandle);
SQLRETURN(SQL_API* AllocStmt)(SQLHDBC ConnectionHandle, SQLHSTMT* StatementHandle);
SQLRETURN(SQL_API* Fetch)(SQLHSTMT StatementHandle);
SQLRETURN(SQL_API* FreeStmt)(SQLHSTMT StatementHandle, SQLUSMALLINT Option);
SQLRETURN(SQL_API* GetData)(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber,
SQLSMALLINT TargetType, SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN* StrLen_or_Ind);
SQLRETURN(SQL_API* DescribeCol)(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLTCHAR* ColumnName,
SQLSMALLINT BufferLength, SQLSMALLINT* NameLength, SQLSMALLINT* DataType,
SQLULEN* ColumnSize, SQLSMALLINT* DecimalDigits, SQLSMALLINT* Nullable);
SQLRETURN(SQL_API* AllocEnv)(SQLHENV* EnvironmentHandle);
SQLRETURN(SQL_API* Prepare)(SQLHSTMT StatementHandle, SQLTCHAR* StatementText, SQLINTEGER TextLength);
SQLRETURN(SQL_API* Execute)(SQLHSTMT StatementHandle);
SQLRETURN(SQL_API* GetDiagRec)(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLTCHAR* Sqlstate,
SQLINTEGER* NativeError, SQLTCHAR* MessageText, SQLSMALLINT BufferLength, SQLSMALLINT* TextLength);
SQLRETURN(SQL_API* NumResultCols)(SQLHSTMT StatementHandle, SQLSMALLINT* ColumnCount);
SQLRETURN(SQL_API* BindParameter)(SQLHSTMT hstmt, SQLUSMALLINT ipar, SQLSMALLINT fParamType, SQLSMALLINT fCType,
SQLSMALLINT fSqlType, SQLULEN cbColDef, SQLSMALLINT ibScale, SQLPOINTER rgbValue, SQLLEN cbValueMax, SQLLEN* pcbValue);
SQLRETURN(SQL_API* NumParams)(SQLHSTMT StatementHandle, SQLSMALLINT* ParameterCountPtr);
SQLRETURN(SQL_API* RowCount)(SQLHSTMT StatementHandle, SQLLEN* RowCount);
} gate_odbc_functions_t;
static gate_odbc_functions_t SQL = GATE_INIT_EMPTY;
static const gate_string_t lib_name = GATE_STRING_INIT_STATIC("odbc32");
#define Name_AllocConnect "SQLAllocConnect"
#define Name_FreeEnv "SQLFreeEnv"
#define Name_FreeConnect "SQLFreeConnect"
#define Name_Disconnect "SQLDisconnect"
#define Name_AllocStmt "SQLAllocStmt"
#define Name_Fetch "SQLFetch"
#define Name_FreeStmt "SQLFreeStmt"
#define Name_GetData "SQLGetData"
#define Name_AllocEnv "SQLAllocEnv"
#define Name_Execute "SQLExecute"
#define Name_NumResultCols "SQLNumResultCols"
#define Name_BindParameter "SQLBindParameter"
#define Name_NumParams "SQLNumParams"
#define Name_RowCount "SQLRowCount"
#if defined(GATE_WIN32_UNICODE)
# define Name_Connect "SQLConnectW"
# define Name_DescribeCol "SQLDescribeColW"
# define Name_Prepare "SQLPrepareW"
# define Name_GetDiagRec "SQLGetDiagRecW"
#else
# define Name_Connect "SQLConnect"
# define Name_DescribeCol "SQLDescribeCol"
# define Name_Prepare "SQLPrepare"
# define Name_GetDiagRec "SQLGetDiagRec"
#endif
static gate_result_t load_odbc_functions()
{
static gate_library_t odbc_lib = NULL;
gate_result_t ret = GATE_RESULT_FAILED;
gate_library_t lib = NULL;
do
{
if (odbc_lib != NULL)
{
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
ret = gate_library_open(&lib_name, &lib, 0);<--- Variable 'ret' is reassigned a value before the old one has been used.
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_AllocConnect, &SQL.AllocConnect);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_FreeEnv, &SQL.FreeEnv);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_FreeConnect, &SQL.FreeConnect);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_Disconnect, &SQL.Disconnect);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_AllocStmt, &SQL.AllocStmt);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_Fetch, &SQL.Fetch);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_FreeStmt, &SQL.FreeStmt);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_GetData, &SQL.GetData);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_AllocEnv, &SQL.AllocEnv);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_Execute, &SQL.Execute);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_NumResultCols, &SQL.NumResultCols);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_Connect, &SQL.Connect);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_DescribeCol, &SQL.DescribeCol);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_Prepare, &SQL.Prepare);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_GetDiagRec, &SQL.GetDiagRec);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_BindParameter, &SQL.BindParameter);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_NumParams, &SQL.NumParams);
GATE_BREAK_IF_FAILED(ret);
ret = gate_library_get_function_name(lib, Name_RowCount, &SQL.RowCount);
GATE_BREAK_IF_FAILED(ret);
odbc_lib = lib;
lib = NULL;
ret = GATE_RESULT_OK;
} while (0);
if (lib != NULL)
{
gate_library_close(lib);
}
return ret;
}
static gate_bool_t gate_data_reader_odbc_is_valid(gate_data_reader_base_t* reader)
{
SQLHSTMT stmt = (SQLHSTMT)reader->native_handles[0];
return stmt != NULL;
}
static gate_result_t gate_data_reader_odbc_close(gate_data_reader_base_t* reader)
{
reader->native_handles[0] = NULL;
return GATE_RESULT_OK;
}
static SQLSMALLINT gate_data_reader_odbc_patch_sql_type(SQLSMALLINT defined_type)
{
/* some types are defined but in SQLGetData another type must be used */
switch (defined_type)
{
case SQL_DECIMAL:
case SQL_NUMERIC:
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR: return SQL_CHAR;
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR: return SQL_WCHAR;
default: break;
}
return defined_type;
}
static gate_value_t* gate_data_reader_odbc_get_value(SQLHSTMT hstmt, SQLUSMALLINT index, SQLSMALLINT sql_type, gate_value_t* value)
{
gate_value_t* ret = NULL;
char buffer[8192] = GATE_INIT_EMPTY;
SQLLEN buffer_used = 0;
SQLRETURN sql_res;
gate_bool_t bool_val;
gate_strbuilder_t text_builder = GATE_INIT_EMPTY;
gate_string_t text = GATE_STRING_INIT_EMPTY;
gate_arraylist_t arrlst = NULL;
gate_array_t arr = GATE_INIT_EMPTY;
SQLGUID const* ptr_sql_guid = NULL;
gate_guid_t guid;
DATE_STRUCT const* ptr_date_struct = NULL;
TIME_STRUCT const* ptr_time_struct = NULL;
TIMESTAMP_STRUCT const* ptr_timestamp_struct = NULL;
gate_datetime_t dt;
sql_type = gate_data_reader_odbc_patch_sql_type(sql_type);
sql_res = SQL.GetData(hstmt, index, sql_type, buffer, sizeof(buffer), &buffer_used);
switch (sql_res)
{
case SQL_SUCCESS:
case SQL_NO_DATA:
case SQL_SUCCESS_WITH_INFO:
{
/* OK, continue */
break;
}
default:
{
/* error case */
return ret;
}
}
switch (sql_type)
{
case SQL_C_STINYINT:
case SQL_TINYINT: { ret = gate_value_create(GATE_TYPE_I8, buffer, value); break; }
case SQL_C_SSHORT:
case SQL_SMALLINT: { ret = gate_value_create(GATE_TYPE_I16, buffer, value); break; }
case SQL_C_SLONG:
case SQL_INTEGER: { ret = gate_value_create(GATE_TYPE_I32, buffer, value); break; }
case SQL_C_SBIGINT:
case SQL_BIGINT: { ret = gate_value_create(GATE_TYPE_I64, buffer, value); break; }
case SQL_REAL: { ret = gate_value_create(GATE_TYPE_R32, buffer, value); break; }
case SQL_FLOAT: { ret = gate_value_create(GATE_TYPE_R32, buffer, value); break; }
case SQL_DOUBLE: { ret = gate_value_create(GATE_TYPE_R64, buffer, value); break; }
case SQL_C_UTINYINT: { ret = gate_value_create(GATE_TYPE_UI8, buffer, value); break; }
case SQL_C_USHORT: { ret = gate_value_create(GATE_TYPE_UI16, buffer, value); break; }
case SQL_C_ULONG: { ret = gate_value_create(GATE_TYPE_UI32, buffer, value); break; }
case SQL_C_UBIGINT: { ret = gate_value_create(GATE_TYPE_UI64, buffer, value); break; }
case SQL_BIT:
{
bool_val = buffer[0] == 0 ? false : true;
ret = gate_value_create(GATE_TYPE_BOOL, &bool_val, value);
break;
}
case SQL_DECIMAL:
case SQL_NUMERIC:
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
{
/* extract byte data */
gate_strbuilder_create(&text_builder, 0);
gate_strbuilder_append_text(&text_builder, &buffer[0], buffer_used);
while (sql_res == SQL_SUCCESS_WITH_INFO)
{
sql_res = SQL.GetData(hstmt, index, sql_type, buffer, sizeof(buffer), &buffer_used);
gate_strbuilder_append_text(&text_builder, &buffer[0], buffer_used);
}
gate_strbuilder_to_string(&text_builder, &text);
ret = gate_value_create(GATE_TYPE_STRING, &text, value);
gate_string_release(&text);
gate_strbuilder_release(&text_builder);
break;
}
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
{
/* extract UTF16 text */
gate_strbuilder_create(&text_builder, 0);
gate_strbuilder_append_text16(&text_builder, (gate_char16_t const*)&buffer[0], buffer_used / sizeof(gate_char16_t));
while (sql_res == SQL_SUCCESS_WITH_INFO)
{
sql_res = SQL.GetData(hstmt, index, sql_type, buffer, sizeof(buffer), &buffer_used);
gate_strbuilder_append_text16(&text_builder, (gate_char16_t const*)&buffer[0], buffer_used / sizeof(gate_char16_t));
}
gate_strbuilder_to_string(&text_builder, &text);
ret = gate_value_create(GATE_TYPE_STRING, &text, value);
gate_string_release(&text);
gate_strbuilder_release(&text_builder);
break;
}
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
{
arrlst = gate_arraylist_create(1, buffer, buffer_used, NULL, NULL);
if (arrlst)
{
while (sql_res == SQL_SUCCESS_WITH_INFO)
{
sql_res = SQL.GetData(hstmt, index, sql_type, buffer, sizeof(buffer), &buffer_used);
gate_arraylist_add_n(arrlst, buffer, buffer_used);
}
if (NULL != gate_array_create(&arr, arrlst))
{
ret = gate_value_create(GATE_TYPE_ARRAY, &arr, value);
gate_array_release(&arr);
}
gate_arraylist_release(arrlst);
}
break;
}
case SQL_GUID:
{
ptr_sql_guid = (SQLGUID const*)&buffer[0];
guid.item1 = ptr_sql_guid->Data1;
guid.item2 = ptr_sql_guid->Data2;
guid.item3 = ptr_sql_guid->Data3;
gate_mem_copy(guid.item4, ptr_sql_guid->Data4, 8);
ret = gate_value_create(GATE_TYPE_GUID, &guid, value);
break;
}
case SQL_TYPE_DATE:
{
ptr_date_struct = (DATE_STRUCT const*)&buffer[0];
dt.date.year = ptr_date_struct->year;
dt.date.month = (gate_uint8_t)ptr_date_struct->month;
dt.date.day = (gate_uint8_t)ptr_date_struct->day;
ret = gate_value_create(GATE_TYPE_DATE, &dt.date, value);
break;
}
case SQL_TYPE_TIME:
{
ptr_time_struct = (TIME_STRUCT const*)&buffer[0];
dt.time.hour = (gate_uint8_t)ptr_time_struct->hour;
dt.time.minute = (gate_uint8_t)ptr_time_struct->minute;
dt.time.second = (gate_uint8_t)ptr_time_struct->second;
dt.time.microsecond = 0;
ret = gate_value_create(GATE_TYPE_DAYTIME, &dt.time, value);
break;
}
case SQL_TYPE_TIMESTAMP:
{
ptr_timestamp_struct = (TIMESTAMP_STRUCT const*)&buffer[0];
dt.date.year = ptr_timestamp_struct->year;
dt.date.month = (gate_uint8_t)ptr_timestamp_struct->month;
dt.date.day = (gate_uint8_t)ptr_timestamp_struct->day;
dt.time.hour = (gate_uint8_t)ptr_timestamp_struct->hour;
dt.time.minute = (gate_uint8_t)ptr_timestamp_struct->minute;
dt.time.second = (gate_uint8_t)ptr_timestamp_struct->second;
dt.time.microsecond = ptr_timestamp_struct->fraction / 1000; /*fraction == nanoseconds */
ret = gate_value_create(GATE_TYPE_DAYTIME, &dt.time, value);
break;
}
default:
{
/* not supported */
break;
}
}
return ret;
}
static gate_result_t gate_data_reader_odbc_next(gate_data_reader_base_t* reader)
{
gate_result_t ret = GATE_RESULT_FAILED;
SQLHSTMT hstmt = (SQLHSTMT)reader->native_handles[0];
SQLRETURN sql_res;
gate_size_t ndx;
do
{
if (hstmt == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
break;
}
sql_res = SQL.Fetch(hstmt);
if (SQL_SUCCESS != sql_res)
{
ret = GATE_RESULT_FAILED;
break;
}
ret = GATE_RESULT_OK;
for (ndx = 0; ndx != reader->field_count; ++ndx)
{
gate_value_release(&reader->values[ndx]);
gate_data_reader_odbc_get_value(hstmt, (SQLUSMALLINT)(ndx + 1), (SQLUSMALLINT)reader->types[ndx], &reader->values[ndx]);
}
} while (0);
return ret;
}
static gate_result_t gate_data_reader_odbc_init(gate_data_reader_base_t* reader)
{
gate_result_t ret = GATE_RESULT_OK;
SQLSMALLINT col_count = 0;
SQLSMALLINT col_index;
SQLRETURN sql_ret;
SQLHSTMT hstmt = (SQLHSTMT)reader->native_handles[0];
SQLTCHAR colname[1024];
SQLSMALLINT colname_len;
SQLSMALLINT coltype;
SQLULEN colvalue_len;
SQLSMALLINT colvalue_digits;
SQLSMALLINT colvalue_nullable;
do
{
sql_ret = SQL.NumResultCols(hstmt, &col_count);
if (SQL_SUCCESS != sql_ret)
{
ret = GATE_RESULT_FAILED;
break;
}
reader->field_count = col_count;
for (col_index = 0; col_index != col_count; ++col_index)
{
colname_len = sizeof(colname) / sizeof(colname[0]);
coltype = 0;
colvalue_len = 0;
colvalue_digits = 0;
colvalue_nullable = 0;
sql_ret = SQL.DescribeCol(hstmt, (SQLUSMALLINT)(col_index + 1), colname, colname_len, &colname_len,
&coltype, &colvalue_len, &colvalue_digits, &colvalue_nullable);
#if defined(GATE_SYS_WIN)
gate_win32_winstr_2_utf8_string((LPCTSTR)&colname[0], colname_len, &reader->names[col_index]);
#else
gate_string_create(&reader->names[col_index], (char const*)&colname[0], colname_len);
#endif
reader->types[col_index] = coltype;
}
} while (0);
return ret;
}
static gate_data_reader_t* gate_data_reader_odbc_create(gate_data_statement_base_t* ptr_statement, SQLHSTMT stmt)
{
gate_data_reader_t* ret = NULL;
gate_data_handles_t handles = GATE_INIT_EMPTY;
handles[0] = (void*)stmt;
ret = gate_data_reader_base_create(
(gate_data_statement_t*)ptr_statement, handles,
&gate_data_reader_odbc_init,
&gate_data_reader_odbc_is_valid,
&gate_data_reader_odbc_next,
&gate_data_reader_odbc_close);
return ret;
}
static gate_result_t odbcstmt_init(gate_data_statement_base_t* self)
{
return GATE_RESULT_OK;
}
static void odbcstmt_close(gate_data_statement_base_t* self)
{
SQLHSTMT hstmt = (SQLHSTMT)self->native_handles[1];
if (hstmt != NULL)
{
SQL.FreeStmt(hstmt, SQL_CLOSE);
SQL.FreeStmt(hstmt, SQL_DROP);
self->native_handles[1] = NULL;
}
}
static gate_result_t odbcstmt_convert_result(SQLRETURN sql_result)
{
if (SQL_SUCCEEDED(sql_result))
{
return GATE_RESULT_OK;
}
else
{
/* TODO: detailed error analysis */
return GATE_RESULT_FAILED;
}
}
static gate_result_t odbcstmt_set_null_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index)
{
SQLINTEGER param1 = 123;
SQLLEN param2Ind = SQL_NULL_DATA;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0, NULL, 0, ¶m2Ind);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_bigint_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, gate_int64_t value)
{
SQLBIGINT bigint = value;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, 0, 0, &bigint, 0, NULL);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_int_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, gate_int32_t value)
{
SQLINTEGER n = value;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &n, 0, NULL);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_real_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, gate_real64_t value)
{
SQLDOUBLE n = value;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, &n, 0, NULL);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_text_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, char const* text, gate_size_t textlen)
{
SQLPOINTER ptr_text = (SQLPOINTER)text;
SQLULEN len = (SQLULEN)textlen;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, len, 0, ptr_text, 0, NULL);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_binary_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, void const* data, gate_size_t datalen)
{
SQLPOINTER ptr_text = (SQLPOINTER)data;
SQLULEN len = (SQLULEN)datalen;
SQLLEN lenret = (SQLLEN)datalen;
SQLRETURN sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, len, 0, ptr_text, 0, &lenret);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_datetime_param(SQLHSTMT hstmt, SQLUSMALLINT odbc_index, gate_datetime_t const* dt)
{
SQLRETURN sql_result;
SQL_TIMESTAMP_STRUCT sql_dt;
sql_dt.year = dt->date.year;
sql_dt.month = dt->date.month;
sql_dt.day = dt->date.day;
sql_dt.hour = dt->time.hour;
sql_dt.minute = dt->time.minute;
sql_dt.second = dt->time.second;
sql_dt.fraction = 0;
sql_result = SQL.BindParameter(
hstmt, odbc_index, SQL_PARAM_INPUT, SQL_C_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP, 0, 0, &sql_dt, 0, NULL);
return odbcstmt_convert_result(sql_result);
}
static gate_result_t odbcstmt_set_param(gate_data_statement_base_t* self, gate_size_t index, gate_value_t const* value)
{
SQLHSTMT hstmt = (SQLHSTMT)self->native_handles[1];
SQLUSMALLINT odbc_index = (SQLUSMALLINT)(index + 1);
gate_cstrbuffer8_t buffer;
gate_datetime_t dt = GATE_INIT_EMPTY;
gate_result_t ret = GATE_RESULT_FAILED;
if ((value == NULL) || (value->value_type == GATE_TYPE_EMPTY))
{
return odbcstmt_set_null_param(hstmt, odbc_index);
}
switch (value->value_type)
{
case GATE_TYPE_BOOL: return odbcstmt_set_int_param(hstmt, odbc_index, value->content.bool_value ? 1 : 0);
case GATE_TYPE_I8: return odbcstmt_set_int_param(hstmt, odbc_index, (gate_int32_t)value->content.i8_value);
case GATE_TYPE_I16: return odbcstmt_set_int_param(hstmt, odbc_index, (gate_int32_t)value->content.i16_value);
case GATE_TYPE_I32: return odbcstmt_set_int_param(hstmt, odbc_index, (gate_int32_t)value->content.i32_value);
case GATE_TYPE_I64: return odbcstmt_set_bigint_param(hstmt, odbc_index, (gate_int32_t)value->content.i64_value);
case GATE_TYPE_UI8: return odbcstmt_set_int_param(hstmt, odbc_index, (gate_int32_t)value->content.ui8_value);
case GATE_TYPE_UI16: return odbcstmt_set_int_param(hstmt, odbc_index, (gate_int32_t)value->content.ui16_value);
case GATE_TYPE_UI32: return odbcstmt_set_bigint_param(hstmt, odbc_index, (gate_int64_t)value->content.ui32_value);
case GATE_TYPE_UI64: return odbcstmt_set_bigint_param(hstmt, odbc_index, (gate_int64_t)value->content.ui64_value);
case GATE_TYPE_R32: return odbcstmt_set_real_param(hstmt, odbc_index, (gate_real64_t)value->content.r32_value);
case GATE_TYPE_R64: return odbcstmt_set_real_param(hstmt, odbc_index, (gate_real64_t)value->content.r64_value);
case GATE_TYPE_BLOB: return odbcstmt_set_binary_param(hstmt, odbc_index, value->content.blob_value.data, value->content.blob_value.length);
case GATE_TYPE_CSTR: return odbcstmt_set_text_param(hstmt, odbc_index, value->content.cstring_value, gate_str_length(value->content.cstring_value));
case GATE_TYPE_STRING: return odbcstmt_set_text_param(hstmt, odbc_index, value->content.string_value.str, value->content.string_value.length);
case GATE_TYPE_WSTR:
{
if (NULL == gate_cstrbuffer_create_wstr(&buffer, value->content.wstring_value, gate_wstr_length(value->content.wstring_value)))
{
return GATE_RESULT_OUTOFMEMORY;
}
ret = odbcstmt_set_text_param(hstmt, odbc_index, gate_cstrbuffer_get(&buffer), gate_cstrbuffer_length(&buffer));
gate_cstrbuffer_destroy(&buffer);
return ret;
}
case GATE_TYPE_GUID:
{
ret = gate_guid_to_string(&value->content.guid_value, buffer.local_buffer);
GATE_RETURN_IF_FAILED(ret);
return odbcstmt_set_text_param(hstmt, odbc_index, buffer.local_buffer, gate_str_length(buffer.local_buffer));
}
case GATE_TYPE_DATE:
{
dt.date = value->content.date_value;
return odbcstmt_set_datetime_param(hstmt, odbc_index, &dt);
}
case GATE_TYPE_DATETIME:
{
return odbcstmt_set_datetime_param(hstmt, odbc_index, &value->content.datetime_value);
}
case GATE_TYPE_TIME:
{
ret = gate_time_to_datetime(&value->content.time_value, &dt);
GATE_RETURN_IF_FAILED(ret);
return odbcstmt_set_datetime_param(hstmt, odbc_index, &dt);
}
case GATE_TYPE_PROPERTY:
default:
return GATE_RESULT_INCORRECTTYPE;
}
}
static gate_result_t odbcstmt_set_named_param(gate_data_statement_base_t* self, gate_string_t const* name, gate_value_t const* value)
{
/* named parameters are not supported by odbc */
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t odbcstmt_reset(gate_data_statement_base_t* self)
{
SQLRETURN sql_ret;
SQLHSTMT hstmt = (SQLHSTMT)self->native_handles[1];
if (hstmt == NULL)
{
return GATE_RESULT_INVALIDSTATE;
}
sql_ret = SQL.FreeStmt(hstmt, SQL_RESET_PARAMS);
switch (sql_ret)
{
case SQL_SUCCESS:
case SQL_SUCCESS_WITH_INFO:
return GATE_RESULT_OK;
default:
return GATE_RESULT_FAILED;
}
}
static gate_result_t odbcstmt_execute(gate_data_statement_base_t* self, gate_int32_t* affected_rows)
{
SQLRETURN sql_ret;
SQLHSTMT hstmt = (SQLHSTMT)self->native_handles[1];
SQLLEN len_rows = 0;
if (hstmt == NULL)
{
return GATE_RESULT_INVALIDSTATE;
}
sql_ret = SQL.Execute(hstmt);
if (SQL_SUCCESS != sql_ret)
{
return GATE_RESULT_FAILED;
}
/* success case */
if (affected_rows)
{
SQL.RowCount(hstmt, &len_rows);
*affected_rows = (gate_int32_t)len_rows;
}
return GATE_RESULT_OK;
}
static gate_result_t odbcstmt_query(gate_data_statement_base_t* self, gate_data_reader_t** ptr_reader)
{
SQLRETURN sql_ret;
SQLHSTMT hstmt = (SQLHSTMT)self->native_handles[1];
SQLLEN len_rows = 0;
if (hstmt == NULL)
{
return GATE_RESULT_INVALIDSTATE;
}
sql_ret = SQL.Execute(hstmt);
if (SQL_SUCCESS != sql_ret)
{
return GATE_RESULT_FAILED;
}
if (ptr_reader == NULL)
{
/* success, reader not required, free unused resources */
SQL.FreeStmt(hstmt, SQL_CLOSE);
SQL.FreeStmt(hstmt, SQL_DROP);
return GATE_RESULT_OK;
}
*ptr_reader = gate_data_reader_odbc_create(self, hstmt);
if (*ptr_reader == NULL)
{
/* failed to allocate data reader */
SQL.FreeStmt(hstmt, SQL_CLOSE);
SQL.FreeStmt(hstmt, SQL_DROP);
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_data_connection_odbc_create_statement(gate_data_connection_base_t* self, gate_string_t const* sql, gate_data_statement_t** ptr_stmt)
{
gate_result_t ret = GATE_RESULT_FAILED;
SQLHSTMT hstmt = NULL;
SQLHDBC hdbc = self->native_handles[0];
SQLRETURN sql_ret;
SQLTCHAR cmd[4096 + 2048 + 1024];
SQLSMALLINT param_count = 0;
gate_size_t cmd_len;
gate_data_handles_t handles;
gate_data_statement_base_t* ptr_stmt_base = NULL;
do
{
#if defined(GATE_SYS_WIN)
cmd_len = gate_win32_utf8_2_winstr(sql->str, sql->length, (LPTSTR)&cmd[0], sizeof(cmd) / sizeof(cmd[0]));
#else
cmd_len = gate_string_to_buffer(sql, (char*)&cmd[0], sizeof(cmd) / sizeof(cmd[0]));
#endif
sql_ret = SQL.AllocStmt(hdbc, &hstmt);
if (SQL_SUCCESS != sql_ret)
{
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
sql_ret = SQL.Prepare(hstmt, cmd, (SQLINTEGER)cmd_len);
if (SQL_SUCCESS != sql_ret)
{
ret = GATE_RESULT_FAILED;
break;
}
gate_mem_clear(&handles[0], sizeof(handles));
handles[0] = (void*)hdbc;
handles[1] = (void*)hstmt;
ret = gate_data_statement_base_create(
self, handles, sql,
&odbcstmt_init,
&odbcstmt_close,
&odbcstmt_set_param,
&odbcstmt_set_named_param,
&odbcstmt_reset,
&odbcstmt_execute,
&odbcstmt_query,
&ptr_stmt_base);
GATE_BREAK_IF_FAILED(ret);
if (SQL_ERROR != SQL.NumParams(hstmt, ¶m_count))
{
ptr_stmt_base->param_count = param_count;
}
/* success case: */
if (ptr_stmt != NULL)
{
*ptr_stmt = (gate_data_statement_t*)ptr_stmt_base;
ptr_stmt_base = NULL;
}
hstmt = NULL;
ret = GATE_RESULT_OK;
} while (0);
if (ptr_stmt_base != NULL)
{
gate_object_release(ptr_stmt_base);
}
if (hstmt != NULL)
{
SQL.FreeStmt(hstmt, SQL_CLOSE);
SQL.FreeStmt(hstmt, SQL_DROP);
}
return ret;
}
static gate_result_t gate_data_connection_odbc_init(gate_data_connection_base_t* self)
{
GATE_UNUSED_ARG(self);
return GATE_RESULT_OK;
}
static void gate_data_connection_odbc_close(gate_data_connection_base_t* self)
{
SQLHDBC hdbc = self->native_handles[0];
SQLHENV henv = self->native_handles[1];
if (hdbc != NULL)
{
SQL.Disconnect(hdbc);
SQL.FreeConnect(hdbc);
}
if (henv != NULL)
{
SQL.FreeEnv(henv);
}
gate_mem_clear(self->native_handles, sizeof(self->native_handles));
}
gate_result_t gate_data_adapter_odbc_create(gate_string_t const* path, gate_enumint_t flags, gate_data_connection_t** ptr_connection)
{
gate_result_t ret = GATE_RESULT_OK;
SQLTCHAR str_connection[4096];
gate_size_t str_connection_len;
SQLTCHAR* str_user = NULL;
gate_size_t str_user_len = 0;
SQLTCHAR* str_pass = NULL;
gate_size_t str_pass_len = 0;
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLRETURN sql_ret;
gate_data_handles_t handles = GATE_INIT_EMPTY;
do
{
ret = load_odbc_functions();
GATE_BREAK_IF_FAILED(ret);
#if defined(GATE_SYS_WIN)
str_connection_len = gate_win32_utf8_2_winstr(path->str, path->length, (LPTSTR)&str_connection[0], sizeof(str_connection) / sizeof(str_connection[0]));
#else
str_connection_len = gate_string_to_buffer(path, (char*)&str_connection[0], sizeof(str_connection) / sizeof(str_connection[0]));
#endif
sql_ret = SQL.AllocEnv(&henv);
if (SQL_SUCCESS != sql_ret)
{
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
sql_ret = SQL.AllocConnect(henv, &hdbc);
if (SQL_SUCCESS != sql_ret)
{
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
sql_ret = SQL.Connect(hdbc, str_connection, (SQLSMALLINT)str_connection_len,
str_user, (SQLSMALLINT)str_user_len, str_pass, (SQLSMALLINT)str_pass_len);
if ((SQL_SUCCESS != sql_ret) && (SQL_SUCCESS_WITH_INFO != sql_ret))
{
SQLTCHAR state_msg[1024];
SQLINTEGER native_error = 0;
SQLTCHAR error_msg[1024];
SQLSMALLINT error_msg_used = 0;
SQL.GetDiagRec(SQL_HANDLE_DBC, hdbc, 1, state_msg, &native_error, error_msg, 1024, &error_msg_used);
ret = GATE_RESULT_FAILED;
break;
}
handles[0] = (void*)hdbc;
handles[1] = (void*)henv;
ret = gate_data_connection_base_create(
handles,
&gate_data_connection_odbc_init,
&gate_data_connection_odbc_close,
&gate_data_connection_odbc_create_statement,
ptr_connection);
if (GATE_SUCCEEDED(ret))
{
hdbc = NULL;
henv = NULL;
}
} while (0);
if (hdbc)
{
SQL.Disconnect(hdbc);
SQL.FreeConnect(hdbc);
}
if (henv)
{
SQL.FreeEnv(henv);
}
return ret;
}
#else /* GATE_DATA_ODBC_IMPL */
gate_result_t gate_data_adapter_odbc_create(gate_string_t const* path, gate_enumint_t flags, gate_data_connection_t** ptr_connection)
{
(void)path;
(void)flags;
(void)ptr_connection;
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif
|