Debian Codegraph @

ogr/ogrsf_frmts/hana/ogrhanafeaturewriter.h

raw
1 /******************************************************************************
2 *
3 * Project: SAP HANA Spatial Driver
4 * Purpose: OGRHanaFeatureWriter class declaration
5 * Author: Maxim Rylov
6 *
7 ******************************************************************************
8 * Copyright (c) 2020, SAP SE
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #ifndef OGRHANAFEATUREWRITER_H_INCLUDED
14 #define OGRHANAFEATUREWRITER_H_INCLUDED
15
16 #include "gdal_priv.h"
17 #include "odbc/Types.h"
18
19 namespace OGRHANA
20 {
21
22 class OGRHanaFeatureWriter
23 {
24 public:
25 explicit OGRHanaFeatureWriter(OGRFeature &feature);
26
27 template <typename T>
28 void SetFieldValue(int fieldIndex, const odbc::Nullable<T> &value);
29
30 void SetFieldValue(int fieldIndex, const odbc::Long &value);
31 void SetFieldValue(int fieldIndex, const odbc::Float &value);
32 void SetFieldValue(int fieldIndex, const odbc::Decimal &value);
33 void SetFieldValue(int fieldIndex, const odbc::String &value);
34 void SetFieldValue(int fieldIndex, const odbc::Date &value);
35 void SetFieldValue(int fieldIndex, const odbc::Time &value);
36 void SetFieldValue(int fieldIndex, const odbc::Timestamp &value);
37 void SetFieldValue(int fieldIndex, const odbc::Binary &value);
38 void SetFieldValue(int fieldIndex, const char *value);
39 void SetFieldValue(int fieldIndex, const void *value, std::size_t size);
40
41 template <typename InputT, typename ResultT>
42 void SetFieldValueAsArray(int fieldIndex, const odbc::Binary &value);
43 void SetFieldValueAsStringArray(int fieldIndex, const odbc::Binary &value);
44
45 private:
46 OGRFeature &feature_;
47 };
48
49 template <typename T>
50 void OGRHanaFeatureWriter::SetFieldValue(int fieldIndex,
51 const odbc::Nullable<T> &value)
52 {
53 if (value.isNull())
54 feature_.SetFieldNull(fieldIndex);
55 else
56 feature_.SetField(fieldIndex, *value);
57 }
58
59 template <typename InputT, typename ResultT>
60 void OGRHanaFeatureWriter::SetFieldValueAsArray(int fieldIndex,
61 const odbc::Binary &value)
62 {
63 if (value.isNull() || value->size() == 0)
64 {
65 feature_.SetFieldNull(fieldIndex);
66 return;
67 }
68
69 const uint8_t *ptr = reinterpret_cast<const uint8_t *>(value->data());
70 const uint32_t numElements = *reinterpret_cast<const uint32_t *>(ptr);
71 ptr += sizeof(uint32_t);
72
73 std::vector<ResultT> values;
74 values.reserve(numElements);
75
76 bool elemHasLength =
77 numElements * sizeof(InputT) != (value->size() - sizeof(uint32_t));
78
79 for (uint32_t i = 0; i < numElements; ++i)
80 {
81 if (elemHasLength)
82 {
83 uint8_t len = *ptr;
84 ++ptr;
85
86 if (len > 0)
87 values.push_back(*reinterpret_cast<const InputT *>(ptr));
88 else
89 values.push_back(ResultT());
90 }
91 else
92 values.push_back(*reinterpret_cast<const InputT *>(ptr));
93
94 ptr += sizeof(InputT);
95 }
96
97 feature_.SetField(fieldIndex, static_cast<int32_t>(values.size()),
98 values.data());
99 }
100
101 } // namespace OGRHANA
102
103 #endif // OGRHANAFEATUREWRITER_H_INCLUDED