21#ifndef SWQ_H_INCLUDED_
22#define SWQ_H_INCLUDED_
35#if defined(_WIN32) && !defined(strcasecmp)
36#define strcasecmp stricmp
40#define SZ_OGR_NULL "__OGR_NULL__"
68 SWQ_AGGREGATE_BEGIN = SWQ_AVG,
75 SWQ_AGGREGATE_END = SWQ_STDDEV_SAMP,
99#define SWQ_IS_INTEGER(x) ((x) == SWQ_INTEGER || (x) == SWQ_INTEGER64)
113struct CPL_UNSTABLE_API swq_evaluation_context
115 bool bUTF8Strings =
false;
118typedef swq_expr_node *(*swq_field_fetcher)(swq_expr_node *op,
119 void *record_handle);
120typedef swq_expr_node *(*swq_op_evaluator)(
121 swq_expr_node *op, swq_expr_node **sub_field_values,
122 const swq_evaluation_context &sContext);
123typedef swq_field_type (*swq_op_checker)(
124 swq_expr_node *op,
int bAllowMismatchTypeOnFieldComparison);
126class swq_custom_func_registrar;
128class CPL_UNSTABLE_API swq_expr_node
130 swq_expr_node *Evaluate(swq_field_fetcher pfnFetcher,
void *record,
131 const swq_evaluation_context &sContext,
137 swq_expr_node(
const swq_expr_node &);
138 swq_expr_node(swq_expr_node &&);
140 swq_expr_node &operator=(
const swq_expr_node &);
141 swq_expr_node &operator=(swq_expr_node &&);
143 bool operator==(
const swq_expr_node &)
const;
145 explicit swq_expr_node(
const char *);
146 explicit swq_expr_node(
int);
147 explicit swq_expr_node(
GIntBig);
148 explicit swq_expr_node(
double);
149 explicit swq_expr_node(OGRGeometry *);
150 explicit swq_expr_node(swq_op);
154 void MarkAsTimestamp();
155 CPLString UnparseOperationFromUnparsedSubExpr(
char **apszSubExpr);
156 char *Unparse(swq_field_list *,
char chColumnQuote);
157 void Dump(FILE *fp,
int depth);
158 swq_field_type Check(swq_field_list *,
int bAllowFieldsInSecondaryTables,
159 int bAllowMismatchTypeOnFieldComparison,
160 swq_custom_func_registrar *poCustomFuncRegistrar);
161 swq_expr_node *Evaluate(swq_field_fetcher pfnFetcher,
void *record,
162 const swq_evaluation_context &sContext);
163 swq_expr_node *Clone();
165 void ReplaceBetweenByGEAndLERecurse();
166 void ReplaceInByOrRecurse();
167 void PushNotOperationDownToStack();
169 void RebalanceAndOr();
171 bool HasReachedMaxDepth()
const;
173 swq_node_type eNodeType = SNT_CONSTANT;
174 swq_field_type field_type = SWQ_INTEGER;
177 void PushSubExpression(swq_expr_node *);
178 void ReverseSubExpressions();
179 swq_op nOperation = SWQ_OR;
180 int nSubExprCount = 0;
181 swq_expr_node **papoSubExpr =
nullptr;
186 char *table_name =
nullptr;
190 int64_t int_value = 0;
191 double float_value = 0.0;
192 OGRGeometry *geometry_value =
nullptr;
197 char *string_value =
nullptr;
201 bool bHidden =
false;
206 static CPLString QuoteIfNecessary(
const CPLString &,
char chQuote =
'\'');
207 static CPLString Quote(
const CPLString &,
char chQuote =
'\'');
214 swq_op_evaluator pfnEvaluator;
215 swq_op_checker pfnChecker;
218class CPL_UNSTABLE_API swq_op_registrar
221 static const swq_operation *GetOperator(
const char *);
222 static const swq_operation *GetOperator(swq_op eOperation);
225class CPL_UNSTABLE_API swq_custom_func_registrar
228 virtual ~swq_custom_func_registrar();
230 virtual const swq_operation *GetOperator(
const char *) = 0;
240class CPL_UNSTABLE_API swq_field_list
245 swq_field_type *types;
250 swq_table_def *table_defs;
253class CPL_UNSTABLE_API swq_parse_context
257 : nStartToken(0), pszInput(nullptr), pszNext(nullptr),
258 pszLastValid(nullptr), bAcceptCustomFuncs(FALSE), poRoot(nullptr),
264 const char *pszInput;
266 const char *pszLastValid;
267 int bAcceptCustomFuncs;
269 swq_expr_node *poRoot;
271 swq_select *poCurSelect;
278int CPL_UNSTABLE_API swqparse(swq_parse_context *context);
279int CPL_UNSTABLE_API swqlex(swq_expr_node **ppNode, swq_parse_context *context);
280void CPL_UNSTABLE_API swqerror(swq_parse_context *context,
const char *msg);
282int CPL_UNSTABLE_API swq_identify_field(
const char *table_name,
284 swq_field_list *field_list,
285 swq_field_type *this_type,
289swq_expr_compile(
const char *where_clause,
int field_count,
char **field_list,
290 swq_field_type *field_types,
int bCheck,
291 swq_custom_func_registrar *poCustomFuncRegistrar,
292 swq_expr_node **expr_root);
295swq_expr_compile2(
const char *where_clause, swq_field_list *field_list,
296 int bCheck, swq_custom_func_registrar *poCustomFuncRegistrar,
297 swq_expr_node **expr_root);
302int CPL_UNSTABLE_API swq_test_like(
const char *input,
const char *pattern);
304swq_expr_node CPL_UNSTABLE_API *
305SWQGeneralEvaluator(swq_expr_node *, swq_expr_node **,
306 const swq_evaluation_context &sContext);
307swq_field_type CPL_UNSTABLE_API
308SWQGeneralChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
309swq_expr_node CPL_UNSTABLE_API *
310SWQCastEvaluator(swq_expr_node *, swq_expr_node **,
311 const swq_evaluation_context &sContext);
312swq_field_type CPL_UNSTABLE_API
313SWQCastChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
314const char CPL_UNSTABLE_API *SWQFieldTypeToString(swq_field_type field_type);
318#define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
320#define SWQM_SUMMARY_RECORD 1
321#define SWQM_RECORDSET 2
322#define SWQM_DISTINCT_LIST 3
330 SWQCF_COUNT = SWQ_COUNT,
332 SWQCF_STDDEV_POP = SWQ_STDDEV_POP,
333 SWQCF_STDDEV_SAMP = SWQ_STDDEV_SAMP,
339 swq_col_func col_func;
345 swq_field_type field_type;
346 swq_field_type target_type;
357class CPL_UNSTABLE_API swq_summary
363 swq_field_type eType;
365 Comparator() : bSortAsc(true), eType(SWQ_STRING)
369 bool operator()(
const CPLString &,
const CPLString &)
const;
376 return sum_only_finite_terms ? sum_acc + sum_correction : sum_acc;
381 std::vector<CPLString> oVectorDistinctValues{};
382 std::set<CPLString, Comparator> oSetDistinctValues{};
383 bool sum_only_finite_terms =
true;
385 double sum_acc = 0.0;
387 double sum_correction = 0.0;
393 double mean_for_variance = 0.0;
394 double sq_dist_from_mean_acc = 0.0;
412 swq_expr_node *poExpr;
415class CPL_UNSTABLE_API swq_select_parse_options
418 swq_custom_func_registrar *poCustomFuncRegistrar;
419 int bAllowFieldsInSecondaryTablesInWhere;
420 int bAddSecondaryTablesGeometryFields;
421 int bAlwaysPrefixWithTableName;
422 int bAllowDistinctOnGeometryField;
423 int bAllowDistinctOnMultipleFields;
425 swq_select_parse_options()
426 : poCustomFuncRegistrar(nullptr),
427 bAllowFieldsInSecondaryTablesInWhere(FALSE),
428 bAddSecondaryTablesGeometryFields(FALSE),
429 bAlwaysPrefixWithTableName(FALSE),
430 bAllowDistinctOnGeometryField(FALSE),
431 bAllowDistinctOnMultipleFields(FALSE)
436class CPL_UNSTABLE_API swq_select
448 char *raw_select =
nullptr;
450 int PushField(swq_expr_node *poExpr,
const char *pszAlias,
451 bool distinct_flag,
bool bHidden);
453 int PushExcludeField(swq_expr_node *poExpr);
455 int result_columns()
const
457 return static_cast<int>(column_defs.size());
460 std::vector<swq_col_def> column_defs{};
461 std::vector<swq_summary> column_summary{};
463 int PushTableDef(
const char *pszDataSource,
const char *pszTableName,
464 const char *pszAlias);
466 swq_table_def *table_defs =
nullptr;
468 void PushJoin(
int iSecondaryTable, swq_expr_node *poExpr);
470 swq_join_def *join_defs =
nullptr;
472 swq_expr_node *where_expr =
nullptr;
474 void PushOrderBy(
const char *pszTableName,
const char *pszFieldName,
477 swq_order_def *order_defs =
nullptr;
482 void SetOffset(
GIntBig nOffset);
485 swq_select *poOtherSelect =
nullptr;
486 void PushUnionAll(swq_select *poOtherSelectIn);
488 CPLErr preparse(
const char *select_statement,
489 int bAcceptCustomFuncs = FALSE);
490 CPLErr expand_wildcard(swq_field_list *field_list,
491 int bAlwaysPrefixWithTableName);
492 CPLErr parse(swq_field_list *field_list,
493 swq_select_parse_options *poParseOptions);
497 bool bExcludedGeometry =
false;
500 bool IsFieldExcluded(
int src_index,
const char *table,
const char *field);
506 std::map<int, std::list<swq_col_def>> m_exclude_fields{};
514const char CPL_UNSTABLE_API *swq_select_summarize(swq_select *select_info,
516 const char *pszValue,
517 const double *pdfValue);
519int CPL_UNSTABLE_API swq_is_reserved_keyword(
const char *pszStr);
521char CPL_UNSTABLE_API *OGRHStoreGetValue(
const char *pszHStore,
522 const char *pszSearchedKey);
524#ifdef GDAL_COMPILATION
525void swq_fixup(swq_parse_context *psParseContext);
526swq_expr_node *swq_create_and_or_or(swq_op op, swq_expr_node *left,
527 swq_expr_node *right);
528int swq_test_like(
const char *input,
const char *pattern,
char chEscape,
529 bool insensitive,
bool bUTF8Strings);
Abstract base class for all geometry classes.
Definition ogr_geometry.h:357
Various convenience functions for CPL.
CPLErr
Error category.
Definition cpl_error.h:37
#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:936
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:205
Various convenience functions for working with strings and string lists.
Core portability services for cross-platform OGR code.
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:799
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:405