-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathodbc_desc_attr.cc
More file actions
555 lines (528 loc) · 17.1 KB
/
Copy pathodbc_desc_attr.cc
File metadata and controls
555 lines (528 loc) · 17.1 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
// 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/internal/odbc_desc_attr.h"
#include "google/cloud/odbc/bq_driver/internal/trace_utils.h"
#include "google/cloud/odbc/internal/sql_state_constants.h"
#include "google/cloud/odbc/internal/status_record_or.h"
#include <vector>
namespace google::cloud::odbc_bq_driver_internal {
using google::cloud::odbc_internal::SQLStates;
using google::cloud::odbc_internal::StatusRecord;
void HeaderRecord::CopyHeaderRecordsFrom(HeaderRecord const& header_record) {
LOG(INFO) << "HeaderRecord::CopyHeaderRecordsFrom:: Copying header records.";
array_size = header_record.array_size;
array_status_ptr = header_record.array_status_ptr;
bind_offset_ptr = header_record.bind_offset_ptr;
bind_type = header_record.bind_type;
rows_processed_ptr = header_record.rows_processed_ptr;
}
void DescriptorRecord::SetName(std::string const& val,
SQLINTEGER const buffer_len) {
if (val.empty() || buffer_len == 0) {
name = "";
unnamed = SQL_UNNAMED;
} else {
if (buffer_len == SQL_NTS ||
buffer_len >= static_cast<SQLINTEGER>(val.size())) {
name = val;
} else {
name = val.substr(0, buffer_len);
}
unnamed = SQL_NAMED;
}
}
StatusRecord DescriptorRecord::SetNumPrecRadix(SQLINTEGER value) {
if (value != kNumPrecRadixForNonNumeric &&
value != kNumPrecRadixForApproximateNumeric &&
value != kNumPrecRadixForExactNumeric) {
LOG(ERROR) << "DescriptorRecord::SetNumPrecRadix:: Invalid "
"attribute/option identifier: "
<< value;
return {SQLStates::k_HY092(), "Invalid attribute/option identifier"};
}
num_prec_radix = value;
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetParameterType(SQLSMALLINT value) {
if (value != SQL_PARAM_INPUT && value != SQL_PARAM_INPUT_OUTPUT &&
value != SQL_PARAM_OUTPUT) {
LOG(ERROR)
<< "DescriptorRecord::SetParameterType:: Invalid parameter type: "
<< value;
return {SQLStates::k_HY105(), "Invalid parameter type"};
}
parameter_type = value;
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetUnnamed(SQLSMALLINT value) {
if (value != SQL_UNNAMED) {
LOG(ERROR) << "DescriptorRecord::SetUnnamed:: Invalid descriptor field "
"identifier: "
<< value;
return {SQLStates::k_HY091(), "Invalid descriptor field identifier"};
}
unnamed = value;
return StatusRecord::Ok();
}
SQLSMALLINT GetPrecisionForIntervalCode(SQLSMALLINT datetime_interval_code) {
return (datetime_interval_code == SQL_CODE_SECOND ||
datetime_interval_code == SQL_CODE_DAY_TO_SECOND ||
datetime_interval_code == SQL_CODE_HOUR_TO_SECOND ||
datetime_interval_code == SQL_CODE_MINUTE_TO_SECOND)
? kDefaultIntervalSecondsPrecision
: kDefaultIntervalPrecision;
}
SQLSMALLINT GetPrecisionForDatetimeCode(SQLSMALLINT datetime_interval_code) {
return (datetime_interval_code == SQL_CODE_TIMESTAMP)
? kDefaultIntervalSecondsPrecision
: kDefaultIntervalPrecision;
}
SQLSMALLINT GetLengthForDatetimeCode(SQLSMALLINT datetime_interval_code) {
switch (datetime_interval_code) {
case SQL_CODE_DATE:
return 10;
case SQL_CODE_TIME:
return 8;
case SQL_CODE_TIMESTAMP:
return 26;
default:
return 0;
}
}
SQLSMALLINT GetLengthForIntervalCode(SQLSMALLINT datetime_interval_code) {
switch (datetime_interval_code) {
case SQL_CODE_MONTH:
case SQL_CODE_YEAR:
case SQL_CODE_DAY:
case SQL_CODE_HOUR:
case SQL_CODE_MINUTE:
return 2;
case SQL_CODE_YEAR_TO_MONTH:
case SQL_CODE_DAY_TO_HOUR:
case SQL_CODE_HOUR_TO_MINUTE:
return 5;
case SQL_CODE_DAY_TO_MINUTE:
return 8;
case SQL_CODE_SECOND:
return 9;
case SQL_CODE_DAY_TO_SECOND:
return 18;
case SQL_CODE_HOUR_TO_SECOND:
return 15;
case SQL_CODE_MINUTE_TO_SECOND:
return 12;
default:
return 0;
}
}
void DescriptorRecord::SetIntervalType(Interval const& entry,
DescriptorType desc_type) {
type = SQL_INTERVAL;
concise_type = (IsDescriptorTypeApplication(desc_type))
? entry.concise_c_type
: entry.concise_sql_type;
datetime_interval_precision = 2;
datetime_interval_code = entry.datetime_interval_code;
precision = GetPrecisionForIntervalCode(datetime_interval_code);
scale = precision;
if (IsDescriptorTypeApplication(desc_type)) {
length = 0;
} else {
length = GetLengthForIntervalCode(entry.datetime_interval_code);
}
}
void DescriptorRecord::SetDatetimeType(Interval const& entry,
DescriptorType desc_type) {
type = SQL_DATETIME;
concise_type = (IsDescriptorTypeApplication(desc_type))
? entry.concise_c_type
: entry.concise_sql_type;
datetime_interval_code = entry.datetime_interval_code;
precision = GetPrecisionForDatetimeCode(entry.datetime_interval_code);
scale = (entry.datetime_interval_code == SQL_CODE_TIMESTAMP &&
desc_type == DescriptorType::kIPD)
? 0
: precision;
if (IsDescriptorTypeApplication(desc_type)) {
length = 0;
} else {
length = GetLengthForDatetimeCode(entry.datetime_interval_code);
}
datetime_interval_precision = 0;
}
StatusRecord DescriptorRecord::SetOtherCType(SQLSMALLINT const value,
std::string const& error_message) {
switch (value) {
case SQL_C_CHAR:
case SQL_C_BINARY:
// case SQL_C_VARBOOKMARK: (this macro has same value as SQL_C_BINARY)
type = concise_type = value;
precision = length = 1;
datetime_interval_precision = 0;
break;
case SQL_C_NUMERIC:
type = concise_type = value;
precision = length = 38;
datetime_interval_precision = 0;
break;
case SQL_C_FLOAT:
type = concise_type = value;
precision = length = 24;
datetime_interval_precision = 0;
break;
case SQL_C_DOUBLE:
type = concise_type = value;
precision = length = 53;
datetime_interval_precision = 0;
break;
case SQL_C_BIT:
case SQL_C_WCHAR:
case SQL_C_SSHORT:
case SQL_C_USHORT:
case SQL_C_SHORT:
case SQL_C_SLONG:
case SQL_C_ULONG:
case SQL_C_LONG:
case SQL_C_STINYINT:
case SQL_C_UTINYINT:
case SQL_C_TINYINT:
case SQL_C_SBIGINT:
case SQL_C_UBIGINT:
type = concise_type = value;
datetime_interval_precision = precision = length = 0;
break;
case SQL_C_GUID:
type = concise_type = value;
precision = length = 16;
datetime_interval_precision = 0;
break;
default:
LOG(ERROR) << "DescriptorRecord::SetOtherCType:: " << error_message
<< ": " << value;
return StatusRecord{SQLStates::k_HY021(), error_message};
}
datetime_interval_code = scale = 0;
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetOtherSQLType(
SQLSMALLINT const value, std::string const& error_message) {
switch (value) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
type = concise_type = value;
length = 1;
precision = 0;
datetime_interval_precision = 0;
break;
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
type = concise_type = value;
datetime_interval_precision = 0;
break;
case SQL_NUMERIC:
case SQL_DECIMAL:
type = concise_type = value;
datetime_interval_precision = 0;
precision = length = 38;
scale = 0;
break;
case SQL_SMALLINT:
type = concise_type = value;
datetime_interval_precision = 0;
length = 5;
break;
case SQL_INTEGER:
type = concise_type = value;
datetime_interval_precision = 0;
length = 10;
break;
case SQL_REAL:
type = concise_type = value;
datetime_interval_precision = 0;
precision = 24;
length = 7;
break;
case SQL_FLOAT:
case SQL_DOUBLE:
type = concise_type = value;
datetime_interval_precision = 0;
precision = 53;
length = 15;
break;
case SQL_BIT:
type = concise_type = value;
datetime_interval_precision = 0;
length = 1;
break;
case SQL_TINYINT:
type = concise_type = value;
datetime_interval_precision = 0;
length = 3;
break;
case SQL_BIGINT:
type = concise_type = value;
datetime_interval_precision = 0;
length = 19;
break;
case SQL_GUID:
type = concise_type = value;
datetime_interval_precision = 0;
precision = 0;
length = 36;
break;
default:
LOG(ERROR) << "DescriptorRecord::SetOtherSQLType:: " << error_message
<< ": " << value;
return StatusRecord{SQLStates::k_HY021(), error_message};
}
datetime_interval_code = 0;
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetDisplaySize(SQLSMALLINT type,
SQLINTEGER value,
SQLINTEGER precision) {
if (!type) {
LOG(ERROR) << "DescriptorRecord::SetDisplaySize:: Invalid attribute/option "
"identifier (type is null).";
return StatusRecord{SQLStates::k_HY092(),
"Invalid attribute/option identifier"};
}
switch (type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
display_size = value;
break;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
display_size = 2 * value;
break;
case SQL_DECIMAL:
case SQL_NUMERIC:
display_size = precision + 2;
break;
case SQL_SMALLINT:
display_size = 6;
break;
case SQL_INTEGER:
display_size = 11;
break;
case SQL_BIGINT:
display_size = 20;
break;
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
display_size = 24;
break;
case SQL_DATE:
display_size = 10;
break;
case SQL_TIME:
display_size = 8;
break;
case SQL_TIMESTAMP:
display_size = 19;
break;
default:
display_size = value;
break;
}
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetOctetLength(SQLSMALLINT type,
SQLINTEGER value,
SQLINTEGER precision) {
if (!type) {
LOG(ERROR) << "DescriptorRecord::SetOctetLength:: Invalid attribute/option "
"identifier (type is null).";
return StatusRecord{SQLStates::k_HY092(),
"Invalid attribute/option identifier"};
}
switch (type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
octet_length = 4 * precision;
break;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
octet_length = value;
break;
case SQL_WCHAR:
case SQL_WLONGVARCHAR:
octet_length = value * sizeof(SQLWCHAR);
break;
case SQL_WVARCHAR:
octet_length = value * 4;
break;
case SQL_DECIMAL:
case SQL_NUMERIC:
octet_length = (precision + 2);
break;
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_BIGINT:
octet_length = 20;
break;
case SQL_REAL:
octet_length = sizeof(SQLREAL);
break;
case SQL_FLOAT:
case SQL_DOUBLE:
octet_length = sizeof(SQLDOUBLE);
break;
case SQL_GUID:
octet_length = 16;
break;
case SQL_TYPE_TIME:
case SQL_TYPE_DATE:
octet_length = 6;
break;
case SQL_TYPE_TIMESTAMP:
octet_length = 16;
break;
default:
octet_length = value;
break;
}
return StatusRecord::Ok();
}
StatusRecord DescriptorRecord::SetType(SQLSMALLINT value,
DescriptorType const& desc_type) {
if (value == SQL_INTERVAL) {
for (auto const& entry : kIntervalTypes) {
if (datetime_interval_code == entry.datetime_interval_code) {
SetIntervalType(entry, desc_type);
return StatusRecord::Ok();
}
}
LOG(ERROR) << "DescriptorRecord::SetType:: Interval code "
<< datetime_interval_code << " invalid or not supported.";
return StatusRecord{SQLStates::k_HY021(),
"Interval code invalid or not supported"};
}
if (value == SQL_DATETIME) {
for (auto const& entry : kDatetimeTypes) {
if (datetime_interval_code == entry.datetime_interval_code) {
SetDatetimeType(entry, desc_type);
return StatusRecord::Ok();
}
}
LOG(ERROR) << "DescriptorRecord::SetType:: Datetime interval code "
<< datetime_interval_code << " invalid or not supported.";
return StatusRecord{SQLStates::k_HY021(),
"Datetime interval code invalid or not supported"};
}
if (IsDescriptorTypeApplication(desc_type)) {
return SetOtherCType(value, "Illegal descriptor type");
}
return SetOtherSQLType(value, "Illegal descriptor type");
}
StatusRecord DescriptorRecord::SetConciseType(SQLSMALLINT value,
DescriptorType const& desc_type) {
for (auto const& entry : kIntervalTypes) {
if (entry.concise_sql_type == value || entry.concise_c_type == value) {
SetIntervalType(entry, desc_type);
return StatusRecord::Ok();
}
}
for (auto const& entry : kDatetimeTypes) {
if (entry.concise_sql_type == value || entry.concise_c_type == value) {
SetDatetimeType(entry, desc_type);
return StatusRecord::Ok();
}
}
std::string error_message =
"Illegal descriptor concise type: " + std::to_string(value);
if (IsDescriptorTypeApplication(desc_type)) {
return SetOtherCType(value, error_message);
}
return SetOtherSQLType(value, error_message);
}
bool DescriptorRecord::IsTypeValid(SQLSMALLINT valid_type,
SQLSMALLINT valid_concise_type,
SQLSMALLINT valid_code) const {
return type == valid_type && concise_type == valid_concise_type &&
datetime_interval_code == valid_code;
}
bool DescriptorRecord::IsTypeValid(SQLSMALLINT valid_type,
Interval const& interval) const {
return IsTypeValid(valid_type, interval.concise_sql_type,
interval.datetime_interval_code) ||
IsTypeValid(valid_type, interval.concise_c_type,
interval.datetime_interval_code);
}
StatusRecord DescriptorRecord::ConsistencyCheck() const {
if (type == SQL_C_DEFAULT && concise_type == SQL_C_DEFAULT) {
return StatusRecord::Ok();
}
for (auto const& entry : kIntervalTypes) {
if (IsTypeValid(SQL_INTERVAL, entry) &&
precision == GetPrecisionForIntervalCode(datetime_interval_code)) {
return StatusRecord::Ok();
}
}
for (auto const& entry : kDatetimeTypes) {
if (IsTypeValid(SQL_DATETIME, entry) &&
precision == GetPrecisionForDatetimeCode(datetime_interval_code)) {
return StatusRecord::Ok();
}
}
if (IsTypeValid(SQL_DATETIME, SQL_DATE, SQL_CODE_DATE)) {
return StatusRecord::Ok();
}
if (IsTypeValid(SQL_DATETIME, SQL_TIME, SQL_CODE_TIME)) {
return StatusRecord::Ok();
}
if (IsTypeValid(SQL_DATETIME, SQL_TIMESTAMP, SQL_CODE_TIMESTAMP)) {
return StatusRecord::Ok();
}
if (std::find(kOtherSQLSupportedTypes.begin(), kOtherSQLSupportedTypes.end(),
type) != kOtherSQLSupportedTypes.end() &&
type == concise_type) {
return StatusRecord::Ok();
}
if (std::find(kOtherCSupportedTypes.begin(), kOtherCSupportedTypes.end(),
type) != kOtherCSupportedTypes.end() &&
type == concise_type) {
return StatusRecord::Ok();
}
LOG(ERROR) << "DescriptorRecord::ConsistencyCheck:: Inconsistent descriptor "
"information. Type: "
<< type << ", ConciseType: " << concise_type;
return StatusRecord{SQLStates::k_HY021(),
"Inconsistent descriptor information"};
}
StatusRecord DescriptorRecord::SetDataPointer(SQLPOINTER ptr,
DescriptorType const& desc_type) {
StatusRecord status_record = ConsistencyCheck();
if (!status_record.ok()) {
LOG(ERROR) << "DescriptorRecord::SetDataPointer::ConsistencyCheck:: "
<< status_record.message;
return status_record;
}
if (desc_type != DescriptorType::kIPD) {
data_ptr = ptr;
}
return StatusRecord::Ok();
}
} // namespace google::cloud::odbc_bq_driver_internal