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__"
91 #define SWQ_IS_INTEGER(x) ((x) == SWQ_INTEGER || (x) == SWQ_INTEGER64)
100 class swq_field_list;
105 typedef swq_expr_node *(*swq_field_fetcher)(swq_expr_node *op,
106 void *record_handle);
107 typedef swq_expr_node *(*swq_op_evaluator)(swq_expr_node *op,
108 swq_expr_node **sub_field_values);
109 typedef swq_field_type (*swq_op_checker)(
110 swq_expr_node *op,
int bAllowMismatchTypeOnFieldComparison);
112 class swq_custom_func_registrar;
114 class CPL_UNSTABLE_API swq_expr_node
116 swq_expr_node *Evaluate(swq_field_fetcher pfnFetcher,
void *record,
122 swq_expr_node(
const swq_expr_node &);
123 swq_expr_node(swq_expr_node &&);
125 swq_expr_node &operator=(
const swq_expr_node &);
126 swq_expr_node &operator=(swq_expr_node &&);
128 bool operator==(
const swq_expr_node &)
const;
130 explicit swq_expr_node(
const char *);
131 explicit swq_expr_node(
int);
132 explicit swq_expr_node(
GIntBig);
133 explicit swq_expr_node(
double);
135 explicit swq_expr_node(swq_op);
139 void MarkAsTimestamp();
140 CPLString UnparseOperationFromUnparsedSubExpr(
char **apszSubExpr);
141 char *Unparse(swq_field_list *,
char chColumnQuote);
142 void Dump(FILE *fp,
int depth);
143 swq_field_type Check(swq_field_list *,
int bAllowFieldsInSecondaryTables,
144 int bAllowMismatchTypeOnFieldComparison,
145 swq_custom_func_registrar *poCustomFuncRegistrar,
147 swq_expr_node *Evaluate(swq_field_fetcher pfnFetcher,
void *record);
148 swq_expr_node *Clone();
150 void ReplaceBetweenByGEAndLERecurse();
151 void PushNotOperationDownToStack();
153 swq_node_type eNodeType = SNT_CONSTANT;
154 swq_field_type field_type = SWQ_INTEGER;
157 void PushSubExpression(swq_expr_node *);
158 void ReverseSubExpressions();
159 swq_op nOperation = SWQ_OR;
160 int nSubExprCount = 0;
161 swq_expr_node **papoSubExpr =
nullptr;
166 char *table_name =
nullptr;
171 double float_value = 0.0;
177 char *string_value =
nullptr;
187 swq_op_evaluator pfnEvaluator;
188 swq_op_checker pfnChecker;
191 class CPL_UNSTABLE_API swq_op_registrar
194 static const swq_operation *GetOperator(
const char *);
195 static const swq_operation *GetOperator(swq_op eOperation);
198 class CPL_UNSTABLE_API swq_custom_func_registrar
201 virtual ~swq_custom_func_registrar()
204 virtual const swq_operation *GetOperator(
const char *) = 0;
214 class CPL_UNSTABLE_API swq_field_list
219 swq_field_type *types;
224 swq_table_def *table_defs;
227 class CPL_UNSTABLE_API swq_parse_context
231 : nStartToken(0), pszInput(nullptr), pszNext(nullptr),
232 pszLastValid(nullptr), bAcceptCustomFuncs(FALSE), poRoot(nullptr),
238 const char *pszInput;
240 const char *pszLastValid;
241 int bAcceptCustomFuncs;
243 swq_expr_node *poRoot;
245 swq_select *poCurSelect;
252 int CPL_UNSTABLE_API swqparse(swq_parse_context *context);
253 int CPL_UNSTABLE_API swqlex(swq_expr_node **ppNode, swq_parse_context *context);
254 void CPL_UNSTABLE_API swqerror(swq_parse_context *context,
const char *msg);
256 int CPL_UNSTABLE_API swq_identify_field(
const char *table_name,
258 swq_field_list *field_list,
259 swq_field_type *this_type,
263 swq_expr_compile(
const char *where_clause,
int field_count,
char **field_list,
264 swq_field_type *field_types,
int bCheck,
265 swq_custom_func_registrar *poCustomFuncRegistrar,
266 swq_expr_node **expr_root);
269 swq_expr_compile2(
const char *where_clause, swq_field_list *field_list,
270 int bCheck, swq_custom_func_registrar *poCustomFuncRegistrar,
271 swq_expr_node **expr_root);
276 int CPL_UNSTABLE_API swq_test_like(
const char *input,
const char *pattern);
278 swq_expr_node CPL_UNSTABLE_API *SWQGeneralEvaluator(swq_expr_node *,
280 swq_field_type CPL_UNSTABLE_API
281 SWQGeneralChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
282 swq_expr_node CPL_UNSTABLE_API *SWQCastEvaluator(swq_expr_node *,
284 swq_field_type CPL_UNSTABLE_API
285 SWQCastChecker(swq_expr_node *node,
int bAllowMismatchTypeOnFieldComparison);
286 const char CPL_UNSTABLE_API *SWQFieldTypeToString(swq_field_type field_type);
290 #define SWQP_ALLOW_UNDEFINED_COL_FUNCS 0x01
292 #define SWQM_SUMMARY_RECORD 1
293 #define SWQM_RECORDSET 2
294 #define SWQM_DISTINCT_LIST 3
302 SWQCF_COUNT = SWQ_COUNT,
309 swq_col_func col_func;
315 swq_field_type field_type;
316 swq_field_type target_type;
326 class CPL_UNSTABLE_API swq_summary
332 swq_field_type eType;
334 Comparator() : bSortAsc(true), eType(SWQ_STRING)
343 std::vector<CPLString> oVectorDistinctValues{};
344 std::set<CPLString, Comparator> oSetDistinctValues{};
364 swq_expr_node *poExpr;
367 class CPL_UNSTABLE_API swq_select_parse_options
370 swq_custom_func_registrar *poCustomFuncRegistrar;
371 int bAllowFieldsInSecondaryTablesInWhere;
372 int bAddSecondaryTablesGeometryFields;
373 int bAlwaysPrefixWithTableName;
374 int bAllowDistinctOnGeometryField;
375 int bAllowDistinctOnMultipleFields;
377 swq_select_parse_options()
378 : poCustomFuncRegistrar(nullptr),
379 bAllowFieldsInSecondaryTablesInWhere(FALSE),
380 bAddSecondaryTablesGeometryFields(FALSE),
381 bAlwaysPrefixWithTableName(FALSE),
382 bAllowDistinctOnGeometryField(FALSE),
383 bAllowDistinctOnMultipleFields(FALSE)
388 class CPL_UNSTABLE_API swq_select
400 char *raw_select =
nullptr;
402 int PushField(swq_expr_node *poExpr,
const char *pszAlias =
nullptr,
403 int distinct_flag = FALSE);
404 int result_columns = 0;
405 swq_col_def *column_defs =
nullptr;
406 std::vector<swq_summary> column_summary{};
408 int PushTableDef(
const char *pszDataSource,
const char *pszTableName,
409 const char *pszAlias);
411 swq_table_def *table_defs =
nullptr;
413 void PushJoin(
int iSecondaryTable, swq_expr_node *poExpr);
415 swq_join_def *join_defs =
nullptr;
417 swq_expr_node *where_expr =
nullptr;
419 void PushOrderBy(
const char *pszTableName,
const char *pszFieldName,
422 swq_order_def *order_defs =
nullptr;
427 void SetOffset(
GIntBig nOffset);
430 swq_select *poOtherSelect =
nullptr;
431 void PushUnionAll(swq_select *poOtherSelectIn);
433 CPLErr preparse(
const char *select_statement,
434 int bAcceptCustomFuncs = FALSE);
435 CPLErr expand_wildcard(swq_field_list *field_list,
436 int bAlwaysPrefixWithTableName);
437 CPLErr parse(swq_field_list *field_list,
438 swq_select_parse_options *poParseOptions);
444 CPLErr CPL_UNSTABLE_API swq_select_parse(swq_select *select_info,
445 swq_field_list *field_list,
448 const char CPL_UNSTABLE_API *swq_select_summarize(swq_select *select_info,
452 int CPL_UNSTABLE_API swq_is_reserved_keyword(
const char *pszStr);
454 char CPL_UNSTABLE_API *OGRHStoreGetValue(
const char *pszHStore,
455 const char *pszSearchedKey);
457 #ifdef GDAL_COMPILATION
458 void swq_fixup(swq_parse_context *psParseContext);
459 swq_expr_node *swq_create_and_or_or(swq_op op, swq_expr_node *left,
460 swq_expr_node *right);