int OGRSQLiteSelectLayer::TestCapability( const char * pszCap ) { if (EQUAL(pszCap,OLCFastSpatialFilter)) { if (osSQLCurrent != osSQLBase) return TRUE; size_t i = 0; OGRSQLiteLayer* poBaseLayer = GetBaseLayer(i); if (poBaseLayer == NULL) { CPLDebug("SQLITE", "Cannot find base layer"); return FALSE; } OGRPolygon oFakePoly; const char* pszWKT = "POLYGON((0 0,0 1,1 1,1 0,0 0))"; oFakePoly.importFromWkt((char**) &pszWKT); CPLString osSpatialWhere = poBaseLayer->GetSpatialWhere(&oFakePoly); return osSpatialWhere.size() != 0; } else return OGRSQLiteLayer::TestCapability( pszCap ); }
int OGRSQLiteSelectLayerCommonBehaviour::TestCapability( const char * pszCap ) { if (EQUAL(pszCap,OLCFastSpatialFilter)) { size_t i = 0; std::pair<OGRLayer*, IOGRSQLiteGetSpatialWhere*> oPair = GetBaseLayer(i); if (oPair.first == NULL) { CPLDebug("SQLITE", "Cannot find base layer"); return FALSE; } return oPair.second->HasFastSpatialFilter(0); } else return poLayer->BaseTestCapability( pszCap ); }
int OGRSQLiteSelectLayerCommonBehaviour::BuildSQL() { osSQLCurrent = osSQLBase; bSpatialFilterInSQL = TRUE; size_t i = 0; std::pair<OGRLayer*, IOGRSQLiteGetSpatialWhere*> oPair = GetBaseLayer(i); OGRLayer* poBaseLayer = oPair.first; if (poBaseLayer == NULL) { CPLDebug("SQLITE", "Cannot find base layer"); bSpatialFilterInSQL = FALSE; return FALSE; } CPLString osSpatialWhere; if (poLayer->GetFilterGeom() != NULL) { const char* pszGeomCol = poLayer->GetLayerDefn()->GetGeomFieldDefn(poLayer->GetIGeomFieldFilter())->GetNameRef(); int nIdx = poBaseLayer->GetLayerDefn()->GetGeomFieldIndex(pszGeomCol); if( nIdx < 0 ) { CPLDebug("SQLITE", "Cannot find field %s in base layer", pszGeomCol); bSpatialFilterInSQL = FALSE; } else { osSpatialWhere = oPair.second->GetSpatialWhere(nIdx, poLayer->GetFilterGeom()); if (osSpatialWhere.size() == 0) { CPLDebug("SQLITE", "Cannot get spatial where clause"); bSpatialFilterInSQL = FALSE; } } } CPLString osCustomWhere; if( osSpatialWhere.size() != 0 ) { osCustomWhere = osSpatialWhere; } if( poLayer->GetAttrQueryString() != NULL && poLayer->GetAttrQueryString()[0] != '\0' ) { if( osSpatialWhere.size() != 0) osCustomWhere += " AND ("; osCustomWhere += poLayer->GetAttrQueryString(); if( osSpatialWhere.size() != 0) osCustomWhere += ")"; } /* Nothing to do */ if( osCustomWhere.size() == 0 ) return TRUE; while (i < osSQLBase.size() && osSQLBase[i] == ' ') i ++; if (i < osSQLBase.size() && STARTS_WITH_CI(osSQLBase.c_str() + i, "WHERE ")) { osSQLCurrent = osSQLBase.substr(0, i + 6); osSQLCurrent += osCustomWhere; osSQLCurrent += " AND ("; size_t nEndOfWhere = osSQLBase.ifind(" GROUP "); if (nEndOfWhere == std::string::npos) nEndOfWhere = osSQLBase.ifind(" ORDER "); if (nEndOfWhere == std::string::npos) nEndOfWhere = osSQLBase.ifind(" LIMIT "); if (nEndOfWhere == std::string::npos) { osSQLCurrent += osSQLBase.substr(i + 6); osSQLCurrent += ")"; } else { osSQLCurrent += osSQLBase.substr(i + 6, nEndOfWhere - (i + 6)); osSQLCurrent += ")"; osSQLCurrent += osSQLBase.substr(nEndOfWhere); } } else if (i < osSQLBase.size() && (STARTS_WITH_CI(osSQLBase.c_str() + i, "GROUP ") || STARTS_WITH_CI(osSQLBase.c_str() + i, "ORDER ") || STARTS_WITH_CI(osSQLBase.c_str() + i, "LIMIT "))) { osSQLCurrent = osSQLBase.substr(0, i); osSQLCurrent += " WHERE "; osSQLCurrent += osCustomWhere; osSQLCurrent += " "; osSQLCurrent += osSQLBase.substr(i); } else if (i == osSQLBase.size()) { osSQLCurrent = osSQLBase.substr(0, i); osSQLCurrent += " WHERE "; osSQLCurrent += osCustomWhere; } else { CPLDebug("SQLITE", "SQL expression too complex for the driver to insert attribute and/or spatial filter in it"); bSpatialFilterInSQL = FALSE; return FALSE; } return TRUE; }
int OGRSQLiteSelectLayer::RebuildSQLWithSpatialClause() { osSQLCurrent = osSQLBase; if (m_poFilterGeom == NULL) { return TRUE; } size_t i = 0; OGRSQLiteLayer* poBaseLayer = GetBaseLayer(i); if (poBaseLayer == NULL) { CPLDebug("SQLITE", "Cannot find base layer"); return FALSE; } CPLString osSpatialWhere = poBaseLayer->GetSpatialWhere(m_poFilterGeom); if (osSpatialWhere.size() == 0) { CPLDebug("SQLITE", "Cannot get spatial where clause"); return FALSE; } while (i < osSQLBase.size() && osSQLBase[i] == ' ') i ++; if (i < osSQLBase.size() && EQUALN(osSQLBase.c_str() + i, "WHERE ", 6)) { osSQLCurrent = osSQLBase.substr(0, i + 6); osSQLCurrent += osSpatialWhere; osSQLCurrent += " AND ("; size_t nEndOfWhere = osSQLBase.ifind(" GROUP "); if (nEndOfWhere == std::string::npos) nEndOfWhere = osSQLBase.ifind(" ORDER "); if (nEndOfWhere == std::string::npos) nEndOfWhere = osSQLBase.ifind(" LIMIT "); if (nEndOfWhere == std::string::npos) { osSQLCurrent += osSQLBase.substr(i + 6); osSQLCurrent += ")"; } else { osSQLCurrent += osSQLBase.substr(i + 6, nEndOfWhere - (i + 6)); osSQLCurrent += ")"; osSQLCurrent += osSQLBase.substr(nEndOfWhere); } } else if (i < osSQLBase.size() && (EQUALN(osSQLBase.c_str() + i, "GROUP ", 6) || EQUALN(osSQLBase.c_str() + i, "ORDER ", 6) || EQUALN(osSQLBase.c_str() + i, "LIMIT ", 6))) { osSQLCurrent = osSQLBase.substr(0, i); osSQLCurrent += " WHERE "; osSQLCurrent += osSpatialWhere; osSQLCurrent += " "; osSQLCurrent += osSQLBase.substr(i); } else if (i == osSQLBase.size()) { osSQLCurrent = osSQLBase.substr(0, i); osSQLCurrent += " WHERE "; osSQLCurrent += osSpatialWhere; } else { CPLDebug("SQLITE", "SQL expression too complex for the driver to insert spatial filter in it"); return FALSE; } return TRUE; }