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 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "cpl_port.h"
32 : #include "ogr_swq.h"
33 :
34 : #include <cstddef>
35 :
36 : #include "cpl_conv.h"
37 : #include "cpl_error.h"
38 :
39 : //! @cond Doxygen_Suppress
40 : static swq_field_type
41 : SWQColumnFuncChecker(swq_expr_node *poNode,
42 : int bAllowMismatchTypeOnFieldComparison);
43 :
44 : static const swq_operation swq_apsOperations[] = {
45 : {"OR", SWQ_OR, SWQGeneralEvaluator, SWQGeneralChecker},
46 : {"AND", SWQ_AND, SWQGeneralEvaluator, SWQGeneralChecker},
47 : {"NOT", SWQ_NOT, SWQGeneralEvaluator, SWQGeneralChecker},
48 : {"=", SWQ_EQ, SWQGeneralEvaluator, SWQGeneralChecker},
49 : {"<>", SWQ_NE, SWQGeneralEvaluator, SWQGeneralChecker},
50 : {">=", SWQ_GE, SWQGeneralEvaluator, SWQGeneralChecker},
51 : {"<=", SWQ_LE, SWQGeneralEvaluator, SWQGeneralChecker},
52 : {"<", SWQ_LT, SWQGeneralEvaluator, SWQGeneralChecker},
53 : {">", SWQ_GT, SWQGeneralEvaluator, SWQGeneralChecker},
54 : {"LIKE", SWQ_LIKE, SWQGeneralEvaluator, SWQGeneralChecker},
55 : {"ILIKE", SWQ_ILIKE, SWQGeneralEvaluator, SWQGeneralChecker},
56 : {"IS NULL", SWQ_ISNULL, SWQGeneralEvaluator, SWQGeneralChecker},
57 : {"IN", SWQ_IN, SWQGeneralEvaluator, SWQGeneralChecker},
58 : {"BETWEEN", SWQ_BETWEEN, SWQGeneralEvaluator, SWQGeneralChecker},
59 : {"+", SWQ_ADD, SWQGeneralEvaluator, SWQGeneralChecker},
60 : {"-", SWQ_SUBTRACT, SWQGeneralEvaluator, SWQGeneralChecker},
61 : {"*", SWQ_MULTIPLY, SWQGeneralEvaluator, SWQGeneralChecker},
62 : {"/", SWQ_DIVIDE, SWQGeneralEvaluator, SWQGeneralChecker},
63 : {"%", SWQ_MODULUS, SWQGeneralEvaluator, SWQGeneralChecker},
64 : {"CONCAT", SWQ_CONCAT, SWQGeneralEvaluator, SWQGeneralChecker},
65 : {"SUBSTR", SWQ_SUBSTR, SWQGeneralEvaluator, SWQGeneralChecker},
66 : {"HSTORE_GET_VALUE", SWQ_HSTORE_GET_VALUE, SWQGeneralEvaluator,
67 : SWQGeneralChecker},
68 :
69 : {"AVG", SWQ_AVG, SWQGeneralEvaluator, SWQColumnFuncChecker},
70 : {"MIN", SWQ_MIN, SWQGeneralEvaluator, SWQColumnFuncChecker},
71 : {"MAX", SWQ_MAX, SWQGeneralEvaluator, SWQColumnFuncChecker},
72 : {"COUNT", SWQ_COUNT, SWQGeneralEvaluator, SWQColumnFuncChecker},
73 : {"SUM", SWQ_SUM, SWQGeneralEvaluator, SWQColumnFuncChecker},
74 :
75 : {"CAST", SWQ_CAST, SWQCastEvaluator, SWQCastChecker}};
76 :
77 : /************************************************************************/
78 : /* GetOperator() */
79 : /************************************************************************/
80 :
81 342 : const swq_operation *swq_op_registrar::GetOperator(const char *pszName)
82 :
83 : {
84 8860 : for (const auto &op : swq_apsOperations)
85 : {
86 8731 : if (EQUAL(pszName, op.pszName))
87 213 : return &op;
88 : }
89 :
90 129 : return nullptr;
91 : }
92 :
93 : /************************************************************************/
94 : /* GetOperator() */
95 : /************************************************************************/
96 :
97 84744 : const swq_operation *swq_op_registrar::GetOperator(swq_op eOperator)
98 :
99 : {
100 359767 : for (const auto &op : swq_apsOperations)
101 : {
102 359751 : if (eOperator == op.eOperation)
103 84728 : return &op;
104 : }
105 :
106 16 : return nullptr;
107 : }
108 :
109 : /************************************************************************/
110 : /* SWQColumnFuncChecker() */
111 : /* */
112 : /* Column summary functions are not legal in any context except */
113 : /* as a root operator on column definitions. They are removed */
114 : /* from this tree before checking so we just need to issue an */
115 : /* error if they are used in any other context. */
116 : /************************************************************************/
117 :
118 : static swq_field_type
119 1 : SWQColumnFuncChecker(swq_expr_node *poNode,
120 : int /* bAllowMismatchTypeOnFieldComparison */)
121 : {
122 : const swq_operation *poOp =
123 1 : swq_op_registrar::GetOperator(static_cast<swq_op>(poNode->nOperation));
124 1 : CPLError(CE_Failure, CPLE_AppDefined,
125 : "Column Summary Function '%s' found in an inappropriate context.",
126 : poOp != nullptr ? poOp->pszName : "");
127 1 : return SWQ_ERROR;
128 : }
129 :
130 : //! @endcond
|