OGR
swq.h
1 /******************************************************************************
2  *
3  * Component: OGDI Driver Support Library
4  * Purpose: Generic SQL WHERE Expression Evaluator Declarations.
5  * Author: Frank Warmerdam <warmerdam@pobox.com>
6  *
7  ******************************************************************************
8  * Copyright (C) 2001 Information Interoperability Institute (3i)
9  * Copyright (c) 2010-2013, Even Rouault <even dot rouault at mines-paris dot org>
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies, that
13  * both the copyright notice and this permission notice appear in
14  * supporting documentation, and that the name of 3i not be used
15  * in advertising or publicity pertaining to distribution of the software
16  * without specific, written prior permission. 3i makes no
17  * representations about the suitability of this software for any purpose.
18  * It is provided "as is" without express or implied warranty.
19  ****************************************************************************/
20 
21 #ifndef _SWQ_H_INCLUDED_
22 #define _SWQ_H_INCLUDED_
23 
24 #include "cpl_conv.h"
25 #include "cpl_string.h"
26 #include "ogr_core.h"
27 
28 #if defined(_WIN32) && !defined(_WIN32_WCE)
29 # define strcasecmp stricmp
30 #elif defined(_WIN32_WCE)
31 # define strcasecmp _stricmp
32 #endif
33 
34 typedef enum {
35  SWQ_OR,
36  SWQ_AND,
37  SWQ_NOT,
38  SWQ_EQ,
39  SWQ_NE,
40  SWQ_GE,
41  SWQ_LE,
42  SWQ_LT,
43  SWQ_GT,
44  SWQ_LIKE,
45  SWQ_ISNULL,
46  SWQ_IN,
47  SWQ_BETWEEN,
48  SWQ_ADD,
49  SWQ_SUBTRACT,
50  SWQ_MULTIPLY,
51  SWQ_DIVIDE,
52  SWQ_MODULUS,
53  SWQ_CONCAT,
54  SWQ_SUBSTR,
55  SWQ_AVG,
56  SWQ_MIN,
57  SWQ_MAX,
58  SWQ_COUNT,
59  SWQ_SUM,
60  SWQ_CAST,
61  SWQ_FUNC_DEFINED,
62  SWQ_UNKNOWN
63 } swq_op;
64 
65 typedef enum {
66  SWQ_INTEGER,
67  SWQ_FLOAT,
68  SWQ_STRING,
69  SWQ_BOOLEAN, // integer
70  SWQ_DATE, // string
71  SWQ_TIME, // string
72  SWQ_TIMESTAMP,// string
73  SWQ_GEOMETRY,
74  SWQ_NULL,
75  SWQ_OTHER,
76  SWQ_ERROR
77 } swq_field_type;
78 
79 typedef enum {
80  SNT_CONSTANT,
81  SNT_COLUMN,
82  SNT_OPERATION
83 } swq_node_type;
84 
85 
86 class swq_field_list;
87 class swq_expr_node;
88 class swq_select;
89 class OGRGeometry;
90 
91 typedef swq_expr_node *(*swq_field_fetcher)( swq_expr_node *op,
92  void *record_handle );
93 typedef swq_expr_node *(*swq_op_evaluator)(swq_expr_node *op,
94  swq_expr_node **sub_field_values );
95 typedef swq_field_type (*swq_op_checker)( swq_expr_node *op );
96 
98  static void Quote( CPLString &, char chQuote = '\'' );
99 public:
100  swq_expr_node();
101 
102  swq_expr_node( const char * );
103  swq_expr_node( int );
104  swq_expr_node( double );
106  swq_expr_node( swq_op );
107 
108  ~swq_expr_node();
109 
110  void Initialize();
111  char *Unparse( swq_field_list *, char chColumnQuote );
112  void Dump( FILE *fp, int depth );
113  swq_field_type Check( swq_field_list *, int bAllowFieldsInSecondaryTables );
114  swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
115  void *record );
116 
117  swq_node_type eNodeType;
118  swq_field_type field_type;
119 
120  /* only for SNT_OPERATION */
121  void PushSubExpression( swq_expr_node * );
122  void ReverseSubExpressions();
123  int nOperation;
124  int nSubExprCount;
125  swq_expr_node **papoSubExpr;
126 
127  /* only for SNT_COLUMN */
128  int field_index;
129  int table_index;
130 
131  /* only for SNT_CONSTANT */
132  int is_null;
133  char *string_value;
134  int int_value;
135  double float_value;
136  OGRGeometry *geometry_value;
137 };
138 
139 typedef struct {
140  const char* pszName;
141  swq_op eOperation;
142  swq_op_evaluator pfnEvaluator;
143  swq_op_checker pfnChecker;
144 } swq_operation;
145 
147 public:
148  static const swq_operation *GetOperator( const char * );
149  static const swq_operation *GetOperator( swq_op eOperation );
150 };
151 
152 typedef struct {
153  char *data_source;
154  char *table_name;
155  char *table_alias;
156 } swq_table_def;
157 
159 public:
160  int count;
161  char **names;
162  swq_field_type *types;
163  int *table_ids;
164  int *ids;
165 
166  int table_count;
167  swq_table_def *table_defs;
168 };
169 
171 public:
172  swq_parse_context() : nStartToken(0), poRoot(NULL), poCurSelect(NULL) {}
173 
174  int nStartToken;
175  const char *pszInput;
176  const char *pszNext;
177  const char *pszLastValid;
178 
179  swq_expr_node *poRoot;
180 
181  swq_select *poCurSelect;
182 };
183 
184 /* Compile an SQL WHERE clause into an internal form. The field_list is
185 ** the list of fields in the target 'table', used to render where into
186 ** field numbers instead of names.
187 */
188 int swqparse( swq_parse_context *context );
189 int swqlex( swq_expr_node **ppNode, swq_parse_context *context );
190 void swqerror( swq_parse_context *context, const char *msg );
191 
192 int swq_identify_field( const char *token, swq_field_list *field_list,
193  swq_field_type *this_type, int *table_id );
194 
195 CPLErr swq_expr_compile( const char *where_clause,
196  int field_count,
197  char **field_list,
198  swq_field_type *field_types,
199  swq_expr_node **expr_root );
200 
201 CPLErr swq_expr_compile2( const char *where_clause,
202  swq_field_list *field_list,
203  swq_expr_node **expr_root );
204 
205 /*
206 ** Evaluation related.
207 */
208 int swq_test_like( const char *input, const char *pattern );
209 
210 swq_expr_node *SWQGeneralEvaluator( swq_expr_node *, swq_expr_node **);
211 swq_field_type SWQGeneralChecker( swq_expr_node *node );
212 swq_expr_node *SWQCastEvaluator( swq_expr_node *, swq_expr_node **);
213 swq_field_type SWQCastChecker( swq_expr_node *node );
214 const char* SWQFieldTypeToString( swq_field_type field_type );
215 
216 /****************************************************************************/
217 
218 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
219 
220 #define SWQM_SUMMARY_RECORD 1
221 #define SWQM_RECORDSET 2
222 #define SWQM_DISTINCT_LIST 3
223 
224 typedef enum {
225  SWQCF_NONE = 0,
226  SWQCF_AVG = SWQ_AVG,
227  SWQCF_MIN = SWQ_MIN,
228  SWQCF_MAX = SWQ_MAX,
229  SWQCF_COUNT = SWQ_COUNT,
230  SWQCF_SUM = SWQ_SUM,
231  SWQCF_CUSTOM
232 } swq_col_func;
233 
234 typedef struct {
235  swq_col_func col_func;
236  char *field_name;
237  char *field_alias;
238  int table_index;
239  int field_index;
240  swq_field_type field_type;
241  swq_field_type target_type;
242  int field_length;
243  int field_precision;
244  int distinct_flag;
245  OGRwkbGeometryType eGeomType;
246  int nSRID;
247  swq_expr_node *expr;
248 } swq_col_def;
249 
250 typedef struct {
251  int count;
252 
253  char **distinct_list; /* items of the list can be NULL */
254  double sum;
255  double min;
256  double max;
257  char szMin[32];
258  char szMax[32];
259 } swq_summary;
260 
261 typedef struct {
262  char *field_name;
263  int table_index;
264  int field_index;
265  int ascending_flag;
266 } swq_order_def;
267 
268 typedef struct {
269  int secondary_table;
270 
271  char *primary_field_name;
272  int primary_field;
273 
274  swq_op op;
275 
276  char *secondary_field_name;
277  int secondary_field;
278 } swq_join_def;
279 
281 {
282 public:
283  swq_select();
284  ~swq_select();
285 
286  int query_mode;
287 
288  char *raw_select;
289 
290  int PushField( swq_expr_node *poExpr, const char *pszAlias=NULL,
291  int distinct_flag = FALSE );
292  int result_columns;
293  swq_col_def *column_defs;
294  swq_summary *column_summary;
295 
296  int PushTableDef( const char *pszDataSource,
297  const char *pszTableName,
298  const char *pszAlias );
299  int table_count;
300  swq_table_def *table_defs;
301 
302  void PushJoin( int iSecondaryTable,
303  const char *pszPrimaryField,
304  const char *pszSecondaryField );
305  int join_count;
306  swq_join_def *join_defs;
307 
308  swq_expr_node *where_expr;
309 
310  void PushOrderBy( const char *pszFieldName, int bAscending );
311  int order_specs;
312  swq_order_def *order_defs;
313 
314  swq_select *poOtherSelect;
315  void PushUnionAll( swq_select* poOtherSelectIn );
316 
317  CPLErr preparse( const char *select_statement );
318  void postpreparse();
319  CPLErr expand_wildcard( swq_field_list *field_list );
320  CPLErr parse( swq_field_list *field_list, int parse_flags );
321 
322  void Dump( FILE * );
323 };
324 
325 CPLErr swq_select_parse( swq_select *select_info,
326  swq_field_list *field_list,
327  int parse_flags );
328 
329 const char *swq_select_finish_summarize( swq_select *select_info );
330 const char *swq_select_summarize( swq_select *select_info,
331  int dest_column,
332  const char *value );
333 
334 int swq_is_reserved_keyword(const char* pszStr);
335 
336 #endif /* def _SWQ_H_INCLUDED_ */
Definition: swq.h:280
Definition: swq.h:139
Definition: swq.h:158
Convenient string class based on std::string.
Definition: cpl_string.h:226
Definition: swq.h:146
Definition: swq.h:234
OGRwkbGeometryType
Definition: ogr_core.h:308
Definition: ogr_geometry.h:79
Definition: swq.h:268
Definition: swq.h:152
Definition: swq.h:250
Definition: swq.h:261
Definition: swq.h:170
Definition: swq.h:97

Generated for GDAL by doxygen 1.8.11.