| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: Implements OGRIDBTableLayer class, access to an existing table |
| 5 |
* (based on ODBC and PG drivers). |
| 6 |
* Author: Oleg Semykin, [email protected] |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2006, Oleg Semykin |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
****************************************************************************/ |
| 13 |
|
| 14 |
#include "cpl_conv.h" |
| 15 |
#include "cpl_string.h" |
| 16 |
#include "ogr_idb.h" |
| 17 |
|
| 18 |
/************************************************************************/ |
| 19 |
/* OGRIDBTableLayer() */ |
| 20 |
/************************************************************************/ |
| 21 |
|
| 22 |
OGRIDBTableLayer::OGRIDBTableLayer(OGRIDBDataSource *poDSIn) |
| 23 |
|
| 24 |
{ |
| 25 |
poDS = poDSIn; |
| 26 |
|
| 27 |
pszQuery = nullptr; |
| 28 |
|
| 29 |
bUpdateAccess = TRUE; |
| 30 |
bHaveSpatialExtents = FALSE; |
| 31 |
|
| 32 |
iNextShapeId = 0; |
| 33 |
|
| 34 |
poFeatureDefn = nullptr; |
| 35 |
} |
| 36 |
|
| 37 |
/************************************************************************/ |
| 38 |
/* ~OGRIDBTableLayer() */ |
| 39 |
/************************************************************************/ |
| 40 |
|
| 41 |
OGRIDBTableLayer::~OGRIDBTableLayer() |
| 42 |
|
| 43 |
{ |
| 44 |
CPLFree(pszQuery); |
| 45 |
ClearQuery(); |
| 46 |
} |
| 47 |
|
| 48 |
/************************************************************************/ |
| 49 |
/* Initialize() */ |
| 50 |
/************************************************************************/ |
| 51 |
|
| 52 |
CPLErr OGRIDBTableLayer::Initialize(const char *pszTableName, |
| 53 |
const char *pszGeomCol, int bUpdate) |
| 54 |
|
| 55 |
{ |
| 56 |
bUpdateAccess = bUpdate; |
| 57 |
|
| 58 |
ITConnection *poConn = poDS->GetConnection(); |
| 59 |
|
| 60 |
if (pszFIDColumn) |
| 61 |
{ |
| 62 |
CPLFree(pszFIDColumn); |
| 63 |
pszFIDColumn = nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
/* -------------------------------------------------------------------- */ |
| 67 |
/* Do we have a simple primary key? */ |
| 68 |
/* -------------------------------------------------------------------- */ |
| 69 |
if (pszFIDColumn == nullptr) |
| 70 |
{ |
| 71 |
ITCursor oGetKey(*poConn); |
| 72 |
|
| 73 |
CPLString osSql = " select sc.colname" |
| 74 |
" from syscolumns sc, sysindexes si, systables st" |
| 75 |
" where st.tabid = si.tabid" |
| 76 |
" and st.tabid = sc.tabid" |
| 77 |
" and si.idxtype = 'U'" |
| 78 |
" and sc.colno = si.part1" |
| 79 |
" and si.part2 = 0" // only one-column keys |
| 80 |
" and st.tabname='"; |
| 81 |
osSql += pszTableName; |
| 82 |
osSql += "'"; |
| 83 |
|
| 84 |
if (oGetKey.Prepare(osSql.c_str()) && oGetKey.Open(ITCursor::ReadOnly)) |
| 85 |
{ |
| 86 |
ITValue *poVal = oGetKey.Fetch(); |
| 87 |
if (poVal && poVal->IsNull() == false) |
| 88 |
{ |
| 89 |
pszFIDColumn = CPLStrdup(poVal->Printable()); |
| 90 |
poVal->Release(); |
| 91 |
} |
| 92 |
|
| 93 |
if (oGetKey.Fetch()) // more than one field in key! |
| 94 |
{ |
| 95 |
CPLFree(pszFIDColumn); |
| 96 |
pszFIDColumn = nullptr; |
| 97 |
|
| 98 |
CPLDebug("OGR_IDB", |
| 99 |
"Table %s has multiple primary key fields," |
| 100 |
" ignoring them all.", |
| 101 |
pszTableName); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/* -------------------------------------------------------------------- */ |
| 107 |
/* Have we been provided a geometry column? */ |
| 108 |
/* -------------------------------------------------------------------- */ |
| 109 |
CPLFree(pszGeomColumn); |
| 110 |
if (pszGeomCol == nullptr) |
| 111 |
pszGeomColumn = nullptr; |
| 112 |
else |
| 113 |
pszGeomColumn = CPLStrdup(pszGeomCol); |
| 114 |
|
| 115 |
/* -------------------------------------------------------------------- */ |
| 116 |
/* Get the column definitions for this table. */ |
| 117 |
/* -------------------------------------------------------------------- */ |
| 118 |
ITCursor oGetCol(*poConn); |
| 119 |
CPLErr eErr; |
| 120 |
|
| 121 |
CPLString sql; |
| 122 |
sql.Printf("select * from %s where 1=0", pszTableName); |
| 123 |
if (!oGetCol.Prepare(sql.c_str()) || !oGetCol.Open(ITCursor::ReadOnly)) |
| 124 |
return CE_Failure; |
| 125 |
|
| 126 |
eErr = BuildFeatureDefn(pszTableName, &oGetCol); |
| 127 |
if (eErr != CE_None) |
| 128 |
return eErr; |
| 129 |
|
| 130 |
if (poFeatureDefn->GetFieldCount() == 0) |
| 131 |
{ |
| 132 |
CPLError( |
| 133 |
CE_Failure, CPLE_AppDefined, |
| 134 |
"No column definitions found for table '%s', layer not usable.", |
| 135 |
pszTableName); |
| 136 |
return CE_Failure; |
| 137 |
} |
| 138 |
|
| 139 |
/* -------------------------------------------------------------------- */ |
| 140 |
/* Do we have XMIN, YMIN, XMAX, YMAX extent fields? */ |
| 141 |
/* -------------------------------------------------------------------- */ |
| 142 |
if (poFeatureDefn->GetFieldIndex("XMIN") != -1 && |
| 143 |
poFeatureDefn->GetFieldIndex("XMAX") != -1 && |
| 144 |
poFeatureDefn->GetFieldIndex("YMIN") != -1 && |
| 145 |
poFeatureDefn->GetFieldIndex("YMAX") != -1) |
| 146 |
{ |
| 147 |
bHaveSpatialExtents = TRUE; |
| 148 |
CPLDebug("OGR_IDB", "Table %s has geometry extent fields.", |
| 149 |
pszTableName); |
| 150 |
} |
| 151 |
|
| 152 |
/* -------------------------------------------------------------------- */ |
| 153 |
/* If we got a geometry column, does it exist? Is it binary? */ |
| 154 |
/* -------------------------------------------------------------------- */ |
| 155 |
if (pszGeomColumn != nullptr) |
| 156 |
{ |
| 157 |
int iColumn = oGetCol.RowType()->ColumnId(pszGeomColumn); |
| 158 |
if (iColumn < 0) |
| 159 |
{ |
| 160 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 161 |
"Column %s requested for geometry, but it does not exist.", |
| 162 |
pszGeomColumn); |
| 163 |
CPLFree(pszGeomColumn); |
| 164 |
pszGeomColumn = nullptr; |
| 165 |
} |
| 166 |
bGeomColumnWKB = TRUE; |
| 167 |
/*else |
| 168 |
{ |
| 169 |
if( ITCursor::GetTypeMapping( |
| 170 |
oGetCol.GetColType( iColumn )) == SQL_C_BINARY ) |
| 171 |
bGeomColumnWKB = TRUE; |
| 172 |
}*/ |
| 173 |
} |
| 174 |
|
| 175 |
return CE_None; |
| 176 |
} |
| 177 |
|
| 178 |
/************************************************************************/ |
| 179 |
/* ClearQuery() */ |
| 180 |
/************************************************************************/ |
| 181 |
|
| 182 |
void OGRIDBTableLayer::ClearQuery() |
| 183 |
|
| 184 |
{ |
| 185 |
if (m_poCurr != nullptr) |
| 186 |
{ |
| 187 |
m_poCurr->Close(); |
| 188 |
delete m_poCurr; |
| 189 |
m_poCurr = nullptr; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/************************************************************************/ |
| 194 |
/* GetQuery() */ |
| 195 |
/************************************************************************/ |
| 196 |
|
| 197 |
ITCursor *OGRIDBTableLayer::GetQuery() |
| 198 |
|
| 199 |
{ |
| 200 |
if (m_poCurr == nullptr) |
| 201 |
ResetQuery(); |
| 202 |
|
| 203 |
return m_poCurr; |
| 204 |
} |
| 205 |
|
| 206 |
/************************************************************************/ |
| 207 |
/* ResetQuery() */ |
| 208 |
/************************************************************************/ |
| 209 |
|
| 210 |
OGRErr OGRIDBTableLayer::ResetQuery() |
| 211 |
|
| 212 |
{ |
| 213 |
ClearQuery(); |
| 214 |
|
| 215 |
iNextShapeId = 0; |
| 216 |
|
| 217 |
m_poCurr = new ITCursor(*poDS->GetConnection()); |
| 218 |
|
| 219 |
// Create list of fields |
| 220 |
CPLString osFields; |
| 221 |
|
| 222 |
if (pszGeomColumn) |
| 223 |
{ |
| 224 |
if (!osFields.empty()) |
| 225 |
osFields += ","; |
| 226 |
|
| 227 |
osFields += "st_asbinary("; |
| 228 |
osFields += pszGeomColumn; |
| 229 |
osFields += ") as "; |
| 230 |
osFields += pszGeomColumn; |
| 231 |
} |
| 232 |
|
| 233 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 234 |
{ |
| 235 |
if (!osFields.empty()) |
| 236 |
osFields += ","; |
| 237 |
|
| 238 |
osFields += poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 239 |
} |
| 240 |
|
| 241 |
CPLString sql; |
| 242 |
|
| 243 |
sql += "SELECT "; |
| 244 |
sql += osFields; |
| 245 |
sql += " FROM "; |
| 246 |
sql += poFeatureDefn->GetName(); |
| 247 |
|
| 248 |
/* Append attribute query if we have it */ |
| 249 |
if (pszQuery != nullptr) |
| 250 |
{ |
| 251 |
sql += " WHERE "; |
| 252 |
sql += pszQuery; |
| 253 |
} |
| 254 |
|
| 255 |
/* If we have a spatial filter, and per record extents, query on it */ |
| 256 |
if (m_poFilterGeom != nullptr && bHaveSpatialExtents) |
| 257 |
{ |
| 258 |
if (pszQuery == nullptr) |
| 259 |
sql += " WHERE"; |
| 260 |
else |
| 261 |
sql += " AND"; |
| 262 |
|
| 263 |
CPLString sqlTmp; |
| 264 |
sqlTmp.Printf("%s XMAX > %.8f AND XMIN < %.8f" |
| 265 |
" AND YMAX > %.8f AND YMIN < %.8f", |
| 266 |
sql.c_str(), m_sFilterEnvelope.MinX, |
| 267 |
m_sFilterEnvelope.MaxX, m_sFilterEnvelope.MinY, |
| 268 |
m_sFilterEnvelope.MaxY); |
| 269 |
sql = sqlTmp; |
| 270 |
} |
| 271 |
/* If we have a spatial filter and GeomColumn, query using st_intersects |
| 272 |
* function */ |
| 273 |
else if (m_poFilterGeom != nullptr && pszGeomColumn) |
| 274 |
{ |
| 275 |
if (pszQuery == nullptr) |
| 276 |
sql += " WHERE"; |
| 277 |
else |
| 278 |
sql += " AND"; |
| 279 |
|
| 280 |
CPLString sqlTmp; |
| 281 |
sqlTmp.Printf("%s st_intersects(st_geomfromtext('POLYGON((%.8f %.8f, " |
| 282 |
"%.8f %.8f, %.8f %.8f, %.8f %.8f, %.8f %.8f))',0),%s)", |
| 283 |
sql.c_str(), m_sFilterEnvelope.MinX, |
| 284 |
m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxX, |
| 285 |
m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxX, |
| 286 |
m_sFilterEnvelope.MaxY, m_sFilterEnvelope.MinX, |
| 287 |
m_sFilterEnvelope.MaxY, m_sFilterEnvelope.MinX, |
| 288 |
m_sFilterEnvelope.MinY, pszGeomColumn); |
| 289 |
sql = sqlTmp; |
| 290 |
} |
| 291 |
|
| 292 |
CPLDebug("OGR_IDB", "Exec(%s)", sql.c_str()); |
| 293 |
if (m_poCurr->Prepare(sql.c_str()) && m_poCurr->Open(ITCursor::ReadOnly)) |
| 294 |
{ |
| 295 |
return OGRERR_NONE; |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
delete m_poCurr; |
| 300 |
m_poCurr = nullptr; |
| 301 |
return OGRERR_FAILURE; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/************************************************************************/ |
| 306 |
/* ResetReading() */ |
| 307 |
/************************************************************************/ |
| 308 |
|
| 309 |
void OGRIDBTableLayer::ResetReading() |
| 310 |
|
| 311 |
{ |
| 312 |
ClearQuery(); |
| 313 |
OGRIDBLayer::ResetReading(); |
| 314 |
} |
| 315 |
|
| 316 |
/************************************************************************/ |
| 317 |
/* GetFeature() */ |
| 318 |
/************************************************************************/ |
| 319 |
|
| 320 |
OGRFeature *OGRIDBTableLayer::GetFeature(GIntBig nFeatureId) |
| 321 |
|
| 322 |
{ |
| 323 |
if (pszFIDColumn == nullptr) |
| 324 |
return OGRIDBLayer::GetFeature(nFeatureId); |
| 325 |
|
| 326 |
ClearQuery(); |
| 327 |
|
| 328 |
iNextShapeId = nFeatureId; |
| 329 |
|
| 330 |
m_poCurr = new ITCursor(*poDS->GetConnection()); |
| 331 |
|
| 332 |
// Create list of fields |
| 333 |
CPLString osFields; |
| 334 |
|
| 335 |
if (poFeatureDefn->GetFieldIndex(pszFIDColumn) == -1) |
| 336 |
osFields += pszFIDColumn; |
| 337 |
|
| 338 |
if (pszGeomColumn) |
| 339 |
{ |
| 340 |
if (!osFields.empty()) |
| 341 |
osFields += ","; |
| 342 |
|
| 343 |
osFields += "st_asbinary("; |
| 344 |
osFields += pszGeomColumn; |
| 345 |
osFields += ") as "; |
| 346 |
osFields += pszGeomColumn; |
| 347 |
} |
| 348 |
|
| 349 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 350 |
{ |
| 351 |
if (!osFields.empty()) |
| 352 |
osFields += ","; |
| 353 |
|
| 354 |
osFields += poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 355 |
} |
| 356 |
|
| 357 |
CPLString sql; |
| 358 |
|
| 359 |
sql.Printf("SELECT %s FROM %s WHERE %s = " CPL_FRMT_GIB, osFields.c_str(), |
| 360 |
poFeatureDefn->GetName(), pszFIDColumn, nFeatureId); |
| 361 |
|
| 362 |
CPLDebug("OGR_IDB", "ExecuteSQL(%s)", sql.c_str()); |
| 363 |
if (!m_poCurr->Prepare(sql.c_str()) || !m_poCurr->Open(ITCursor::ReadOnly)) |
| 364 |
{ |
| 365 |
delete m_poCurr; |
| 366 |
m_poCurr = nullptr; |
| 367 |
return nullptr; |
| 368 |
} |
| 369 |
|
| 370 |
return GetNextRawFeature(); |
| 371 |
} |
| 372 |
|
| 373 |
/************************************************************************/ |
| 374 |
/* SetAttributeFilter() */ |
| 375 |
/************************************************************************/ |
| 376 |
|
| 377 |
OGRErr OGRIDBTableLayer::SetAttributeFilter(const char *pszQueryIn) |
| 378 |
|
| 379 |
{ |
| 380 |
CPLFree(m_pszAttrQueryString); |
| 381 |
m_pszAttrQueryString = (pszQueryIn) ? CPLStrdup(pszQueryIn) : nullptr; |
| 382 |
|
| 383 |
if ((pszQueryIn == nullptr && this->pszQuery == nullptr) || |
| 384 |
(pszQueryIn != nullptr && this->pszQuery != nullptr && |
| 385 |
EQUAL(pszQueryIn, this->pszQuery))) |
| 386 |
return OGRERR_NONE; |
| 387 |
|
| 388 |
CPLFree(this->pszQuery); |
| 389 |
this->pszQuery = CPLStrdup(pszQueryIn); |
| 390 |
|
| 391 |
ClearQuery(); |
| 392 |
|
| 393 |
return OGRERR_NONE; |
| 394 |
} |
| 395 |
|
| 396 |
/************************************************************************/ |
| 397 |
/* TestCapability() */ |
| 398 |
/************************************************************************/ |
| 399 |
|
| 400 |
int OGRIDBTableLayer::TestCapability(const char *pszCap) const |
| 401 |
|
| 402 |
{ |
| 403 |
if (EQUAL(pszCap, OLCSequentialWrite) || EQUAL(pszCap, OLCRandomWrite)) |
| 404 |
return bUpdateAccess; |
| 405 |
|
| 406 |
else if (EQUAL(pszCap, OLCRandomRead)) |
| 407 |
return TRUE; |
| 408 |
|
| 409 |
else |
| 410 |
return OGRIDBLayer::TestCapability(pszCap); |
| 411 |
} |
| 412 |
|
| 413 |
/************************************************************************/ |
| 414 |
/* GetFeatureCount() */ |
| 415 |
/* */ |
| 416 |
/* If a spatial filter is in effect, we turn control over to */ |
| 417 |
/* the generic counter. Otherwise we return the total count. */ |
| 418 |
/* Eventually we should consider implementing a more efficient */ |
| 419 |
/* way of counting features matching a spatial query. */ |
| 420 |
/************************************************************************/ |
| 421 |
|
| 422 |
GIntBig OGRIDBTableLayer::GetFeatureCount(int bForce) |
| 423 |
|
| 424 |
{ |
| 425 |
return OGRIDBLayer::GetFeatureCount(bForce); |
| 426 |
} |
| 427 |
|
| 428 |
/************************************************************************/ |
| 429 |
/* GetSpatialRef() */ |
| 430 |
/* */ |
| 431 |
/* We override this to try and fetch the table SRID from the */ |
| 432 |
/* geometry_columns table if the srsid is -2 (meaning we */ |
| 433 |
/* haven't yet even looked for it). */ |
| 434 |
/************************************************************************/ |
| 435 |
|
| 436 |
const OGRSpatialReference *OGRIDBTableLayer::GetSpatialRef() const |
| 437 |
|
| 438 |
{ |
| 439 |
if (nSRSId == -2) |
| 440 |
{ |
| 441 |
nSRSId = -1; |
| 442 |
|
| 443 |
if (!pszGeomColumn) |
| 444 |
return nullptr; |
| 445 |
|
| 446 |
CPLString osCmd; |
| 447 |
osCmd.Printf(" SELECT FIRST 1 srid, trim(srtext)" |
| 448 |
" FROM spatial_ref_sys, %s" |
| 449 |
" WHERE srid = ST_Srid(%s) ", |
| 450 |
poFeatureDefn->GetName(), pszGeomColumn); |
| 451 |
|
| 452 |
ITCursor oSridCur(*poDS->GetConnection()); |
| 453 |
|
| 454 |
if (oSridCur.Prepare(osCmd.c_str()) && |
| 455 |
oSridCur.Open(ITCursor::ReadOnly)) |
| 456 |
{ |
| 457 |
ITRow *row = static_cast<ITRow *>(oSridCur.NextRow()); |
| 458 |
if (row && !row->IsNull()) |
| 459 |
{ |
| 460 |
nSRSId = atoi(row->Column(0)->Printable()); |
| 461 |
const char *wkt = row->Column(1)->Printable(); |
| 462 |
|
| 463 |
if (poSRS) |
| 464 |
{ |
| 465 |
// Hmm ... it should be null |
| 466 |
delete poSRS; |
| 467 |
} |
| 468 |
poSRS = new OGRSpatialReference(); |
| 469 |
poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
| 470 |
if (poSRS->importFromWkt(wkt) != OGRERR_NONE) |
| 471 |
{ |
| 472 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 473 |
"Error parse srs wkt: %s", wkt); |
| 474 |
delete poSRS; |
| 475 |
poSRS = nullptr; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
return OGRIDBLayer::GetSpatialRef(); |
| 482 |
} |
| 483 |
|
| 484 |
#if 0 |
| 485 |
OGRErr OGRIDBTableLayer::ISetFeature( OGRFeature *poFeature ) |
| 486 |
{ |
| 487 |
OGRErr eErr(OGRERR_FAILURE); |
| 488 |
|
| 489 |
if ( ! bUpdateAccess ) |
| 490 |
{ |
| 491 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 492 |
"Error update feature. Layer is read only." ); |
| 493 |
return eErr; |
| 494 |
} |
| 495 |
|
| 496 |
if( nullptr == poFeature ) |
| 497 |
{ |
| 498 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 499 |
"NULL pointer to OGRFeature passed to SetFeature()." ); |
| 500 |
return eErr; |
| 501 |
} |
| 502 |
|
| 503 |
if( poFeature->GetFID() == OGRNullFID ) |
| 504 |
{ |
| 505 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 506 |
"FID required on features given to SetFeature()." ); |
| 507 |
return eErr; |
| 508 |
} |
| 509 |
|
| 510 |
ITStatement oQuery( *poDS->GetConnection() ); |
| 511 |
|
| 512 |
int bUpdateGeom = TRUE; |
| 513 |
OGRwkbGeometryType nGeomType = poFeature->GetGeometryRef()->getGeometryType(); |
| 514 |
CPLString osGeomFunc; |
| 515 |
int nSrid = 0; // FIXME Obtain geometry SRID |
| 516 |
|
| 517 |
switch (nGeomType) |
| 518 |
{ |
| 519 |
case wkbPoint: |
| 520 |
osGeomFunc = "ST_PointFromText"; |
| 521 |
break; |
| 522 |
case wkbLineString: |
| 523 |
osGeomFunc = "ST_LineFromText"; |
| 524 |
break; |
| 525 |
case wkbPolygon: |
| 526 |
osGeomFunc = "ST_PolyFromText"; |
| 527 |
break; |
| 528 |
case wkbMultiPoint: |
| 529 |
osGeomFunc = "ST_MPointFromText"; |
| 530 |
break; |
| 531 |
case wkbMultiLineString: |
| 532 |
osGeomFunc = "ST_MLineFromText"; |
| 533 |
break; |
| 534 |
case wkbMultiPolygon: |
| 535 |
osGeomFunc = "ST_MPolyFromText"; |
| 536 |
break; |
| 537 |
default: |
| 538 |
bUpdateGeom = FALSE; |
| 539 |
CPLDebug("OGR_IDB", "SetFeature(): Unknown geometry type. Geometry will not be updated."); |
| 540 |
} |
| 541 |
|
| 542 |
// Create query |
| 543 |
CPLString osSql; |
| 544 |
CPLString osFields; |
| 545 |
|
| 546 |
if ( pszGeomColumn && bUpdateGeom ) |
| 547 |
{ |
| 548 |
osFields.Printf( "%s = %s( ?, %d )", pszGeomColumn, osGeomFunc.c_str(), nSrid ); |
| 549 |
} |
| 550 |
|
| 551 |
for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) |
| 552 |
{ |
| 553 |
const char * pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 554 |
|
| 555 |
// skip fid column from update |
| 556 |
if ( EQUAL( pszFIDColumn, pszFieldName ) ) |
| 557 |
continue; |
| 558 |
|
| 559 |
if ( ! osFields.empty() ) |
| 560 |
{ |
| 561 |
osFields += ","; |
| 562 |
} |
| 563 |
|
| 564 |
osFields += pszFieldName; |
| 565 |
osFields += "=?"; |
| 566 |
} |
| 567 |
|
| 568 |
osSql.Printf( "UPDATE %s SET %s WHERE %s = %d", |
| 569 |
poFeatureDefn->GetName(), |
| 570 |
osFields.c_str(), |
| 571 |
pszFIDColumn, |
| 572 |
poFeature->GetFID() ); |
| 573 |
|
| 574 |
if ( ! oQuery.Prepare( osSql.c_str() ) ) |
| 575 |
{ |
| 576 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 577 |
"Error prepare SQL.\n%s",osSql.c_str() ); |
| 578 |
return eErr; |
| 579 |
} |
| 580 |
|
| 581 |
int iParam = 0; |
| 582 |
|
| 583 |
if ( pszGeomColumn && bUpdateGeom ) |
| 584 |
{ |
| 585 |
ITValue * par = oQuery.Param( iParam ); // it should be a geom value |
| 586 |
if ( ! par ) |
| 587 |
{ |
| 588 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 589 |
"Error prepare geom param"); |
| 590 |
return eErr; |
| 591 |
} |
| 592 |
|
| 593 |
OGRGeometry * poGeom = poFeature->GetGeometryRef(); |
| 594 |
char * wkt; |
| 595 |
poGeom->exportToWkt( &wkt ); |
| 596 |
|
| 597 |
if( ! par->FromPrintable( wkt ) ) |
| 598 |
{ |
| 599 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 600 |
"Error prepare geom param"); |
| 601 |
par->Release(); |
| 602 |
return eErr; |
| 603 |
} |
| 604 |
|
| 605 |
CPLFree( wkt ); |
| 606 |
par->Release(); |
| 607 |
|
| 608 |
iParam++; |
| 609 |
} |
| 610 |
|
| 611 |
for ( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) |
| 612 |
{ |
| 613 |
ITValue * par = oQuery.Param( iParam ); |
| 614 |
if ( ! par ) |
| 615 |
{ |
| 616 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 617 |
"Error prepare param %d", iParam); |
| 618 |
return eErr; |
| 619 |
} |
| 620 |
|
| 621 |
if ( ! poFeature->IsFieldSetAndNotNull( i ) ) |
| 622 |
{ |
| 623 |
if ( ! par->SetNull() ) |
| 624 |
{ |
| 625 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 626 |
"Error set param %d to NULL", iParam); |
| 627 |
par->Release(); |
| 628 |
return eErr; |
| 629 |
} |
| 630 |
par->Release(); |
| 631 |
continue; |
| 632 |
} |
| 633 |
|
| 634 |
ITConversions * cv = 0; |
| 635 |
bool res = FALSE; |
| 636 |
|
| 637 |
if ( par->QueryInterface( ITConversionsIID, (void **) &cv) != |
| 638 |
IT_QUERYINTERFACE_SUCCESS ) |
| 639 |
{ |
| 640 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 641 |
"Error prepare param %d", iParam); |
| 642 |
par->Release(); |
| 643 |
return eErr; |
| 644 |
} |
| 645 |
|
| 646 |
switch ( poFeatureDefn->GetFieldDefn( i )->GetType() ) |
| 647 |
{ |
| 648 |
case OFTInteger: |
| 649 |
res = cv->ConvertFrom( poFeature->GetFieldAsInteger( i ) ); |
| 650 |
break; |
| 651 |
|
| 652 |
case OFTReal: |
| 653 |
res = cv->ConvertFrom( poFeature->GetFieldAsDouble( i ) ); |
| 654 |
break; |
| 655 |
|
| 656 |
case OFTIntegerList: |
| 657 |
case OFTRealList: |
| 658 |
case OFTStringList: |
| 659 |
// FIXME Prepare array of values field |
| 660 |
//cv->ConvertFrom( poFeature->GetFieldAsStringList( i ) ); |
| 661 |
//break; |
| 662 |
|
| 663 |
case OFTBinary: |
| 664 |
// FIXME Prepare binary field |
| 665 |
|
| 666 |
case OFTString: |
| 667 |
case OFTDate: |
| 668 |
case OFTTime: |
| 669 |
case OFTDateTime: |
| 670 |
res = cv->ConvertFrom( poFeature->GetFieldAsString( i ) ); |
| 671 |
break; |
| 672 |
default: |
| 673 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 674 |
"Error prepare param %d. Unknown data type.", iParam); |
| 675 |
cv->Release(); |
| 676 |
par->Release(); |
| 677 |
return eErr; |
| 678 |
} |
| 679 |
if ( res != TRUE ) |
| 680 |
CPLError( CE_Failure, CPLE_AppDefined, |
| 681 |
"Error prepare param."); |
| 682 |
cv->Release(); |
| 683 |
par->Release(); |
| 684 |
} |
| 685 |
|
| 686 |
CPLDebug( "OGR_IDB", "ExecuteSQL(%s)", oQuery.QueryText().Data() ); |
| 687 |
if( !oQuery.Exec() ) |
| 688 |
{ |
| 689 |
CPLError( CE_Failure, CPLE_AppDefined, "Error update Feature."); |
| 690 |
return eErr; |
| 691 |
} |
| 692 |
|
| 693 |
return OGRERR_NONE; |
| 694 |
} |
| 695 |
|
| 696 |
#endif |
| 697 |
|
| 698 |
OGRErr OGRIDBTableLayer::ISetFeature(OGRFeature *poFeature) |
| 699 |
{ |
| 700 |
OGRErr eErr(OGRERR_FAILURE); |
| 701 |
|
| 702 |
if (!bUpdateAccess) |
| 703 |
{ |
| 704 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 705 |
"Error update feature. Layer is read only."); |
| 706 |
return eErr; |
| 707 |
} |
| 708 |
|
| 709 |
if (nullptr == poFeature) |
| 710 |
{ |
| 711 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 712 |
"NULL pointer to OGRFeature passed to SetFeature()."); |
| 713 |
return eErr; |
| 714 |
} |
| 715 |
|
| 716 |
if (poFeature->GetFID() == OGRNullFID) |
| 717 |
{ |
| 718 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 719 |
"FID required on features given to SetFeature()."); |
| 720 |
return eErr; |
| 721 |
} |
| 722 |
|
| 723 |
ITStatement oQuery(*poDS->GetConnection()); |
| 724 |
|
| 725 |
int bUpdateGeom = TRUE; |
| 726 |
CPLString osGeomFunc; |
| 727 |
|
| 728 |
if (poFeature->GetGeometryRef()) |
| 729 |
{ |
| 730 |
OGRwkbGeometryType nGeomType = |
| 731 |
poFeature->GetGeometryRef()->getGeometryType(); |
| 732 |
|
| 733 |
switch (nGeomType) |
| 734 |
{ |
| 735 |
case wkbPoint: |
| 736 |
osGeomFunc = "ST_PointFromText"; |
| 737 |
break; |
| 738 |
case wkbLineString: |
| 739 |
osGeomFunc = "ST_LineFromText"; |
| 740 |
break; |
| 741 |
case wkbPolygon: |
| 742 |
osGeomFunc = "ST_PolyFromText"; |
| 743 |
break; |
| 744 |
case wkbMultiPoint: |
| 745 |
osGeomFunc = "ST_MPointFromText"; |
| 746 |
break; |
| 747 |
case wkbMultiLineString: |
| 748 |
osGeomFunc = "ST_MLineFromText"; |
| 749 |
break; |
| 750 |
case wkbMultiPolygon: |
| 751 |
osGeomFunc = "ST_MPolyFromText"; |
| 752 |
break; |
| 753 |
default: |
| 754 |
bUpdateGeom = FALSE; |
| 755 |
CPLDebug("OGR_IDB", "SetFeature(): Unknown geometry type. " |
| 756 |
"Geometry will not be updated."); |
| 757 |
} |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
bUpdateGeom = FALSE; |
| 762 |
} |
| 763 |
|
| 764 |
// Create query |
| 765 |
CPLString osSql; |
| 766 |
CPLString osFields; |
| 767 |
|
| 768 |
if (pszGeomColumn && bUpdateGeom) |
| 769 |
{ |
| 770 |
OGRGeometry *poGeom = poFeature->GetGeometryRef(); |
| 771 |
char *wkt; |
| 772 |
poGeom->exportToWkt(&wkt); |
| 773 |
|
| 774 |
osFields.Printf("%s = %s( '%s', %d )", pszGeomColumn, |
| 775 |
osGeomFunc.c_str(), wkt, nSRSId); |
| 776 |
|
| 777 |
CPLFree(wkt); |
| 778 |
} |
| 779 |
|
| 780 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 781 |
{ |
| 782 |
const char *pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 783 |
|
| 784 |
// skip fid column from update |
| 785 |
if (EQUAL(pszFIDColumn, pszFieldName)) |
| 786 |
continue; |
| 787 |
|
| 788 |
if (!osFields.empty()) |
| 789 |
{ |
| 790 |
osFields += ","; |
| 791 |
} |
| 792 |
|
| 793 |
osFields += pszFieldName; |
| 794 |
osFields += "="; |
| 795 |
|
| 796 |
if (!poFeature->IsFieldSetAndNotNull(i)) |
| 797 |
{ |
| 798 |
osFields += "NULL"; |
| 799 |
continue; |
| 800 |
} |
| 801 |
|
| 802 |
CPLString osVal; |
| 803 |
|
| 804 |
switch (poFeatureDefn->GetFieldDefn(i)->GetType()) |
| 805 |
{ |
| 806 |
case OFTInteger: |
| 807 |
osVal.Printf("%d", poFeature->GetFieldAsInteger(i)); |
| 808 |
break; |
| 809 |
|
| 810 |
case OFTReal: |
| 811 |
if (poFeatureDefn->GetFieldDefn(i)->GetPrecision()) |
| 812 |
{ |
| 813 |
// have a decimal format width.precision |
| 814 |
CPLString osFormatString; |
| 815 |
osFormatString.Printf( |
| 816 |
"%%%d.%df", poFeatureDefn->GetFieldDefn(i)->GetWidth(), |
| 817 |
poFeatureDefn->GetFieldDefn(i)->GetPrecision()); |
| 818 |
osVal.Printf(osFormatString.c_str(), |
| 819 |
poFeature->GetFieldAsDouble(i)); |
| 820 |
} |
| 821 |
else |
| 822 |
osVal.Printf("%f", poFeature->GetFieldAsDouble(i)); |
| 823 |
break; |
| 824 |
|
| 825 |
case OFTIntegerList: |
| 826 |
case OFTRealList: |
| 827 |
case OFTStringList: |
| 828 |
// FIXME Prepare array of values field |
| 829 |
// cv->ConvertFrom( poFeature->GetFieldAsStringList( i ) ); |
| 830 |
// break; |
| 831 |
|
| 832 |
case OFTBinary: |
| 833 |
// FIXME Prepare binary field |
| 834 |
|
| 835 |
case OFTString: |
| 836 |
case OFTDate: |
| 837 |
case OFTTime: |
| 838 |
case OFTDateTime: |
| 839 |
default: |
| 840 |
osVal.Printf("'%s'", poFeature->GetFieldAsString(i)); |
| 841 |
break; |
| 842 |
} |
| 843 |
osFields += osVal; |
| 844 |
} |
| 845 |
|
| 846 |
osSql.Printf("UPDATE %s SET %s WHERE %s = " CPL_FRMT_GIB, |
| 847 |
poFeatureDefn->GetName(), osFields.c_str(), pszFIDColumn, |
| 848 |
poFeature->GetFID()); |
| 849 |
|
| 850 |
if (!oQuery.Prepare(osSql.c_str())) |
| 851 |
{ |
| 852 |
CPLError(CE_Failure, CPLE_AppDefined, "Error prepare SQL.\n%s", |
| 853 |
osSql.c_str()); |
| 854 |
return eErr; |
| 855 |
} |
| 856 |
|
| 857 |
CPLDebug("OGR_IDB", "Exec(%s)", oQuery.QueryText().Data()); |
| 858 |
if (!oQuery.Exec()) |
| 859 |
{ |
| 860 |
CPLError(CE_Failure, CPLE_AppDefined, "Error update Feature."); |
| 861 |
return eErr; |
| 862 |
} |
| 863 |
|
| 864 |
return OGRERR_NONE; |
| 865 |
} |
| 866 |
|
| 867 |
OGRErr OGRIDBTableLayer::ICreateFeature(OGRFeature *poFeature) |
| 868 |
{ |
| 869 |
OGRErr eErr(OGRERR_FAILURE); |
| 870 |
|
| 871 |
if (!bUpdateAccess) |
| 872 |
{ |
| 873 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 874 |
"Error create feature. Layer is read only."); |
| 875 |
return eErr; |
| 876 |
} |
| 877 |
|
| 878 |
if (nullptr == poFeature) |
| 879 |
{ |
| 880 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 881 |
"NULL pointer to OGRFeature passed to CreateFeature()."); |
| 882 |
return eErr; |
| 883 |
} |
| 884 |
|
| 885 |
if (poFeature->GetFID() != OGRNullFID && pszFIDColumn == nullptr) |
| 886 |
{ |
| 887 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 888 |
"FID ignored on feature given to CreateFeature(). Unknown FID " |
| 889 |
"column."); |
| 890 |
return eErr; |
| 891 |
} |
| 892 |
|
| 893 |
int bUpdateGeom = TRUE; |
| 894 |
CPLString osGeomFunc; |
| 895 |
|
| 896 |
if (poFeature->GetGeometryRef()) |
| 897 |
{ |
| 898 |
OGRwkbGeometryType nGeomType = |
| 899 |
poFeature->GetGeometryRef()->getGeometryType(); |
| 900 |
|
| 901 |
switch (nGeomType) |
| 902 |
{ |
| 903 |
case wkbPoint: |
| 904 |
osGeomFunc = "ST_PointFromText"; |
| 905 |
break; |
| 906 |
case wkbLineString: |
| 907 |
osGeomFunc = "ST_LineFromText"; |
| 908 |
break; |
| 909 |
case wkbPolygon: |
| 910 |
osGeomFunc = "ST_PolyFromText"; |
| 911 |
break; |
| 912 |
case wkbMultiPoint: |
| 913 |
osGeomFunc = "ST_MPointFromText"; |
| 914 |
break; |
| 915 |
case wkbMultiLineString: |
| 916 |
osGeomFunc = "ST_MLineFromText"; |
| 917 |
break; |
| 918 |
case wkbMultiPolygon: |
| 919 |
osGeomFunc = "ST_MPolyFromText"; |
| 920 |
break; |
| 921 |
default: |
| 922 |
bUpdateGeom = FALSE; |
| 923 |
CPLDebug("OGR_IDB", "SetFeature(): Unknown geometry type. " |
| 924 |
"Geometry will not be updated."); |
| 925 |
} |
| 926 |
} |
| 927 |
else |
| 928 |
bUpdateGeom = FALSE; |
| 929 |
|
| 930 |
// Create query |
| 931 |
CPLString osSql; |
| 932 |
CPLString osFields; |
| 933 |
CPLString osValues; |
| 934 |
|
| 935 |
if (pszGeomColumn && bUpdateGeom) |
| 936 |
{ |
| 937 |
OGRGeometry *poGeom = poFeature->GetGeometryRef(); |
| 938 |
char *wkt; |
| 939 |
poGeom->exportToWkt(&wkt); |
| 940 |
|
| 941 |
osFields += pszGeomColumn; |
| 942 |
osValues.Printf("%s( '%s', %d )", osGeomFunc.c_str(), wkt, nSRSId); |
| 943 |
|
| 944 |
CPLFree(wkt); |
| 945 |
} |
| 946 |
|
| 947 |
for (int i = 0; i < poFeatureDefn->GetFieldCount(); i++) |
| 948 |
{ |
| 949 |
const char *pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); |
| 950 |
|
| 951 |
// Skip NULL fields |
| 952 |
if (!poFeature->IsFieldSetAndNotNull(i)) |
| 953 |
{ |
| 954 |
continue; |
| 955 |
} |
| 956 |
|
| 957 |
if (!osFields.empty()) |
| 958 |
{ |
| 959 |
osFields += ","; |
| 960 |
osValues += ","; |
| 961 |
} |
| 962 |
|
| 963 |
osFields += pszFieldName; |
| 964 |
|
| 965 |
CPLString osVal; |
| 966 |
|
| 967 |
switch (poFeatureDefn->GetFieldDefn(i)->GetType()) |
| 968 |
{ |
| 969 |
case OFTInteger: |
| 970 |
osVal.Printf("%d", poFeature->GetFieldAsInteger(i)); |
| 971 |
break; |
| 972 |
|
| 973 |
case OFTReal: |
| 974 |
if (poFeatureDefn->GetFieldDefn(i)->GetPrecision()) |
| 975 |
{ |
| 976 |
// have a decimal format width.precision |
| 977 |
CPLString osFormatString; |
| 978 |
osFormatString.Printf( |
| 979 |
"%%%d.%df", poFeatureDefn->GetFieldDefn(i)->GetWidth(), |
| 980 |
poFeatureDefn->GetFieldDefn(i)->GetPrecision()); |
| 981 |
osVal.Printf(osFormatString.c_str(), |
| 982 |
poFeature->GetFieldAsDouble(i)); |
| 983 |
} |
| 984 |
else |
| 985 |
osVal.Printf("%f", poFeature->GetFieldAsDouble(i)); |
| 986 |
break; |
| 987 |
|
| 988 |
case OFTIntegerList: |
| 989 |
case OFTRealList: |
| 990 |
case OFTStringList: |
| 991 |
// FIXME Prepare array of values field |
| 992 |
// cv->ConvertFrom( poFeature->GetFieldAsStringList( i ) ); |
| 993 |
// break; |
| 994 |
|
| 995 |
case OFTBinary: |
| 996 |
// FIXME Prepare binary field |
| 997 |
|
| 998 |
case OFTString: |
| 999 |
case OFTDate: |
| 1000 |
case OFTTime: |
| 1001 |
case OFTDateTime: |
| 1002 |
default: |
| 1003 |
osVal.Printf("'%s'", poFeature->GetFieldAsString(i)); |
| 1004 |
break; |
| 1005 |
} |
| 1006 |
osValues += osVal; |
| 1007 |
} |
| 1008 |
|
| 1009 |
osSql.Printf("INSERT INTO %s (%s) VALUES (%s)", poFeatureDefn->GetName(), |
| 1010 |
osFields.c_str(), osValues.c_str()); |
| 1011 |
|
| 1012 |
ITStatement oQuery(*poDS->GetConnection()); |
| 1013 |
|
| 1014 |
if (!oQuery.Prepare(osSql.c_str())) |
| 1015 |
{ |
| 1016 |
CPLError(CE_Failure, CPLE_AppDefined, "Error prepare SQL.\n%s", |
| 1017 |
osSql.c_str()); |
| 1018 |
return eErr; |
| 1019 |
} |
| 1020 |
|
| 1021 |
CPLDebug("OGR_IDB", "Exec(%s)", oQuery.QueryText().Data()); |
| 1022 |
if (!oQuery.Exec()) |
| 1023 |
{ |
| 1024 |
CPLError(CE_Failure, CPLE_AppDefined, "Error create Feature."); |
| 1025 |
return eErr; |
| 1026 |
} |
| 1027 |
|
| 1028 |
ITQuery oFidQuery(*poDS->GetConnection()); |
| 1029 |
osSql.Printf("SELECT MAX(%s) from %s", pszFIDColumn, |
| 1030 |
poFeatureDefn->GetName()); |
| 1031 |
|
| 1032 |
CPLDebug("OGR_IDB", "Exec(%s)", osSql.c_str()); |
| 1033 |
|
| 1034 |
ITRow *row = oFidQuery.ExecOneRow(osSql.c_str()); |
| 1035 |
if (!row || row->NumColumns() < 1) |
| 1036 |
{ |
| 1037 |
CPLError(CE_Failure, CPLE_AppDefined, "Error create Feature."); |
| 1038 |
return eErr; |
| 1039 |
} |
| 1040 |
|
| 1041 |
int fid = atoi(row->Column(0)->Printable()); |
| 1042 |
|
| 1043 |
if (fid > 0) |
| 1044 |
poFeature->SetFID(fid); |
| 1045 |
else |
| 1046 |
{ |
| 1047 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 1048 |
"Error create Feature. Unable to get new fid"); |
| 1049 |
return eErr; |
| 1050 |
} |
| 1051 |
|
| 1052 |
return OGRERR_NONE; |
| 1053 |
} |