OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DBETypes.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "DBETypes.h"
18 
19 namespace {
20 void checkColumnRange(size_t index, size_t count) {
21  if (index >= count) {
22  throw std::out_of_range("Column index " + std::to_string(index) +
23  " is out of range: 0.." + std::to_string(count - 1));
24  }
25 }
26 
27 template <typename T>
29  ScalarTargetValue* scalar_value = boost::get<ScalarTargetValue>(field);
30  if (!scalar_value) {
31  throw std::runtime_error("Unsupported field type");
32  }
33  T* result_ptr = boost::get<T>(scalar_value);
34  if (!result_ptr) {
35  throw std::runtime_error("Null field");
36  }
37  result = *result_ptr;
38 }
39 } // namespace
40 
41 namespace EmbeddedDatabase {
42 
46  : col_type(ColumnType::UNKNOWN)
47  , encoding(ColumnEncoding::NONE)
48  , nullable(false)
49  , is_array(false)
50  , precision(0)
51  , scale(0)
52  , comp_param(0) {}
53 
54 ColumnDetails::ColumnDetails(const std::string& _col_name,
55  ColumnType _col_type,
56  ColumnEncoding _encoding,
57  bool _nullable,
58  bool _is_array,
59  int _precision,
60  int _scale,
61  int _comp_param)
62  : col_name(_col_name)
63  , col_type(_col_type)
64  , encoding(_encoding)
65  , nullable(_nullable)
66  , is_array(_is_array)
67  , precision(_precision)
68  , scale(_scale)
69  , comp_param(_comp_param) {}
70 
74 
75 Row::Row(std::vector<TargetValue>& row) : row_(std::move(row)) {}
76 
77 int64_t Row::getInt(size_t col_num) {
78  int64_t value;
79  checkColumnRange(col_num, row_.size());
80  getFieldValue(value, &row_[col_num]);
81  return value;
82 }
83 
84 float Row::getFloat(size_t col_num) {
85  float value;
86  checkColumnRange(col_num, row_.size());
87  getFieldValue(value, &row_[col_num]);
88  return value;
89 }
90 
91 double Row::getDouble(size_t col_num) {
92  double value;
93  checkColumnRange(col_num, row_.size());
94  getFieldValue(value, &row_[col_num]);
95  return value;
96 }
97 
98 std::string Row::getStr(size_t col_num) {
99  checkColumnRange(col_num, row_.size());
100  const auto scalar_value = boost::get<ScalarTargetValue>(&row_[col_num]);
101  auto value = boost::get<NullableString>(scalar_value);
102  if (!value || boost::get<void*>(value)) {
103  return "";
104  }
105  if (auto str = boost::get<std::string>(value)) {
106  return *str;
107  }
108  return "";
109 }
110 
112  switch (type) {
113  case kBOOLEAN:
114  return ColumnType::BOOL;
115  case kTINYINT:
116  return ColumnType::TINYINT;
117  case kSMALLINT:
118  return ColumnType::SMALLINT;
119  case kINT:
120  return ColumnType::INT;
121  case kBIGINT:
122  return ColumnType::BIGINT;
123  case kFLOAT:
124  return ColumnType::FLOAT;
125  case kNUMERIC:
126  case kDECIMAL:
127  return ColumnType::DECIMAL;
128  case kDOUBLE:
129  return ColumnType::DOUBLE;
130  case kTEXT:
131  case kVARCHAR:
132  case kCHAR:
133  return ColumnType::STR;
134  case kTIME:
135  return ColumnType::TIME;
136  case kTIMESTAMP:
137  return ColumnType::TIMESTAMP;
138  case kDATE:
139  return ColumnType::DATE;
140  case kINTERVAL_DAY_TIME:
144  case kPOINT:
145  return ColumnType::POINT;
146  case kLINESTRING:
147  return ColumnType::LINESTRING;
148  case kPOLYGON:
149  return ColumnType::POLYGON;
150  case kMULTIPOLYGON:
152  case kGEOMETRY:
153  return ColumnType::GEOMETRY;
154  case kGEOGRAPHY:
155  return ColumnType::GEOGRAPHY;
156  default:
157  return ColumnType::UNKNOWN;
158  }
159  return ColumnType::UNKNOWN;
160 }
161 
163  switch (type) {
164  case kENCODING_NONE:
165  return ColumnEncoding::NONE;
166  case kENCODING_FIXED:
167  return ColumnEncoding::FIXED;
168  case kENCODING_RL:
169  return ColumnEncoding::RL;
170  case kENCODING_DIFF:
171  return ColumnEncoding::DIFF;
172  case kENCODING_DICT:
173  return ColumnEncoding::DICT;
174  case kENCODING_SPARSE:
175  return ColumnEncoding::SPARSE;
176  case kENCODING_GEOINT:
177  return ColumnEncoding::GEOINT;
180  default:
181  return ColumnEncoding::NONE;
182  }
183  return ColumnEncoding::NONE;
184 }
185 } // namespace EmbeddedDatabase
std::string getStr(size_t col_num)
Definition: DBETypes.cpp:98
double getDouble(size_t col_num)
Definition: DBETypes.cpp:91
Definition: sqltypes.h:76
SQLTypes
Definition: sqltypes.h:65
void checkColumnRange(size_t index, size_t count)
Definition: DBETypes.cpp:20
std::string to_string(char const *&&v)
float getFloat(size_t col_num)
Definition: DBETypes.cpp:84
int64_t getInt(size_t col_num)
Definition: DBETypes.cpp:77
ColumnType sqlToColumnType(const SQLTypes &type)
Definition: DBETypes.cpp:111
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
EncodingType
Definition: sqltypes.h:240
void getFieldValue(T &result, TargetValue *field)
Definition: DBETypes.cpp:28
Definition: sqltypes.h:79
Definition: sqltypes.h:80
std::vector< TargetValue > row_
Definition: DBETypes.h:92
ColumnEncoding sqlToColumnEncoding(const EncodingType &type)
Definition: DBETypes.cpp:162
Definition: sqltypes.h:68
bool g_enable_watchdog false
Definition: Execute.cpp:80
boost::variant< ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr > TargetValue
Definition: TargetValue.h:195
Definition: sqltypes.h:72
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:180