Line data Source code
1 : /******************************************************************************
2 : *
3 : * Component: OGR SQL Engine
4 : * Purpose: Implementation of the swq_op_registrar class used to
5 : * represent operations possible in an SQL expression.
6 : * Author: Frank Warmerdam <warmerdam@pobox.com>
7 : *
8 : ******************************************************************************
9 : * Copyright (C) 2010 Frank Warmerdam <warmerdam@pobox.com>
10 : * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #include "cpl_port.h"
16 : #include "ogr_swq.h"
17 :
18 : #include <cstddef>
19 :
20 : #include "cpl_conv.h"
21 : #include "cpl_error.h"
22 :
23 : //! @cond Doxygen_Suppress
24 : static swq_field_type
25 : SWQColumnFuncChecker(swq_expr_node *poNode,
26 : int bAllowMismatchTypeOnFieldComparison);
27 :
28 : static const swq_operation swq_apsOperations[] = {
29 : {"OR", SWQ_OR, SWQGeneralEvaluator, SWQGeneralChecker},
30 : {"AND", SWQ_AND, SWQGeneralEvaluator, SWQGeneralChecker},
31 : {"NOT", SWQ_NOT, SWQGeneralEvaluator, SWQGeneralChecker},
32 : {"=", SWQ_EQ, SWQGeneralEvaluator, SWQGeneralChecker},
33 : {"<>", SWQ_NE, SWQGeneralEvaluator, SWQGeneralChecker},
34 : {">=", SWQ_GE, SWQGeneralEvaluator, SWQGeneralChecker},
35 : {"<=", SWQ_LE, SWQGeneralEvaluator, SWQGeneralChecker},
36 : {"<", SWQ_LT, SWQGeneralEvaluator, SWQGeneralChecker},
37 : {">", SWQ_GT, SWQGeneralEvaluator, SWQGeneralChecker},
38 : {"LIKE", SWQ_LIKE, SWQGeneralEvaluator, SWQGeneralChecker},
39 : {"ILIKE", SWQ_ILIKE, SWQGeneralEvaluator, SWQGeneralChecker},
40 : {"IS NULL", SWQ_ISNULL, SWQGeneralEvaluator, SWQGeneralChecker},
41 : {"IN", SWQ_IN, SWQGeneralEvaluator, SWQGeneralChecker},
42 : {"BETWEEN", SWQ_BETWEEN, SWQGeneralEvaluator, SWQGeneralChecker},
43 : {"+", SWQ_ADD, SWQGeneralEvaluator, SWQGeneralChecker},
44 : {"-", SWQ_SUBTRACT, SWQGeneralEvaluator, SWQGeneralChecker},
45 : {"*", SWQ_MULTIPLY, SWQGeneralEvaluator, SWQGeneralChecker},
46 : {"/", SWQ_DIVIDE, SWQGeneralEvaluator, SWQGeneralChecker},
47 : {"%", SWQ_MODULUS, SWQGeneralEvaluator, SWQGeneralChecker},
48 : {"CONCAT", SWQ_CONCAT, SWQGeneralEvaluator, SWQGeneralChecker},
49 : {"SUBSTR", SWQ_SUBSTR, SWQGeneralEvaluator, SWQGeneralChecker},
50 : {"HSTORE_GET_VALUE", SWQ_HSTORE_GET_VALUE, SWQGeneralEvaluator,
51 : SWQGeneralChecker},
52 :
53 : {"AVG", SWQ_AVG, SWQGeneralEvaluator, SWQColumnFuncChecker},
54 : {"MIN", SWQ_MIN, SWQGeneralEvaluator, SWQColumnFuncChecker},
55 : {"MAX", SWQ_MAX, SWQGeneralEvaluator, SWQColumnFuncChecker},
56 : {"COUNT", SWQ_COUNT, SWQGeneralEvaluator, SWQColumnFuncChecker},
57 : {"SUM", SWQ_SUM, SWQGeneralEvaluator, SWQColumnFuncChecker},
58 : {"STDDEV_POP", SWQ_STDDEV_POP, SWQGeneralEvaluator, SWQColumnFuncChecker},
59 : {"STDDEV_SAMP", SWQ_STDDEV_SAMP, SWQGeneralEvaluator, SWQColumnFuncChecker},
60 :
61 : {"CAST", SWQ_CAST, SWQCastEvaluator, SWQCastChecker}};
62 :
63 : /************************************************************************/
64 : /* GetOperator() */
65 : /************************************************************************/
66 :
67 345 : const swq_operation *swq_op_registrar::GetOperator(const char *pszName)
68 :
69 : {
70 9227 : for (const auto &op : swq_apsOperations)
71 : {
72 9099 : if (EQUAL(pszName, op.pszName))
73 217 : return &op;
74 : }
75 :
76 128 : return nullptr;
77 : }
78 :
79 : /************************************************************************/
80 : /* GetOperator() */
81 : /************************************************************************/
82 :
83 99053 : const swq_operation *swq_op_registrar::GetOperator(swq_op eOperator)
84 :
85 : {
86 428426 : for (const auto &op : swq_apsOperations)
87 : {
88 428410 : if (eOperator == op.eOperation)
89 99037 : return &op;
90 : }
91 :
92 16 : return nullptr;
93 : }
94 :
95 : /************************************************************************/
96 : /* SWQColumnFuncChecker() */
97 : /* */
98 : /* Column summary functions are not legal in any context except */
99 : /* as a root operator on column definitions. They are removed */
100 : /* from this tree before checking so we just need to issue an */
101 : /* error if they are used in any other context. */
102 : /************************************************************************/
103 :
104 : static swq_field_type
105 0 : SWQColumnFuncChecker(swq_expr_node *poNode,
106 : int /* bAllowMismatchTypeOnFieldComparison */)
107 : {
108 : const swq_operation *poOp =
109 0 : swq_op_registrar::GetOperator(static_cast<swq_op>(poNode->nOperation));
110 0 : CPLError(CE_Failure, CPLE_AppDefined,
111 : "Column Summary Function '%s' found in an inappropriate context.",
112 : poOp != nullptr ? poOp->pszName : "");
113 0 : return SWQ_ERROR;
114 : }
115 :
116 : //! @endcond
|