1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/main/capi/capi_cast_from_decimal.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/main/capi/cast/utils.hpp"
12
13namespace duckdb {
14
15//! DECIMAL -> ?
16template <class RESULT_TYPE>
17bool CastDecimalCInternal(duckdb_result *source, RESULT_TYPE &result, idx_t col, idx_t row) {
18 auto result_data = (duckdb::DuckDBResultData *)source->internal_data;
19 auto &query_result = result_data->result;
20 auto &source_type = query_result->types[col];
21 auto width = duckdb::DecimalType::GetWidth(type: source_type);
22 auto scale = duckdb::DecimalType::GetScale(type: source_type);
23 void *source_address = UnsafeFetchPtr<hugeint_t>(result: source, col, row);
24 switch (source_type.InternalType()) {
25 case duckdb::PhysicalType::INT16:
26 return duckdb::TryCastFromDecimal::Operation<int16_t, RESULT_TYPE>(UnsafeFetchFromPtr<int16_t>(pointer: source_address),
27 result, nullptr, width, scale);
28 case duckdb::PhysicalType::INT32:
29 return duckdb::TryCastFromDecimal::Operation<int32_t, RESULT_TYPE>(UnsafeFetchFromPtr<int32_t>(pointer: source_address),
30 result, nullptr, width, scale);
31 case duckdb::PhysicalType::INT64:
32 return duckdb::TryCastFromDecimal::Operation<int64_t, RESULT_TYPE>(UnsafeFetchFromPtr<int64_t>(pointer: source_address),
33 result, nullptr, width, scale);
34 case duckdb::PhysicalType::INT128:
35 return duckdb::TryCastFromDecimal::Operation<hugeint_t, RESULT_TYPE>(
36 UnsafeFetchFromPtr<hugeint_t>(pointer: source_address), result, nullptr, width, scale);
37 default:
38 throw duckdb::InternalException("Unimplemented internal type for decimal");
39 }
40}
41
42//! DECIMAL -> VARCHAR
43template <>
44bool CastDecimalCInternal(duckdb_result *source, duckdb_string &result, idx_t col, idx_t row);
45
46//! DECIMAL -> DECIMAL (internal fetch)
47template <>
48bool CastDecimalCInternal(duckdb_result *source, duckdb_decimal &result, idx_t col, idx_t row);
49
50//! DECIMAL -> ...
51template <class RESULT_TYPE>
52RESULT_TYPE TryCastDecimalCInternal(duckdb_result *source, idx_t col, idx_t row) {
53 RESULT_TYPE result_value;
54 try {
55 if (!CastDecimalCInternal<RESULT_TYPE>(source, result_value, col, row)) {
56 return FetchDefaultValue::Operation<RESULT_TYPE>();
57 }
58 } catch (...) {
59 return FetchDefaultValue::Operation<RESULT_TYPE>();
60 }
61 return result_value;
62}
63
64} // namespace duckdb
65