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 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "cpl_conv.h"
31 : #include "cpl_multiproc.h"
32 : #include "swq.h"
33 : #include <vector>
34 :
35 : static void *hOperationsMutex = NULL;
36 : static std::vector<swq_operation*>* papoOperations = NULL;
37 :
38 : /************************************************************************/
39 : /* GetOperator() */
40 : /************************************************************************/
41 :
42 1598 : const swq_operation *swq_op_registrar::GetOperator( const char *pszName )
43 :
44 : {
45 : unsigned int i;
46 :
47 1598 : if( !papoOperations )
48 0 : Initialize();
49 :
50 22111 : for( i = 0; i < papoOperations->size(); i++ )
51 : {
52 20577 : if( EQUAL(pszName,(*papoOperations)[i]->osName.c_str()) )
53 64 : return (*papoOperations)[i];
54 : }
55 :
56 1534 : return NULL;
57 : }
58 :
59 : /************************************************************************/
60 : /* GetOperator() */
61 : /************************************************************************/
62 :
63 922010 : const swq_operation *swq_op_registrar::GetOperator( swq_op eOperator )
64 :
65 : {
66 : unsigned int i;
67 :
68 922010 : if( !papoOperations )
69 59 : Initialize();
70 :
71 4294928 : for( i = 0; i < papoOperations->size(); i++ )
72 : {
73 4294928 : if( eOperator == (*papoOperations)[i]->eOperation )
74 922010 : return (*papoOperations)[i];
75 : }
76 :
77 0 : return NULL;
78 : }
79 :
80 :
81 : /************************************************************************/
82 : /* AddOperator() */
83 : /************************************************************************/
84 :
85 1534 : void swq_op_registrar::AddOperator( const char *pszName, swq_op eOpCode,
86 : swq_op_evaluator pfnEvaluator,
87 : swq_op_checker pfnChecker )
88 :
89 : {
90 1534 : if( GetOperator( pszName ) != NULL )
91 0 : return;
92 :
93 1534 : if( pfnEvaluator == NULL )
94 1475 : pfnEvaluator = SWQGeneralEvaluator;
95 1534 : if( pfnChecker == NULL )
96 1180 : pfnChecker = SWQGeneralChecker;
97 :
98 1534 : swq_operation *poOp = new swq_operation();
99 :
100 1534 : poOp->eOperation = eOpCode;
101 3068 : poOp->osName = pszName;
102 1534 : poOp->pfnEvaluator = pfnEvaluator;
103 1534 : poOp->pfnChecker = pfnChecker;
104 :
105 1534 : papoOperations->push_back( poOp );
106 : }
107 :
108 :
109 :
110 : /************************************************************************/
111 : /* SWQColumnFuncChecker() */
112 : /* */
113 : /* Column summary functions are not legal in any context except */
114 : /* as a root operator on column definitions. They are removed */
115 : /* from this tree before checking so we just need to issue an */
116 : /* error if they are used in any other context. */
117 : /************************************************************************/
118 :
119 2 : static swq_field_type SWQColumnFuncChecker( swq_expr_node *poNode )
120 : {
121 : const swq_operation *poOp =
122 2 : swq_op_registrar::GetOperator((swq_op)poNode->nOperation);
123 : CPLError( CE_Failure, CPLE_AppDefined,
124 : "Column Summary Function '%s' found in an inappropriate context.",
125 2 : (poOp) ? poOp->osName.c_str() : "" );
126 2 : return SWQ_ERROR;
127 : }
128 :
129 :
130 : /************************************************************************/
131 : /* Initialize() */
132 : /************************************************************************/
133 :
134 59 : void swq_op_registrar::Initialize()
135 :
136 : {
137 59 : CPLMutexHolderD( &hOperationsMutex );
138 :
139 59 : if( papoOperations )
140 : return;
141 :
142 59 : papoOperations = new std::vector<swq_operation*>;
143 :
144 59 : AddOperator( "OR", SWQ_OR );
145 59 : AddOperator( "AND", SWQ_AND );
146 59 : AddOperator( "NOT", SWQ_NOT );
147 59 : AddOperator( "=", SWQ_EQ );
148 59 : AddOperator( "<>", SWQ_NE );
149 59 : AddOperator( ">=", SWQ_GE );
150 59 : AddOperator( "<=", SWQ_LE );
151 59 : AddOperator( "<", SWQ_LT );
152 59 : AddOperator( ">", SWQ_GT );
153 59 : AddOperator( "LIKE", SWQ_LIKE );
154 59 : AddOperator( "IS NULL", SWQ_ISNULL );
155 59 : AddOperator( "IN", SWQ_IN );
156 59 : AddOperator( "BETWEEN", SWQ_BETWEEN );
157 59 : AddOperator( "+", SWQ_ADD );
158 59 : AddOperator( "-", SWQ_SUBTRACT );
159 59 : AddOperator( "*", SWQ_MULTIPLY );
160 59 : AddOperator( "/", SWQ_DIVIDE );
161 59 : AddOperator( "%", SWQ_MODULUS );
162 59 : AddOperator( "CONCAT", SWQ_CONCAT );
163 59 : AddOperator( "SUBSTR", SWQ_SUBSTR );
164 :
165 59 : AddOperator( "AVG", SWQ_AVG, NULL, SWQColumnFuncChecker );
166 59 : AddOperator( "MIN", SWQ_MIN, NULL, SWQColumnFuncChecker );
167 59 : AddOperator( "MAX", SWQ_MAX, NULL, SWQColumnFuncChecker );
168 59 : AddOperator( "COUNT", SWQ_COUNT, NULL, SWQColumnFuncChecker );
169 59 : AddOperator( "SUM", SWQ_SUM, NULL, SWQColumnFuncChecker );
170 :
171 59 : AddOperator( "CAST", SWQ_CAST, SWQCastEvaluator, SWQCastChecker );
172 : }
173 :
174 : /************************************************************************/
175 : /* DeInitialize() */
176 : /************************************************************************/
177 :
178 917 : void swq_op_registrar::DeInitialize()
179 :
180 : {
181 : {
182 917 : CPLMutexHolderD( &hOperationsMutex );
183 :
184 917 : if( papoOperations != NULL)
185 : {
186 1593 : for( unsigned int i=0; i < papoOperations->size(); i++ )
187 1534 : delete (*papoOperations)[i];
188 :
189 59 : delete papoOperations;
190 59 : papoOperations = NULL;
191 917 : }
192 : }
193 :
194 917 : CPLDestroyMutex( hOperationsMutex );
195 917 : hOperationsMutex = NULL;
196 917 : }
|