OGRPGResultLayer::OGRPGResultLayer( OGRPGDataSource *poDSIn, const char * pszRawQueryIn, PGresult *hInitialResultIn ) { poDS = poDSIn; iNextShapeId = 0; pszRawStatement = CPLStrdup(pszRawQueryIn); osWHERE = ""; BuildFullQueryStatement(); ReadResultDefinition(hInitialResultIn); pszGeomTableName = NULL; pszGeomTableSchemaName = NULL; /* Find at which index the geometry column is */ int iGeomCol = -1; if (poFeatureDefn->GetGeomFieldCount() == 1) { int iRawField; for( iRawField = 0; iRawField < PQnfields(hInitialResultIn); iRawField++ ) { if( strcmp(PQfname(hInitialResultIn,iRawField), poFeatureDefn->GetGeomFieldDefn(0)->GetNameRef()) == 0 ) { iGeomCol = iRawField; break; } } } #ifndef PG_PRE74 /* Determine the table from which the geometry column is extracted */ if (iGeomCol != -1) { Oid tableOID = PQftable(hInitialResultIn, iGeomCol); if (tableOID != InvalidOid) { CPLString osGetTableName; osGetTableName.Printf("SELECT c.relname, n.nspname FROM pg_class c " "JOIN pg_namespace n ON c.relnamespace=n.oid WHERE c.oid = %d ", tableOID); PGresult* hTableNameResult = OGRPG_PQexec(poDS->GetPGConn(), osGetTableName ); if( hTableNameResult && PQresultStatus(hTableNameResult) == PGRES_TUPLES_OK) { if ( PQntuples(hTableNameResult) > 0 ) { pszGeomTableName = CPLStrdup(PQgetvalue(hTableNameResult,0,0)); pszGeomTableSchemaName = CPLStrdup(PQgetvalue(hTableNameResult,0,1)); } } OGRPGClearResult( hTableNameResult ); } } #endif }
OGRPGResultLayer::OGRPGResultLayer( OGRPGDataSource *poDSIn, const char * pszRawQueryIn, PGresult *hInitialResultIn ) { poDS = poDSIn; iNextShapeId = 0; pszRawStatement = CPLStrdup(pszRawQueryIn); osWHERE = ""; BuildFullQueryStatement(); poFeatureDefn = ReadResultDefinition(hInitialResultIn); /* We have to get the SRID of the geometry column, so to be able */ /* to do spatial filtering */ if (bHasPostGISGeometry) { CPLString osGetSRID; osGetSRID += "SELECT getsrid(\""; osGetSRID += pszGeomColumn; osGetSRID += "\") FROM ("; osGetSRID += pszRawStatement; osGetSRID += ") AS ogrpggetsrid LIMIT 1"; PGresult* hSRSIdResult = PQexec(poDS->GetPGConn(), osGetSRID ); if( hSRSIdResult && PQresultStatus(hSRSIdResult) == PGRES_TUPLES_OK) { if ( PQntuples(hSRSIdResult) > 0 ) nSRSId = atoi(PQgetvalue(hSRSIdResult, 0, 0)); } else { CPLError( CE_Failure, CPLE_AppDefined, "%s", PQerrorMessage(poDS->GetPGConn()) ); } OGRPGClearResult(hSRSIdResult); } else if (bHasPostGISGeography) { // FIXME? But for the moment, PostGIS 1.5 only handles SRID:4326. nSRSId = 4326; } /* Now set the cursor that will fetch the first rows */ /* This is usefull when used in situations like */ /* ds->ReleaseResultSet(ds->ExecuteSQL("SELECT AddGeometryColumn(....)")) */ /* when people don't actually try to get elements */ SetInitialQueryCursor(); }
OGRMySQLResultLayer::OGRMySQLResultLayer( OGRMySQLDataSource *poDSIn, const char * pszRawQueryIn, MYSQL_RES *hResultSetIn ) : pszRawStatement(CPLStrdup(pszRawQueryIn)) { poDS = poDSIn; iNextShapeId = 0; hResultSet = hResultSetIn; BuildFullQueryStatement(); poFeatureDefn = ReadResultDefinition(); }
OGRIngresResultLayer::OGRIngresResultLayer( OGRIngresDataSource *poDSIn, const char * pszRawQueryIn, OGRIngresStatement *poResultSetIn ) { poDS = poDSIn; iNextShapeId = 0; pszRawStatement = CPLStrdup(pszRawQueryIn); poResultSet = poResultSetIn; BuildFullQueryStatement(); poFeatureDefn = ReadResultDefinition(); nFeatureCount = 0; }
OGRPGResultLayer::OGRPGResultLayer( OGRPGDataSource *poDSIn, const char * pszRawQueryIn, PGresult *hInitialResultIn ) { poDS = poDSIn; iNextShapeId = 0; pszRawStatement = CPLStrdup(pszRawQueryIn); osWHERE = ""; BuildFullQueryStatement(); ReadResultDefinition(hInitialResultIn); pszGeomTableName = NULL; pszGeomTableSchemaName = NULL; /* Find at which index the geometry column is */ /* and prepare a request to identify not-nullable fields */ int iGeomCol = -1; CPLString osRequest; std::map< std::pair<int,int>, int> oMapAttributeToFieldIndex; int iRawField; for( iRawField = 0; iRawField < PQnfields(hInitialResultIn); iRawField++ ) { if( poFeatureDefn->GetGeomFieldCount() == 1 && strcmp(PQfname(hInitialResultIn,iRawField), poFeatureDefn->GetGeomFieldDefn(0)->GetNameRef()) == 0 ) { iGeomCol = iRawField; } Oid tableOID = PQftable(hInitialResultIn, iRawField); int tableCol = PQftablecol(hInitialResultIn, iRawField); if( tableOID != InvalidOid && tableCol > 0 ) { if( osRequest.size() ) osRequest += " OR "; osRequest += "(attrelid = "; osRequest += CPLSPrintf("%d", tableOID); osRequest += " AND attnum = "; osRequest += CPLSPrintf("%d)", tableCol); oMapAttributeToFieldIndex[std::pair<int,int>(tableOID,tableCol)] = iRawField; } } if( osRequest.size() ) { osRequest = "SELECT attnum, attrelid FROM pg_attribute WHERE attnotnull = 't' AND (" + osRequest + ")"; PGresult* hResult = OGRPG_PQexec(poDS->GetPGConn(), osRequest ); if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK) { int iCol; for( iCol = 0; iCol < PQntuples(hResult); iCol++ ) { const char* pszAttNum = PQgetvalue(hResult,iCol,0); const char* pszAttRelid = PQgetvalue(hResult,iCol,1); int iRawField = oMapAttributeToFieldIndex[std::pair<int,int>(atoi(pszAttRelid),atoi(pszAttNum))]; const char* pszFieldname = PQfname(hInitialResultIn,iRawField); int iFieldIdx = poFeatureDefn->GetFieldIndex(pszFieldname); if( iFieldIdx >= 0 ) poFeatureDefn->GetFieldDefn(iFieldIdx)->SetNullable(FALSE); else { iFieldIdx = poFeatureDefn->GetGeomFieldIndex(pszFieldname); if( iFieldIdx >= 0 ) poFeatureDefn->GetGeomFieldDefn(iFieldIdx)->SetNullable(FALSE); } } } OGRPGClearResult( hResult ); } #ifndef PG_PRE74 /* Determine the table from which the geometry column is extracted */ if (iGeomCol != -1) { Oid tableOID = PQftable(hInitialResultIn, iGeomCol); if (tableOID != InvalidOid) { CPLString osGetTableName; osGetTableName.Printf("SELECT c.relname, n.nspname FROM pg_class c " "JOIN pg_namespace n ON c.relnamespace=n.oid WHERE c.oid = %d ", tableOID); PGresult* hTableNameResult = OGRPG_PQexec(poDS->GetPGConn(), osGetTableName ); if( hTableNameResult && PQresultStatus(hTableNameResult) == PGRES_TUPLES_OK) { if ( PQntuples(hTableNameResult) > 0 ) { pszGeomTableName = CPLStrdup(PQgetvalue(hTableNameResult,0,0)); pszGeomTableSchemaName = CPLStrdup(PQgetvalue(hTableNameResult,0,1)); } } OGRPGClearResult( hTableNameResult ); } } #endif }