-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnection_test.cc
More file actions
2050 lines (1740 loc) · 76.5 KB
/
Copy pathconnection_test.cc
File metadata and controls
2050 lines (1740 loc) · 76.5 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
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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.
#include "google/cloud/odbc/testing/odbc_utils/connection.h"
#include "google/cloud/odbc/internal/version.h"
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>
#include <fstream>
namespace google::cloud::odbc_tests {
using google::cloud::odbc_tests::SetAttributes;
using ::testing::HasSubstr;
void CheckDiagnosticRecord(SQLHDBC hdbc, std::string const& expected_sqlstate,
int expected_error_code,
std::string const& expected_message_regex);
std::string GetDriverName() {
#ifndef BQ_DRIVER_INTEGRATION_TESTS
#ifdef _WIN32
return kExistingDriverWindows;
#else
return kExistingDriverNonWindows;
#endif /* _WIN32 */
#else
return "ODBC Driver for BigQuery";
#endif /* BQ_DRIVER_INTEGRATION_TESTS */
}
TEST(SQLGetInfo, CheckPositionalUpdate) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
// Check SQL_DYNAMIC_CURSOR_ATTRIBUTES1
SQLUINTEGER sql_bitmask_buf = 0;
SQLRETURN status;
status = SQLGetInfo(conn->hdbc, SQL_DYNAMIC_CURSOR_ATTRIBUTES1,
&sql_bitmask_buf, 0, nullptr);
CheckError(status, "SQLGetInfo(SQL_DYNAMIC_CURSOR_ATTRIBUTES1)", conn);
EXPECT_NE(SQL_CA1_POSITIONED_UPDATE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_UPDATE));
EXPECT_NE(SQL_CA1_POSITIONED_DELETE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_DELETE));
// Check SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1
sql_bitmask_buf = 0;
status = SQLGetInfo(conn->hdbc, SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1,
&sql_bitmask_buf, 0, nullptr);
CheckError(status, "SQLGetInfo(SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1)", conn);
EXPECT_NE(SQL_CA1_POSITIONED_UPDATE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_UPDATE));
EXPECT_NE(SQL_CA1_POSITIONED_DELETE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_DELETE));
// Check SQL_KEYSET_CURSOR_ATTRIBUTES1
sql_bitmask_buf = 0;
status = SQLGetInfo(conn->hdbc, SQL_KEYSET_CURSOR_ATTRIBUTES1,
&sql_bitmask_buf, 0, nullptr);
CheckError(status, "SQLGetInfo(SQL_KEYSET_CURSOR_ATTRIBUTES1)", conn);
EXPECT_NE(SQL_CA1_POSITIONED_UPDATE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_UPDATE));
EXPECT_NE(SQL_CA1_POSITIONED_DELETE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_DELETE));
// Check SQL_STATIC_CURSOR_ATTRIBUTES1
sql_bitmask_buf = 0;
status = SQLGetInfo(conn->hdbc, SQL_STATIC_CURSOR_ATTRIBUTES1,
&sql_bitmask_buf, 0, nullptr);
CheckError(status, "SQLGetInfo(SQL_KEYSET_CURSOR_ATTRIBUTES1)", conn);
EXPECT_NE(SQL_CA1_POSITIONED_UPDATE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_UPDATE));
EXPECT_NE(SQL_CA1_POSITIONED_DELETE,
(sql_bitmask_buf & SQL_CA1_POSITIONED_DELETE));
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(SQLGetInfo, CheckSqlConformance) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
SQLUINTEGER conformance_val = 0;
auto status = SQLGetInfo(conn->hdbc, SQL_SQL_CONFORMANCE, &conformance_val,
sizeof(conformance_val), nullptr);
ASSERT_TRUE(SQL_SUCCEEDED(status));
EXPECT_EQ(conformance_val, SQL_SC_SQL92_ENTRY);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(SQLGetInfoW, CheckDriverName_Wide) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
SQLWCHAR sqlWCharBuf[kBufferLength];
std::string expected_info_val = "ODBC Driver For BigQuery";
SQLSMALLINT out_len;
SQLRETURN status = SQLGetInfoW(conn->hdbc, SQL_DRIVER_NAME,
reinterpret_cast<SQLPOINTER>(sqlWCharBuf),
kBufferLength, &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
std::string str_out =
ConvertSQLWCHARToString(sqlWCharBuf, out_len / sizeof(SQLWCHAR));
#ifdef BQ_DRIVER_INTEGRATION_TESTS
EXPECT_STREQ(str_out.data(), "ODBC Driver For BigQuery");
#else
EXPECT_STREQ(str_out.data(), kExistingDriverWindows.c_str());
#endif
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
#ifdef BQ_DRIVER_INTEGRATION_TESTS
namespace {
// Constants for GetBQDriverInfo
static std::map<SQLUSMALLINT, std::string> const kUnsupportedEmptyCharMap = {
{SQL_KEYWORDS, ""},
{SQL_PROCEDURE_TERM, ""},
{SQL_SPECIAL_CHARACTERS, ""},
{SQL_USER_NAME, ""}};
static std::map<SQLUSMALLINT, std::string> const kUnsupportedNCharMap = {
{SQL_ACCESSIBLE_PROCEDURES, "N"},
{SQL_DATA_SOURCE_READ_ONLY, "N"},
{SQL_INTEGRITY, "N"},
{SQL_LIKE_ESCAPE_CLAUSE, "N"},
{SQL_MAX_ROW_SIZE_INCLUDES_LONG, "N"},
{SQL_MULT_RESULT_SETS, "N"},
{SQL_NEED_LONG_DATA_LEN, "N"},
{SQL_ORDER_BY_COLUMNS_IN_SELECT, "N"},
{SQL_ROW_UPDATES, "N"}};
static std::map<SQLUSMALLINT, std::string> const kSupportedCharMap = {
{SQL_ACCESSIBLE_TABLES, "Y"},
{SQL_CATALOG_NAME, "Y"},
{SQL_CATALOG_NAME_SEPARATOR, "."},
{SQL_CATALOG_TERM, "Project"},
{SQL_COLLATION_SEQ, "UTF-16LE_BINARY"},
{SQL_COLUMN_ALIAS, "Y"},
{SQL_DBMS_NAME, "BigQuery"},
{SQL_DBMS_VER, "2"},
{SQL_DESCRIBE_PARAMETER, "Y"},
{SQL_DRIVER_NAME, "ODBC Driver For BigQuery"},
{SQL_DRIVER_ODBC_VER, "03.80"},
{SQL_DRIVER_VER, DRIVER_VERSION},
{SQL_EXPRESSIONS_IN_ORDERBY, "Y"},
{SQL_IDENTIFIER_QUOTE_CHAR, "`"},
{SQL_MULTIPLE_ACTIVE_TXN, "Y"},
{SQL_PROCEDURES, "Y"},
{SQL_SCHEMA_TERM, "Dataset"},
{SQL_SEARCH_PATTERN_ESCAPE, "\\"},
{SQL_SERVER_NAME, "Google"},
{SQL_TABLE_TERM, "Table"}};
static std::map<SQLUSMALLINT, SQLUSMALLINT> const kUnsupportedUSmallIntMap = {
{SQL_ACTIVE_ENVIRONMENTS, 0},
{SQL_CONCAT_NULL_BEHAVIOR, 0},
{SQL_FILE_USAGE, 0},
{SQL_MAX_COLUMNS_IN_GROUP_BY, 0},
{SQL_MAX_COLUMNS_IN_INDEX, 0},
{SQL_MAX_COLUMNS_IN_ORDER_BY, 0},
{SQL_MAX_COLUMNS_IN_SELECT, 0},
{SQL_MAX_CONCURRENT_ACTIVITIES, 0},
{SQL_MAX_CURSOR_NAME_LEN, 0},
{SQL_MAX_DRIVER_CONNECTIONS, 0},
{SQL_MAX_PROCEDURE_NAME_LEN, 0},
{SQL_MAX_USER_NAME_LEN, 0},
{SQL_NON_NULLABLE_COLUMNS, 0}};
static std::map<SQLUSMALLINT, SQLUSMALLINT> const kSupportedUSmallIntMap = {
{SQL_CATALOG_LOCATION, 1},
{SQL_CORRELATION_NAME, 2},
{SQL_CURSOR_COMMIT_BEHAVIOR, 1},
{SQL_CURSOR_ROLLBACK_BEHAVIOR, 1},
{SQL_GROUP_BY, 2},
{SQL_IDENTIFIER_CASE, 3},
{SQL_MAX_CATALOG_NAME_LEN, 128},
{SQL_MAX_COLUMNS_IN_TABLE, 10000},
{SQL_MAX_COLUMN_NAME_LEN, 128},
{SQL_MAX_IDENTIFIER_LEN, 255},
{SQL_MAX_SCHEMA_NAME_LEN, 1024},
{SQL_MAX_TABLES_IN_SELECT, 1000},
{SQL_MAX_TABLE_NAME_LEN, 1024},
{SQL_NULL_COLLATION, 1},
{SQL_QUOTED_IDENTIFIER_CASE, 3},
{SQL_TXN_CAPABLE, 1}};
static std::map<SQLUSMALLINT, SQLUINTEGER> const kSupportedUIntMap = {
{SQL_ASYNC_MODE, 2},
{SQL_DEFAULT_TXN_ISOLATION, 8},
{SQL_ODBC_INTERFACE_CONFORMANCE, 1},
{SQL_SQL_CONFORMANCE, 1}};
static std::map<SQLUSMALLINT, SQLUINTEGER> const kUnsupportedUIntMap = {
{SQL_BATCH_ROW_COUNT, 0},
{SQL_BATCH_SUPPORT, 0},
{SQL_BOOKMARK_PERSISTENCE, 0},
{SQL_CURSOR_SENSITIVITY, 0},
{SQL_DDL_INDEX, 0},
{SQL_MAX_ASYNC_CONCURRENT_STATEMENTS, 0},
{SQL_MAX_BINARY_LITERAL_LEN, 0},
{SQL_MAX_CHAR_LITERAL_LEN, 0},
{SQL_MAX_INDEX_SIZE, 0},
{SQL_MAX_ROW_SIZE, 0},
{SQL_MAX_STATEMENT_LEN, 0},
{SQL_PARAM_ARRAY_ROW_COUNTS, 0},
{SQL_PARAM_ARRAY_SELECTS, 0}};
static std::map<SQLUSMALLINT, SQLUINTEGER> const kUnsupportedBitmaskMap = {
{SQL_ALTER_DOMAIN, 0L},
{SQL_ALTER_TABLE, 0L},
{SQL_CONVERT_BINARY, 0L},
{SQL_CONVERT_CHAR, 0L},
{SQL_CONVERT_DECIMAL, 0L},
{SQL_CONVERT_FLOAT, 0L},
{SQL_CONVERT_INTEGER, 0L},
{SQL_CONVERT_INTERVAL_DAY_TIME, 0L},
{SQL_CONVERT_INTERVAL_YEAR_MONTH, 0L},
{SQL_CONVERT_LONGVARBINARY, 0L},
{SQL_CONVERT_LONGVARCHAR, 0L},
{SQL_CONVERT_NUMERIC, 0L},
{SQL_CONVERT_REAL, 0L},
{SQL_CONVERT_SMALLINT, 0L},
{SQL_CONVERT_TINYINT, 0L},
{SQL_CREATE_ASSERTION, 0L},
{SQL_CREATE_CHARACTER_SET, 0L},
{SQL_CREATE_COLLATION, 0L},
{SQL_CREATE_DOMAIN, 0L},
{SQL_CREATE_SCHEMA, 0L},
{SQL_CREATE_SCHEMA, 0L},
{SQL_CREATE_TABLE, 0L},
{SQL_CREATE_TRANSLATION, 0L},
{SQL_CREATE_VIEW, 0L},
{SQL_DROP_ASSERTION, 0L},
{SQL_DROP_CHARACTER_SET, 0L},
{SQL_DROP_COLLATION, 0L},
{SQL_DROP_DOMAIN, 0L},
{SQL_DROP_SCHEMA, 0L},
{SQL_DROP_TABLE, 0L},
{SQL_DROP_TRANSLATION, 0L},
{SQL_DROP_VIEW, 0L},
{SQL_DYNAMIC_CURSOR_ATTRIBUTES1, 0L},
{SQL_DYNAMIC_CURSOR_ATTRIBUTES2, 0L},
{SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1, 0L},
{SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1, 0L},
{SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2, 0L},
{SQL_INDEX_KEYWORDS, 0L},
{SQL_INFO_SCHEMA_VIEWS, 0L},
{SQL_INSERT_STATEMENT, 0L},
{SQL_KEYSET_CURSOR_ATTRIBUTES1, 0L},
{SQL_KEYSET_CURSOR_ATTRIBUTES2, 0L},
{SQL_POS_OPERATIONS, 0L},
{SQL_SQL92_FOREIGN_KEY_DELETE_RULE, 0L},
{SQL_SQL92_FOREIGN_KEY_UPDATE_RULE, 0L},
{SQL_SQL92_GRANT, 0L},
{SQL_SQL92_NUMERIC_VALUE_FUNCTIONS, 0L},
{SQL_SQL92_REVOKE, 0L},
{SQL_STATIC_CURSOR_ATTRIBUTES1, 0L},
{SQL_STATIC_CURSOR_ATTRIBUTES2, 0L},
{SQL_UNION, 0L}};
static std::map<SQLUSMALLINT, SQLUINTEGER> const kSupportedBitmaskMap = {
{SQL_AGGREGATE_FUNCTIONS, 127},
{SQL_CATALOG_USAGE, 1},
{SQL_CONVERT_BIT, 20736},
{SQL_CONVERT_DATE, 164096},
{SQL_CONVERT_DOUBLE, 16768},
{SQL_CONVERT_FUNCTIONS, 3},
{SQL_CONVERT_TIME, 65792},
{SQL_CONVERT_VARBINARY, 2304},
{SQL_CONVERT_VARCHAR, 252288},
{SQL_DATETIME_LITERALS, 4},
{SQL_GETDATA_EXTENSIONS, 15},
{SQL_NUMERIC_FUNCTIONS, 14221311},
{SQL_CONVERT_TIMESTAMP, 196864},
{SQL_OJ_CAPABILITIES, 127},
{SQL_SCROLL_OPTIONS, 1},
{SQL_SCHEMA_USAGE, 31},
{SQL_SUBQUERIES, 31},
{SQL_TXN_ISOLATION_OPTION, 8},
{SQL_TIMEDATE_FUNCTIONS, 2097151},
{SQL_SYSTEM_FUNCTIONS, 4},
{SQL_TIMEDATE_ADD_INTERVALS, 510},
{SQL_TIMEDATE_DIFF_INTERVALS, 478},
{SQL_SQL92_PREDICATES, 16135},
{SQL_SQL92_RELATIONAL_JOIN_OPERATORS, 346},
{SQL_SQL92_ROW_VALUE_CONSTRUCTOR, 15},
{SQL_SQL92_DATETIME_FUNCTIONS, 7},
{SQL_SQL92_STRING_FUNCTIONS, 254},
{SQL_SQL92_VALUE_EXPRESSIONS, 2},
{SQL_STANDARD_CLI_CONFORMANCE, 2},
{SQL_STRING_FUNCTIONS, 15822265},
{SQL_CONVERT_BIGINT, 20864}};
void AssertBQDriverSQLGetInfo(std::shared_ptr<ODBCHandles> conn) {
SQLCHAR sqlCharBuf[kBufferLength];
SQLUSMALLINT sqlUSmallIntBuf;
SQLUINTEGER sqlUIntegerBuf;
SQLUINTEGER sqlBitmaskBuf;
SQLSMALLINT out_len;
SQLRETURN status;
for (auto elem : kSupportedCharMap) {
auto info_type = elem.first;
auto expected_info_val = elem.second;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(sqlCharBuf), kBufferLength,
&out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
std::string actual_val = reinterpret_cast<char*>(sqlCharBuf);
EXPECT_EQ(expected_info_val, actual_val);
}
for (auto elem : kUnsupportedEmptyCharMap) {
auto info_type = elem.first;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(sqlCharBuf), kBufferLength,
&out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
std::string actual_val = (char*)sqlCharBuf;
EXPECT_EQ("", actual_val);
}
for (auto elem : kUnsupportedNCharMap) {
auto info_type = elem.first;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(sqlCharBuf), kBufferLength,
&out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
std::string actual_val = (char*)sqlCharBuf;
EXPECT_EQ("N", actual_val);
}
for (auto elem : kSupportedUSmallIntMap) {
auto info_type = elem.first;
auto expected_info_val = elem.second;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlUSmallIntBuf),
sizeof(sqlUSmallIntBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUSMALLINT actual_val = sqlUSmallIntBuf;
EXPECT_EQ(expected_info_val, actual_val);
}
for (auto elem : kUnsupportedUSmallIntMap) {
auto info_type = elem.first;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlUSmallIntBuf),
sizeof(sqlUSmallIntBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUSMALLINT actual_val = sqlUSmallIntBuf;
EXPECT_EQ(0, actual_val);
}
for (auto elem : kSupportedUIntMap) {
auto info_type = elem.first;
auto expected_info_val = elem.second;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlUIntegerBuf),
sizeof(sqlUIntegerBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUINTEGER actual_val = sqlUIntegerBuf;
EXPECT_EQ(expected_info_val, actual_val);
}
for (auto elem : kUnsupportedUIntMap) {
auto info_type = elem.first;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlUIntegerBuf),
sizeof(sqlUIntegerBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUINTEGER actual_val = sqlUIntegerBuf;
EXPECT_EQ(0, actual_val);
}
for (auto elem : kSupportedBitmaskMap) {
auto info_type = elem.first;
auto expected_info_val = elem.second;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlBitmaskBuf),
sizeof(sqlBitmaskBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUINTEGER actual_val = sqlBitmaskBuf;
EXPECT_EQ(expected_info_val, actual_val);
}
for (auto elem : kUnsupportedBitmaskMap) {
auto info_type = elem.first;
status = SQLGetInfo(conn->hdbc, info_type,
reinterpret_cast<SQLPOINTER>(&sqlBitmaskBuf),
sizeof(sqlBitmaskBuf), &out_len);
ASSERT_TRUE(SQL_SUCCEEDED(status));
SQLUINTEGER actual_val = sqlBitmaskBuf;
EXPECT_EQ(0L, actual_val);
}
}
void AssertSupportedFnsODBC3(SQLUSMALLINT* odbc3_fns) {
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLALLOCHANDLE));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETDESCFIELD));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETCONNECTATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDRIVERS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLBINDCOL));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETDESCREC));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCANCEL));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETDIAGFIELD));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCLOSECURSOR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETDIAGREC));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCOLATTRIBUTE));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETENVATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCONNECT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETFUNCTIONS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCOPYDESC));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETINFO));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDATASOURCES));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETSTMTATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDESCRIBECOL));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETTYPEINFO));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDISCONNECT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLNUMRESULTCOLS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPARAMDATA));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLENDTRAN));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPREPARE));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLEXECDIRECT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPUTDATA));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLEXECUTE));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLROWCOUNT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLFETCH));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLFETCHSCROLL));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETCURSORNAME));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLFREEHANDLE));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETDESCFIELD));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETDESCREC));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETCONNECTATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETENVATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETCURSORNAME));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSETSTMTATTR));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLGETDATA));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCOLUMNS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSTATISTICS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLSPECIALCOLUMNS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLTABLES));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLBINDPARAM));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLNATIVESQL));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLBROWSECONNECT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLNUMPARAMS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPRIMARYKEYS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLCOLUMNPRIVILEGES));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPROCEDURECOLUMNS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDESCRIBEPARAM));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLPROCEDURES));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLDRIVERCONNECT));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLFOREIGNKEYS));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLTABLEPRIVILEGES));
EXPECT_EQ(SQL_TRUE, SQL_FUNC_EXISTS(odbc3_fns, SQL_API_SQLMORERESULTS));
}
} // namespace
#endif // BQ_DRIVER_INTEGRATION_TESTS
std::vector<int> GetMajorMinorVer(std::string version_str) {
std::vector<int> versions;
int start, end = -1;
do {
start = end + 1;
end = version_str.find(".", start);
versions.emplace_back(stoi(version_str.substr(start, end - start)));
} while (end != -1);
return versions;
}
void VerifyDriverInfo(std::shared_ptr<ODBCHandles> conn) {
EXPECT_EQ(conn->metadata.dsn_name, GetDefaultDSN());
std::vector<int> db_odbc_versions =
GetMajorMinorVer(conn->metadata.db_odbc_ver);
EXPECT_EQ(db_odbc_versions[0], 3);
std::vector<int> driver_odbc_versions =
GetMajorMinorVer(conn->metadata.driver_odbc_ver);
EXPECT_EQ(driver_odbc_versions[0], 3);
#ifdef BQ_DRIVER_INTEGRATION_TESTS
EXPECT_EQ(conn->metadata.driver_name, "ODBC Driver For BigQuery");
#else
EXPECT_EQ(conn->metadata.driver_name, kExistingDriverWindows);
#endif // BQ_DRIVER_INTEGRATION_TESTS
}
void SetAttr(std::shared_ptr<ODBCHandles> conn, bool use_ansi = false) {
SQLCHAR buf[256] = "test";
EXPECT_EQ(Connect(kDefaultConnectionString, conn, use_ansi), SQL_SUCCESS);
SQLRETURN status;
if (use_ansi) {
status = SQLSetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, SQL_NTS);
} else {
status = SQLSetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, SQL_NTS);
}
CheckError(status, "SQLSetConnectAttr", conn, use_ansi);
SQLCHAR output[256];
SQLINTEGER length;
if (use_ansi) {
status = SQLGetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
} else {
status = SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
}
CheckError(status, "SQLGetConnectAttr", conn, use_ansi);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ("test", actual);
EXPECT_EQ(4, length);
}
// Sets the window handle for SQLDriverConnect (Windows: desktop handle, others:
// nullptr).
void GetWindowHandle(SQLHWND& window_handle) {
#ifdef _WIN32
window_handle = GetDesktopWindow();
#else
window_handle = nullptr;
#endif // _WIN32
}
TEST(ConnectionTest, SQLDriverConnect) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnectW) {
auto conn = std::make_shared<ODBCHandles>();
std::wstring defaultConnectionWstring = Utf8ToUtf16(kDefaultConnectionString);
EXPECT_EQ(Connect(defaultConnectionWstring, conn, 30, true), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnect_NULLOutput) {
auto conn = std::make_shared<ODBCHandles>();
std::wstring defaultConnectionWstring = Utf8ToUtf16(kDefaultConnectionString);
EXPECT_EQ(ConnectWithNullOutputParams(kDefaultConnectionString,
defaultConnectionWstring, conn),
SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnectW_NULLOutput) {
auto conn = std::make_shared<ODBCHandles>();
std::wstring defaultConnectionWstring = Utf8ToUtf16(kDefaultConnectionString);
EXPECT_EQ(ConnectWithNullOutputParams(kDefaultConnectionString,
defaultConnectionWstring, conn, true),
SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
void CreateDriverConnection() {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(MultipleConnectionTest, SQLDriverConnect) {
int const number_of_threads = 50;
std::thread threads[number_of_threads];
for (int i = 0; i < number_of_threads; i++) {
threads[i] = std::thread(CreateDriverConnection);
}
for (int i = 0; i < number_of_threads; i++) {
threads[i].join();
}
}
TEST(ConnectionTest, VerifySQLANSIAttributes) {
auto conn = std::make_shared<ODBCHandles>();
SQLRETURN status;
SQLCHAR data_source[kBufferLength];
SQLSMALLINT buflen = 0;
SetAttributes(conn, 30, true);
StrToChar(reinterpret_cast<char*>(data_source), kDefaultConnectionString);
status =
SQLDriverConnectA(conn->hdbc, nullptr, data_source, SQL_NTS,
reinterpret_cast<SQLCHAR*>(conn->outdsn),
sizeof(conn->outdsn), &buflen, SQL_DRIVER_COMPLETE);
CheckError(status, "SQLDriverConnectA", conn, true);
// SQL_ATTR_ANSI_APP is expected to be successful after connection.
status = SQLSetConnectAttr(conn->hdbc, SQL_ATTR_ANSI_APP,
ToSqlPointer(SQL_AA_FALSE), 0);
EXPECT_EQ(status, SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnectA) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn, true), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnect_SQL_DRIVER_COMPLETE) {
auto conn = std::make_shared<ODBCHandles>();
SQLHWND window_handle;
GetWindowHandle(window_handle);
EXPECT_EQ(ConnectWithPromptWindows(kDefaultConnectionString, conn,
window_handle, SQL_DRIVER_COMPLETE, true),
SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnect_SQL_DRIVER_COMPLETE_REQUIRED) {
auto conn = std::make_shared<ODBCHandles>();
SQLHWND window_handle;
GetWindowHandle(window_handle);
EXPECT_EQ(
ConnectWithPromptWindows(kDefaultConnectionString, conn, window_handle,
SQL_DRIVER_COMPLETE_REQUIRED, true),
SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnect_SQL_DRIVER_NOPROMPT) {
auto conn = std::make_shared<ODBCHandles>();
SQLHWND window_handle;
GetWindowHandle(window_handle);
EXPECT_EQ(ConnectWithPromptWindows(kDefaultConnectionString, conn,
window_handle, SQL_DRIVER_NOPROMPT, true),
SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLDriverConnect_StringDataRightTruncated) {
auto conn = std::make_shared<ODBCHandles>();
std::string conn_str = kDefaultConnectionString;
SQLCHAR in_conn_str[kBufferLength];
SQLCHAR out_conn_str[10] = {0};
SQLSMALLINT out_conn_str_len;
StrToChar((char*)in_conn_str, conn_str);
google::cloud::odbc_tests::SetAttributes(conn, 30, true);
auto status =
SQLDriverConnect(conn->hdbc, nullptr, (SQLCHAR*)in_conn_str, SQL_NTS,
(SQLCHAR*)out_conn_str, sizeof(out_conn_str),
&out_conn_str_len, SQL_DRIVER_COMPLETE);
PrintDriverVerName(conn);
EXPECT_EQ(status, SQL_SUCCESS_WITH_INFO);
EXPECT_NE(out_conn_str_len, sizeof(out_conn_str));
CleanupODBCHandles(*conn);
}
TEST(ConnectionTest, SQL_DriverConnect_CaseInsensitive) {
auto conn = std::make_shared<ODBCHandles>();
std::vector<std::string> const conn_string = {"dsn=" + GetDefaultDSN(),
"DSN=" + GetDefaultDSN(),
"DsN=" + GetDefaultDSN()};
for (auto const& conn_str : conn_string) {
EXPECT_EQ(Connect(conn_str, conn, true), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
}
TEST(ConnectionTest, SQLSetConnectAttr_StringWithNullTermInMiddle) {
SQLCHAR buf[256] = "te\0t";
SQLINTEGER len = strlen(reinterpret_cast<char*>(buf));
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
auto status = SQLSetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, len);
CheckError(status, "SQLSetConnectAttr", conn);
SQLCHAR output[256];
SQLINTEGER length;
status = SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ("te\0t", actual);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttrA_StringWithNullTermInMiddle) {
SQLCHAR buf[256] = "te\0t";
SQLINTEGER len = strlen(reinterpret_cast<char*>(buf));
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn, true), SQL_SUCCESS);
auto status = SQLSetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, len);
CheckError(status, "SQLSetConnectAttr", conn, true);
SQLCHAR output[256];
SQLINTEGER length;
status = SQLGetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn, true);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ("te\0t", actual);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttr_UpdateString) {
SQLCHAR buf[256] = "test";
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
// As per the spec if valuePtr is a character string data, string length
// should either the length of the string or SQL_NTS
// existing is not following the spec and accepts incorrect lengths,
// Google driver will follow the spec and accept correct lengths.
auto status = SQLSetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, 4);
CheckError(status, "SQLSetConnectAttr", conn);
std::string expected = "test";
buf[0] = '0';
std::string buffer = reinterpret_cast<char*>(buf);
EXPECT_EQ("0est", buffer);
SQLCHAR output[256];
SQLINTEGER length;
status = SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn);
std::string actual = reinterpret_cast<char*>(output);
// Parity with existing Driver - Original value is retained even though
// input buf has been modified by the caller.
EXPECT_EQ(expected, actual);
EXPECT_EQ(expected.size(), length);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttrA_UpdateString) {
SQLCHAR buf[256] = "test";
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn, true), SQL_SUCCESS);
// As per the spec if valuePtr is a character string data, string length
// should either the length of the string or SQL_NTS
// Existing Driver is not following the spec and accepts incorrect lengths,
// Google driver will follow the spec and accept correct lengths.
auto status = SQLSetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf, 4);
CheckError(status, "SQLSetConnectAttr", conn, true);
std::string expected = "test";
buf[0] = '0';
std::string buffer = reinterpret_cast<char*>(buf);
EXPECT_EQ("0est", buffer);
SQLCHAR output[256];
SQLINTEGER length;
status = SQLGetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn, true);
std::string actual = reinterpret_cast<char*>(output);
// Parity with existing Driver - Original value is retained even though
// input buf has been modified by the caller.
EXPECT_EQ(expected, actual);
EXPECT_EQ(expected.size(), length);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttrW_UpdateString) {
std::wstring wstr = L"test";
std::vector<SQLWCHAR> buf(wstr.begin(), wstr.end());
buf.emplace_back(L'\0');
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
auto status = SQLSetConnectAttrW(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)buf.data(), SQL_NTS);
CheckError(status, "SQLSetConnectAttrW", conn);
std::string expected = "test";
buf[0] = '0';
std::string buffer =
ConvertSQLWCHARToString(reinterpret_cast<SQLWCHAR*>(buf.data()), SQL_NTS);
EXPECT_STREQ("0est", buffer.data());
SQLWCHAR output[256];
SQLINTEGER length = 0;
status = SQLGetConnectAttrW(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttrW", conn);
std::string str_out = ConvertSQLWCHARToString(output, SQL_NTS);
EXPECT_EQ(expected, str_out);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttr_DeleteString) {
auto conn = std::make_shared<ODBCHandles>();
SetAttr(conn);
SQLCHAR output[256];
SQLINTEGER length;
auto status = SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ("test", actual);
EXPECT_EQ(4, length);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttrA_DeleteString) {
auto conn = std::make_shared<ODBCHandles>();
SetAttr(conn, true);
SQLCHAR output[256];
SQLINTEGER length;
auto status = SQLGetConnectAttrA(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn, true);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ("test", actual);
EXPECT_EQ(4, length);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttr_Integer) {
SQLULEN buf = SQL_ASYNC_ENABLE_ON;
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
auto status =
SQLSetConnectAttr(conn->hdbc, SQL_ATTR_ASYNC_ENABLE, (SQLPOINTER)buf, 4);
CheckError(status, "SQLSetConnectAttr", conn);
auto* buf_ptr = &buf;
*buf_ptr = 222;
EXPECT_EQ(222, buf);
SQLULEN output = 0;
SQLINTEGER len;
// Spec doesn't say anything about len being null so its upto the driver to
// implement. Google Driver does not accept nullptr for len.
status =
SQLGetConnectAttr(conn->hdbc, SQL_ATTR_ASYNC_ENABLE, &output, 256, &len);
CheckError(status, "SQLGetConnectAttr", conn);
// Parity with existing driver - Original value is retained even
// though input buf has been modified by the caller.
EXPECT_EQ(SQL_ASYNC_ENABLE_ON, output);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLSetConnectAttrA_Integer) {
SQLULEN buf = SQL_ASYNC_ENABLE_ON;
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn, true), SQL_SUCCESS);
auto status =
SQLSetConnectAttrA(conn->hdbc, SQL_ATTR_ASYNC_ENABLE, (SQLPOINTER)buf, 4);
CheckError(status, "SQLSetConnectAttr", conn, true);
auto* buf_ptr = &buf;
*buf_ptr = 222;
EXPECT_EQ(222, buf);
SQLULEN output = 0;
SQLINTEGER len;
// Spec doesn't say anything about len being null so its upto the driver to
// implement. Google Driver does not accept nullptr for len.
status =
SQLGetConnectAttrA(conn->hdbc, SQL_ATTR_ASYNC_ENABLE, &output, 256, &len);
CheckError(status, "SQLGetConnectAttr", conn, true);
// Parity with existing driver - Original value is retained even
// though input buf has been modified by the caller.
EXPECT_EQ(SQL_ASYNC_ENABLE_ON, output);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SQLGetConnectAttr_DefaultCatalog) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
SQLCHAR output[256];
SQLINTEGER length;
auto status = SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER)output, 256, &length);
CheckError(status, "SQLGetConnectAttr", conn);
std::string actual = reinterpret_cast<char*>(output);
EXPECT_EQ(kCatalogName, actual);
EXPECT_EQ(kCatalogName.size(), length);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, GetDefaultValueForAutocommit) {
auto conn = std::make_shared<ODBCHandles>();
EXPECT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS);
SQLUINTEGER commit_mode = 0;
auto status =
SQLGetConnectAttr(conn->hdbc, SQL_ATTR_AUTOCOMMIT, &commit_mode, 0, NULL);
CheckError(status, "SQLGetConnectAttr", conn);
EXPECT_EQ(SQL_AUTOCOMMIT_ON, commit_mode);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
// TODO(b/435322167): External Authentication crash with service account key
TEST(ConnectionTest, DISABLED_FailsForExternalAuthWithServiceAccountJson) {
std::shared_ptr<ODBCHandles> conn = std::make_shared<ODBCHandles>();
ASSERT_TRUE(conn != nullptr);
auto service_account_path =
GetEnv("CPP_BIGQUERY_ODBC_TEST_SERVICE_ACCOUNT_AUTH_KEY").value_or("");
ASSERT_FALSE(service_account_path.empty())
<< "CPP_BIGQUERY_ODBC_TEST_SERVICE_ACCOUNT_AUTH_KEY is not set";
std::string driver_name = GetDriverName();
std::string conn_str = "DRIVER={" + driver_name +
"};"
"OAuthMechanism=4;"
"KeyFilePath=" +
service_account_path + ";";
EXPECT_EQ(Connect(conn_str, conn, false), SQL_ERROR);
}
#ifdef BQ_DRIVER_INTEGRATION_TESTS
TEST(ConnectionTest, SuccessForExternalAuthWithExternalAccountJson) {
std::shared_ptr<ODBCHandles> conn = std::make_shared<ODBCHandles>();
ASSERT_TRUE(conn != nullptr);
auto external_account_path =
GetEnv("CPP_BIGQUERY_ODBC_TEST_EXTERNAL_ACCOUNT_AUTH_KEY").value_or("");
ASSERT_FALSE(external_account_path.empty())
<< "CPP_BIGQUERY_ODBC_TEST_EXTERNAL_ACCOUNT_AUTH_KEY is not set";
std::string driver_name = GetDriverName();
std::string conn_str = "DRIVER={" + driver_name +
"};"
"OAuthMechanism=4;"
"KeyFilePath=" +
external_account_path + ";";
EXPECT_EQ(Connect(conn_str, conn, 30, false), SQL_SUCCESS);
EXPECT_EQ(Disconnect(conn), SQL_SUCCESS);
}
TEST(ConnectionTest, SuccessForExternalAuthWithBYOIDProperties) {
std::shared_ptr<ODBCHandles> conn = std::make_shared<ODBCHandles>();
ASSERT_TRUE(conn != nullptr);
auto external_account_path =
GetEnv("CPP_BIGQUERY_ODBC_TEST_EXTERNAL_ACCOUNT_AUTH_KEY").value_or("");
ASSERT_FALSE(external_account_path.empty())
<< "CPP_BIGQUERY_ODBC_TEST_EXTERNAL_ACCOUNT_AUTH_KEY is not set";
std::ifstream f(external_account_path);
nlohmann::json credentials = nlohmann::json::parse(f);
std::string audience = credentials["audience"];
std::string credential_source = credentials["credential_source"].dump();
std::string subject_token_type = credentials["subject_token_type"];
std::string token_url = credentials["token_url"];
std::string driver_name = GetDriverName();