forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodbc_api.cc
More file actions
1649 lines (1364 loc) · 57.9 KB
/
odbc_api.cc
File metadata and controls
1649 lines (1364 loc) · 57.9 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// flight_sql_connection.h needs to be included first due to conflicts with windows.h
#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h"
#include "arrow/flight/sql/odbc/odbc_api_internal.h"
#include "arrow/flight/sql/odbc/odbc_impl/attribute_utils.h"
#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h"
#include "arrow/flight/sql/odbc/odbc_impl/diagnostics.h"
#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h"
#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_driver.h"
#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h"
#include "arrow/flight/sql/odbc/odbc_impl/odbc_descriptor.h"
#include "arrow/flight/sql/odbc/odbc_impl/odbc_environment.h"
#include "arrow/flight/sql/odbc/odbc_impl/odbc_statement.h"
#include "arrow/flight/sql/odbc/odbc_impl/spi/connection.h"
#include "arrow/util/logging.h"
#if defined _WIN32
// For displaying DSN Window
# include "arrow/flight/sql/odbc/odbc_impl/system_dsn.h"
#endif // defined(_WIN32)
namespace arrow::flight::sql::odbc {
void LoadPropertiesFromDSN(const std::string& dsn, Connection::ConnPropertyMap& props);
SQLRETURN SQLAllocHandle(SQLSMALLINT type, SQLHANDLE parent, SQLHANDLE* result) {
ARROW_LOG(DEBUG) << "SQLAllocHandle called with type: " << type
<< ", parent: " << parent
<< ", result: " << static_cast<const void*>(result);
*result = nullptr;
switch (type) {
case SQL_HANDLE_ENV: {
using ODBC::ODBCEnvironment;
*result = SQL_NULL_HENV;
try {
static std::shared_ptr<FlightSqlDriver> odbc_driver =
std::make_shared<FlightSqlDriver>();
*result = reinterpret_cast<SQLHENV>(new ODBCEnvironment(odbc_driver));
return SQL_SUCCESS;
} catch (const std::bad_alloc&) {
// allocating environment failed so cannot log diagnostic error here
return SQL_ERROR;
}
}
case SQL_HANDLE_DBC: {
using ODBC::ODBCConnection;
using ODBC::ODBCEnvironment;
*result = SQL_NULL_HDBC;
ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(parent);
return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() {
std::shared_ptr<ODBCConnection> conn = environment->CreateConnection();
if (conn) {
// Inside `CreateConnection`, the shared_ptr `conn` is kept
// in a `std::vector` of connections inside the environment handle.
// As long as the parent environment handle is alive, the connection shared_ptr
// will be kept alive unless the user frees the connection.
*result = reinterpret_cast<SQLHDBC>(conn.get());
return SQL_SUCCESS;
}
return SQL_ERROR;
});
}
case SQL_HANDLE_STMT: {
using ODBC::ODBCConnection;
using ODBC::ODBCStatement;
*result = SQL_NULL_HSTMT;
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(parent);
return ODBCConnection::ExecuteWithDiagnostics(connection, SQL_ERROR, [=]() {
std::shared_ptr<ODBCStatement> statement = connection->CreateStatement();
if (statement) {
*result = reinterpret_cast<SQLHSTMT>(statement.get());
return SQL_SUCCESS;
}
return SQL_ERROR;
});
}
case SQL_HANDLE_DESC: {
using ODBC::ODBCConnection;
using ODBC::ODBCDescriptor;
*result = SQL_NULL_HDESC;
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(parent);
return ODBCConnection::ExecuteWithDiagnostics(connection, SQL_ERROR, [=]() {
std::shared_ptr<ODBCDescriptor> descriptor = connection->CreateDescriptor();
if (descriptor) {
*result = reinterpret_cast<SQLHDESC>(descriptor.get());
return SQL_SUCCESS;
}
return SQL_ERROR;
});
}
default:
break;
}
return SQL_ERROR;
}
SQLRETURN SQLFreeHandle(SQLSMALLINT type, SQLHANDLE handle) {
ARROW_LOG(DEBUG) << "SQLFreeHandle called with type: " << type
<< ", handle: " << handle;
switch (type) {
case SQL_HANDLE_ENV: {
using ODBC::ODBCEnvironment;
ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(handle);
if (!environment) {
return SQL_INVALID_HANDLE;
}
delete environment;
return SQL_SUCCESS;
}
case SQL_HANDLE_DBC: {
using ODBC::ODBCConnection;
ODBCConnection* conn = reinterpret_cast<ODBCConnection*>(handle);
if (!conn) {
return SQL_INVALID_HANDLE;
}
// `ReleaseConnection` does the equivalent of `delete`.
// `ReleaseConnection` removes the connection `shared_ptr` from the `std::vector` of
// connections, and the `shared_ptr` is automatically destructed afterwards.
conn->ReleaseConnection();
return SQL_SUCCESS;
}
case SQL_HANDLE_STMT: {
using ODBC::ODBCStatement;
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(handle);
if (!statement) {
return SQL_INVALID_HANDLE;
}
statement->ReleaseStatement();
return SQL_SUCCESS;
}
case SQL_HANDLE_DESC: {
using ODBC::ODBCDescriptor;
ODBCDescriptor* descriptor = reinterpret_cast<ODBCDescriptor*>(handle);
if (!descriptor) {
return SQL_INVALID_HANDLE;
}
descriptor->ReleaseDescriptor();
return SQL_SUCCESS;
}
default:
break;
}
return SQL_ERROR;
}
SQLRETURN SQLFreeStmt(SQLHSTMT handle, SQLUSMALLINT option) {
ARROW_LOG(DEBUG) << "SQLFreeStmt called with handle: " << handle
<< ", option: " << option;
switch (option) {
case SQL_CLOSE: {
using ODBC::ODBCStatement;
return ODBCStatement::ExecuteWithDiagnostics(handle, SQL_ERROR, [=]() {
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(handle);
// Close cursor with suppressErrors set to true
statement->CloseCursor(true);
return SQL_SUCCESS;
});
}
case SQL_DROP: {
return SQLFreeHandle(SQL_HANDLE_STMT, handle);
}
case SQL_UNBIND: {
using ODBC::ODBCDescriptor;
using ODBC::ODBCStatement;
return ODBCStatement::ExecuteWithDiagnostics(handle, SQL_ERROR, [=]() {
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(handle);
ODBCDescriptor* ard = statement->GetARD();
// Unbind columns
ard->SetHeaderField(SQL_DESC_COUNT, (void*)0, 0);
return SQL_SUCCESS;
});
}
// SQLBindParameter is not supported
case SQL_RESET_PARAMS: {
return SQL_SUCCESS;
}
}
return SQL_ERROR;
}
#if defined(__APPLE__)
SQLRETURN SQLError(SQLHENV env, SQLHDBC conn, SQLHSTMT stmt, SQLWCHAR* sql_state,
SQLINTEGER* native_error_ptr, SQLWCHAR* message_text,
SQLSMALLINT buffer_length, SQLSMALLINT* text_length_ptr) {
ARROW_LOG(DEBUG) << "SQLError called with env: " << env << ", conn: " << conn
<< ", stmt: " << stmt
<< ", sql_state: " << static_cast<const void*>(sql_state)
<< ", native_error_ptr: " << static_cast<const void*>(native_error_ptr)
<< ", message_text: " << static_cast<const void*>(message_text)
<< ", buffer_length: " << buffer_length
<< ", text_length_ptr: " << static_cast<const void*>(text_length_ptr);
SQLSMALLINT handle_type;
SQLHANDLE handle;
if (env) {
handle_type = SQL_HANDLE_ENV;
handle = static_cast<SQLHANDLE>(env);
} else if (conn) {
handle_type = SQL_HANDLE_DBC;
handle = static_cast<SQLHANDLE>(conn);
} else if (stmt) {
handle_type = SQL_HANDLE_STMT;
handle = static_cast<SQLHANDLE>(stmt);
} else {
return static_cast<SQLRETURN>(SQL_INVALID_HANDLE);
}
// Use the last record
SQLINTEGER diag_number;
SQLSMALLINT diag_number_length;
SQLRETURN ret = arrow::flight::sql::odbc::SQLGetDiagField(
handle_type, handle, 0, SQL_DIAG_NUMBER, &diag_number, sizeof(SQLINTEGER), 0);
if (ret != SQL_SUCCESS) {
return ret;
}
if (diag_number == 0) {
return SQL_NO_DATA;
}
SQLSMALLINT rec_number = static_cast<SQLSMALLINT>(diag_number);
return arrow::flight::sql::odbc::SQLGetDiagRec(
handle_type, handle, rec_number, sql_state, native_error_ptr, message_text,
buffer_length, text_length_ptr);
}
#endif // __APPLE__
inline bool IsValidStringFieldArgs(SQLPOINTER diag_info_ptr, SQLSMALLINT buffer_length,
SQLSMALLINT* string_length_ptr, bool is_unicode) {
const SQLSMALLINT char_size = is_unicode ? GetSqlWCharSize() : sizeof(char);
const bool has_valid_buffer =
buffer_length == SQL_NTS || (buffer_length >= 0 && buffer_length % char_size == 0);
// regardless of capacity return false if invalid
if (diag_info_ptr && !has_valid_buffer) {
return false;
}
return has_valid_buffer || string_length_ptr;
}
SQLRETURN SQLGetDiagField(SQLSMALLINT handle_type, SQLHANDLE handle,
SQLSMALLINT rec_number, SQLSMALLINT diag_identifier,
SQLPOINTER diag_info_ptr, SQLSMALLINT buffer_length,
SQLSMALLINT* string_length_ptr) {
// GH-46573 TODO: Implement additional fields types
ARROW_LOG(DEBUG) << "SQLGetDiagFieldW called with handle_type: " << handle_type
<< ", handle: " << handle << ", rec_number: " << rec_number
<< ", diag_identifier: " << diag_identifier
<< ", diag_info_ptr: " << diag_info_ptr
<< ", buffer_length: " << buffer_length << ", string_length_ptr: "
<< static_cast<const void*>(string_length_ptr);
using ODBC::GetStringAttribute;
using ODBC::ODBCConnection;
using ODBC::ODBCDescriptor;
using ODBC::ODBCEnvironment;
using ODBC::ODBCStatement;
if (!handle) {
return SQL_INVALID_HANDLE;
}
if (!diag_info_ptr && !string_length_ptr) {
return SQL_ERROR;
}
// If buffer length derived from null terminated string
if (diag_info_ptr && buffer_length == SQL_NTS) {
const wchar_t* str = reinterpret_cast<wchar_t*>(diag_info_ptr);
buffer_length = wcslen(str) * GetSqlWCharSize();
}
// Set character type to be Unicode by default
const bool is_unicode = true;
Diagnostics* diagnostics = nullptr;
switch (handle_type) {
case SQL_HANDLE_ENV: {
ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(handle);
diagnostics = &environment->GetDiagnostics();
break;
}
case SQL_HANDLE_DBC: {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(handle);
diagnostics = &connection->GetDiagnostics();
break;
}
case SQL_HANDLE_DESC: {
ODBCDescriptor* descriptor = reinterpret_cast<ODBCDescriptor*>(handle);
diagnostics = &descriptor->GetDiagnostics();
break;
}
case SQL_HANDLE_STMT: {
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(handle);
diagnostics = &statement->GetDiagnostics();
break;
}
default:
return SQL_ERROR;
}
if (!diagnostics) {
return SQL_ERROR;
}
// Retrieve and return if header level diagnostics
switch (diag_identifier) {
case SQL_DIAG_NUMBER: {
if (diag_info_ptr) {
*static_cast<SQLINTEGER*>(diag_info_ptr) =
static_cast<SQLINTEGER>(diagnostics->GetRecordCount());
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLINTEGER);
}
return SQL_SUCCESS;
}
// Driver manager implements SQL_DIAG_RETURNCODE
case SQL_DIAG_RETURNCODE: {
return SQL_SUCCESS;
}
case SQL_DIAG_CURSOR_ROW_COUNT: {
if (handle_type == SQL_HANDLE_STMT) {
if (diag_info_ptr) {
// Will always be 0 if only SELECT supported
*static_cast<SQLLEN*>(diag_info_ptr) = 0;
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLLEN);
}
return SQL_SUCCESS;
}
return SQL_ERROR;
}
// Not supported
case SQL_DIAG_DYNAMIC_FUNCTION:
case SQL_DIAG_DYNAMIC_FUNCTION_CODE: {
if (handle_type == SQL_HANDLE_STMT) {
return SQL_SUCCESS;
}
return SQL_ERROR;
}
case SQL_DIAG_ROW_COUNT: {
if (handle_type == SQL_HANDLE_STMT) {
if (diag_info_ptr) {
// Will always be 0 if only SELECT is supported
*static_cast<SQLLEN*>(diag_info_ptr) = 0;
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLLEN);
}
return SQL_SUCCESS;
}
return SQL_ERROR;
}
}
// If not a diagnostic header field then the record number must be 1 or greater
if (rec_number < 1) {
return SQL_ERROR;
}
// Retrieve record level diagnostics from specified 1 based record
const uint32_t record_index = static_cast<uint32_t>(rec_number - 1);
if (!diagnostics->HasRecord(record_index)) {
return SQL_NO_DATA;
}
// Retrieve record field data
switch (diag_identifier) {
case SQL_DIAG_MESSAGE_TEXT: {
if (IsValidStringFieldArgs(diag_info_ptr, buffer_length, string_length_ptr,
is_unicode)) {
const std::string& message = diagnostics->GetMessageText(record_index);
return GetStringAttribute(is_unicode, message, true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
}
return SQL_ERROR;
}
case SQL_DIAG_NATIVE: {
if (diag_info_ptr) {
*static_cast<SQLINTEGER*>(diag_info_ptr) =
diagnostics->GetNativeError(record_index);
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLINTEGER);
}
return SQL_SUCCESS;
}
case SQL_DIAG_SERVER_NAME: {
if (IsValidStringFieldArgs(diag_info_ptr, buffer_length, string_length_ptr,
is_unicode)) {
switch (handle_type) {
case SQL_HANDLE_DBC: {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(handle);
std::string dsn = connection->GetDSN();
return GetStringAttribute(is_unicode, dsn, true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
}
case SQL_HANDLE_DESC: {
ODBCDescriptor* descriptor = reinterpret_cast<ODBCDescriptor*>(handle);
ODBCConnection* connection = &descriptor->GetConnection();
std::string dsn = connection->GetDSN();
return GetStringAttribute(is_unicode, dsn, true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
break;
}
case SQL_HANDLE_STMT: {
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(handle);
ODBCConnection* connection = &statement->GetConnection();
std::string dsn = connection->GetDSN();
return GetStringAttribute(is_unicode, dsn, true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
}
default:
return SQL_ERROR;
}
}
return SQL_ERROR;
}
case SQL_DIAG_SQLSTATE: {
if (IsValidStringFieldArgs(diag_info_ptr, buffer_length, string_length_ptr,
is_unicode)) {
const std::string& state = diagnostics->GetSQLState(record_index);
return GetStringAttribute(is_unicode, state, true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
}
return SQL_ERROR;
}
// Return valid dummy variable for unimplemented field
case SQL_DIAG_COLUMN_NUMBER: {
if (diag_info_ptr) {
*static_cast<SQLINTEGER*>(diag_info_ptr) = SQL_NO_COLUMN_NUMBER;
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLINTEGER);
}
return SQL_SUCCESS;
}
// Return empty string dummy variable for unimplemented fields
case SQL_DIAG_CLASS_ORIGIN:
case SQL_DIAG_CONNECTION_NAME:
case SQL_DIAG_SUBCLASS_ORIGIN: {
if (IsValidStringFieldArgs(diag_info_ptr, buffer_length, string_length_ptr,
is_unicode)) {
return GetStringAttribute(is_unicode, "", true, diag_info_ptr, buffer_length,
string_length_ptr, *diagnostics);
}
return SQL_ERROR;
}
// Return valid dummy variable for unimplemented field
case SQL_DIAG_ROW_NUMBER: {
if (diag_info_ptr) {
*static_cast<SQLLEN*>(diag_info_ptr) = SQL_NO_ROW_NUMBER;
}
if (string_length_ptr) {
*string_length_ptr = sizeof(SQLLEN);
}
return SQL_SUCCESS;
}
default: {
return SQL_ERROR;
}
}
return SQL_ERROR;
}
SQLRETURN SQLGetDiagRec(SQLSMALLINT handle_type, SQLHANDLE handle, SQLSMALLINT rec_number,
SQLWCHAR* sql_state, SQLINTEGER* native_error_ptr,
SQLWCHAR* message_text, SQLSMALLINT buffer_length,
SQLSMALLINT* text_length_ptr) {
ARROW_LOG(DEBUG) << "SQLGetDiagRecW called with handle_type: " << handle_type
<< ", handle: " << handle << ", rec_number: " << rec_number
<< ", sql_state: " << static_cast<const void*>(sql_state)
<< ", native_error_ptr: " << static_cast<const void*>(native_error_ptr)
<< ", message_text: " << static_cast<const void*>(message_text)
<< ", buffer_length: " << buffer_length
<< ", text_length_ptr: " << static_cast<const void*>(text_length_ptr);
using arrow::flight::sql::odbc::Diagnostics;
using ODBC::GetStringAttribute;
using ODBC::ODBCConnection;
using ODBC::ODBCDescriptor;
using ODBC::ODBCEnvironment;
using ODBC::ODBCStatement;
if (!handle) {
return SQL_INVALID_HANDLE;
}
// Record number must be greater or equal to 1
if (rec_number < 1 || buffer_length < 0) {
return SQL_ERROR;
}
// Set character type to be Unicode by default
const bool is_unicode = true;
Diagnostics* diagnostics = nullptr;
switch (handle_type) {
case SQL_HANDLE_ENV: {
auto* environment = ODBCEnvironment::Of(handle);
diagnostics = &environment->GetDiagnostics();
break;
}
case SQL_HANDLE_DBC: {
auto* connection = ODBCConnection::Of(handle);
diagnostics = &connection->GetDiagnostics();
break;
}
case SQL_HANDLE_DESC: {
auto* descriptor = ODBCDescriptor::Of(handle);
diagnostics = &descriptor->GetDiagnostics();
break;
}
case SQL_HANDLE_STMT: {
auto* statement = ODBCStatement::Of(handle);
diagnostics = &statement->GetDiagnostics();
break;
}
default:
return SQL_INVALID_HANDLE;
}
if (!diagnostics) {
return SQL_ERROR;
}
// Convert from ODBC 1 based record number to internal diagnostics 0 indexed storage
const size_t record_index = static_cast<size_t>(rec_number - 1);
if (!diagnostics->HasRecord(record_index)) {
return SQL_NO_DATA;
}
if (sql_state) {
// The length of the sql state is always 5 characters plus null
SQLSMALLINT size = 6;
const std::string& state = diagnostics->GetSQLState(record_index);
// Microsoft documentation does not mention
// any SQLGetDiagRec return value that is associated with `sql_state` buffer, so
// the return value for writing `sql_state` buffer is ignored by the driver.
ARROW_UNUSED(GetStringAttribute(is_unicode, state, false, sql_state, size, &size,
*diagnostics));
}
if (native_error_ptr) {
*native_error_ptr = diagnostics->GetNativeError(record_index);
}
if (message_text || text_length_ptr) {
const std::string& message = diagnostics->GetMessageText(record_index);
// According to Microsoft documentation,
// SQL_SUCCESS_WITH_INFO should be returned if `*message_text` buffer was too
// small to hold the requested diagnostic message.
return GetStringAttribute(is_unicode, message, false, message_text, buffer_length,
text_length_ptr, *diagnostics);
}
return SQL_SUCCESS;
}
SQLRETURN SQLGetEnvAttr(SQLHENV env, SQLINTEGER attr, SQLPOINTER value_ptr,
SQLINTEGER buffer_length, SQLINTEGER* str_len_ptr) {
ARROW_LOG(DEBUG) << "SQLGetEnvAttr called with env: " << env << ", attr: " << attr
<< ", value_ptr: " << value_ptr << ", buffer_length: " << buffer_length
<< ", str_len_ptr: " << static_cast<const void*>(str_len_ptr);
using ODBC::ODBCEnvironment;
ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(env);
return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() {
switch (attr) {
case SQL_ATTR_ODBC_VERSION: {
if (!value_ptr && !str_len_ptr) {
throw DriverException("Invalid null pointer for attribute.", "HY000");
}
if (value_ptr) {
SQLINTEGER* value = reinterpret_cast<SQLINTEGER*>(value_ptr);
*value = static_cast<SQLSMALLINT>(environment->GetODBCVersion());
}
if (str_len_ptr) {
*str_len_ptr = sizeof(SQLINTEGER);
}
return SQL_SUCCESS;
}
case SQL_ATTR_OUTPUT_NTS: {
if (!value_ptr && !str_len_ptr) {
throw DriverException("Invalid null pointer for attribute.", "HY000");
}
if (value_ptr) {
// output nts always returns SQL_TRUE
SQLINTEGER* value = reinterpret_cast<SQLINTEGER*>(value_ptr);
*value = SQL_TRUE;
}
if (str_len_ptr) {
*str_len_ptr = sizeof(SQLINTEGER);
}
return SQL_SUCCESS;
}
case SQL_ATTR_CONNECTION_POOLING: {
throw DriverException("Optional feature not supported.", "HYC00");
}
default: {
throw DriverException("Invalid attribute", "HYC00");
}
}
});
}
SQLRETURN SQLSetEnvAttr(SQLHENV env, SQLINTEGER attr, SQLPOINTER value_ptr,
SQLINTEGER str_len) {
ARROW_LOG(DEBUG) << "SQLSetEnvAttr called with env: " << env << ", attr: " << attr
<< ", value_ptr: " << value_ptr << ", str_len: " << str_len;
using ODBC::ODBCEnvironment;
ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(env);
return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() {
if (!value_ptr) {
throw DriverException("Invalid null pointer for attribute.", "HY024");
}
switch (attr) {
case SQL_ATTR_ODBC_VERSION: {
SQLINTEGER version =
static_cast<SQLINTEGER>(reinterpret_cast<intptr_t>(value_ptr));
if (version == SQL_OV_ODBC2 || version == SQL_OV_ODBC3) {
environment->SetODBCVersion(version);
return SQL_SUCCESS;
} else {
throw DriverException("Invalid value for attribute", "HY024");
}
}
case SQL_ATTR_OUTPUT_NTS: {
// output nts can not be set to SQL_FALSE, is always SQL_TRUE
SQLINTEGER value = static_cast<SQLINTEGER>(reinterpret_cast<intptr_t>(value_ptr));
if (value == SQL_TRUE) {
return SQL_SUCCESS;
} else {
throw DriverException("Invalid value for attribute", "HY024");
}
}
case SQL_ATTR_CONNECTION_POOLING: {
throw DriverException("Optional feature not supported.", "HYC00");
}
default: {
throw DriverException("Invalid attribute", "HY092");
}
}
});
}
SQLRETURN SQLGetConnectAttr(SQLHDBC conn, SQLINTEGER attribute, SQLPOINTER value_ptr,
SQLINTEGER buffer_length, SQLINTEGER* string_length_ptr) {
ARROW_LOG(DEBUG) << "SQLGetConnectAttrW called with conn: " << conn
<< ", attribute: " << attribute << ", value_ptr: " << value_ptr
<< ", buffer_length: " << buffer_length << ", string_length_ptr: "
<< static_cast<const void*>(string_length_ptr);
using ODBC::ODBCConnection;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
const bool is_unicode = true;
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
return connection->GetConnectAttr(attribute, value_ptr, buffer_length,
string_length_ptr, is_unicode);
});
}
SQLRETURN SQLSetConnectAttr(SQLHDBC conn, SQLINTEGER attr, SQLPOINTER value_ptr,
SQLINTEGER value_len) {
ARROW_LOG(DEBUG) << "SQLSetConnectAttrW called with conn: " << conn
<< ", attr: " << attr << ", value_ptr: " << value_ptr
<< ", value_len: " << value_len;
using ODBC::ODBCConnection;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
const bool is_unicode = true;
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
connection->SetConnectAttr(attr, value_ptr, value_len, is_unicode);
return SQL_SUCCESS;
});
}
// Load properties from the given DSN. The properties loaded do _not_ overwrite existing
// entries in the properties.
void LoadPropertiesFromDSN(const std::string& dsn,
Connection::ConnPropertyMap& properties) {
config::Configuration config;
config.LoadDsn(dsn);
Connection::ConnPropertyMap dsn_properties = config.GetProperties();
for (auto& [key, value] : dsn_properties) {
auto prop_iter = properties.find(key);
if (prop_iter == properties.end()) {
properties.emplace(std::make_pair(std::move(key), std::move(value)));
}
}
}
SQLRETURN SQLDriverConnect(SQLHDBC conn, SQLHWND window_handle,
SQLWCHAR* in_connection_string,
SQLSMALLINT in_connection_string_len,
SQLWCHAR* out_connection_string,
SQLSMALLINT out_connection_string_buffer_len,
SQLSMALLINT* out_connection_string_len,
SQLUSMALLINT driver_completion) {
ARROW_LOG(DEBUG) << "SQLDriverConnectW called with conn: " << conn
<< ", window_handle: " << static_cast<const void*>(window_handle)
<< ", in_connection_string: "
<< static_cast<const void*>(in_connection_string)
<< ", in_connection_string_len: " << in_connection_string_len
<< ", out_connection_string: "
<< static_cast<const void*>(out_connection_string)
<< ", out_connection_string_buffer_len: "
<< out_connection_string_buffer_len << ", out_connection_string_len: "
<< static_cast<const void*>(out_connection_string_len)
<< ", driver_completion: " << driver_completion;
// GH-46449 TODO: Implement FILEDSN and SAVEFILE keywords according to the spec
// GH-46560 TODO: Copy connection string properly in SQLDriverConnect according to the
// spec
using ODBC::ODBCConnection;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
std::string connection_string =
ODBC::SqlWcharToString(in_connection_string, in_connection_string_len);
Connection::ConnPropertyMap properties;
std::string dsn_value = "";
std::optional<std::string> dsn = ODBCConnection::GetDsnIfExists(connection_string);
if (dsn.has_value()) {
dsn_value = dsn.value();
LoadPropertiesFromDSN(dsn_value, properties);
}
ODBCConnection::GetPropertiesFromConnString(connection_string, properties);
std::vector<std::string_view> missing_properties;
// GH-46448 TODO: Implement SQL_DRIVER_COMPLETE_REQUIRED in SQLDriverConnect according
// to the spec
#if defined _WIN32
// Load the DSN window according to driver_completion
if (driver_completion == SQL_DRIVER_PROMPT) {
// Load DSN window before first attempt to connect
config::Configuration config;
if (!DisplayConnectionWindow(window_handle, config, properties)) {
return static_cast<SQLRETURN>(SQL_NO_DATA);
}
connection->Connect(dsn_value, properties, missing_properties);
} else if (driver_completion == SQL_DRIVER_COMPLETE ||
driver_completion == SQL_DRIVER_COMPLETE_REQUIRED) {
try {
connection->Connect(dsn_value, properties, missing_properties);
} catch (const DriverException&) {
// If first connection fails due to missing attributes, load
// the DSN window and try to connect again
if (!missing_properties.empty()) {
config::Configuration config;
missing_properties.clear();
if (!DisplayConnectionWindow(window_handle, config, properties)) {
return static_cast<SQLRETURN>(SQL_NO_DATA);
}
connection->Connect(dsn_value, properties, missing_properties);
} else {
throw;
}
}
} else {
// Default case: attempt connection without showing DSN window
connection->Connect(dsn_value, properties, missing_properties);
}
#else
// Attempt connection without loading DSN window on macOS/Linux
connection->Connect(dsn_value, properties, missing_properties);
#endif
// Copy connection string to out_connection_string after connection attempt
return ODBC::GetStringAttribute(true, connection_string, false, out_connection_string,
out_connection_string_buffer_len,
out_connection_string_len,
connection->GetDiagnostics());
});
}
SQLRETURN SQLConnect(SQLHDBC conn, SQLWCHAR* dsn_name, SQLSMALLINT dsn_name_len,
SQLWCHAR* user_name, SQLSMALLINT user_name_len, SQLWCHAR* password,
SQLSMALLINT password_len) {
ARROW_LOG(DEBUG) << "SQLConnectW called with conn: " << conn
<< ", dsn_name: " << static_cast<const void*>(dsn_name)
<< ", dsn_name_len: " << dsn_name_len
<< ", user_name: " << static_cast<const void*>(user_name)
<< ", user_name_len: " << user_name_len
<< ", password: " << static_cast<const void*>(password)
<< ", password_len: " << password_len;
using ODBC::ODBCConnection;
using ODBC::SqlWcharToString;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
std::string dsn = SqlWcharToString(dsn_name, dsn_name_len);
config::Configuration config;
config.LoadDsn(dsn);
if (user_name) {
std::string uid = SqlWcharToString(user_name, user_name_len);
config.Emplace(FlightSqlConnection::UID, std::move(uid));
}
if (password) {
std::string pwd = SqlWcharToString(password, password_len);
config.Emplace(FlightSqlConnection::PWD, std::move(pwd));
}
std::vector<std::string_view> missing_properties;
connection->Connect(dsn, config.GetProperties(), missing_properties);
return SQL_SUCCESS;
});
}
SQLRETURN SQLDisconnect(SQLHDBC conn) {
ARROW_LOG(DEBUG) << "SQLDisconnect called with conn: " << conn;
using ODBC::ODBCConnection;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
connection->Disconnect();
return SQL_SUCCESS;
});
}
SQLRETURN SQLGetInfo(SQLHDBC conn, SQLUSMALLINT info_type, SQLPOINTER info_value_ptr,
SQLSMALLINT buf_len, SQLSMALLINT* string_length_ptr) {
ARROW_LOG(DEBUG) << "SQLGetInfoW called with conn: " << conn
<< ", info_type: " << info_type
<< ", info_value_ptr: " << info_value_ptr << ", buf_len: " << buf_len
<< ", string_length_ptr: "
<< static_cast<const void*>(string_length_ptr);
using ODBC::ODBCConnection;
return ODBCConnection::ExecuteWithDiagnostics(conn, SQL_ERROR, [=]() {
ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(conn);
// Set character type to be Unicode by default
const bool is_unicode = true;
if (!info_value_ptr && !string_length_ptr) {
return static_cast<SQLRETURN>(SQL_ERROR);
}
return connection->GetInfo(info_type, info_value_ptr, buf_len, string_length_ptr,
is_unicode);
});
}
SQLRETURN SQLGetStmtAttr(SQLHSTMT stmt, SQLINTEGER attribute, SQLPOINTER value_ptr,
SQLINTEGER buffer_length, SQLINTEGER* string_length_ptr) {
ARROW_LOG(DEBUG) << "SQLGetStmtAttrW called with stmt: " << stmt