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(_WIN32_WCE)
00029 # define strcasecmp stricmp
00030 #elif defined(_WIN32_WCE)
00031 # define strcasecmp _stricmp
00032 #endif
00033
00034 typedef enum {
00035 SWQ_OR,
00036 SWQ_AND,
00037 SWQ_NOT,
00038 SWQ_EQ,
00039 SWQ_NE,
00040 SWQ_GE,
00041 SWQ_LE,
00042 SWQ_LT,
00043 SWQ_GT,
00044 SWQ_LIKE,
00045 SWQ_ISNULL,
00046 SWQ_IN,
00047 SWQ_BETWEEN,
00048 SWQ_ADD,
00049 SWQ_SUBTRACT,
00050 SWQ_MULTIPLY,
00051 SWQ_DIVIDE,
00052 SWQ_MODULUS,
00053 SWQ_CONCAT,
00054 SWQ_SUBSTR,
00055 SWQ_AVG,
00056 SWQ_MIN,
00057 SWQ_MAX,
00058 SWQ_COUNT,
00059 SWQ_SUM,
00060 SWQ_CAST,
00061 SWQ_FUNC_DEFINED,
00062 SWQ_UNKNOWN
00063 } swq_op;
00064
00065 typedef enum {
00066 SWQ_INTEGER,
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 typedef enum {
00080 SNT_CONSTANT,
00081 SNT_COLUMN,
00082 SNT_OPERATION
00083 } swq_node_type;
00084
00085
00086 class swq_field_list;
00087 class swq_expr_node;
00088 class swq_select;
00089 class OGRGeometry;
00090
00091 typedef swq_expr_node *(*swq_field_fetcher)( swq_expr_node *op,
00092 void *record_handle );
00093 typedef swq_expr_node *(*swq_op_evaluator)(swq_expr_node *op,
00094 swq_expr_node **sub_field_values );
00095 typedef swq_field_type (*swq_op_checker)( swq_expr_node *op );
00096
00097 class swq_expr_node {
00098 static void Quote( CPLString &, char chQuote = '\'' );
00099 public:
00100 swq_expr_node();
00101
00102 swq_expr_node( const char * );
00103 swq_expr_node( int );
00104 swq_expr_node( double );
00105 swq_expr_node( OGRGeometry* );
00106 swq_expr_node( swq_op );
00107
00108 ~swq_expr_node();
00109
00110 void Initialize();
00111 char *Unparse( swq_field_list *, char chColumnQuote );
00112 void Dump( FILE *fp, int depth );
00113 swq_field_type Check( swq_field_list *, int bAllowFieldsInSecondaryTables );
00114 swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
00115 void *record );
00116
00117 swq_node_type eNodeType;
00118 swq_field_type field_type;
00119
00120
00121 void PushSubExpression( swq_expr_node * );
00122 void ReverseSubExpressions();
00123 int nOperation;
00124 int nSubExprCount;
00125 swq_expr_node **papoSubExpr;
00126
00127
00128 int field_index;
00129 int table_index;
00130
00131
00132 int is_null;
00133 char *string_value;
00134 int int_value;
00135 double float_value;
00136 OGRGeometry *geometry_value;
00137 };
00138
00139 typedef struct {
00140 const char* pszName;
00141 swq_op eOperation;
00142 swq_op_evaluator pfnEvaluator;
00143 swq_op_checker pfnChecker;
00144 } swq_operation;
00145
00146 class swq_op_registrar {
00147 public:
00148 static const swq_operation *GetOperator( const char * );
00149 static const swq_operation *GetOperator( swq_op eOperation );
00150 };
00151
00152 typedef struct {
00153 char *data_source;
00154 char *table_name;
00155 char *table_alias;
00156 } swq_table_def;
00157
00158 class swq_field_list {
00159 public:
00160 int count;
00161 char **names;
00162 swq_field_type *types;
00163 int *table_ids;
00164 int *ids;
00165
00166 int table_count;
00167 swq_table_def *table_defs;
00168 };
00169
00170 class swq_parse_context {
00171 public:
00172 swq_parse_context() : nStartToken(0), poRoot(NULL), poCurSelect(NULL) {}
00173
00174 int nStartToken;
00175 const char *pszInput;
00176 const char *pszNext;
00177 const char *pszLastValid;
00178
00179 swq_expr_node *poRoot;
00180
00181 swq_select *poCurSelect;
00182 };
00183
00184
00185
00186
00187
00188 int swqparse( swq_parse_context *context );
00189 int swqlex( swq_expr_node **ppNode, swq_parse_context *context );
00190 void swqerror( swq_parse_context *context, const char *msg );
00191
00192 int swq_identify_field( const char *token, swq_field_list *field_list,
00193 swq_field_type *this_type, int *table_id );
00194
00195 CPLErr swq_expr_compile( const char *where_clause,
00196 int field_count,
00197 char **field_list,
00198 swq_field_type *field_types,
00199 swq_expr_node **expr_root );
00200
00201 CPLErr swq_expr_compile2( const char *where_clause,
00202 swq_field_list *field_list,
00203 swq_expr_node **expr_root );
00204
00205
00206
00207
00208 int swq_test_like( const char *input, const char *pattern );
00209
00210 swq_expr_node *SWQGeneralEvaluator( swq_expr_node *, swq_expr_node **);
00211 swq_field_type SWQGeneralChecker( swq_expr_node *node );
00212 swq_expr_node *SWQCastEvaluator( swq_expr_node *, swq_expr_node **);
00213 swq_field_type SWQCastChecker( swq_expr_node *node );
00214 const char* SWQFieldTypeToString( swq_field_type field_type );
00215
00216
00217
00218 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
00219
00220 #define SWQM_SUMMARY_RECORD 1
00221 #define SWQM_RECORDSET 2
00222 #define SWQM_DISTINCT_LIST 3
00223
00224 typedef enum {
00225 SWQCF_NONE = 0,
00226 SWQCF_AVG = SWQ_AVG,
00227 SWQCF_MIN = SWQ_MIN,
00228 SWQCF_MAX = SWQ_MAX,
00229 SWQCF_COUNT = SWQ_COUNT,
00230 SWQCF_SUM = SWQ_SUM,
00231 SWQCF_CUSTOM
00232 } swq_col_func;
00233
00234 typedef struct {
00235 swq_col_func col_func;
00236 char *field_name;
00237 char *field_alias;
00238 int table_index;
00239 int field_index;
00240 swq_field_type field_type;
00241 swq_field_type target_type;
00242 int field_length;
00243 int field_precision;
00244 int distinct_flag;
00245 OGRwkbGeometryType eGeomType;
00246 int nSRID;
00247 swq_expr_node *expr;
00248 } swq_col_def;
00249
00250 typedef struct {
00251 int count;
00252
00253 char **distinct_list;
00254 double sum;
00255 double min;
00256 double max;
00257 char szMin[32];
00258 char szMax[32];
00259 } swq_summary;
00260
00261 typedef struct {
00262 char *field_name;
00263 int table_index;
00264 int field_index;
00265 int ascending_flag;
00266 } swq_order_def;
00267
00268 typedef struct {
00269 int secondary_table;
00270
00271 char *primary_field_name;
00272 int primary_field;
00273
00274 swq_op op;
00275
00276 char *secondary_field_name;
00277 int secondary_field;
00278 } swq_join_def;
00279
00280 class swq_select
00281 {
00282 public:
00283 swq_select();
00284 ~swq_select();
00285
00286 int query_mode;
00287
00288 char *raw_select;
00289
00290 int PushField( swq_expr_node *poExpr, const char *pszAlias=NULL,
00291 int distinct_flag = FALSE );
00292 int result_columns;
00293 swq_col_def *column_defs;
00294 swq_summary *column_summary;
00295
00296 int PushTableDef( const char *pszDataSource,
00297 const char *pszTableName,
00298 const char *pszAlias );
00299 int table_count;
00300 swq_table_def *table_defs;
00301
00302 void PushJoin( int iSecondaryTable,
00303 const char *pszPrimaryField,
00304 const char *pszSecondaryField );
00305 int join_count;
00306 swq_join_def *join_defs;
00307
00308 swq_expr_node *where_expr;
00309
00310 void PushOrderBy( const char *pszFieldName, int bAscending );
00311 int order_specs;
00312 swq_order_def *order_defs;
00313
00314 swq_select *poOtherSelect;
00315 void PushUnionAll( swq_select* poOtherSelectIn );
00316
00317 CPLErr preparse( const char *select_statement );
00318 void postpreparse();
00319 CPLErr expand_wildcard( swq_field_list *field_list );
00320 CPLErr parse( swq_field_list *field_list, int parse_flags );
00321
00322 void Dump( FILE * );
00323 };
00324
00325 CPLErr swq_select_parse( swq_select *select_info,
00326 swq_field_list *field_list,
00327 int parse_flags );
00328
00329 const char *swq_select_finish_summarize( swq_select *select_info );
00330 const char *swq_select_summarize( swq_select *select_info,
00331 int dest_column,
00332 const char *value );
00333
00334 int swq_is_reserved_keyword(const char* pszStr);
00335
00336 #endif