| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: Implements OGRIDBSelectLayer class, layer access to the results |
| 5 |
* of a SELECT statement executed via Open() |
| 6 |
* (based on ODBC and PG drivers). |
| 7 |
* Author: Oleg Semykin, [email protected] |
| 8 |
* |
| 9 |
****************************************************************************** |
| 10 |
* Copyright (c) 2006, Oleg Semykin |
| 11 |
* |
| 12 |
* SPDX-License-Identifier: MIT |
| 13 |
****************************************************************************/ |
| 14 |
|
| 15 |
#include "cpl_conv.h" |
| 16 |
#include "ogr_idb.h" |
| 17 |
|
| 18 |
/************************************************************************/ |
| 19 |
/* OGRIDBSelectLayer() */ |
| 20 |
/************************************************************************/ |
| 21 |
|
| 22 |
OGRIDBSelectLayer::OGRIDBSelectLayer(OGRIDBDataSource *poDSIn, |
| 23 |
ITCursor *poCurrIn) |
| 24 |
|
| 25 |
{ |
| 26 |
poDS = poDSIn; |
| 27 |
|
| 28 |
iNextShapeId = 0; |
| 29 |
nSRSId = -1; |
| 30 |
poFeatureDefn = nullptr; |
| 31 |
|
| 32 |
m_poCurr = poCurrIn; |
| 33 |
pszBaseQuery = CPLStrdup(poCurrIn->Command()); |
| 34 |
|
| 35 |
BuildFeatureDefn("SELECT", m_poCurr); |
| 36 |
} |
| 37 |
|
| 38 |
/************************************************************************/ |
| 39 |
/* ~OGRIDBSelectLayer() */ |
| 40 |
/************************************************************************/ |
| 41 |
|
| 42 |
OGRIDBSelectLayer::~OGRIDBSelectLayer() |
| 43 |
|
| 44 |
{ |
| 45 |
ClearQuery(); |
| 46 |
} |
| 47 |
|
| 48 |
/************************************************************************/ |
| 49 |
/* ClearQuery() */ |
| 50 |
/************************************************************************/ |
| 51 |
|
| 52 |
void OGRIDBSelectLayer::ClearQuery() |
| 53 |
|
| 54 |
{ |
| 55 |
if (m_poCurr != nullptr) |
| 56 |
{ |
| 57 |
delete m_poCurr; |
| 58 |
m_poCurr = nullptr; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/************************************************************************/ |
| 63 |
/* GetQuery() */ |
| 64 |
/************************************************************************/ |
| 65 |
|
| 66 |
ITCursor *OGRIDBSelectLayer::GetQuery() |
| 67 |
|
| 68 |
{ |
| 69 |
if (m_poCurr == nullptr) |
| 70 |
ResetQuery(); |
| 71 |
|
| 72 |
return m_poCurr; |
| 73 |
} |
| 74 |
|
| 75 |
/************************************************************************/ |
| 76 |
/* ResetQuery() */ |
| 77 |
/************************************************************************/ |
| 78 |
|
| 79 |
OGRErr OGRIDBSelectLayer::ResetQuery() |
| 80 |
|
| 81 |
{ |
| 82 |
ClearQuery(); |
| 83 |
|
| 84 |
iNextShapeId = 0; |
| 85 |
|
| 86 |
CPLDebug("OGR_IDB", "Recreating statement."); |
| 87 |
m_poCurr = new ITCursor(*poDS->GetConnection()); |
| 88 |
|
| 89 |
if (m_poCurr->Prepare(pszBaseQuery) && m_poCurr->Open(ITCursor::ReadOnly)) |
| 90 |
return OGRERR_NONE; |
| 91 |
else |
| 92 |
{ |
| 93 |
delete m_poCurr; |
| 94 |
m_poCurr = nullptr; |
| 95 |
return OGRERR_FAILURE; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/************************************************************************/ |
| 100 |
/* ResetReading() */ |
| 101 |
/************************************************************************/ |
| 102 |
|
| 103 |
void OGRIDBSelectLayer::ResetReading() |
| 104 |
|
| 105 |
{ |
| 106 |
if (iNextShapeId != 0) |
| 107 |
ClearQuery(); |
| 108 |
|
| 109 |
OGRIDBLayer::ResetReading(); |
| 110 |
} |
| 111 |
|
| 112 |
/************************************************************************/ |
| 113 |
/* GetFeature() */ |
| 114 |
/************************************************************************/ |
| 115 |
|
| 116 |
OGRFeature *OGRIDBSelectLayer::GetFeature(GIntBig nFeatureId) |
| 117 |
|
| 118 |
{ |
| 119 |
return OGRIDBLayer::GetFeature(nFeatureId); |
| 120 |
} |
| 121 |
|
| 122 |
/************************************************************************/ |
| 123 |
/* TestCapability() */ |
| 124 |
/************************************************************************/ |
| 125 |
|
| 126 |
int OGRIDBSelectLayer::TestCapability(const char *pszCap) const |
| 127 |
|
| 128 |
{ |
| 129 |
return OGRIDBLayer::TestCapability(pszCap); |
| 130 |
} |
| 131 |
|
| 132 |
/************************************************************************/ |
| 133 |
/* IGetExtent() */ |
| 134 |
/* */ |
| 135 |
/* Since SELECT layers currently cannot ever have geometry, we */ |
| 136 |
/* can optimize the IGetExtent() method! */ |
| 137 |
/************************************************************************/ |
| 138 |
|
| 139 |
OGRErr OGRIDBSelectLayer::IGetExtent(int, OGREnvelope *, bool) |
| 140 |
|
| 141 |
{ |
| 142 |
return OGRERR_FAILURE; |
| 143 |
} |
| 144 |
|
| 145 |
/************************************************************************/ |
| 146 |
/* GetFeatureCount() */ |
| 147 |
/* */ |
| 148 |
/* If a spatial filter is in effect, we turn control over to */ |
| 149 |
/* the generic counter. Otherwise we return the total count. */ |
| 150 |
/* Eventually we should consider implementing a more efficient */ |
| 151 |
/* way of counting features matching a spatial query. */ |
| 152 |
/************************************************************************/ |
| 153 |
|
| 154 |
GIntBig OGRIDBSelectLayer::GetFeatureCount(int bForce) |
| 155 |
|
| 156 |
{ |
| 157 |
return OGRIDBLayer::GetFeatureCount(bForce); |
| 158 |
} |