OGRLayer * OGRMemDataSource::ICreateLayer( const char *pszLayerName, OGRSpatialReference *poSRSIn, OGRwkbGeometryType eType, char **papszOptions ) { // Create the layer object. OGRSpatialReference* poSRS = poSRSIn; if( poSRS ) { poSRS = poSRS->Clone(); poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); } OGRMemLayer *poLayer = new OGRMemLayer(pszLayerName, poSRS, eType); if( poSRS ) { poSRS->Release(); } if( CPLFetchBool(papszOptions, "ADVERTIZE_UTF8", false) ) poLayer->SetAdvertizeUTF8(true); // Add layer to data source layer list. papoLayers = static_cast<OGRMemLayer **>( CPLRealloc(papoLayers, sizeof(OGRMemLayer *) * (nLayers + 1))); papoLayers[nLayers++] = poLayer; return poLayer; }
OGRLayer * OGRMemDataSource::ICreateLayer( const char * pszLayerName, OGRSpatialReference *poSRS, OGRwkbGeometryType eType, char ** papszOptions ) { /* -------------------------------------------------------------------- */ /* Create the layer object. */ /* -------------------------------------------------------------------- */ OGRMemLayer *poLayer = new OGRMemLayer( pszLayerName, poSRS, eType ); if( CPLFetchBool(papszOptions, "ADVERTIZE_UTF8", false) ) poLayer->SetAdvertizeUTF8(true); /* -------------------------------------------------------------------- */ /* Add layer to data source layer list. */ /* -------------------------------------------------------------------- */ papoLayers = static_cast<OGRMemLayer **>( CPLRealloc( papoLayers, sizeof(OGRMemLayer *) * (nLayers+1) ) ); papoLayers[nLayers++] = poLayer; return poLayer; }
OGRLayer* OGROpenFileGDBDataSource::ExecuteSQL( const char *pszSQLCommand, OGRGeometry *poSpatialFilter, const char *pszDialect ) { /* -------------------------------------------------------------------- */ /* Special case GetLayerDefinition */ /* -------------------------------------------------------------------- */ if (EQUALN(pszSQLCommand, "GetLayerDefinition ", strlen("GetLayerDefinition "))) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*) GetLayerByName(pszSQLCommand + strlen("GetLayerDefinition ")); if (poLayer) { OGRLayer* poRet = new OGROpenFileGDBSingleFeatureLayer( "LayerDefinition", poLayer->GetXMLDefinition().c_str() ); return poRet; } else return NULL; } /* -------------------------------------------------------------------- */ /* Special case GetLayerMetadata */ /* -------------------------------------------------------------------- */ if (EQUALN(pszSQLCommand, "GetLayerMetadata ", strlen("GetLayerMetadata "))) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*) GetLayerByName(pszSQLCommand + strlen("GetLayerMetadata ")); if (poLayer) { OGRLayer* poRet = new OGROpenFileGDBSingleFeatureLayer( "LayerMetadata", poLayer->GetXMLDocumentation().c_str() ); return poRet; } else return NULL; } /* -------------------------------------------------------------------- */ /* Special case GetLayerAttrIndexUse (only for debugging purposes) */ /* -------------------------------------------------------------------- */ if (EQUALN(pszSQLCommand, "GetLayerAttrIndexUse ", strlen("GetLayerAttrIndexUse "))) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*) GetLayerByName(pszSQLCommand + strlen("GetLayerAttrIndexUse ")); if (poLayer) { OGRLayer* poRet = new OGROpenFileGDBSingleFeatureLayer( "LayerAttrIndexUse", CPLSPrintf("%d", poLayer->GetAttrIndexUse()) ); return poRet; } else return NULL; } /* -------------------------------------------------------------------- */ /* Special case GetLayerSpatialIndexState (only for debugging purposes) */ /* -------------------------------------------------------------------- */ if (EQUALN(pszSQLCommand, "GetLayerSpatialIndexState ", strlen("GetLayerSpatialIndexState "))) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*) GetLayerByName(pszSQLCommand + strlen("GetLayerSpatialIndexState ")); if (poLayer) { OGRLayer* poRet = new OGROpenFileGDBSingleFeatureLayer( "LayerSpatialIndexState", CPLSPrintf("%d", poLayer->GetSpatialIndexState()) ); return poRet; } else return NULL; } /* -------------------------------------------------------------------- */ /* Special case GetLastSQLUsedOptimizedImplementation (only for debugging purposes) */ /* -------------------------------------------------------------------- */ if (EQUAL(pszSQLCommand, "GetLastSQLUsedOptimizedImplementation")) { OGRLayer* poRet = new OGROpenFileGDBSingleFeatureLayer( "GetLastSQLUsedOptimizedImplementation", CPLSPrintf("%d", bLastSQLUsedOptimizedImplementation) ); return poRet; } bLastSQLUsedOptimizedImplementation = FALSE; /* -------------------------------------------------------------------- */ /* Special cases for SQL optimizations */ /* -------------------------------------------------------------------- */ if( EQUALN(pszSQLCommand, "SELECT ", strlen("SELECT ")) && (pszDialect == NULL || EQUAL(pszDialect, "") || EQUAL(pszDialect, "OGRSQL")) && CSLTestBoolean(CPLGetConfigOption("OPENFILEGDB_USE_INDEX", "YES")) ) { swq_select oSelect; if( oSelect.preparse(pszSQLCommand) != CE_None ) return NULL; /* -------------------------------------------------------------------- */ /* MIN/MAX/SUM/AVG/COUNT optimization */ /* -------------------------------------------------------------------- */ if( oSelect.join_count == 0 && oSelect.poOtherSelect == NULL && oSelect.table_count == 1 && oSelect.order_specs == 0 && oSelect.query_mode != SWQM_DISTINCT_LIST ) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*)GetLayerByName( oSelect.table_defs[0].table_name); if( poLayer ) { OGRMemLayer* poMemLayer = NULL; int i; for(i = 0; i < oSelect.result_columns; i ++ ) { swq_col_func col_func = oSelect.column_defs[i].col_func; if( !(col_func == SWQCF_MIN || col_func == SWQCF_MAX || col_func == SWQCF_COUNT || col_func == SWQCF_AVG || col_func == SWQCF_SUM) ) break; if( oSelect.column_defs[i].field_name == NULL ) break; if( oSelect.column_defs[i].distinct_flag ) break; if( oSelect.column_defs[i].target_type != SWQ_OTHER ) break; int idx = poLayer->GetLayerDefn()->GetFieldIndex( oSelect.column_defs[i].field_name); if( idx < 0 ) break; OGRFieldDefn* poFieldDefn = poLayer->GetLayerDefn()->GetFieldDefn(idx); if( col_func == SWQCF_SUM && poFieldDefn->GetType() == OFTDateTime ) break; int eOutOGRType = -1; int nCount = 0; double dfSum = 0.0; const OGRField* psField = NULL; OGRField sField; if( col_func == SWQCF_MIN || col_func == SWQCF_MAX ) { psField = poLayer->GetMinMaxValue( poFieldDefn, col_func == SWQCF_MIN, eOutOGRType); if( eOutOGRType < 0 ) break; } else { double dfMin = 0.0, dfMax = 0.0; if( !poLayer->GetMinMaxSumCount(poFieldDefn, dfMin, dfMax, dfSum, nCount) ) break; psField = &sField; if( col_func == SWQCF_AVG ) { if( nCount == 0 ) { eOutOGRType = OFTReal; psField = NULL; } else { if( poFieldDefn->GetType() == OFTDateTime ) { eOutOGRType = OFTDateTime; FileGDBDoubleDateToOGRDate(dfSum / nCount, &sField); } else { eOutOGRType = OFTReal; sField.Real = dfSum / nCount; } } } else if( col_func == SWQCF_COUNT ) { sField.Integer = nCount; eOutOGRType = OFTInteger; } else { sField.Real = dfSum; eOutOGRType = OFTReal; } } if( poMemLayer == NULL ) { poMemLayer = new OGRMemLayer("SELECT", NULL, wkbNone); OGRFeature* poFeature = new OGRFeature(poMemLayer->GetLayerDefn()); poMemLayer->CreateFeature(poFeature); delete poFeature; } const char* pszMinMaxFieldName = CPLSPrintf( "%s_%s", (col_func == SWQCF_MIN) ? "MIN" : (col_func == SWQCF_MAX) ? "MAX" : (col_func == SWQCF_AVG) ? "AVG" : (col_func == SWQCF_SUM) ? "SUM" : "COUNT", oSelect.column_defs[i].field_name); OGRFieldDefn oFieldDefn(pszMinMaxFieldName, (OGRFieldType) eOutOGRType); poMemLayer->CreateField(&oFieldDefn); if( psField != NULL ) { OGRFeature* poFeature = poMemLayer->GetFeature(0); poFeature->SetField(oFieldDefn.GetNameRef(), (OGRField*) psField); poMemLayer->SetFeature(poFeature); delete poFeature; } } if( i != oSelect.result_columns ) { delete poMemLayer; } else { CPLDebug("OpenFileGDB", "Using optimized MIN/MAX/SUM/AVG/COUNT implementation"); bLastSQLUsedOptimizedImplementation = TRUE; return poMemLayer; } } } /* -------------------------------------------------------------------- */ /* ORDER BY optimization */ /* -------------------------------------------------------------------- */ if( oSelect.join_count == 0 && oSelect.poOtherSelect == NULL && oSelect.table_count == 1 && oSelect.order_specs == 1 && oSelect.query_mode != SWQM_DISTINCT_LIST ) { OGROpenFileGDBLayer* poLayer = (OGROpenFileGDBLayer*)GetLayerByName( oSelect.table_defs[0].table_name); if( poLayer != NULL && poLayer->HasIndexForField(oSelect.order_defs[0].field_name) ) { OGRErr eErr = OGRERR_NONE; if( oSelect.where_expr != NULL ) { /* The where must be a simple comparison on the column */ /* that is used for ordering */ if( oSelect.where_expr->eNodeType == SNT_OPERATION && OGROpenFileGDBIsComparisonOp(oSelect.where_expr->nOperation) && oSelect.where_expr->nOperation != SWQ_NE && oSelect.where_expr->nSubExprCount == 2 && (oSelect.where_expr->papoSubExpr[0]->eNodeType == SNT_COLUMN || oSelect.where_expr->papoSubExpr[0]->eNodeType == SNT_CONSTANT) && oSelect.where_expr->papoSubExpr[0]->field_type == SWQ_STRING && EQUAL(oSelect.where_expr->papoSubExpr[0]->string_value, oSelect.order_defs[0].field_name) && oSelect.where_expr->papoSubExpr[1]->eNodeType == SNT_CONSTANT ) { /* ok */ } else eErr = OGRERR_FAILURE; } if( eErr == OGRERR_NONE ) { int i; for(i = 0; i < oSelect.result_columns; i ++ ) { if( oSelect.column_defs[i].col_func != SWQCF_NONE ) break; if( oSelect.column_defs[i].field_name == NULL ) break; if( oSelect.column_defs[i].distinct_flag ) break; if( oSelect.column_defs[i].target_type != SWQ_OTHER ) break; if( strcmp(oSelect.column_defs[i].field_name, "*") != 0 && poLayer->GetLayerDefn()->GetFieldIndex( oSelect.column_defs[i].field_name) < 0 ) break; } if( i != oSelect.result_columns ) eErr = OGRERR_FAILURE; } if( eErr == OGRERR_NONE ) { int op = -1; swq_expr_node* poValue = NULL; if( oSelect.where_expr != NULL ) { op = oSelect.where_expr->nOperation; poValue = oSelect.where_expr->papoSubExpr[1]; } FileGDBIterator *poIter = poLayer->BuildIndex( oSelect.order_defs[0].field_name, oSelect.order_defs[0].ascending_flag, op, poValue); /* Check that they are no NULL values */ if( oSelect.where_expr == NULL && poIter->GetRowCount() != poLayer->GetFeatureCount(FALSE) ) { delete poIter; poIter = NULL; } if( poIter != NULL ) { CPLDebug("OpenFileGDB", "Using OGROpenFileGDBSimpleSQLLayer"); bLastSQLUsedOptimizedImplementation = TRUE; return new OGROpenFileGDBSimpleSQLLayer(poLayer, poIter, oSelect.result_columns, oSelect.column_defs); } } } } } return OGRDataSource::ExecuteSQL(pszSQLCommand, poSpatialFilter, pszDialect); }