ogrpgtablelayer.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,798 行 · 第 1/4 页
CPP
1,798 行
pszGeomColumn, sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MaxY, nSRSId ); } if( strlen(osQuery) > 0 ) { if( strlen(osWHERE) == 0 ) { osWHERE.Printf( "WHERE %s ", osQuery.c_str() ); } else { osWHERE += "AND "; osWHERE += osQuery; } } // XXX - mloskot - some debugging logic, can be removed if( bHasPostGISGeometry ) CPLDebug( "PG", "OGRPGTableLayer::BuildWhere returns: %s", osWHERE.c_str() ); else CPLDebug( "PG", "PostGIS is NOT available!" );}/************************************************************************//* BuildFullQueryStatement() *//************************************************************************/void OGRPGTableLayer::BuildFullQueryStatement(){ if( pszQueryStatement != NULL ) { CPLFree( pszQueryStatement ); pszQueryStatement = NULL; } char *pszFields = BuildFields(); pszQueryStatement = (char *) CPLMalloc(strlen(pszFields)+strlen(osWHERE) +strlen(pszSqlTableName) + 40); sprintf( pszQueryStatement, "SELECT %s FROM %s %s", pszFields, pszSqlTableName, osWHERE.c_str() ); CPLFree( pszFields );}/************************************************************************//* ResetReading() *//************************************************************************/void OGRPGTableLayer::ResetReading(){ bUseCopy = USE_COPY_UNSET; BuildFullQueryStatement(); OGRPGLayer::ResetReading();}/************************************************************************//* BuildFields() *//* *//* Build list of fields to fetch, performing any required *//* transformations (such as on geometry). *//************************************************************************/char *OGRPGTableLayer::BuildFields(){ int i = 0; int nSize = 0; char *pszFieldList = NULL; nSize = 25; if( pszGeomColumn ) nSize += strlen(pszGeomColumn); if( bHasFid ) nSize += strlen(pszFIDColumn); for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) nSize += strlen(poFeatureDefn->GetFieldDefn(i)->GetNameRef()) + 4; pszFieldList = (char *) CPLMalloc(nSize); pszFieldList[0] = '\0'; if( bHasFid && poFeatureDefn->GetFieldIndex( pszFIDColumn ) == -1 ) sprintf( pszFieldList, "\"%s\"", pszFIDColumn ); if( pszGeomColumn ) { if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); if( bHasPostGISGeometry ) { if ( poDS->bUseBinaryCursor ) { nSize += 10; sprintf( pszFieldList+strlen(pszFieldList), "AsBinary(\"%s\")", pszGeomColumn ); } else if ( poDS->sPostGISVersion.nMajor >= 1 ) sprintf( pszFieldList+strlen(pszFieldList), "AsEWKT(\"%s\")", pszGeomColumn ); else sprintf( pszFieldList+strlen(pszFieldList), "AsText(\"%s\")", pszGeomColumn ); } else { sprintf( pszFieldList+strlen(pszFieldList), "\"%s\"", pszGeomColumn ); } } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); strcat( pszFieldList, "\"" ); strcat( pszFieldList, pszName ); strcat( pszFieldList, "\"" ); } CPLAssert( (int) strlen(pszFieldList) < nSize ); return pszFieldList;}/************************************************************************//* SetAttributeFilter() *//************************************************************************/OGRErr OGRPGTableLayer::SetAttributeFilter( const char *pszQuery ){ if( pszQuery == NULL ) osQuery = ""; else osQuery = pszQuery; BuildWhere(); ResetReading(); return OGRERR_NONE;}/************************************************************************//* DeleteFeature() *//************************************************************************/OGRErr OGRPGTableLayer::DeleteFeature( long nFID ){ PGconn *hPGConn = poDS->GetPGConn(); PGresult *hResult = NULL; CPLString osCommand;/* -------------------------------------------------------------------- *//* We can only delete features if we have a well defined FID *//* column to target. *//* -------------------------------------------------------------------- */ if( !bHasFid ) { CPLError( CE_Failure, CPLE_AppDefined, "DeleteFeature(%d) failed. Unable to delete features in tables without\n" "a recognised FID column.", nFID ); return OGRERR_FAILURE; }/* -------------------------------------------------------------------- *//* Form the statement to drop the record. *//* -------------------------------------------------------------------- */ osCommand.Printf( "DELETE FROM %s WHERE \"%s\" = %ld", pszSqlTableName, pszFIDColumn, nFID );/* -------------------------------------------------------------------- *//* Execute the delete. *//* -------------------------------------------------------------------- */ OGRErr eErr; eErr = poDS->SoftStartTransaction(); if( eErr != OGRERR_NONE ) return eErr; CPLDebug( "OGR_PG", "PQexec(%s)\n", osCommand.c_str() ); hResult = PQexec(hPGConn, osCommand); if( PQresultStatus(hResult) != PGRES_COMMAND_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "DeleteFeature() DELETE statement failed.\n%s", PQerrorMessage(hPGConn) ); OGRPGClearResult( hResult ); poDS->SoftRollback(); eErr = OGRERR_FAILURE; } else { OGRPGClearResult( hResult ); eErr = poDS->SoftCommit(); } return eErr;}/************************************************************************//* SetFeature() *//* *//* SetFeature() is implemented by dropping the old copy of the *//* feature in question (if there is one) and then creating a *//* new one with the provided feature id. *//************************************************************************/OGRErr OGRPGTableLayer::SetFeature( OGRFeature *poFeature ){ OGRErr eErr(OGRERR_FAILURE); 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; } eErr = DeleteFeature( poFeature->GetFID() ); if( eErr != OGRERR_NONE ) return eErr; return CreateFeature( poFeature );}/************************************************************************//* CreateFeature() *//************************************************************************/OGRErr OGRPGTableLayer::CreateFeature( OGRFeature *poFeature ){ if( NULL == poFeature ) { CPLError( CE_Failure, CPLE_AppDefined, "NULL pointer to OGRFeature passed to CreateFeature()." ); return OGRERR_FAILURE; } // We avoid testing the config option too often. if( bUseCopy == USE_COPY_UNSET ) bUseCopy = CSLTestBoolean( CPLGetConfigOption( "PG_USE_COPY", "NO") ); if( !bUseCopy ) { return CreateFeatureViaInsert( poFeature ); } else { if ( !bCopyActive ) StartCopy(); return CreateFeatureViaCopy( poFeature ); }}/************************************************************************//* CreateFeatureViaInsert() *//************************************************************************/OGRErr OGRPGTableLayer::CreateFeatureViaInsert( OGRFeature *poFeature ){ PGconn *hPGConn = poDS->GetPGConn(); PGresult *hResult = NULL; CPLString osCommand; int i = 0; int bNeedComma = FALSE; OGRErr eErr = OGRERR_FAILURE; if( NULL == poFeature ) { CPLError( CE_Failure, CPLE_AppDefined, "NULL pointer to OGRFeature passed to CreateFeatureViaInsert()." ); return eErr; } eErr = poDS->SoftStartTransaction(); if( eErr != OGRERR_NONE ) { return eErr; }/* -------------------------------------------------------------------- *//* Form the INSERT command. *//* -------------------------------------------------------------------- */ osCommand.Printf( "INSERT INTO %s (", pszSqlTableName ); if( bHasWkb && poFeature->GetGeometryRef() != NULL ) { osCommand += "WKB_GEOMETRY "; bNeedComma = TRUE; } if( bHasPostGISGeometry && poFeature->GetGeometryRef() != NULL ) { osCommand = osCommand + pszGeomColumn + " "; bNeedComma = TRUE; } if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) { if( bNeedComma ) osCommand += ", "; osCommand = osCommand + pszFIDColumn + " "; bNeedComma = TRUE; } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if( !bNeedComma ) bNeedComma = TRUE; else osCommand += ", "; osCommand = osCommand + "\"" + poFeatureDefn->GetFieldDefn(i)->GetNameRef() + "\""; } osCommand += ") VALUES ("; /* Set the geometry */ bNeedComma = poFeature->GetGeometryRef() != NULL; if( bHasPostGISGeometry && poFeature->GetGeometryRef() != NULL) { char *pszWKT = NULL; if( poFeature->GetGeometryRef() != NULL ) { OGRGeometry *poGeom = (OGRGeometry *) poFeature->GetGeometryRef(); poGeom->closeRings(); poGeom->setCoordinateDimension( nCoordDimension ); poGeom->exportToWkt( &pszWKT ); } if( pszWKT != NULL ) { if( poDS->sPostGISVersion.nMajor >= 1 ) osCommand += CPLString().Printf( "GeomFromEWKT('SRID=%d;%s'::TEXT) ", nSRSId, pszWKT ); else osCommand += CPLString().Printf( "GeometryFromText('%s'::TEXT,%d) ", pszWKT, nSRSId ); OGRFree( pszWKT ); } else osCommand += "''"; } else if( bHasWkb && !bWkbAsOid && poFeature->GetGeometryRef() != NULL ) { char *pszBytea = GeometryToBYTEA( poFeature->GetGeometryRef() ); if( pszBytea != NULL ) { osCommand = osCommand + "'" + pszBytea + "'"; CPLFree( pszBytea ); } else osCommand += "''"; } else if( bHasWkb && bWkbAsOid && poFeature->GetGeometryRef() != NULL ) { Oid oid = GeometryToOID( poFeature->GetGeometryRef() ); if( oid != 0 ) { osCommand += CPLString().Printf( "'%d' ", oid ); } else osCommand += "''"; } /* Set the FID */ if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) { if( bNeedComma ) osCommand += ", "; osCommand += CPLString().Printf( "%ld ", poFeature->GetFID() ); bNeedComma = TRUE; } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { // Flag indicating NULL or not-a-date date value // e.g. 0000-00-00 - there is no year 0 OGRBoolean bIsDateNull = FALSE; const char *pszStrValue = poFeature->GetFieldAsString(i); char *pszNeedToFree = NULL; if( !poFeature->IsFieldSet( i ) ) continue; if( bNeedComma ) osCommand += ", "; else bNeedComma = TRUE; // We need special formatting for integer list values. if( poFeatureDefn->GetFieldDefn(i)->GetType() == OFTIntegerList ) { int nCount, nOff = 0, j; const int *panItems = poFeature->GetFieldAsIntegerList(i,&nCount); pszNeedToFree = (char *) CPLMalloc(nCount * 13 + 10); strcpy( pszNeedToFree, "{" ); for( j = 0; j < nCount; j++ ) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?