1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/main/capi/cast/generic_cast.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/types/time.hpp" |
12 | #include "duckdb/common/types/timestamp.hpp" |
13 | #include "duckdb/common/types/date.hpp" |
14 | |
15 | #include "duckdb/main/capi/capi_internal.hpp" |
16 | #include "duckdb/main/capi/cast/utils.hpp" |
17 | #include "duckdb/main/capi/cast/from_decimal.hpp" |
18 | |
19 | namespace duckdb { |
20 | |
21 | template <class RESULT_TYPE, class OP = duckdb::TryCast> |
22 | RESULT_TYPE GetInternalCValue(duckdb_result *result, idx_t col, idx_t row) { |
23 | if (!CanFetchValue(result, col, row)) { |
24 | return FetchDefaultValue::Operation<RESULT_TYPE>(); |
25 | } |
26 | switch (result->__deprecated_columns[col].__deprecated_type) { |
27 | case DUCKDB_TYPE_BOOLEAN: |
28 | return TryCastCInternal<bool, RESULT_TYPE, OP>(result, col, row); |
29 | case DUCKDB_TYPE_TINYINT: |
30 | return TryCastCInternal<int8_t, RESULT_TYPE, OP>(result, col, row); |
31 | case DUCKDB_TYPE_SMALLINT: |
32 | return TryCastCInternal<int16_t, RESULT_TYPE, OP>(result, col, row); |
33 | case DUCKDB_TYPE_INTEGER: |
34 | return TryCastCInternal<int32_t, RESULT_TYPE, OP>(result, col, row); |
35 | case DUCKDB_TYPE_BIGINT: |
36 | return TryCastCInternal<int64_t, RESULT_TYPE, OP>(result, col, row); |
37 | case DUCKDB_TYPE_UTINYINT: |
38 | return TryCastCInternal<uint8_t, RESULT_TYPE, OP>(result, col, row); |
39 | case DUCKDB_TYPE_USMALLINT: |
40 | return TryCastCInternal<uint16_t, RESULT_TYPE, OP>(result, col, row); |
41 | case DUCKDB_TYPE_UINTEGER: |
42 | return TryCastCInternal<uint32_t, RESULT_TYPE, OP>(result, col, row); |
43 | case DUCKDB_TYPE_UBIGINT: |
44 | return TryCastCInternal<uint64_t, RESULT_TYPE, OP>(result, col, row); |
45 | case DUCKDB_TYPE_FLOAT: |
46 | return TryCastCInternal<float, RESULT_TYPE, OP>(result, col, row); |
47 | case DUCKDB_TYPE_DOUBLE: |
48 | return TryCastCInternal<double, RESULT_TYPE, OP>(result, col, row); |
49 | case DUCKDB_TYPE_DATE: |
50 | return TryCastCInternal<date_t, RESULT_TYPE, OP>(result, col, row); |
51 | case DUCKDB_TYPE_TIME: |
52 | return TryCastCInternal<dtime_t, RESULT_TYPE, OP>(result, col, row); |
53 | case DUCKDB_TYPE_TIMESTAMP: |
54 | return TryCastCInternal<timestamp_t, RESULT_TYPE, OP>(result, col, row); |
55 | case DUCKDB_TYPE_HUGEINT: |
56 | return TryCastCInternal<hugeint_t, RESULT_TYPE, OP>(result, col, row); |
57 | case DUCKDB_TYPE_DECIMAL: |
58 | return TryCastDecimalCInternal<RESULT_TYPE>(result, col, row); |
59 | case DUCKDB_TYPE_INTERVAL: |
60 | return TryCastCInternal<interval_t, RESULT_TYPE, OP>(result, col, row); |
61 | case DUCKDB_TYPE_VARCHAR: |
62 | return TryCastCInternal<char *, RESULT_TYPE, FromCStringCastWrapper<OP>>(result, col, row); |
63 | case DUCKDB_TYPE_BLOB: |
64 | return TryCastCInternal<duckdb_blob, RESULT_TYPE, FromCBlobCastWrapper>(result, col, row); |
65 | default: { // LCOV_EXCL_START |
66 | // invalid type for C to C++ conversion |
67 | D_ASSERT(0); |
68 | return FetchDefaultValue::Operation<RESULT_TYPE>(); |
69 | } // LCOV_EXCL_STOP |
70 | } |
71 | } |
72 | |
73 | } // namespace duckdb |
74 | |