OGRErr OGRSQLiteSelectLayer::ResetStatement() { int rc; ClearStatement(); iNextShapeId = 0; bDoStep = TRUE; #ifdef DEBUG CPLDebug( "OGR_SQLITE", "prepare(%s)", poBehaviour->osSQLCurrent.c_str() ); #endif rc = sqlite3_prepare( poDS->GetDB(), poBehaviour->osSQLCurrent, static_cast<int>(poBehaviour->osSQLCurrent.size()), &hStmt, NULL ); if( rc == SQLITE_OK ) { return OGRERR_NONE; } else { CPLError( CE_Failure, CPLE_AppDefined, "In ResetStatement(): sqlite3_prepare(%s):\n %s", poBehaviour->osSQLCurrent.c_str(), sqlite3_errmsg(poDS->GetDB()) ); hStmt = NULL; return OGRERR_FAILURE; } }
OGRErr OGRSQLiteTableLayer::ResetStatement() { int rc; CPLString osSQL; ClearStatement(); iNextShapeId = 0; osSQL.Printf( "SELECT _rowid_, * FROM '%s' %s", pszEscapedTableName, osWHERE.c_str() ); rc = sqlite3_prepare( poDS->GetDB(), osSQL, osSQL.size(), &hStmt, NULL ); if( rc == SQLITE_OK ) { return OGRERR_NONE; } else { CPLError( CE_Failure, CPLE_AppDefined, "In ResetStatement(): sqlite3_prepare(%s):\n %s", osSQL.c_str(), sqlite3_errmsg(poDS->GetDB()) ); hStmt = NULL; return OGRERR_FAILURE; } }
OGRFeature *OGRWalkTableLayer::GetFeature( long nFeatureId ) { if( pszFIDColumn == NULL ) return OGRWalkLayer::GetFeature( nFeatureId ); ClearStatement(); iNextShapeId = nFeatureId; poStmt = new CPLODBCStatement( poDS->GetSession() ); poStmt->Append( "SELECT * FROM " ); poStmt->Append( poFeatureDefn->GetName() ); poStmt->Append( "Features" ); poStmt->Appendf( " WHERE %s = %ld", pszFIDColumn, nFeatureId ); if( !poStmt->ExecuteSQL() ) { delete poStmt; poStmt = NULL; return NULL; } return GetNextRawFeature(); }
OGRSQLiteViewLayer::~OGRSQLiteViewLayer() { ClearStatement(); CPLFree(pszEscapedTableName); CPLFree(pszEscapedUnderlyingTableName); }
OGRErr OGRWalkTableLayer::ResetStatement() { ClearStatement(); iNextShapeId = 0; poStmt = new CPLODBCStatement( poDS->GetSession() ); poStmt->Append( "SELECT * FROM " ); poStmt->Append( poFeatureDefn->GetName() ); poStmt->Append( "Features" ); /* Append attribute query if we have it */ if( (pszQuery != NULL) && strcmp(pszQuery, "") ) poStmt->Appendf( " WHERE %s", pszQuery ); CPLDebug( "Walk", "ExecuteSQL(%s)", poStmt->GetCommand() ); if( poStmt->ExecuteSQL() ) return OGRERR_NONE; else { delete poStmt; poStmt = NULL; return OGRERR_FAILURE; } }
void OGRDB2SelectLayer::ResetReading() { if( iNextShapeId != 0 ) ClearStatement(); OGRDB2Layer::ResetReading(); }
void OGRGeomediaSelectLayer::ResetReading() { if( iNextShapeId != 0 ) ClearStatement(); OGRGeomediaLayer::ResetReading(); }
void OGRMSSQLSpatialSelectLayer::ResetReading() { if( iNextShapeId != 0 ) ClearStatement(); OGRMSSQLSpatialLayer::ResetReading(); }
OGRODBCTableLayer::~OGRODBCTableLayer() { CPLFree( pszTableName ); CPLFree( pszSchemaName ); CPLFree( pszQuery ); ClearStatement(); }
OGRMSSQLSpatialTableLayer::~OGRMSSQLSpatialTableLayer() { CPLFree( pszTableName ); CPLFree( pszSchemaName ); CPLFree( pszQuery ); ClearStatement(); }
OGRFeature *OGRGeoPackageLayer::GetNextFeature() { for( ; TRUE; ) { OGRFeature *poFeature; if( m_poQueryStatement == NULL ) { ResetStatement(); if (m_poQueryStatement == NULL) return NULL; } /* -------------------------------------------------------------------- */ /* Fetch a record (unless otherwise instructed) */ /* -------------------------------------------------------------------- */ if( bDoStep ) { int rc; rc = sqlite3_step( m_poQueryStatement ); if( rc != SQLITE_ROW ) { if ( rc != SQLITE_DONE ) { sqlite3_reset(m_poQueryStatement); CPLError( CE_Failure, CPLE_AppDefined, "In GetNextRawFeature(): sqlite3_step() : %s", sqlite3_errmsg(m_poDS->GetDB()) ); } ClearStatement(); return NULL; } } else bDoStep = TRUE; poFeature = TranslateFeature(m_poQueryStatement); if( poFeature == NULL ) return NULL; if( (m_poFilterGeom == NULL || FilterGeometry( poFeature->GetGeomFieldRef(m_iGeomFieldFilter) ) ) && (m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeature )) ) return poFeature; delete poFeature; } }
OGRFeature *OGRSQLiteViewLayer::GetFeature( long nFeatureId ) { if (HasLayerDefnError()) return NULL; /* -------------------------------------------------------------------- */ /* If we don't have an explicit FID column, just read through */ /* the result set iteratively to find our target. */ /* -------------------------------------------------------------------- */ if( pszFIDColumn == NULL ) return OGRSQLiteLayer::GetFeature( nFeatureId ); /* -------------------------------------------------------------------- */ /* Setup explicit query statement to fetch the record we want. */ /* -------------------------------------------------------------------- */ CPLString osSQL; int rc; ClearStatement(); iNextShapeId = nFeatureId; osSQL.Printf( "SELECT \"%s\", * FROM '%s' WHERE \"%s\" = %d", OGRSQLiteEscapeName(pszFIDColumn).c_str(), pszEscapedTableName, OGRSQLiteEscapeName(pszFIDColumn).c_str(), (int) nFeatureId ); CPLDebug( "OGR_SQLITE", "exec(%s)", osSQL.c_str() ); rc = sqlite3_prepare( poDS->GetDB(), osSQL, osSQL.size(), &hStmt, NULL ); if( rc != SQLITE_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "In GetFeature(): sqlite3_prepare(%s):\n %s", osSQL.c_str(), sqlite3_errmsg(poDS->GetDB()) ); return NULL; } /* -------------------------------------------------------------------- */ /* Get the feature if possible. */ /* -------------------------------------------------------------------- */ OGRFeature *poFeature = NULL; poFeature = GetNextRawFeature(); ResetReading(); return poFeature; }
OGRErr OGRGeomediaTableLayer::SetAttributeFilter( const char *pszQueryIn ) { if( (pszQueryIn == NULL && this->pszQuery == NULL) || (pszQueryIn != NULL && this->pszQuery != NULL && EQUAL(pszQueryIn,this->pszQuery)) ) return OGRERR_NONE; CPLFree( this->pszQuery ); this->pszQuery = pszQueryIn ? CPLStrdup( pszQueryIn ) : NULL; ClearStatement(); return OGRERR_NONE; }
OGRErr OGRMSSQLSpatialTableLayer::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; }
OGRErr OGRPGeoTableLayer::SetAttributeFilter( const char *pszQuery ) { CPLFree(m_pszAttrQueryString); m_pszAttrQueryString = (pszQuery) ? CPLStrdup(pszQuery) : NULL; 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; }
OGRErr OGRODBCTableLayer::SetAttributeFilter( const char *pszQueryIn ) { CPLFree(m_pszAttrQueryString); m_pszAttrQueryString = (pszQueryIn) ? CPLStrdup(pszQueryIn) : NULL; if( (pszQueryIn == NULL && pszQuery == NULL) || (pszQueryIn != NULL && pszQuery != NULL && EQUAL(pszQueryIn, pszQuery)) ) return OGRERR_NONE; CPLFree( pszQuery ); pszQuery = pszQueryIn != NULL ? CPLStrdup( pszQueryIn ) : NULL; ClearStatement(); return OGRERR_NONE; }
OGRErr OGRDB2SelectLayer::ResetStatement() { ClearStatement(); iNextShapeId = 0; CPLDebug( "OGR_DB2SelectLayer::ResetStatement", "Recreating statement." ); m_poStmt = new OGRDB2Statement( poDS->GetSession() ); m_poStmt->Append( pszBaseStatement ); if( m_poStmt->ExecuteSQL() ) return OGRERR_NONE; else { delete m_poStmt; m_poStmt = NULL; return OGRERR_FAILURE; } }
OGRErr OGRGeomediaSelectLayer::ResetStatement() { ClearStatement(); iNextShapeId = 0; CPLDebug( "ODBC", "Recreating statement." ); poStmt = new CPLODBCStatement( poDS->GetSession() ); poStmt->Append( pszBaseStatement ); if( poStmt->ExecuteSQL() ) return OGRERR_NONE; else { delete poStmt; poStmt = nullptr; return OGRERR_FAILURE; } }
OGRErr OGRMSSQLSpatialTableLayer::DeleteFeature( long nFID ) { GetLayerDefn(); if( pszFIDColumn == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "DeleteFeature() without any FID column." ); return OGRERR_FAILURE; } if( nFID == OGRNullFID ) { CPLError( CE_Failure, CPLE_AppDefined, "DeleteFeature() with unset FID fails." ); return OGRERR_FAILURE; } ClearStatement(); /* -------------------------------------------------------------------- */ /* Drop the record with this FID. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oStatement( poDS->GetSession() ); oStatement.Appendf("DELETE FROM [%s] WHERE [%s] = %ld", poFeatureDefn->GetName(), pszFIDColumn, nFID); if( !oStatement.ExecuteSQL() ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to delete feature with FID %ld failed. %s", nFID, poDS->GetSession()->GetLastError() ); return OGRERR_FAILURE; } return OGRERR_NONE; }
OGRErr OGRODBCTableLayer::ResetStatement() { ClearStatement(); iNextShapeId = 0; poStmt = new CPLODBCStatement( poDS->GetSession() ); poStmt->Append( "SELECT * FROM " ); poStmt->Append( poFeatureDefn->GetName() ); /* Append attribute query if we have it */ if( pszQuery != NULL ) poStmt->Appendf( " WHERE %s", pszQuery ); /* If we have a spatial filter, and per record extents, query on it */ if( m_poFilterGeom != NULL && bHaveSpatialExtents ) { if( pszQuery == NULL ) poStmt->Append( " WHERE" ); else poStmt->Append( " AND" ); poStmt->Appendf( " XMAX > %.8f AND XMIN < %.8f" " AND YMAX > %.8f AND YMIN < %.8f", m_sFilterEnvelope.MinX, m_sFilterEnvelope.MaxX, m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxY ); } CPLDebug( "OGR_ODBC", "ExecuteSQL(%s)", poStmt->GetCommand() ); if( poStmt->ExecuteSQL() ) return OGRERR_NONE; else { delete poStmt; poStmt = NULL; return OGRERR_FAILURE; } }
OGRErr OGRPGeoTableLayer::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; } }
int OGRMSSQLSpatialTableLayer::GetFeatureCount( int bForce ) { GetLayerDefn(); if( TestCapability(OLCFastFeatureCount) == FALSE ) return OGRMSSQLSpatialLayer::GetFeatureCount( bForce ); ClearStatement(); CPLODBCStatement* poStatement = BuildStatement( "count(*)" ); if (poStatement == NULL || !poStatement->Fetch()) { delete poStatement; return OGRMSSQLSpatialLayer::GetFeatureCount( bForce ); } int nRet = atoi(poStatement->GetColData( 0 )); delete poStatement; return nRet; }
OGRFeature *OGRMSSQLSpatialTableLayer::GetFeature( long nFeatureId ) { if( pszFIDColumn == NULL ) return OGRMSSQLSpatialLayer::GetFeature( nFeatureId ); ClearStatement(); iNextShapeId = nFeatureId; poStmt = new CPLODBCStatement( poDS->GetSession() ); CPLString osFields = BuildFields(); poStmt->Appendf( "select %s from %s where %s = %ld", osFields.c_str(), poFeatureDefn->GetName(), pszFIDColumn, nFeatureId ); if( !poStmt->ExecuteSQL() ) { delete poStmt; poStmt = NULL; return NULL; } return GetNextRawFeature(); }
OGRErr OGRMSSQLSpatialTableLayer::SetFeature( OGRFeature *poFeature ) { OGRErr eErr = OGRERR_FAILURE; GetLayerDefn(); if( NULL == poFeature ) { CPLError( CE_Failure, CPLE_AppDefined, "NULL pointer to OGRFeature passed to SetFeature()." ); return eErr; } if( poFeature->GetFID() == OGRNullFID ) { CPLError( CE_Failure, CPLE_AppDefined, "FID required on features given to SetFeature()." ); return eErr; } if( !pszFIDColumn ) { CPLError( CE_Failure, CPLE_AppDefined, "Unable to update features in tables without\n" "a recognised FID column."); return eErr; } ClearStatement(); /* -------------------------------------------------------------------- */ /* Form the UPDATE command. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oStmt( poDS->GetSession() ); oStmt.Appendf( "UPDATE [%s].[%s] SET ", pszSchemaName, pszTableName); OGRMSSQLGeometryValidator oValidator(poFeature->GetGeometryRef()); OGRGeometry *poGeom = oValidator.GetValidGeometryRef(); if (poFeature->GetGeometryRef() != poGeom) { CPLError( CE_Warning, CPLE_NotSupported, "Geometry with FID = %ld has been modified.", poFeature->GetFID() ); } int bNeedComma = FALSE; if(pszGeomColumn != NULL) { char *pszWKT = NULL; if (poGeom != NULL) poGeom->exportToWkt( &pszWKT ); oStmt.Appendf( "[%s] = ", pszGeomColumn ); if( pszWKT != NULL && (nGeomColumnType == MSSQLCOLTYPE_GEOMETRY || nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY)) { if (nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY) { oStmt.Append( "geography::STGeomFromText(" ); OGRMSSQLAppendEscaped(&oStmt, pszWKT); oStmt.Appendf(",%d)", nSRSId ); } else { oStmt.Append( "geometry::STGeomFromText(" ); OGRMSSQLAppendEscaped(&oStmt, pszWKT); oStmt.Appendf(",%d).MakeValid()", nSRSId ); } } else oStmt.Append( "null" ); bNeedComma = TRUE; CPLFree(pszWKT); } int nFieldCount = poFeatureDefn->GetFieldCount(); int i; for( i = 0; i < nFieldCount; i++ ) { if (bNeedComma) oStmt.Appendf( ", [%s] = ", poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); else { oStmt.Appendf( "[%s] = ", poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); bNeedComma = TRUE; } if( !poFeature->IsFieldSet( i ) ) oStmt.Append( "null" ); else AppendFieldValue(&oStmt, poFeature, i); } /* Add the WHERE clause */ oStmt.Appendf( " WHERE [%s] = %ld" , pszFIDColumn, poFeature->GetFID()); /* -------------------------------------------------------------------- */ /* Execute the update. */ /* -------------------------------------------------------------------- */ if( !oStmt.ExecuteSQL() ) { CPLError( CE_Failure, CPLE_AppDefined, "Error updating feature with FID:%ld, %s", poFeature->GetFID(), poDS->GetSession()->GetLastError() ); return OGRERR_FAILURE; } return OGRERR_NONE; }
OGRMSSQLSpatialSelectLayer::~OGRMSSQLSpatialSelectLayer() { ClearStatement(); CPLFree(pszBaseStatement); }
void OGRMSSQLSpatialTableLayer::ResetReading() { ClearStatement(); OGRMSSQLSpatialLayer::ResetReading(); }
OGRDB2SelectLayer::~OGRDB2SelectLayer() { ClearStatement(); CPLFree(pszBaseStatement); }
OGRErr OGRMSSQLSpatialTableLayer::CreateFeature( OGRFeature *poFeature ) { GetLayerDefn(); if( NULL == poFeature ) { CPLError( CE_Failure, CPLE_AppDefined, "NULL pointer to OGRFeature passed to CreateFeature()." ); return OGRERR_FAILURE; } ClearStatement(); CPLODBCStatement oStatement( poDS->GetSession() ); /* the fid values are retieved from the source layer */ if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) oStatement.Appendf("SET IDENTITY_INSERT [%s].[%s] ON;", pszSchemaName, pszTableName ); /* -------------------------------------------------------------------- */ /* Form the INSERT command. */ /* -------------------------------------------------------------------- */ oStatement.Appendf( "INSERT INTO [%s].[%s] (", pszSchemaName, pszTableName ); OGRMSSQLGeometryValidator oValidator(poFeature->GetGeometryRef()); OGRGeometry *poGeom = oValidator.GetValidGeometryRef(); if (poFeature->GetGeometryRef() != poGeom) { CPLError( CE_Warning, CPLE_NotSupported, "Geometry with FID = %ld has been modified.", poFeature->GetFID() ); } int bNeedComma = FALSE; if (poGeom != NULL && pszGeomColumn != NULL) { oStatement.Append( pszGeomColumn ); bNeedComma = TRUE; } if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) { if (bNeedComma) oStatement.Appendf( ", [%s]", pszFIDColumn ); else { oStatement.Appendf( "[%s]", pszFIDColumn ); bNeedComma = TRUE; } } int nFieldCount = poFeatureDefn->GetFieldCount(); int i; for( i = 0; i < nFieldCount; i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if (bNeedComma) oStatement.Appendf( ", [%s]", poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); else { oStatement.Appendf( "[%s]", poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); bNeedComma = TRUE; } } oStatement.Appendf( ") VALUES (" ); /* Set the geometry */ bNeedComma = FALSE; if(poGeom != NULL && pszGeomColumn != NULL) { char *pszWKT = NULL; //poGeom->setCoordinateDimension( nCoordDimension ); poGeom->exportToWkt( &pszWKT ); if( pszWKT != NULL && (nGeomColumnType == MSSQLCOLTYPE_GEOMETRY || nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY)) { if (nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY) { oStatement.Append( "geography::STGeomFromText(" ); OGRMSSQLAppendEscaped(&oStatement, pszWKT); oStatement.Appendf(",%d)", nSRSId ); } else { oStatement.Append( "geometry::STGeomFromText(" ); OGRMSSQLAppendEscaped(&oStatement, pszWKT); oStatement.Appendf(",%d).MakeValid()", nSRSId ); } } else oStatement.Append( "null" ); bNeedComma = TRUE; CPLFree(pszWKT); } /* Set the FID */ if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) { if (bNeedComma) oStatement.Appendf( ", %ld", poFeature->GetFID() ); else { oStatement.Appendf( "%ld", poFeature->GetFID() ); bNeedComma = TRUE; } } for( i = 0; i < nFieldCount; i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if (bNeedComma) oStatement.Append( ", " ); else bNeedComma = TRUE; AppendFieldValue(&oStatement, poFeature, i); } oStatement.Append( ");" ); if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) oStatement.Appendf("SET IDENTITY_INSERT [%s].[%s] OFF;", pszSchemaName, pszTableName ); /* -------------------------------------------------------------------- */ /* Execute the insert. */ /* -------------------------------------------------------------------- */ if( !oStatement.ExecuteSQL() ) { CPLError( CE_Failure, CPLE_AppDefined, "INSERT command for new feature failed. %s", poDS->GetSession()->GetLastError() ); return OGRERR_FAILURE; } return OGRERR_NONE; }
OGRSQLiteTableLayer::~OGRSQLiteTableLayer() { ClearStatement(); CPLFree(pszEscapedTableName); }
OGRGeomediaSelectLayer::~OGRGeomediaSelectLayer() { ClearStatement(); CPLFree(pszBaseStatement); }