1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/******************************************************************************
* $Id: ogrgeomediatablelayer.cpp 21561 2011-01-23 12:22:58Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements OGRGeomediaTableLayer class, access to an existing table.
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
*
******************************************************************************
* Copyright (c) 2011, Even Rouault, <even dot rouault at mines dash paris dot org>
* Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_conv.h"
#include "ogr_geomedia.h"
CPL_CVSID("$Id: ogrgeomediatablelayer.cpp 21561 2011-01-23 12:22:58Z rouault $");
/************************************************************************/
/* OGRGeomediaTableLayer() */
/************************************************************************/
OGRGeomediaTableLayer::OGRGeomediaTableLayer( OGRGeomediaDataSource *poDSIn )
{
poDS = poDSIn;
pszQuery = NULL;
bUpdateAccess = TRUE;
iNextShapeId = 0;
nSRSId = -1;
poFeatureDefn = NULL;
}
/************************************************************************/
/* ~OGRGeomediaTableLayer() */
/************************************************************************/
OGRGeomediaTableLayer::~OGRGeomediaTableLayer()
{
CPLFree( pszQuery );
ClearStatement();
}
/************************************************************************/
/* Initialize() */
/************************************************************************/
CPLErr OGRGeomediaTableLayer::Initialize( const char *pszTableName,
const char *pszGeomCol,
OGRSpatialReference* poSRS )
{
CPLODBCSession *poSession = poDS->GetSession();
CPLFree( pszGeomColumn );
if( pszGeomCol == NULL )
pszGeomColumn = NULL;
else
pszGeomColumn = CPLStrdup( pszGeomCol );
CPLFree( pszFIDColumn );
pszFIDColumn = NULL;
this->poSRS = poSRS;
/* -------------------------------------------------------------------- */
/* Do we have a simple primary key? */
/* -------------------------------------------------------------------- */
{
CPLODBCStatement oGetKey( poSession );
if( oGetKey.GetPrimaryKeys( pszTableName ) && oGetKey.Fetch() )
{
pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 ));
if( oGetKey.Fetch() ) // more than one field in key!
{
CPLFree( pszFIDColumn );
pszFIDColumn = NULL;
CPLDebug( "Geomedia", "%s: Compound primary key, ignoring.",
pszTableName );
}
else
CPLDebug( "Geomedia",
"%s: Got primary key %s.",
pszTableName, pszFIDColumn );
}
else
CPLDebug( "Geomedia", "%s: no primary key", pszTableName );
}
/* -------------------------------------------------------------------- */
/* Get the column definitions for this table. */
/* -------------------------------------------------------------------- */
CPLODBCStatement oGetCol( poSession );
CPLErr eErr;
if( !oGetCol.GetColumns( pszTableName ) )
{
CPLError( CE_Failure, CPLE_AppDefined,
"GetColumns() failed on %s.\n%s",
pszTableName, poSession->GetLastError() );
return CE_Failure;
}
eErr = BuildFeatureDefn( pszTableName, &oGetCol );
if( eErr != CE_None )
return eErr;
if( poFeatureDefn->GetFieldCount() == 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"No column definitions found for table '%s', layer not usable.",
pszTableName );
return CE_Failure;
}
return CE_None;
}
/************************************************************************/
/* ClearStatement() */
/************************************************************************/
void OGRGeomediaTableLayer::ClearStatement()
{
if( poStmt != NULL )
{
delete poStmt;
poStmt = NULL;
}
}
/************************************************************************/
/* GetStatement() */
/************************************************************************/
CPLODBCStatement *OGRGeomediaTableLayer::GetStatement()
{
if( poStmt == NULL )
ResetStatement();
return poStmt;
}
/************************************************************************/
/* ResetStatement() */
/************************************************************************/
OGRErr OGRGeomediaTableLayer::ResetStatement()
{
ClearStatement();
iNextShapeId = 0;
poStmt = new CPLODBCStatement( poDS->GetSession() );
poStmt->Append( "SELECT * FROM " );
poStmt->Append( poFeatureDefn->GetName() );
if( pszQuery != NULL )
poStmt->Appendf( " WHERE %s", pszQuery );
if( poStmt->ExecuteSQL() )
return OGRERR_NONE;
else
{
delete poStmt;
poStmt = NULL;
return OGRERR_FAILURE;
}
}
/************************************************************************/
/* ResetReading() */
/************************************************************************/
void OGRGeomediaTableLayer::ResetReading()
{
ClearStatement();
OGRGeomediaLayer::ResetReading();
}
/************************************************************************/
/* GetFeature() */
/************************************************************************/
OGRFeature *OGRGeomediaTableLayer::GetFeature( long nFeatureId )
{
if( pszFIDColumn == NULL )
return OGRGeomediaLayer::GetFeature( nFeatureId );
ClearStatement();
iNextShapeId = nFeatureId;
poStmt = new CPLODBCStatement( poDS->GetSession() );
poStmt->Append( "SELECT * FROM " );
poStmt->Append( poFeatureDefn->GetName() );
poStmt->Appendf( " WHERE %s = %ld", pszFIDColumn, nFeatureId );
if( !poStmt->ExecuteSQL() )
{
delete poStmt;
poStmt = NULL;
return NULL;
}
return GetNextRawFeature();
}
/************************************************************************/
/* SetAttributeFilter() */
/************************************************************************/
OGRErr OGRGeomediaTableLayer::SetAttributeFilter( const char *pszQuery )
{
if( (pszQuery == NULL && this->pszQuery == NULL)
|| (pszQuery != NULL && this->pszQuery != NULL
&& EQUAL(pszQuery,this->pszQuery)) )
return OGRERR_NONE;
CPLFree( this->pszQuery );
this->pszQuery = pszQuery ? CPLStrdup( pszQuery ) : NULL;
ClearStatement();
return OGRERR_NONE;
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRGeomediaTableLayer::TestCapability( const char * pszCap )
{
if( EQUAL(pszCap,OLCRandomRead) )
return TRUE;
else if( EQUAL(pszCap,OLCFastFeatureCount) )
return m_poFilterGeom == NULL;
else if( EQUAL(pszCap,OLCFastSpatialFilter) )
return FALSE;
else
return OGRGeomediaLayer::TestCapability( pszCap );
}
/************************************************************************/
/* GetFeatureCount() */
/* */
/* If a spatial filter is in effect, we turn control over to */
/* the generic counter. Otherwise we return the total count. */
/* Eventually we should consider implementing a more efficient */
/* way of counting features matching a spatial query. */
/************************************************************************/
int OGRGeomediaTableLayer::GetFeatureCount( int bForce )
{
if( m_poFilterGeom != NULL )
return OGRGeomediaLayer::GetFeatureCount( bForce );
CPLODBCStatement oStmt( poDS->GetSession() );
oStmt.Append( "SELECT COUNT(*) FROM " );
oStmt.Append( poFeatureDefn->GetName() );
if( pszQuery != NULL )
oStmt.Appendf( " WHERE %s", pszQuery );
if( !oStmt.ExecuteSQL() || !oStmt.Fetch() )
{
CPLError( CE_Failure, CPLE_AppDefined,
"GetFeatureCount() failed on query %s.\n%s",
oStmt.GetCommand(), poDS->GetSession()->GetLastError() );
return OGRGeomediaLayer::GetFeatureCount(bForce);
}
return atoi(oStmt.GetColData(0));
}
|