| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: SAP HANA Spatial Driver |
| 4 |
* Purpose: OGRHanaUtils class implementation |
| 5 |
* Author: Maxim Rylov |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2020, SAP SE |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: MIT |
| 11 |
****************************************************************************/ |
| 12 |
|
| 13 |
#include "ogrhanautils.h" |
| 14 |
#include "cpl_string.h" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
|
| 18 |
namespace OGRHANA |
| 19 |
{ |
| 20 |
|
| 21 |
HanaVersion HanaVersion::fromString(const char *version) |
| 22 |
{ |
| 23 |
CPLString splVersion(version); |
| 24 |
splVersion.replaceAll('-', '.').replaceAll(' ', '.'); |
| 25 |
|
| 26 |
const CPLStringList parts(CSLTokenizeString2(splVersion, ".", 0)); |
| 27 |
if (parts.size() < 3) |
| 28 |
return HanaVersion(0, 0, 0); |
| 29 |
|
| 30 |
return HanaVersion(atoi(parts[0]), atoi(parts[1]), atoi(parts[2])); |
| 31 |
} |
| 32 |
|
| 33 |
const char *SkipLeadingSpaces(const char *value) |
| 34 |
{ |
| 35 |
while (*value == ' ') |
| 36 |
value++; |
| 37 |
return value; |
| 38 |
} |
| 39 |
|
| 40 |
CPLString JoinStrings(const std::vector<CPLString> &strs, const char *delimiter, |
| 41 |
CPLString (*decorator)(const CPLString &str)) |
| 42 |
{ |
| 43 |
CPLString ret; |
| 44 |
for (std::size_t i = 0; i < strs.size(); ++i) |
| 45 |
{ |
| 46 |
ret += ((decorator != nullptr) ? decorator(strs[i]) : strs[i]); |
| 47 |
if (i != strs.size() - 1) |
| 48 |
ret += delimiter; |
| 49 |
} |
| 50 |
return ret; |
| 51 |
} |
| 52 |
|
| 53 |
std::vector<CPLString> SplitStrings(const char *str, const char *delimiter) |
| 54 |
{ |
| 55 |
std::vector<CPLString> ret; |
| 56 |
if (str != nullptr) |
| 57 |
{ |
| 58 |
char **items = CSLTokenizeString2(str, delimiter, CSLT_HONOURSTRINGS); |
| 59 |
for (int i = 0; items[i] != nullptr; ++i) |
| 60 |
{ |
| 61 |
CPLString item(items[i]); |
| 62 |
ret.push_back(item.Trim()); |
| 63 |
} |
| 64 |
|
| 65 |
CSLDestroy(items); |
| 66 |
} |
| 67 |
|
| 68 |
return ret; |
| 69 |
} |
| 70 |
|
| 71 |
CPLString GetFullTableName(const CPLString &schemaName, |
| 72 |
const CPLString &tableName) |
| 73 |
{ |
| 74 |
if (schemaName.empty()) |
| 75 |
return tableName; |
| 76 |
return schemaName + "." + tableName; |
| 77 |
} |
| 78 |
|
| 79 |
CPLString GetFullTableNameQuoted(const CPLString &schemaName, |
| 80 |
const CPLString &tableName) |
| 81 |
{ |
| 82 |
if (schemaName.empty()) |
| 83 |
return QuotedIdentifier(tableName); |
| 84 |
return QuotedIdentifier(schemaName) + "." + QuotedIdentifier(tableName); |
| 85 |
} |
| 86 |
|
| 87 |
CPLString GetFullColumnNameQuoted(const CPLString &schemaName, |
| 88 |
const CPLString &tableName, |
| 89 |
const CPLString &columnName) |
| 90 |
{ |
| 91 |
return GetFullTableNameQuoted(schemaName, tableName) + "." + |
| 92 |
QuotedIdentifier(columnName); |
| 93 |
} |
| 94 |
|
| 95 |
CPLString Literal(const CPLString &value) |
| 96 |
{ |
| 97 |
CPLString ret("'"); |
| 98 |
char *tmp = CPLEscapeString(value, -1, CPLES_SQL); |
| 99 |
ret += tmp; |
| 100 |
CPLFree(tmp); |
| 101 |
ret += "'"; |
| 102 |
return ret; |
| 103 |
} |
| 104 |
|
| 105 |
CPLString QuotedIdentifier(const CPLString &value) |
| 106 |
{ |
| 107 |
return "\"" + value + "\""; |
| 108 |
} |
| 109 |
|
| 110 |
bool IsArrayField(OGRFieldType fieldType) |
| 111 |
{ |
| 112 |
return (fieldType == OFTIntegerList || fieldType == OFTInteger64List || |
| 113 |
fieldType == OFTRealList || fieldType == OFTStringList || |
| 114 |
fieldType == OFTWideStringList); |
| 115 |
} |
| 116 |
|
| 117 |
bool IsGeometryTypeSupported(OGRwkbGeometryType wkbType) |
| 118 |
{ |
| 119 |
switch (wkbFlatten(wkbType)) |
| 120 |
{ |
| 121 |
case OGRwkbGeometryType::wkbPoint: |
| 122 |
case OGRwkbGeometryType::wkbLineString: |
| 123 |
case OGRwkbGeometryType::wkbPolygon: |
| 124 |
case OGRwkbGeometryType::wkbMultiPoint: |
| 125 |
case OGRwkbGeometryType::wkbMultiLineString: |
| 126 |
case OGRwkbGeometryType::wkbMultiPolygon: |
| 127 |
case OGRwkbGeometryType::wkbCircularString: |
| 128 |
case OGRwkbGeometryType::wkbGeometryCollection: |
| 129 |
return true; |
| 130 |
default: |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
OGRwkbGeometryType ToWkbType(const char *type, bool hasZ, bool hasM) |
| 136 |
{ |
| 137 |
if (strcmp(type, "ST_POINT") == 0) |
| 138 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbPoint, hasZ, hasM); |
| 139 |
else if (strcmp(type, "ST_MULTIPOINT") == 0) |
| 140 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbMultiPoint, hasZ, |
| 141 |
hasM); |
| 142 |
else if (strcmp(type, "ST_LINESTRING") == 0) |
| 143 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbLineString, hasZ, |
| 144 |
hasM); |
| 145 |
else if (strcmp(type, "ST_MULTILINESTRING") == 0) |
| 146 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbMultiLineString, hasZ, |
| 147 |
hasM); |
| 148 |
else if (strcmp(type, "ST_POLYGON") == 0) |
| 149 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbPolygon, hasZ, hasM); |
| 150 |
else if (strcmp(type, "ST_MULTIPOLYGON") == 0) |
| 151 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbMultiPolygon, hasZ, |
| 152 |
hasM); |
| 153 |
else if (strcmp(type, "ST_CIRCULARSTRING") == 0) |
| 154 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbCircularString, hasZ, |
| 155 |
hasM); |
| 156 |
else if (strcmp(type, "ST_GEOMETRYCOLLECTION") == 0) |
| 157 |
return OGR_GT_SetModifier(OGRwkbGeometryType::wkbGeometryCollection, |
| 158 |
hasZ, hasM); |
| 159 |
return OGRwkbGeometryType::wkbUnknown; |
| 160 |
} |
| 161 |
|
| 162 |
constexpr int PLANAR_SRID_OFFSET = 1000000000; |
| 163 |
|
| 164 |
int ToPlanarSRID(int srid) |
| 165 |
{ |
| 166 |
return srid < PLANAR_SRID_OFFSET ? PLANAR_SRID_OFFSET + srid : srid; |
| 167 |
} |
| 168 |
|
| 169 |
} // namespace OGRHANA |