-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathodbc_windows.cc
More file actions
342 lines (315 loc) · 14.3 KB
/
Copy pathodbc_windows.cc
File metadata and controls
342 lines (315 loc) · 14.3 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
// Copyright 2024 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/bq_driver/odbc_windows.h"
#include "google/cloud/odbc/bq_client_interface/odbc_authentication.h"
#include "google/cloud/odbc/bq_driver/internal/driver_adv_opt_form.h"
#include "google/cloud/odbc/bq_driver/internal/driver_form.h"
#include "google/cloud/odbc/bq_driver/internal/driver_form_proxy.h"
#include "google/cloud/odbc/bq_driver/internal/odbc_internal_commons.h"
#include "google/cloud/odbc/bq_driver/internal/trace_utils.h"
#include "google/cloud/odbc/internal/status_record_or.h"
namespace google::cloud::odbc_bq_driver {
using ::google::cloud::odbc_bigquery_client_interface::OauthMechanism;
using google::cloud::odbc_bq_driver_internal::AddLogTraceToRegistry;
using google::cloud::odbc_bq_driver_internal::AdvanceOptions;
using google::cloud::odbc_bq_driver_internal::ConvertLPCSTRToString;
using google::cloud::odbc_bq_driver_internal::DriverForm;
using google::cloud::odbc_bq_driver_internal::EncryptPassword;
using google::cloud::odbc_bq_driver_internal::GetOdbcTraceConfigPath;
using google::cloud::odbc_bq_driver_internal::GetPathToOdbcIni;
using google::cloud::odbc_bq_driver_internal::GetSectionWin;
using google::cloud::odbc_bq_driver_internal::GetUpperStr;
using google::cloud::odbc_bq_driver_internal::GetValueOrDefault;
using google::cloud::odbc_bq_driver_internal::kDefaultMaxFiles;
using google::cloud::odbc_bq_driver_internal::kDefaultMaxSize;
using google::cloud::odbc_bq_driver_internal::kLogFileCount;
using google::cloud::odbc_bq_driver_internal::kLogFileSize;
using google::cloud::odbc_bq_driver_internal::kLogLevel;
using google::cloud::odbc_bq_driver_internal::kLogPath;
using ::google::cloud::odbc_bq_driver_internal::LanguageDialect;
using google::cloud::odbc_bq_driver_internal::LogLevel;
using google::cloud::odbc_bq_driver_internal::LogTraceDialog;
using google::cloud::odbc_bq_driver_internal::ParseConnectionString;
using google::cloud::odbc_bq_driver_internal::ProxyOptions;
using google::cloud::odbc_bq_driver_internal::Section;
using google::cloud::odbc_bq_driver_internal::Trim;
using google::cloud::odbc_internal::StatusRecordOr;
std::string ConvertOAuthMechanism(std::string o_auth_mechanism) {
std::string o_auth_value;
if (o_auth_mechanism == "Service Authentication") {
o_auth_value = std::to_string(
static_cast<int>(OauthMechanism::kServiceAndUserAccount));
} else if (o_auth_mechanism == "Application Default Credentials") {
o_auth_value =
std::to_string(static_cast<int>(OauthMechanism::kApplicationDefault));
} else if (o_auth_mechanism == "External Account Authentication") {
o_auth_value =
std::to_string(static_cast<int>(OauthMechanism::kExternalUser));
} else
o_auth_value = "";
return o_auth_value;
}
std::string ConvertLanguageDialect(std::string language_dialect) {
std::string language_dialect_value;
if (language_dialect == "GoogleSQL") {
language_dialect_value =
std::to_string(static_cast<int>(LanguageDialect::kStandardSQL));
} else if (language_dialect == "LegacySQL") {
language_dialect_value =
std::to_string(static_cast<int>(LanguageDialect::kLegacySQL));
} else
language_dialect_value = "";
return language_dialect_value;
}
// TODO(b/b/391859145): Customization and Support For Logging and Driver
// Parameters
std::string ConvertLogLevel(std::string log_level) {
std::string log_level_val;
if (log_level == "LOG_INFO") {
log_level_val = std::to_string(static_cast<int>(LogLevel::kLogInfo));
} else if (log_level == "LOG_OFF") {
log_level_val = std::to_string(static_cast<int>(LogLevel::kLogOff));
} else if (log_level == "LOG_ERROR") {
log_level_val = std::to_string(static_cast<int>(LogLevel::kLogError));
} else if (log_level == "LOG_WARNING") {
log_level_val = std::to_string(static_cast<int>(LogLevel::kLogWarning));
} else {
log_level_val = std::to_string(static_cast<int>(LogLevel::kLogOff));
}
return log_level_val;
}
bool ConfigDSNInternal(HWND hwnd_parent, WORD f_request, LPCSTR lpsz_driver,
LPCSTR lpsz_attributes) {
if (!lpsz_driver) {
return FALSE;
}
std::string attribute = ConvertLPCSTRToString(lpsz_attributes);
StatusRecordOr<Section> status_or_section = ParseConnectionString(attribute);
Section section = *status_or_section;
std::string dsn_value = GetValueOrDefault(section, dsn_key, kDefaultDsnValue);
std::string dsn_name;
std::string key_file_path = GetValueOrDefault(section, key_file_path_key);
std::string o_auth_mechanism =
ConvertOAuthMechanism(GetValueOrDefault(section, oauth_mechanism_key));
std::string catalog = GetValueOrDefault(section, catalog_key);
std::string dataset_name = GetValueOrDefault(section, dataset_key);
std::string encrypt_data = GetValueOrDefault(section, encrypt_data_key);
std::string trusted_certs = GetValueOrDefault(section, trusted_certs_key);
std::string min_tls_version = GetValueOrDefault(section, min_tls_key);
std::string description = GetValueOrDefault(section, description_key);
std::string log_level =
ConvertLogLevel(GetValueOrDefault(section, kLogLevel, kDefaultLogLevel));
std::string log_file = GetValueOrDefault(section, kLogPath);
std::string log_max_files =
GetValueOrDefault(section, kLogFileCount, kDefaultMaxFiles);
std::string log_max_size =
GetValueOrDefault(section, kLogFileSize, kDefaultMaxSize);
std::string language_dialect =
ConvertLanguageDialect(GetValueOrDefault(section, sql_dialect_key));
std::string large_dataset_name = GetValueOrDefault(
section, large_results_dataset_key, kDefaultLargeDatasetName);
std::string encryption_key_value = GetValueOrDefault(section, encryption_key);
std::string rows_per_block =
GetValueOrDefault(section, rows_per_block_key, kDefaultRowsPerBlock);
std::string default_string_length = GetValueOrDefault(
section, default_string_length_key, kDefaultStringLength);
std::string temp_expiration =
GetValueOrDefault(section, temp_expiration_key, kDefaultTempExpiration);
std::string session_location =
GetValueOrDefault(section, session_location_key);
std::string max_threads =
GetValueOrDefault(section, max_threads_key, kDefaultMaxThreads);
std::string max_retries =
GetValueOrDefault(section, max_retries_key, kDefaultMaxRetries);
std::string additional_projects =
GetValueOrDefault(section, additional_projects_key);
std::string query_properties =
GetValueOrDefault(section, query_properties_key);
std::string activation_threshold =
GetValueOrDefault(section, activation_threshold_key);
std::string use_wvarchar = GetValueOrDefault(section, use_wvarchar_key);
std::string enable_session = GetValueOrDefault(section, enable_session_key);
std::string htapi_activation_threshold_check =
GetValueOrDefault(section, htapi_activation_threshold_check_key);
std::string allow_large_results =
GetValueOrDefault(section, allow_large_results_key);
std::string use_default_large_results_dataset =
GetValueOrDefault(section, use_default_large_results_dataset_key);
std::string proxy_check = GetValueOrDefault(section, proxy_check_key);
std::string proxy_host = GetValueOrDefault(section, proxy_host_key);
std::string proxy_port = GetValueOrDefault(section, proxy_port_key);
std::string proxy_username = GetValueOrDefault(section, proxy_username_key);
std::string proxy_pwd = GetValueOrDefault(section, proxy_pwd_key);
std::string proxy_pwd_enc =
EncryptPassword(GetValueOrDefault(section, proxy_pwd_enc_key));
std::string encryption_type_value =
GetValueOrDefault(section, encryption_type);
DriverForm form;
AdvanceOptions advance_form;
ProxyOptions proxy_form;
auto CreateSectionFromForm = [&]() -> Section {
return {
{key_file_path_key, key_file_path},
{oauth_mechanism_key, o_auth_mechanism},
{catalog_key, catalog},
{dataset_key, dataset_name},
{encrypt_data_key, encrypt_data},
{trusted_certs_key, trusted_certs},
{min_tls_key, min_tls_version},
{description_key, description},
{sql_dialect_key, language_dialect},
{large_results_dataset_key, large_dataset_name},
{encryption_key, encryption_key_value},
{rows_per_block_key, rows_per_block},
{default_string_length_key, default_string_length},
{temp_expiration_key, temp_expiration},
{session_location_key, session_location},
{additional_projects_key, additional_projects},
{query_properties_key, query_properties},
{activation_threshold_key, activation_threshold},
{use_wvarchar_key, use_wvarchar},
{enable_session_key, enable_session},
{max_threads_key, max_threads},
{max_retries_key, max_retries},
{htapi_activation_threshold_check_key,
htapi_activation_threshold_check},
{allow_large_results_key, allow_large_results},
{use_default_large_results_dataset_key,
use_default_large_results_dataset},
{encryption_type, encryption_type_value},
{proxy_check_key, proxy_check},
{proxy_host_key, proxy_host},
{proxy_port_key, proxy_port},
{proxy_username_key, proxy_username},
{proxy_pwd_key, proxy_pwd},
{proxy_pwd_enc_key, proxy_pwd_enc},
};
};
auto CreateSectionFromLogForm = [&]() -> Section {
return {{kLogLevel, log_level},
{kLogPath, log_file},
{kLogFileCount, log_max_files},
{kLogFileSize, log_max_size}};
};
auto ShowFormAndReturnValues = [&]() -> std::string {
form.Show();
form.GetHwnd();
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
dsn_name = form.GetDSN();
key_file_path = form.GetKeyFilePath();
o_auth_mechanism = ConvertOAuthMechanism(form.GetOAuthMechanism());
catalog = form.GetCatalogName();
dataset_name = form.GetDatasetName();
encrypt_data = form.GetEncryptData();
trusted_certs = form.GetTrustedCerts();
min_tls_version = form.GetMinTls();
description = form.GetDescription();
log_level = ConvertLogLevel(form.GetLogLevel());
log_file = form.GetLogFilePath();
log_max_files = form.GetLogMaxFiles();
log_max_size = form.GetLogMaxSize();
language_dialect =
ConvertLanguageDialect(advance_form.GetLanguageDialect());
large_dataset_name = advance_form.GetDatasetName();
encryption_key_value = advance_form.GetEncryptionKey();
rows_per_block = advance_form.GetRowsPerBlock();
default_string_length = advance_form.GetDefaultStringLength();
temp_expiration = advance_form.GetTempTableExpiration();
session_location = advance_form.GetSessionLocation();
additional_projects = advance_form.GetAdditionalProjects();
query_properties = advance_form.GetQueryProperties();
activation_threshold = advance_form.GetActivationThreshold();
use_wvarchar = advance_form.GetUseWVarChar();
enable_session = advance_form.GetEnableSession();
max_threads = advance_form.GetMaxThreads();
max_retries = advance_form.GetMaxRetries();
htapi_activation_threshold_check =
advance_form.GetActivationThresholdCheckbox();
allow_large_results = advance_form.GetAllowLargeResults();
use_default_large_results_dataset =
advance_form.GetUseDefaultLargeResults();
encryption_type_value = advance_form.GetEncryptionType();
proxy_check = proxy_form.GetProxyCheck();
proxy_host = proxy_form.GetProxyHost();
proxy_port = proxy_form.GetProxyPort();
proxy_username = proxy_form.GetProxyUsername();
proxy_pwd_enc = EncryptPassword(proxy_form.GetProxyPass());
return dsn_name;
};
switch (f_request) {
case ODBC_ADD_DSN: {
std::string dsn_name = dsn_value;
if (hwnd_parent != NULL) {
form.ResetToDefaults();
dsn_name = ShowFormAndReturnValues();
if (dsn_name.empty()) {
return FALSE;
}
}
Section section = CreateSectionFromForm();
if (!SQLWriteDSNToIni(dsn_name.c_str(), lpsz_driver)) {
return FALSE;
}
for (auto const& kv : section) {
SQLWritePrivateProfileString(dsn_name.c_str(), kv.first.c_str(),
kv.second.c_str(), "ODBC.INI");
}
Section trace_section = CreateSectionFromLogForm();
AddLogTraceToRegistry(trace_section);
return TRUE;
}
case ODBC_CONFIG_DSN: {
std::string dsn_name = dsn_value;
if (hwnd_parent != NULL) {
int const BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
Section section = CreateSectionFromForm();
for (auto& kv : section) {
SQLGetPrivateProfileString(dsn_name.c_str(), kv.first.c_str(), "",
buffer, BUFFER_SIZE, "ODBC.INI");
section[kv.first.c_str()] = buffer;
}
section[dsn_key] = dsn_value;
std::string driver_registry_key = GetOdbcTraceConfigPath() + "\\Driver";
auto trace_result = GetSectionWin(driver_registry_key);
auto trace_section = trace_result.GetValue();
form.SetValues(section);
advance_form.SetValues(section);
proxy_form.SetValues(section);
form.SetLogTraceValues(*trace_section);
dsn_name = ShowFormAndReturnValues();
Section trace_config_section = CreateSectionFromLogForm();
AddLogTraceToRegistry(trace_config_section);
}
Section section_config = CreateSectionFromForm();
for (auto const& kv : section_config) {
SQLWritePrivateProfileString(dsn_name.c_str(), kv.first.c_str(),
kv.second.c_str(), "ODBC.INI");
}
return TRUE;
}
case ODBC_REMOVE_DSN:
if (!SQLRemoveDSNFromIni(dsn_value.c_str())) {
return FALSE;
}
return TRUE;
default:
return FALSE;
}
}
} // namespace google::cloud::odbc_bq_driver