int OGRMSSQLSpatialTableLayer::FetchSRSId() { CPLODBCStatement oStatement = CPLODBCStatement( poDS->GetSession() ); oStatement.Appendf( "select srid from geometry_columns " "where f_table_schema = '%s' and f_table_name = '%s'", pszSchemaName, pszTableName ); if( oStatement.ExecuteSQL() && oStatement.Fetch() ) { if ( oStatement.GetColData( 0 ) ) nSRSId = atoi( oStatement.GetColData( 0 ) ); } return nSRSId; }
CPLODBCStatement* OGRMSSQLSpatialTableLayer::BuildStatement(const char* pszColumns) { CPLODBCStatement* poStatement = new CPLODBCStatement( poDS->GetSession() ); poStatement->Append( "select " ); poStatement->Append( pszColumns ); poStatement->Append( " from " ); poStatement->Append( pszSchemaName ); poStatement->Append( "." ); poStatement->Append( pszTableName ); /* Append attribute query if we have it */ if( pszQuery != NULL ) poStatement->Appendf( " where (%s)", pszQuery ); /* If we have a spatial filter, query on it */ if ( m_poFilterGeom != NULL ) { if (nGeomColumnType == MSSQLCOLTYPE_GEOMETRY || nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY) { if( pszQuery == NULL ) poStatement->Append( " where" ); else poStatement->Append( " and" ); poStatement->Appendf(" [%s].STIntersects(", pszGeomColumn ); if (nGeomColumnType == MSSQLCOLTYPE_GEOGRAPHY) poStatement->Append( "geography::" ); else poStatement->Append( "geometry::" ); if ( m_sFilterEnvelope.MinX == m_sFilterEnvelope.MaxX || m_sFilterEnvelope.MinY == m_sFilterEnvelope.MaxY) poStatement->Appendf("STGeomFromText('POINT(%.15g %.15g)',%d)) = 1", m_sFilterEnvelope.MinX, m_sFilterEnvelope.MinY, nSRSId >= 0? nSRSId : 0); else poStatement->Appendf( "STGeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%d)) = 1", m_sFilterEnvelope.MinX, m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxX, m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxX, m_sFilterEnvelope.MaxY, m_sFilterEnvelope.MinX, m_sFilterEnvelope.MaxY, m_sFilterEnvelope.MinX, m_sFilterEnvelope.MinY, nSRSId >= 0? nSRSId : 0 ); } else { CPLError( CE_Failure, CPLE_AppDefined, "Spatial filter is supported only on geometry and geography column types." ); delete poStatement; return NULL; } } CPLDebug( "OGR_MSSQLSpatial", "ExecuteSQL(%s)", poStatement->GetCommand() ); if( poStatement->ExecuteSQL() ) return poStatement; else { delete poStatement; return NULL; } }