Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Implements OGRPGeoSelectLayer class, layer access to the results 5 : * of a SELECT statement executed via ExecuteSQL(). 6 : * Author: Frank Warmerdam, warmerdam@pobox.com 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2005, Frank Warmerdam 10 : * Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys.com> 11 : * 12 : * Permission is hereby granted, free of charge, to any person obtaining a 13 : * copy of this software and associated documentation files (the "Software"), 14 : * to deal in the Software without restriction, including without limitation 15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 16 : * and/or sell copies of the Software, and to permit persons to whom the 17 : * Software is furnished to do so, subject to the following conditions: 18 : * 19 : * The above copyright notice and this permission notice shall be included 20 : * in all copies or substantial portions of the Software. 21 : * 22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 : * DEALINGS IN THE SOFTWARE. 29 : ****************************************************************************/ 30 : 31 : #include "cpl_conv.h" 32 : #include "ogr_pgeo.h" 33 : 34 : /************************************************************************/ 35 : /* OGRPGeoSelectLayer() */ 36 : /************************************************************************/ 37 : 38 0 : OGRPGeoSelectLayer::OGRPGeoSelectLayer(OGRPGeoDataSource *poDSIn, 39 0 : CPLODBCStatement *poStmtIn) 40 0 : : pszBaseStatement(CPLStrdup(poStmtIn->GetCommand())) 41 : { 42 0 : poDS = poDSIn; 43 : 44 0 : iNextShapeId = 0; 45 0 : nSRSId = -1; 46 0 : poFeatureDefn = nullptr; 47 : 48 0 : poStmt = poStmtIn; 49 : 50 : // Just to make test_ogrsf happy, but would/could need be extended to 51 : // other cases. 52 0 : if (STARTS_WITH_CI(pszBaseStatement, "SELECT * FROM ")) 53 : { 54 : 55 : OGRLayer *poBaseLayer = 56 0 : poDSIn->GetLayerByName(pszBaseStatement + strlen("SELECT * FROM ")); 57 0 : if (poBaseLayer != nullptr) 58 : { 59 0 : poSRS = poBaseLayer->GetSpatialRef(); 60 0 : if (poSRS != nullptr) 61 0 : poSRS->Reference(); 62 : } 63 : } 64 : 65 0 : BuildFeatureDefn("SELECT", poStmt); 66 0 : } 67 : 68 : /************************************************************************/ 69 : /* ~OGRPGeoSelectLayer() */ 70 : /************************************************************************/ 71 : 72 0 : OGRPGeoSelectLayer::~OGRPGeoSelectLayer() 73 : 74 : { 75 0 : ClearStatement(); 76 0 : CPLFree(pszBaseStatement); 77 0 : } 78 : 79 : /************************************************************************/ 80 : /* ClearStatement() */ 81 : /************************************************************************/ 82 : 83 0 : void OGRPGeoSelectLayer::ClearStatement() 84 : 85 : { 86 0 : if (poStmt != nullptr) 87 : { 88 0 : delete poStmt; 89 0 : poStmt = nullptr; 90 : } 91 0 : } 92 : 93 : /************************************************************************/ 94 : /* GetStatement() */ 95 : /************************************************************************/ 96 : 97 0 : CPLODBCStatement *OGRPGeoSelectLayer::GetStatement() 98 : 99 : { 100 0 : if (poStmt == nullptr) 101 0 : ResetStatement(); 102 : 103 0 : return poStmt; 104 : } 105 : 106 : /************************************************************************/ 107 : /* ResetStatement() */ 108 : /************************************************************************/ 109 : 110 0 : OGRErr OGRPGeoSelectLayer::ResetStatement() 111 : 112 : { 113 0 : ClearStatement(); 114 : 115 0 : iNextShapeId = 0; 116 : 117 0 : CPLDebug("ODBC", "Recreating statement."); 118 0 : poStmt = new CPLODBCStatement(poDS->GetSession(), m_nStatementFlags); 119 0 : poStmt->Append(pszBaseStatement); 120 : 121 0 : if (poStmt->ExecuteSQL()) 122 0 : return OGRERR_NONE; 123 : else 124 : { 125 0 : delete poStmt; 126 0 : poStmt = nullptr; 127 0 : return OGRERR_FAILURE; 128 : } 129 : } 130 : 131 : /************************************************************************/ 132 : /* ResetReading() */ 133 : /************************************************************************/ 134 : 135 0 : void OGRPGeoSelectLayer::ResetReading() 136 : 137 : { 138 0 : if (iNextShapeId != 0) 139 0 : ClearStatement(); 140 : 141 0 : OGRPGeoLayer::ResetReading(); 142 0 : } 143 : 144 : /************************************************************************/ 145 : /* GetFeature() */ 146 : /************************************************************************/ 147 : 148 0 : OGRFeature *OGRPGeoSelectLayer::GetFeature(GIntBig nFeatureId) 149 : 150 : { 151 0 : return OGRPGeoLayer::GetFeature(nFeatureId); 152 : } 153 : 154 : /************************************************************************/ 155 : /* TestCapability() */ 156 : /************************************************************************/ 157 : 158 0 : int OGRPGeoSelectLayer::TestCapability(const char *pszCap) 159 : 160 : { 161 0 : return OGRPGeoLayer::TestCapability(pszCap); 162 : } 163 : 164 : /************************************************************************/ 165 : /* GetFeatureCount() */ 166 : /* */ 167 : /* If a spatial filter is in effect, we turn control over to */ 168 : /* the generic counter. Otherwise we return the total count. */ 169 : /* Eventually we should consider implementing a more efficient */ 170 : /* way of counting features matching a spatial query. */ 171 : /************************************************************************/ 172 : 173 0 : GIntBig OGRPGeoSelectLayer::GetFeatureCount(int bForce) 174 : 175 : { 176 0 : return OGRPGeoLayer::GetFeatureCount(bForce); 177 : }