GDAL
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 #ifndef DOXYGEN_SKIP
25 
26 #include "cpl_conv.h"
27 #include "cpl_string.h"
28 #include "ogr_core.h"
29 
30 #include <vector>
31 #include <set>
32 
33 #if defined(_WIN32) && !defined(strcasecmp)
34 # define strcasecmp stricmp
35 #endif
36 
37 // Used for swq_summary.oSetDistinctValues and oVectorDistinctValues
38 #define SZ_OGR_NULL "__OGR_NULL__"
39 
40 typedef enum {
41  SWQ_OR,
42  SWQ_AND,
43  SWQ_NOT,
44  SWQ_EQ,
45  SWQ_NE,
46  SWQ_GE,
47  SWQ_LE,
48  SWQ_LT,
49  SWQ_GT,
50  SWQ_LIKE,
51  SWQ_ISNULL,
52  SWQ_IN,
53  SWQ_BETWEEN,
54  SWQ_ADD,
55  SWQ_SUBTRACT,
56  SWQ_MULTIPLY,
57  SWQ_DIVIDE,
58  SWQ_MODULUS,
59  SWQ_CONCAT,
60  SWQ_SUBSTR,
61  SWQ_HSTORE_GET_VALUE,
62  SWQ_AVG,
63  SWQ_MIN,
64  SWQ_MAX,
65  SWQ_COUNT,
66  SWQ_SUM,
67  SWQ_CAST,
68  SWQ_CUSTOM_FUNC, /* only if parsing done in bAcceptCustomFuncs mode */
69  SWQ_ARGUMENT_LIST /* temporary value only set during parsing and replaced by something else at the end */
70 } swq_op;
71 
72 typedef enum {
73  SWQ_INTEGER,
74  SWQ_INTEGER64,
75  SWQ_FLOAT,
76  SWQ_STRING,
77  SWQ_BOOLEAN, // integer
78  SWQ_DATE, // string
79  SWQ_TIME, // string
80  SWQ_TIMESTAMP,// string
81  SWQ_GEOMETRY,
82  SWQ_NULL,
83  SWQ_OTHER,
84  SWQ_ERROR
85 } swq_field_type;
86 
87 #define SWQ_IS_INTEGER(x) ((x) == SWQ_INTEGER || (x) == SWQ_INTEGER64)
88 
89 typedef enum {
90  SNT_CONSTANT,
91  SNT_COLUMN,
92  SNT_OPERATION
93 } swq_node_type;
94 
95 class swq_field_list;
96 class swq_expr_node;
97 class swq_select;
98 class OGRGeometry;
99 
100 typedef swq_expr_node *(*swq_field_fetcher)( swq_expr_node *op,
101  void *record_handle );
102 typedef swq_expr_node *(*swq_op_evaluator)(swq_expr_node *op,
103  swq_expr_node **sub_field_values );
104 typedef swq_field_type (*swq_op_checker)( swq_expr_node *op,
105  int bAllowMismatchTypeOnFieldComparison );
106 
107 class swq_custom_func_registrar;
108 
109 class swq_expr_node {
110 
111  CPL_DISALLOW_COPY_ASSIGN(swq_expr_node)
112 
113 public:
114  swq_expr_node();
115 
116  explicit swq_expr_node( const char * );
117  explicit swq_expr_node( int );
118  explicit swq_expr_node( GIntBig );
119  explicit swq_expr_node( double );
120  explicit swq_expr_node( OGRGeometry* );
121  explicit swq_expr_node( swq_op );
122 
123  ~swq_expr_node();
124 
125  void MarkAsTimestamp();
126  CPLString UnparseOperationFromUnparsedSubExpr(char** apszSubExpr);
127  char *Unparse( swq_field_list *, char chColumnQuote );
128  void Dump( FILE *fp, int depth );
129  swq_field_type Check( swq_field_list *, int bAllowFieldsInSecondaryTables,
130  int bAllowMismatchTypeOnFieldComparison,
131  swq_custom_func_registrar* poCustomFuncRegistrar );
132  swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
133  void *record );
134  swq_expr_node* Clone();
135 
136  void ReplaceBetweenByGEAndLERecurse();
137 
138  swq_node_type eNodeType = SNT_CONSTANT;
139  swq_field_type field_type = SWQ_INTEGER;
140 
141  /* only for SNT_OPERATION */
142  void PushSubExpression( swq_expr_node * );
143  void ReverseSubExpressions();
144  int nOperation = 0;
145  int nSubExprCount = 0;
146  swq_expr_node **papoSubExpr = nullptr;
147 
148  /* only for SNT_COLUMN */
149  int field_index = 0;
150  int table_index = 0;
151  char *table_name = nullptr;
152 
153  /* only for SNT_CONSTANT */
154  int is_null = false;
155  GIntBig int_value = 0;
156  double float_value = 0.0;
157  OGRGeometry *geometry_value = nullptr;
158 
159  /* shared by SNT_COLUMN, SNT_CONSTANT and also possibly SNT_OPERATION when */
160  /* nOperation == SWQ_CUSTOM_FUNC */
161  char *string_value = nullptr; /* column name when SNT_COLUMN */
162 
163  static CPLString QuoteIfNecessary( const CPLString &, char chQuote = '\'' );
164  static CPLString Quote( const CPLString &, char chQuote = '\'' );
165 };
166 
167 typedef struct {
168  const char* pszName;
169  swq_op eOperation;
170  swq_op_evaluator pfnEvaluator;
171  swq_op_checker pfnChecker;
172 } swq_operation;
173 
174 class swq_op_registrar {
175 public:
176  static const swq_operation *GetOperator( const char * );
177  static const swq_operation *GetOperator( swq_op eOperation );
178 };
179 
180 class swq_custom_func_registrar
181 {
182  public:
183  virtual ~swq_custom_func_registrar() {}
184  virtual const swq_operation *GetOperator( const char * ) = 0;
185 };
186 
187 typedef struct {
188  char *data_source;
189  char *table_name;
190  char *table_alias;
191 } swq_table_def;
192 
193 class swq_field_list {
194 public:
195  int count;
196  char **names;
197  swq_field_type *types;
198  int *table_ids;
199  int *ids;
200 
201  int table_count;
202  swq_table_def *table_defs;
203 };
204 
205 class swq_parse_context {
206 public:
207  swq_parse_context() : nStartToken(0), pszInput(nullptr), pszNext(nullptr),
208  pszLastValid(nullptr), bAcceptCustomFuncs(FALSE),
209  poRoot(nullptr), poCurSelect(nullptr) {}
210 
211  int nStartToken;
212  const char *pszInput;
213  const char *pszNext;
214  const char *pszLastValid;
215  int bAcceptCustomFuncs;
216 
217  swq_expr_node *poRoot;
218 
219  swq_select *poCurSelect;
220 };
221 
222 /* Compile an SQL WHERE clause into an internal form. The field_list is
223 ** the list of fields in the target 'table', used to render where into
224 ** field numbers instead of names.
225 */
226 int swqparse( swq_parse_context *context );
227 int swqlex( swq_expr_node **ppNode, swq_parse_context *context );
228 void swqerror( swq_parse_context *context, const char *msg );
229 
230 int swq_identify_field( const char* table_name,
231  const char *token, swq_field_list *field_list,
232  swq_field_type *this_type, int *table_id );
233 
234 CPLErr swq_expr_compile( const char *where_clause,
235  int field_count,
236  char **field_list,
237  swq_field_type *field_types,
238  int bCheck,
239  swq_custom_func_registrar* poCustomFuncRegistrar,
240  swq_expr_node **expr_root );
241 
242 CPLErr swq_expr_compile2( const char *where_clause,
243  swq_field_list *field_list,
244  int bCheck,
245  swq_custom_func_registrar* poCustomFuncRegistrar,
246  swq_expr_node **expr_root );
247 
248 /*
249 ** Evaluation related.
250 */
251 int swq_test_like( const char *input, const char *pattern );
252 
253 swq_expr_node *SWQGeneralEvaluator( swq_expr_node *, swq_expr_node **);
254 swq_field_type SWQGeneralChecker( swq_expr_node *node, int bAllowMismatchTypeOnFieldComparison );
255 swq_expr_node *SWQCastEvaluator( swq_expr_node *, swq_expr_node **);
256 swq_field_type SWQCastChecker( swq_expr_node *node, int bAllowMismatchTypeOnFieldComparison );
257 const char* SWQFieldTypeToString( swq_field_type field_type );
258 
259 /****************************************************************************/
260 
261 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
262 
263 #define SWQM_SUMMARY_RECORD 1
264 #define SWQM_RECORDSET 2
265 #define SWQM_DISTINCT_LIST 3
266 
267 typedef enum {
268  SWQCF_NONE = 0,
269  SWQCF_AVG = SWQ_AVG,
270  SWQCF_MIN = SWQ_MIN,
271  SWQCF_MAX = SWQ_MAX,
272  SWQCF_COUNT = SWQ_COUNT,
273  SWQCF_SUM = SWQ_SUM,
274  SWQCF_CUSTOM
275 } swq_col_func;
276 
277 typedef struct {
278  swq_col_func col_func;
279  char *table_name;
280  char *field_name;
281  char *field_alias;
282  int table_index;
283  int field_index;
284  swq_field_type field_type;
285  swq_field_type target_type;
286  OGRFieldSubType target_subtype;
287  int field_length;
288  int field_precision;
289  int distinct_flag;
290  OGRwkbGeometryType eGeomType;
291  int nSRID;
292  swq_expr_node *expr;
293 } swq_col_def;
294 
295 class swq_summary {
296 public:
297  struct Comparator
298  {
299  bool bSortAsc;
300  swq_field_type eType;
301 
302  Comparator() : bSortAsc(true), eType(SWQ_STRING) {}
303 
304  bool operator() (const CPLString&, const CPLString &) const;
305  };
306 
307  GIntBig count = 0;
308 
309  std::vector<CPLString> oVectorDistinctValues{};
310  std::set<CPLString, Comparator> oSetDistinctValues{};
311  double sum = 0.0;
312  double min = 0.0;
313  double max = 0.0;
314  CPLString osMin{};
315  CPLString osMax{};
316 };
317 
318 typedef struct {
319  char *table_name;
320  char *field_name;
321  int table_index;
322  int field_index;
323  int ascending_flag;
324 } swq_order_def;
325 
326 typedef struct {
327  int secondary_table;
328  swq_expr_node *poExpr;
329 } swq_join_def;
330 
331 class swq_select_parse_options
332 {
333 public:
334  swq_custom_func_registrar* poCustomFuncRegistrar;
335  int bAllowFieldsInSecondaryTablesInWhere;
336  int bAddSecondaryTablesGeometryFields;
337  int bAlwaysPrefixWithTableName;
338  int bAllowDistinctOnGeometryField;
339  int bAllowDistinctOnMultipleFields;
340 
341  swq_select_parse_options(): poCustomFuncRegistrar(nullptr),
342  bAllowFieldsInSecondaryTablesInWhere(FALSE),
343  bAddSecondaryTablesGeometryFields(FALSE),
344  bAlwaysPrefixWithTableName(FALSE),
345  bAllowDistinctOnGeometryField(FALSE),
346  bAllowDistinctOnMultipleFields(FALSE) {}
347 };
348 
349 class swq_select
350 {
351  void postpreparse();
352 
353  CPL_DISALLOW_COPY_ASSIGN(swq_select)
354 
355 public:
356  swq_select();
357  ~swq_select();
358 
359  int query_mode = 0;
360 
361  char *raw_select = nullptr;
362 
363  int PushField( swq_expr_node *poExpr, const char *pszAlias=nullptr,
364  int distinct_flag = FALSE );
365  int result_columns = 0;
366  swq_col_def *column_defs = nullptr;
367  std::vector<swq_summary> column_summary{};
368 
369  int PushTableDef( const char *pszDataSource,
370  const char *pszTableName,
371  const char *pszAlias );
372  int table_count = 0;
373  swq_table_def *table_defs = nullptr;
374 
375  void PushJoin( int iSecondaryTable, swq_expr_node* poExpr );
376  int join_count = 0;
377  swq_join_def *join_defs = nullptr;
378 
379  swq_expr_node *where_expr = nullptr;
380 
381  void PushOrderBy( const char* pszTableName, const char *pszFieldName, int bAscending );
382  int order_specs = 0;
383  swq_order_def *order_defs = nullptr;
384 
385  void SetLimit( GIntBig nLimit );
386  GIntBig limit = -1;
387 
388  void SetOffset( GIntBig nOffset );
389  GIntBig offset = 0;
390 
391  swq_select *poOtherSelect = nullptr;
392  void PushUnionAll( swq_select* poOtherSelectIn );
393 
394  CPLErr preparse( const char *select_statement,
395  int bAcceptCustomFuncs = FALSE );
396  CPLErr expand_wildcard( swq_field_list *field_list,
397  int bAlwaysPrefixWithTableName );
398  CPLErr parse( swq_field_list *field_list,
399  swq_select_parse_options* poParseOptions );
400 
401  char *Unparse();
402  void Dump( FILE * );
403 };
404 
405 CPLErr swq_select_parse( swq_select *select_info,
406  swq_field_list *field_list,
407  int parse_flags );
408 
409 const char *swq_select_summarize( swq_select *select_info,
410  int dest_column,
411  const char *value );
412 
413 int swq_is_reserved_keyword(const char* pszStr);
414 
415 char* OGRHStoreGetValue(const char* pszHStore, const char* pszSearchedKey);
416 
417 #endif /* #ifndef DOXYGEN_SKIP */
418 
419 #endif /* def SWQ_H_INCLUDED_ */
OGRFieldSubType
List of field subtypes.
Definition: ogr_core.h:622
Convenient string class based on std::string.
Definition: cpl_string.h:327
Various convenience functions for working with strings and string lists.
OGRwkbGeometryType
List of well known binary geometry types.
Definition: ogr_core.h:317
Abstract base class for all geometry classes.
Definition: ogr_geometry.h:286
Core portability services for cross-platform OGR code.
Various convenience functions for CPL.
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:246
CPLErr
Error category.
Definition: cpl_error.h:52
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:987

Generated for GDAL by doxygen 1.8.8.