1 /******************************************************************************
2  *
3  * Project:  OpenGIS Simple Features Reference Implementation
4  * Purpose:  Implements OGRGeomediaSelectLayer 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) 2011, 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_geomedia.h"
33 
34 CPL_CVSID("$Id: ogrgeomediaselectlayer.cpp b1c9c12ad373e40b955162b45d704070d4ebf7b0 2019-06-19 16:50:15 +0200 Even Rouault $")
35 
36 /************************************************************************/
37 /*                        OGRGeomediaSelectLayer()                      */
38 /************************************************************************/
39 
OGRGeomediaSelectLayer(OGRGeomediaDataSource * poDSIn,CPLODBCStatement * poStmtIn)40 OGRGeomediaSelectLayer::OGRGeomediaSelectLayer( OGRGeomediaDataSource *poDSIn,
41                                                 CPLODBCStatement * poStmtIn ) :
42     pszBaseStatement(CPLStrdup( poStmtIn->GetCommand() ))
43 {
44     poDS = poDSIn;
45 
46     iNextShapeId = 0;
47     nSRSId = -1;
48     poFeatureDefn = nullptr;
49 
50     poStmt = poStmtIn;
51 
52     BuildFeatureDefn( "SELECT", poStmt );
53 }
54 
55 /************************************************************************/
56 /*                        ~OGRGeomediaSelectLayer()                     */
57 /************************************************************************/
58 
~OGRGeomediaSelectLayer()59 OGRGeomediaSelectLayer::~OGRGeomediaSelectLayer()
60 
61 {
62     ClearStatement();
63     CPLFree(pszBaseStatement);
64 }
65 
66 /************************************************************************/
67 /*                           ClearStatement()                           */
68 /************************************************************************/
69 
ClearStatement()70 void OGRGeomediaSelectLayer::ClearStatement()
71 
72 {
73     if( poStmt != nullptr )
74     {
75         delete poStmt;
76         poStmt = nullptr;
77     }
78 }
79 
80 /************************************************************************/
81 /*                            GetStatement()                            */
82 /************************************************************************/
83 
GetStatement()84 CPLODBCStatement *OGRGeomediaSelectLayer::GetStatement()
85 
86 {
87     if( poStmt == nullptr )
88         ResetStatement();
89 
90     return poStmt;
91 }
92 
93 /************************************************************************/
94 /*                           ResetStatement()                           */
95 /************************************************************************/
96 
ResetStatement()97 OGRErr OGRGeomediaSelectLayer::ResetStatement()
98 
99 {
100     ClearStatement();
101 
102     iNextShapeId = 0;
103 
104     CPLDebug( "ODBC", "Recreating statement." );
105     poStmt = new CPLODBCStatement( poDS->GetSession() );
106     poStmt->Append( pszBaseStatement );
107 
108     if( poStmt->ExecuteSQL() )
109         return OGRERR_NONE;
110     else
111     {
112         delete poStmt;
113         poStmt = nullptr;
114         return OGRERR_FAILURE;
115     }
116 }
117 
118 /************************************************************************/
119 /*                            ResetReading()                            */
120 /************************************************************************/
121 
ResetReading()122 void OGRGeomediaSelectLayer::ResetReading()
123 
124 {
125     if( iNextShapeId != 0 )
126         ClearStatement();
127 
128     OGRGeomediaLayer::ResetReading();
129 }
130 
131 /************************************************************************/
132 /*                             GetFeature()                             */
133 /************************************************************************/
134 
GetFeature(GIntBig nFeatureId)135 OGRFeature *OGRGeomediaSelectLayer::GetFeature( GIntBig nFeatureId )
136 
137 {
138     return OGRGeomediaLayer::GetFeature( nFeatureId );
139 }
140 
141 /************************************************************************/
142 /*                           TestCapability()                           */
143 /************************************************************************/
144 
TestCapability(const char * pszCap)145 int OGRGeomediaSelectLayer::TestCapability( const char * pszCap )
146 
147 {
148     return OGRGeomediaLayer::TestCapability( pszCap );
149 }
150 
151 /************************************************************************/
152 /*                          GetFeatureCount()                           */
153 /*                                                                      */
154 /*      If a spatial filter is in effect, we turn control over to       */
155 /*      the generic counter.  Otherwise we return the total count.      */
156 /*      Eventually we should consider implementing a more efficient     */
157 /*      way of counting features matching a spatial query.              */
158 /************************************************************************/
159 
GetFeatureCount(int bForce)160 GIntBig OGRGeomediaSelectLayer::GetFeatureCount( int bForce )
161 
162 {
163     return OGRGeomediaLayer::GetFeatureCount( bForce );
164 }
165