update dev300-m58
[ooovba.git] / connectivity / source / drivers / file / fcomp.cxx
blob81c4fc052ca75746e425e246802ea5a492487d82
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fcomp.cxx,v $
10 * $Revision: 1.30.30.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "file/fcomp.hxx"
34 #include <tools/debug.hxx>
35 #include "TConnection.hxx"
36 #include "connectivity/sqlparse.hxx"
37 #include "file/fanalyzer.hxx"
38 #include <com/sun/star/sdbc/XColumnLocate.hpp>
39 #include <com/sun/star/util/DateTime.hpp>
40 #include <com/sun/star/util/Date.hpp>
41 #include <com/sun/star/util/Time.hpp>
42 #include "connectivity/dbexception.hxx"
43 #include "connectivity/dbconversion.hxx"
44 #include <com/sun/star/sdb/SQLFilterOperator.hpp>
45 #include "resource/file_res.hrc"
46 #include "file/FStringFunctions.hxx"
47 #include "file/FDateFunctions.hxx"
48 #include "file/FNumericFunctions.hxx"
49 #include "file/FConnection.hxx"
50 #include <com/sun/star/sdb/SQLFilterOperator.hpp>
52 using namespace connectivity;
53 using namespace connectivity::file;
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::sdbc;
56 using namespace com::sun::star::sdb;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::util;
60 DBG_NAME(OPredicateCompiler)
61 //------------------------------------------------------------------
62 OPredicateCompiler::OPredicateCompiler(OSQLAnalyzer* pAnalyzer)//,OCursor& rCurs)
63 // : m_rCursor(rCurs)
64 : m_pAnalyzer(pAnalyzer)
65 , m_nParamCounter(0)
66 , m_bORCondition(FALSE)
68 DBG_CTOR(OPredicateCompiler,NULL);
71 //------------------------------------------------------------------
72 OPredicateCompiler::~OPredicateCompiler()
74 Clean();
75 DBG_DTOR(OPredicateCompiler,NULL);
77 // -----------------------------------------------------------------------------
78 void OPredicateCompiler::dispose()
80 Clean();
81 m_orgColumns = NULL;
82 m_xIndexes.clear();
84 //------------------------------------------------------------------
85 // inline OCursor& OPredicateCompiler::Cursor() const {return m_rCursor;}
86 //------------------------------------------------------------------
87 void OPredicateCompiler::start(OSQLParseNode* pSQLParseNode)
89 if (!pSQLParseNode)
90 return;
92 m_nParamCounter = 0;
93 // Parse Tree analysieren (je nach Statement-Typ)
94 // und Zeiger auf WHERE-Klausel setzen:
95 OSQLParseNode * pWhereClause = NULL;
96 OSQLParseNode * pOrderbyClause = NULL;
98 if (SQL_ISRULE(pSQLParseNode,select_statement))
100 DBG_ASSERT(pSQLParseNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
102 OSQLParseNode * pTableExp = pSQLParseNode->getChild(3);
103 DBG_ASSERT(pTableExp != NULL,"Fehler im Parse Tree");
104 DBG_ASSERT(SQL_ISRULE(pTableExp,table_exp)," Fehler im Parse Tree");
105 DBG_ASSERT(pTableExp->count() == 5,"Fehler im Parse Tree");
107 // check that we don't use anything other than count(*) as function
108 OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
109 if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
111 for (sal_uInt32 i = 0; i < pSelection->count(); i++)
113 OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
114 if ( SQL_ISRULE(pColumnRef,general_set_fct) && pColumnRef->count() != 4 )
116 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_COMPLEX_COUNT,NULL);
122 pWhereClause = pTableExp->getChild(1);
123 pOrderbyClause = pTableExp->getChild(4);
125 else if (SQL_ISRULE(pSQLParseNode,update_statement_searched))
127 DBG_ASSERT(pSQLParseNode->count() == 5,"OFILECursor: Fehler im Parse Tree");
128 pWhereClause = pSQLParseNode->getChild(4);
130 else if (SQL_ISRULE(pSQLParseNode,delete_statement_searched))
132 DBG_ASSERT(pSQLParseNode->count() == 4,"Fehler im Parse Tree");
133 pWhereClause = pSQLParseNode->getChild(3);
135 else
136 // Anderes Statement. Keine Selektionskriterien.
137 return;
139 if (SQL_ISRULE(pWhereClause,where_clause))
141 // Wenn es aber eine where_clause ist, dann darf sie nicht leer sein:
142 DBG_ASSERT(pWhereClause->count() == 2,"OFILECursor: Fehler im Parse Tree");
144 OSQLParseNode * pComparisonPredicate = pWhereClause->getChild(1);
145 DBG_ASSERT(pComparisonPredicate != NULL,"OFILECursor: Fehler im Parse Tree");
147 execute( pComparisonPredicate );
149 else
151 // Die Where Clause ist meistens optional, d. h. es koennte sich auch
152 // um "optional_where_clause" handeln.
153 DBG_ASSERT(SQL_ISRULE(pWhereClause,opt_where_clause),"OPredicateCompiler: Fehler im Parse Tree");
157 //------------------------------------------------------------------
158 OOperand* OPredicateCompiler::execute(OSQLParseNode* pPredicateNode)
160 OOperand* pOperand = NULL;
161 if (pPredicateNode->count() == 3 && // Ausdruck is geklammert
162 SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"(") &&
163 SQL_ISPUNCTUATION(pPredicateNode->getChild(2),")"))
165 execute(pPredicateNode->getChild(1));
167 else if ((SQL_ISRULE(pPredicateNode,search_condition) || (SQL_ISRULE(pPredicateNode,boolean_term)))
168 && // AND/OR-Verknuepfung:
169 pPredicateNode->count() == 3)
171 execute(pPredicateNode->getChild(0)); // Bearbeiten des linken Zweigs
172 execute(pPredicateNode->getChild(2)); // Bearbeiten des rechten Zweigs
174 if (SQL_ISTOKEN(pPredicateNode->getChild(1),OR)) // OR-Operator
176 m_aCodeList.push_back(new OOp_OR());
177 m_bORCondition = sal_True;
179 else if (SQL_ISTOKEN(pPredicateNode->getChild(1),AND)) // AND-Operator
180 m_aCodeList.push_back(new OOp_AND());
181 else
183 DBG_ERROR("OPredicateCompiler: Fehler im Parse Tree");
186 else if (SQL_ISRULE(pPredicateNode,boolean_factor))
188 execute(pPredicateNode->getChild(1));
189 m_aCodeList.push_back(new OOp_NOT());
191 else if (SQL_ISRULE(pPredicateNode,comparison_predicate))
193 execute_COMPARE(pPredicateNode);
195 else if (SQL_ISRULE(pPredicateNode,like_predicate))
197 execute_LIKE(pPredicateNode);
199 else if (SQL_ISRULE(pPredicateNode,between_predicate))
201 execute_BETWEEN(pPredicateNode);
203 else if (SQL_ISRULE(pPredicateNode,test_for_null))
205 execute_ISNULL(pPredicateNode);
207 else if(SQL_ISRULE(pPredicateNode,num_value_exp))
209 execute(pPredicateNode->getChild(0)); // Bearbeiten des linken Zweigs
210 execute(pPredicateNode->getChild(2)); // Bearbeiten des rechten Zweigs
211 if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"+"))
213 m_aCodeList.push_back(new OOp_ADD());
215 else if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"-"))
216 m_aCodeList.push_back(new OOp_SUB());
217 else
219 DBG_ERROR("OPredicateCompiler: Fehler im Parse Tree num_value_exp");
222 else if(SQL_ISRULE(pPredicateNode,term))
224 execute(pPredicateNode->getChild(0)); // Bearbeiten des linken Zweigs
225 execute(pPredicateNode->getChild(2)); // Bearbeiten des rechten Zweigs
226 if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"*"))
228 m_aCodeList.push_back(new OOp_MUL());
230 else if (SQL_ISPUNCTUATION(pPredicateNode->getChild(1),"/"))
231 m_aCodeList.push_back(new OOp_DIV());
232 else
234 DBG_ERROR("OPredicateCompiler: Fehler im Parse Tree num_value_exp");
237 else
238 pOperand = execute_Operand(pPredicateNode); // jetzt werden nur einfache Operanden verarbeitet
240 return pOperand;
243 //------------------------------------------------------------------
244 OOperand* OPredicateCompiler::execute_COMPARE(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
246 DBG_ASSERT(pPredicateNode->count() == 3,"OFILECursor: Fehler im Parse Tree");
248 if ( !(SQL_ISRULE(pPredicateNode->getChild(0),column_ref) ||
249 pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_STRING ||
250 pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_INTNUM ||
251 pPredicateNode->getChild(2)->getNodeType() == SQL_NODE_APPROXNUM ||
252 SQL_ISTOKEN(pPredicateNode->getChild(2),TRUE) ||
253 SQL_ISTOKEN(pPredicateNode->getChild(2),FALSE) ||
254 SQL_ISRULE(pPredicateNode->getChild(2),parameter) ||
255 // odbc date
256 SQL_ISRULE(pPredicateNode->getChild(2),set_fct_spec) ||
257 SQL_ISRULE(pPredicateNode->getChild(2),position_exp) ||
258 SQL_ISRULE(pPredicateNode->getChild(2),char_substring_fct) ||
259 // upper, lower etc.
260 SQL_ISRULE(pPredicateNode->getChild(2),fold)) )
262 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
263 return NULL;
266 sal_Int32 ePredicateType( SQLFilterOperator::EQUAL );
267 OSQLParseNode *pPrec = pPredicateNode->getChild(1);
269 if (pPrec->getNodeType() == SQL_NODE_EQUAL)
270 ePredicateType = SQLFilterOperator::EQUAL;
271 else if (pPrec->getNodeType() == SQL_NODE_NOTEQUAL)
272 ePredicateType = SQLFilterOperator::NOT_EQUAL;
273 else if (pPrec->getNodeType() == SQL_NODE_LESS)
274 ePredicateType = SQLFilterOperator::LESS;
275 else if (pPrec->getNodeType() == SQL_NODE_LESSEQ)
276 ePredicateType = SQLFilterOperator::LESS_EQUAL;
277 else if (pPrec->getNodeType() == SQL_NODE_GREATEQ)
278 ePredicateType = SQLFilterOperator::GREATER_EQUAL;
279 else if (pPrec->getNodeType() == SQL_NODE_GREAT)
280 ePredicateType = SQLFilterOperator::GREATER;
281 else
282 OSL_ENSURE( false, "OPredicateCompiler::execute_COMPARE: unexpected node type!" );
284 execute(pPredicateNode->getChild(0));
285 execute(pPredicateNode->getChild(2));
286 m_aCodeList.push_back( new OOp_COMPARE(ePredicateType) );
288 return NULL;
291 //------------------------------------------------------------------
292 OOperand* OPredicateCompiler::execute_LIKE(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
294 DBG_ASSERT(pPredicateNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
296 sal_Int32 ePredicateType;
297 sal_Unicode cEscape = L'\0';
298 if (pPredicateNode->count() == 5)
299 ePredicateType = SQLFilterOperator::NOT_LIKE;
300 else
301 ePredicateType = SQLFilterOperator::LIKE;
303 OSQLParseNode* pAtom = pPredicateNode->getChild(pPredicateNode->count()-2);
304 OSQLParseNode* pOptEscape = pPredicateNode->getChild(pPredicateNode->count()-1);
306 if (!(pAtom->getNodeType() == SQL_NODE_STRING || SQL_ISRULE(pAtom,parameter)))
308 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_LIKE_STRING,NULL);
310 if (pOptEscape->count() != 0)
312 if (pOptEscape->count() != 2)
314 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_LIKE_STRING,NULL);
316 OSQLParseNode *pEscNode = pOptEscape->getChild(1);
317 if (pEscNode->getNodeType() != SQL_NODE_STRING)
319 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_LIKE_STRING,NULL);
321 else
322 cEscape = pEscNode->getTokenValue().toChar();
325 execute(pPredicateNode->getChild(0));
326 execute(pAtom);
328 OBoolOperator* pOperator = (ePredicateType == SQLFilterOperator::LIKE)
329 ? new OOp_LIKE(cEscape)
330 : new OOp_NOTLIKE(cEscape);
331 m_aCodeList.push_back(pOperator);
333 return NULL;
335 //------------------------------------------------------------------
336 OOperand* OPredicateCompiler::execute_BETWEEN(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
338 DBG_ASSERT(pPredicateNode->count() == 6,"OFILECursor: Fehler im Parse Tree");
340 OSQLParseNode* pColumn = pPredicateNode->getChild(0);
341 OSQLParseNode* p1stValue = pPredicateNode->getChild(3);
342 OSQLParseNode* p2ndtValue = pPredicateNode->getChild(5);
344 if (
345 !(p1stValue->getNodeType() == SQL_NODE_STRING || SQL_ISRULE(p1stValue,parameter))
346 && !(p2ndtValue->getNodeType() == SQL_NODE_STRING || SQL_ISRULE(p2ndtValue,parameter))
349 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_INVALID_BETWEEN,NULL);
352 sal_Bool bNot = SQL_ISTOKEN(pPredicateNode->getChild(1),NOT);
354 OOperand* pColumnOp = execute(pColumn);
355 OOperand* pOb1 = execute(p1stValue);
356 OBoolOperator* pOperator = new OOp_COMPARE(bNot ? SQLFilterOperator::LESS_EQUAL : SQLFilterOperator::GREATER);
357 m_aCodeList.push_back(pOperator);
359 execute(pColumn);
360 OOperand* pOb2 = execute(p2ndtValue);
361 pOperator = new OOp_COMPARE(bNot ? SQLFilterOperator::GREATER_EQUAL : SQLFilterOperator::LESS);
362 m_aCodeList.push_back(pOperator);
364 if ( pColumnOp && pOb1 && pOb2 )
366 switch(pColumnOp->getDBType())
368 case DataType::CHAR:
369 case DataType::VARCHAR:
370 case DataType::LONGVARCHAR:
371 pOb1->setValue(pOb1->getValue().getString());
372 pOb2->setValue(pOb2->getValue().getString());
373 break;
374 case DataType::DECIMAL:
375 case DataType::NUMERIC:
376 pOb1->setValue((double)pOb1->getValue());
377 pOb2->setValue((double)pOb2->getValue());
378 break;
379 case DataType::FLOAT:
380 pOb1->setValue((float)pOb1->getValue());
381 pOb2->setValue((float)pOb2->getValue());
382 break;
383 case DataType::DOUBLE:
384 case DataType::REAL:
385 pOb1->setValue((double)pOb1->getValue());
386 pOb2->setValue((double)pOb2->getValue());
387 break;
388 case DataType::DATE:
389 pOb1->setValue((Date)pOb1->getValue());
390 pOb2->setValue((Date)pOb2->getValue());
391 break;
392 case DataType::TIME:
393 pOb1->setValue((Time)pOb1->getValue());
394 pOb2->setValue((Time)pOb2->getValue());
395 break;
396 case DataType::TIMESTAMP:
397 pOb1->setValue((DateTime)pOb1->getValue());
398 pOb2->setValue((DateTime)pOb2->getValue());
399 break;
405 OBoolOperator* pBoolOp = NULL;
406 if ( bNot )
407 pBoolOp = new OOp_OR();
408 else
409 pBoolOp = new OOp_AND();
410 m_aCodeList.push_back(pBoolOp);
412 return NULL;
414 //------------------------------------------------------------------
415 OOperand* OPredicateCompiler::execute_ISNULL(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
417 DBG_ASSERT(pPredicateNode->count() >= 3,"OFILECursor: Fehler im Parse Tree");
418 DBG_ASSERT(SQL_ISTOKEN(pPredicateNode->getChild(1),IS),"OFILECursor: Fehler im Parse Tree");
420 sal_Int32 ePredicateType;
421 if (SQL_ISTOKEN(pPredicateNode->getChild(2),NOT))
422 ePredicateType = SQLFilterOperator::NOT_SQLNULL;
423 else
424 ePredicateType = SQLFilterOperator::SQLNULL;
426 execute(pPredicateNode->getChild(0));
427 OBoolOperator* pOperator = (ePredicateType == SQLFilterOperator::SQLNULL) ?
428 new OOp_ISNULL() : new OOp_ISNOTNULL();
429 m_aCodeList.push_back(pOperator);
431 return NULL;
433 //------------------------------------------------------------------
434 OOperand* OPredicateCompiler::execute_Operand(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
436 OOperand* pOperand = NULL;
438 if (SQL_ISRULE(pPredicateNode,column_ref))
440 ::rtl::OUString aColumnName;
441 if (pPredicateNode->count() == 1)
443 aColumnName = pPredicateNode->getChild(0)->getTokenValue();
445 else if (pPredicateNode->count() == 3)
447 ::rtl::OUString aTableName = pPredicateNode->getChild(0)->getTokenValue();
448 if(SQL_ISRULE(pPredicateNode->getChild(2),column_val))
449 aColumnName = pPredicateNode->getChild(2)->getChild(0)->getTokenValue();
450 else
451 aColumnName = pPredicateNode->getChild(2)->getTokenValue();
454 if(!m_orgColumns->hasByName(aColumnName))
456 const ::rtl::OUString sError( m_pAnalyzer->getConnection()->getResources().getResourceStringWithSubstitution(
457 STR_INVALID_COLUMNNAME,
458 "$columnname$", aColumnName
459 ) );
460 ::dbtools::throwGenericSQLException( sError, NULL );
462 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xCol;
465 if (m_orgColumns->getByName(aColumnName) >>= xCol)
467 pOperand = m_pAnalyzer->createOperandAttr(Reference< XColumnLocate>(m_orgColumns,UNO_QUERY)->findColumn(aColumnName),xCol,m_xIndexes);
469 else
470 {// Column existiert nicht im Resultset
471 const ::rtl::OUString sError( m_pAnalyzer->getConnection()->getResources().getResourceStringWithSubstitution(
472 STR_INVALID_COLUMNNAME,
473 "$columnname$", aColumnName
474 ) );
475 ::dbtools::throwGenericSQLException( sError, NULL );
478 catch(Exception &)
480 OSL_ENSURE(0,"OPredicateCompiler::execute_Operand Exception");
483 else if (SQL_ISRULE(pPredicateNode,parameter))
485 pOperand = new OOperandParam(pPredicateNode, ++m_nParamCounter);
487 else if (pPredicateNode->getNodeType() == SQL_NODE_STRING ||
488 pPredicateNode->getNodeType() == SQL_NODE_INTNUM ||
489 pPredicateNode->getNodeType() == SQL_NODE_APPROXNUM ||
490 pPredicateNode->getNodeType() == SQL_NODE_NAME ||
491 SQL_ISTOKEN(pPredicateNode,TRUE) ||
492 SQL_ISTOKEN(pPredicateNode,FALSE) ||
493 SQL_ISRULE(pPredicateNode,parameter))
495 pOperand = new OOperandConst(*pPredicateNode, pPredicateNode->getTokenValue());
497 else if((pPredicateNode->count() == 2) &&
498 (SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"+") || SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"-")) &&
499 pPredicateNode->getChild(1)->getNodeType() == SQL_NODE_INTNUM)
500 { // falls -1 bzw. +1 vorhanden ist
501 ::rtl::OUString aValue(pPredicateNode->getChild(0)->getTokenValue());
502 aValue += pPredicateNode->getChild(1)->getTokenValue();
503 pOperand = new OOperandConst(*pPredicateNode->getChild(1), aValue);
505 else if( SQL_ISRULE(pPredicateNode,set_fct_spec) && SQL_ISPUNCTUATION(pPredicateNode->getChild(0),"{") )
507 const OSQLParseNode* pODBCNode = pPredicateNode->getChild(1);
508 const OSQLParseNode* pODBCNodeChild = pODBCNode->getChild(0);
510 // Odbc Date or time
511 if (pODBCNodeChild->getNodeType() == SQL_NODE_KEYWORD && (
512 SQL_ISTOKEN(pODBCNodeChild,D) ||
513 SQL_ISTOKEN(pODBCNodeChild,T) ||
514 SQL_ISTOKEN(pODBCNodeChild,TS) ))
516 ::rtl::OUString sDateTime = pODBCNode->getChild(1)->getTokenValue();
517 pOperand = new OOperandConst(*pODBCNode->getChild(1), sDateTime);
518 if(SQL_ISTOKEN(pODBCNodeChild,D))
520 pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(sDateTime)));
522 else if(SQL_ISTOKEN(pODBCNodeChild,T))
524 pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(sDateTime)));
526 else if(SQL_ISTOKEN(pODBCNodeChild,TS))
528 pOperand->setValue(::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDateTime(sDateTime)));
531 else
532 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
535 else if( SQL_ISRULE(pPredicateNode,fold) )
537 execute_Fold(pPredicateNode);
539 else if( SQL_ISRULE(pPredicateNode,set_fct_spec)
540 || SQL_ISRULE(pPredicateNode,position_exp)
541 || SQL_ISRULE(pPredicateNode,char_substring_fct)
544 executeFunction(pPredicateNode);
546 else if( SQL_ISRULE(pPredicateNode,length_exp) )
548 executeFunction(pPredicateNode->getChild(0));
550 else
552 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,NULL);
554 if (pOperand)
555 m_aCodeList.push_back(pOperand);
556 return pOperand;
559 ////////////////////////////////////////////////////////////////////////////////////////
560 sal_Bool OPredicateInterpreter::evaluate(OCodeList& rCodeList)
562 static sal_Bool bResult;
564 OCodeList::iterator aIter = rCodeList.begin();
565 if (!(*aIter))
566 return sal_True; // kein Praedikat
568 for(;aIter != rCodeList.end();++aIter)
570 OOperand* pOperand = PTR_CAST(OOperand,(*aIter));
571 if (pOperand)
572 m_aStack.push(pOperand);
573 else
574 ((OOperator *)(*aIter))->Exec(m_aStack);
577 OOperand* pOperand = m_aStack.top();
578 m_aStack.pop();
580 DBG_ASSERT(m_aStack.size() == 0, "StackFehler");
581 DBG_ASSERT(pOperand, "StackFehler");
583 bResult = pOperand->isValid();
584 if (IS_TYPE(OOperandResult,pOperand))
585 delete pOperand;
586 return bResult;
588 // -----------------------------------------------------------------------------
589 void OPredicateInterpreter::evaluateSelection(OCodeList& rCodeList,ORowSetValueDecoratorRef& _rVal)
591 OCodeList::iterator aIter = rCodeList.begin();
592 if (!(*aIter))
593 return ; // kein Praedikat
595 for(;aIter != rCodeList.end();++aIter)
597 OOperand* pOperand = PTR_CAST(OOperand,(*aIter));
598 if (pOperand)
599 m_aStack.push(pOperand);
600 else
601 ((OOperator *)(*aIter))->Exec(m_aStack);
604 OOperand* pOperand = m_aStack.top();
605 m_aStack.pop();
607 DBG_ASSERT(m_aStack.size() == 0, "StackFehler");
608 DBG_ASSERT(pOperand, "StackFehler");
610 (*_rVal) = pOperand->getValue();
611 if (IS_TYPE(OOperandResult,pOperand))
612 delete pOperand;
614 // -----------------------------------------------------------------------------
615 OOperand* OPredicateCompiler::execute_Fold(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
617 DBG_ASSERT(pPredicateNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
619 sal_Bool bUpper = SQL_ISTOKEN(pPredicateNode->getChild(0),UPPER);
621 execute(pPredicateNode->getChild(2));
622 OOperator* pOperator = NULL;
623 if ( bUpper )
624 pOperator = new OOp_Upper();
625 else
626 pOperator = new OOp_Lower();
628 m_aCodeList.push_back(pOperator);
629 return NULL;
631 // -----------------------------------------------------------------------------
632 OOperand* OPredicateCompiler::executeFunction(OSQLParseNode* pPredicateNode) throw(SQLException, RuntimeException)
634 OOperator* pOperator = NULL;
636 OSL_ENSURE(pPredicateNode->getChild(0)->isToken(),"The first one must be the name of the function!");
637 sal_Int32 nTokenId = pPredicateNode->getChild(0)->getTokenID();
638 switch ( nTokenId )
640 case SQL_TOKEN_CHAR_LENGTH:
641 case SQL_TOKEN_CHARACTER_LENGTH:
642 case SQL_TOKEN_LENGTH:
643 case SQL_TOKEN_OCTET_LENGTH:
644 case SQL_TOKEN_ASCII:
645 case SQL_TOKEN_LCASE:
646 case SQL_TOKEN_LTRIM:
647 case SQL_TOKEN_RTRIM:
648 case SQL_TOKEN_SPACE:
649 case SQL_TOKEN_UCASE:
650 case SQL_TOKEN_ABS:
651 case SQL_TOKEN_ACOS:
652 case SQL_TOKEN_ASIN:
653 case SQL_TOKEN_ATAN:
654 case SQL_TOKEN_CEILING:
655 case SQL_TOKEN_COS:
656 case SQL_TOKEN_DEGREES:
657 case SQL_TOKEN_EXP:
658 case SQL_TOKEN_FLOOR:
659 case SQL_TOKEN_LOG10:
660 case SQL_TOKEN_LN:
661 case SQL_TOKEN_RADIANS:
662 case SQL_TOKEN_SIGN:
663 case SQL_TOKEN_SIN:
664 case SQL_TOKEN_SQRT:
665 case SQL_TOKEN_TAN:
666 case SQL_TOKEN_DAYNAME:
667 case SQL_TOKEN_DAYOFMONTH:
668 case SQL_TOKEN_DAYOFWEEK:
669 case SQL_TOKEN_DAYOFYEAR:
670 case SQL_TOKEN_HOUR:
671 case SQL_TOKEN_MINUTE:
672 case SQL_TOKEN_MONTH:
673 case SQL_TOKEN_MONTHNAME:
674 case SQL_TOKEN_QUARTER:
675 case SQL_TOKEN_SECOND:
676 case SQL_TOKEN_YEAR:
678 execute(pPredicateNode->getChild(2));
680 switch( nTokenId )
682 case SQL_TOKEN_CHAR_LENGTH:
683 case SQL_TOKEN_CHARACTER_LENGTH:
684 case SQL_TOKEN_LENGTH:
685 case SQL_TOKEN_OCTET_LENGTH:
686 pOperator = new OOp_CharLength();
687 break;
688 case SQL_TOKEN_ASCII:
689 pOperator = new OOp_Ascii();
690 break;
691 case SQL_TOKEN_LCASE:
692 pOperator = new OOp_Lower();
693 break;
695 case SQL_TOKEN_LTRIM:
696 pOperator = new OOp_LTrim();
697 break;
698 case SQL_TOKEN_RTRIM:
699 pOperator = new OOp_RTrim();
700 break;
701 case SQL_TOKEN_SPACE:
702 pOperator = new OOp_Space();
703 break;
704 case SQL_TOKEN_UCASE:
705 pOperator = new OOp_Upper();
706 break;
707 case SQL_TOKEN_ABS:
708 pOperator = new OOp_Abs();
709 break;
710 case SQL_TOKEN_ACOS:
711 pOperator = new OOp_ACos();
712 break;
713 case SQL_TOKEN_ASIN:
714 pOperator = new OOp_ASin();
715 break;
716 case SQL_TOKEN_ATAN:
717 pOperator = new OOp_ATan();
718 break;
719 case SQL_TOKEN_CEILING:
720 pOperator = new OOp_Ceiling();
721 break;
722 case SQL_TOKEN_COS:
723 pOperator = new OOp_Cos();
724 break;
725 case SQL_TOKEN_DEGREES:
726 pOperator = new OOp_Degrees();
727 break;
728 case SQL_TOKEN_EXP:
729 pOperator = new OOp_Exp();
730 break;
731 case SQL_TOKEN_FLOOR:
732 pOperator = new OOp_Floor();
733 break;
734 case SQL_TOKEN_LOG10:
735 pOperator = new OOp_Log10();
736 break;
737 case SQL_TOKEN_LN:
738 pOperator = new OOp_Ln();
739 break;
740 case SQL_TOKEN_RADIANS:
741 pOperator = new OOp_Radians();
742 break;
743 case SQL_TOKEN_SIGN:
744 pOperator = new OOp_Sign();
745 break;
746 case SQL_TOKEN_SIN:
747 pOperator = new OOp_Sin();
748 break;
749 case SQL_TOKEN_SQRT:
750 pOperator = new OOp_Sqrt();
751 break;
752 case SQL_TOKEN_TAN:
753 pOperator = new OOp_Tan();
754 break;
755 case SQL_TOKEN_DAYOFWEEK:
756 pOperator = new OOp_DayOfWeek();
757 break;
758 case SQL_TOKEN_DAYOFMONTH:
759 pOperator = new OOp_DayOfMonth();
760 break;
761 case SQL_TOKEN_DAYOFYEAR:
762 pOperator = new OOp_DayOfYear();
763 break;
764 case SQL_TOKEN_MONTH:
765 pOperator = new OOp_Month();
766 break;
767 case SQL_TOKEN_DAYNAME:
768 pOperator = new OOp_DayName();
769 break;
770 case SQL_TOKEN_MONTHNAME:
771 pOperator = new OOp_MonthName();
772 break;
773 case SQL_TOKEN_QUARTER:
774 pOperator = new OOp_Quarter();
775 break;
776 case SQL_TOKEN_YEAR:
777 pOperator = new OOp_Year();
778 break;
779 case SQL_TOKEN_HOUR:
780 pOperator = new OOp_Hour();
781 break;
782 case SQL_TOKEN_MINUTE:
783 pOperator = new OOp_Minute();
784 break;
785 case SQL_TOKEN_SECOND:
786 pOperator = new OOp_Second();
787 break;
788 default:
789 OSL_ENSURE(0,"Error in switch!");
791 break;
792 case SQL_TOKEN_CHAR:
793 case SQL_TOKEN_CONCAT:
794 case SQL_TOKEN_INSERT:
795 case SQL_TOKEN_LEFT:
796 case SQL_TOKEN_LOCATE:
797 case SQL_TOKEN_LOCATE_2:
798 case SQL_TOKEN_REPEAT:
799 case SQL_TOKEN_REPLACE:
800 case SQL_TOKEN_RIGHT:
801 case SQL_TOKEN_MOD:
802 case SQL_TOKEN_ROUND:
803 case SQL_TOKEN_LOGF:
804 case SQL_TOKEN_LOG:
805 case SQL_TOKEN_POWER:
806 case SQL_TOKEN_ATAN2:
807 case SQL_TOKEN_PI:
808 case SQL_TOKEN_CURDATE:
809 case SQL_TOKEN_CURTIME:
810 case SQL_TOKEN_NOW:
811 case SQL_TOKEN_WEEK:
813 m_aCodeList.push_back(new OStopOperand);
814 OSQLParseNode* pList = pPredicateNode->getChild(2);
815 for (sal_uInt32 i=0; i < pList->count(); ++i)
816 execute(pList->getChild(i));
818 switch( nTokenId )
820 case SQL_TOKEN_CHAR:
821 pOperator = new OOp_Char();
822 break;
823 case SQL_TOKEN_CONCAT:
824 pOperator = new OOp_Concat();
825 break;
826 case SQL_TOKEN_INSERT:
827 pOperator = new OOp_Insert();
828 break;
829 case SQL_TOKEN_LEFT:
830 pOperator = new OOp_Left();
831 break;
832 case SQL_TOKEN_LOCATE:
833 case SQL_TOKEN_LOCATE_2:
834 pOperator = new OOp_Locate();
835 break;
836 case SQL_TOKEN_REPEAT:
837 pOperator = new OOp_Repeat();
838 break;
839 case SQL_TOKEN_REPLACE:
840 pOperator = new OOp_Replace();
841 break;
842 case SQL_TOKEN_RIGHT:
843 pOperator = new OOp_Right();
844 break;
845 case SQL_TOKEN_MOD:
846 pOperator = new OOp_Mod();
847 break;
848 case SQL_TOKEN_ROUND:
849 pOperator = new OOp_Round();
850 break;
851 case SQL_TOKEN_LOGF:
852 case SQL_TOKEN_LOG:
853 pOperator = new OOp_Log();
854 break;
855 case SQL_TOKEN_POWER:
856 pOperator = new OOp_Pow();
857 break;
858 case SQL_TOKEN_ATAN2:
859 pOperator = new OOp_ATan2();
860 break;
861 case SQL_TOKEN_PI:
862 pOperator = new OOp_Pi();
863 break;
864 case SQL_TOKEN_CURDATE:
865 pOperator = new OOp_CurDate();
866 break;
867 case SQL_TOKEN_CURTIME:
868 pOperator = new OOp_CurTime();
869 break;
870 case SQL_TOKEN_NOW:
871 pOperator = new OOp_Now();
872 break;
873 case SQL_TOKEN_WEEK:
874 pOperator = new OOp_Week();
875 break;
876 default:
877 OSL_ENSURE(0,"Error in switch!");
880 break;
882 case SQL_TOKEN_SUBSTRING:
883 m_aCodeList.push_back(new OStopOperand);
884 if ( pPredicateNode->count() == 4 ) //char_substring_fct
886 OSQLParseNode* pList = pPredicateNode->getChild(2);
887 for (sal_uInt32 i=0; i < pList->count(); ++i)
888 execute(pList->getChild(i));
890 else
892 execute(pPredicateNode->getChild(2));
893 execute(pPredicateNode->getChild(4));
894 execute(pPredicateNode->getChild(5)->getChild(1));
896 pOperator = new OOp_SubString();
897 break;
899 case SQL_TOKEN_POSITION:
900 m_aCodeList.push_back(new OStopOperand);
901 if ( pPredicateNode->count() == 4 ) //position_exp
903 OSQLParseNode* pList = pPredicateNode->getChild(2);
904 for (sal_uInt32 i=0; i < pList->count(); ++i)
905 execute(pList->getChild(i));
907 else
909 execute(pPredicateNode->getChild(2));
910 execute(pPredicateNode->getChild(4));
912 pOperator = new OOp_Locate();
913 break;
914 default:
915 m_pAnalyzer->getConnection()->throwGenericSQLException(STR_QUERY_FUNCTION_NOT_SUPPORTED,NULL);
918 m_aCodeList.push_back(pOperator);
919 return NULL;
921 // -----------------------------------------------------------------------------