Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "file/fcomp.hxx"
21 : #include <tools/debug.hxx>
22 : #include "TConnection.hxx"
23 : #include "connectivity/sqlparse.hxx"
24 : #include "file/fanalyzer.hxx"
25 : #include <com/sun/star/sdbc/XColumnLocate.hpp>
26 : #include <com/sun/star/util/DateTime.hpp>
27 : #include <com/sun/star/util/Date.hpp>
28 : #include <com/sun/star/util/Time.hpp>
29 : #include "connectivity/dbexception.hxx"
30 : #include "connectivity/dbconversion.hxx"
31 : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
32 : #include "resource/file_res.hrc"
33 : #include "file/FStringFunctions.hxx"
34 : #include "file/FDateFunctions.hxx"
35 : #include "file/FNumericFunctions.hxx"
36 : #include "file/FConnection.hxx"
37 :
38 : using namespace connectivity;
39 : using namespace connectivity::file;
40 : using namespace com::sun::star::uno;
41 : using namespace com::sun::star::sdbc;
42 : using namespace com::sun::star::sdb;
43 : using namespace ::com::sun::star::container;
44 : using namespace com::sun::star;
45 :
46 : DBG_NAME(OPredicateCompiler)
47 : //------------------------------------------------------------------
48 0 : OPredicateCompiler::OPredicateCompiler(OSQLAnalyzer* pAnalyzer)//,OCursor& rCurs)
49 : : m_pAnalyzer(pAnalyzer)
50 : , m_nParamCounter(0)
51 0 : , m_bORCondition(sal_False)
52 : {
53 : DBG_CTOR(OPredicateCompiler,NULL);
54 0 : }
55 :
56 : //------------------------------------------------------------------
57 0 : OPredicateCompiler::~OPredicateCompiler()
58 : {
59 0 : Clean();
60 : DBG_DTOR(OPredicateCompiler,NULL);
61 0 : }
62 : // -----------------------------------------------------------------------------
63 0 : void OPredicateCompiler::dispose()
64 : {
65 0 : Clean();
66 0 : m_orgColumns = NULL;
67 0 : m_xIndexes.clear();
68 0 : }
69 :
70 0 : void OPredicateCompiler::start(OSQLParseNode* pSQLParseNode)
71 : {
72 0 : if (!pSQLParseNode)
73 0 : return;
74 :
75 0 : m_nParamCounter = 0;
76 : // analyse Parse Tree (depending on Statement-type)
77 : // and set pointer on WHERE-clause:
78 0 : OSQLParseNode * pWhereClause = NULL;
79 0 : OSQLParseNode * pOrderbyClause = NULL;
80 :
81 0 : if (SQL_ISRULE(pSQLParseNode,select_statement))
82 : {
83 : DBG_ASSERT(pSQLParseNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
84 :
85 0 : OSQLParseNode * pTableExp = pSQLParseNode->getChild(3);
86 : DBG_ASSERT(pTableExp != NULL,"Fehler im Parse Tree");
87 : DBG_ASSERT(SQL_ISRULE(pTableExp,table_exp)," Fehler im Parse Tree");
88 : DBG_ASSERT(pTableExp->count() == TABLE_EXPRESSION_CHILD_COUNT,"Fehler im Parse Tree");
89 :
90 : // check that we don't use anything other than count(*) as function
91 0 : OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
92 0 : if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
93 : {
94 0 : for (sal_uInt32 i = 0; i < pSelection->count(); i++)
95 : {
96 0 : OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
97 0 : if ( SQL_ISRULE(pColumnRef,general_set_fct) && pColumnRef->count() != 4 )
98 : {
99 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_COMPLEX_COUNT,NULL);
100 : }
101 : }
102 : }
103 :
104 :
105 0 : pWhereClause = pTableExp->getChild(1);
106 0 : pOrderbyClause = pTableExp->getChild(ORDER_BY_CHILD_POS);
107 : (void)pOrderbyClause;
108 : }
109 0 : else if (SQL_ISRULE(pSQLParseNode,update_statement_searched))
110 : {
111 : DBG_ASSERT(pSQLParseNode->count() == 5,"OFILECursor: Fehler im Parse Tree");
112 0 : pWhereClause = pSQLParseNode->getChild(4);
113 : }
114 0 : else if (SQL_ISRULE(pSQLParseNode,delete_statement_searched))
115 : {
116 : DBG_ASSERT(pSQLParseNode->count() == 4,"Fehler im Parse Tree");
117 0 : pWhereClause = pSQLParseNode->getChild(3);
118 : }
119 : else
120 : // Other Statement. no selection-criteria
121 0 : return;
122 :
123 0 : if (SQL_ISRULE(pWhereClause,where_clause))
124 : {
125 : // a where-clause is not allowed to be empty:
126 : DBG_ASSERT(pWhereClause->count() == 2,"OFILECursor: Fehler im Parse Tree");
127 :
128 0 : OSQLParseNode * pComparisonPredicate = pWhereClause->getChild(1);
129 : DBG_ASSERT(pComparisonPredicate != NULL,"OFILECursor: Fehler im Parse Tree");
130 :
131 0 : execute( pComparisonPredicate );
132 : }
133 : else
134 : {
135 : // The where-clause is optionally in the majority of cases, i.e. it might be an "optional-where-clause".
136 : DBG_ASSERT(SQL_ISRULE(pWhereClause,opt_where_clause),"OPredicateCompiler: Fehler im Parse Tree");
137 : }
138 : }
139 :
140 : //------------------------------------------------------------------
141 0 : OOperand* OPredicateCompiler::execute(OSQLParseNode* pPredicateNode)
142 : {
143 0 : OOperand* pOperand = NULL;
144 0 : if (pPredicateNode->count() == 3 && // Expression is bracketed
145 0 : SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"(") &&
146 0 : SQL_ISPUNCTUATION(pPredicateNode->getChild(2),")"))
147 : {
148 0 : execute(pPredicateNode->getChild(1));
149 : }
150 0 : else if ((SQL_ISRULE(pPredicateNode,search_condition) || (SQL_ISRULE(pPredicateNode,boolean_term)))
151 : && // AND/OR-linkage:
152 0 : pPredicateNode->count() == 3)
153 : {
154 0 : execute(pPredicateNode->getChild(0)); // process the left branch
155 0 : execute(pPredicateNode->getChild(2)); // process the right branch
156 :
157 0 : if (SQL_ISTOKEN(pPredicateNode->getChild(1),OR)) // OR-Operator
158 : {
159 0 : m_aCodeList.push_back(new OOp_OR());
160 0 : m_bORCondition = sal_True;
161 : }
162 0 : else if (SQL_ISTOKEN(pPredicateNode->getChild(1),AND)) // AND-Operator
163 0 : m_aCodeList.push_back(new OOp_AND());
164 : else
165 : {
166 : OSL_FAIL("OPredicateCompiler: Fehler im Parse Tree");
167 : }
168 : }
169 0 : else if (SQL_ISRULE(pPredicateNode,boolean_factor))
170 : {
171 0 : execute(pPredicateNode->getChild(1));
172 0 : m_aCodeList.push_back(new OOp_NOT());
173 : }
174 0 : else if (SQL_ISRULE(pPredicateNode,comparison_predicate))
175 : {
176 0 : execute_COMPARE(pPredicateNode);
177 : }
178 0 : else if (SQL_ISRULE(pPredicateNode,like_predicate))
179 : {
180 0 : execute_LIKE(pPredicateNode);
181 : }
182 0 : else if (SQL_ISRULE(pPredicateNode,between_predicate))
183 : {
184 0 : execute_BETWEEN(pPredicateNode);
185 : }
186 0 : else if (SQL_ISRULE(pPredicateNode,test_for_null))
187 : {
188 0 : execute_ISNULL(pPredicateNode);
189 : }
190 0 : else if(SQL_ISRULE(pPredicateNode,num_value_exp))
191 : {
192 0 : execute(pPredicateNode->getChild(0)); // process the left branch
193 0 : execute(pPredicateNode->getChild(2)); // process the right branch
194 0 : if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"+"))
195 : {
196 0 : m_aCodeList.push_back(new OOp_ADD());
197 : }
198 0 : else if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"-"))
199 0 : m_aCodeList.push_back(new OOp_SUB());
200 : else
201 : {
202 : OSL_FAIL("OPredicateCompiler: Fehler im Parse Tree num_value_exp");
203 : }
204 : }
205 0 : else if(SQL_ISRULE(pPredicateNode,term))
206 : {
207 0 : execute(pPredicateNode->getChild(0)); // process the left branch
208 0 : execute(pPredicateNode->getChild(2)); // process the right branch
209 0 : if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"*"))
210 : {
211 0 : m_aCodeList.push_back(new OOp_MUL());
212 : }
213 0 : else if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"/"))
214 0 : m_aCodeList.push_back(new OOp_DIV());
215 : else
216 : {
217 : OSL_FAIL("OPredicateCompiler: Fehler im Parse Tree num_value_exp");
218 : }
219 : }
220 : else
221 0 : pOperand = execute_Operand(pPredicateNode); // now only simple operands will be processed
222 :
223 0 : return pOperand;
224 : }
225 :
226 : //------------------------------------------------------------------
227 0 : OOperand* OPredicateCompiler::execute_COMPARE(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
228 : {
229 : DBG_ASSERT(pPredicateNode->count() == 3,"OFILECursor: Fehler im Parse Tree");
230 :
231 0 : if ( !(SQL_ISRULE(pPredicateNode->getChild(0),column_ref) ||
232 0 : pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_STRING ||
233 0 : pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_INTNUM ||
234 0 : pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_APPROXNUM ||
235 0 : SQL_ISTOKEN(pPredicateNode->getChild(2),TRUE) ||
236 0 : SQL_ISTOKEN(pPredicateNode->getChild(2),FALSE) ||
237 0 : SQL_ISRULE(pPredicateNode->getChild(2),parameter) ||
238 : // odbc date
239 0 : SQL_ISRULE(pPredicateNode->getChild(2),set_fct_spec) ||
240 0 : SQL_ISRULE(pPredicateNode->getChild(2),position_exp) ||
241 0 : SQL_ISRULE(pPredicateNode->getChild(2),char_substring_fct) ||
242 : // upper, lower etc.
243 0 : SQL_ISRULE(pPredicateNode->getChild(2),fold)) )
244 : {
245 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
246 0 : return NULL;
247 : }
248 :
249 0 : sal_Int32 ePredicateType( SQLFilterOperator::EQUAL );
250 0 : OSQLParseNode *pPrec = pPredicateNode->getChild(1);
251 :
252 0 : if (pPrec->getNodeType() == SQL_NODE_EQUAL)
253 0 : ePredicateType = SQLFilterOperator::EQUAL;
254 0 : else if (pPrec->getNodeType() == SQL_NODE_NOTEQUAL)
255 0 : ePredicateType = SQLFilterOperator::NOT_EQUAL;
256 0 : else if (pPrec->getNodeType() == SQL_NODE_LESS)
257 0 : ePredicateType = SQLFilterOperator::LESS;
258 0 : else if (pPrec->getNodeType() == SQL_NODE_LESSEQ)
259 0 : ePredicateType = SQLFilterOperator::LESS_EQUAL;
260 0 : else if (pPrec->getNodeType() == SQL_NODE_GREATEQ)
261 0 : ePredicateType = SQLFilterOperator::GREATER_EQUAL;
262 0 : else if (pPrec->getNodeType() == SQL_NODE_GREAT)
263 0 : ePredicateType = SQLFilterOperator::GREATER;
264 : else
265 : OSL_FAIL( "OPredicateCompiler::execute_COMPARE: unexpected node type!" );
266 :
267 0 : execute(pPredicateNode->getChild(0));
268 0 : execute(pPredicateNode->getChild(2));
269 0 : m_aCodeList.push_back( new OOp_COMPARE(ePredicateType) );
270 :
271 0 : return NULL;
272 : }
273 :
274 : //------------------------------------------------------------------
275 0 : OOperand* OPredicateCompiler::execute_LIKE(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
276 : {
277 : DBG_ASSERT(pPredicateNode->count() == 2,"OFILECursor: Fehler im Parse Tree");
278 0 : const OSQLParseNode* pPart2 = pPredicateNode->getChild(1);
279 :
280 0 : sal_Unicode cEscape = L'\0';
281 0 : const bool bNotLike = pPart2->getChild(0)->isToken();
282 :
283 0 : OSQLParseNode* pAtom = pPart2->getChild(pPart2->count()-2);
284 0 : OSQLParseNode* pOptEscape = pPart2->getChild(pPart2->count()-1);
285 :
286 0 : if (!(pAtom->getNodeType() == SQL_NODE_STRING ||
287 0 : SQL_ISRULE(pAtom,parameter) ||
288 : // odbc date
289 0 : SQL_ISRULE(pAtom,set_fct_spec) ||
290 0 : SQL_ISRULE(pAtom,position_exp) ||
291 0 : SQL_ISRULE(pAtom,char_substring_fct) ||
292 : // upper, lower etc.
293 0 : SQL_ISRULE(pAtom,fold)) )
294 : {
295 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
296 0 : return NULL;
297 : }
298 :
299 0 : if (pOptEscape->count() != 0)
300 : {
301 0 : if (pOptEscape->count() != 2)
302 : {
303 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_LIKE_STRING,NULL);
304 : }
305 0 : OSQLParseNode *pEscNode = pOptEscape->getChild(1);
306 0 : if (pEscNode->getNodeType() != SQL_NODE_STRING)
307 : {
308 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_LIKE_STRING,NULL);
309 : }
310 : else
311 0 : cEscape = pEscNode->getTokenValue().toChar();
312 : }
313 :
314 0 : execute(pPredicateNode->getChild(0));
315 0 : execute(pAtom);
316 :
317 : OBoolOperator* pOperator = bNotLike
318 0 : ? new OOp_NOTLIKE(cEscape)
319 0 : : new OOp_LIKE(cEscape);
320 0 : m_aCodeList.push_back(pOperator);
321 :
322 0 : return NULL;
323 : }
324 : //------------------------------------------------------------------
325 0 : OOperand* OPredicateCompiler::execute_BETWEEN(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
326 : {
327 : DBG_ASSERT(pPredicateNode->count() == 2,"OFILECursor: Fehler im Parse Tree");
328 :
329 0 : OSQLParseNode* pColumn = pPredicateNode->getChild(0);
330 0 : const OSQLParseNode* pPart2 = pPredicateNode->getChild(1);
331 0 : OSQLParseNode* p1stValue = pPart2->getChild(2);
332 0 : OSQLParseNode* p2ndtValue = pPart2->getChild(4);
333 :
334 0 : if (
335 0 : !(p1stValue->getNodeType() == SQL_NODE_STRING || SQL_ISRULE(p1stValue,parameter))
336 0 : && !(p2ndtValue->getNodeType() == SQL_NODE_STRING || SQL_ISRULE(p2ndtValue,parameter))
337 : )
338 : {
339 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_BETWEEN,NULL);
340 : }
341 :
342 0 : sal_Bool bNot = SQL_ISTOKEN(pPart2->getChild(0),NOT);
343 :
344 0 : OOperand* pColumnOp = execute(pColumn);
345 0 : OOperand* pOb1 = execute(p1stValue);
346 0 : OBoolOperator* pOperator = new OOp_COMPARE(bNot ? SQLFilterOperator::LESS_EQUAL : SQLFilterOperator::GREATER);
347 0 : m_aCodeList.push_back(pOperator);
348 :
349 0 : execute(pColumn);
350 0 : OOperand* pOb2 = execute(p2ndtValue);
351 0 : pOperator = new OOp_COMPARE(bNot ? SQLFilterOperator::GREATER_EQUAL : SQLFilterOperator::LESS);
352 0 : m_aCodeList.push_back(pOperator);
353 :
354 0 : if ( pColumnOp && pOb1 && pOb2 )
355 : {
356 0 : switch(pColumnOp->getDBType())
357 : {
358 : case DataType::CHAR:
359 : case DataType::VARCHAR:
360 : case DataType::LONGVARCHAR:
361 0 : pOb1->setValue(pOb1->getValue().getString());
362 0 : pOb2->setValue(pOb2->getValue().getString());
363 0 : break;
364 : case DataType::DECIMAL:
365 : case DataType::NUMERIC:
366 0 : pOb1->setValue((double)pOb1->getValue());
367 0 : pOb2->setValue((double)pOb2->getValue());
368 0 : break;
369 : case DataType::FLOAT:
370 0 : pOb1->setValue((float)pOb1->getValue());
371 0 : pOb2->setValue((float)pOb2->getValue());
372 0 : break;
373 : case DataType::DOUBLE:
374 : case DataType::REAL:
375 0 : pOb1->setValue((double)pOb1->getValue());
376 0 : pOb2->setValue((double)pOb2->getValue());
377 0 : break;
378 : case DataType::DATE:
379 0 : pOb1->setValue((util::Date)pOb1->getValue());
380 0 : pOb2->setValue((util::Date)pOb2->getValue());
381 0 : break;
382 : case DataType::TIME:
383 0 : pOb1->setValue((util::Time)pOb1->getValue());
384 0 : pOb2->setValue((util::Time)pOb2->getValue());
385 0 : break;
386 : case DataType::TIMESTAMP:
387 0 : pOb1->setValue((util::DateTime)pOb1->getValue());
388 0 : pOb2->setValue((util::DateTime)pOb2->getValue());
389 0 : break;
390 : }
391 : }
392 :
393 :
394 :
395 0 : OBoolOperator* pBoolOp = NULL;
396 0 : if ( bNot )
397 0 : pBoolOp = new OOp_OR();
398 : else
399 0 : pBoolOp = new OOp_AND();
400 0 : m_aCodeList.push_back(pBoolOp);
401 :
402 0 : return NULL;
403 : }
404 : //------------------------------------------------------------------
405 0 : OOperand* OPredicateCompiler::execute_ISNULL(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
406 : {
407 : DBG_ASSERT(pPredicateNode->count() == 2,"OFILECursor: Fehler im Parse Tree");
408 0 : const OSQLParseNode* pPart2 = pPredicateNode->getChild(1);
409 : DBG_ASSERT(SQL_ISTOKEN(pPart2->getChild(0),IS),"OFILECursor: Fehler im Parse Tree");
410 :
411 : sal_Int32 ePredicateType;
412 0 : if (SQL_ISTOKEN(pPart2->getChild(1),NOT))
413 0 : ePredicateType = SQLFilterOperator::NOT_SQLNULL;
414 : else
415 0 : ePredicateType = SQLFilterOperator::SQLNULL;
416 :
417 0 : execute(pPredicateNode->getChild(0));
418 : OBoolOperator* pOperator = (ePredicateType == SQLFilterOperator::SQLNULL) ?
419 0 : new OOp_ISNULL() : new OOp_ISNOTNULL();
420 0 : m_aCodeList.push_back(pOperator);
421 :
422 0 : return NULL;
423 : }
424 : //------------------------------------------------------------------
425 0 : OOperand* OPredicateCompiler::execute_Operand(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
426 : {
427 0 : OOperand* pOperand = NULL;
428 :
429 0 : if (SQL_ISRULE(pPredicateNode,column_ref))
430 : {
431 0 : ::rtl::OUString aColumnName;
432 0 : if (pPredicateNode->count() == 1)
433 : {
434 0 : aColumnName = pPredicateNode->getChild(0)->getTokenValue();
435 : }
436 0 : else if (pPredicateNode->count() == 3)
437 : {
438 0 : ::rtl::OUString aTableName = pPredicateNode->getChild(0)->getTokenValue();
439 0 : if(SQL_ISRULE(pPredicateNode->getChild(2),column_val))
440 0 : aColumnName = pPredicateNode->getChild(2)->getChild(0)->getTokenValue();
441 : else
442 0 : aColumnName = pPredicateNode->getChild(2)->getTokenValue();
443 : }
444 :
445 0 : if(!m_orgColumns->hasByName(aColumnName))
446 : {
447 0 : const ::rtl::OUString sError( m_pAnalyzer->getConnection()->getResources().getResourceStringWithSubstitution(
448 : STR_INVALID_COLUMNNAME,
449 : "$columnname$", aColumnName
450 0 : ) );
451 0 : ::dbtools::throwGenericSQLException( sError, NULL );
452 : }
453 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xCol;
454 : try
455 : {
456 0 : if (m_orgColumns->getByName(aColumnName) >>= xCol)
457 : {
458 0 : pOperand = m_pAnalyzer->createOperandAttr(Reference< XColumnLocate>(m_orgColumns,UNO_QUERY)->findColumn(aColumnName),xCol,m_xIndexes);
459 : }
460 : else
461 : {// Column doesn't exist in the Result-set
462 0 : const ::rtl::OUString sError( m_pAnalyzer->getConnection()->getResources().getResourceStringWithSubstitution(
463 : STR_INVALID_COLUMNNAME,
464 : "$columnname$", aColumnName
465 0 : ) );
466 0 : ::dbtools::throwGenericSQLException( sError, NULL );
467 : }
468 : }
469 0 : catch(Exception &)
470 : {
471 : OSL_FAIL("OPredicateCompiler::execute_Operand Exception");
472 0 : }
473 : }
474 0 : else if (SQL_ISRULE(pPredicateNode,parameter))
475 : {
476 0 : pOperand = new OOperandParam(pPredicateNode, ++m_nParamCounter);
477 : }
478 0 : else if (pPredicateNode->getNodeType() == SQL_NODE_STRING ||
479 0 : pPredicateNode->getNodeType() == SQL_NODE_INTNUM ||
480 0 : pPredicateNode->getNodeType() == SQL_NODE_APPROXNUM ||
481 0 : pPredicateNode->getNodeType() == SQL_NODE_NAME ||
482 0 : SQL_ISTOKEN(pPredicateNode,TRUE) ||
483 0 : SQL_ISTOKEN(pPredicateNode,FALSE) ||
484 0 : SQL_ISRULE(pPredicateNode,parameter))
485 : {
486 0 : pOperand = new OOperandConst(*pPredicateNode, pPredicateNode->getTokenValue());
487 : }
488 0 : else if((pPredicateNode->count() == 2) &&
489 0 : (SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"+") || SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"-")) &&
490 0 : pPredicateNode->getChild(1)->getNodeType() == SQL_NODE_INTNUM)
491 : { // if -1 or +1 is there
492 0 : ::rtl::OUString aValue(pPredicateNode->getChild(0)->getTokenValue());
493 0 : aValue += pPredicateNode->getChild(1)->getTokenValue();
494 0 : pOperand = new OOperandConst(*pPredicateNode->getChild(1), aValue);
495 : }
496 0 : else if( SQL_ISRULE(pPredicateNode,set_fct_spec) && SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"{") )
497 : {
498 0 : const OSQLParseNode* pODBCNode = pPredicateNode->getChild(1);
499 0 : const OSQLParseNode* pODBCNodeChild = pODBCNode->getChild(0);
500 :
501 : // Odbc Date or time
502 0 : if (pODBCNodeChild->getNodeType() == SQL_NODE_KEYWORD && (
503 0 : SQL_ISTOKEN(pODBCNodeChild,D) ||
504 0 : SQL_ISTOKEN(pODBCNodeChild,T) ||
505 0 : SQL_ISTOKEN(pODBCNodeChild,TS) ))
506 : {
507 0 : ::rtl::OUString sDateTime = pODBCNode->getChild(1)->getTokenValue();
508 0 : pOperand = new OOperandConst(*pODBCNode->getChild(1), sDateTime);
509 0 : if(SQL_ISTOKEN(pODBCNodeChild,D))
510 : {
511 0 : pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(sDateTime)));
512 : }
513 0 : else if(SQL_ISTOKEN(pODBCNodeChild,T))
514 : {
515 0 : pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(sDateTime)));
516 : }
517 0 : else if(SQL_ISTOKEN(pODBCNodeChild,TS))
518 : {
519 0 : pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDateTime(sDateTime)));
520 0 : }
521 : }
522 : else
523 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
524 :
525 : }
526 0 : else if( SQL_ISRULE(pPredicateNode,fold) )
527 : {
528 0 : execute_Fold(pPredicateNode);
529 : }
530 0 : else if( SQL_ISRULE(pPredicateNode,set_fct_spec)
531 0 : || SQL_ISRULE(pPredicateNode,position_exp)
532 0 : || SQL_ISRULE(pPredicateNode,char_substring_fct)
533 : )
534 : {
535 0 : executeFunction(pPredicateNode);
536 : }
537 0 : else if( SQL_ISRULE(pPredicateNode,length_exp) )
538 : {
539 0 : executeFunction(pPredicateNode->getChild(0));
540 : }
541 : else
542 : {
543 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
544 : }
545 0 : if (pOperand)
546 0 : m_aCodeList.push_back(pOperand);
547 0 : return pOperand;
548 : }
549 :
550 : ////////////////////////////////////////////////////////////////////////////////////////
551 0 : sal_Bool OPredicateInterpreter::evaluate(OCodeList& rCodeList)
552 : {
553 : static sal_Bool bResult;
554 :
555 0 : OCodeList::iterator aIter = rCodeList.begin();
556 0 : if (!(*aIter))
557 0 : return sal_True; // no Predicate
558 :
559 0 : for(;aIter != rCodeList.end();++aIter)
560 : {
561 0 : OOperand* pOperand = PTR_CAST(OOperand,(*aIter));
562 0 : if (pOperand)
563 0 : m_aStack.push(pOperand);
564 : else
565 0 : ((OOperator *)(*aIter))->Exec(m_aStack);
566 : }
567 :
568 0 : OOperand* pOperand = m_aStack.top();
569 0 : m_aStack.pop();
570 :
571 : DBG_ASSERT(m_aStack.empty(), "StackFehler");
572 : DBG_ASSERT(pOperand, "StackFehler");
573 :
574 0 : bResult = pOperand->isValid();
575 0 : if (IS_TYPE(OOperandResult,pOperand))
576 0 : delete pOperand;
577 0 : return bResult;
578 : }
579 : // -----------------------------------------------------------------------------
580 0 : void OPredicateInterpreter::evaluateSelection(OCodeList& rCodeList,ORowSetValueDecoratorRef& _rVal)
581 : {
582 0 : OCodeList::iterator aIter = rCodeList.begin();
583 0 : if (!(*aIter))
584 0 : return ; // no Predicate
585 :
586 0 : for(;aIter != rCodeList.end();++aIter)
587 : {
588 0 : OOperand* pOperand = PTR_CAST(OOperand,(*aIter));
589 0 : if (pOperand)
590 0 : m_aStack.push(pOperand);
591 : else
592 0 : ((OOperator *)(*aIter))->Exec(m_aStack);
593 : }
594 :
595 0 : OOperand* pOperand = m_aStack.top();
596 0 : m_aStack.pop();
597 :
598 : DBG_ASSERT(m_aStack.empty(), "StackFehler");
599 : DBG_ASSERT(pOperand, "StackFehler");
600 :
601 0 : (*_rVal) = pOperand->getValue();
602 0 : if (IS_TYPE(OOperandResult,pOperand))
603 0 : delete pOperand;
604 : }
605 : // -----------------------------------------------------------------------------
606 0 : OOperand* OPredicateCompiler::execute_Fold(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
607 : {
608 : DBG_ASSERT(pPredicateNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
609 :
610 0 : sal_Bool bUpper = SQL_ISTOKEN(pPredicateNode->getChild(0),UPPER);
611 :
612 0 : execute(pPredicateNode->getChild(2));
613 0 : OOperator* pOperator = NULL;
614 0 : if ( bUpper )
615 0 : pOperator = new OOp_Upper();
616 : else
617 0 : pOperator = new OOp_Lower();
618 :
619 0 : m_aCodeList.push_back(pOperator);
620 0 : return NULL;
621 : }
622 : // -----------------------------------------------------------------------------
623 0 : OOperand* OPredicateCompiler::executeFunction(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
624 : {
625 0 : OOperator* pOperator = NULL;
626 :
627 : OSL_ENSURE(pPredicateNode->getChild(0)->isToken(),"The first one must be the name of the function!");
628 0 : sal_Int32 nTokenId = pPredicateNode->getChild(0)->getTokenID();
629 0 : switch ( nTokenId )
630 : {
631 : case SQL_TOKEN_CHAR_LENGTH:
632 : case SQL_TOKEN_LENGTH:
633 : case SQL_TOKEN_OCTET_LENGTH:
634 : case SQL_TOKEN_ASCII:
635 : case SQL_TOKEN_LCASE:
636 : case SQL_TOKEN_LTRIM:
637 : case SQL_TOKEN_RTRIM:
638 : case SQL_TOKEN_SPACE:
639 : case SQL_TOKEN_UCASE:
640 : case SQL_TOKEN_ABS:
641 : case SQL_TOKEN_ACOS:
642 : case SQL_TOKEN_ASIN:
643 : case SQL_TOKEN_ATAN:
644 : case SQL_TOKEN_CEILING:
645 : case SQL_TOKEN_COS:
646 : case SQL_TOKEN_DEGREES:
647 : case SQL_TOKEN_EXP:
648 : case SQL_TOKEN_FLOOR:
649 : case SQL_TOKEN_LOG10:
650 : case SQL_TOKEN_LN:
651 : case SQL_TOKEN_RADIANS:
652 : case SQL_TOKEN_SIGN:
653 : case SQL_TOKEN_SIN:
654 : case SQL_TOKEN_SQRT:
655 : case SQL_TOKEN_TAN:
656 : case SQL_TOKEN_DAYNAME:
657 : case SQL_TOKEN_DAYOFMONTH:
658 : case SQL_TOKEN_DAYOFWEEK:
659 : case SQL_TOKEN_DAYOFYEAR:
660 : case SQL_TOKEN_HOUR:
661 : case SQL_TOKEN_MINUTE:
662 : case SQL_TOKEN_MONTH:
663 : case SQL_TOKEN_MONTHNAME:
664 : case SQL_TOKEN_QUARTER:
665 : case SQL_TOKEN_SECOND:
666 : case SQL_TOKEN_YEAR:
667 :
668 0 : execute(pPredicateNode->getChild(2));
669 :
670 0 : switch( nTokenId )
671 : {
672 : case SQL_TOKEN_CHAR_LENGTH:
673 : case SQL_TOKEN_LENGTH:
674 : case SQL_TOKEN_OCTET_LENGTH:
675 0 : pOperator = new OOp_CharLength();
676 0 : break;
677 : case SQL_TOKEN_ASCII:
678 0 : pOperator = new OOp_Ascii();
679 0 : break;
680 : case SQL_TOKEN_LCASE:
681 0 : pOperator = new OOp_Lower();
682 0 : break;
683 :
684 : case SQL_TOKEN_LTRIM:
685 0 : pOperator = new OOp_LTrim();
686 0 : break;
687 : case SQL_TOKEN_RTRIM:
688 0 : pOperator = new OOp_RTrim();
689 0 : break;
690 : case SQL_TOKEN_SPACE:
691 0 : pOperator = new OOp_Space();
692 0 : break;
693 : case SQL_TOKEN_UCASE:
694 0 : pOperator = new OOp_Upper();
695 0 : break;
696 : case SQL_TOKEN_ABS:
697 0 : pOperator = new OOp_Abs();
698 0 : break;
699 : case SQL_TOKEN_ACOS:
700 0 : pOperator = new OOp_ACos();
701 0 : break;
702 : case SQL_TOKEN_ASIN:
703 0 : pOperator = new OOp_ASin();
704 0 : break;
705 : case SQL_TOKEN_ATAN:
706 0 : pOperator = new OOp_ATan();
707 0 : break;
708 : case SQL_TOKEN_CEILING:
709 0 : pOperator = new OOp_Ceiling();
710 0 : break;
711 : case SQL_TOKEN_COS:
712 0 : pOperator = new OOp_Cos();
713 0 : break;
714 : case SQL_TOKEN_DEGREES:
715 0 : pOperator = new OOp_Degrees();
716 0 : break;
717 : case SQL_TOKEN_EXP:
718 0 : pOperator = new OOp_Exp();
719 0 : break;
720 : case SQL_TOKEN_FLOOR:
721 0 : pOperator = new OOp_Floor();
722 0 : break;
723 : case SQL_TOKEN_LOG10:
724 0 : pOperator = new OOp_Log10();
725 0 : break;
726 : case SQL_TOKEN_LN:
727 0 : pOperator = new OOp_Ln();
728 0 : break;
729 : case SQL_TOKEN_RADIANS:
730 0 : pOperator = new OOp_Radians();
731 0 : break;
732 : case SQL_TOKEN_SIGN:
733 0 : pOperator = new OOp_Sign();
734 0 : break;
735 : case SQL_TOKEN_SIN:
736 0 : pOperator = new OOp_Sin();
737 0 : break;
738 : case SQL_TOKEN_SQRT:
739 0 : pOperator = new OOp_Sqrt();
740 0 : break;
741 : case SQL_TOKEN_TAN:
742 0 : pOperator = new OOp_Tan();
743 0 : break;
744 : case SQL_TOKEN_DAYOFWEEK:
745 0 : pOperator = new OOp_DayOfWeek();
746 0 : break;
747 : case SQL_TOKEN_DAYOFMONTH:
748 0 : pOperator = new OOp_DayOfMonth();
749 0 : break;
750 : case SQL_TOKEN_DAYOFYEAR:
751 0 : pOperator = new OOp_DayOfYear();
752 0 : break;
753 : case SQL_TOKEN_MONTH:
754 0 : pOperator = new OOp_Month();
755 0 : break;
756 : case SQL_TOKEN_DAYNAME:
757 0 : pOperator = new OOp_DayName();
758 0 : break;
759 : case SQL_TOKEN_MONTHNAME:
760 0 : pOperator = new OOp_MonthName();
761 0 : break;
762 : case SQL_TOKEN_QUARTER:
763 0 : pOperator = new OOp_Quarter();
764 0 : break;
765 : case SQL_TOKEN_YEAR:
766 0 : pOperator = new OOp_Year();
767 0 : break;
768 : case SQL_TOKEN_HOUR:
769 0 : pOperator = new OOp_Hour();
770 0 : break;
771 : case SQL_TOKEN_MINUTE:
772 0 : pOperator = new OOp_Minute();
773 0 : break;
774 : case SQL_TOKEN_SECOND:
775 0 : pOperator = new OOp_Second();
776 0 : break;
777 : default:
778 : OSL_FAIL("Error in switch!");
779 : }
780 0 : break;
781 : case SQL_TOKEN_CHAR:
782 : case SQL_TOKEN_CONCAT:
783 : case SQL_TOKEN_INSERT:
784 : case SQL_TOKEN_LEFT:
785 : case SQL_TOKEN_LOCATE:
786 : case SQL_TOKEN_LOCATE_2:
787 : case SQL_TOKEN_REPEAT:
788 : case SQL_TOKEN_REPLACE:
789 : case SQL_TOKEN_RIGHT:
790 : case SQL_TOKEN_MOD:
791 : case SQL_TOKEN_ROUND:
792 : case SQL_TOKEN_LOGF:
793 : case SQL_TOKEN_LOG:
794 : case SQL_TOKEN_POWER:
795 : case SQL_TOKEN_ATAN2:
796 : case SQL_TOKEN_PI:
797 : case SQL_TOKEN_CURDATE:
798 : case SQL_TOKEN_CURTIME:
799 : case SQL_TOKEN_NOW:
800 : case SQL_TOKEN_WEEK:
801 : {
802 0 : m_aCodeList.push_back(new OStopOperand);
803 0 : OSQLParseNode* pList = pPredicateNode->getChild(2);
804 0 : for (sal_uInt32 i=0; i < pList->count(); ++i)
805 0 : execute(pList->getChild(i));
806 :
807 0 : switch( nTokenId )
808 : {
809 : case SQL_TOKEN_CHAR:
810 0 : pOperator = new OOp_Char();
811 0 : break;
812 : case SQL_TOKEN_CONCAT:
813 0 : pOperator = new OOp_Concat();
814 0 : break;
815 : case SQL_TOKEN_INSERT:
816 0 : pOperator = new OOp_Insert();
817 0 : break;
818 : case SQL_TOKEN_LEFT:
819 0 : pOperator = new OOp_Left();
820 0 : break;
821 : case SQL_TOKEN_LOCATE:
822 : case SQL_TOKEN_LOCATE_2:
823 0 : pOperator = new OOp_Locate();
824 0 : break;
825 : case SQL_TOKEN_REPEAT:
826 0 : pOperator = new OOp_Repeat();
827 0 : break;
828 : case SQL_TOKEN_REPLACE:
829 0 : pOperator = new OOp_Replace();
830 0 : break;
831 : case SQL_TOKEN_RIGHT:
832 0 : pOperator = new OOp_Right();
833 0 : break;
834 : case SQL_TOKEN_MOD:
835 0 : pOperator = new OOp_Mod();
836 0 : break;
837 : case SQL_TOKEN_ROUND:
838 0 : pOperator = new OOp_Round();
839 0 : break;
840 : case SQL_TOKEN_LOGF:
841 : case SQL_TOKEN_LOG:
842 0 : pOperator = new OOp_Log();
843 0 : break;
844 : case SQL_TOKEN_POWER:
845 0 : pOperator = new OOp_Pow();
846 0 : break;
847 : case SQL_TOKEN_ATAN2:
848 0 : pOperator = new OOp_ATan2();
849 0 : break;
850 : case SQL_TOKEN_PI:
851 0 : pOperator = new OOp_Pi();
852 0 : break;
853 : case SQL_TOKEN_CURDATE:
854 0 : pOperator = new OOp_CurDate();
855 0 : break;
856 : case SQL_TOKEN_CURTIME:
857 0 : pOperator = new OOp_CurTime();
858 0 : break;
859 : case SQL_TOKEN_NOW:
860 0 : pOperator = new OOp_Now();
861 0 : break;
862 : case SQL_TOKEN_WEEK:
863 0 : pOperator = new OOp_Week();
864 0 : break;
865 : default:
866 : OSL_FAIL("Error in switch!");
867 : }
868 : }
869 0 : break;
870 :
871 : case SQL_TOKEN_SUBSTRING:
872 0 : m_aCodeList.push_back(new OStopOperand);
873 0 : if ( pPredicateNode->count() == 4 ) //char_substring_fct
874 : {
875 0 : OSQLParseNode* pList = pPredicateNode->getChild(2);
876 0 : for (sal_uInt32 i=0; i < pList->count(); ++i)
877 0 : execute(pList->getChild(i));
878 : }
879 : else
880 : {
881 0 : execute(pPredicateNode->getChild(2));
882 0 : execute(pPredicateNode->getChild(4));
883 0 : execute(pPredicateNode->getChild(5)->getChild(1));
884 : }
885 0 : pOperator = new OOp_SubString();
886 0 : break;
887 :
888 : case SQL_TOKEN_POSITION:
889 0 : m_aCodeList.push_back(new OStopOperand);
890 0 : if ( pPredicateNode->count() == 4 ) //position_exp
891 : {
892 0 : OSQLParseNode* pList = pPredicateNode->getChild(2);
893 0 : for (sal_uInt32 i=0; i < pList->count(); ++i)
894 0 : execute(pList->getChild(i));
895 : }
896 : else
897 : {
898 0 : execute(pPredicateNode->getChild(2));
899 0 : execute(pPredicateNode->getChild(4));
900 : }
901 0 : pOperator = new OOp_Locate();
902 0 : break;
903 : default:
904 0 : m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_FUNCTION_NOT_SUPPORTED,NULL);
905 : }
906 :
907 0 : m_aCodeList.push_back(pOperator);
908 0 : return NULL;
909 : }
910 : // -----------------------------------------------------------------------------
911 :
912 :
913 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|