| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: SAP HANA Spatial Driver |
| 4 |
* Purpose: OGRHanaFeatureReader class implementation |
| 5 |
* Author: Maxim Rylov |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2020, SAP SE |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: MIT |
| 11 |
****************************************************************************/ |
| 12 |
|
| 13 |
#include "ogrhanafeaturereader.h" |
| 14 |
#include "ogrhanautils.h" |
| 15 |
|
| 16 |
#include "cpl_time.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <cmath> |
| 20 |
#include <cstring> |
| 21 |
#include <ctime> |
| 22 |
#include <limits> |
| 23 |
#include <sstream> |
| 24 |
|
| 25 |
#include "odbc/Types.h" |
| 26 |
|
| 27 |
namespace OGRHANA |
| 28 |
{ |
| 29 |
namespace |
| 30 |
{ |
| 31 |
|
| 32 |
template <typename T> |
| 33 |
odbc::String CreateStringFromValues(const T *elements, int numElements, |
| 34 |
std::string (*toString)(T e)) |
| 35 |
{ |
| 36 |
if (numElements == 0) |
| 37 |
return odbc::String(); |
| 38 |
|
| 39 |
std::ostringstream os; |
| 40 |
for (int i = 0; i < numElements; ++i) |
| 41 |
{ |
| 42 |
if (i > 0) |
| 43 |
os << ARRAY_VALUES_DELIMITER; |
| 44 |
os << toString(elements[i]); |
| 45 |
} |
| 46 |
return odbc::String(os.str()); |
| 47 |
} |
| 48 |
|
| 49 |
template <typename T> T castInt(int value) |
| 50 |
{ |
| 51 |
if (value < std::numeric_limits<T>::min() || |
| 52 |
value > std::numeric_limits<T>::max()) |
| 53 |
throw std::overflow_error("Integer value lies outside of the range"); |
| 54 |
return static_cast<T>(value); |
| 55 |
} |
| 56 |
|
| 57 |
// Specialization to make Coverity Scan happy |
| 58 |
template <> int castInt(int value) |
| 59 |
{ |
| 60 |
return value; |
| 61 |
} |
| 62 |
|
| 63 |
template <typename T> T strToInt(const char *value) |
| 64 |
{ |
| 65 |
return castInt<T>(std::stoi(value)); |
| 66 |
} |
| 67 |
|
| 68 |
} // anonymous namespace |
| 69 |
|
| 70 |
OGRHanaFeatureReader::OGRHanaFeatureReader(const OGRFeature &feature) |
| 71 |
: feature_(feature) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
odbc::Boolean OGRHanaFeatureReader::GetFieldAsBoolean(int fieldIndex) const |
| 76 |
{ |
| 77 |
if (IsFieldSet(fieldIndex)) |
| 78 |
return feature_.GetFieldAsInteger(fieldIndex) == 1; |
| 79 |
|
| 80 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 81 |
if (defaultValue == nullptr) |
| 82 |
return odbc::Boolean(); |
| 83 |
|
| 84 |
return (EQUAL(defaultValue, "1") || EQUAL(defaultValue, "'t'")); |
| 85 |
} |
| 86 |
|
| 87 |
odbc::Byte OGRHanaFeatureReader::GetFieldAsByte(int fieldIndex) const |
| 88 |
{ |
| 89 |
if (IsFieldSet(fieldIndex)) |
| 90 |
return odbc::Byte( |
| 91 |
castInt<std::int8_t>(feature_.GetFieldAsInteger(fieldIndex))); |
| 92 |
|
| 93 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 94 |
if (defaultValue == nullptr) |
| 95 |
return odbc::Byte(); |
| 96 |
return odbc::Byte(strToInt<std::int8_t>(defaultValue)); |
| 97 |
} |
| 98 |
|
| 99 |
odbc::Short OGRHanaFeatureReader::GetFieldAsShort(int fieldIndex) const |
| 100 |
{ |
| 101 |
if (IsFieldSet(fieldIndex)) |
| 102 |
return odbc::Short( |
| 103 |
castInt<std::int16_t>(feature_.GetFieldAsInteger(fieldIndex))); |
| 104 |
|
| 105 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 106 |
if (defaultValue == nullptr) |
| 107 |
return odbc::Short(); |
| 108 |
return odbc::Short(strToInt<std::int16_t>(defaultValue)); |
| 109 |
} |
| 110 |
|
| 111 |
odbc::Int OGRHanaFeatureReader::GetFieldAsInt(int fieldIndex) const |
| 112 |
{ |
| 113 |
if (IsFieldSet(fieldIndex)) |
| 114 |
return odbc::Int(feature_.GetFieldAsInteger(fieldIndex)); |
| 115 |
|
| 116 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 117 |
if (defaultValue == nullptr) |
| 118 |
return odbc::Int(); |
| 119 |
return odbc::Int(strToInt<int>(defaultValue)); |
| 120 |
} |
| 121 |
|
| 122 |
odbc::Long OGRHanaFeatureReader::GetFieldAsLong(int fieldIndex) const |
| 123 |
{ |
| 124 |
if (IsFieldSet(fieldIndex)) |
| 125 |
return odbc::Long(feature_.GetFieldAsInteger64(fieldIndex)); |
| 126 |
|
| 127 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 128 |
if (defaultValue == nullptr) |
| 129 |
return odbc::Long(); |
| 130 |
return odbc::Long(std::stol(defaultValue)); |
| 131 |
} |
| 132 |
|
| 133 |
odbc::Float OGRHanaFeatureReader::GetFieldAsFloat(int fieldIndex) const |
| 134 |
{ |
| 135 |
if (IsFieldSet(fieldIndex)) |
| 136 |
{ |
| 137 |
double dValue = feature_.GetFieldAsDouble(fieldIndex); |
| 138 |
return odbc::Float(static_cast<float>(dValue)); |
| 139 |
} |
| 140 |
|
| 141 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 142 |
if (defaultValue == nullptr) |
| 143 |
return odbc::Float(); |
| 144 |
return odbc::Float(std::stof(defaultValue)); |
| 145 |
} |
| 146 |
|
| 147 |
odbc::Double OGRHanaFeatureReader::GetFieldAsDouble(int fieldIndex) const |
| 148 |
{ |
| 149 |
if (IsFieldSet(fieldIndex)) |
| 150 |
return odbc::Double(feature_.GetFieldAsDouble(fieldIndex)); |
| 151 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 152 |
if (defaultValue == nullptr) |
| 153 |
return odbc::Double(); |
| 154 |
return odbc::Double(std::stod(defaultValue)); |
| 155 |
} |
| 156 |
|
| 157 |
odbc::String OGRHanaFeatureReader::GetFieldAsString(int fieldIndex, |
| 158 |
int maxCharLength) const |
| 159 |
{ |
| 160 |
auto getString = [&](const char *str) |
| 161 |
{ |
| 162 |
if (str == nullptr) |
| 163 |
return odbc::String(); |
| 164 |
|
| 165 |
if (maxCharLength > 0 && |
| 166 |
std::strlen(str) > static_cast<std::size_t>(maxCharLength)) |
| 167 |
return odbc::String( |
| 168 |
std::string(str, static_cast<std::size_t>(maxCharLength))); |
| 169 |
return odbc::String(str); |
| 170 |
}; |
| 171 |
|
| 172 |
if (IsFieldSet(fieldIndex)) |
| 173 |
return getString(feature_.GetFieldAsString(fieldIndex)); |
| 174 |
|
| 175 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 176 |
if (defaultValue == nullptr) |
| 177 |
return odbc::String(); |
| 178 |
|
| 179 |
if (defaultValue[0] == '\'' && |
| 180 |
defaultValue[strlen(defaultValue) - 1] == '\'') |
| 181 |
{ |
| 182 |
CPLString str(defaultValue + 1); |
| 183 |
str.pop_back(); |
| 184 |
char *tmp = CPLUnescapeString(str, nullptr, CPLES_SQL); |
| 185 |
odbc::String ret = getString(tmp); |
| 186 |
CPLFree(tmp); |
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
return odbc::String(defaultValue); |
| 191 |
} |
| 192 |
|
| 193 |
odbc::String OGRHanaFeatureReader::GetFieldAsNString(int fieldIndex, |
| 194 |
int maxCharLength) const |
| 195 |
{ |
| 196 |
auto getString = [&](const char *str) |
| 197 |
{ |
| 198 |
if (str == nullptr) |
| 199 |
return odbc::String(); |
| 200 |
|
| 201 |
if (maxCharLength <= 0) |
| 202 |
return odbc::String(std::string(str)); |
| 203 |
|
| 204 |
size_t nSrcLen = std::strlen(str); |
| 205 |
const size_t nSrcLenUTF = CPLStrlenUTF8Ex(str); |
| 206 |
|
| 207 |
if (nSrcLenUTF > static_cast<size_t>(maxCharLength)) |
| 208 |
{ |
| 209 |
CPLDebug("HANA", |
| 210 |
"Truncated field value '%s' at index %d to %d characters.", |
| 211 |
str, fieldIndex, maxCharLength); |
| 212 |
|
| 213 |
size_t iUTF8Char = 0; |
| 214 |
for (size_t iChar = 0; iChar < nSrcLen; ++iChar) |
| 215 |
{ |
| 216 |
if ((str[iChar] & 0xc0) != 0x80) |
| 217 |
{ |
| 218 |
if (iUTF8Char == static_cast<size_t>(maxCharLength)) |
| 219 |
{ |
| 220 |
nSrcLen = iChar; |
| 221 |
break; |
| 222 |
} |
| 223 |
++iUTF8Char; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return odbc::String(std::string(str, nSrcLen)); |
| 229 |
}; |
| 230 |
|
| 231 |
if (IsFieldSet(fieldIndex)) |
| 232 |
return getString(feature_.GetFieldAsString(fieldIndex)); |
| 233 |
|
| 234 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 235 |
if (defaultValue == nullptr) |
| 236 |
return odbc::String(); |
| 237 |
|
| 238 |
if (defaultValue[0] == '\'' && |
| 239 |
defaultValue[strlen(defaultValue) - 1] == '\'') |
| 240 |
{ |
| 241 |
CPLString str(defaultValue + 1); |
| 242 |
str.pop_back(); |
| 243 |
char *tmp = CPLUnescapeString(str, nullptr, CPLES_SQL); |
| 244 |
odbc::String ret = getString(tmp); |
| 245 |
CPLFree(tmp); |
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
return odbc::String(defaultValue); |
| 250 |
} |
| 251 |
|
| 252 |
odbc::Date OGRHanaFeatureReader::GetFieldAsDate(int fieldIndex) const |
| 253 |
{ |
| 254 |
if (IsFieldSet(fieldIndex)) |
| 255 |
{ |
| 256 |
int year = 0; |
| 257 |
int month = 0; |
| 258 |
int day = 0; |
| 259 |
int hour = 0; |
| 260 |
int minute = 0; |
| 261 |
int timeZoneFlag = 0; |
| 262 |
float second = 0.0f; |
| 263 |
feature_.GetFieldAsDateTime(fieldIndex, &year, &month, &day, &hour, |
| 264 |
&minute, &second, &timeZoneFlag); |
| 265 |
|
| 266 |
return odbc::makeNullable<odbc::date>(year, month, day); |
| 267 |
} |
| 268 |
|
| 269 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 270 |
if (defaultValue == nullptr) |
| 271 |
return odbc::Date(); |
| 272 |
|
| 273 |
if (EQUAL(defaultValue, "CURRENT_DATE")) |
| 274 |
{ |
| 275 |
std::time_t t = std::time(nullptr); |
| 276 |
tm *now = std::localtime(&t); |
| 277 |
if (now == nullptr) |
| 278 |
return odbc::Date(); |
| 279 |
return odbc::makeNullable<odbc::date>(now->tm_year + 1900, |
| 280 |
now->tm_mon + 1, now->tm_mday); |
| 281 |
} |
| 282 |
|
| 283 |
int year, month, day; |
| 284 |
sscanf(defaultValue, "'%04d/%02d/%02d'", &year, &month, &day); |
| 285 |
|
| 286 |
return odbc::makeNullable<odbc::date>(year, month, day); |
| 287 |
} |
| 288 |
|
| 289 |
odbc::Time OGRHanaFeatureReader::GetFieldAsTime(int fieldIndex) const |
| 290 |
{ |
| 291 |
if (IsFieldSet(fieldIndex)) |
| 292 |
{ |
| 293 |
int year = 0; |
| 294 |
int month = 0; |
| 295 |
int day = 0; |
| 296 |
int hour = 0; |
| 297 |
int minute = 0; |
| 298 |
int timeZoneFlag = 0; |
| 299 |
float second = 0.0f; |
| 300 |
feature_.GetFieldAsDateTime(fieldIndex, &year, &month, &day, &hour, |
| 301 |
&minute, &second, &timeZoneFlag); |
| 302 |
return odbc::makeNullable<odbc::time>(hour, minute, |
| 303 |
static_cast<int>(round(second))); |
| 304 |
} |
| 305 |
|
| 306 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 307 |
if (defaultValue == nullptr) |
| 308 |
return odbc::Time(); |
| 309 |
|
| 310 |
if (EQUAL(defaultValue, "CURRENT_TIME")) |
| 311 |
{ |
| 312 |
std::time_t t = std::time(nullptr); |
| 313 |
tm *now = std::localtime(&t); |
| 314 |
if (now == nullptr) |
| 315 |
return odbc::Time(); |
| 316 |
return odbc::makeNullable<odbc::time>(now->tm_hour, now->tm_min, |
| 317 |
now->tm_sec); |
| 318 |
} |
| 319 |
|
| 320 |
int hour = 0; |
| 321 |
int minute = 0; |
| 322 |
int second = 0; |
| 323 |
sscanf(defaultValue, "'%02d:%02d:%02d'", &hour, &minute, &second); |
| 324 |
return odbc::makeNullable<odbc::time>(hour, minute, second); |
| 325 |
} |
| 326 |
|
| 327 |
odbc::Timestamp OGRHanaFeatureReader::GetFieldAsTimestamp(int fieldIndex) const |
| 328 |
{ |
| 329 |
if (IsFieldSet(fieldIndex)) |
| 330 |
{ |
| 331 |
int year = 0; |
| 332 |
int month = 0; |
| 333 |
int day = 0; |
| 334 |
int hour = 0; |
| 335 |
int minute = 0; |
| 336 |
float secondWithMillisecond = 0.0f; |
| 337 |
int timeZoneFlag = 0; |
| 338 |
feature_.GetFieldAsDateTime(fieldIndex, &year, &month, &day, &hour, |
| 339 |
&minute, &secondWithMillisecond, |
| 340 |
&timeZoneFlag); |
| 341 |
double seconds = 0.0; |
| 342 |
double milliseconds = |
| 343 |
std::modf(static_cast<double>(secondWithMillisecond), &seconds); |
| 344 |
int second = static_cast<int>(std::floor(seconds)); |
| 345 |
int millisecond = static_cast<int>(std::floor(milliseconds * 1000)); |
| 346 |
|
| 347 |
if (!(timeZoneFlag == 0 || timeZoneFlag == 100 || timeZoneFlag == 1)) |
| 348 |
{ |
| 349 |
struct tm time; |
| 350 |
time.tm_year = year - 1900; |
| 351 |
time.tm_mon = month - 1; |
| 352 |
time.tm_mday = day; |
| 353 |
time.tm_hour = hour; |
| 354 |
time.tm_min = minute; |
| 355 |
time.tm_sec = second; |
| 356 |
GIntBig dt = CPLYMDHMSToUnixTime(&time); |
| 357 |
const int tzoffset = std::abs(timeZoneFlag - 100) * 15; |
| 358 |
dt -= tzoffset * 60; |
| 359 |
CPLUnixTimeToYMDHMS(dt, &time); |
| 360 |
year = time.tm_year + 1900; |
| 361 |
month = time.tm_mon + 1; |
| 362 |
day = time.tm_mday; |
| 363 |
hour = time.tm_hour; |
| 364 |
minute = time.tm_min; |
| 365 |
second = time.tm_sec; |
| 366 |
} |
| 367 |
|
| 368 |
return odbc::makeNullable<odbc::timestamp>(year, month, day, hour, |
| 369 |
minute, second, millisecond); |
| 370 |
} |
| 371 |
|
| 372 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 373 |
if (defaultValue == nullptr) |
| 374 |
return odbc::Timestamp(); |
| 375 |
|
| 376 |
if (EQUAL(defaultValue, "CURRENT_TIMESTAMP")) |
| 377 |
{ |
| 378 |
time_t t = std::time(nullptr); |
| 379 |
tm *now = std::localtime(&t); |
| 380 |
if (now == nullptr) |
| 381 |
return odbc::Timestamp(); |
| 382 |
return odbc::makeNullable<odbc::timestamp>( |
| 383 |
now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, |
| 384 |
now->tm_min, now->tm_sec, 0); |
| 385 |
} |
| 386 |
|
| 387 |
int year = 0; |
| 388 |
int month = 0; |
| 389 |
int day = 0; |
| 390 |
int hour = 0; |
| 391 |
int minute = 0; |
| 392 |
int second = 0; |
| 393 |
int millisecond = 0; |
| 394 |
|
| 395 |
if (strchr(defaultValue, '.') == nullptr) |
| 396 |
sscanf(defaultValue, "'%04d/%02d/%02d %02d:%02d:%02d'", &year, &month, |
| 397 |
&day, &hour, &minute, &second); |
| 398 |
else |
| 399 |
sscanf(defaultValue, "'%04d/%02d/%02d %02d:%02d:%02d.%03d'", &year, |
| 400 |
&month, &day, &hour, &minute, &second, &millisecond); |
| 401 |
|
| 402 |
return odbc::makeNullable<odbc::timestamp>(year, month, day, hour, minute, |
| 403 |
second, millisecond); |
| 404 |
} |
| 405 |
|
| 406 |
Binary OGRHanaFeatureReader::GetFieldAsBinary(int fieldIndex) const |
| 407 |
{ |
| 408 |
if (IsFieldSet(fieldIndex)) |
| 409 |
{ |
| 410 |
int size = 0; |
| 411 |
GByte *data = feature_.GetFieldAsBinary(fieldIndex, &size); |
| 412 |
return {data, static_cast<std::size_t>(size)}; |
| 413 |
} |
| 414 |
|
| 415 |
const char *defaultValue = GetDefaultValue(fieldIndex); |
| 416 |
if (defaultValue == nullptr) |
| 417 |
return {nullptr, 0U}; |
| 418 |
|
| 419 |
return {const_cast<GByte *>(reinterpret_cast<const GByte *>(defaultValue)), |
| 420 |
std::strlen(defaultValue)}; |
| 421 |
} |
| 422 |
|
| 423 |
odbc::String OGRHanaFeatureReader::GetFieldAsIntArray(int fieldIndex) const |
| 424 |
{ |
| 425 |
if (!IsFieldSet(fieldIndex)) |
| 426 |
return odbc::String(); |
| 427 |
|
| 428 |
int numElements; |
| 429 |
const int *values = |
| 430 |
feature_.GetFieldAsIntegerList(fieldIndex, &numElements); |
| 431 |
return CreateStringFromValues<int>(values, numElements, &std::to_string); |
| 432 |
} |
| 433 |
|
| 434 |
odbc::String OGRHanaFeatureReader::GetFieldAsBigIntArray(int fieldIndex) const |
| 435 |
{ |
| 436 |
if (!IsFieldSet(fieldIndex)) |
| 437 |
return odbc::String(); |
| 438 |
|
| 439 |
int numElements; |
| 440 |
const GIntBig *values = |
| 441 |
feature_.GetFieldAsInteger64List(fieldIndex, &numElements); |
| 442 |
return CreateStringFromValues<GIntBig>(values, numElements, |
| 443 |
&std::to_string); |
| 444 |
} |
| 445 |
|
| 446 |
odbc::String OGRHanaFeatureReader::GetFieldAsRealArray(int fieldIndex) const |
| 447 |
{ |
| 448 |
if (!IsFieldSet(fieldIndex)) |
| 449 |
return odbc::String(); |
| 450 |
|
| 451 |
int numElements; |
| 452 |
const double *values = |
| 453 |
feature_.GetFieldAsDoubleList(fieldIndex, &numElements); |
| 454 |
return CreateStringFromValues<double>( |
| 455 |
values, numElements, |
| 456 |
[](double value) -> std::string |
| 457 |
{ |
| 458 |
return std::isnan(value) |
| 459 |
? "NULL" |
| 460 |
: std::to_string(static_cast<float>(value)); |
| 461 |
}); |
| 462 |
} |
| 463 |
|
| 464 |
odbc::String OGRHanaFeatureReader::GetFieldAsDoubleArray(int fieldIndex) const |
| 465 |
{ |
| 466 |
if (!IsFieldSet(fieldIndex)) |
| 467 |
return odbc::String(); |
| 468 |
|
| 469 |
int numElements; |
| 470 |
const double *values = |
| 471 |
feature_.GetFieldAsDoubleList(fieldIndex, &numElements); |
| 472 |
return CreateStringFromValues<double>( |
| 473 |
values, numElements, [](double value) -> std::string |
| 474 |
{ return std::isnan(value) ? "NULL" : std::to_string(value); }); |
| 475 |
} |
| 476 |
|
| 477 |
odbc::String OGRHanaFeatureReader::GetFieldAsStringArray(int fieldIndex) const |
| 478 |
{ |
| 479 |
if (!IsFieldSet(fieldIndex)) |
| 480 |
return odbc::String(); |
| 481 |
|
| 482 |
char **items = feature_.GetFieldAsStringList(fieldIndex); |
| 483 |
if (items == nullptr) |
| 484 |
return odbc::String(); |
| 485 |
|
| 486 |
std::ostringstream os; |
| 487 |
bool firstItem = true; |
| 488 |
while (items && *items) |
| 489 |
{ |
| 490 |
if (!firstItem) |
| 491 |
os << ARRAY_VALUES_DELIMITER; |
| 492 |
|
| 493 |
char *itemValue = *items; |
| 494 |
if (*itemValue != '\0') |
| 495 |
{ |
| 496 |
os << '\''; |
| 497 |
while (*itemValue) |
| 498 |
{ |
| 499 |
if (*itemValue == '\'') |
| 500 |
os << "'"; |
| 501 |
os << *itemValue; |
| 502 |
++itemValue; |
| 503 |
} |
| 504 |
os << '\''; |
| 505 |
} |
| 506 |
|
| 507 |
++items; |
| 508 |
firstItem = false; |
| 509 |
} |
| 510 |
|
| 511 |
return odbc::String(os.str()); |
| 512 |
} |
| 513 |
|
| 514 |
const char *OGRHanaFeatureReader::GetDefaultValue(int fieldIndex) const |
| 515 |
{ |
| 516 |
const OGRFieldDefn *fieldDef = feature_.GetFieldDefnRef(fieldIndex); |
| 517 |
return fieldDef->GetDefault(); |
| 518 |
} |
| 519 |
|
| 520 |
bool OGRHanaFeatureReader::IsFieldSet(int fieldIndex) const |
| 521 |
{ |
| 522 |
return feature_.IsFieldSet(fieldIndex) && !feature_.IsFieldNull(fieldIndex); |
| 523 |
} |
| 524 |
|
| 525 |
} // namespace OGRHANA |