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