1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _CONNECTIVITY_FILE_FCODE_HXX_
25 #define _CONNECTIVITY_FILE_FCODE_HXX_
26 
27 #include "connectivity/sqliterator.hxx"
28 #include <com/sun/star/sdbc/DataType.hpp>
29 #include "connectivity/CommonTools.hxx"
30 #include <tools/rtti.hxx>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/container/XIndexAccess.hpp>
33 #include "connectivity/FValue.hxx"
34 #include "file/filedllapi.hxx"
35 
36 namespace connectivity
37 {
38 	class OSQLParseNode;
39 	namespace dbase
40 	{
41 		class ODbaseIndex;
42 	}
43 	namespace file
44 	{
45 
46 		class OOperand;
47 		typedef ::std::stack<OOperand*> OCodeStack;
48 		class OBoolOperator;
49 		typedef ::std::map<sal_Int32,sal_Int32> OEvaluateSet;
50 
51 		typedef ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> OFileColumns;
52 
53 
54 		class OOO_DLLPUBLIC_FILE OCode
55 		{
56 		public:
57 			OCode();
58 			virtual ~OCode();
59 
operator new(size_t nSize)60 			inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
61 				{ return ::rtl_allocateMemory( nSize ); }
operator new(size_t,void * _pHint)62 			inline static void * SAL_CALL operator new( size_t /*nSize*/,void* _pHint ) SAL_THROW( () )
63 				{ return _pHint; }
operator delete(void * pMem)64 			inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
65 				{ ::rtl_freeMemory( pMem ); }
operator delete(void *,void *)66 			inline static void SAL_CALL operator delete( void * /*pMem*/,void* /*_pHint*/ ) SAL_THROW( () )
67 				{  }
68 
69 			TYPEINFO();
70 		};
71 
72 
73 		// operands that the parsetree generate
74 		class OOO_DLLPUBLIC_FILE OOperand : public OCode
75 		{
76 		protected:
77 			sal_Int32 m_eDBType;
78 
OOperand(const sal_Int32 & _rType)79 			OOperand(const sal_Int32& _rType) : m_eDBType(_rType){}
OOperand()80 			OOperand() : m_eDBType(::com::sun::star::sdbc::DataType::OTHER){}
81 
82 		public:
83 			virtual const ORowSetValue& getValue() const = 0;
84 			virtual void setValue(const ORowSetValue& _rVal) = 0;
85 
getDBType() const86 			virtual sal_Int32 getDBType() const {return m_eDBType;}
87 			virtual OEvaluateSet* preProcess(OBoolOperator* pOp, OOperand* pRight = 0);
88 			inline sal_Bool	isValid() const;
89 
90 			TYPEINFO();
91 		};
92 
93 		class OOO_DLLPUBLIC_FILE OOperandRow : public OOperand
94 		{
95 			sal_uInt16	m_nRowPos;
96 		protected:
97 			OValueRefRow	m_pRow;
98 
99 			OOperandRow(sal_uInt16 _nPos, sal_Int32 _rType);
100 		public:
getRowPos() const101 			sal_uInt16 getRowPos() const {return m_nRowPos;}
102 			virtual const ORowSetValue& getValue() const;
103 			virtual void setValue(const ORowSetValue& _rVal);
104             void bindValue(const OValueRefRow& _pRow);                      // Bindung an den Wert, den der Operand repraesentiert
105 
106 			TYPEINFO();
107 		};
108 
109 		// Attribute aus einer Ergebniszeile
110 		class OOO_DLLPUBLIC_FILE OOperandAttr : public OOperandRow
111 		{
112 		protected:
113 			::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> m_xColumn;
114 
115 		public:
116 			OOperandAttr(sal_uInt16 _nPos,
117 						 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xColumn);
118 
119 			virtual sal_Bool isIndexed() const;
120 			virtual OEvaluateSet* preProcess(OBoolOperator* pOp, OOperand* pRight = 0);
121 			TYPEINFO();
122 		};
123 
124         // Parameter fuer ein Praedikat
125 		class OOperandParam : public OOperandRow
126 		{
127 		public:
128 			OOperandParam(connectivity::OSQLParseNode* pNode, sal_Int32 _nPos);
129 			void describe(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xColumn, ::vos::ORef<connectivity::OSQLColumns> _xParamColumns);
130 
131 			TYPEINFO();
132 		};
133 
134 		// WerteOperanden
135 		class OOperandValue : public OOperand
136 		{
137 		protected:
138 			ORowSetValue m_aValue;
139 
140 		protected:
OOperandValue()141 			OOperandValue(){}
OOperandValue(const ORowSetValue & _rVar,sal_Int32 eDbType)142 			OOperandValue(const ORowSetValue& _rVar, sal_Int32 eDbType)
143 				: OOperand(eDbType)
144 				, m_aValue(_rVar)
145 			{}
146 
OOperandValue(sal_Int32 eDbType)147 			OOperandValue(sal_Int32 eDbType) :OOperand(eDbType){}
148 		public:
149 			virtual const ORowSetValue& getValue() const;
150 			virtual void setValue(const ORowSetValue& _rVal);
151 
152 			TYPEINFO();
153 		};
154 
155 
156 		// Konstanten
157 		class OOperandConst : public OOperandValue
158 		{
159 		public:
160 			OOperandConst(const connectivity::OSQLParseNode& rColumnRef, const rtl::OUString& aStrValue);
161 
162 			TYPEINFO();
163 		};
164 
165 
166 		// Ergebnis Operanden
167 		class OOperandResult : public OOperandValue
168 		{
169 		protected:
OOperandResult(const ORowSetValue & _rVar,sal_Int32 eDbType)170 			OOperandResult(const ORowSetValue& _rVar, sal_Int32 eDbType)
171 							:OOperandValue(_rVar, eDbType) {}
OOperandResult(sal_Int32 eDbType)172 			OOperandResult(sal_Int32 eDbType)
173 							:OOperandValue(eDbType) {}
174 		public:
OOperandResult(const ORowSetValue & _rVar)175 			OOperandResult(const ORowSetValue& _rVar)
176 							:OOperandValue(_rVar, _rVar.getTypeKind()) {}
177 			TYPEINFO();
178 		};
179 
180 
181 		class OOperandResultBOOL : public OOperandResult
182 		{
183 		public:
OOperandResultBOOL(sal_Bool bResult)184 			OOperandResultBOOL(sal_Bool bResult) : OOperandResult(::com::sun::star::sdbc::DataType::BIT)
185 			{
186 				m_aValue = bResult ? 1.0 : 0.0;
187 				m_aValue.setBound(sal_True);
188 			}
189 		};
190 
191 		class OOperandResultNUM : public OOperandResult
192 		{
193 		public:
OOperandResultNUM(double fNum)194 			OOperandResultNUM(double fNum) : OOperandResult(::com::sun::star::sdbc::DataType::DOUBLE)
195 			{
196 				m_aValue = fNum;
197 				m_aValue.setBound(sal_True);
198 			}
199 		};
200 
201 		/** special stop operand
202 			is appended when a list of arguments ends
203 		*/
204 		class OStopOperand : public OOperandValue
205 		{
206 		public:
OStopOperand()207 			OStopOperand(){}
208 			TYPEINFO();
209 		};
210 
211 		// Operatoren
212 		class OOO_DLLPUBLIC_FILE OOperator : public OCode
213 		{
214 		public:
215 			virtual void Exec(OCodeStack&) = 0;
216             virtual sal_uInt16 getRequestedOperands() const;    // Anzahl benoetigter Operanden
217 																// Standard ist 2
218 			TYPEINFO();
219 		};
220 
221 
222 		// boolsche Operatoren
223 
224 		class OOO_DLLPUBLIC_FILE OBoolOperator : public OOperator
225 		{
226 		public:
227 			TYPEINFO();
228 			virtual void Exec(OCodeStack&);
229 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
230 		};
231 
232         class OOp_NOT : public OBoolOperator
233 		{
234 		public:
235 			TYPEINFO();
236 
237 		protected:
238             virtual void Exec(OCodeStack&);
239 			virtual sal_Bool operate(const OOperand*, const OOperand* = NULL) const;
240             virtual sal_uInt16 getRequestedOperands() const;
241 		};
242 
243 		class OOp_AND : public OBoolOperator
244 		{
245 		public:
246 			TYPEINFO();
247 
248 		protected:
249 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
250 		};
251 
252 		class OOp_OR : public OBoolOperator
253 		{
254 		public:
255 			TYPEINFO();
256 		protected:
257 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
258 		};
259 
260 		class OOO_DLLPUBLIC_FILE OOp_ISNULL : public OBoolOperator
261 		{
262 		public:
263 			TYPEINFO();
264 		public:
265 			virtual void Exec(OCodeStack&);
266 			virtual sal_uInt16 getRequestedOperands() const;
267 			virtual sal_Bool operate(const OOperand*, const OOperand* = NULL) const;
268 		};
269 
270 		class OOO_DLLPUBLIC_FILE OOp_ISNOTNULL : public OOp_ISNULL
271 		{
272 		public:
273 			TYPEINFO();
274 			virtual sal_Bool operate(const OOperand*, const OOperand* = NULL) const;
275 		};
276 
277 		class OOO_DLLPUBLIC_FILE OOp_LIKE : public OBoolOperator
278 		{
279 		public:
280 			TYPEINFO();
281 		protected:
282 			const sal_Unicode cEscape;
283 
284 		public:
OOp_LIKE(const sal_Unicode cEsc=L'\\0')285 			OOp_LIKE(const sal_Unicode cEsc = L'\0'):cEscape(cEsc){};
286 
287 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
288 		};
289 
290 		class OOp_NOTLIKE : public OOp_LIKE
291 		{
292 		public:
293 			TYPEINFO();
294 		public:
OOp_NOTLIKE(const sal_Unicode cEsc=L'\\0')295 			OOp_NOTLIKE(const sal_Unicode cEsc = L'\0'):OOp_LIKE(cEsc){};
296 
297 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
298 		};
299 
300 		class OOO_DLLPUBLIC_FILE OOp_COMPARE : public OBoolOperator
301 		{
302 			sal_Int32 aPredicateType;
303 
304 		public:
305 			TYPEINFO();
OOp_COMPARE(sal_Int32 aPType)306 			OOp_COMPARE(sal_Int32 aPType)
307 						 :aPredicateType(aPType) {}
308 
getPredicateType() const309 			inline sal_Int32 getPredicateType() const { return aPredicateType; }
310 			virtual sal_Bool operate(const OOperand*, const OOperand*) const;
311 		};
312 
313 		// numerische Operatoren
314 
315 		class ONumOperator : public OOperator
316 		{
317 		public:
318 			virtual void Exec(OCodeStack&);
319 
320 			TYPEINFO();
321 
322 		protected:
323 			virtual double operate(const double& fLeft,const double& fRight) const = 0;
324 		};
325 
326 		class OOp_ADD : public ONumOperator
327 		{
328 		protected:
329 			virtual double operate(const double& fLeft,const double& fRight) const;
330 		};
331 
332 		class OOp_SUB : public ONumOperator
333 		{
334 		protected:
335 			virtual double operate(const double& fLeft,const double& fRight) const;
336 		};
337 
338 		class OOp_MUL : public ONumOperator
339 		{
340 		protected:
341 			virtual double operate(const double& fLeft,const double& fRight) const;
342 		};
343 
344 		class OOp_DIV : public ONumOperator
345 		{
346 		protected:
347 			virtual double operate(const double& fLeft,const double& fRight) const;
348 		};
349 
isValid() const350 		inline sal_Bool	OOperand::isValid() const
351 		{
352 			return getValue().getDouble() != double(0.0);
353 		}
354 
355 		// operator
356 		class ONthOperator : public OOperator
357 		{
358 		public:
359 			virtual void Exec(OCodeStack&);
360 
361 			TYPEINFO();
362 
363 		protected:
364 			virtual ORowSetValue operate(const ::std::vector<ORowSetValue>& lhs) const = 0;
365 		};
366 
367 		class OBinaryOperator : public OOperator
368 		{
369 		public:
370 			virtual void Exec(OCodeStack&);
371 
372 			TYPEINFO();
373 
374 		protected:
375 			virtual ORowSetValue operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const = 0;
376 		};
377 
378 		class OUnaryOperator : public OOperator
379 		{
380 		public:
381 			virtual void Exec(OCodeStack&);
382 			virtual sal_uInt16 getRequestedOperands() const;
383 			virtual ORowSetValue operate(const ORowSetValue& lhs) const = 0;
384 
385 			TYPEINFO();
386 
387 		};
388 	}
389 }
390 
391 #endif // _CONNECTIVITY_FILE_FCODE_HXX_
392 
393