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 680 : const swq_operation *swq_op_registrar::GetOperator( const char *pszName )
43 :
44 : {
45 : unsigned int i;
46 :
47 680 : if( !papoOperations )
48 0 : Initialize();
49 :
50 9434 : for( i = 0; i < papoOperations->size(); i++ )
51 : {
52 8784 : if( EQUAL(pszName,(*papoOperations)[i]->osName.c_str()) )
53 30 : return (*papoOperations)[i];
54 : }
55 :
56 650 : return NULL;
57 : }
58 :
59 : /************************************************************************/
60 : /* GetOperator() */
61 : /************************************************************************/
62 :
63 456541 : const swq_operation *swq_op_registrar::GetOperator( swq_op eOperator )
64 :
65 : {
66 : unsigned int i;
67 :
68 456541 : if( !papoOperations )
69 25 : Initialize();
70 :
71 2126399 : for( i = 0; i < papoOperations->size(); i++ )
72 : {
73 2126399 : if( eOperator == (*papoOperations)[i]->eOperation )
74 456541 : return (*papoOperations)[i];
75 : }
76 :
77 0 : return NULL;
78 : }
79 :
80 :
81 : /************************************************************************/
82 : /* AddOperator() */
83 : /************************************************************************/
84 :
85 650 : void swq_op_registrar::AddOperator( const char *pszName, swq_op eOpCode,
86 : swq_op_evaluator pfnEvaluator,
87 : swq_op_checker pfnChecker )
88 :
89 : {
90 650 : if( GetOperator( pszName ) != NULL )
91 0 : return;
92 :
93 650 : if( pfnEvaluator == NULL )
94 625 : pfnEvaluator = SWQGeneralEvaluator;
95 650 : if( pfnChecker == NULL )
96 500 : pfnChecker = SWQGeneralChecker;
97 :
98 650 : swq_operation *poOp = new swq_operation();
99 :
100 650 : poOp->eOperation = eOpCode;
101 1300 : poOp->osName = pszName;
102 650 : poOp->pfnEvaluator = pfnEvaluator;
103 650 : poOp->pfnChecker = pfnChecker;
104 :
105 650 : 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 1 : static swq_field_type SWQColumnFuncChecker( swq_expr_node *poNode )
120 : {
121 : const swq_operation *poOp =
122 1 : 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 1 : (poOp) ? poOp->osName.c_str() : "" );
126 1 : return SWQ_ERROR;
127 : }
128 :
129 :
130 : /************************************************************************/
131 : /* Initialize() */
132 : /************************************************************************/
133 :
134 25 : void swq_op_registrar::Initialize()
135 :
136 : {
137 25 : CPLMutexHolderD( &hOperationsMutex );
138 :
139 25 : if( papoOperations )
140 : return;
141 :
142 25 : papoOperations = new std::vector<swq_operation*>;
143 :
144 25 : AddOperator( "OR", SWQ_OR );
145 25 : AddOperator( "AND", SWQ_AND );
146 25 : AddOperator( "NOT", SWQ_NOT );
147 25 : AddOperator( "=", SWQ_EQ );
148 25 : AddOperator( "<>", SWQ_NE );
149 25 : AddOperator( ">=", SWQ_GE );
150 25 : AddOperator( "<=", SWQ_LE );
151 25 : AddOperator( "<", SWQ_LT );
152 25 : AddOperator( ">", SWQ_GT );
153 25 : AddOperator( "LIKE", SWQ_LIKE );
154 25 : AddOperator( "IS NULL", SWQ_ISNULL );
155 25 : AddOperator( "IN", SWQ_IN );
156 25 : AddOperator( "BETWEEN", SWQ_BETWEEN );
157 25 : AddOperator( "+", SWQ_ADD );
158 25 : AddOperator( "-", SWQ_SUBTRACT );
159 25 : AddOperator( "*", SWQ_MULTIPLY );
160 25 : AddOperator( "/", SWQ_DIVIDE );
161 25 : AddOperator( "%", SWQ_MODULUS );
162 25 : AddOperator( "CONCAT", SWQ_CONCAT );
163 25 : AddOperator( "SUBSTR", SWQ_SUBSTR );
164 :
165 25 : AddOperator( "AVG", SWQ_AVG, NULL, SWQColumnFuncChecker );
166 25 : AddOperator( "MIN", SWQ_MIN, NULL, SWQColumnFuncChecker );
167 25 : AddOperator( "MAX", SWQ_MAX, NULL, SWQColumnFuncChecker );
168 25 : AddOperator( "COUNT", SWQ_COUNT, NULL, SWQColumnFuncChecker );
169 25 : AddOperator( "SUM", SWQ_SUM, NULL, SWQColumnFuncChecker );
170 :
171 25 : AddOperator( "CAST", SWQ_CAST, SWQCastEvaluator, SWQCastChecker );
172 : }
173 :
174 : /************************************************************************/
175 : /* DeInitialize() */
176 : /************************************************************************/
177 :
178 442 : void swq_op_registrar::DeInitialize()
179 :
180 : {
181 : {
182 442 : CPLMutexHolderD( &hOperationsMutex );
183 :
184 442 : if( papoOperations != NULL)
185 : {
186 675 : for( unsigned int i=0; i < papoOperations->size(); i++ )
187 650 : delete (*papoOperations)[i];
188 :
189 25 : delete papoOperations;
190 25 : papoOperations = NULL;
191 442 : }
192 : }
193 :
194 442 : CPLDestroyMutex( hOperationsMutex );
195 442 : hOperationsMutex = NULL;
196 442 : }
|