| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: Oracle Spatial Driver |
| 4 |
* Purpose: Implementation of the OGROCITableLayer class. This class provides |
| 5 |
* layer semantics on a table, but utilizing a lot of machinery from |
| 6 |
* the OGROCILayer base class. |
| 7 |
* Author: Frank Warmerdam, [email protected] |
| 8 |
* |
| 9 |
****************************************************************************** |
| 10 |
* Copyright (c) 2002, Frank Warmerdam <[email protected]> |
| 11 |
* |
| 12 |
* SPDX-License-Identifier: MIT |
| 13 |
****************************************************************************/ |
| 14 |
|
| 15 |
#include "ogr_oci.h" |
| 16 |
#include "cpl_conv.h" |
| 17 |
#include "cpl_string.h" |
| 18 |
|
| 19 |
#include <cmath> |
| 20 |
|
| 21 |
static int nDiscarded = 0; |
| 22 |
static int nHits = 0; |
| 23 |
|
| 24 |
#define HSI_UNKNOWN -2 |
| 25 |
|
| 26 |
/************************************************************************/ |
| 27 |
/* OGROCITableLayer() */ |
| 28 |
/************************************************************************/ |
| 29 |
|
| 30 |
OGROCITableLayer::OGROCITableLayer(OGROCIDataSource *poDSIn, |
| 31 |
const char *pszTableName, |
| 32 |
OGRwkbGeometryType eGType, int nSRIDIn, |
| 33 |
int bUpdate, int bNewLayerIn) |
| 34 |
: OGROCIWritableLayer(poDSIn) |
| 35 |
|
| 36 |
{ |
| 37 |
bExtentUpdated = false; |
| 38 |
|
| 39 |
pszQuery = nullptr; |
| 40 |
pszWHERE = CPLStrdup(""); |
| 41 |
pszQueryStatement = nullptr; |
| 42 |
|
| 43 |
bUpdateAccess = bUpdate; |
| 44 |
bNewLayer = bNewLayerIn; |
| 45 |
|
| 46 |
iNextShapeId = 0; |
| 47 |
iNextFIDToWrite = -1; |
| 48 |
|
| 49 |
bValidTable = FALSE; |
| 50 |
if (bNewLayerIn) |
| 51 |
bHaveSpatialIndex = FALSE; |
| 52 |
else |
| 53 |
bHaveSpatialIndex = HSI_UNKNOWN; |
| 54 |
|
| 55 |
poFeatureDefn = ReadTableDefinition(pszTableName); |
| 56 |
if (eGType != wkbUnknown && poFeatureDefn->GetGeomFieldCount() > 0) |
| 57 |
poFeatureDefn->GetGeomFieldDefn(0)->SetType(eGType); |
| 58 |
SetDescription(poFeatureDefn->GetName()); |
| 59 |
|
| 60 |
nSRID = nSRIDIn; |
| 61 |
if (nSRID == -1) |
| 62 |
nSRID = LookupTableSRID(); |
| 63 |
|
| 64 |
poSRS = poDSIn->FetchSRS(nSRID); |
| 65 |
|
| 66 |
hOrdVARRAY = nullptr; |
| 67 |
hElemInfoVARRAY = nullptr; |
| 68 |
|
| 69 |
poBoundStatement = nullptr; |
| 70 |
|
| 71 |
nWriteCacheMax = 0; |
| 72 |
nWriteCacheUsed = 0; |
| 73 |
pasWriteGeoms = nullptr; |
| 74 |
papsWriteGeomMap = nullptr; |
| 75 |
pasWriteGeomInd = nullptr; |
| 76 |
papsWriteGeomIndMap = nullptr; |
| 77 |
|
| 78 |
papWriteFields = nullptr; |
| 79 |
papaeWriteFieldInd = nullptr; |
| 80 |
|
| 81 |
panWriteFIDs = nullptr; |
| 82 |
|
| 83 |
nDefaultStringSize = 4000; |
| 84 |
|
| 85 |
OGROCITableLayer::ResetReading(); |
| 86 |
} |
| 87 |
|
| 88 |
/************************************************************************/ |
| 89 |
/* ~OGROCITableLayer() */ |
| 90 |
/************************************************************************/ |
| 91 |
|
| 92 |
OGROCITableLayer::~OGROCITableLayer() |
| 93 |
|
| 94 |
{ |
| 95 |
int i; |
| 96 |
|
| 97 |
OGROCITableLayer::SyncToDisk(); |
| 98 |
|
| 99 |
CPLFree(panWriteFIDs); |
| 100 |
if (papWriteFields != nullptr) |
| 101 |
{ |
| 102 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 103 |
{ |
| 104 |
CPLFree(papWriteFields[i]); |
| 105 |
CPLFree(papaeWriteFieldInd[i]); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
CPLFree(papWriteFields); |
| 110 |
CPLFree(papaeWriteFieldInd); |
| 111 |
|
| 112 |
if (poBoundStatement != nullptr) |
| 113 |
delete poBoundStatement; |
| 114 |
|
| 115 |
CPLFree(pasWriteGeomInd); |
| 116 |
CPLFree(papsWriteGeomIndMap); |
| 117 |
|
| 118 |
CPLFree(papsWriteGeomMap); |
| 119 |
CPLFree(pasWriteGeoms); |
| 120 |
|
| 121 |
CPLFree(pszQuery); |
| 122 |
CPLFree(pszWHERE); |
| 123 |
} |
| 124 |
|
| 125 |
/************************************************************************/ |
| 126 |
/* ReadTableDefinition() */ |
| 127 |
/* */ |
| 128 |
/* Build a schema from the named table. Done by querying the */ |
| 129 |
/* catalog. */ |
| 130 |
/************************************************************************/ |
| 131 |
|
| 132 |
OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition(const char *pszTable) |
| 133 |
|
| 134 |
{ |
| 135 |
OGROCISession *poSession = poDS->GetSession(); |
| 136 |
sword nStatus; |
| 137 |
|
| 138 |
CPLString osUnquotedTableName; |
| 139 |
CPLString osQuotedTableName; |
| 140 |
|
| 141 |
/* -------------------------------------------------------------------- */ |
| 142 |
/* Split out the owner if available. */ |
| 143 |
/* -------------------------------------------------------------------- */ |
| 144 |
if (strstr(pszTable, ".") != nullptr) |
| 145 |
{ |
| 146 |
osTableName = strstr(pszTable, ".") + 1; |
| 147 |
osOwner.assign(pszTable, strlen(pszTable) - osTableName.size() - 1); |
| 148 |
osUnquotedTableName.Printf("%s.%s", osOwner.c_str(), |
| 149 |
osTableName.c_str()); |
| 150 |
osQuotedTableName.Printf("\"%s\".\"%s\"", osOwner.c_str(), |
| 151 |
osTableName.c_str()); |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
osTableName = pszTable; |
| 156 |
osOwner = ""; |
| 157 |
osUnquotedTableName.Printf("%s", pszTable); |
| 158 |
osQuotedTableName.Printf("\"%s\"", pszTable); |
| 159 |
} |
| 160 |
|
| 161 |
OGRFeatureDefn *poDefn = new OGRFeatureDefn(osUnquotedTableName.c_str()); |
| 162 |
|
| 163 |
poDefn->Reference(); |
| 164 |
|
| 165 |
/* -------------------------------------------------------------------- */ |
| 166 |
/* Do a DescribeAll on the table. */ |
| 167 |
/* -------------------------------------------------------------------- */ |
| 168 |
OCIParam *hAttrParam = nullptr; |
| 169 |
OCIParam *hAttrList = nullptr; |
| 170 |
|
| 171 |
// Table name unquoted |
| 172 |
|
| 173 |
nStatus = OCIDescribeAny(poSession->hSvcCtx, poSession->hError, |
| 174 |
(dvoid *)osUnquotedTableName.c_str(), |
| 175 |
static_cast<ub4>(osUnquotedTableName.length()), |
| 176 |
OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_TABLE, |
| 177 |
poSession->hDescribe); |
| 178 |
|
| 179 |
if (poSession->Failed(nStatus, "OCIDescribeAny")) |
| 180 |
{ |
| 181 |
CPLErrorReset(); |
| 182 |
|
| 183 |
// View name unquoted |
| 184 |
|
| 185 |
nStatus = OCIDescribeAny(poSession->hSvcCtx, poSession->hError, |
| 186 |
(dvoid *)osUnquotedTableName.c_str(), |
| 187 |
static_cast<ub4>(osUnquotedTableName.length()), |
| 188 |
OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_VIEW, |
| 189 |
poSession->hDescribe); |
| 190 |
|
| 191 |
if (poSession->Failed(nStatus, "OCIDescribeAny")) |
| 192 |
{ |
| 193 |
CPLErrorReset(); |
| 194 |
|
| 195 |
// Table name quoted |
| 196 |
|
| 197 |
nStatus = OCIDescribeAny( |
| 198 |
poSession->hSvcCtx, poSession->hError, |
| 199 |
(dvoid *)osQuotedTableName.c_str(), |
| 200 |
static_cast<ub4>(osQuotedTableName.length()), OCI_OTYPE_NAME, |
| 201 |
OCI_DEFAULT, OCI_PTYPE_TABLE, poSession->hDescribe); |
| 202 |
|
| 203 |
if (poSession->Failed(nStatus, "OCIDescribeAny")) |
| 204 |
{ |
| 205 |
CPLErrorReset(); |
| 206 |
|
| 207 |
// View name quoted |
| 208 |
|
| 209 |
nStatus = |
| 210 |
OCIDescribeAny(poSession->hSvcCtx, poSession->hError, |
| 211 |
(dvoid *)osQuotedTableName.c_str(), |
| 212 |
static_cast<ub4>(osQuotedTableName.length()), |
| 213 |
OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_VIEW, |
| 214 |
poSession->hDescribe); |
| 215 |
|
| 216 |
if (poSession->Failed(nStatus, "OCIDescribeAny")) |
| 217 |
return poDefn; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (poSession->Failed(OCIAttrGet(poSession->hDescribe, OCI_HTYPE_DESCRIBE, |
| 223 |
&hAttrParam, nullptr, OCI_ATTR_PARAM, |
| 224 |
poSession->hError), |
| 225 |
"OCIAttrGet(ATTR_PARAM)")) |
| 226 |
return poDefn; |
| 227 |
|
| 228 |
if (poSession->Failed(OCIAttrGet(hAttrParam, OCI_DTYPE_PARAM, &hAttrList, |
| 229 |
nullptr, OCI_ATTR_LIST_COLUMNS, |
| 230 |
poSession->hError), |
| 231 |
"OCIAttrGet(ATTR_LIST_COLUMNS)")) |
| 232 |
return poDefn; |
| 233 |
|
| 234 |
/* -------------------------------------------------------------------- */ |
| 235 |
/* What is the name of the column to use as FID? This defaults */ |
| 236 |
/* to OGR_FID but we allow it to be overridden by a config */ |
| 237 |
/* variable. Ideally we would identify a column that is a */ |
| 238 |
/* primary key and use that, but I'm not yet sure how to */ |
| 239 |
/* accomplish that. */ |
| 240 |
/* -------------------------------------------------------------------- */ |
| 241 |
const char *pszExpectedFIDName = CPLGetConfigOption("OCI_FID", "OGR_FID"); |
| 242 |
int bGeomFieldNullable = FALSE; |
| 243 |
|
| 244 |
/* -------------------------------------------------------------------- */ |
| 245 |
/* Parse the returned table information. */ |
| 246 |
/* -------------------------------------------------------------------- */ |
| 247 |
for (int iRawFld = 0; true; iRawFld++) |
| 248 |
{ |
| 249 |
OGRFieldDefn oField("", OFTString); |
| 250 |
OCIParam *hParamDesc; |
| 251 |
ub2 nOCIType; |
| 252 |
ub4 nOCILen; |
| 253 |
|
| 254 |
nStatus = OCIParamGet(hAttrList, OCI_DTYPE_PARAM, poSession->hError, |
| 255 |
reinterpret_cast<dvoid **>(&hParamDesc), |
| 256 |
(ub4)iRawFld + 1); |
| 257 |
if (nStatus != OCI_SUCCESS) |
| 258 |
break; |
| 259 |
|
| 260 |
if (poSession->GetParamInfo(hParamDesc, &oField, &nOCIType, &nOCILen) != |
| 261 |
CE_None) |
| 262 |
return poDefn; |
| 263 |
|
| 264 |
if (oField.GetType() == OFTBinary) |
| 265 |
{ |
| 266 |
if (nOCIType == 108 && pszGeomName == nullptr) |
| 267 |
{ |
| 268 |
CPLFree(pszGeomName); |
| 269 |
pszGeomName = CPLStrdup(oField.GetNameRef()); |
| 270 |
iGeomColumn = iRawFld; |
| 271 |
bGeomFieldNullable = oField.IsNullable(); |
| 272 |
} |
| 273 |
continue; |
| 274 |
} |
| 275 |
|
| 276 |
if (EQUAL(oField.GetNameRef(), pszExpectedFIDName) && |
| 277 |
(oField.GetType() == OFTInteger || |
| 278 |
oField.GetType() == OFTInteger64)) |
| 279 |
{ |
| 280 |
pszFIDName = CPLStrdup(oField.GetNameRef()); |
| 281 |
continue; |
| 282 |
} |
| 283 |
|
| 284 |
if (oField.GetTZFlag() >= OGR_TZFLAG_MIXED_TZ) |
| 285 |
{ |
| 286 |
setFieldIndexWithTimeStampWithTZ.insert(poDefn->GetFieldCount()); |
| 287 |
} |
| 288 |
|
| 289 |
poDefn->AddFieldDefn(&oField); |
| 290 |
} |
| 291 |
|
| 292 |
OGROCIStatement defaultValuesStatement(poSession); |
| 293 |
|
| 294 |
const char *pszDefaultValueSQL = |
| 295 |
"SELECT COLUMN_NAME, DATA_DEFAULT\n" |
| 296 |
"FROM user_tab_columns\n" |
| 297 |
"WHERE DATA_DEFAULT IS NOT NULL AND TABLE_NAME = UPPER(:table_name)"; |
| 298 |
|
| 299 |
defaultValuesStatement.Prepare(pszDefaultValueSQL); |
| 300 |
defaultValuesStatement.BindString(":table_name", pszTable); |
| 301 |
|
| 302 |
if (defaultValuesStatement.Execute(nullptr) == CE_None) |
| 303 |
{ |
| 304 |
char **papszRow; |
| 305 |
|
| 306 |
while ((papszRow = defaultValuesStatement.SimpleFetchRow()) != nullptr) |
| 307 |
{ |
| 308 |
const char *pszColName = papszRow[0]; |
| 309 |
const char *pszDefault = papszRow[1]; |
| 310 |
int nIdx = poDefn->GetFieldIndex(pszColName); |
| 311 |
if (nIdx >= 0) |
| 312 |
poDefn->GetFieldDefn(nIdx)->SetDefault(pszDefault); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if (EQUAL(pszExpectedFIDName, "OGR_FID") && pszFIDName) |
| 317 |
{ |
| 318 |
for (int i = 0; i < poDefn->GetFieldCount(); i++) |
| 319 |
{ |
| 320 |
// This is presumably a Integer since we always create Integer64 |
| 321 |
// with a defined precision |
| 322 |
if (poDefn->GetFieldDefn(i)->GetType() == OFTInteger64 && |
| 323 |
poDefn->GetFieldDefn(i)->GetWidth() == 0) |
| 324 |
{ |
| 325 |
poDefn->GetFieldDefn(i)->SetType(OFTInteger); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* -------------------------------------------------------------------- */ |
| 331 |
/* Identify Geometry dimension */ |
| 332 |
/* -------------------------------------------------------------------- */ |
| 333 |
|
| 334 |
if (pszGeomName != nullptr && strlen(pszGeomName) > 0) |
| 335 |
{ |
| 336 |
OGROCIStatement oDimStatement(poSession); |
| 337 |
char **papszResult; |
| 338 |
int iDim = -1; |
| 339 |
|
| 340 |
if (osOwner != "") |
| 341 |
{ |
| 342 |
const char *pszDimCmdA = |
| 343 |
"SELECT COUNT(*)\n" |
| 344 |
"FROM ALL_SDO_GEOM_METADATA u, TABLE(u.diminfo) t\n" |
| 345 |
"WHERE u.table_name = :table_name\n" |
| 346 |
" AND u.column_name = :geometry_name\n" |
| 347 |
" AND u.owner = :table_owner"; |
| 348 |
|
| 349 |
oDimStatement.Prepare(pszDimCmdA); |
| 350 |
oDimStatement.BindString(":table_name", osTableName.c_str()); |
| 351 |
oDimStatement.BindString(":geometry_name", pszGeomName); |
| 352 |
oDimStatement.BindString(":table_owner", osOwner.c_str()); |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
const char *pszDimCmdB = |
| 357 |
"SELECT COUNT(*)\n" |
| 358 |
"FROM USER_SDO_GEOM_METADATA u, TABLE(u.diminfo) t\n" |
| 359 |
"WHERE u.table_name = :table_name\n" |
| 360 |
" AND u.column_name = :geometry_name"; |
| 361 |
|
| 362 |
oDimStatement.Prepare(pszDimCmdB); |
| 363 |
oDimStatement.BindString(":table_name", osTableName.c_str()); |
| 364 |
oDimStatement.BindString(":geometry_name", pszGeomName); |
| 365 |
} |
| 366 |
oDimStatement.Execute(nullptr); |
| 367 |
|
| 368 |
papszResult = oDimStatement.SimpleFetchRow(); |
| 369 |
|
| 370 |
if (CSLCount(papszResult) < 1) |
| 371 |
{ |
| 372 |
OGROCIStatement oDimStatement2(poSession); |
| 373 |
char **papszResult2; |
| 374 |
|
| 375 |
CPLErrorReset(); |
| 376 |
|
| 377 |
if (osOwner != "") |
| 378 |
{ |
| 379 |
const char *pszDimCmd2A = |
| 380 |
"select m.sdo_index_dims\n" |
| 381 |
"from all_sdo_index_metadata m, all_sdo_index_info i\n" |
| 382 |
"where i.index_name = m.sdo_index_name\n" |
| 383 |
" and i.sdo_index_owner = m.sdo_index_owner\n" |
| 384 |
" and i.sdo_index_owner = upper(:table_owner)\n" |
| 385 |
" and i.table_name = upper(:table_name)"; |
| 386 |
|
| 387 |
oDimStatement2.Prepare(pszDimCmd2A); |
| 388 |
oDimStatement2.BindString(":table_owner", osOwner.c_str()); |
| 389 |
oDimStatement2.BindString(":table_name", osTableName.c_str()); |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
const char *pszDimCmd2B = |
| 394 |
"select m.sdo_index_dims\n" |
| 395 |
"from user_sdo_index_metadata m, user_sdo_index_info i\n" |
| 396 |
"where i.index_name = m.sdo_index_name\n" |
| 397 |
" and i.table_name = upper(:table_name)"; |
| 398 |
|
| 399 |
oDimStatement2.Prepare(pszDimCmd2B); |
| 400 |
oDimStatement2.BindString(":table_name", osTableName.c_str()); |
| 401 |
} |
| 402 |
oDimStatement2.Execute(nullptr); |
| 403 |
|
| 404 |
papszResult2 = oDimStatement2.SimpleFetchRow(); |
| 405 |
|
| 406 |
if (CSLCount(papszResult2) > 0) |
| 407 |
{ |
| 408 |
iDim = atoi(papszResult2[0]); |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
// we want to clear any errors to avoid confusing the |
| 413 |
// application. |
| 414 |
CPLErrorReset(); |
| 415 |
} |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
iDim = atoi(papszResult[0]); |
| 420 |
} |
| 421 |
|
| 422 |
if (iDim > 0) |
| 423 |
{ |
| 424 |
SetDimension(iDim); |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
CPLDebug("OCI", "get dim based of existing data or index failed."); |
| 429 |
} |
| 430 |
|
| 431 |
{ |
| 432 |
OGROCIStatement oDimStatement2(poSession); |
| 433 |
char **papszResult2; |
| 434 |
|
| 435 |
CPLErrorReset(); |
| 436 |
if (osOwner != "") |
| 437 |
{ |
| 438 |
const char *pszLayerTypeCmdA = |
| 439 |
"select m.SDO_LAYER_GTYPE " |
| 440 |
"from all_sdo_index_metadata m, all_sdo_index_info i " |
| 441 |
"where i.index_name = m.sdo_index_name " |
| 442 |
"and i.sdo_index_owner = m.sdo_index_owner " |
| 443 |
"and i.sdo_index_owner = upper(:table_owner) " |
| 444 |
"and i.table_name = upper(:table_name)"; |
| 445 |
|
| 446 |
oDimStatement2.Prepare(pszLayerTypeCmdA); |
| 447 |
oDimStatement2.BindString(":table_owner", osOwner.c_str()); |
| 448 |
oDimStatement2.BindString(":table_name", osTableName.c_str()); |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
const char *pszLayerTypeCmdB = |
| 453 |
"select m.SDO_LAYER_GTYPE " |
| 454 |
"from user_sdo_index_metadata m, user_sdo_index_info i " |
| 455 |
"where i.index_name = m.sdo_index_name " |
| 456 |
"and i.table_name = upper(:table_name)"; |
| 457 |
oDimStatement2.Prepare(pszLayerTypeCmdB); |
| 458 |
oDimStatement2.BindString(":table_name", osTableName.c_str()); |
| 459 |
} |
| 460 |
|
| 461 |
oDimStatement2.Execute(nullptr); |
| 462 |
|
| 463 |
papszResult2 = oDimStatement2.SimpleFetchRow(); |
| 464 |
|
| 465 |
if (CSLCount(papszResult2) > 0) |
| 466 |
{ |
| 467 |
const char *pszLayerGType = papszResult2[0]; |
| 468 |
OGRwkbGeometryType eGeomType = wkbUnknown; |
| 469 |
if (EQUAL(pszLayerGType, "POINT")) |
| 470 |
eGeomType = wkbPoint; |
| 471 |
else if (EQUAL(pszLayerGType, "LINE")) |
| 472 |
eGeomType = wkbLineString; |
| 473 |
else if (EQUAL(pszLayerGType, "POLYGON")) |
| 474 |
eGeomType = wkbPolygon; |
| 475 |
else if (EQUAL(pszLayerGType, "MULTIPOINT")) |
| 476 |
eGeomType = wkbMultiPoint; |
| 477 |
else if (EQUAL(pszLayerGType, "MULTILINE")) |
| 478 |
eGeomType = wkbMultiLineString; |
| 479 |
else if (EQUAL(pszLayerGType, "MULTIPOLYGON")) |
| 480 |
eGeomType = wkbMultiPolygon; |
| 481 |
else if (!EQUAL(pszLayerGType, "COLLECTION")) |
| 482 |
CPLDebug("OCI", "LAYER_GTYPE = %s", pszLayerGType); |
| 483 |
if (iDim == 3) |
| 484 |
eGeomType = wkbSetZ(eGeomType); |
| 485 |
poDefn->GetGeomFieldDefn(0)->SetType(eGeomType); |
| 486 |
poDefn->GetGeomFieldDefn(0)->SetNullable(bGeomFieldNullable); |
| 487 |
} |
| 488 |
else |
| 489 |
{ |
| 490 |
// we want to clear any errors to avoid confusing the |
| 491 |
// application. |
| 492 |
CPLErrorReset(); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
poDefn->SetGeomType(wkbNone); |
| 499 |
} |
| 500 |
|
| 501 |
bValidTable = TRUE; |
| 502 |
|
| 503 |
return poDefn; |
| 504 |
} |
| 505 |
|
| 506 |
/************************************************************************/ |
| 507 |
/* ISetSpatialFilter() */ |
| 508 |
/************************************************************************/ |
| 509 |
|
| 510 |
OGRErr OGROCITableLayer::ISetSpatialFilter(int, const OGRGeometry *poGeomIn) |
| 511 |
|
| 512 |
{ |
| 513 |
if (!InstallFilter(poGeomIn)) |
| 514 |
return OGRERR_NONE; |
| 515 |
|
| 516 |
BuildWhere(); |
| 517 |
|
| 518 |
ResetReading(); |
| 519 |
return OGRERR_NONE; |
| 520 |
} |
| 521 |
|
| 522 |
/************************************************************************/ |
| 523 |
/* TestForSpatialIndex() */ |
| 524 |
/************************************************************************/ |
| 525 |
|
| 526 |
void OGROCITableLayer::TestForSpatialIndex(const char *pszSpatWHERE) |
| 527 |
|
| 528 |
{ |
| 529 |
OGROCIStringBuf oTestCmd; |
| 530 |
OGROCIStatement oTestStatement(poDS->GetSession()); |
| 531 |
|
| 532 |
oTestCmd.Append("SELECT COUNT(*) FROM "); |
| 533 |
oTestCmd.Append(poFeatureDefn->GetName()); |
| 534 |
oTestCmd.Append(pszSpatWHERE); |
| 535 |
|
| 536 |
if (oTestStatement.Execute(oTestCmd.GetString()) != CE_None) |
| 537 |
bHaveSpatialIndex = FALSE; |
| 538 |
else |
| 539 |
bHaveSpatialIndex = TRUE; |
| 540 |
} |
| 541 |
|
| 542 |
/************************************************************************/ |
| 543 |
/* BuildWhere() */ |
| 544 |
/* */ |
| 545 |
/* Build the WHERE statement appropriate to the current set of */ |
| 546 |
/* criteria (spatial and attribute queries). */ |
| 547 |
/************************************************************************/ |
| 548 |
|
| 549 |
void OGROCITableLayer::BuildWhere() |
| 550 |
|
| 551 |
{ |
| 552 |
OGROCIStringBuf oWHERE; |
| 553 |
|
| 554 |
CPLFree(pszWHERE); |
| 555 |
pszWHERE = nullptr; |
| 556 |
|
| 557 |
if (m_poFilterGeom != nullptr && bHaveSpatialIndex) |
| 558 |
{ |
| 559 |
OGREnvelope sEnvelope; |
| 560 |
|
| 561 |
m_poFilterGeom->getEnvelope(&sEnvelope); |
| 562 |
|
| 563 |
oWHERE.Append(" WHERE sdo_filter("); |
| 564 |
oWHERE.Append(pszGeomName); |
| 565 |
oWHERE.Append(", MDSYS.SDO_GEOMETRY(2003,"); |
| 566 |
if (nSRID == -1) |
| 567 |
oWHERE.Append("NULL"); |
| 568 |
else |
| 569 |
oWHERE.Appendf(15, "%d", nSRID); |
| 570 |
oWHERE.Append(",NULL,"); |
| 571 |
oWHERE.Append("MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),"); |
| 572 |
oWHERE.Append("MDSYS.SDO_ORDINATE_ARRAY("); |
| 573 |
oWHERE.Appendf( |
| 574 |
600, "%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g", |
| 575 |
sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MinY, |
| 576 |
sEnvelope.MaxX, sEnvelope.MaxY, sEnvelope.MinX, sEnvelope.MaxY, |
| 577 |
sEnvelope.MinX, sEnvelope.MinY); |
| 578 |
oWHERE.Append(")), 'querytype=window') = 'TRUE' "); |
| 579 |
} |
| 580 |
|
| 581 |
if (bHaveSpatialIndex == HSI_UNKNOWN) |
| 582 |
{ |
| 583 |
TestForSpatialIndex(oWHERE.GetString()); |
| 584 |
if (!bHaveSpatialIndex) |
| 585 |
oWHERE.Clear(); |
| 586 |
} |
| 587 |
|
| 588 |
if (pszQuery != nullptr) |
| 589 |
{ |
| 590 |
if (oWHERE.GetLast() == '\0') |
| 591 |
oWHERE.Append("WHERE "); |
| 592 |
else |
| 593 |
oWHERE.Append("AND "); |
| 594 |
|
| 595 |
oWHERE.Append(pszQuery); |
| 596 |
} |
| 597 |
|
| 598 |
pszWHERE = oWHERE.StealString(); |
| 599 |
} |
| 600 |
|
| 601 |
/************************************************************************/ |
| 602 |
/* BuildFullQueryStatement() */ |
| 603 |
/************************************************************************/ |
| 604 |
|
| 605 |
void OGROCITableLayer::BuildFullQueryStatement() |
| 606 |
|
| 607 |
{ |
| 608 |
if (pszQueryStatement != nullptr) |
| 609 |
{ |
| 610 |
CPLFree(pszQueryStatement); |
| 611 |
pszQueryStatement = nullptr; |
| 612 |
} |
| 613 |
|
| 614 |
OGROCIStringBuf oCmd; |
| 615 |
char *pszFields = BuildFields(); |
| 616 |
|
| 617 |
oCmd.Append("SELECT "); |
| 618 |
oCmd.Append(pszFields); |
| 619 |
oCmd.Append(" FROM "); |
| 620 |
oCmd.Append(poFeatureDefn->GetName()); |
| 621 |
oCmd.Append(" "); |
| 622 |
oCmd.Append(pszWHERE); |
| 623 |
|
| 624 |
pszQueryStatement = oCmd.StealString(); |
| 625 |
|
| 626 |
CPLFree(pszFields); |
| 627 |
} |
| 628 |
|
| 629 |
/************************************************************************/ |
| 630 |
/* GetFeature() */ |
| 631 |
/************************************************************************/ |
| 632 |
|
| 633 |
OGRFeature *OGROCITableLayer::GetFeature(GIntBig nFeatureId) |
| 634 |
|
| 635 |
{ |
| 636 |
|
| 637 |
/* -------------------------------------------------------------------- */ |
| 638 |
/* If we don't have an FID column scan for the desired feature. */ |
| 639 |
/* -------------------------------------------------------------------- */ |
| 640 |
if (pszFIDName == nullptr) |
| 641 |
return OGROCILayer::GetFeature(nFeatureId); |
| 642 |
|
| 643 |
/* -------------------------------------------------------------------- */ |
| 644 |
/* Clear any existing query. */ |
| 645 |
/* -------------------------------------------------------------------- */ |
| 646 |
ResetReading(); |
| 647 |
|
| 648 |
/* -------------------------------------------------------------------- */ |
| 649 |
/* Build query for this specific feature. */ |
| 650 |
/* -------------------------------------------------------------------- */ |
| 651 |
OGROCIStringBuf oCmd; |
| 652 |
char *pszFields = BuildFields(); |
| 653 |
|
| 654 |
oCmd.Append("SELECT "); |
| 655 |
oCmd.Append(pszFields); |
| 656 |
oCmd.Append(" FROM "); |
| 657 |
oCmd.Append(poFeatureDefn->GetName()); |
| 658 |
oCmd.Append(" "); |
| 659 |
oCmd.Appendf(static_cast<int>(50 + strlen(pszFIDName)), |
| 660 |
" WHERE \"%s\" = " CPL_FRMT_GIB " ", pszFIDName, nFeatureId); |
| 661 |
|
| 662 |
CPLFree(pszFields); |
| 663 |
|
| 664 |
/* -------------------------------------------------------------------- */ |
| 665 |
/* Execute the statement. */ |
| 666 |
/* -------------------------------------------------------------------- */ |
| 667 |
if (!ExecuteQuery(oCmd.GetString())) |
| 668 |
return nullptr; |
| 669 |
|
| 670 |
/* -------------------------------------------------------------------- */ |
| 671 |
/* Get the feature. */ |
| 672 |
/* -------------------------------------------------------------------- */ |
| 673 |
OGRFeature *poFeature; |
| 674 |
|
| 675 |
poFeature = GetNextRawFeature(); |
| 676 |
|
| 677 |
if (poFeature != nullptr && poFeature->GetGeometryRef() != nullptr) |
| 678 |
poFeature->GetGeometryRef()->assignSpatialReference(poSRS.get()); |
| 679 |
|
| 680 |
/* -------------------------------------------------------------------- */ |
| 681 |
/* Cleanup the statement. */ |
| 682 |
/* -------------------------------------------------------------------- */ |
| 683 |
ResetReading(); |
| 684 |
|
| 685 |
/* -------------------------------------------------------------------- */ |
| 686 |
/* verify the FID. */ |
| 687 |
/* -------------------------------------------------------------------- */ |
| 688 |
if (poFeature != nullptr && poFeature->GetFID() != nFeatureId) |
| 689 |
{ |
| 690 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 691 |
"OGROCITableLayer::GetFeature(" CPL_FRMT_GIB |
| 692 |
") ... query returned feature " CPL_FRMT_GIB " instead!", |
| 693 |
nFeatureId, poFeature->GetFID()); |
| 694 |
delete poFeature; |
| 695 |
return nullptr; |
| 696 |
} |
| 697 |
else |
| 698 |
return poFeature; |
| 699 |
} |
| 700 |
|
| 701 |
/************************************************************************/ |
| 702 |
/* GetNextFeature() */ |
| 703 |
/* */ |
| 704 |
/* We override the next feature method because we know that we */ |
| 705 |
/* implement the attribute query within the statement and so we */ |
| 706 |
/* don't have to test here. Eventually the spatial query will */ |
| 707 |
/* be fully tested within the statement as well. */ |
| 708 |
/************************************************************************/ |
| 709 |
|
| 710 |
OGRFeature *OGROCITableLayer::GetNextFeature() |
| 711 |
|
| 712 |
{ |
| 713 |
|
| 714 |
while (true) |
| 715 |
{ |
| 716 |
OGRFeature *poFeature; |
| 717 |
|
| 718 |
poFeature = GetNextRawFeature(); |
| 719 |
if (poFeature == nullptr) |
| 720 |
{ |
| 721 |
CPLDebug("OCI", "Query complete, got %d hits, and %d discards.", |
| 722 |
nHits, nDiscarded); |
| 723 |
nHits = 0; |
| 724 |
nDiscarded = 0; |
| 725 |
return nullptr; |
| 726 |
} |
| 727 |
|
| 728 |
if (m_poFilterGeom == nullptr || |
| 729 |
FilterGeometry(poFeature->GetGeometryRef())) |
| 730 |
{ |
| 731 |
nHits++; |
| 732 |
if (poFeature->GetGeometryRef() != nullptr) |
| 733 |
poFeature->GetGeometryRef()->assignSpatialReference( |
| 734 |
poSRS.get()); |
| 735 |
return poFeature; |
| 736 |
} |
| 737 |
|
| 738 |
if (m_poFilterGeom != nullptr) |
| 739 |
nDiscarded++; |
| 740 |
|
| 741 |
delete poFeature; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
/************************************************************************/ |
| 746 |
/* ResetReading() */ |
| 747 |
/************************************************************************/ |
| 748 |
|
| 749 |
void OGROCITableLayer::ResetReading() |
| 750 |
|
| 751 |
{ |
| 752 |
nHits = 0; |
| 753 |
nDiscarded = 0; |
| 754 |
|
| 755 |
FlushPendingFeatures(); |
| 756 |
|
| 757 |
BuildFullQueryStatement(); |
| 758 |
|
| 759 |
OGROCILayer::ResetReading(); |
| 760 |
} |
| 761 |
|
| 762 |
/************************************************************************/ |
| 763 |
/* BuildFields() */ |
| 764 |
/* */ |
| 765 |
/* Build list of fields to fetch, performing any required */ |
| 766 |
/* transformations (such as on geometry). */ |
| 767 |
/************************************************************************/ |
| 768 |
|
| 769 |
char *OGROCITableLayer::BuildFields() |
| 770 |
|
| 771 |
{ |
| 772 |
int i; |
| 773 |
OGROCIStringBuf oFldList; |
| 774 |
|
| 775 |
if (pszGeomName) |
| 776 |
{ |
| 777 |
oFldList.Append("\""); |
| 778 |
oFldList.Append(pszGeomName); |
| 779 |
oFldList.Append("\""); |
| 780 |
iGeomColumn = 0; |
| 781 |
} |
| 782 |
|
| 783 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 784 |
{ |
| 785 |
const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 786 |
|
| 787 |
if (oFldList.GetLast() != '\0') |
| 788 |
oFldList.Append(","); |
| 789 |
|
| 790 |
oFldList.Append("\""); |
| 791 |
oFldList.Append(pszName); |
| 792 |
oFldList.Append("\""); |
| 793 |
} |
| 794 |
|
| 795 |
if (pszFIDName != nullptr) |
| 796 |
{ |
| 797 |
iFIDColumn = poFeatureDefn->GetFieldCount(); |
| 798 |
oFldList.Append(",\""); |
| 799 |
oFldList.Append(pszFIDName); |
| 800 |
oFldList.Append("\""); |
| 801 |
} |
| 802 |
|
| 803 |
return oFldList.StealString(); |
| 804 |
} |
| 805 |
|
| 806 |
/************************************************************************/ |
| 807 |
/* SetAttributeFilter() */ |
| 808 |
/************************************************************************/ |
| 809 |
|
| 810 |
OGRErr OGROCITableLayer::SetAttributeFilter(const char *pszQueryIn) |
| 811 |
|
| 812 |
{ |
| 813 |
CPLFree(m_pszAttrQueryString); |
| 814 |
m_pszAttrQueryString = (pszQueryIn) ? CPLStrdup(pszQueryIn) : nullptr; |
| 815 |
|
| 816 |
if ((pszQueryIn == nullptr && this->pszQuery == nullptr) || |
| 817 |
(pszQueryIn != nullptr && this->pszQuery != nullptr && |
| 818 |
strcmp(pszQueryIn, this->pszQuery) == 0)) |
| 819 |
return OGRERR_NONE; |
| 820 |
|
| 821 |
CPLFree(this->pszQuery); |
| 822 |
|
| 823 |
if (pszQueryIn == nullptr) |
| 824 |
this->pszQuery = nullptr; |
| 825 |
else |
| 826 |
this->pszQuery = CPLStrdup(pszQueryIn); |
| 827 |
|
| 828 |
BuildWhere(); |
| 829 |
|
| 830 |
ResetReading(); |
| 831 |
|
| 832 |
return OGRERR_NONE; |
| 833 |
} |
| 834 |
|
| 835 |
/************************************************************************/ |
| 836 |
/* ISetFeature() */ |
| 837 |
/* */ |
| 838 |
/* We implement SetFeature() by deleting the existing row (if */ |
| 839 |
/* it exists), and then using CreateFeature() to write it out */ |
| 840 |
/* tot he table normally. CreateFeature() will preserve the */ |
| 841 |
/* existing FID if possible. */ |
| 842 |
/************************************************************************/ |
| 843 |
|
| 844 |
OGRErr OGROCITableLayer::ISetFeature(OGRFeature *poFeature) |
| 845 |
|
| 846 |
{ |
| 847 |
/* -------------------------------------------------------------------- */ |
| 848 |
/* Do some validation. */ |
| 849 |
/* -------------------------------------------------------------------- */ |
| 850 |
if (pszFIDName == nullptr) |
| 851 |
{ |
| 852 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 853 |
"OGROCITableLayer::ISetFeature(" CPL_FRMT_GIB |
| 854 |
") failed because there is " |
| 855 |
"no apparent FID column on table %s.", |
| 856 |
poFeature->GetFID(), poFeatureDefn->GetName()); |
| 857 |
|
| 858 |
return OGRERR_FAILURE; |
| 859 |
} |
| 860 |
|
| 861 |
if (poFeature->GetFID() == OGRNullFID) |
| 862 |
{ |
| 863 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 864 |
"OGROCITableLayer::ISetFeature(" CPL_FRMT_GIB |
| 865 |
") failed because the feature " |
| 866 |
"has no FID!", |
| 867 |
poFeature->GetFID()); |
| 868 |
|
| 869 |
return OGRERR_FAILURE; |
| 870 |
} |
| 871 |
|
| 872 |
OGRErr eErr = DeleteFeature(poFeature->GetFID()); |
| 873 |
if (eErr != OGRERR_NONE) |
| 874 |
return eErr; |
| 875 |
|
| 876 |
return CreateFeature(poFeature); |
| 877 |
} |
| 878 |
|
| 879 |
/************************************************************************/ |
| 880 |
/* DeleteFeature() */ |
| 881 |
/************************************************************************/ |
| 882 |
|
| 883 |
OGRErr OGROCITableLayer::DeleteFeature(GIntBig nFID) |
| 884 |
|
| 885 |
{ |
| 886 |
/* -------------------------------------------------------------------- */ |
| 887 |
/* Do some validation. */ |
| 888 |
/* -------------------------------------------------------------------- */ |
| 889 |
if (pszFIDName == nullptr) |
| 890 |
{ |
| 891 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 892 |
"OGROCITableLayer::DeleteFeature(" CPL_FRMT_GIB |
| 893 |
") failed because there is " |
| 894 |
"no apparent FID column on table %s.", |
| 895 |
nFID, poFeatureDefn->GetName()); |
| 896 |
|
| 897 |
return OGRERR_FAILURE; |
| 898 |
} |
| 899 |
|
| 900 |
if (nFID == OGRNullFID) |
| 901 |
{ |
| 902 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 903 |
"OGROCITableLayer::DeleteFeature(" CPL_FRMT_GIB |
| 904 |
") failed for Null FID", |
| 905 |
nFID); |
| 906 |
|
| 907 |
return OGRERR_FAILURE; |
| 908 |
} |
| 909 |
|
| 910 |
/* -------------------------------------------------------------------- */ |
| 911 |
/* Prepare the delete command, and execute. We don't check the */ |
| 912 |
/* error result of the execute, since attempting to Set a */ |
| 913 |
/* non-existing feature may be OK. */ |
| 914 |
/* -------------------------------------------------------------------- */ |
| 915 |
OGROCIStringBuf oCmdText; |
| 916 |
OGROCIStatement oCmdStatement(poDS->GetSession()); |
| 917 |
|
| 918 |
oCmdText.Appendf(static_cast<int>(strlen(poFeatureDefn->GetName()) + |
| 919 |
strlen(pszFIDName) + 100), |
| 920 |
"DELETE FROM %s WHERE \"%s\" = " CPL_FRMT_GIB, |
| 921 |
poFeatureDefn->GetName(), pszFIDName, nFID); |
| 922 |
|
| 923 |
if (oCmdStatement.Execute(oCmdText.GetString()) == CE_None) |
| 924 |
return (oCmdStatement.GetAffectedRows() > 0) |
| 925 |
? OGRERR_NONE |
| 926 |
: OGRERR_NON_EXISTING_FEATURE; |
| 927 |
else |
| 928 |
return OGRERR_FAILURE; |
| 929 |
} |
| 930 |
|
| 931 |
/************************************************************************/ |
| 932 |
/* ICreateFeature() */ |
| 933 |
/************************************************************************/ |
| 934 |
|
| 935 |
OGRErr OGROCITableLayer::ICreateFeature(OGRFeature *poFeature) |
| 936 |
|
| 937 |
{ |
| 938 |
/* -------------------------------------------------------------------- */ |
| 939 |
/* Add extents of this geometry to the existing layer extents. */ |
| 940 |
/* -------------------------------------------------------------------- */ |
| 941 |
if (poFeature->GetGeometryRef() != nullptr) |
| 942 |
{ |
| 943 |
OGREnvelope sThisExtent; |
| 944 |
|
| 945 |
poFeature->GetGeometryRef()->getEnvelope(&sThisExtent); |
| 946 |
|
| 947 |
if (!sExtent.Contains(sThisExtent)) |
| 948 |
{ |
| 949 |
sExtent.Merge(sThisExtent); |
| 950 |
bExtentUpdated = true; |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
/* -------------------------------------------------------------------- */ |
| 955 |
/* Get the first id value from open options */ |
| 956 |
/* -------------------------------------------------------------------- */ |
| 957 |
|
| 958 |
this->nFirstId = -1; |
| 959 |
|
| 960 |
if (CSLFetchNameValue(papszOptions, "FIRST_ID") != nullptr) |
| 961 |
{ |
| 962 |
this->nFirstId = atoi(CSLFetchNameValue(papszOptions, "FIRST_ID")); |
| 963 |
} |
| 964 |
|
| 965 |
/* -------------------------------------------------------------------- */ |
| 966 |
/* Get the multi load count value from open options */ |
| 967 |
/* -------------------------------------------------------------------- */ |
| 968 |
|
| 969 |
this->bMultiLoad = CPLFetchBool(papszOptions, "MULTI_LOAD", true); |
| 970 |
|
| 971 |
this->nMultiLoadCount = 100; |
| 972 |
|
| 973 |
if (CSLFetchNameValue(papszOptions, "MULTI_LOAD_COUNT") != nullptr) |
| 974 |
{ |
| 975 |
this->nMultiLoadCount = |
| 976 |
atoi(CSLFetchNameValue(papszOptions, "MULTI_LOAD_COUNT")); |
| 977 |
this->bMultiLoad = true; // overwrites MULTI_LOAD=NO |
| 978 |
} |
| 979 |
|
| 980 |
/* -------------------------------------------------------------------- */ |
| 981 |
/* Do the actual creation. */ |
| 982 |
/* -------------------------------------------------------------------- */ |
| 983 |
if (bMultiLoad) |
| 984 |
return BoundCreateFeature(poFeature); |
| 985 |
else |
| 986 |
return UnboundCreateFeature(poFeature); |
| 987 |
} |
| 988 |
|
| 989 |
/************************************************************************/ |
| 990 |
/* UnboundCreateFeature() */ |
| 991 |
/************************************************************************/ |
| 992 |
|
| 993 |
OGRErr OGROCITableLayer::UnboundCreateFeature(OGRFeature *poFeature) |
| 994 |
|
| 995 |
{ |
| 996 |
OGROCISession *poSession = poDS->GetSession(); |
| 997 |
char *pszCommand; |
| 998 |
int bNeedComma = FALSE; |
| 999 |
size_t nCommandBufSize; |
| 1000 |
|
| 1001 |
/* -------------------------------------------------------------------- */ |
| 1002 |
/* Prepare SQL statement buffer. */ |
| 1003 |
/* -------------------------------------------------------------------- */ |
| 1004 |
nCommandBufSize = 2000; |
| 1005 |
pszCommand = (char *)CPLMalloc(nCommandBufSize); |
| 1006 |
|
| 1007 |
/* -------------------------------------------------------------------- */ |
| 1008 |
/* Form the INSERT command. */ |
| 1009 |
/* -------------------------------------------------------------------- */ |
| 1010 |
snprintf(pszCommand, nCommandBufSize, "INSERT INTO \"%s\"(\"", |
| 1011 |
poFeatureDefn->GetName()); |
| 1012 |
|
| 1013 |
if (poFeature->GetGeometryRef() != nullptr) |
| 1014 |
{ |
| 1015 |
bNeedComma = TRUE; |
| 1016 |
strcat(pszCommand, pszGeomName); |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (pszFIDName != nullptr) |
| 1020 |
{ |
| 1021 |
if (bNeedComma) |
| 1022 |
strcat(pszCommand, "\",\""); |
| 1023 |
|
| 1024 |
strcat(pszCommand, pszFIDName); |
| 1025 |
bNeedComma = TRUE; |
| 1026 |
} |
| 1027 |
|
| 1028 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1029 |
{ |
| 1030 |
if (!poFeature->IsFieldSetAndNotNull(i)) |
| 1031 |
continue; |
| 1032 |
|
| 1033 |
if (!bNeedComma) |
| 1034 |
bNeedComma = TRUE; |
| 1035 |
else |
| 1036 |
strcat(pszCommand, "\",\""); |
| 1037 |
|
| 1038 |
snprintf(pszCommand + strlen(pszCommand), |
| 1039 |
nCommandBufSize - strlen(pszCommand), "%s", |
| 1040 |
poFeatureDefn->GetFieldDefn(i)->GetNameRef()); |
| 1041 |
} |
| 1042 |
|
| 1043 |
strcat(pszCommand, "\") VALUES ("); |
| 1044 |
|
| 1045 |
CPLAssert(strlen(pszCommand) < nCommandBufSize); |
| 1046 |
|
| 1047 |
/* -------------------------------------------------------------------- */ |
| 1048 |
/* Set the geometry */ |
| 1049 |
/* -------------------------------------------------------------------- */ |
| 1050 |
bNeedComma = poFeature->GetGeometryRef() != nullptr; |
| 1051 |
if (poFeature->GetGeometryRef() != nullptr) |
| 1052 |
{ |
| 1053 |
OGRGeometry *poGeometry = poFeature->GetGeometryRef(); |
| 1054 |
char szSDO_GEOMETRY[512]; |
| 1055 |
char szSRID[128]; |
| 1056 |
|
| 1057 |
if (nSRID == -1) |
| 1058 |
strcpy(szSRID, "NULL"); |
| 1059 |
else |
| 1060 |
snprintf(szSRID, sizeof(szSRID), "%d", nSRID); |
| 1061 |
|
| 1062 |
if (wkbFlatten(poGeometry->getGeometryType()) == wkbPoint) |
| 1063 |
{ |
| 1064 |
OGRPoint *poPoint = poGeometry->toPoint(); |
| 1065 |
|
| 1066 |
if (nDimension == 2) |
| 1067 |
CPLsnprintf( |
| 1068 |
szSDO_GEOMETRY, sizeof(szSDO_GEOMETRY), |
| 1069 |
"%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,0),NULL,NULL)", |
| 1070 |
SDO_GEOMETRY, 2001, szSRID, poPoint->getX(), |
| 1071 |
poPoint->getY()); |
| 1072 |
else |
| 1073 |
CPLsnprintf(szSDO_GEOMETRY, sizeof(szSDO_GEOMETRY), |
| 1074 |
"%s(%d,%s,MDSYS.SDO_POINT_TYPE(%.16g,%.16g,%.16g)," |
| 1075 |
"NULL,NULL)", |
| 1076 |
SDO_GEOMETRY, 3001, szSRID, poPoint->getX(), |
| 1077 |
poPoint->getY(), poPoint->getZ()); |
| 1078 |
} |
| 1079 |
else |
| 1080 |
{ |
| 1081 |
int nGType; |
| 1082 |
|
| 1083 |
if (TranslateToSDOGeometry(poFeature->GetGeometryRef(), &nGType) == |
| 1084 |
OGRERR_NONE) |
| 1085 |
CPLsnprintf(szSDO_GEOMETRY, sizeof(szSDO_GEOMETRY), |
| 1086 |
"%s(%d,%s,NULL,:elem_info,:ordinates)", |
| 1087 |
SDO_GEOMETRY, nGType, szSRID); |
| 1088 |
else |
| 1089 |
CPLsnprintf(szSDO_GEOMETRY, sizeof(szSDO_GEOMETRY), "NULL"); |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (strlen(pszCommand) + strlen(szSDO_GEOMETRY) > nCommandBufSize - 50) |
| 1093 |
{ |
| 1094 |
nCommandBufSize = |
| 1095 |
strlen(pszCommand) + strlen(szSDO_GEOMETRY) + 10000; |
| 1096 |
pszCommand = (char *)CPLRealloc(pszCommand, nCommandBufSize); |
| 1097 |
} |
| 1098 |
|
| 1099 |
strcat(pszCommand, szSDO_GEOMETRY); |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* -------------------------------------------------------------------- */ |
| 1103 |
/* Set the FID. */ |
| 1104 |
/* -------------------------------------------------------------------- */ |
| 1105 |
size_t nOffset = strlen(pszCommand); |
| 1106 |
|
| 1107 |
if (pszFIDName != nullptr) |
| 1108 |
{ |
| 1109 |
GIntBig nFID; |
| 1110 |
|
| 1111 |
if (bNeedComma) |
| 1112 |
strcat(pszCommand + nOffset, ", "); |
| 1113 |
bNeedComma = TRUE; |
| 1114 |
|
| 1115 |
nOffset += strlen(pszCommand + nOffset); |
| 1116 |
|
| 1117 |
nFID = poFeature->GetFID(); |
| 1118 |
if (nFID == OGRNullFID) |
| 1119 |
{ |
| 1120 |
if (iNextFIDToWrite < 0) |
| 1121 |
{ |
| 1122 |
iNextFIDToWrite = GetMaxFID() + 1; |
| 1123 |
} |
| 1124 |
nFID = iNextFIDToWrite++; |
| 1125 |
poFeature->SetFID(nFID); |
| 1126 |
} |
| 1127 |
snprintf(pszCommand + nOffset, nCommandBufSize - nOffset, CPL_FRMT_GIB, |
| 1128 |
nFID); |
| 1129 |
} |
| 1130 |
|
| 1131 |
/* -------------------------------------------------------------------- */ |
| 1132 |
/* Set the other fields. */ |
| 1133 |
/* -------------------------------------------------------------------- */ |
| 1134 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1135 |
{ |
| 1136 |
if (!poFeature->IsFieldSetAndNotNull(i)) |
| 1137 |
continue; |
| 1138 |
|
| 1139 |
OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i); |
| 1140 |
const char *pszStrValue = poFeature->GetFieldAsString(i); |
| 1141 |
|
| 1142 |
if (bNeedComma) |
| 1143 |
strcat(pszCommand + nOffset, ", "); |
| 1144 |
else |
| 1145 |
bNeedComma = TRUE; |
| 1146 |
|
| 1147 |
if (strlen(pszStrValue) + strlen(pszCommand + nOffset) + nOffset > |
| 1148 |
nCommandBufSize - 50) |
| 1149 |
{ |
| 1150 |
nCommandBufSize = strlen(pszCommand) + strlen(pszStrValue) + 10000; |
| 1151 |
pszCommand = (char *)CPLRealloc(pszCommand, nCommandBufSize); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (poFldDefn->GetType() == OFTInteger || |
| 1155 |
poFldDefn->GetType() == OFTInteger64 || |
| 1156 |
poFldDefn->GetType() == OFTReal) |
| 1157 |
{ |
| 1158 |
if (poFldDefn->GetWidth() > 0 && bPreservePrecision && |
| 1159 |
(int)strlen(pszStrValue) > poFldDefn->GetWidth()) |
| 1160 |
{ |
| 1161 |
strcat(pszCommand + nOffset, "NULL"); |
| 1162 |
ReportTruncation(poFldDefn); |
| 1163 |
} |
| 1164 |
else |
| 1165 |
strcat(pszCommand + nOffset, pszStrValue); |
| 1166 |
} |
| 1167 |
else |
| 1168 |
{ |
| 1169 |
int iChar; |
| 1170 |
|
| 1171 |
/* We need to quote and escape string fields. */ |
| 1172 |
strcat(pszCommand + nOffset, "'"); |
| 1173 |
|
| 1174 |
nOffset += strlen(pszCommand + nOffset); |
| 1175 |
|
| 1176 |
for (iChar = 0; pszStrValue[iChar] != '\0'; iChar++) |
| 1177 |
{ |
| 1178 |
if (poFldDefn->GetWidth() != 0 && bPreservePrecision && |
| 1179 |
iChar >= poFldDefn->GetWidth()) |
| 1180 |
{ |
| 1181 |
ReportTruncation(poFldDefn); |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
|
| 1185 |
if (pszStrValue[iChar] == '\'') |
| 1186 |
{ |
| 1187 |
pszCommand[nOffset++] = '\''; |
| 1188 |
pszCommand[nOffset++] = pszStrValue[iChar]; |
| 1189 |
} |
| 1190 |
else |
| 1191 |
pszCommand[nOffset++] = pszStrValue[iChar]; |
| 1192 |
} |
| 1193 |
pszCommand[nOffset] = '\0'; |
| 1194 |
|
| 1195 |
strcat(pszCommand + nOffset, "'"); |
| 1196 |
} |
| 1197 |
nOffset += strlen(pszCommand + nOffset); |
| 1198 |
} |
| 1199 |
|
| 1200 |
strcat(pszCommand + nOffset, ")"); |
| 1201 |
|
| 1202 |
/* -------------------------------------------------------------------- */ |
| 1203 |
/* Prepare statement. */ |
| 1204 |
/* -------------------------------------------------------------------- */ |
| 1205 |
OGROCIStatement oInsert(poSession); |
| 1206 |
int bHaveOrdinates = strstr(pszCommand, ":ordinates") != nullptr; |
| 1207 |
int bHaveElemInfo = strstr(pszCommand, ":elem_info") != nullptr; |
| 1208 |
|
| 1209 |
if (oInsert.Prepare(pszCommand) != CE_None) |
| 1210 |
{ |
| 1211 |
CPLFree(pszCommand); |
| 1212 |
return OGRERR_FAILURE; |
| 1213 |
} |
| 1214 |
|
| 1215 |
CPLFree(pszCommand); |
| 1216 |
|
| 1217 |
/* -------------------------------------------------------------------- */ |
| 1218 |
/* Bind and translate the elem_info if we have some. */ |
| 1219 |
/* -------------------------------------------------------------------- */ |
| 1220 |
if (bHaveElemInfo) |
| 1221 |
{ |
| 1222 |
OCIBind *hBindOrd = nullptr; |
| 1223 |
int i; |
| 1224 |
OCINumber oci_number; |
| 1225 |
|
| 1226 |
// Create or clear VARRAY |
| 1227 |
if (hElemInfoVARRAY == nullptr) |
| 1228 |
{ |
| 1229 |
if (poSession->Failed( |
| 1230 |
OCIObjectNew(poSession->hEnv, poSession->hError, |
| 1231 |
poSession->hSvcCtx, OCI_TYPECODE_VARRAY, |
| 1232 |
poSession->hElemInfoTDO, nullptr, |
| 1233 |
OCI_DURATION_SESSION, FALSE, |
| 1234 |
reinterpret_cast<dvoid **>(&hElemInfoVARRAY)), |
| 1235 |
"OCIObjectNew(hElemInfoVARRAY)")) |
| 1236 |
return OGRERR_FAILURE; |
| 1237 |
} |
| 1238 |
else |
| 1239 |
{ |
| 1240 |
sb4 nOldCount; |
| 1241 |
|
| 1242 |
OCICollSize(poSession->hEnv, poSession->hError, hElemInfoVARRAY, |
| 1243 |
&nOldCount); |
| 1244 |
OCICollTrim(poSession->hEnv, poSession->hError, nOldCount, |
| 1245 |
hElemInfoVARRAY); |
| 1246 |
} |
| 1247 |
|
| 1248 |
// Prepare the VARRAY of ordinate values. |
| 1249 |
for (i = 0; i < nElemInfoCount; i++) |
| 1250 |
{ |
| 1251 |
if (poSession->Failed( |
| 1252 |
OCINumberFromInt(poSession->hError, |
| 1253 |
static_cast<dvoid *>(panElemInfo + i), |
| 1254 |
(uword)sizeof(int), OCI_NUMBER_SIGNED, |
| 1255 |
&oci_number), |
| 1256 |
"OCINumberFromInt")) |
| 1257 |
return OGRERR_FAILURE; |
| 1258 |
|
| 1259 |
if (poSession->Failed( |
| 1260 |
OCICollAppend(poSession->hEnv, poSession->hError, |
| 1261 |
static_cast<dvoid *>(&oci_number), nullptr, |
| 1262 |
hElemInfoVARRAY), |
| 1263 |
"OCICollAppend")) |
| 1264 |
return OGRERR_FAILURE; |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Do the binding. |
| 1268 |
if (poSession->Failed( |
| 1269 |
OCIBindByName( |
| 1270 |
oInsert.GetStatement(), &hBindOrd, poSession->hError, |
| 1271 |
reinterpret_cast<text *>(const_cast<char *>(":elem_info")), |
| 1272 |
(sb4)-1, nullptr, 0, SQLT_NTY, nullptr, nullptr, nullptr, |
| 1273 |
(ub4)0, nullptr, (ub4)OCI_DEFAULT), |
| 1274 |
"OCIBindByName(:elem_info)")) |
| 1275 |
return OGRERR_FAILURE; |
| 1276 |
|
| 1277 |
if (poSession->Failed( |
| 1278 |
OCIBindObject(hBindOrd, poSession->hError, |
| 1279 |
poSession->hElemInfoTDO, |
| 1280 |
reinterpret_cast<dvoid **>(&hElemInfoVARRAY), |
| 1281 |
nullptr, nullptr, nullptr), |
| 1282 |
"OCIBindObject(:elem_info)")) |
| 1283 |
return OGRERR_FAILURE; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* -------------------------------------------------------------------- */ |
| 1287 |
/* Bind and translate the ordinates if we have some. */ |
| 1288 |
/* -------------------------------------------------------------------- */ |
| 1289 |
if (bHaveOrdinates) |
| 1290 |
{ |
| 1291 |
OCIBind *hBindOrd = nullptr; |
| 1292 |
int i; |
| 1293 |
OCINumber oci_number; |
| 1294 |
|
| 1295 |
// Create or clear VARRAY |
| 1296 |
if (hOrdVARRAY == nullptr) |
| 1297 |
{ |
| 1298 |
if (poSession->Failed( |
| 1299 |
OCIObjectNew(poSession->hEnv, poSession->hError, |
| 1300 |
poSession->hSvcCtx, OCI_TYPECODE_VARRAY, |
| 1301 |
poSession->hOrdinatesTDO, nullptr, |
| 1302 |
OCI_DURATION_SESSION, FALSE, |
| 1303 |
reinterpret_cast<dvoid **>(&hOrdVARRAY)), |
| 1304 |
"OCIObjectNew(hOrdVARRAY)")) |
| 1305 |
return OGRERR_FAILURE; |
| 1306 |
} |
| 1307 |
else |
| 1308 |
{ |
| 1309 |
sb4 nOldCount; |
| 1310 |
|
| 1311 |
OCICollSize(poSession->hEnv, poSession->hError, hOrdVARRAY, |
| 1312 |
&nOldCount); |
| 1313 |
OCICollTrim(poSession->hEnv, poSession->hError, nOldCount, |
| 1314 |
hOrdVARRAY); |
| 1315 |
} |
| 1316 |
|
| 1317 |
// Prepare the VARRAY of ordinate values. |
| 1318 |
for (i = 0; i < nOrdinalCount; i++) |
| 1319 |
{ |
| 1320 |
if (poSession->Failed( |
| 1321 |
OCINumberFromReal(poSession->hError, |
| 1322 |
static_cast<dvoid *>(padfOrdinals + i), |
| 1323 |
(uword)sizeof(double), &oci_number), |
| 1324 |
"OCINumberFromReal")) |
| 1325 |
return OGRERR_FAILURE; |
| 1326 |
|
| 1327 |
if (poSession->Failed( |
| 1328 |
OCICollAppend(poSession->hEnv, poSession->hError, |
| 1329 |
(dvoid *)&oci_number, nullptr, hOrdVARRAY), |
| 1330 |
"OCICollAppend")) |
| 1331 |
return OGRERR_FAILURE; |
| 1332 |
} |
| 1333 |
|
| 1334 |
// Do the binding. |
| 1335 |
if (poSession->Failed( |
| 1336 |
OCIBindByName( |
| 1337 |
oInsert.GetStatement(), &hBindOrd, poSession->hError, |
| 1338 |
reinterpret_cast<text *>(const_cast<char *>(":ordinates")), |
| 1339 |
(sb4)-1, nullptr, 0, SQLT_NTY, nullptr, nullptr, nullptr, 0, |
| 1340 |
nullptr, (ub4)OCI_DEFAULT), |
| 1341 |
"OCIBindByName(:ordinates)")) |
| 1342 |
return OGRERR_FAILURE; |
| 1343 |
|
| 1344 |
if (poSession->Failed( |
| 1345 |
OCIBindObject(hBindOrd, poSession->hError, |
| 1346 |
poSession->hOrdinatesTDO, |
| 1347 |
reinterpret_cast<dvoid **>(&hOrdVARRAY), nullptr, |
| 1348 |
nullptr, nullptr), |
| 1349 |
"OCIBindObject(:ordinates)")) |
| 1350 |
return OGRERR_FAILURE; |
| 1351 |
} |
| 1352 |
|
| 1353 |
/* -------------------------------------------------------------------- */ |
| 1354 |
/* Execute the insert. */ |
| 1355 |
/* -------------------------------------------------------------------- */ |
| 1356 |
if (oInsert.Execute(nullptr) != CE_None) |
| 1357 |
return OGRERR_FAILURE; |
| 1358 |
else |
| 1359 |
return OGRERR_NONE; |
| 1360 |
} |
| 1361 |
|
| 1362 |
/************************************************************************/ |
| 1363 |
/* IGetExtent() */ |
| 1364 |
/************************************************************************/ |
| 1365 |
|
| 1366 |
OGRErr OGROCITableLayer::IGetExtent(int iGeomField, OGREnvelope *psExtent, |
| 1367 |
bool bForce) |
| 1368 |
|
| 1369 |
{ |
| 1370 |
CPLAssert(nullptr != psExtent); |
| 1371 |
|
| 1372 |
OGRErr err = OGRERR_FAILURE; |
| 1373 |
|
| 1374 |
if (EQUAL(GetGeometryColumn(), "")) |
| 1375 |
{ |
| 1376 |
return OGRERR_NONE; |
| 1377 |
} |
| 1378 |
|
| 1379 |
/* -------------------------------------------------------------------- */ |
| 1380 |
/* Build query command. */ |
| 1381 |
/* -------------------------------------------------------------------- */ |
| 1382 |
CPLAssert(nullptr != pszGeomName); |
| 1383 |
|
| 1384 |
OGROCIStringBuf oCommand; |
| 1385 |
oCommand.Appendf( |
| 1386 |
1000, |
| 1387 |
"SELECT " |
| 1388 |
"MIN(SDO_GEOM.SDO_MIN_MBR_ORDINATE(t.%s,m.DIMINFO,1)) AS MINX," |
| 1389 |
"MIN(SDO_GEOM.SDO_MIN_MBR_ORDINATE(t.%s,m.DIMINFO,2)) AS MINY," |
| 1390 |
"MAX(SDO_GEOM.SDO_MAX_MBR_ORDINATE(t.%s,m.DIMINFO,1)) AS MAXX," |
| 1391 |
"MAX(SDO_GEOM.SDO_MAX_MBR_ORDINATE(t.%s,m.DIMINFO,2)) AS MAXY " |
| 1392 |
"FROM ALL_SDO_GEOM_METADATA m, ", |
| 1393 |
pszGeomName, pszGeomName, pszGeomName, pszGeomName); |
| 1394 |
|
| 1395 |
if (osOwner != "") |
| 1396 |
{ |
| 1397 |
oCommand.Appendf(500, " %s.%s t ", osOwner.c_str(), |
| 1398 |
osTableName.c_str()); |
| 1399 |
} |
| 1400 |
else |
| 1401 |
{ |
| 1402 |
oCommand.Appendf(500, " %s t ", osTableName.c_str()); |
| 1403 |
} |
| 1404 |
|
| 1405 |
oCommand.Appendf( |
| 1406 |
500, "WHERE m.TABLE_NAME = UPPER('%s') AND m.COLUMN_NAME = UPPER('%s')", |
| 1407 |
osTableName.c_str(), pszGeomName); |
| 1408 |
|
| 1409 |
if (osOwner != "") |
| 1410 |
{ |
| 1411 |
oCommand.Appendf(500, " AND OWNER = UPPER('%s')", osOwner.c_str()); |
| 1412 |
} |
| 1413 |
|
| 1414 |
/* -------------------------------------------------------------------- */ |
| 1415 |
/* Execute query command. */ |
| 1416 |
/* -------------------------------------------------------------------- */ |
| 1417 |
OGROCISession *poSession = poDS->GetSession(); |
| 1418 |
CPLAssert(nullptr != poSession); |
| 1419 |
|
| 1420 |
OGROCIStatement oGetExtent(poSession); |
| 1421 |
|
| 1422 |
if (oGetExtent.Execute(oCommand.GetString()) == CE_None) |
| 1423 |
{ |
| 1424 |
char **papszRow = oGetExtent.SimpleFetchRow(); |
| 1425 |
|
| 1426 |
if (papszRow != nullptr && papszRow[0] != nullptr && |
| 1427 |
papszRow[1] != nullptr && papszRow[2] != nullptr && |
| 1428 |
papszRow[3] != nullptr) |
| 1429 |
{ |
| 1430 |
psExtent->MinX = CPLAtof(papszRow[0]); |
| 1431 |
psExtent->MinY = CPLAtof(papszRow[1]); |
| 1432 |
psExtent->MaxX = CPLAtof(papszRow[2]); |
| 1433 |
psExtent->MaxY = CPLAtof(papszRow[3]); |
| 1434 |
|
| 1435 |
err = OGRERR_NONE; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
/* -------------------------------------------------------------------- */ |
| 1440 |
/* Query spatial extent of layer using default, */ |
| 1441 |
/* but not optimized implementation. */ |
| 1442 |
/* -------------------------------------------------------------------- */ |
| 1443 |
if (err != OGRERR_NONE) |
| 1444 |
{ |
| 1445 |
err = OGRLayer::IGetExtent(iGeomField, psExtent, bForce); |
| 1446 |
CPLDebug("OCI", "Failing to query extent of %s using default GetExtent", |
| 1447 |
osTableName.c_str()); |
| 1448 |
} |
| 1449 |
|
| 1450 |
return err; |
| 1451 |
} |
| 1452 |
|
| 1453 |
/************************************************************************/ |
| 1454 |
/* TestCapability() */ |
| 1455 |
/************************************************************************/ |
| 1456 |
|
| 1457 |
int OGROCITableLayer::TestCapability(const char *pszCap) const |
| 1458 |
|
| 1459 |
{ |
| 1460 |
if (EQUAL(pszCap, OLCSequentialWrite) || EQUAL(pszCap, OLCRandomWrite)) |
| 1461 |
return bUpdateAccess; |
| 1462 |
|
| 1463 |
else if (EQUAL(pszCap, OLCCreateField)) |
| 1464 |
return bUpdateAccess; |
| 1465 |
|
| 1466 |
else |
| 1467 |
return OGROCILayer::TestCapability(pszCap); |
| 1468 |
} |
| 1469 |
|
| 1470 |
/************************************************************************/ |
| 1471 |
/* GetFeatureCount() */ |
| 1472 |
/* */ |
| 1473 |
/* If a spatial filter is in effect, we turn control over to */ |
| 1474 |
/* the generic counter. Otherwise we return the total count. */ |
| 1475 |
/* Eventually we should consider implementing a more efficient */ |
| 1476 |
/* way of counting features matching a spatial query. */ |
| 1477 |
/************************************************************************/ |
| 1478 |
|
| 1479 |
GIntBig OGROCITableLayer::GetFeatureCount(int bForce) |
| 1480 |
|
| 1481 |
{ |
| 1482 |
/* -------------------------------------------------------------------- */ |
| 1483 |
/* Use a more brute force mechanism if we have a spatial query */ |
| 1484 |
/* in play. */ |
| 1485 |
/* -------------------------------------------------------------------- */ |
| 1486 |
if (m_poFilterGeom != nullptr) |
| 1487 |
return OGROCILayer::GetFeatureCount(bForce); |
| 1488 |
|
| 1489 |
/* -------------------------------------------------------------------- */ |
| 1490 |
/* In theory it might be wise to cache this result, but it */ |
| 1491 |
/* won't be trivial to work out the lifetime of the value. */ |
| 1492 |
/* After all someone else could be adding records from another */ |
| 1493 |
/* application when working against a database. */ |
| 1494 |
/* -------------------------------------------------------------------- */ |
| 1495 |
OGROCISession *poSession = poDS->GetSession(); |
| 1496 |
OGROCIStatement oGetCount(poSession); |
| 1497 |
char szCommand[1024]; |
| 1498 |
char **papszResult; |
| 1499 |
|
| 1500 |
snprintf(szCommand, sizeof(szCommand), "SELECT COUNT(*) FROM %s %s", |
| 1501 |
poFeatureDefn->GetName(), pszWHERE); |
| 1502 |
|
| 1503 |
oGetCount.Execute(szCommand); |
| 1504 |
|
| 1505 |
papszResult = oGetCount.SimpleFetchRow(); |
| 1506 |
|
| 1507 |
if (CSLCount(papszResult) < 1) |
| 1508 |
{ |
| 1509 |
CPLDebug("OCI", "Fast get count failed, doing hard way."); |
| 1510 |
return OGROCILayer::GetFeatureCount(bForce); |
| 1511 |
} |
| 1512 |
|
| 1513 |
return CPLAtoGIntBig(papszResult[0]); |
| 1514 |
} |
| 1515 |
|
| 1516 |
/************************************************************************/ |
| 1517 |
/* UpdateLayerExtents() */ |
| 1518 |
/************************************************************************/ |
| 1519 |
|
| 1520 |
void OGROCITableLayer::UpdateLayerExtents() |
| 1521 |
|
| 1522 |
{ |
| 1523 |
if (!bExtentUpdated) |
| 1524 |
return; |
| 1525 |
|
| 1526 |
bExtentUpdated = false; |
| 1527 |
|
| 1528 |
/* -------------------------------------------------------------------- */ |
| 1529 |
/* Do we have existing layer extents we need to merge in to the */ |
| 1530 |
/* ones we collected as we created features? */ |
| 1531 |
/* -------------------------------------------------------------------- */ |
| 1532 |
bool bHaveOldExtent = false; |
| 1533 |
|
| 1534 |
if (!bNewLayer && pszGeomName) |
| 1535 |
{ |
| 1536 |
OGROCIStringBuf oCommand; |
| 1537 |
|
| 1538 |
oCommand.Appendf( |
| 1539 |
1000, |
| 1540 |
"select min(case when r=1 then sdo_lb else null end) minx, " |
| 1541 |
"min(case when r=2 then sdo_lb else null end) miny, " |
| 1542 |
"min(case when r=1 then sdo_ub else null end) maxx, min(case when " |
| 1543 |
"r=2 then sdo_ub else null end) maxy" |
| 1544 |
" from (SELECT d.sdo_dimname, d.sdo_lb, sdo_ub, sdo_tolerance, " |
| 1545 |
"rownum r" |
| 1546 |
" FROM ALL_SDO_GEOM_METADATA m, table(m.diminfo) d" |
| 1547 |
" where m.table_name = UPPER('%s') and m.COLUMN_NAME = UPPER('%s')", |
| 1548 |
osTableName.c_str(), pszGeomName); |
| 1549 |
|
| 1550 |
if (osOwner != "") |
| 1551 |
{ |
| 1552 |
oCommand.Appendf(500, " AND OWNER = UPPER('%s')", osOwner.c_str()); |
| 1553 |
} |
| 1554 |
|
| 1555 |
oCommand.Append(" ) "); |
| 1556 |
|
| 1557 |
OGROCISession *poSession = poDS->GetSession(); |
| 1558 |
CPLAssert(nullptr != poSession); |
| 1559 |
|
| 1560 |
OGROCIStatement oGetExtent(poSession); |
| 1561 |
|
| 1562 |
if (oGetExtent.Execute(oCommand.GetString()) == CE_None) |
| 1563 |
{ |
| 1564 |
char **papszRow = oGetExtent.SimpleFetchRow(); |
| 1565 |
|
| 1566 |
if (papszRow != nullptr && papszRow[0] != nullptr && |
| 1567 |
papszRow[1] != nullptr && papszRow[2] != nullptr && |
| 1568 |
papszRow[3] != nullptr) |
| 1569 |
{ |
| 1570 |
OGREnvelope sOldExtent; |
| 1571 |
|
| 1572 |
bHaveOldExtent = true; |
| 1573 |
|
| 1574 |
sOldExtent.MinX = CPLAtof(papszRow[0]); |
| 1575 |
sOldExtent.MinY = CPLAtof(papszRow[1]); |
| 1576 |
sOldExtent.MaxX = CPLAtof(papszRow[2]); |
| 1577 |
sOldExtent.MaxY = CPLAtof(papszRow[3]); |
| 1578 |
|
| 1579 |
if (sOldExtent.Contains(sExtent)) |
| 1580 |
{ |
| 1581 |
// nothing to do! |
| 1582 |
sExtent = sOldExtent; |
| 1583 |
bExtentUpdated = false; |
| 1584 |
return; |
| 1585 |
} |
| 1586 |
else |
| 1587 |
{ |
| 1588 |
sExtent.Merge(sOldExtent); |
| 1589 |
} |
| 1590 |
} |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
/* -------------------------------------------------------------------- */ |
| 1595 |
/* Establish the extents and resolution to use. */ |
| 1596 |
/* -------------------------------------------------------------------- */ |
| 1597 |
double dfResSize; |
| 1598 |
double dfXMin, dfXMax, dfXRes; |
| 1599 |
double dfYMin, dfYMax, dfYRes; |
| 1600 |
double dfZMin, dfZMax, dfZRes; |
| 1601 |
|
| 1602 |
if (sExtent.MaxX - sExtent.MinX > 400) |
| 1603 |
dfResSize = 0.001; |
| 1604 |
else |
| 1605 |
dfResSize = 0.0000001; |
| 1606 |
|
| 1607 |
dfXMin = sExtent.MinX - dfResSize * 3; |
| 1608 |
dfXMax = sExtent.MaxX + dfResSize * 3; |
| 1609 |
dfXRes = dfResSize; |
| 1610 |
ParseDIMINFO("DIMINFO_X", &dfXMin, &dfXMax, &dfXRes); |
| 1611 |
|
| 1612 |
dfYMin = sExtent.MinY - dfResSize * 3; |
| 1613 |
dfYMax = sExtent.MaxY + dfResSize * 3; |
| 1614 |
dfYRes = dfResSize; |
| 1615 |
ParseDIMINFO("DIMINFO_Y", &dfYMin, &dfYMax, &dfYRes); |
| 1616 |
|
| 1617 |
dfZMin = -100000.0; |
| 1618 |
dfZMax = 100000.0; |
| 1619 |
dfZRes = 0.002; |
| 1620 |
ParseDIMINFO("DIMINFO_Z", &dfZMin, &dfZMax, &dfZRes); |
| 1621 |
|
| 1622 |
/* -------------------------------------------------------------------- */ |
| 1623 |
/* If we already have an extent in the table, we will need to */ |
| 1624 |
/* update it in place. */ |
| 1625 |
/* -------------------------------------------------------------------- */ |
| 1626 |
OGROCIStringBuf sDimUpdate; |
| 1627 |
|
| 1628 |
if (bHaveOldExtent) |
| 1629 |
{ |
| 1630 |
sDimUpdate.Append("UPDATE USER_SDO_GEOM_METADATA "); |
| 1631 |
sDimUpdate.Append("SET DIMINFO ="); |
| 1632 |
sDimUpdate.Append("MDSYS.SDO_DIM_ARRAY("); |
| 1633 |
sDimUpdate.Appendf(200, "MDSYS.SDO_DIM_ELEMENT('X',%.16g,%.16g,%.12g)", |
| 1634 |
dfXMin, dfXMax, dfXRes); |
| 1635 |
sDimUpdate.Appendf(200, ",MDSYS.SDO_DIM_ELEMENT('Y',%.16g,%.16g,%.12g)", |
| 1636 |
dfYMin, dfYMax, dfYRes); |
| 1637 |
|
| 1638 |
if (nDimension == 3) |
| 1639 |
{ |
| 1640 |
sDimUpdate.Appendf(200, |
| 1641 |
",MDSYS.SDO_DIM_ELEMENT('Z',%.16g,%.16g,%.12g)", |
| 1642 |
dfZMin, dfZMax, dfZRes); |
| 1643 |
} |
| 1644 |
|
| 1645 |
sDimUpdate.Appendf( |
| 1646 |
static_cast<int>(strlen(poFeatureDefn->GetName()) + 100), |
| 1647 |
") WHERE TABLE_NAME = '%s'", poFeatureDefn->GetName()); |
| 1648 |
} |
| 1649 |
else |
| 1650 |
{ |
| 1651 |
/* -------------------------------------------------------------------- |
| 1652 |
*/ |
| 1653 |
/* Prepare dimension update statement. */ |
| 1654 |
/* -------------------------------------------------------------------- |
| 1655 |
*/ |
| 1656 |
sDimUpdate.Append("INSERT INTO USER_SDO_GEOM_METADATA VALUES "); |
| 1657 |
sDimUpdate.Appendf( |
| 1658 |
static_cast<int>(strlen(poFeatureDefn->GetName()) + 100), |
| 1659 |
"('%s', '%s', ", poFeatureDefn->GetName(), pszGeomName); |
| 1660 |
|
| 1661 |
sDimUpdate.Append("MDSYS.SDO_DIM_ARRAY("); |
| 1662 |
sDimUpdate.Appendf(200, "MDSYS.SDO_DIM_ELEMENT('X',%.16g,%.16g,%.12g)", |
| 1663 |
dfXMin, dfXMax, dfXRes); |
| 1664 |
sDimUpdate.Appendf(200, ",MDSYS.SDO_DIM_ELEMENT('Y',%.16g,%.16g,%.12g)", |
| 1665 |
dfYMin, dfYMax, dfYRes); |
| 1666 |
|
| 1667 |
if (nDimension == 3) |
| 1668 |
{ |
| 1669 |
sDimUpdate.Appendf(200, |
| 1670 |
",MDSYS.SDO_DIM_ELEMENT('Z',%.16g,%.16g,%.12g)", |
| 1671 |
dfZMin, dfZMax, dfZRes); |
| 1672 |
} |
| 1673 |
|
| 1674 |
if (nSRID == -1) |
| 1675 |
sDimUpdate.Append("), NULL)"); |
| 1676 |
else |
| 1677 |
sDimUpdate.Appendf(100, "), %d)", nSRID); |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* -------------------------------------------------------------------- */ |
| 1681 |
/* Run the update/insert command. */ |
| 1682 |
/* -------------------------------------------------------------------- */ |
| 1683 |
OGROCIStatement oExecStatement(poDS->GetSession()); |
| 1684 |
|
| 1685 |
oExecStatement.Execute(sDimUpdate.GetString()); |
| 1686 |
} |
| 1687 |
|
| 1688 |
/************************************************************************/ |
| 1689 |
/* AllocAndBindForWrite() */ |
| 1690 |
/************************************************************************/ |
| 1691 |
|
| 1692 |
int OGROCITableLayer::AllocAndBindForWrite() |
| 1693 |
|
| 1694 |
{ |
| 1695 |
OGROCISession *poSession = poDS->GetSession(); |
| 1696 |
int i; |
| 1697 |
|
| 1698 |
CPLAssert(nWriteCacheMax == 0); |
| 1699 |
|
| 1700 |
/* -------------------------------------------------------------------- */ |
| 1701 |
/* Decide on the number of rows we want to be able to cache at */ |
| 1702 |
/* a time. */ |
| 1703 |
/* -------------------------------------------------------------------- */ |
| 1704 |
nWriteCacheMax = nMultiLoadCount; |
| 1705 |
|
| 1706 |
/* -------------------------------------------------------------------- */ |
| 1707 |
/* Collect the INSERT statement. */ |
| 1708 |
/* -------------------------------------------------------------------- */ |
| 1709 |
OGROCIStringBuf oCmdBuf; |
| 1710 |
|
| 1711 |
oCmdBuf.Append("INSERT /*+ APPEND */ INTO \""); |
| 1712 |
oCmdBuf.Append(poFeatureDefn->GetName()); |
| 1713 |
oCmdBuf.Append("\"(\""); |
| 1714 |
oCmdBuf.Append(pszFIDName); |
| 1715 |
|
| 1716 |
if (GetGeomType() != wkbNone) |
| 1717 |
{ |
| 1718 |
oCmdBuf.Append("\",\""); |
| 1719 |
oCmdBuf.Append(pszGeomName); |
| 1720 |
} |
| 1721 |
|
| 1722 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1723 |
{ |
| 1724 |
oCmdBuf.Append("\",\""); |
| 1725 |
oCmdBuf.Append(poFeatureDefn->GetFieldDefn(i)->GetNameRef()); |
| 1726 |
} |
| 1727 |
|
| 1728 |
oCmdBuf.Append("\") VALUES ( :fid "); |
| 1729 |
|
| 1730 |
if (GetGeomType() != wkbNone) |
| 1731 |
oCmdBuf.Append(", :geometry"); |
| 1732 |
|
| 1733 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1734 |
{ |
| 1735 |
oCmdBuf.Append(", "); |
| 1736 |
oCmdBuf.Appendf(20, " :field_%d", i); |
| 1737 |
} |
| 1738 |
|
| 1739 |
oCmdBuf.Append(") "); |
| 1740 |
|
| 1741 |
/* -------------------------------------------------------------------- */ |
| 1742 |
/* Bind and Prepare it. */ |
| 1743 |
/* -------------------------------------------------------------------- */ |
| 1744 |
poBoundStatement = new OGROCIStatement(poSession); |
| 1745 |
poBoundStatement->Prepare(oCmdBuf.GetString()); |
| 1746 |
|
| 1747 |
/* -------------------------------------------------------------------- */ |
| 1748 |
/* Setup geometry indicator information. */ |
| 1749 |
/* -------------------------------------------------------------------- */ |
| 1750 |
if (GetGeomType() != wkbNone) |
| 1751 |
{ |
| 1752 |
pasWriteGeomInd = (SDO_GEOMETRY_ind *)CPLCalloc( |
| 1753 |
sizeof(SDO_GEOMETRY_ind), nWriteCacheMax); |
| 1754 |
|
| 1755 |
papsWriteGeomIndMap = (SDO_GEOMETRY_ind **)CPLCalloc( |
| 1756 |
sizeof(SDO_GEOMETRY_ind *), nWriteCacheMax); |
| 1757 |
|
| 1758 |
for (i = 0; i < nWriteCacheMax; i++) |
| 1759 |
papsWriteGeomIndMap[i] = pasWriteGeomInd + i; |
| 1760 |
|
| 1761 |
/* -------------------------------------------------------------------- |
| 1762 |
*/ |
| 1763 |
/* Setup all the required geometry objects, and the */ |
| 1764 |
/* corresponding indicator map. */ |
| 1765 |
/* -------------------------------------------------------------------- |
| 1766 |
*/ |
| 1767 |
pasWriteGeoms = (SDO_GEOMETRY_TYPE *)CPLCalloc( |
| 1768 |
sizeof(SDO_GEOMETRY_TYPE), nWriteCacheMax); |
| 1769 |
papsWriteGeomMap = (SDO_GEOMETRY_TYPE **)CPLCalloc( |
| 1770 |
sizeof(SDO_GEOMETRY_TYPE *), nWriteCacheMax); |
| 1771 |
|
| 1772 |
for (i = 0; i < nWriteCacheMax; i++) |
| 1773 |
papsWriteGeomMap[i] = pasWriteGeoms + i; |
| 1774 |
|
| 1775 |
/* -------------------------------------------------------------------- |
| 1776 |
*/ |
| 1777 |
/* Allocate VARRAYs for the elem_info and ordinates. */ |
| 1778 |
/* -------------------------------------------------------------------- |
| 1779 |
*/ |
| 1780 |
for (i = 0; i < nWriteCacheMax; i++) |
| 1781 |
{ |
| 1782 |
if (poSession->Failed( |
| 1783 |
OCIObjectNew(poSession->hEnv, poSession->hError, |
| 1784 |
poSession->hSvcCtx, OCI_TYPECODE_VARRAY, |
| 1785 |
poSession->hElemInfoTDO, nullptr, |
| 1786 |
OCI_DURATION_SESSION, FALSE, |
| 1787 |
reinterpret_cast<dvoid **>( |
| 1788 |
&(pasWriteGeoms[i].sdo_elem_info))), |
| 1789 |
"OCIObjectNew(elem_info)")) |
| 1790 |
return FALSE; |
| 1791 |
|
| 1792 |
if (poSession->Failed( |
| 1793 |
OCIObjectNew(poSession->hEnv, poSession->hError, |
| 1794 |
poSession->hSvcCtx, OCI_TYPECODE_VARRAY, |
| 1795 |
poSession->hOrdinatesTDO, nullptr, |
| 1796 |
OCI_DURATION_SESSION, FALSE, |
| 1797 |
reinterpret_cast<dvoid **>( |
| 1798 |
&(pasWriteGeoms[i].sdo_ordinates))), |
| 1799 |
"OCIObjectNew(ordinates)")) |
| 1800 |
return FALSE; |
| 1801 |
} |
| 1802 |
|
| 1803 |
/* -------------------------------------------------------------------- |
| 1804 |
*/ |
| 1805 |
/* Bind the geometry column. */ |
| 1806 |
/* -------------------------------------------------------------------- |
| 1807 |
*/ |
| 1808 |
if (poBoundStatement->BindObject( |
| 1809 |
":geometry", papsWriteGeomMap, poSession->hGeometryTDO, |
| 1810 |
(void **)papsWriteGeomIndMap) != CE_None) |
| 1811 |
return FALSE; |
| 1812 |
} |
| 1813 |
|
| 1814 |
/* -------------------------------------------------------------------- */ |
| 1815 |
/* Bind the FID column. */ |
| 1816 |
/* -------------------------------------------------------------------- */ |
| 1817 |
panWriteFIDs = (int *)CPLMalloc(sizeof(int) * nWriteCacheMax); |
| 1818 |
|
| 1819 |
if (poBoundStatement->BindScalar(":fid", panWriteFIDs, sizeof(int), |
| 1820 |
SQLT_INT) != CE_None) |
| 1821 |
return FALSE; |
| 1822 |
|
| 1823 |
/* -------------------------------------------------------------------- */ |
| 1824 |
/* Allocate each of the column data bind arrays. */ |
| 1825 |
/* -------------------------------------------------------------------- */ |
| 1826 |
|
| 1827 |
papWriteFields = |
| 1828 |
(void **)CPLMalloc(sizeof(void *) * poFeatureDefn->GetFieldCount()); |
| 1829 |
papaeWriteFieldInd = |
| 1830 |
(OCIInd **)CPLCalloc(sizeof(OCIInd *), poFeatureDefn->GetFieldCount()); |
| 1831 |
|
| 1832 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1833 |
{ |
| 1834 |
OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i); |
| 1835 |
char szFieldPlaceholderName[80]; |
| 1836 |
|
| 1837 |
snprintf(szFieldPlaceholderName, sizeof(szFieldPlaceholderName), |
| 1838 |
":field_%d", i); |
| 1839 |
|
| 1840 |
papaeWriteFieldInd[i] = |
| 1841 |
(OCIInd *)CPLCalloc(sizeof(OCIInd), nWriteCacheMax); |
| 1842 |
|
| 1843 |
if (poFldDefn->GetType() == OFTInteger) |
| 1844 |
{ |
| 1845 |
papWriteFields[i] = (void *)CPLCalloc(sizeof(int), nWriteCacheMax); |
| 1846 |
|
| 1847 |
if (poBoundStatement->BindScalar( |
| 1848 |
szFieldPlaceholderName, papWriteFields[i], sizeof(int), |
| 1849 |
SQLT_INT, papaeWriteFieldInd[i]) != CE_None) |
| 1850 |
return FALSE; |
| 1851 |
} |
| 1852 |
else if (poFldDefn->GetType() == OFTInteger64) |
| 1853 |
{ |
| 1854 |
papWriteFields[i] = |
| 1855 |
(void *)CPLCalloc(sizeof(GIntBig), nWriteCacheMax); |
| 1856 |
|
| 1857 |
if (poBoundStatement->BindScalar( |
| 1858 |
szFieldPlaceholderName, papWriteFields[i], sizeof(GIntBig), |
| 1859 |
SQLT_INT, papaeWriteFieldInd[i]) != CE_None) |
| 1860 |
return FALSE; |
| 1861 |
} |
| 1862 |
else if (poFldDefn->GetType() == OFTReal) |
| 1863 |
{ |
| 1864 |
papWriteFields[i] = |
| 1865 |
(void *)CPLCalloc(sizeof(double), nWriteCacheMax); |
| 1866 |
|
| 1867 |
if (poBoundStatement->BindScalar( |
| 1868 |
szFieldPlaceholderName, papWriteFields[i], sizeof(double), |
| 1869 |
SQLT_FLT, papaeWriteFieldInd[i]) != CE_None) |
| 1870 |
return FALSE; |
| 1871 |
} |
| 1872 |
else |
| 1873 |
{ |
| 1874 |
int nEachBufSize = nDefaultStringSize + 1; |
| 1875 |
|
| 1876 |
if (poFldDefn->GetType() == OFTString && poFldDefn->GetWidth() != 0) |
| 1877 |
nEachBufSize = poFldDefn->GetWidth() + 1; |
| 1878 |
|
| 1879 |
papWriteFields[i] = (void *)CPLCalloc(nEachBufSize, nWriteCacheMax); |
| 1880 |
|
| 1881 |
if (poBoundStatement->BindScalar( |
| 1882 |
szFieldPlaceholderName, papWriteFields[i], nEachBufSize, |
| 1883 |
SQLT_STR, papaeWriteFieldInd[i]) != CE_None) |
| 1884 |
return FALSE; |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
return TRUE; |
| 1889 |
} |
| 1890 |
|
| 1891 |
/************************************************************************/ |
| 1892 |
/* BoundCreateFeature() */ |
| 1893 |
/************************************************************************/ |
| 1894 |
|
| 1895 |
OGRErr OGROCITableLayer::BoundCreateFeature(OGRFeature *poFeature) |
| 1896 |
|
| 1897 |
{ |
| 1898 |
OGROCISession *poSession = poDS->GetSession(); |
| 1899 |
int iCache, i; |
| 1900 |
OGRErr eErr; |
| 1901 |
OCINumber oci_number; |
| 1902 |
|
| 1903 |
/* If an unset field has a default value, the current implementation */ |
| 1904 |
/* of BoundCreateFeature() doesn't work. */ |
| 1905 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 1906 |
{ |
| 1907 |
if (!poFeature->IsFieldSetAndNotNull(i) && |
| 1908 |
poFeature->GetFieldDefnRef(i)->GetDefault() != nullptr) |
| 1909 |
{ |
| 1910 |
FlushPendingFeatures(); |
| 1911 |
return UnboundCreateFeature(poFeature); |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
if (!poFeature->Validate(OGR_F_VAL_NULL | OGR_F_VAL_ALLOW_NULL_WHEN_DEFAULT, |
| 1916 |
TRUE)) |
| 1917 |
return OGRERR_FAILURE; |
| 1918 |
|
| 1919 |
iCache = nWriteCacheUsed; |
| 1920 |
|
| 1921 |
/* -------------------------------------------------------------------- */ |
| 1922 |
/* Initiate the Insert */ |
| 1923 |
/* -------------------------------------------------------------------- */ |
| 1924 |
if (nWriteCacheMax == 0) |
| 1925 |
{ |
| 1926 |
if (!AllocAndBindForWrite()) |
| 1927 |
return OGRERR_FAILURE; |
| 1928 |
} |
| 1929 |
|
| 1930 |
/* -------------------------------------------------------------------- */ |
| 1931 |
/* Set the geometry */ |
| 1932 |
/* -------------------------------------------------------------------- */ |
| 1933 |
if (poFeature->GetGeometryRef() != nullptr) |
| 1934 |
{ |
| 1935 |
SDO_GEOMETRY_TYPE *psGeom = pasWriteGeoms + iCache; |
| 1936 |
SDO_GEOMETRY_ind *psInd = pasWriteGeomInd + iCache; |
| 1937 |
OGRGeometry *poGeometry = poFeature->GetGeometryRef(); |
| 1938 |
int nGType; |
| 1939 |
|
| 1940 |
psInd->_atomic = OCI_IND_NOTNULL; |
| 1941 |
|
| 1942 |
if (nSRID == -1) |
| 1943 |
psInd->sdo_srid = OCI_IND_NULL; |
| 1944 |
else |
| 1945 |
{ |
| 1946 |
psInd->sdo_srid = OCI_IND_NOTNULL; |
| 1947 |
OCINumberFromInt(poSession->hError, &nSRID, (uword)sizeof(int), |
| 1948 |
OCI_NUMBER_SIGNED, &(psGeom->sdo_srid)); |
| 1949 |
} |
| 1950 |
|
| 1951 |
/* special more efficient case for simple points */ |
| 1952 |
if (wkbFlatten(poGeometry->getGeometryType()) == wkbPoint) |
| 1953 |
{ |
| 1954 |
OGRPoint *poPoint = poGeometry->toPoint(); |
| 1955 |
double dfValue; |
| 1956 |
|
| 1957 |
psInd->sdo_point._atomic = OCI_IND_NOTNULL; |
| 1958 |
psInd->sdo_elem_info = OCI_IND_NULL; |
| 1959 |
psInd->sdo_ordinates = OCI_IND_NULL; |
| 1960 |
|
| 1961 |
dfValue = poPoint->getX(); |
| 1962 |
OCINumberFromReal(poSession->hError, &dfValue, |
| 1963 |
(uword)sizeof(double), &(psGeom->sdo_point.x)); |
| 1964 |
|
| 1965 |
dfValue = poPoint->getY(); |
| 1966 |
OCINumberFromReal(poSession->hError, &dfValue, |
| 1967 |
(uword)sizeof(double), &(psGeom->sdo_point.y)); |
| 1968 |
|
| 1969 |
if (nDimension == 2) |
| 1970 |
{ |
| 1971 |
nGType = 2001; |
| 1972 |
psInd->sdo_point.z = OCI_IND_NULL; |
| 1973 |
} |
| 1974 |
else |
| 1975 |
{ |
| 1976 |
nGType = 3001; |
| 1977 |
psInd->sdo_point.z = OCI_IND_NOTNULL; |
| 1978 |
|
| 1979 |
dfValue = poPoint->getZ(); |
| 1980 |
OCINumberFromReal(poSession->hError, &dfValue, |
| 1981 |
(uword)sizeof(double), |
| 1982 |
&(psGeom->sdo_point.z)); |
| 1983 |
} |
| 1984 |
} |
| 1985 |
else |
| 1986 |
{ |
| 1987 |
psInd->sdo_point._atomic = OCI_IND_NULL; |
| 1988 |
psInd->sdo_elem_info = OCI_IND_NOTNULL; |
| 1989 |
psInd->sdo_ordinates = OCI_IND_NOTNULL; |
| 1990 |
|
| 1991 |
eErr = TranslateToSDOGeometry(poFeature->GetGeometryRef(), &nGType); |
| 1992 |
|
| 1993 |
if (eErr != OGRERR_NONE) |
| 1994 |
return eErr; |
| 1995 |
|
| 1996 |
/* Clear the existing eleminfo and ordinates arrays */ |
| 1997 |
sb4 nOldCount; |
| 1998 |
|
| 1999 |
OCICollSize(poSession->hEnv, poSession->hError, |
| 2000 |
psGeom->sdo_elem_info, &nOldCount); |
| 2001 |
OCICollTrim(poSession->hEnv, poSession->hError, nOldCount, |
| 2002 |
psGeom->sdo_elem_info); |
| 2003 |
|
| 2004 |
OCICollSize(poSession->hEnv, poSession->hError, |
| 2005 |
psGeom->sdo_ordinates, &nOldCount); |
| 2006 |
OCICollTrim(poSession->hEnv, poSession->hError, nOldCount, |
| 2007 |
psGeom->sdo_ordinates); |
| 2008 |
|
| 2009 |
// Prepare the VARRAY of element values. |
| 2010 |
for (i = 0; i < nElemInfoCount; i++) |
| 2011 |
{ |
| 2012 |
OCINumberFromInt( |
| 2013 |
poSession->hError, static_cast<dvoid *>(panElemInfo + i), |
| 2014 |
(uword)sizeof(int), OCI_NUMBER_SIGNED, &oci_number); |
| 2015 |
|
| 2016 |
OCICollAppend(poSession->hEnv, poSession->hError, |
| 2017 |
static_cast<dvoid *>(&oci_number), nullptr, |
| 2018 |
psGeom->sdo_elem_info); |
| 2019 |
} |
| 2020 |
|
| 2021 |
// Prepare the VARRAY of ordinate values. |
| 2022 |
for (i = 0; i < nOrdinalCount; i++) |
| 2023 |
{ |
| 2024 |
OCINumberFromReal(poSession->hError, |
| 2025 |
static_cast<dvoid *>(padfOrdinals + i), |
| 2026 |
(uword)sizeof(double), &oci_number); |
| 2027 |
OCICollAppend(poSession->hEnv, poSession->hError, |
| 2028 |
static_cast<dvoid *>(&oci_number), nullptr, |
| 2029 |
psGeom->sdo_ordinates); |
| 2030 |
} |
| 2031 |
} |
| 2032 |
|
| 2033 |
psInd->sdo_gtype = OCI_IND_NOTNULL; |
| 2034 |
OCINumberFromInt(poSession->hError, &nGType, (uword)sizeof(int), |
| 2035 |
OCI_NUMBER_SIGNED, &(psGeom->sdo_gtype)); |
| 2036 |
} |
| 2037 |
else if (pasWriteGeomInd != nullptr) |
| 2038 |
{ |
| 2039 |
SDO_GEOMETRY_ind *psInd = pasWriteGeomInd + iCache; |
| 2040 |
psInd->_atomic = OCI_IND_NULL; |
| 2041 |
psInd->sdo_srid = OCI_IND_NULL; |
| 2042 |
psInd->sdo_point._atomic = OCI_IND_NULL; |
| 2043 |
psInd->sdo_elem_info = OCI_IND_NULL; |
| 2044 |
psInd->sdo_ordinates = OCI_IND_NULL; |
| 2045 |
psInd->sdo_gtype = OCI_IND_NULL; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* -------------------------------------------------------------------- */ |
| 2049 |
/* Set the FID. */ |
| 2050 |
/* -------------------------------------------------------------------- */ |
| 2051 |
if (poFeature->GetFID() == OGRNullFID) |
| 2052 |
{ |
| 2053 |
if (iNextFIDToWrite < 0) |
| 2054 |
{ |
| 2055 |
iNextFIDToWrite = GetMaxFID() + 1; |
| 2056 |
} |
| 2057 |
|
| 2058 |
poFeature->SetFID(iNextFIDToWrite++); |
| 2059 |
} |
| 2060 |
|
| 2061 |
panWriteFIDs[iCache] = static_cast<int>(poFeature->GetFID()); |
| 2062 |
|
| 2063 |
/* -------------------------------------------------------------------- */ |
| 2064 |
/* Set the other fields. */ |
| 2065 |
/* -------------------------------------------------------------------- */ |
| 2066 |
for (i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 2067 |
{ |
| 2068 |
if (!poFeature->IsFieldSetAndNotNull(i)) |
| 2069 |
{ |
| 2070 |
papaeWriteFieldInd[i][iCache] = OCI_IND_NULL; |
| 2071 |
continue; |
| 2072 |
} |
| 2073 |
|
| 2074 |
papaeWriteFieldInd[i][iCache] = OCI_IND_NOTNULL; |
| 2075 |
|
| 2076 |
OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i); |
| 2077 |
|
| 2078 |
if (poFldDefn->GetType() == OFTInteger) |
| 2079 |
((int *)(papWriteFields[i]))[iCache] = |
| 2080 |
poFeature->GetFieldAsInteger(i); |
| 2081 |
|
| 2082 |
else if (poFldDefn->GetType() == OFTInteger64) |
| 2083 |
((GIntBig *)(papWriteFields[i]))[iCache] = |
| 2084 |
poFeature->GetFieldAsInteger64(i); |
| 2085 |
|
| 2086 |
else if (poFldDefn->GetType() == OFTReal) |
| 2087 |
((double *)(papWriteFields[i]))[iCache] = |
| 2088 |
poFeature->GetFieldAsDouble(i); |
| 2089 |
|
| 2090 |
else |
| 2091 |
{ |
| 2092 |
int nLen = 1; |
| 2093 |
int nEachBufSize = nDefaultStringSize + 1; |
| 2094 |
const char *pszStrValue = poFeature->GetFieldAsString(i); |
| 2095 |
|
| 2096 |
if (poFldDefn->GetType() == OFTString && poFldDefn->GetWidth() != 0) |
| 2097 |
nEachBufSize = poFldDefn->GetWidth() + 1; |
| 2098 |
|
| 2099 |
nLen = static_cast<int>(strlen(pszStrValue)); |
| 2100 |
if (nLen > nEachBufSize - 1) |
| 2101 |
nLen = nEachBufSize - 1; |
| 2102 |
|
| 2103 |
char *pszTarget = |
| 2104 |
((char *)papWriteFields[i]) + iCache * nEachBufSize; |
| 2105 |
strncpy(pszTarget, pszStrValue, nLen); |
| 2106 |
pszTarget[nLen] = '\0'; |
| 2107 |
|
| 2108 |
if (poFldDefn->GetType() == OFTDateTime && |
| 2109 |
cpl::contains(setFieldIndexWithTimeStampWithTZ, i)) |
| 2110 |
{ |
| 2111 |
const auto *psField = poFeature->GetRawFieldRef(i); |
| 2112 |
int nTZHour = 0; |
| 2113 |
int nTZMin = 0; |
| 2114 |
if (psField->Date.TZFlag > OGR_TZFLAG_MIXED_TZ) |
| 2115 |
{ |
| 2116 |
const int nOffset = |
| 2117 |
(psField->Date.TZFlag - OGR_TZFLAG_UTC) * 15; |
| 2118 |
nTZHour = |
| 2119 |
static_cast<int>(nOffset / 60); // Round towards zero. |
| 2120 |
nTZMin = std::abs(nOffset - nTZHour * 60); |
| 2121 |
} |
| 2122 |
else |
| 2123 |
{ |
| 2124 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 2125 |
"DateTime %s has no time zone whereas it should " |
| 2126 |
"have. Assuming +00:00", |
| 2127 |
pszTarget); |
| 2128 |
} |
| 2129 |
CPLsnprintf(pszTarget, nEachBufSize, |
| 2130 |
"%04d-%02d-%02d %02d:%02d:%06.3f %s%02d%02d", |
| 2131 |
psField->Date.Year, psField->Date.Month, |
| 2132 |
psField->Date.Day, psField->Date.Hour, |
| 2133 |
psField->Date.Minute, psField->Date.Second, |
| 2134 |
(psField->Date.TZFlag <= OGR_TZFLAG_MIXED_TZ || |
| 2135 |
psField->Date.TZFlag >= OGR_TZFLAG_UTC) |
| 2136 |
? "+" |
| 2137 |
: "-", |
| 2138 |
std::abs(nTZHour), nTZMin); |
| 2139 |
} |
| 2140 |
else if (poFldDefn->GetType() == OFTDateTime) |
| 2141 |
{ |
| 2142 |
const auto *psField = poFeature->GetRawFieldRef(i); |
| 2143 |
if (psField->Date.TZFlag > OGR_TZFLAG_MIXED_TZ) |
| 2144 |
{ |
| 2145 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 2146 |
"DateTime %s has a time zone whereas the target " |
| 2147 |
"field does not support time zone. Time zone will " |
| 2148 |
"be dropped.", |
| 2149 |
pszTarget); |
| 2150 |
} |
| 2151 |
CPLsnprintf(pszTarget, nEachBufSize, |
| 2152 |
"%04d-%02d-%02d %02d:%02d:%06.3f", |
| 2153 |
psField->Date.Year, psField->Date.Month, |
| 2154 |
psField->Date.Day, psField->Date.Hour, |
| 2155 |
psField->Date.Minute, psField->Date.Second); |
| 2156 |
} |
| 2157 |
} |
| 2158 |
} |
| 2159 |
|
| 2160 |
/* -------------------------------------------------------------------- */ |
| 2161 |
/* Do we need to flush out a full set of rows? */ |
| 2162 |
/* -------------------------------------------------------------------- */ |
| 2163 |
nWriteCacheUsed++; |
| 2164 |
|
| 2165 |
if (nWriteCacheUsed == nWriteCacheMax) |
| 2166 |
return FlushPendingFeatures(); |
| 2167 |
else |
| 2168 |
return OGRERR_NONE; |
| 2169 |
} |
| 2170 |
|
| 2171 |
/************************************************************************/ |
| 2172 |
/* FlushPendingFeatures() */ |
| 2173 |
/************************************************************************/ |
| 2174 |
|
| 2175 |
OGRErr OGROCITableLayer::FlushPendingFeatures() |
| 2176 |
|
| 2177 |
{ |
| 2178 |
OGROCISession *poSession = poDS->GetSession(); |
| 2179 |
|
| 2180 |
if (nWriteCacheUsed > 0) |
| 2181 |
{ |
| 2182 |
CPLDebug("OCI", "Flushing %d features on layer %s", nWriteCacheUsed, |
| 2183 |
poFeatureDefn->GetName()); |
| 2184 |
|
| 2185 |
if (poSession->Failed( |
| 2186 |
OCIStmtExecute(poSession->hSvcCtx, |
| 2187 |
poBoundStatement->GetStatement(), |
| 2188 |
poSession->hError, (ub4)nWriteCacheUsed, (ub4)0, |
| 2189 |
(OCISnapshot *)nullptr, (OCISnapshot *)nullptr, |
| 2190 |
(ub4)OCI_COMMIT_ON_SUCCESS), |
| 2191 |
"OCIStmtExecute")) |
| 2192 |
{ |
| 2193 |
nWriteCacheUsed = 0; |
| 2194 |
return OGRERR_FAILURE; |
| 2195 |
} |
| 2196 |
else |
| 2197 |
{ |
| 2198 |
nWriteCacheUsed = 0; |
| 2199 |
return OGRERR_NONE; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
else |
| 2203 |
return OGRERR_NONE; |
| 2204 |
} |
| 2205 |
|
| 2206 |
/************************************************************************/ |
| 2207 |
/* SyncToDisk() */ |
| 2208 |
/* */ |
| 2209 |
/* Perhaps we should also be putting the metadata into a */ |
| 2210 |
/* usable state? */ |
| 2211 |
/************************************************************************/ |
| 2212 |
|
| 2213 |
OGRErr OGROCITableLayer::SyncToDisk() |
| 2214 |
|
| 2215 |
{ |
| 2216 |
OGRErr eErr = FlushPendingFeatures(); |
| 2217 |
|
| 2218 |
UpdateLayerExtents(); |
| 2219 |
|
| 2220 |
CreateSpatialIndex(); |
| 2221 |
|
| 2222 |
bNewLayer = FALSE; |
| 2223 |
|
| 2224 |
return eErr; |
| 2225 |
} |
| 2226 |
|
| 2227 |
/************************************************************************/ |
| 2228 |
/* CreateSpatialIndex() */ |
| 2229 |
/************************************************************************/ |
| 2230 |
|
| 2231 |
void OGROCITableLayer::CreateSpatialIndex() |
| 2232 |
|
| 2233 |
{ |
| 2234 |
/* -------------------------------------------------------------------- */ |
| 2235 |
/* For new layers we try to create a spatial index. */ |
| 2236 |
/* -------------------------------------------------------------------- */ |
| 2237 |
if (bNewLayer && sExtent.IsInit()) |
| 2238 |
{ |
| 2239 |
/* -------------------------------------------------------------------- |
| 2240 |
*/ |
| 2241 |
/* If the user has disabled INDEX support then don't create the */ |
| 2242 |
/* index. */ |
| 2243 |
/* -------------------------------------------------------------------- |
| 2244 |
*/ |
| 2245 |
if (!CPLFetchBool(papszOptions, "SPATIAL_INDEX", true) || |
| 2246 |
!CPLFetchBool(papszOptions, "INDEX", true)) |
| 2247 |
return; |
| 2248 |
|
| 2249 |
/* -------------------------------------------------------------------- |
| 2250 |
*/ |
| 2251 |
/* Establish an index name. For some reason Oracle 8.1.7 does */ |
| 2252 |
/* not support spatial index names longer than 18 characters so */ |
| 2253 |
/* we magic up an index name if it would be too long. */ |
| 2254 |
/* -------------------------------------------------------------------- |
| 2255 |
*/ |
| 2256 |
char szIndexName[20]; |
| 2257 |
|
| 2258 |
if (strlen(poFeatureDefn->GetName()) < 15) |
| 2259 |
snprintf(szIndexName, sizeof(szIndexName), "%s_idx", |
| 2260 |
poFeatureDefn->GetName()); |
| 2261 |
else if (strlen(poFeatureDefn->GetName()) < 17) |
| 2262 |
snprintf(szIndexName, sizeof(szIndexName), "%si", |
| 2263 |
poFeatureDefn->GetName()); |
| 2264 |
else |
| 2265 |
{ |
| 2266 |
int i, nHash = 0; |
| 2267 |
const char *pszSrcName = poFeatureDefn->GetName(); |
| 2268 |
|
| 2269 |
for (i = 0; pszSrcName[i] != '\0'; i++) |
| 2270 |
nHash = (nHash + i * pszSrcName[i]) % 987651; |
| 2271 |
|
| 2272 |
snprintf(szIndexName, sizeof(szIndexName), "OSI_%d", nHash); |
| 2273 |
} |
| 2274 |
|
| 2275 |
poDS->GetSession()->CleanName(szIndexName); |
| 2276 |
|
| 2277 |
/* -------------------------------------------------------------------- |
| 2278 |
*/ |
| 2279 |
/* Try creating an index on the table now. Use a simple 5 */ |
| 2280 |
/* level quadtree based index. Would R-tree be a better default? |
| 2281 |
*/ |
| 2282 |
/* -------------------------------------------------------------------- |
| 2283 |
*/ |
| 2284 |
OGROCIStringBuf sIndexCmd; |
| 2285 |
OGROCIStatement oExecStatement(poDS->GetSession()); |
| 2286 |
|
| 2287 |
sIndexCmd.Appendf(10000, |
| 2288 |
"CREATE INDEX \"%s\" ON %s(\"%s\") " |
| 2289 |
"INDEXTYPE IS MDSYS.SPATIAL_INDEX ", |
| 2290 |
szIndexName, poFeatureDefn->GetName(), pszGeomName); |
| 2291 |
|
| 2292 |
int bAddLayerGType = CPLTestBool(CSLFetchNameValueDef( |
| 2293 |
papszOptions, "ADD_LAYER_GTYPE", "YES")) && |
| 2294 |
GetGeomType() != wkbUnknown; |
| 2295 |
|
| 2296 |
CPLString osParams( |
| 2297 |
CSLFetchNameValueDef(papszOptions, "INDEX_PARAMETERS", "")); |
| 2298 |
if (bAddLayerGType || !osParams.empty()) |
| 2299 |
{ |
| 2300 |
sIndexCmd.Append(" PARAMETERS( '"); |
| 2301 |
if (!osParams.empty()) |
| 2302 |
sIndexCmd.Append(osParams.c_str()); |
| 2303 |
if (bAddLayerGType && |
| 2304 |
osParams.ifind("LAYER_GTYPE") == std::string::npos) |
| 2305 |
{ |
| 2306 |
if (!osParams.empty()) |
| 2307 |
sIndexCmd.Append(", "); |
| 2308 |
sIndexCmd.Append("LAYER_GTYPE="); |
| 2309 |
if (wkbFlatten(GetGeomType()) == wkbPoint) |
| 2310 |
sIndexCmd.Append("POINT"); |
| 2311 |
else if (wkbFlatten(GetGeomType()) == wkbLineString) |
| 2312 |
sIndexCmd.Append("LINE"); |
| 2313 |
else if (wkbFlatten(GetGeomType()) == wkbPolygon) |
| 2314 |
sIndexCmd.Append("POLYGON"); |
| 2315 |
else if (wkbFlatten(GetGeomType()) == wkbMultiPoint) |
| 2316 |
sIndexCmd.Append("MULTIPOINT"); |
| 2317 |
else if (wkbFlatten(GetGeomType()) == wkbMultiLineString) |
| 2318 |
sIndexCmd.Append("MULTILINE"); |
| 2319 |
else if (wkbFlatten(GetGeomType()) == wkbMultiPolygon) |
| 2320 |
sIndexCmd.Append("MULTIPOLYGON"); |
| 2321 |
else |
| 2322 |
sIndexCmd.Append("COLLECTION"); |
| 2323 |
} |
| 2324 |
sIndexCmd.Append("' )"); |
| 2325 |
} |
| 2326 |
|
| 2327 |
if (oExecStatement.Execute(sIndexCmd.GetString()) != CE_None) |
| 2328 |
{ |
| 2329 |
CPLString osDropCommand; |
| 2330 |
osDropCommand.Printf("DROP INDEX \"%s\"", szIndexName); |
| 2331 |
oExecStatement.Execute(osDropCommand); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
} |
| 2335 |
|
| 2336 |
int OGROCITableLayer::GetMaxFID() |
| 2337 |
{ |
| 2338 |
if (nFirstId > 0) |
| 2339 |
return nFirstId - 1; |
| 2340 |
|
| 2341 |
if (pszFIDName == nullptr) |
| 2342 |
return 0; |
| 2343 |
|
| 2344 |
OGROCIStringBuf sCmd; |
| 2345 |
OGROCIStatement oSelect(poDS->GetSession()); |
| 2346 |
|
| 2347 |
sCmd.Appendf(10000, "SELECT MAX(\"%s\") FROM \"%s\"", pszFIDName, |
| 2348 |
poFeatureDefn->GetName()); |
| 2349 |
|
| 2350 |
oSelect.Execute(sCmd.GetString()); |
| 2351 |
|
| 2352 |
char **papszResult = oSelect.SimpleFetchRow(); |
| 2353 |
return CSLCount(papszResult) == 1 ? atoi(papszResult[0]) : 0; |
| 2354 |
} |