21 #ifndef SWQ_H_INCLUDED_
22 #define SWQ_H_INCLUDED_
33 #if defined(_WIN32) && !defined(strcasecmp)
34 # define strcasecmp stricmp
38 #define SZ_OGR_NULL "__OGR_NULL__"
87 #define SWQ_IS_INTEGER(x) ((x) == SWQ_INTEGER || (x) == SWQ_INTEGER64)
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 );
107 class swq_custom_func_registrar;
109 class swq_expr_node {
112 swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
113 void *record,
int nRecLevel );
117 explicit swq_expr_node(
const char * );
118 explicit swq_expr_node(
int );
119 explicit swq_expr_node(
GIntBig );
120 explicit swq_expr_node(
double );
122 explicit swq_expr_node( swq_op );
127 void MarkAsTimestamp();
128 CPLString UnparseOperationFromUnparsedSubExpr(
char** apszSubExpr);
129 char *Unparse( swq_field_list *,
char chColumnQuote );
130 void Dump( FILE *fp,
int depth );
131 swq_field_type Check( swq_field_list *,
int bAllowFieldsInSecondaryTables,
132 int bAllowMismatchTypeOnFieldComparison,
133 swq_custom_func_registrar* poCustomFuncRegistrar );
134 swq_expr_node* Evaluate( swq_field_fetcher pfnFetcher,
136 swq_expr_node* Clone();
138 void ReplaceBetweenByGEAndLERecurse();
140 swq_node_type eNodeType;
141 swq_field_type field_type;
144 void PushSubExpression( swq_expr_node * );
145 void ReverseSubExpressions();
148 swq_expr_node **papoSubExpr;
172 swq_op_evaluator pfnEvaluator;
173 swq_op_checker pfnChecker;
176 class swq_op_registrar {
178 static const swq_operation *GetOperator(
const char * );
179 static const swq_operation *GetOperator( swq_op eOperation );
182 class swq_custom_func_registrar
185 virtual ~swq_custom_func_registrar() {}
186 virtual const swq_operation *GetOperator(
const char * ) = 0;
195 class swq_field_list {
199 swq_field_type *types;
204 swq_table_def *table_defs;
207 class swq_parse_context {
209 swq_parse_context() : nStartToken(0), pszInput(nullptr), pszNext(nullptr),
210 pszLastValid(nullptr), bAcceptCustomFuncs(FALSE),
211 poRoot(nullptr), poCurSelect(nullptr) {}
214 const char *pszInput;
216 const char *pszLastValid;
217 int bAcceptCustomFuncs;
219 swq_expr_node *poRoot;
221 swq_select *poCurSelect;
228 int swqparse( swq_parse_context *context );
229 int swqlex( swq_expr_node **ppNode, swq_parse_context *context );
230 void swqerror( swq_parse_context *context,
const char *msg );
232 int swq_identify_field(
const char* table_name,
233 const char *token, swq_field_list *field_list,
234 swq_field_type *this_type,
int *table_id );
236 CPLErr swq_expr_compile(
const char *where_clause,
239 swq_field_type *field_types,
241 swq_custom_func_registrar* poCustomFuncRegistrar,
242 swq_expr_node **expr_root );
244 CPLErr swq_expr_compile2(
const char *where_clause,
245 swq_field_list *field_list,
247 swq_custom_func_registrar* poCustomFuncRegistrar,
248 swq_expr_node **expr_root );
253 int swq_test_like(
const char *input,
const char *pattern );
255 swq_expr_node *SWQGeneralEvaluator( swq_expr_node *, swq_expr_node **);
256 swq_field_type SWQGeneralChecker( swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison );
257 swq_expr_node *SWQCastEvaluator( swq_expr_node *, swq_expr_node **);
258 swq_field_type SWQCastChecker( swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison );
259 const char* SWQFieldTypeToString( swq_field_type field_type );
263 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
265 #define SWQM_SUMMARY_RECORD 1
266 #define SWQM_RECORDSET 2
267 #define SWQM_DISTINCT_LIST 3
274 SWQCF_COUNT = SWQ_COUNT,
280 swq_col_func col_func;
286 swq_field_type field_type;
287 swq_field_type target_type;
302 swq_field_type eType;
304 Comparator() : bSortAsc(true), eType(SWQ_STRING) {}
311 std::vector<CPLString> oVectorDistinctValues;
312 std::set<CPLString, Comparator> oSetDistinctValues;
319 swq_summary() : count(0), sum(0.0), min(0.0), max(0.0) {}
332 swq_expr_node *poExpr;
335 class swq_select_parse_options
338 swq_custom_func_registrar* poCustomFuncRegistrar;
339 int bAllowFieldsInSecondaryTablesInWhere;
340 int bAddSecondaryTablesGeometryFields;
341 int bAlwaysPrefixWithTableName;
342 int bAllowDistinctOnGeometryField;
343 int bAllowDistinctOnMultipleFields;
345 swq_select_parse_options(): poCustomFuncRegistrar(nullptr),
346 bAllowFieldsInSecondaryTablesInWhere(FALSE),
347 bAddSecondaryTablesGeometryFields(FALSE),
348 bAlwaysPrefixWithTableName(FALSE),
349 bAllowDistinctOnGeometryField(FALSE),
350 bAllowDistinctOnMultipleFields(FALSE) {}
365 int PushField( swq_expr_node *poExpr,
const char *pszAlias=
nullptr,
366 int distinct_flag = FALSE );
368 swq_col_def *column_defs;
369 std::vector<swq_summary> column_summary;
371 int PushTableDef(
const char *pszDataSource,
372 const char *pszTableName,
373 const char *pszAlias );
375 swq_table_def *table_defs;
377 void PushJoin(
int iSecondaryTable, swq_expr_node* poExpr );
379 swq_join_def *join_defs;
381 swq_expr_node *where_expr;
383 void PushOrderBy(
const char* pszTableName,
const char *pszFieldName,
int bAscending );
385 swq_order_def *order_defs;
387 void SetLimit(
GIntBig nLimit );
390 void SetOffset(
GIntBig nOffset );
393 swq_select *poOtherSelect;
394 void PushUnionAll( swq_select* poOtherSelectIn );
396 CPLErr preparse(
const char *select_statement,
397 int bAcceptCustomFuncs = FALSE );
398 CPLErr expand_wildcard( swq_field_list *field_list,
399 int bAlwaysPrefixWithTableName );
400 CPLErr parse( swq_field_list *field_list,
401 swq_select_parse_options* poParseOptions );
407 CPLErr swq_select_parse( swq_select *select_info,
408 swq_field_list *field_list,
411 const char *swq_select_summarize( swq_select *select_info,
415 int swq_is_reserved_keyword(
const char* pszStr);
417 char* OGRHStoreGetValue(
const char* pszHStore,
const char* pszSearchedKey);