00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef SWQ_H_INCLUDED_
00022 #define SWQ_H_INCLUDED_
00023
00024 #include "cpl_conv.h"
00025 #include "cpl_string.h"
00026 #include "ogr_core.h"
00027
00028 #if defined(_WIN32) && !defined(strcasecmp)
00029 # define strcasecmp stricmp
00030 #endif
00031
00032 typedef enum {
00033 SWQ_OR,
00034 SWQ_AND,
00035 SWQ_NOT,
00036 SWQ_EQ,
00037 SWQ_NE,
00038 SWQ_GE,
00039 SWQ_LE,
00040 SWQ_LT,
00041 SWQ_GT,
00042 SWQ_LIKE,
00043 SWQ_ISNULL,
00044 SWQ_IN,
00045 SWQ_BETWEEN,
00046 SWQ_ADD,
00047 SWQ_SUBTRACT,
00048 SWQ_MULTIPLY,
00049 SWQ_DIVIDE,
00050 SWQ_MODULUS,
00051 SWQ_CONCAT,
00052 SWQ_SUBSTR,
00053 SWQ_HSTORE_GET_VALUE,
00054 SWQ_AVG,
00055 SWQ_MIN,
00056 SWQ_MAX,
00057 SWQ_COUNT,
00058 SWQ_SUM,
00059 SWQ_CAST,
00060 SWQ_CUSTOM_FUNC,
00061 SWQ_ARGUMENT_LIST
00062 } swq_op;
00063
00064 typedef enum {
00065 SWQ_INTEGER,
00066 SWQ_INTEGER64,
00067 SWQ_FLOAT,
00068 SWQ_STRING,
00069 SWQ_BOOLEAN,
00070 SWQ_DATE,
00071 SWQ_TIME,
00072 SWQ_TIMESTAMP,
00073 SWQ_GEOMETRY,
00074 SWQ_NULL,
00075 SWQ_OTHER,
00076 SWQ_ERROR
00077 } swq_field_type;
00078
00079 #define SWQ_IS_INTEGER(x) ((x) == SWQ_INTEGER || (x) == SWQ_INTEGER64)
00080
00081 typedef enum {
00082 SNT_CONSTANT,
00083 SNT_COLUMN,
00084 SNT_OPERATION
00085 } swq_node_type;
00086
00087
00088 class swq_field_list;
00089 class swq_expr_node;
00090 class swq_select;
00091 class OGRGeometry;
00092
00093 typedef swq_expr_node *(*swq_field_fetcher)( swq_expr_node *op,
00094 void *record_handle );
00095 typedef swq_expr_node *(*swq_op_evaluator)(swq_expr_node *op,
00096 swq_expr_node **sub_field_values );
00097 typedef swq_field_type (*swq_op_checker)( swq_expr_node *op,
00098 int bAllowMismatchTypeOnFieldComparison );
00099
00100 class swq_custom_func_registrar;
00101
00102 class swq_expr_node {
00103 public:
00104 swq_expr_node();
00105
00106 explicit swq_expr_node( const char * );
00107 explicit swq_expr_node( int );
00108 explicit swq_expr_node( GIntBig );
00109 explicit swq_expr_node( double );
00110 explicit swq_expr_node( OGRGeometry* );
00111 explicit swq_expr_node( swq_op );
00112
00113 ~swq_expr_node();
00114
00115 void Initialize();
00116 CPLString UnparseOperationFromUnparsedSubExpr(char** apszSubExpr);
00117 char *Unparse( swq_field_list *, char chColumnQuote );
00118 void Dump( FILE *fp, int depth );
00119 swq_field_type Check( swq_field_list *, int bAllowFieldsInSecondaryTables,
00120 int bAllowMismatchTypeOnFieldComparison,
00121 swq_custom_func_registrar* poCustomFuncRegistrar );
00122 swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
00123 void *record );
00124 swq_expr_node* Clone();
00125
00126 void ReplaceBetweenByGEAndLERecurse();
00127
00128 swq_node_type eNodeType;
00129 swq_field_type field_type;
00130
00131
00132 void PushSubExpression( swq_expr_node * );
00133 void ReverseSubExpressions();
00134 int nOperation;
00135 int nSubExprCount;
00136 swq_expr_node **papoSubExpr;
00137
00138
00139 int field_index;
00140 int table_index;
00141 char *table_name;
00142
00143
00144 int is_null;
00145 GIntBig int_value;
00146 double float_value;
00147 OGRGeometry *geometry_value;
00148
00149
00150
00151 char *string_value;
00152
00153 static CPLString QuoteIfNecessary( const CPLString &, char chQuote = '\'' );
00154 static CPLString Quote( const CPLString &, char chQuote = '\'' );
00155 };
00156
00157 typedef struct {
00158 const char* pszName;
00159 swq_op eOperation;
00160 swq_op_evaluator pfnEvaluator;
00161 swq_op_checker pfnChecker;
00162 } swq_operation;
00163
00164 class swq_op_registrar {
00165 public:
00166 static const swq_operation *GetOperator( const char * );
00167 static const swq_operation *GetOperator( swq_op eOperation );
00168 };
00169
00170 class swq_custom_func_registrar
00171 {
00172 public:
00173 virtual ~swq_custom_func_registrar() {}
00174 virtual const swq_operation *GetOperator( const char * ) = 0;
00175 };
00176
00177
00178 typedef struct {
00179 char *data_source;
00180 char *table_name;
00181 char *table_alias;
00182 } swq_table_def;
00183
00184 class swq_field_list {
00185 public:
00186 int count;
00187 char **names;
00188 swq_field_type *types;
00189 int *table_ids;
00190 int *ids;
00191
00192 int table_count;
00193 swq_table_def *table_defs;
00194 };
00195
00196 class swq_parse_context {
00197 public:
00198 swq_parse_context() : nStartToken(0), pszInput(NULL), pszNext(NULL),
00199 pszLastValid(NULL), bAcceptCustomFuncs(FALSE),
00200 poRoot(NULL), poCurSelect(NULL) {}
00201
00202 int nStartToken;
00203 const char *pszInput;
00204 const char *pszNext;
00205 const char *pszLastValid;
00206 int bAcceptCustomFuncs;
00207
00208 swq_expr_node *poRoot;
00209
00210 swq_select *poCurSelect;
00211 };
00212
00213
00214
00215
00216
00217 int swqparse( swq_parse_context *context );
00218 int swqlex( swq_expr_node **ppNode, swq_parse_context *context );
00219 void swqerror( swq_parse_context *context, const char *msg );
00220
00221 int swq_identify_field( const char* table_name,
00222 const char *token, swq_field_list *field_list,
00223 swq_field_type *this_type, int *table_id );
00224
00225 CPLErr swq_expr_compile( const char *where_clause,
00226 int field_count,
00227 char **field_list,
00228 swq_field_type *field_types,
00229 int bCheck,
00230 swq_custom_func_registrar* poCustomFuncRegistrar,
00231 swq_expr_node **expr_root );
00232
00233 CPLErr swq_expr_compile2( const char *where_clause,
00234 swq_field_list *field_list,
00235 int bCheck,
00236 swq_custom_func_registrar* poCustomFuncRegistrar,
00237 swq_expr_node **expr_root );
00238
00239
00240
00241
00242 int swq_test_like( const char *input, const char *pattern );
00243
00244 swq_expr_node *SWQGeneralEvaluator( swq_expr_node *, swq_expr_node **);
00245 swq_field_type SWQGeneralChecker( swq_expr_node *node, int bAllowMismatchTypeOnFieldComparison );
00246 swq_expr_node *SWQCastEvaluator( swq_expr_node *, swq_expr_node **);
00247 swq_field_type SWQCastChecker( swq_expr_node *node, int bAllowMismatchTypeOnFieldComparison );
00248 const char* SWQFieldTypeToString( swq_field_type field_type );
00249
00250
00251
00252 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
00253
00254 #define SWQM_SUMMARY_RECORD 1
00255 #define SWQM_RECORDSET 2
00256 #define SWQM_DISTINCT_LIST 3
00257
00258 typedef enum {
00259 SWQCF_NONE = 0,
00260 SWQCF_AVG = SWQ_AVG,
00261 SWQCF_MIN = SWQ_MIN,
00262 SWQCF_MAX = SWQ_MAX,
00263 SWQCF_COUNT = SWQ_COUNT,
00264 SWQCF_SUM = SWQ_SUM,
00265 SWQCF_CUSTOM
00266 } swq_col_func;
00267
00268 typedef struct {
00269 swq_col_func col_func;
00270 char *table_name;
00271 char *field_name;
00272 char *field_alias;
00273 int table_index;
00274 int field_index;
00275 swq_field_type field_type;
00276 swq_field_type target_type;
00277 OGRFieldSubType target_subtype;
00278 int field_length;
00279 int field_precision;
00280 int distinct_flag;
00281 OGRwkbGeometryType eGeomType;
00282 int nSRID;
00283 swq_expr_node *expr;
00284 } swq_col_def;
00285
00286 typedef struct {
00287 GIntBig count;
00288
00289 char **distinct_list;
00290 double sum;
00291 double min;
00292 double max;
00293 char szMin[32];
00294 char szMax[32];
00295 } swq_summary;
00296
00297 typedef struct {
00298 char *table_name;
00299 char *field_name;
00300 int table_index;
00301 int field_index;
00302 int ascending_flag;
00303 } swq_order_def;
00304
00305 typedef struct {
00306 int secondary_table;
00307 swq_expr_node *poExpr;
00308 } swq_join_def;
00309
00310 class swq_select_parse_options
00311 {
00312 public:
00313 swq_custom_func_registrar* poCustomFuncRegistrar;
00314 int bAllowFieldsInSecondaryTablesInWhere;
00315 int bAddSecondaryTablesGeometryFields;
00316 int bAlwaysPrefixWithTableName;
00317 int bAllowDistinctOnGeometryField;
00318 int bAllowDistinctOnMultipleFields;
00319
00320 swq_select_parse_options(): poCustomFuncRegistrar(NULL),
00321 bAllowFieldsInSecondaryTablesInWhere(FALSE),
00322 bAddSecondaryTablesGeometryFields(FALSE),
00323 bAlwaysPrefixWithTableName(FALSE),
00324 bAllowDistinctOnGeometryField(FALSE),
00325 bAllowDistinctOnMultipleFields(FALSE) {}
00326 };
00327
00328 class swq_select
00329 {
00330 void postpreparse();
00331
00332 public:
00333 swq_select();
00334 ~swq_select();
00335
00336 int query_mode;
00337
00338 char *raw_select;
00339
00340 int PushField( swq_expr_node *poExpr, const char *pszAlias=NULL,
00341 int distinct_flag = FALSE );
00342 int result_columns;
00343 swq_col_def *column_defs;
00344 swq_summary *column_summary;
00345
00346 int PushTableDef( const char *pszDataSource,
00347 const char *pszTableName,
00348 const char *pszAlias );
00349 int table_count;
00350 swq_table_def *table_defs;
00351
00352 void PushJoin( int iSecondaryTable, swq_expr_node* poExpr );
00353 int join_count;
00354 swq_join_def *join_defs;
00355
00356 swq_expr_node *where_expr;
00357
00358 void PushOrderBy( const char* pszTableName, const char *pszFieldName, int bAscending );
00359 int order_specs;
00360 swq_order_def *order_defs;
00361
00362 swq_select *poOtherSelect;
00363 void PushUnionAll( swq_select* poOtherSelectIn );
00364
00365 CPLErr preparse( const char *select_statement,
00366 int bAcceptCustomFuncs = FALSE );
00367 CPLErr expand_wildcard( swq_field_list *field_list,
00368 int bAlwaysPrefixWithTableName );
00369 CPLErr parse( swq_field_list *field_list,
00370 swq_select_parse_options* poParseOptions );
00371
00372 char *Unparse();
00373 void Dump( FILE * );
00374 };
00375
00376 CPLErr swq_select_parse( swq_select *select_info,
00377 swq_field_list *field_list,
00378 int parse_flags );
00379
00380 const char *swq_select_finish_summarize( swq_select *select_info );
00381 const char *swq_select_summarize( swq_select *select_info,
00382 int dest_column,
00383 const char *value );
00384
00385 int swq_is_reserved_keyword(const char* pszStr);
00386
00387 char* OGRHStoreGetValue(const char* pszHStore, const char* pszSearchedKey);
00388
00389 #endif