OGRErr OGRFeatureQuery::Compile(OGRFeatureDefn *poDefn, const char *pszExpression) { /* -------------------------------------------------------------------- */ /* Clear any existing expression. */ /* -------------------------------------------------------------------- */ if (pSWQExpr != NULL) { delete (swq_expr_node*) pSWQExpr; pSWQExpr = NULL; } /* -------------------------------------------------------------------- */ /* Build list of fields. */ /* -------------------------------------------------------------------- */ char **papszFieldNames; swq_field_type *paeFieldTypes; int iField; int nFieldCount = poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT; papszFieldNames = (char**) CPLMalloc(sizeof(char*) * nFieldCount); paeFieldTypes = (swq_field_type*) CPLMalloc(sizeof(swq_field_type) * nFieldCount); for (iField = 0; iField < poDefn->GetFieldCount(); iField++) { OGRFieldDefn *poField = poDefn->GetFieldDefn(iField); papszFieldNames[iField] = (char*) poField->GetNameRef(); switch (poField->GetType()) { case OFTInteger: paeFieldTypes[iField] = SWQ_INTEGER; break; case OFTReal: paeFieldTypes[iField] = SWQ_FLOAT; break; case OFTString: paeFieldTypes[iField] = SWQ_STRING; break; case OFTDate: case OFTTime: case OFTDateTime: paeFieldTypes[iField] = SWQ_TIMESTAMP; break; default: paeFieldTypes[iField] = SWQ_OTHER; break; } } iField = 0; while (iField < SPECIAL_FIELD_COUNT) { papszFieldNames[poDefn->GetFieldCount() + iField] = (char*) SpecialFieldNames[iField]; paeFieldTypes[poDefn->GetFieldCount() + iField] = SpecialFieldTypes[iField]; ++iField; } /* -------------------------------------------------------------------- */ /* Try to parse. */ /* -------------------------------------------------------------------- */ OGRErr eErr = OGRERR_NONE; CPLErr eCPLErr; poTargetDefn = poDefn; eCPLErr = swq_expr_compile(pszExpression, nFieldCount, papszFieldNames, paeFieldTypes, (swq_expr_node**) &pSWQExpr); if (eCPLErr != CE_None) { eErr = OGRERR_CORRUPT_DATA; pSWQExpr = NULL; } CPLFree(papszFieldNames); CPLFree(paeFieldTypes); return eErr; }
OGRErr OGRFeatureQuery::Compile( OGRFeatureDefn *poDefn, const char * pszExpression, int bCheck, swq_custom_func_registrar* poCustomFuncRegistrar ) { /* -------------------------------------------------------------------- */ /* Clear any existing expression. */ /* -------------------------------------------------------------------- */ if( pSWQExpr != NULL ) { delete (swq_expr_node *) pSWQExpr; pSWQExpr = NULL; } /* -------------------------------------------------------------------- */ /* Build list of fields. */ /* -------------------------------------------------------------------- */ char **papszFieldNames; swq_field_type *paeFieldTypes; int iField; int nFieldCount = poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT + poDefn->GetGeomFieldCount(); papszFieldNames = (char **) CPLMalloc(sizeof(char *) * nFieldCount ); paeFieldTypes = (swq_field_type *) CPLMalloc(sizeof(swq_field_type) * nFieldCount ); for( iField = 0; iField < poDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poField = poDefn->GetFieldDefn( iField ); papszFieldNames[iField] = (char *) poField->GetNameRef(); switch( poField->GetType() ) { case OFTInteger: { if( poField->GetSubType() == OFSTBoolean ) paeFieldTypes[iField] = SWQ_BOOLEAN; else paeFieldTypes[iField] = SWQ_INTEGER; break; } case OFTInteger64: { if( poField->GetSubType() == OFSTBoolean ) paeFieldTypes[iField] = SWQ_BOOLEAN; else paeFieldTypes[iField] = SWQ_INTEGER64; break; } case OFTReal: paeFieldTypes[iField] = SWQ_FLOAT; break; case OFTString: paeFieldTypes[iField] = SWQ_STRING; break; case OFTDate: case OFTTime: case OFTDateTime: paeFieldTypes[iField] = SWQ_TIMESTAMP; break; default: paeFieldTypes[iField] = SWQ_OTHER; break; } } iField = 0; while (iField < SPECIAL_FIELD_COUNT) { papszFieldNames[poDefn->GetFieldCount() + iField] = (char *) SpecialFieldNames[iField]; paeFieldTypes[poDefn->GetFieldCount() + iField] = SpecialFieldTypes[iField]; ++iField; } for( iField = 0; iField < poDefn->GetGeomFieldCount(); iField++ ) { OGRGeomFieldDefn *poField = poDefn->GetGeomFieldDefn( iField ); int iDstField = poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT + iField; papszFieldNames[iDstField] = (char *) poField->GetNameRef(); if( *papszFieldNames[iDstField] == '\0' ) papszFieldNames[iDstField] = (char*) OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME; paeFieldTypes[iDstField] = SWQ_GEOMETRY; } /* -------------------------------------------------------------------- */ /* Try to parse. */ /* -------------------------------------------------------------------- */ OGRErr eErr = OGRERR_NONE; CPLErr eCPLErr; poTargetDefn = poDefn; eCPLErr = swq_expr_compile( pszExpression, nFieldCount, papszFieldNames, paeFieldTypes, bCheck, poCustomFuncRegistrar, (swq_expr_node **) &pSWQExpr ); if( eCPLErr != CE_None ) { eErr = OGRERR_CORRUPT_DATA; pSWQExpr = NULL; } CPLFree( papszFieldNames ); CPLFree( paeFieldTypes ); return eErr; }