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);
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,
162 swq_expr_node *Evaluate(swq_field_fetcher pfnFetcher,
void *record,
163 const swq_evaluation_context &sContext);
164 swq_expr_node *Clone();
166 void ReplaceBetweenByGEAndLERecurse();
167 void PushNotOperationDownToStack();
169 swq_node_type eNodeType = SNT_CONSTANT;
170 swq_field_type field_type = SWQ_INTEGER;
173 void PushSubExpression(swq_expr_node *);
174 void ReverseSubExpressions();
175 swq_op nOperation = SWQ_OR;
176 int nSubExprCount = 0;
177 swq_expr_node **papoSubExpr =
nullptr;
182 char *table_name =
nullptr;
187 double float_value = 0.0;
193 char *string_value =
nullptr;
197 bool bHidden =
false;
207 swq_op_evaluator pfnEvaluator;
208 swq_op_checker pfnChecker;
211class CPL_UNSTABLE_API swq_op_registrar
214 static const swq_operation *GetOperator(
const char *);
215 static const swq_operation *GetOperator(swq_op eOperation);
218class CPL_UNSTABLE_API swq_custom_func_registrar
221 virtual ~swq_custom_func_registrar()
225 virtual const swq_operation *GetOperator(
const char *) = 0;
235class CPL_UNSTABLE_API swq_field_list
240 swq_field_type *types;
245 swq_table_def *table_defs;
248class CPL_UNSTABLE_API swq_parse_context
252 : nStartToken(0), pszInput(nullptr), pszNext(nullptr),
253 pszLastValid(nullptr), bAcceptCustomFuncs(FALSE), poRoot(nullptr),
259 const char *pszInput;
261 const char *pszLastValid;
262 int bAcceptCustomFuncs;
264 swq_expr_node *poRoot;
266 swq_select *poCurSelect;
273int CPL_UNSTABLE_API swqparse(swq_parse_context *context);
274int CPL_UNSTABLE_API swqlex(swq_expr_node **ppNode, swq_parse_context *context);
275void CPL_UNSTABLE_API swqerror(swq_parse_context *context,
const char *msg);
277int CPL_UNSTABLE_API swq_identify_field(
const char *table_name,
279 swq_field_list *field_list,
280 swq_field_type *this_type,
284swq_expr_compile(
const char *where_clause,
int field_count,
char **field_list,
285 swq_field_type *field_types,
int bCheck,
286 swq_custom_func_registrar *poCustomFuncRegistrar,
287 swq_expr_node **expr_root);
290swq_expr_compile2(
const char *where_clause, swq_field_list *field_list,
291 int bCheck, swq_custom_func_registrar *poCustomFuncRegistrar,
292 swq_expr_node **expr_root);
297int CPL_UNSTABLE_API swq_test_like(
const char *input,
const char *pattern);
299swq_expr_node CPL_UNSTABLE_API *
300SWQGeneralEvaluator(swq_expr_node *, swq_expr_node **,
301 const swq_evaluation_context &sContext);
302swq_field_type CPL_UNSTABLE_API
303SWQGeneralChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
304swq_expr_node CPL_UNSTABLE_API *
305SWQCastEvaluator(swq_expr_node *, swq_expr_node **,
306 const swq_evaluation_context &sContext);
307swq_field_type CPL_UNSTABLE_API
308SWQCastChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
309const char CPL_UNSTABLE_API *SWQFieldTypeToString(swq_field_type field_type);
313#define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
315#define SWQM_SUMMARY_RECORD 1
316#define SWQM_RECORDSET 2
317#define SWQM_DISTINCT_LIST 3
325 SWQCF_COUNT = SWQ_COUNT,
327 SWQCF_STDDEV_POP = SWQ_STDDEV_POP,
328 SWQCF_STDDEV_SAMP = SWQ_STDDEV_SAMP,
334 swq_col_func col_func;
340 swq_field_type field_type;
341 swq_field_type target_type;
352class CPL_UNSTABLE_API swq_summary
358 swq_field_type eType;
360 Comparator() : bSortAsc(true), eType(SWQ_STRING)
371 return sum_only_finite_terms ? sum_acc + sum_correction : sum_acc;
376 std::vector<CPLString> oVectorDistinctValues{};
377 std::set<CPLString, Comparator> oSetDistinctValues{};
378 bool sum_only_finite_terms =
true;
380 double sum_acc = 0.0;
382 double sum_correction = 0.0;
388 double mean_for_variance = 0.0;
389 double sq_dist_from_mean_acc = 0.0;
407 swq_expr_node *poExpr;
410class CPL_UNSTABLE_API swq_select_parse_options
413 swq_custom_func_registrar *poCustomFuncRegistrar;
414 int bAllowFieldsInSecondaryTablesInWhere;
415 int bAddSecondaryTablesGeometryFields;
416 int bAlwaysPrefixWithTableName;
417 int bAllowDistinctOnGeometryField;
418 int bAllowDistinctOnMultipleFields;
420 swq_select_parse_options()
421 : poCustomFuncRegistrar(nullptr),
422 bAllowFieldsInSecondaryTablesInWhere(FALSE),
423 bAddSecondaryTablesGeometryFields(FALSE),
424 bAlwaysPrefixWithTableName(FALSE),
425 bAllowDistinctOnGeometryField(FALSE),
426 bAllowDistinctOnMultipleFields(FALSE)
431class CPL_UNSTABLE_API swq_select
443 char *raw_select =
nullptr;
445 int PushField(swq_expr_node *poExpr,
const char *pszAlias,
446 bool distinct_flag,
bool bHidden);
448 int PushExcludeField(swq_expr_node *poExpr);
450 int result_columns()
const
452 return static_cast<int>(column_defs.size());
455 std::vector<swq_col_def> column_defs{};
456 std::vector<swq_summary> column_summary{};
458 int PushTableDef(
const char *pszDataSource,
const char *pszTableName,
459 const char *pszAlias);
461 swq_table_def *table_defs =
nullptr;
463 void PushJoin(
int iSecondaryTable, swq_expr_node *poExpr);
465 swq_join_def *join_defs =
nullptr;
467 swq_expr_node *where_expr =
nullptr;
469 void PushOrderBy(
const char *pszTableName,
const char *pszFieldName,
472 swq_order_def *order_defs =
nullptr;
477 void SetOffset(
GIntBig nOffset);
480 swq_select *poOtherSelect =
nullptr;
481 void PushUnionAll(swq_select *poOtherSelectIn);
483 CPLErr preparse(
const char *select_statement,
484 int bAcceptCustomFuncs = FALSE);
485 CPLErr expand_wildcard(swq_field_list *field_list,
486 int bAlwaysPrefixWithTableName);
487 CPLErr parse(swq_field_list *field_list,
488 swq_select_parse_options *poParseOptions);
492 bool bExcludedGeometry =
false;
495 bool IsFieldExcluded(
int src_index,
const char *table,
const char *field);
501 std::map<int, std::list<swq_col_def>> m_exclude_fields{};
509const char CPL_UNSTABLE_API *swq_select_summarize(swq_select *select_info,
511 const char *pszValue,
512 const double *pdfValue);
514int CPL_UNSTABLE_API swq_is_reserved_keyword(
const char *pszStr);
516char CPL_UNSTABLE_API *OGRHStoreGetValue(
const char *pszHStore,
517 const char *pszSearchedKey);
519#ifdef GDAL_COMPILATION
520void swq_fixup(swq_parse_context *psParseContext);
521swq_expr_node *swq_create_and_or_or(swq_op op, swq_expr_node *left,
522 swq_expr_node *right);
523int swq_test_like(
const char *input,
const char *pattern,
char chEscape,
524 bool insensitive,
bool bUTF8Strings);
Convenient string class based on std::string.
Definition cpl_string.h:307
Abstract base class for all geometry classes.
Definition ogr_geometry.h:361
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:1030
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:199
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:816
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:407