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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/******************************************************************************
* $Id: ogridbdatasource.cpp 10645 2007-01-18 02:22:39Z warmerdam $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements OGRIDBDataSource class
* (based on ODBC and PG drivers).
* Author: Oleg Semykin, oleg.semykin@gmail.com
*
******************************************************************************
* Copyright (c) 2006, Oleg Semykin
*
* 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 "ogr_idb.h"
#include "cpl_conv.h"
#include "cpl_string.h"
CPL_CVSID("$Id: ogridbdatasource.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
/************************************************************************/
/* OGRIDBDataSource() */
/************************************************************************/
OGRIDBDataSource::OGRIDBDataSource()
{
pszName = NULL;
papoLayers = NULL;
nLayers = 0;
poConn = 0;
}
/************************************************************************/
/* ~OGRIDBDataSource() */
/************************************************************************/
OGRIDBDataSource::~OGRIDBDataSource()
{
int i;
CPLFree( pszName );
for( i = 0; i < nLayers; i++ )
delete papoLayers[i];
CPLFree( papoLayers );
delete poConn;
}
/************************************************************************/
/* Open() */
/************************************************************************/
int OGRIDBDataSource::Open( const char * pszNewName, int bUpdate,
int bTestOpen )
{
CPLAssert( poConn == NULL );
/* -------------------------------------------------------------------- */
/* URL: DBI:dbname=.. server=.. user=.. pass=.. table=.. */
/* Any of params is optional, table param can repeat more than once */
/* -------------------------------------------------------------------- */
char * pszDbName = NULL;
char * pszServer = NULL;
char * pszUser = NULL;
char * pszPass = NULL;
char **papszTables = NULL;
char **papszGeomCol = NULL;
char ** papszTokens = CSLTokenizeString2(pszNewName + 4, " ", 0);
char * pszToken = 0;
int i = 0;
while ( pszToken = papszTokens[i++] )
{
if ( EQUALN( pszToken, "dbname=", 7 ) )
pszDbName = CPLStrdup( pszToken + 7 );
else if ( EQUALN( pszToken, "server=", 7 ) )
pszServer = CPLStrdup( pszToken + 7 );
else if ( EQUALN( pszToken, "user=", 5 ) )
pszUser = CPLStrdup( pszToken + 5 );
else if ( EQUALN( pszToken, "pass=", 5 ) )
pszPass = CPLStrdup( pszToken + 5 );
else if ( EQUALN( pszToken, "table=", 6 ) )
{
papszTables = CSLAddString( papszTables, pszToken + 6 );
papszGeomCol = CSLAddString( papszGeomCol, "" );
}
}
/* -------------------------------------------------------------------- */
/* Initialize based on the DSN. */
/* -------------------------------------------------------------------- */
ITDBInfo oDbInfo(pszDbName,pszUser,pszServer,pszPass);
CPLDebug( "OGR_IDB",
"Connect to: db:'%s' server:'%s', user:'%s', pass:'%s'",
pszDbName ? pszDbName : "",
pszServer ? pszServer : "",
pszUser ? pszUser : "",
pszPass ? pszPass : "" );
CPLFree( pszDbName );
CPLFree( pszServer );
CPLFree( pszUser );
CPLFree( pszPass );
poConn = new ITConnection( oDbInfo );
poConn->AddCallback( IDBErrorHandler, 0 );
if( !poConn->Open() )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unable to initialize IDB connection to %s",
pszNewName+4);
CSLDestroy( papszTables );
return FALSE;
}
pszName = CPLStrdup( pszNewName );
bDSUpdate = bUpdate;
/* -------------------------------------------------------------------- */
/* If no explicit list of tables was given, check for a list in */
/* a geometry_columns table. */
/* -------------------------------------------------------------------- */
if( papszTables == NULL )
{
ITCursor oCurr( *poConn );
if( oCurr.Prepare(" SELECT f_table_name, f_geometry_column,"
" geometry_type FROM geometry_columns" ) &&
oCurr.Open(ITCursor::ReadOnly) )
{
ITRow * row = 0;
while( (row = oCurr.NextRow()) )
{
papszTables =
CSLAddString( papszTables, row->Column(0)->Printable() );
papszGeomCol =
CSLAddString( papszGeomCol, row->Column(1)->Printable() );
row->Release();
}
}
}
/* -------------------------------------------------------------------- */
/* Otherwise our final resort is to return all tables as */
/* non-spatial tables. */
/* -------------------------------------------------------------------- */
if( papszTables == NULL )
{
ITCursor oTableList( *poConn );
if ( oTableList.Prepare("select tabname from systables where tabtype='T' and tabid > 99") &&
oTableList.Open(ITCursor::ReadOnly) )
{
ITRow * row = 0;
while( (row = oTableList.NextRow()) )
{
papszTables =
CSLAddString( papszTables, row->Column(0)->Printable() );
papszGeomCol = CSLAddString(papszGeomCol,"");
row->Release();
}
}
else
CPLError( CE_Failure, CPLE_AppDefined,
"Unable to open cursor for '%s'",
oTableList.QueryText().Data() );
}
/* -------------------------------------------------------------------- */
/* If we have an explicit list of requested tables, use them */
/* (non-spatial). */
/* -------------------------------------------------------------------- */
for( int iTable = 0;
papszTables != NULL && papszTables[iTable] != NULL;
iTable++ )
{
char * pszGeomCol = NULL;
if( strlen(papszGeomCol[iTable]) > 0 )
pszGeomCol = papszGeomCol[iTable];
OpenTable( papszTables[iTable], pszGeomCol, bUpdate );
}
CSLDestroy( papszTables );
CSLDestroy( papszGeomCol );
return TRUE;
}
/************************************************************************/
/* OpenTable() */
/************************************************************************/
int OGRIDBDataSource::OpenTable( const char *pszNewName,
const char *pszGeomCol,
int bUpdate )
{
/* -------------------------------------------------------------------- */
/* Create the layer object. */
/* -------------------------------------------------------------------- */
OGRIDBTableLayer *poLayer;
poLayer = new OGRIDBTableLayer( this );
if( poLayer->Initialize( pszNewName, pszGeomCol, bUpdate ) )
{
delete poLayer;
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Add layer to data source layer list. */
/* -------------------------------------------------------------------- */
papoLayers = (OGRIDBLayer **)
CPLRealloc( papoLayers, sizeof(OGRIDBLayer *) * (nLayers+1) );
papoLayers[nLayers++] = poLayer;
return TRUE;
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRIDBDataSource::TestCapability( const char * pszCap )
{
if( EQUAL(pszCap,ODsCCreateLayer) )
return TRUE;
else
return FALSE;
}
/************************************************************************/
/* GetLayer() */
/************************************************************************/
OGRLayer *OGRIDBDataSource::GetLayer( int iLayer )
{
if( iLayer < 0 || iLayer >= nLayers )
return NULL;
else
return papoLayers[iLayer];
}
/************************************************************************/
/* ExecuteSQL() */
/************************************************************************/
OGRLayer * OGRIDBDataSource::ExecuteSQL( const char *pszSQLCommand,
OGRGeometry *poSpatialFilter,
const char *pszDialect )
{
/* -------------------------------------------------------------------- */
/* Use generic imlplementation for OGRSQL dialect. */
/* -------------------------------------------------------------------- */
if( pszDialect != NULL && EQUAL(pszDialect,"OGRSQL") )
return OGRDataSource::ExecuteSQL( pszSQLCommand,
poSpatialFilter,
pszDialect );
/* -------------------------------------------------------------------- */
/* Execute statement. */
/* -------------------------------------------------------------------- */
ITCursor *poCurr = new ITCursor( *poConn );
poCurr->Prepare( pszSQLCommand );
if( !poCurr->Open(ITCursor::ReadOnly) )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Error execute SQL: %s", pszSQLCommand );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Are there result columns for this statement? */
/* -------------------------------------------------------------------- */
if( poCurr->RowType()->ColumnCount() == 0 )
{
delete poCurr;
CPLErrorReset();
return NULL;
}
/* -------------------------------------------------------------------- */
/* Create a results layer. It will take ownership of the */
/* statement. */
/* -------------------------------------------------------------------- */
OGRIDBSelectLayer *poLayer = NULL;
poLayer = new OGRIDBSelectLayer( this, poCurr );
if( poSpatialFilter != NULL )
poLayer->SetSpatialFilter( poSpatialFilter );
return poLayer;
}
/************************************************************************/
/* ReleaseResultSet() */
/************************************************************************/
void OGRIDBDataSource::ReleaseResultSet( OGRLayer * poLayer )
{
delete poLayer;
}
ITCallbackResult
IDBErrorHandler( const ITErrorManager &err, void * , long )
{
if ( err.Error() )
CPLError( CE_Failure, CPLE_AppDefined,
"IDB Error: %s", err.ErrorText().Data() );
/*if ( err.Warn() )
CPLError( CE_Warning, CPLE_AppDefined,
"IDB Warning: %s", err.WarningText().Data() );
*/
return IT_NOTHANDLED;
}
|