-
Notifications
You must be signed in to change notification settings - Fork 88
/
type_info.h
executable file
·2260 lines (1858 loc) · 82.9 KB
/
type_info.h
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
#pragma once
#include "driver/platform/platform.h"
#include "driver/utils/utils.h"
#include "driver/utils/conversion.h"
#include "driver/exception.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <limits>
#include <map>
#include <cstring>
#define lengthof(a) (sizeof(a) / sizeof(a[0]))
struct TypeInfo {
std::string sql_type_name;
bool is_unsigned;
SQLSMALLINT sql_type;
// https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/column-size-decimal-digits-transfer-octet-length-and-display-size
int32_t column_size; // max width of value in textual represntation, e.g. number of decimal digits fror numeric types.
int32_t octet_length; // max binary size of value in memory.
static constexpr auto string_max_size = 0xFFFFFF;
inline bool isIntegerType() const noexcept {
return sql_type == SQL_TINYINT || sql_type == SQL_SMALLINT || sql_type == SQL_INTEGER || sql_type == SQL_BIGINT;
}
inline bool isBufferType() const noexcept {
return
sql_type == SQL_CHAR || sql_type == SQL_VARCHAR || sql_type == SQL_LONGVARCHAR ||
sql_type == SQL_WCHAR || sql_type == SQL_WVARCHAR || sql_type == SQL_WLONGVARCHAR ||
sql_type == SQL_BINARY || sql_type == SQL_VARBINARY || sql_type == SQL_LONGVARBINARY
;
}
inline bool isWideCharStringType() const noexcept {
return sql_type == SQL_WCHAR || sql_type == SQL_WVARCHAR || sql_type == SQL_WLONGVARCHAR;
}
};
extern const std::map<std::string, TypeInfo> types_g;
inline const TypeInfo & type_info_for(const std::string & type) {
const auto it = types_g.find(type);
if (it == types_g.end())
throw std::runtime_error("unknown type");
return it->second;
}
enum class DataSourceTypeId {
Unknown,
Date,
DateTime,
Decimal,
Decimal32,
Decimal64,
Decimal128,
FixedString,
Float32,
Float64,
Int8,
Int16,
Int32,
Int64,
Nothing,
String,
UInt8,
UInt16,
UInt32,
UInt64,
UUID
};
DataSourceTypeId convertUnparametrizedTypeNameToTypeId(const std::string & type_name);
std::string convertTypeIdToUnparametrizedCanonicalTypeName(DataSourceTypeId type_id);
SQLSMALLINT convertSQLTypeToCType(SQLSMALLINT sql_type) noexcept;
bool isVerboseType(SQLSMALLINT type) noexcept;
bool isConciseDateTimeIntervalType(SQLSMALLINT sql_type) noexcept;
bool isConciseNonDateTimeIntervalType(SQLSMALLINT sql_type) noexcept;
SQLSMALLINT tryConvertSQLTypeToVerboseType(SQLSMALLINT type) noexcept;
SQLSMALLINT convertSQLTypeToDateTimeIntervalCode(SQLSMALLINT type) noexcept;
SQLSMALLINT convertDateTimeIntervalCodeToSQLType(SQLSMALLINT code, SQLSMALLINT verbose_type) noexcept;
bool isIntervalCode(SQLSMALLINT code) noexcept;
bool intervalCodeHasSecondComponent(SQLSMALLINT code) noexcept;
bool isInputParam(SQLSMALLINT param_io_type) noexcept;
bool isOutputParam(SQLSMALLINT param_io_type) noexcept;
bool isStreamParam(SQLSMALLINT param_io_type) noexcept;
/// Helper structure that represents information about where and
/// how to get or put values when reading or writing bound buffers.
struct BindingInfo {
SQLSMALLINT c_type = SQL_C_DEFAULT;
SQLPOINTER value = nullptr;
SQLLEN value_max_size = 0;
SQLLEN * value_size = nullptr;
SQLLEN * indicator = nullptr;
// These are relevant only for bound SQL_NUMERIC/SQL_C_NUMERIC or SQL_DECIMAL.
std::int16_t precision = 0;
std::int16_t scale = 0;
};
/// Helper structure that represents information about where and
/// how to get or put values when reading or writing bound parameter buffers.
struct ParamBindingInfo
: public BindingInfo
{
SQLSMALLINT io_type = SQL_PARAM_INPUT;
SQLSMALLINT sql_type = SQL_UNKNOWN_TYPE;
bool is_nullable = false;
};
/// Helper structure that represents different aspects of parameter info in a prepared query.
struct ParamInfo {
std::string name;
std::string tmp_placeholder;
};
struct BoundTypeInfo {
SQLSMALLINT c_type = SQL_C_DEFAULT;
SQLSMALLINT sql_type = SQL_UNKNOWN_TYPE;
SQLLEN value_max_size = 0;
std::int16_t precision = 0;
std::int16_t scale = 0;
bool is_nullable = false;
};
std::string convertCTypeToDataSourceType(const BoundTypeInfo & type_info);
std::string convertSQLTypeToDataSourceType(const BoundTypeInfo & type_info);
std::string convertSQLOrCTypeToDataSourceType(const BoundTypeInfo & type_info);
bool isMappedToStringDataSourceType(SQLSMALLINT sql_type, SQLSMALLINT c_type) noexcept;
// Directly write raw bytes to the buffer, respecting its size.
// All lengths are in bytes. If 'out_value_max_length == 0',
// assume 'out_value' is able to hold the entire 'in_value'.
// Throw exceptions on some detected errors, but tolerate right truncations.
template <typename LengthType1, typename LengthType2>
inline void fillOutputBufferInternal(
const void * in_value,
LengthType1 in_value_length,
void * out_value,
LengthType2 out_value_max_length
) {
if (in_value_length < 0 || (in_value_length > 0 && !in_value))
throw SqlException("Invalid string or buffer length", "HY090");
if (in_value_length > 0 && out_value) {
if (out_value_max_length < 0)
throw SqlException("Invalid string or buffer length", "HY090");
auto bytes_to_copy = in_value_length;
if (out_value_max_length >= 0 && out_value_max_length < bytes_to_copy)
bytes_to_copy = out_value_max_length;
std::memcpy(out_value, in_value, bytes_to_copy);
}
}
// Directly write raw bytes to the buffer.
// Throw on all errors, including right truncations.
template <typename LengthType1, typename LengthType2, typename LengthType3>
inline SQLRETURN fillOutputBuffer(
const void * in_value,
LengthType1 in_value_length,
void * out_value,
LengthType2 out_value_max_length,
LengthType3 * out_value_length
) {
fillOutputBufferInternal(
in_value,
in_value_length,
out_value,
out_value_max_length
);
if (out_value_length)
*out_value_length = in_value_length;
if (in_value_length > out_value_max_length)
throw SqlException("String data, right truncated", "01004", SQL_SUCCESS_WITH_INFO);
return SQL_SUCCESS;
}
// Change encoding, when appropriate, and write the result to the buffer.
// Extra string copy happens here for wide char strings, and strings that require encoding change.
template <typename CharType, typename LengthType1, typename LengthType2, typename ConversionContext>
inline SQLRETURN fillOutputString(
const std::string & in_value,
void * out_value,
LengthType1 out_value_max_length,
LengthType2 * out_value_length,
bool in_length_in_bytes,
bool out_length_in_bytes,
bool ensure_nts,
ConversionContext && context
) {
if (out_value) {
if (out_value_max_length < 0)
throw SqlException("Invalid string or buffer length", "HY090");
if (out_length_in_bytes && (out_value_max_length % sizeof(CharType)) != 0)
throw SqlException("Invalid string or buffer length", "HY090");
}
auto converted = fromUTF8<CharType>(in_value, context);
const auto converted_length_in_symbols = converted.size();
const auto converted_length_in_bytes = converted_length_in_symbols * sizeof(CharType);
const auto out_value_max_length_in_symbols = (out_length_in_bytes ? (out_value_max_length / sizeof(CharType)) : out_value_max_length);
const auto out_value_max_length_in_bytes = (out_length_in_bytes ? out_value_max_length : (out_value_max_length * sizeof(CharType)));
fillOutputBufferInternal(
converted.data(),
converted_length_in_bytes,
out_value,
out_value_max_length_in_bytes
);
context.string_pool.retireString(std::move(converted));
if (out_value_length) {
if (out_length_in_bytes)
*out_value_length = converted_length_in_bytes;
else
*out_value_length = converted_length_in_symbols;
}
if (ensure_nts && out_value) {
if (converted_length_in_symbols < out_value_max_length_in_symbols)
reinterpret_cast<CharType *>(out_value)[converted_length_in_symbols] = CharType{};
else if (out_value_max_length_in_symbols > 0)
reinterpret_cast<CharType *>(out_value)[out_value_max_length_in_symbols - 1] = CharType{};
}
if ((converted_length_in_symbols + 1) > out_value_max_length_in_symbols) // +1 for null terminating character
throw SqlException("String data, right truncated", "01004", SQL_SUCCESS_WITH_INFO);
return SQL_SUCCESS;
}
template <typename CharType, typename LengthType1, typename LengthType2, typename ConversionContext = DefaultConversionContext>
inline SQLRETURN fillOutputString(
const std::string & in_value,
void * out_value,
LengthType1 out_value_max_length,
LengthType2 * out_value_length,
bool length_in_bytes,
ConversionContext && context = ConversionContext{}
) {
return fillOutputString<CharType>(
in_value,
out_value,
out_value_max_length,
out_value_length,
length_in_bytes,
length_in_bytes,
true,
std::forward<ConversionContext>(context)
);
}
// If ObjectType is a pointer type then obj is treated as an integer corrsponding to the value of that pointer itself.
template <typename ObjectType, typename LengthType1, typename LengthType2>
inline SQLRETURN fillOutputPOD(
const ObjectType & obj,
void * out_value,
LengthType1 out_value_max_length,
LengthType2 * out_value_length
) {
return fillOutputBuffer(
&obj,
sizeof(obj),
out_value,
out_value_max_length,
out_value_length
);
}
template <typename ObjectType, typename LengthType1>
inline SQLRETURN fillOutputPOD(
const ObjectType & obj,
void * out_value,
LengthType1 * out_value_length
) {
return fillOutputPOD(
obj,
out_value,
sizeof(obj),
out_value_length
);
}
template <typename LengthType1, typename LengthType2>
inline SQLRETURN fillOutputNULL(
void * out_value,
LengthType1 out_value_max_length,
LengthType2 * out_value_length
) {
if (!out_value_length)
throw SqlException("Indicator variable required but not supplied", "22002");
*out_value_length = SQL_NULL_DATA;
return SQL_SUCCESS;
}
namespace value_manip {
template <typename T>
inline void to_null(T & obj) {
obj = T{};
}
inline void to_null(std::string & str) {
str.clear();
}
inline void to_null(SQL_NUMERIC_STRUCT & numeric) {
numeric.precision = 0;
numeric.scale = 0;
numeric.sign = 0;
std::fill(std::begin(numeric.val), std::end(numeric.val), 0);
}
inline void to_null(SQLGUID & guid) {
guid.Data1 = 0;
guid.Data2 = 0;
guid.Data3 = 0;
std::fill(std::begin(guid.Data4), std::end(guid.Data4), 0);
}
inline void to_null(SQL_DATE_STRUCT & date) {
date.year = 0;
date.month = 0;
date.day = 0;
}
inline void to_null(SQL_TIME_STRUCT & time) {
time.hour = 0;
time.minute = 0;
time.second = 0;
}
inline void to_null(SQL_TIMESTAMP_STRUCT & timestamp) {
timestamp.year = 0;
timestamp.month = 0;
timestamp.day = 0;
timestamp.hour = 0;
timestamp.minute = 0;
timestamp.second = 0;
timestamp.fraction = 0;
}
template <typename T>
inline void to_default(T & obj) {
return to_null(obj);
}
template <typename T>
static void normalize_date(T & date) {
if (date.year == 0)
date.year = 1970;
if (date.month == 0)
date.month = 1;
if (date.day == 0)
date.day = 1;
}
} // namespace value_manip
template <typename T>
struct SimpleTypeWrapper {
explicit SimpleTypeWrapper() {
value_manip::to_null(value);
}
template <typename U>
explicit SimpleTypeWrapper(U && val)
: value(std::forward<U>(val))
{
}
T value;
};
// Values stored exactly as they are written on wire in ODBCDriver2 format.
struct WireTypeAnyAsString
: public SimpleTypeWrapper<std::string>
{
using SimpleTypeWrapper<std::string>::SimpleTypeWrapper;
};
// Date stored exactly as it is represented on wire in RowBinaryWithNamesAndTypes format.
struct WireTypeDateAsInt
: public SimpleTypeWrapper<std::uint16_t>
{
using SimpleTypeWrapper<std::uint16_t>::SimpleTypeWrapper;
};
// DateTime stored exactly as it is represented on wire in RowBinaryWithNamesAndTypes format.
struct WireTypeDateTimeAsInt
: public SimpleTypeWrapper<std::uint32_t>
{
using SimpleTypeWrapper<std::uint32_t>::SimpleTypeWrapper;
};
template <DataSourceTypeId Id> struct DataSourceType; // Leave unimplemented for general case.
template <>
struct DataSourceType<DataSourceTypeId::Date>
: public SimpleTypeWrapper<SQL_DATE_STRUCT>
{
using SimpleTypeWrapper<SQL_DATE_STRUCT>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::DateTime>
: public SimpleTypeWrapper<SQL_TIMESTAMP_STRUCT>
{
using SimpleTypeWrapper<SQL_TIMESTAMP_STRUCT>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Decimal> {
// An integer type big enough to hold the integer value that is built from all
// decimal digits of Decimal/Numeric values, as if there is no decimal point.
// Size of this integer defines the upper bound of the "info" the internal
// representation can carry.
// TODO: switch to some 128-bit or even arbitrary-precision unsigned integer type.
using ContainerIntType = std::uint_fast64_t;
ContainerIntType value = 0;
std::int8_t sign = 0;
std::int16_t precision = 0;
std::int16_t scale = 0;
};
template <>
struct DataSourceType<DataSourceTypeId::Decimal32>
: public DataSourceType<DataSourceTypeId::Decimal>
{
};
template <>
struct DataSourceType<DataSourceTypeId::Decimal64>
: public DataSourceType<DataSourceTypeId::Decimal>
{
};
template <>
struct DataSourceType<DataSourceTypeId::Decimal128>
: public DataSourceType<DataSourceTypeId::Decimal>
{
};
template <>
struct DataSourceType<DataSourceTypeId::FixedString>
: public SimpleTypeWrapper<std::string>
{
using SimpleTypeWrapper<std::string>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Float32>
: public SimpleTypeWrapper<float>
{
using SimpleTypeWrapper<float>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Float64>
: public SimpleTypeWrapper<double>
{
using SimpleTypeWrapper<double>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Int8>
: public SimpleTypeWrapper<std::int8_t>
{
using SimpleTypeWrapper<std::int8_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Int16>
: public SimpleTypeWrapper<std::int16_t>
{
using SimpleTypeWrapper<std::int16_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Int32>
: public SimpleTypeWrapper<std::int32_t>
{
using SimpleTypeWrapper<std::int32_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Int64>
: public SimpleTypeWrapper<std::int64_t>
{
using SimpleTypeWrapper<std::int64_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::Nothing> {
};
template <>
struct DataSourceType<DataSourceTypeId::String>
: public SimpleTypeWrapper<std::string>
{
using SimpleTypeWrapper<std::string>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::UInt8>
: public SimpleTypeWrapper<std::uint8_t>
{
using SimpleTypeWrapper<std::uint8_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::UInt16>
: public SimpleTypeWrapper<std::uint16_t>
{
using SimpleTypeWrapper<std::uint16_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::UInt32>
: public SimpleTypeWrapper<std::uint32_t>
{
using SimpleTypeWrapper<std::uint32_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::UInt64>
: public SimpleTypeWrapper<std::uint64_t>
{
using SimpleTypeWrapper<std::uint64_t>::SimpleTypeWrapper;
};
template <>
struct DataSourceType<DataSourceTypeId::UUID>
: public SimpleTypeWrapper<SQLGUID>
{
using SimpleTypeWrapper<SQLGUID>::SimpleTypeWrapper;
};
template <class T> struct is_string_data_source_type
: public std::false_type
{
};
template <> struct is_string_data_source_type<DataSourceType<DataSourceTypeId::String>>
: public std::true_type
{
};
template <> struct is_string_data_source_type<DataSourceType<DataSourceTypeId::FixedString>>
: public std::true_type
{
};
template <> struct is_string_data_source_type<WireTypeAnyAsString>
: public std::true_type
{
};
template <class T> inline constexpr bool is_string_data_source_type_v = is_string_data_source_type<T>::value;
// Used to avoid duplicate specializations in platforms where 'std::int32_t' or 'std::int64_t' are typedef'd as 'long'.
struct long_if_not_typedefed {
struct dummy {};
using type = std::conditional_t<
(std::is_same_v<long, std::int32_t> || std::is_same_v<long, std::int64_t>),
dummy,
long
>;
};
// Used to avoid duplicate specializations in platforms where 'std::uint32_t' or 'std::uint64_t' are typedef'd as 'unsigned long'.
struct unsigned_long_if_not_typedefed {
struct dummy {};
using type = std::conditional_t<
(std::is_same_v<unsigned long, std::uint32_t> || std::is_same_v<unsigned long, std::uint64_t>),
dummy,
unsigned long
>;
};
namespace value_manip {
template <typename T>
inline std::int16_t getColumnSize(const T & obj, const TypeInfo & type_info) {
return type_info.column_size;
}
inline std::int16_t getColumnSize(const SQL_NUMERIC_STRUCT & numeric, const TypeInfo & type_info) {
return (numeric.precision == 0 ? type_info.column_size : numeric.precision);
}
// TODO: implement getColumnSize() for other types.
template <typename T>
inline std::int16_t getDecimalDigits(const T & obj, const TypeInfo & type_info) {
return 0;
}
inline std::int16_t getDecimalDigits(const SQL_NUMERIC_STRUCT & numeric, const TypeInfo & type_info) {
return numeric.scale;
}
// TODO: implement getDecimalDigits() for other types.
template <typename ProxyType, typename SourceType, typename DestinationType>
void convert_via_proxy(const SourceType & src, DestinationType & dest);
template <typename SourceType>
struct from_value {
template <typename DestinationType>
struct to_value {
static inline void convert(const SourceType & src, DestinationType & dest); // Leave unimplemented for general case.
};
};
template <>
struct from_value<std::string> {
using SourceType = std::string;
template <typename DestinationType>
struct to_value {
static inline void convert(const SourceType & src, DestinationType & dest) {
dest = fromString<DestinationType>(src);
}
static inline void convert(SourceType && src, DestinationType & dest) {
dest = fromString<DestinationType>(std::move(src));
}
};
};
template <>
struct from_value<std::string>::to_value<std::string> {
using DestinationType = std::string;
static inline void convert(const SourceType & src, DestinationType & dest) {
dest = src;
}
static inline void convert(SourceType && src, DestinationType & dest) {
dest = std::move(src);
}
};
template <>
struct from_value<std::string>::to_value<std::int64_t> {
using DestinationType = std::int64_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::size_t pos = 0;
try {
dest = std::stoll(src, &pos, 10);
}
catch (const std::exception & e) {
throw std::runtime_error("Cannot interpret '" + src + "' as signed 64-bit integer: " + e.what());
}
if (pos != src.size())
throw std::runtime_error("Cannot interpret '" + src + "' as signed 64-bit integer: string consumed partially");
}
};
template <>
struct from_value<std::string>::to_value<std::int32_t> {
using DestinationType = std::int32_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::int64_t tmp = 0;
to_value<std::int64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as signed 32-bit integer: value out of range");
dest = static_cast<std::int32_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<long_if_not_typedefed::type> {
using DestinationType = long;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::int64_t tmp = 0;
to_value<std::int64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as long integer: value out of range");
dest = static_cast<long>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<std::int16_t> {
using DestinationType = std::int16_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::int64_t tmp = 0;
to_value<std::int64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as signed 16-bit integer: value out of range");
dest = static_cast<std::int16_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<std::int8_t> {
using DestinationType = std::int8_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::int64_t tmp = 0;
to_value<std::int64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as signed 8-bit integer: value out of range");
dest = static_cast<std::int8_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<std::uint64_t> {
using DestinationType = std::uint64_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::size_t pos = 0;
try {
dest = std::stoull(src, &pos, 10);
}
catch (const std::exception & e) {
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned 64-bit integer: " + e.what());
}
if (pos != src.size())
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned 64-bit integer: string consumed partially");
}
};
template <>
struct from_value<std::string>::to_value<std::uint32_t> {
using DestinationType = std::uint32_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::uint64_t tmp = 0;
to_value<std::uint64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned 32-bit integer: value out of range");
dest = static_cast<std::uint32_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<unsigned_long_if_not_typedefed::type> {
using DestinationType = unsigned long;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::uint64_t tmp = 0;
to_value<std::uint64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned long integer: value out of range");
dest = static_cast<unsigned long>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<std::uint16_t> {
using DestinationType = std::uint16_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::uint64_t tmp = 0;
to_value<std::uint64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned 16-bit integer: value out of range");
dest = static_cast<std::uint16_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<std::uint8_t> {
using DestinationType = std::uint8_t;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::uint64_t tmp = 0;
to_value<std::uint64_t>::convert(src, tmp);
if (std::numeric_limits<DestinationType>::max() < tmp || tmp < std::numeric_limits<DestinationType>::min())
throw std::runtime_error("Cannot interpret '" + src + "' as unsigned 8-bit integer: value out of range");
dest = static_cast<std::uint8_t>(tmp);
}
};
template <>
struct from_value<std::string>::to_value<float> {
using DestinationType = float;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::size_t pos = 0;
try {
dest = std::stof(src, &pos);
}
catch (const std::exception & e) {
throw std::runtime_error("Cannot interpret '" + src + "' as float: " + e.what());
}
if (pos != src.size())
throw std::runtime_error("Cannot interpret '" + src + "' as float: string consumed partially");
}
};
template <>
struct from_value<std::string>::to_value<double> {
using DestinationType = double;
static inline void convert(const SourceType & src, DestinationType & dest) {
std::size_t pos = 0;
try {
dest = std::stod(src, &pos);
}
catch (const std::exception & e) {
throw std::runtime_error("Cannot interpret '" + src + "' as double: " + e.what());
}
if (pos != src.size())
throw std::runtime_error("Cannot interpret '" + src + "' as double: string consumed partially");
}
};
template <>
struct from_value<std::string>::to_value<SQLGUID> {
using DestinationType = SQLGUID;
static inline void convert(const SourceType & src, DestinationType & dest) {
unsigned int Data1 = 0;
unsigned int Data2 = 0;
unsigned int Data3 = 0;
unsigned int Data4[8] = { 0 };
char guard = '\0';
const auto read = std::sscanf(src.c_str(), "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x%c",
&Data1, &Data2, &Data3,
&Data4[0], &Data4[1], &Data4[2], &Data4[3],
&Data4[4], &Data4[5], &Data4[6], &Data4[7],
&guard
);
if (read != 11) // All 'DataN' must be successfully read, but not the 'guard'.
throw std::runtime_error("Cannot interpret '" + src + "' as GUID");
dest.Data1 = Data1;
dest.Data2 = Data2;
dest.Data3 = Data3;
std::copy(std::begin(Data4), std::end(Data4), std::begin(dest.Data4));
}
};
template <>
struct from_value<std::string>::to_value<SQL_NUMERIC_STRUCT> {
using DestinationType = SQL_NUMERIC_STRUCT;
static inline void convert(const SourceType & src, DestinationType & dest) {
convert_via_proxy<DataSourceType<DataSourceTypeId::Decimal>>(src, dest);
}
};
template <>
struct from_value<std::string>::to_value<SQL_DATE_STRUCT> {
using DestinationType = SQL_DATE_STRUCT;
static inline void convert(const SourceType & src, DestinationType & dest) {
if (src.size() != 10)
throw std::runtime_error("Cannot interpret '" + src + "' as Date");
dest.year = (src[0] - '0') * 1000 + (src[1] - '0') * 100 + (src[2] - '0') * 10 + (src[3] - '0');
dest.month = (src[5] - '0') * 10 + (src[6] - '0');
dest.day = (src[8] - '0') * 10 + (src[9] - '0');
normalize_date(dest);
}
};
template <>
struct from_value<std::string>::to_value<SQL_TIME_STRUCT> {
using DestinationType = SQL_TIME_STRUCT;
static inline void convert(const SourceType & src, DestinationType & dest) {
if constexpr (std::is_same_v<SourceType, DestinationType>) {
std::memcpy(&dest, &src, sizeof(dest));
}
else {
throw std::runtime_error("conversion not supported");
// TODO: implement?
}
}
};
template <>
struct from_value<std::string>::to_value<SQL_TIMESTAMP_STRUCT> {
using DestinationType = SQL_TIMESTAMP_STRUCT;
static inline void convert(const SourceType & src, DestinationType & dest) {
if (src.size() == 10) {
dest.year = (src[0] - '0') * 1000 + (src[1] - '0') * 100 + (src[2] - '0') * 10 + (src[3] - '0');
dest.month = (src[5] - '0') * 10 + (src[6] - '0');
dest.day = (src[8] - '0') * 10 + (src[9] - '0');
dest.hour = 0;
dest.minute = 0;
dest.second = 0;
dest.fraction = 0;
}
else if (src.size() == 19) {
dest.year = (src[0] - '0') * 1000 + (src[1] - '0') * 100 + (src[2] - '0') * 10 + (src[3] - '0');
dest.month = (src[5] - '0') * 10 + (src[6] - '0');
dest.day = (src[8] - '0') * 10 + (src[9] - '0');
dest.hour = (src[11] - '0') * 10 + (src[12] - '0');
dest.minute = (src[14] - '0') * 10 + (src[15] - '0');
dest.second = (src[17] - '0') * 10 + (src[18] - '0');
dest.fraction = 0;
}
else
throw std::runtime_error("Cannot interpret '" + src + "' as DateTime");
normalize_date(dest);
}
};
template <DataSourceTypeId Id>
struct from_value<std::string>::to_value<DataSourceType<Id>> {
using DestinationType = DataSourceType<Id>;
static inline void convert(const SourceType & src, DestinationType & dest) {
return from_value<SourceType>::template to_value<decltype(dest.value)>::convert(src, dest.value);
}
static inline void convert(SourceType && src, DestinationType & dest) {
return from_value<SourceType>::template to_value<decltype(dest.value)>::convert(std::move(src), dest.value);
}
};
template <>
struct from_value<std::string>::to_value<DataSourceType<DataSourceTypeId::Decimal>> {
using DestinationType = DataSourceType<DataSourceTypeId::Decimal>;
static inline void convert(const SourceType & src, DestinationType & dest) {
constexpr auto dest_value_max = (std::numeric_limits<std::decay_t<decltype(dest.value)>>::max)();
constexpr std::uint32_t dec_mult = 10;
std::size_t left_n = 0;
std::size_t right_n = 0;
bool sign_met = false;
bool dot_met = false;
bool dig_met = false;
dest.value = 0;
dest.sign = 1;
for (auto ch : src) {
switch (ch) {
case '+':
case '-': {