1 : /******************************************************************************
2 : * $Id: ogrmssqlspatialselectlayer.cpp 20578 2010-09-12 11:23:32Z rouault $
3 : *
4 : * Project: MSSQL Spatial driver
5 : * Purpose: Implements OGRMSSQLSpatialSelectLayer class, layer access to the results
6 : * of a SELECT statement executed via ExecuteSQL().
7 : * Author: Tamas Szekeres, szekerest at gmail.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2010, Tamas Szekeres
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_mssqlspatial.h"
33 :
34 : CPL_CVSID("$Id: ogrmssqlspatialselectlayer.cpp 20578 2010-09-12 11:23:32Z rouault $");
35 : /************************************************************************/
36 : /* OGRMSSQLSpatialSelectLayer() */
37 : /************************************************************************/
38 :
39 0 : OGRMSSQLSpatialSelectLayer::OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *poDSIn,
40 0 : CPLODBCStatement * poStmtIn )
41 :
42 : {
43 0 : poDS = poDSIn;
44 :
45 0 : iNextShapeId = 0;
46 0 : nSRSId = -1;
47 0 : poFeatureDefn = NULL;
48 :
49 0 : poStmt = poStmtIn;
50 0 : pszBaseStatement = CPLStrdup( poStmtIn->GetCommand() );
51 :
52 : /* identify the geometry column */
53 0 : pszGeomColumn = NULL;
54 0 : for ( int iColumn = 0; iColumn < poStmt->GetColCount(); iColumn++ )
55 : {
56 0 : if ( EQUAL(poStmt->GetColTypeName( iColumn ), "image") )
57 : {
58 0 : nGeomColumnType = MSSQLCOLTYPE_BINARY;
59 0 : pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn));
60 0 : break;
61 : }
62 0 : if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geometry") )
63 : {
64 0 : nGeomColumnType = MSSQLCOLTYPE_GEOMETRY;
65 0 : pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn));
66 0 : break;
67 : }
68 0 : else if ( EQUAL(poStmt->GetColTypeName( iColumn ), "geography") )
69 : {
70 0 : nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY;
71 0 : pszGeomColumn = CPLStrdup(poStmt->GetColName(iColumn));
72 0 : break;
73 : }
74 : }
75 :
76 0 : BuildFeatureDefn( "SELECT", poStmt );
77 0 : }
78 :
79 : /************************************************************************/
80 : /* ~OGRMSSQLSpatialSelectLayer() */
81 : /************************************************************************/
82 :
83 0 : OGRMSSQLSpatialSelectLayer::~OGRMSSQLSpatialSelectLayer()
84 :
85 : {
86 0 : ClearStatement();
87 0 : CPLFree(pszBaseStatement);
88 0 : }
89 :
90 : /************************************************************************/
91 : /* ClearStatement() */
92 : /************************************************************************/
93 :
94 0 : void OGRMSSQLSpatialSelectLayer::ClearStatement()
95 :
96 : {
97 0 : if( poStmt != NULL )
98 : {
99 0 : delete poStmt;
100 0 : poStmt = NULL;
101 : }
102 0 : }
103 :
104 : /************************************************************************/
105 : /* GetStatement() */
106 : /************************************************************************/
107 :
108 0 : CPLODBCStatement *OGRMSSQLSpatialSelectLayer::GetStatement()
109 :
110 : {
111 0 : if( poStmt == NULL )
112 0 : ResetStatement();
113 :
114 0 : return poStmt;
115 : }
116 :
117 : /************************************************************************/
118 : /* ResetStatement() */
119 : /************************************************************************/
120 :
121 0 : OGRErr OGRMSSQLSpatialSelectLayer::ResetStatement()
122 :
123 : {
124 0 : ClearStatement();
125 :
126 0 : iNextShapeId = 0;
127 :
128 0 : CPLDebug( "OGR_MSSQLSpatial", "Recreating statement." );
129 0 : poStmt = new CPLODBCStatement( poDS->GetSession() );
130 0 : poStmt->Append( pszBaseStatement );
131 :
132 0 : if( poStmt->ExecuteSQL() )
133 0 : return OGRERR_NONE;
134 : else
135 : {
136 0 : delete poStmt;
137 0 : poStmt = NULL;
138 0 : return OGRERR_FAILURE;
139 : }
140 : }
141 :
142 : /************************************************************************/
143 : /* ResetReading() */
144 : /************************************************************************/
145 :
146 0 : void OGRMSSQLSpatialSelectLayer::ResetReading()
147 :
148 : {
149 0 : if( iNextShapeId != 0 )
150 0 : ClearStatement();
151 :
152 0 : OGRMSSQLSpatialLayer::ResetReading();
153 0 : }
154 :
155 : /************************************************************************/
156 : /* GetFeature() */
157 : /************************************************************************/
158 :
159 0 : OGRFeature *OGRMSSQLSpatialSelectLayer::GetFeature( long nFeatureId )
160 :
161 : {
162 0 : return OGRMSSQLSpatialLayer::GetFeature( nFeatureId );
163 : }
164 :
165 : /************************************************************************/
166 : /* TestCapability() */
167 : /************************************************************************/
168 :
169 0 : int OGRMSSQLSpatialSelectLayer::TestCapability( const char * pszCap )
170 :
171 : {
172 0 : return OGRMSSQLSpatialLayer::TestCapability( pszCap );
173 : }
174 :
175 : /************************************************************************/
176 : /* GetExtent() */
177 : /* */
178 : /* Since SELECT layers currently cannot ever have geometry, we */
179 : /* can optimize the GetExtent() method! */
180 : /************************************************************************/
181 :
182 0 : OGRErr OGRMSSQLSpatialSelectLayer::GetExtent(OGREnvelope *, int )
183 :
184 : {
185 0 : return OGRERR_FAILURE;
186 : }
187 :
188 : /************************************************************************/
189 : /* GetFeatureCount() */
190 : /* */
191 : /* If a spatial filter is in effect, we turn control over to */
192 : /* the generic counter. Otherwise we return the total count. */
193 : /* Eventually we should consider implementing a more efficient */
194 : /* way of counting features matching a spatial query. */
195 : /************************************************************************/
196 :
197 0 : int OGRMSSQLSpatialSelectLayer::GetFeatureCount( int bForce )
198 :
199 : {
200 0 : return OGRMSSQLSpatialLayer::GetFeatureCount( bForce );
201 : }
|