1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sqlnode.cxx,v $
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"
35 #include <connectivity/sqlnode.hxx>
36 #include <connectivity/sqlerror.hxx>
37 #include <internalnode.hxx>
39 #ifndef BISON_INCLUDED
40 #define BISON_INCLUDED
41 #include <sqlbison.hxx>
43 #include <connectivity/sqlparse.hxx>
44 #include <com/sun/star/lang/Locale.hpp>
45 #include <com/sun/star/util/XNumberFormatter.hpp>
46 #include <com/sun/star/util/XNumberFormatTypes.hpp>
47 #include <com/sun/star/i18n/NumberFormatIndex.hpp>
48 #include <com/sun/star/beans/XPropertySet.hpp>
49 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
50 #include <com/sun/star/sdbc/DataType.hpp>
51 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
52 #include <com/sun/star/sdb/ErrorCondition.hpp>
53 #include <com/sun/star/util/XNumberFormatter.hpp>
54 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
55 #include <com/sun/star/util/XNumberFormats.hpp>
56 #include <com/sun/star/util/NumberFormat.hpp>
57 #include <com/sun/star/util/XNumberFormatTypes.hpp>
58 #include <com/sun/star/lang/Locale.hpp>
59 #include <com/sun/star/i18n/KParseType.hpp>
60 #include <com/sun/star/i18n/KParseTokens.hpp>
61 #include "connectivity/dbconversion.hxx"
62 #include <com/sun/star/util/DateTime.hpp>
63 #include <com/sun/star/util/Time.hpp>
64 #include <com/sun/star/util/Date.hpp>
65 #include "TConnection.hxx"
66 #include "sqlscan.hxx"
67 #include <comphelper/numbers.hxx>
68 #include <comphelper/processfactory.hxx>
69 #include <comphelper/stl_types.hxx>
70 #include "connectivity/dbtools.hxx"
71 #include "connectivity/dbmetadata.hxx"
72 #include "connectivity/sqlerror.hxx"
73 #include <tools/diagnose_ex.h>
75 #include <boost/bind.hpp>
78 #include <rtl/logfile.hxx>
79 #include <rtl/ustrbuf.hxx>
81 using namespace ::com::sun::star::sdbc
;
82 using namespace ::com::sun::star::util
;
83 using namespace ::com::sun::star::beans
;
84 using namespace ::com::sun::star::sdb
;
85 using namespace ::com::sun::star::uno
;
86 using namespace ::com::sun::star::lang
;
87 using namespace ::com::sun::star::i18n
;
88 using namespace ::com::sun::star
;
89 using namespace ::osl
;
90 using namespace ::dbtools
;
91 using namespace ::comphelper
;
94 extern int SQLyyparse (void);
95 extern ::rtl::OUString
ConvertLikeToken(const ::connectivity::OSQLParseNode
* pTokenNode
, const ::connectivity::OSQLParseNode
* pEscapeNode
, sal_Bool bInternational
);
96 extern void setParser( ::connectivity::OSQLParser
* );
100 // -----------------------------------------------------------------------------
101 sal_Bool
lcl_saveConvertToNumber(const Reference
< XNumberFormatter
> & _xFormatter
,sal_Int32 _nKey
,const ::rtl::OUString
& _sValue
,double& _nrValue
)
103 sal_Bool bRet
= sal_False
;
106 _nrValue
= _xFormatter
->convertStringToNumber(_nKey
, _sValue
);
114 // -----------------------------------------------------------------------------
115 void replaceAndReset(connectivity::OSQLParseNode
*& _pResetNode
,connectivity::OSQLParseNode
* _pNewNode
)
117 _pResetNode
->getParent()->replace(_pResetNode
, _pNewNode
);
119 _pResetNode
= _pNewNode
;
121 // -----------------------------------------------------------------------------
122 /** quotes a string and search for quotes inside the string and replace them with the new quote
124 The value to be quoted.
127 @param rQuotToReplace
128 The quote to replace with
132 ::rtl::OUString
SetQuotation(const ::rtl::OUString
& rValue
, const ::rtl::OUString
& rQuot
, const ::rtl::OUString
& rQuotToReplace
)
134 ::rtl::OUString rNewValue
= rQuot
;
136 sal_Int32 nIndex
= (sal_Int32
)-1; // Quotes durch zweifache Quotes ersetzen, sonst kriegt der Parser Probleme
138 if (rQuot
.getLength())
143 nIndex
= rNewValue
.indexOf(rQuot
,nIndex
);
145 rNewValue
= rNewValue
.replaceAt(nIndex
,rQuot
.getLength(),rQuotToReplace
);
146 } while (nIndex
!= -1);
154 namespace connectivity
157 //=============================================================================
158 struct OSQLParser_Data
160 ::com::sun::star::lang::Locale aLocale
;
161 ::connectivity::SQLError aErrors
;
163 OSQLParser_Data( const Reference
< XMultiServiceFactory
>& _xServiceFactory
)
164 :aErrors( _xServiceFactory
)
169 //=============================================================================
170 //= SQLParseNodeParameter
171 //=============================================================================
172 //-----------------------------------------------------------------------------
173 SQLParseNodeParameter::SQLParseNodeParameter( const Reference
< XConnection
>& _rxConnection
,
174 const Reference
< XNumberFormatter
>& _xFormatter
, const Reference
< XPropertySet
>& _xField
,
175 const Locale
& _rLocale
, const IParseContext
* _pContext
,
176 bool _bIntl
, bool _bQuote
, sal_Char _cDecSep
, bool _bPredicate
, bool _bParseToSDBC
)
178 ,aMetaData( _rxConnection
)
180 ,pSubQueryHistory( new QueryNameSet
)
181 ,xFormatter(_xFormatter
)
183 ,m_rContext( _pContext
? (const IParseContext
&)(*_pContext
) : (const IParseContext
&)OSQLParser::s_aDefaultContext
)
186 ,bInternational(_bIntl
)
187 ,bPredicate(_bPredicate
)
188 ,bParseToSDBCLevel( _bParseToSDBC
)
192 //-----------------------------------------------------------------------------
193 SQLParseNodeParameter::~SQLParseNodeParameter()
197 //=============================================================================
199 //=============================================================================
200 //-----------------------------------------------------------------------------
201 ::rtl::OUString
OSQLParseNode::convertDateString(const SQLParseNodeParameter
& rParam
, const ::rtl::OUString
& rString
) const
203 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertDateString" );
204 Date aDate
= DBTypeConversion::toDate(rString
);
205 Reference
< XNumberFormatsSupplier
> xSupplier(rParam
.xFormatter
->getNumberFormatsSupplier());
206 Reference
< XNumberFormatTypes
> xTypes(xSupplier
->getNumberFormats(), UNO_QUERY
);
208 double fDate
= DBTypeConversion::toDouble(aDate
,DBTypeConversion::getNULLDate(xSupplier
));
209 sal_Int32 nKey
= xTypes
->getStandardIndex(rParam
.rLocale
) + 36; // XXX hack
210 return rParam
.xFormatter
->convertNumberToString(nKey
, fDate
);
213 //-----------------------------------------------------------------------------
214 ::rtl::OUString
OSQLParseNode::convertDateTimeString(const SQLParseNodeParameter
& rParam
, const ::rtl::OUString
& rString
) const
216 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertDateTimeString" );
217 DateTime aDate
= DBTypeConversion::toDateTime(rString
);
218 Reference
< XNumberFormatsSupplier
> xSupplier(rParam
.xFormatter
->getNumberFormatsSupplier());
219 Reference
< XNumberFormatTypes
> xTypes(xSupplier
->getNumberFormats(), UNO_QUERY
);
221 double fDateTime
= DBTypeConversion::toDouble(aDate
,DBTypeConversion::getNULLDate(xSupplier
));
222 sal_Int32 nKey
= xTypes
->getStandardIndex(rParam
.rLocale
) + 51; // XXX hack
223 return rParam
.xFormatter
->convertNumberToString(nKey
, fDateTime
);
226 //-----------------------------------------------------------------------------
227 ::rtl::OUString
OSQLParseNode::convertTimeString(const SQLParseNodeParameter
& rParam
, const ::rtl::OUString
& rString
) const
229 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertTimeString" );
230 Time aTime
= DBTypeConversion::toTime(rString
);
231 Reference
< XNumberFormatsSupplier
> xSupplier(rParam
.xFormatter
->getNumberFormatsSupplier());
233 Reference
< XNumberFormatTypes
> xTypes(xSupplier
->getNumberFormats(), UNO_QUERY
);
235 double fTime
= DBTypeConversion::toDouble(aTime
);
236 sal_Int32 nKey
= xTypes
->getStandardIndex(rParam
.rLocale
) + 41; // XXX hack
237 return rParam
.xFormatter
->convertNumberToString(nKey
, fTime
);
240 //-----------------------------------------------------------------------------
241 void OSQLParseNode::parseNodeToStr(::rtl::OUString
& rString
,
242 const Reference
< XConnection
>& _rxConnection
,
243 const IParseContext
* pContext
,
245 sal_Bool _bQuote
) const
247 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToStr" );
250 rString
, _rxConnection
, NULL
, NULL
,
251 pContext
? pContext
->getPreferredLocale() : OParseContext::getDefaultLocale(),
252 pContext
, _bIntl
, _bQuote
, '.', false, false );
255 //-----------------------------------------------------------------------------
256 void OSQLParseNode::parseNodeToPredicateStr(::rtl::OUString
& rString
,
257 const Reference
< XConnection
>& _rxConnection
,
258 const Reference
< XNumberFormatter
> & xFormatter
,
259 const ::com::sun::star::lang::Locale
& rIntl
,
261 const IParseContext
* pContext
) const
263 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToPredicateStr" );
265 OSL_ENSURE(xFormatter
.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
268 parseNodeToStr(rString
, _rxConnection
, xFormatter
, NULL
, rIntl
, pContext
, sal_True
, sal_True
, _cDec
, true, false);
271 //-----------------------------------------------------------------------------
272 void OSQLParseNode::parseNodeToPredicateStr(::rtl::OUString
& rString
,
273 const Reference
< XConnection
> & _rxConnection
,
274 const Reference
< XNumberFormatter
> & xFormatter
,
275 const Reference
< XPropertySet
> & _xField
,
276 const ::com::sun::star::lang::Locale
& rIntl
,
278 const IParseContext
* pContext
) const
280 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToPredicateStr" );
282 OSL_ENSURE(xFormatter
.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
285 parseNodeToStr( rString
, _rxConnection
, xFormatter
, _xField
, rIntl
, pContext
, true, true, _cDec
, true, false );
288 //-----------------------------------------------------------------------------
289 void OSQLParseNode::parseNodeToStr(::rtl::OUString
& rString
,
290 const Reference
< XConnection
> & _rxConnection
,
291 const Reference
< XNumberFormatter
> & xFormatter
,
292 const Reference
< XPropertySet
> & _xField
,
293 const ::com::sun::star::lang::Locale
& rIntl
,
294 const IParseContext
* pContext
,
299 bool _bSubstitute
) const
301 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToStr" );
303 OSL_ENSURE( _rxConnection
.is(), "OSQLParseNode::parseNodeToStr: invalid connection!" );
305 if ( _rxConnection
.is() )
307 ::rtl::OUStringBuffer sBuffer
= rString
;
310 OSQLParseNode::impl_parseNodeToString_throw( sBuffer
,
311 SQLParseNodeParameter(
312 _rxConnection
, xFormatter
, _xField
, rIntl
, pContext
,
313 _bIntl
, _bQuote
, _cDecSep
, _bPredicate
, _bSubstitute
316 catch( const SQLException
& )
318 OSL_ENSURE( false, "OSQLParseNode::parseNodeToStr: this should not throw!" );
319 // our callers don't expect this method to throw anything. The only known situation
320 // where impl_parseNodeToString_throw can throw is when there is a cyclic reference
321 // in the sub queries, but this cannot be the case here, as we do not parse to
324 rString
= sBuffer
.makeStringAndClear();
327 //-----------------------------------------------------------------------------
328 bool OSQLParseNode::parseNodeToExecutableStatement( ::rtl::OUString
& _out_rString
, const Reference
< XConnection
>& _rxConnection
,
329 OSQLParser
& _rParser
, ::com::sun::star::sdbc::SQLException
* _pErrorHolder
) const
331 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToExecutableStatement" );
332 OSL_PRECOND( _rxConnection
.is(), "OSQLParseNode::parseNodeToExecutableStatement: invalid connection!" );
333 SQLParseNodeParameter
aParseParam( _rxConnection
,
334 NULL
, NULL
, OParseContext::getDefaultLocale(), NULL
, false, true, '.', false, true );
336 if ( aParseParam
.aMetaData
.supportsSubqueriesInFrom() )
338 Reference
< XQueriesSupplier
> xSuppQueries( _rxConnection
, UNO_QUERY
);
339 OSL_ENSURE( xSuppQueries
.is(), "OSQLParseNode::parseNodeToExecutableStatement: cannot substitute everything without a QueriesSupplier!" );
340 if ( xSuppQueries
.is() )
341 aParseParam
.xQueries
= xSuppQueries
->getQueries();
344 aParseParam
.pParser
= &_rParser
;
346 _out_rString
= ::rtl::OUString();
347 ::rtl::OUStringBuffer sBuffer
;
348 bool bSuccess
= false;
351 impl_parseNodeToString_throw( sBuffer
, aParseParam
);
354 catch( const SQLException
& e
)
359 _out_rString
= sBuffer
.makeStringAndClear();
363 //-----------------------------------------------------------------------------
366 bool lcl_isAliasNamePresent( const OSQLParseNode
& _rTableNameNode
)
368 return OSQLParseNode::getTableRange(_rTableNameNode
.getParent()).getLength() != 0;
372 //-----------------------------------------------------------------------------
373 void OSQLParseNode::impl_parseNodeToString_throw(::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
375 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableRange" );
378 parseLeaf(rString
,rParam
);
382 // einmal auswerten wieviel Subtrees dieser Knoten besitzt
383 sal_uInt32 nCount
= count();
385 bool bHandled
= false;
386 switch ( getKnownRuleID() )
388 // special handling for parameters
391 if(rString
.getLength())
392 rString
.appendAscii(" ");
393 if (nCount
== 1) // ?
394 m_aChildren
[0]->impl_parseNodeToString_throw( rString
, rParam
);
395 else if (nCount
== 2) // :Name
397 m_aChildren
[0]->impl_parseNodeToString_throw( rString
, rParam
);
398 rString
.append(m_aChildren
[1]->m_aNodeValue
);
402 m_aChildren
[0]->impl_parseNodeToString_throw( rString
, rParam
);
403 rString
.append(m_aChildren
[1]->m_aNodeValue
);
404 rString
.append(m_aChildren
[2]->m_aNodeValue
);
412 if ( ( nCount
== 2 ) || ( nCount
== 3 ) || ( nCount
== 5 ) )
414 impl_parseTableRangeNodeToString_throw( rString
, rParam
);
419 // table name - might be a query name
421 bHandled
= impl_parseTableNameNodeToString_throw( rString
, rParam
);
425 if ( rParam
.aMetaData
.generateASBeforeCorrelationName() )
426 rString
.append(::rtl::OUString::createFromAscii( " AS" ));
431 // je nachdem ob international angegeben wird oder nicht wird like anders behandelt
432 // interanational: *, ? sind Platzhalter
433 // sonst SQL92 konform: %, _
434 impl_parseLikeNodeToString_throw( rString
, rParam
);
438 case general_set_fct
:
445 if (!addDateValue(rString
, rParam
))
447 // Funktionsname nicht quoten
448 SQLParseNodeParameter
aNewParam(rParam
);
449 aNewParam
.bQuote
= ( SQL_ISRULE(this,length_exp
) || SQL_ISRULE(this,char_value_fct
) );
451 m_aChildren
[0]->impl_parseNodeToString_throw( rString
, aNewParam
);
452 aNewParam
.bQuote
= rParam
.bQuote
;
453 //aNewParam.bPredicate = sal_False; // disable [ ] around names // look at i73215
454 ::rtl::OUStringBuffer aStringPara
;
455 for (sal_uInt32 i
=1; i
<nCount
; i
++)
457 const OSQLParseNode
* pSubTree
= m_aChildren
[i
];
460 pSubTree
->impl_parseNodeToString_throw( aStringPara
, aNewParam
);
462 // bei den CommaListen zwischen alle Subtrees Commas setzen
463 if ((m_eNodeType
== SQL_NODE_COMMALISTRULE
) && (i
< (nCount
- 1)))
464 aStringPara
.appendAscii(",");
469 rString
.append(aStringPara
.makeStringAndClear());
476 } // switch ( getKnownRuleID() )
480 for (OSQLParseNodes::const_iterator i
= m_aChildren
.begin();
481 i
!= m_aChildren
.end();)
483 const OSQLParseNode
* pSubTree
= *i
;
490 SQLParseNodeParameter
aNewParam(rParam
);
492 // don't replace the field for subqueries
493 if (rParam
.xField
.is() && SQL_ISRULE(pSubTree
,subquery
))
494 aNewParam
.xField
= NULL
;
496 // if there is a field given we don't display the fieldname, if there is any
497 if (rParam
.xField
.is() && SQL_ISRULE(pSubTree
,column_ref
))
499 sal_Bool bFilter
= sal_False
;
500 // retrieve the fields name
501 ::rtl::OUString aFieldName
;
504 sal_Int32 nNamePropertyId
= PROPERTY_ID_NAME
;
505 if ( rParam
.xField
->getPropertySetInfo()->hasPropertyByName( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_REALNAME
) ) )
506 nNamePropertyId
= PROPERTY_ID_REALNAME
;
507 rParam
.xField
->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( nNamePropertyId
) ) >>= aFieldName
;
513 if(pSubTree
->count())
515 const OSQLParseNode
* pCol
= pSubTree
->m_aChildren
[pSubTree
->count()-1];
516 if ( ( SQL_ISRULE(pCol
,column_val
)
517 && pCol
->getChild(0)->getTokenValue().equalsIgnoreAsciiCase(aFieldName
)
519 || pCol
->getTokenValue().equalsIgnoreAsciiCase(aFieldName
)
524 // ok we found the field, if the following node is the
525 // comparision operator '=' we filter it as well
528 if (SQL_ISRULE(this, comparison_predicate
))
531 if(i
!= m_aChildren
.end())
534 if (pSubTree
&& pSubTree
->getNodeType() == SQL_NODE_EQUAL
)
543 pSubTree
->impl_parseNodeToString_throw( rString
, aNewParam
);
546 // bei den CommaListen zwischen alle Subtrees Commas setzen
547 if ((m_eNodeType
== SQL_NODE_COMMALISTRULE
) && (i
!= m_aChildren
.end()))
548 rString
.appendAscii(",");
553 pSubTree
->impl_parseNodeToString_throw( rString
, aNewParam
);
556 // bei den CommaListen zwischen alle Subtrees Commas setzen
557 if ((m_eNodeType
== SQL_NODE_COMMALISTRULE
) && (i
!= m_aChildren
.end()))
559 if (SQL_ISRULE(this,value_exp_commalist
) && rParam
.bPredicate
)
560 rString
.appendAscii(";");
562 rString
.appendAscii(",");
569 //-----------------------------------------------------------------------------
570 bool OSQLParseNode::impl_parseTableNameNodeToString_throw( ::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
572 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseTableNameNodeToString_throw" );
573 // is the table_name part of a table_ref?
574 OSL_ENSURE( getParent(), "OSQLParseNode::impl_parseTableNameNodeToString_throw: table_name without parent?" );
575 if ( !getParent() || ( getParent()->getKnownRuleID() != table_ref
) )
578 // if it's a query, maybe we need to substitute the SQL statement ...
579 if ( !rParam
.bParseToSDBCLevel
)
582 if ( !rParam
.xQueries
.is() )
583 // connection does not support queries in queries, or was no query supplier
588 ::rtl::OUString
sTableOrQueryName( getChild(0)->getTokenValue() );
589 bool bIsQuery
= rParam
.xQueries
->hasByName( sTableOrQueryName
);
593 // avoid recursion (e.g. "foo" defined as "SELECT * FROM bar" and "bar" defined as "SELECT * FROM foo".
594 if ( rParam
.pSubQueryHistory
->find( sTableOrQueryName
) != rParam
.pSubQueryHistory
->end() )
596 ::rtl::OUString
sMessage( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "cyclic sub queries" ) ) );
597 OSL_ENSURE( rParam
.pParser
, "OSQLParseNode::impl_parseTableNameNodeToString_throw: no parser?" );
598 if ( rParam
.pParser
)
600 const SQLError
& rErrors( rParam
.pParser
->getErrorHelper() );
601 rErrors
.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES
);
605 SQLError
aErrors( ::comphelper::getProcessServiceFactory() );
606 aErrors
.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES
);
609 rParam
.pSubQueryHistory
->insert( sTableOrQueryName
);
611 Reference
< XPropertySet
> xQuery( rParam
.xQueries
->getByName( sTableOrQueryName
), UNO_QUERY_THROW
);
613 // substitute the query name with the constituting command
614 ::rtl::OUString sCommand
;
615 OSL_VERIFY( xQuery
->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND
) ) >>= sCommand
);
617 sal_Bool bEscapeProcessing
= sal_False
;
618 OSL_VERIFY( xQuery
->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING
) ) >>= bEscapeProcessing
);
620 // the query we found here might itself be based on another query, so parse it recursively
621 OSL_ENSURE( rParam
.pParser
, "OSQLParseNode::impl_parseTableNameNodeToString_throw: cannot analyze sub queries without a parser!" );
622 if ( bEscapeProcessing
&& rParam
.pParser
)
624 ::rtl::OUString sError
;
625 ::std::auto_ptr
< OSQLParseNode
> pSubQueryNode( rParam
.pParser
->parseTree( sError
, sCommand
, sal_False
) );
626 if ( pSubQueryNode
.get() )
628 // parse the sub-select to SDBC level, too
629 ::rtl::OUStringBuffer sSubSelect
;
630 pSubQueryNode
->impl_parseNodeToString_throw( sSubSelect
, rParam
);
631 if ( sSubSelect
.getLength() )
632 sCommand
= sSubSelect
.makeStringAndClear();
636 rString
.appendAscii( " ( " );
637 rString
.append(sCommand
);
638 rString
.appendAscii( " )" );
640 // append the query name as table alias, since it might be referenced in other
641 // parts of the statement - but only if there's no other alias name present
642 if ( !lcl_isAliasNamePresent( *this ) )
644 rString
.appendAscii( " AS " );
646 rString
.append(SetQuotation( sTableOrQueryName
,
647 rParam
.aMetaData
.getIdentifierQuoteString(), rParam
.aMetaData
.getIdentifierQuoteString() ));
650 // don't forget to remove the query name from the history, else multiple inclusions
652 // #i69227# / 2006-10-10 / frank.schoenheit@sun.com
653 rParam
.pSubQueryHistory
->erase( sTableOrQueryName
);
657 catch( const SQLException
& )
661 catch( const Exception
& )
663 DBG_UNHANDLED_EXCEPTION();
668 //-----------------------------------------------------------------------------
669 void OSQLParseNode::impl_parseTableRangeNodeToString_throw(::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
671 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseTableRangeNodeToString_throw" );
672 OSL_PRECOND( ( count() == 2 ) || ( count() == 3 ) || ( count() == 5 ) ,"Illegal count");
674 // rString += ::rtl::OUString::createFromAscii(" ");
675 ::std::for_each(m_aChildren
.begin(),m_aChildren
.end(),
676 boost::bind( &OSQLParseNode::impl_parseNodeToString_throw
, _1
, boost::ref( rString
), boost::cref( rParam
) ));
679 //-----------------------------------------------------------------------------
680 void OSQLParseNode::impl_parseLikeNodeToString_throw( ::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
682 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseLikeNodeToString_throw" );
683 OSL_ENSURE(count() >= 4,"count != 5: Prepare for GPF");
685 const OSQLParseNode
* pEscNode
= NULL
;
686 const OSQLParseNode
* pParaNode
= NULL
;
688 SQLParseNodeParameter
aNewParam(rParam
);
689 //aNewParam.bQuote = sal_True; // why setting this to true? @see http://www.openoffice.org/issues/show_bug.cgi?id=75557
691 // if there is a field given we don't display the fieldname, if there are any
692 sal_Bool bAddName
= sal_True
;
693 if (rParam
.xField
.is())
695 // retrieve the fields name
696 ::rtl::OUString aFieldName
;
699 // retrieve the fields name
700 rtl::OUString aString
;
701 rParam
.xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)) >>= aString
;
702 aFieldName
= aString
.getStr();
706 OSL_ENSURE( false, "OSQLParseNode::impl_parseLikeNodeToString_throw Exception occured!" );
708 if ( !m_aChildren
[0]->isLeaf() )
710 const OSQLParseNode
* pCol
= m_aChildren
[0]->getChild(m_aChildren
[0]->count()-1);
711 if ((SQL_ISRULE(pCol
,column_val
) && pCol
->getChild(0)->getTokenValue().equalsIgnoreAsciiCase(aFieldName
)) ||
712 pCol
->getTokenValue().equalsIgnoreAsciiCase(aFieldName
) )
713 bAddName
= sal_False
;
718 m_aChildren
[0]->impl_parseNodeToString_throw( rString
, aNewParam
);
720 m_aChildren
[1]->impl_parseNodeToString_throw( rString
, aNewParam
);
722 m_aChildren
[2]->impl_parseNodeToString_throw( rString
, aNewParam
);
724 sal_Int32 nCurentPos
= m_aChildren
.size()-2;
725 pParaNode
= m_aChildren
[nCurentPos
];
726 pEscNode
= m_aChildren
[nCurentPos
+1];
728 if (pParaNode
->isToken())
730 ::rtl::OUString aStr
= ConvertLikeToken(pParaNode
, pEscNode
, rParam
.bInternational
);
731 rString
.appendAscii(" ");
732 rString
.append(SetQuotation(aStr
,::rtl::OUString::createFromAscii("\'"),::rtl::OUString::createFromAscii("\'\'")));
735 pParaNode
->impl_parseNodeToString_throw( rString
, aNewParam
);
737 pEscNode
->impl_parseNodeToString_throw( rString
, aNewParam
);
741 // -----------------------------------------------------------------------------
742 sal_Bool
OSQLParseNode::getTableComponents(const OSQLParseNode
* _pTableNode
,
743 ::com::sun::star::uno::Any
&_rCatalog
,
744 ::rtl::OUString
&_rSchema
,
745 ::rtl::OUString
&_rTable
,
746 const Reference
< XDatabaseMetaData
>& _xMetaData
)
748 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableComponents" );
749 OSL_ENSURE(_pTableNode
,"Wrong use of getTableComponents! _pTableNode is not allowed to be null!");
752 const sal_Bool bSupportsCatalog
= _xMetaData
.is() && _xMetaData
->supportsCatalogsInDataManipulation();
753 const sal_Bool bSupportsSchema
= _xMetaData
.is() && _xMetaData
->supportsSchemasInDataManipulation();
754 const OSQLParseNode
* pTableNode
= _pTableNode
;
755 // clear the parameter given
757 _rSchema
= _rTable
= ::rtl::OUString();
758 // see rule catalog_name: in sqlbison.y
759 if (SQL_ISRULE(pTableNode
,catalog_name
))
761 OSL_ENSURE(pTableNode
->getChild(0) && pTableNode
->getChild(0)->isToken(),"Invalid parsenode!");
762 _rCatalog
<<= pTableNode
->getChild(0)->getTokenValue();
763 pTableNode
= pTableNode
->getChild(2);
765 // check if we have schema_name rule
766 if(SQL_ISRULE(pTableNode
,schema_name
))
768 if ( bSupportsCatalog
&& !bSupportsSchema
)
769 _rCatalog
<<= pTableNode
->getChild(0)->getTokenValue();
771 _rSchema
= pTableNode
->getChild(0)->getTokenValue();
772 pTableNode
= pTableNode
->getChild(2);
774 // check if we have table_name rule
775 if(SQL_ISRULE(pTableNode
,table_name
))
777 _rTable
= pTableNode
->getChild(0)->getTokenValue();
781 OSL_ENSURE(0,"Error in parse tree!");
784 return _rTable
.getLength() != 0;
786 // -----------------------------------------------------------------------------
787 void OSQLParser::killThousandSeparator(OSQLParseNode
* pLiteral
)
791 if ( s_xLocaleData
->getLocaleItem( m_pData
->aLocale
).decimalSeparator
.toChar() == ',' )
793 pLiteral
->m_aNodeValue
= pLiteral
->m_aNodeValue
.replace('.', sal_Unicode());
794 // and replace decimal
795 pLiteral
->m_aNodeValue
= pLiteral
->m_aNodeValue
.replace(',', '.');
798 pLiteral
->m_aNodeValue
= pLiteral
->m_aNodeValue
.replace(',', sal_Unicode());
801 // -----------------------------------------------------------------------------
802 OSQLParseNode
* OSQLParser::convertNode(sal_Int32 nType
,OSQLParseNode
*& pLiteral
)
807 OSQLParseNode
* pReturn
= pLiteral
;
809 if ( ( pLiteral
->isRule() && !SQL_ISRULE(pLiteral
,value_exp
) ) || SQL_ISTOKEN(pLiteral
,FALSE
) || SQL_ISTOKEN(pLiteral
,TRUE
) )
814 case DataType::VARCHAR
:
815 case DataType::LONGVARCHAR
:
816 if ( !SQL_ISRULE(pReturn
,char_value_exp
) && !buildStringNodes(pReturn
) )
824 switch(pLiteral
->getNodeType())
826 case SQL_NODE_STRING
:
830 case DataType::VARCHAR
:
831 case DataType::LONGVARCHAR
:
835 case DataType::TIMESTAMP
:
836 if (m_xFormatter
.is())
837 pReturn
= buildDate( nType
, pReturn
);
840 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE
);
844 case SQL_NODE_ACCESS_DATE
:
849 case DataType::TIMESTAMP
:
850 if ( m_xFormatter
.is() )
851 pReturn
= buildDate( nType
, pReturn
);
853 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE
);
856 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE
);
860 case SQL_NODE_INTNUM
:
864 case DataType::BOOLEAN
:
865 case DataType::DECIMAL
:
866 case DataType::NUMERIC
:
867 case DataType::TINYINT
:
868 case DataType::SMALLINT
:
869 case DataType::INTEGER
:
870 case DataType::BIGINT
:
871 case DataType::FLOAT
:
873 case DataType::DOUBLE
:
874 // kill thousand seperators if any
875 killThousandSeparator(pReturn
);
878 case DataType::VARCHAR
:
879 case DataType::LONGVARCHAR
:
880 pReturn
= buildNode_STR_NUM(pReturn
);
883 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_INT_COMPARE
);
887 case SQL_NODE_APPROXNUM
:
890 case DataType::DECIMAL
:
891 case DataType::NUMERIC
:
892 case DataType::FLOAT
:
894 case DataType::DOUBLE
:
895 // kill thousand seperators if any
896 killThousandSeparator(pReturn
);
899 case DataType::VARCHAR
:
900 case DataType::LONGVARCHAR
:
901 pReturn
= buildNode_STR_NUM(pReturn
);
903 case DataType::INTEGER
:
905 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_REAL_COMPARE
);
910 OSL_ENSURE(0,"Not handled!");
915 // -----------------------------------------------------------------------------
916 sal_Int16
OSQLParser::buildPredicateRule(OSQLParseNode
*& pAppend
,OSQLParseNode
* pLiteral
,OSQLParseNode
*& pCompare
,OSQLParseNode
* pLiteral2
)
918 OSL_ENSURE(inPredicateCheck(),"Only in predicate check allowed!");
925 m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
)) >>= nType
;
932 OSQLParseNode
* pNode1
= convertNode(nType
,pLiteral
);
935 OSQLParseNode
* pNode2
= convertNode(nType
,pLiteral2
);
936 if ( !m_sErrorMessage
.getLength() )
937 nErg
= buildNode(pAppend
,pCompare
,pNode1
,pNode2
);
940 if (!pCompare
->getParent()) // I have no parent so I was not used and I must die :-)
944 // -----------------------------------------------------------------------------
945 sal_Int16
OSQLParser::buildLikeRule(OSQLParseNode
*& pAppend
, OSQLParseNode
*& pLiteral
, const OSQLParseNode
* pEscape
)
956 aValue
= m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
));
968 case DataType::VARCHAR
:
969 case DataType::LONGVARCHAR
:
970 if(pLiteral
->isRule())
972 pAppend
->append(pLiteral
);
977 switch(pLiteral
->getNodeType())
979 case SQL_NODE_STRING
:
980 pLiteral
->m_aNodeValue
= ConvertLikeToken(pLiteral
, pEscape
, sal_False
);
981 pAppend
->append(pLiteral
);
984 case SQL_NODE_APPROXNUM
:
985 if (m_xFormatter
.is() && m_nFormatKey
)
987 sal_Int16 nScale
= 0;
990 Any aValue
= getNumberFormatProperty( m_xFormatter
, m_nFormatKey
, ::rtl::OUString::createFromAscii("Decimals") );
997 pAppend
->append(new OSQLInternalNode(stringToDouble(pLiteral
->getTokenValue(),nScale
),SQL_NODE_STRING
));
1000 pAppend
->append(new OSQLInternalNode(pLiteral
->getTokenValue(),SQL_NODE_STRING
));
1006 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_VALUE_NO_LIKE
);
1007 m_sErrorMessage
= m_sErrorMessage
.replaceAt(m_sErrorMessage
.indexOf(::rtl::OUString::createFromAscii("#1")),2,pLiteral
->getTokenValue());
1013 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_FIELD_NO_LIKE
);
1018 //-----------------------------------------------------------------------------
1019 OSQLParseNode
* OSQLParser::buildNode_Date(const double& fValue
, sal_Int32 nType
)
1021 ::rtl::OUString aEmptyString
;
1022 OSQLParseNode
* pNewNode
= new OSQLInternalNode(aEmptyString
, SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::set_fct_spec
));
1023 pNewNode
->append(new OSQLInternalNode(::rtl::OUString::createFromAscii("{"), SQL_NODE_PUNCTUATION
));
1024 OSQLParseNode
* pDateNode
= new OSQLInternalNode(aEmptyString
, SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::odbc_fct_spec
));
1025 pNewNode
->append(pDateNode
);
1026 pNewNode
->append(new OSQLInternalNode(::rtl::OUString::createFromAscii("}"), SQL_NODE_PUNCTUATION
));
1030 case DataType::DATE
:
1032 Date aDate
= DBTypeConversion::toDate(fValue
,DBTypeConversion::getNULLDate(m_xFormatter
->getNumberFormatsSupplier()));
1033 ::rtl::OUString aString
= DBTypeConversion::toDateString(aDate
);
1034 pDateNode
->append(new OSQLInternalNode(aEmptyString
, SQL_NODE_KEYWORD
, SQL_TOKEN_D
));
1035 pDateNode
->append(new OSQLInternalNode(aString
, SQL_NODE_STRING
));
1038 case DataType::TIME
:
1040 Time aTime
= DBTypeConversion::toTime(fValue
);
1041 ::rtl::OUString aString
= DBTypeConversion::toTimeString(aTime
);
1042 pDateNode
->append(new OSQLInternalNode(aEmptyString
, SQL_NODE_KEYWORD
, SQL_TOKEN_T
));
1043 pDateNode
->append(new OSQLInternalNode(aString
, SQL_NODE_STRING
));
1046 case DataType::TIMESTAMP
:
1048 DateTime aDateTime
= DBTypeConversion::toDateTime(fValue
,DBTypeConversion::getNULLDate(m_xFormatter
->getNumberFormatsSupplier()));
1049 if (aDateTime
.Seconds
|| aDateTime
.Minutes
|| aDateTime
.Hours
)
1051 ::rtl::OUString aString
= DBTypeConversion::toDateTimeString(aDateTime
);
1052 pDateNode
->append(new OSQLInternalNode(aEmptyString
, SQL_NODE_KEYWORD
, SQL_TOKEN_TS
));
1053 pDateNode
->append(new OSQLInternalNode(aString
, SQL_NODE_STRING
));
1057 Date
aDate(aDateTime
.Day
,aDateTime
.Month
,aDateTime
.Year
);
1058 pDateNode
->append(new OSQLInternalNode(aEmptyString
, SQL_NODE_KEYWORD
, SQL_TOKEN_D
));
1059 pDateNode
->append(new OSQLInternalNode(DBTypeConversion::toDateString(aDate
), SQL_NODE_STRING
));
1067 // -----------------------------------------------------------------------------
1068 OSQLParseNode
* OSQLParser::buildNode_STR_NUM(OSQLParseNode
*& _pLiteral
)
1070 OSQLParseNode
* pReturn
= NULL
;
1075 sal_Int16 nScale
= 0;
1076 ::rtl::OUString aDec
;
1079 Any aValue
= getNumberFormatProperty( m_xFormatter
, m_nFormatKey
, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Decimals")) );
1086 pReturn
= new OSQLInternalNode(stringToDouble(_pLiteral
->getTokenValue(),nScale
),SQL_NODE_STRING
);
1089 pReturn
= new OSQLInternalNode(_pLiteral
->getTokenValue(),SQL_NODE_STRING
);
1096 // -----------------------------------------------------------------------------
1097 ::rtl::OUString
OSQLParser::stringToDouble(const ::rtl::OUString
& _rValue
,sal_Int16 _nScale
)
1099 ::rtl::OUString aValue
;
1100 if(!m_xCharClass
.is())
1101 m_xCharClass
= Reference
<XCharacterClassification
>(m_xServiceFactory
->createInstance(::rtl::OUString::createFromAscii("com.sun.star.i18n.CharacterClassification")),UNO_QUERY
);
1102 if(m_xCharClass
.is() && s_xLocaleData
.is())
1106 ParseResult aResult
= m_xCharClass
->parsePredefinedToken(KParseType::ANY_NUMBER
,_rValue
,0,m_pData
->aLocale
,0,::rtl::OUString(),KParseType::ANY_NUMBER
,::rtl::OUString());
1107 if((aResult
.TokenType
& KParseType::IDENTNAME
) && aResult
.EndPos
== _rValue
.getLength())
1109 aValue
= ::rtl::OUString::valueOf(aResult
.Value
);
1110 sal_Int32 nPos
= aValue
.lastIndexOf(::rtl::OUString::createFromAscii("."));
1111 if((nPos
+_nScale
) < aValue
.getLength())
1112 aValue
= aValue
.replaceAt(nPos
+_nScale
,aValue
.getLength()-nPos
-_nScale
,::rtl::OUString());
1113 aValue
= aValue
.replaceAt(aValue
.lastIndexOf(::rtl::OUString::createFromAscii(".")),1,s_xLocaleData
->getLocaleItem(m_pData
->aLocale
).decimalSeparator
);
1123 // -----------------------------------------------------------------------------
1125 ::osl::Mutex
& OSQLParser::getMutex()
1127 static ::osl::Mutex aMutex
;
1131 //-----------------------------------------------------------------------------
1132 OSQLParseNode
* OSQLParser::predicateTree(::rtl::OUString
& rErrorMessage
, const ::rtl::OUString
& rStatement
,
1133 const Reference
< ::com::sun::star::util::XNumberFormatter
> & xFormatter
,
1134 const Reference
< XPropertySet
> & xField
)
1138 // mutex for parsing
1139 static ::osl::Mutex aMutex
;
1141 // Guard the parsing
1142 ::osl::MutexGuard
aGuard(getMutex());
1149 m_xFormatter
= xFormatter
;
1156 // get the field name
1157 rtl::OUString aString
;
1159 // retrieve the fields name
1160 // #75243# use the RealName of the column if there is any otherwise the name which could be the alias
1162 Reference
< XPropertySetInfo
> xInfo
= m_xField
->getPropertySetInfo();
1163 if ( xInfo
->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME
)))
1164 m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME
)) >>= aString
;
1166 m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)) >>= aString
;
1168 m_sFieldName
= aString
;
1170 // get the field format key
1171 if ( xInfo
->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY
)))
1172 m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY
)) >>= m_nFormatKey
;
1176 // get the field type
1177 m_xField
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
)) >>= nType
;
1179 catch ( Exception
& )
1184 if (m_nFormatKey
&& m_xFormatter
.is())
1186 Any aValue
= getNumberFormatProperty( m_xFormatter
, m_nFormatKey
, OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_LOCALE
) );
1187 OSL_ENSURE(aValue
.getValueType() == ::getCppuType((const ::com::sun::star::lang::Locale
*)0), "OSQLParser::PredicateTree : invalid language property !");
1189 if (aValue
.getValueType() == ::getCppuType((const ::com::sun::star::lang::Locale
*)0))
1190 aValue
>>= m_pData
->aLocale
;
1193 m_pData
->aLocale
= m_pContext
->getPreferredLocale();
1195 if ( m_xFormatter
.is() )
1199 Reference
< ::com::sun::star::util::XNumberFormatsSupplier
> xFormatSup
= m_xFormatter
->getNumberFormatsSupplier();
1200 if ( xFormatSup
.is() )
1202 Reference
< ::com::sun::star::util::XNumberFormats
> xFormats
= xFormatSup
->getNumberFormats();
1203 if ( xFormats
.is() )
1205 ::com::sun::star::lang::Locale aLocale
;
1206 aLocale
.Language
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en"));
1207 aLocale
.Country
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("US"));
1208 ::rtl::OUString
sFormat(RTL_CONSTASCII_USTRINGPARAM("YYYY-MM-DD"));
1209 m_nDateFormatKey
= xFormats
->queryKey(sFormat
,aLocale
,sal_False
);
1210 if ( m_nDateFormatKey
== sal_Int32(-1) )
1211 m_nDateFormatKey
= xFormats
->addNew(sFormat
, aLocale
);
1215 catch ( Exception
& )
1217 OSL_ENSURE(0,"DateFormatKey");
1223 case DataType::DATE
:
1224 case DataType::TIME
:
1225 case DataType::TIMESTAMP
:
1226 s_pScanner
->SetRule(s_pScanner
->GetDATERule());
1228 case DataType::CHAR
:
1229 case DataType::VARCHAR
:
1230 case DataType::LONGVARCHAR
:
1231 s_pScanner
->SetRule(s_pScanner
->GetSTRINGRule());
1234 if ( s_xLocaleData
->getLocaleItem( m_pData
->aLocale
).decimalSeparator
.toChar() == ',' )
1235 s_pScanner
->SetRule(s_pScanner
->GetGERRule());
1237 s_pScanner
->SetRule(s_pScanner
->GetENGRule());
1242 s_pScanner
->SetRule(s_pScanner
->GetSQLRule());
1244 s_pScanner
->prepareScan(rStatement
, m_pContext
, sal_True
);
1246 SQLyylval
.pParseNode
= NULL
;
1248 m_pParseTree
= NULL
;
1249 m_sErrorMessage
= ::rtl::OUString();
1251 // ... und den Parser anwerfen ...
1252 if (SQLyyparse() != 0)
1254 m_sFieldName
= ::rtl::OUString();
1256 m_xFormatter
.clear();
1258 m_nDateFormatKey
= 0;
1260 if (!m_sErrorMessage
.getLength())
1261 m_sErrorMessage
= s_pScanner
->getErrorMessage();
1262 if (!m_sErrorMessage
.getLength())
1263 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_GENERAL
);
1265 rErrorMessage
= m_sErrorMessage
;
1267 // clear the garbage collector
1268 (*s_pGarbageCollector
)->clearAndDelete();
1273 (*s_pGarbageCollector
)->clear();
1275 m_sFieldName
= ::rtl::OUString();
1277 m_xFormatter
.clear();
1279 m_nDateFormatKey
= 0;
1281 // Das Ergebnis liefern (den Root Parse Node):
1283 // Stattdessen setzt die Parse-Routine jetzt den Member pParseTree
1284 // - einfach diesen zurueckliefern:
1285 OSL_ENSURE(m_pParseTree
!= NULL
,"OSQLParser: Parser hat keinen ParseTree geliefert");
1286 return m_pParseTree
;
1290 //=============================================================================
1291 //-----------------------------------------------------------------------------
1292 OSQLParser::OSQLParser(const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _xServiceFactory
,const IParseContext
* _pContext
)
1293 :m_pContext(_pContext
)
1295 ,m_pData( new OSQLParser_Data( _xServiceFactory
) )
1297 ,m_nDateFormatKey(0)
1298 ,m_xServiceFactory(_xServiceFactory
)
1305 #ifdef SQLYYDEBUG_ON
1310 ::osl::MutexGuard
aGuard(getMutex());
1311 // do we have to initialize the data
1312 if (s_nRefCount
== 0)
1314 s_pScanner
= new OSQLScanner();
1315 s_pScanner
->setScanner();
1316 s_pGarbageCollector
= new OSQLParseNodesGarbageCollector();
1318 if(!s_xLocaleData
.is())
1319 s_xLocaleData
= Reference
<XLocaleData
>(m_xServiceFactory
->createInstance(::rtl::OUString::createFromAscii("com.sun.star.i18n.LocaleData")),UNO_QUERY
);
1321 // auf 0 zuruecksetzen
1322 memset(OSQLParser::s_nRuleIDs
,0,sizeof(OSQLParser::s_nRuleIDs
[0]) * (OSQLParseNode::rule_count
+1));
1326 OSQLParseNode::Rule eRule
; // the parse node's ID for the rule
1327 ::rtl::OString sRuleName
; // the name of the rule ("select_statement")
1328 } aRuleDescriptions
[] =
1330 { OSQLParseNode::select_statement
, "select_statement" },
1331 { OSQLParseNode::table_exp
, "table_exp" },
1332 { OSQLParseNode::table_ref_commalist
, "table_ref_commalist" },
1333 { OSQLParseNode::table_ref
, "table_ref" },
1334 { OSQLParseNode::catalog_name
, "catalog_name" },
1335 { OSQLParseNode::schema_name
, "schema_name" },
1336 { OSQLParseNode::table_name
, "table_name" },
1337 { OSQLParseNode::opt_column_commalist
, "opt_column_commalist" },
1338 { OSQLParseNode::column_commalist
, "column_commalist" },
1339 { OSQLParseNode::column_ref_commalist
, "column_ref_commalist" },
1340 { OSQLParseNode::column_ref
, "column_ref" },
1341 { OSQLParseNode::opt_order_by_clause
, "opt_order_by_clause" },
1342 { OSQLParseNode::ordering_spec_commalist
, "ordering_spec_commalist" },
1343 { OSQLParseNode::ordering_spec
, "ordering_spec" },
1344 { OSQLParseNode::opt_asc_desc
, "opt_asc_desc" },
1345 { OSQLParseNode::where_clause
, "where_clause" },
1346 { OSQLParseNode::opt_where_clause
, "opt_where_clause" },
1347 { OSQLParseNode::search_condition
, "search_condition" },
1348 { OSQLParseNode::comparison_predicate
, "comparison_predicate" },
1349 { OSQLParseNode::between_predicate
, "between_predicate" },
1350 { OSQLParseNode::like_predicate
, "like_predicate" },
1351 { OSQLParseNode::opt_escape
, "opt_escape" },
1352 { OSQLParseNode::test_for_null
, "test_for_null" },
1353 { OSQLParseNode::scalar_exp_commalist
, "scalar_exp_commalist" },
1354 { OSQLParseNode::scalar_exp
, "scalar_exp" },
1355 { OSQLParseNode::parameter_ref
, "parameter_ref" },
1356 { OSQLParseNode::parameter
, "parameter" },
1357 { OSQLParseNode::general_set_fct
, "general_set_fct" },
1358 { OSQLParseNode::range_variable
, "range_variable" },
1359 { OSQLParseNode::column
, "column" },
1360 { OSQLParseNode::delete_statement_positioned
, "delete_statement_positioned" },
1361 { OSQLParseNode::delete_statement_searched
, "delete_statement_searched" },
1362 { OSQLParseNode::update_statement_positioned
, "update_statement_positioned" },
1363 { OSQLParseNode::update_statement_searched
, "update_statement_searched" },
1364 { OSQLParseNode::assignment_commalist
, "assignment_commalist" },
1365 { OSQLParseNode::assignment
, "assignment" },
1366 { OSQLParseNode::values_or_query_spec
, "values_or_query_spec" },
1367 { OSQLParseNode::insert_statement
, "insert_statement" },
1368 { OSQLParseNode::insert_atom_commalist
, "insert_atom_commalist" },
1369 { OSQLParseNode::insert_atom
, "insert_atom" },
1370 { OSQLParseNode::predicate_check
, "predicate_check" },
1371 { OSQLParseNode::from_clause
, "from_clause" },
1372 { OSQLParseNode::qualified_join
, "qualified_join" },
1373 { OSQLParseNode::cross_union
, "cross_union" },
1374 { OSQLParseNode::select_sublist
, "select_sublist" },
1375 { OSQLParseNode::derived_column
, "derived_column" },
1376 { OSQLParseNode::column_val
, "column_val" },
1377 { OSQLParseNode::set_fct_spec
, "set_fct_spec" },
1378 { OSQLParseNode::boolean_term
, "boolean_term" },
1379 { OSQLParseNode::boolean_primary
, "boolean_primary" },
1380 { OSQLParseNode::num_value_exp
, "num_value_exp" },
1381 { OSQLParseNode::join_type
, "join_type" },
1382 { OSQLParseNode::position_exp
, "position_exp" },
1383 { OSQLParseNode::extract_exp
, "extract_exp" },
1384 { OSQLParseNode::length_exp
, "length_exp" },
1385 { OSQLParseNode::char_value_fct
, "char_value_fct" },
1386 { OSQLParseNode::odbc_call_spec
, "odbc_call_spec" },
1387 { OSQLParseNode::in_predicate
, "in_predicate" },
1388 { OSQLParseNode::existence_test
, "existence_test" },
1389 { OSQLParseNode::unique_test
, "unique_test" },
1390 { OSQLParseNode::all_or_any_predicate
, "all_or_any_predicate" },
1391 { OSQLParseNode::named_columns_join
, "named_columns_join" },
1392 { OSQLParseNode::join_condition
, "join_condition" },
1393 { OSQLParseNode::joined_table
, "joined_table" },
1394 { OSQLParseNode::boolean_factor
, "boolean_factor" },
1395 { OSQLParseNode::sql_not
, "sql_not" },
1396 { OSQLParseNode::boolean_test
, "boolean_test" },
1397 { OSQLParseNode::manipulative_statement
, "manipulative_statement" },
1398 { OSQLParseNode::subquery
, "subquery" },
1399 { OSQLParseNode::value_exp_commalist
, "value_exp_commalist" },
1400 { OSQLParseNode::odbc_fct_spec
, "odbc_fct_spec" },
1401 { OSQLParseNode::union_statement
, "union_statement" },
1402 { OSQLParseNode::outer_join_type
, "outer_join_type" },
1403 { OSQLParseNode::char_value_exp
, "char_value_exp" },
1404 { OSQLParseNode::term
, "term" },
1405 { OSQLParseNode::value_exp_primary
, "value_exp_primary" },
1406 { OSQLParseNode::value_exp
, "value_exp" },
1407 { OSQLParseNode::selection
, "selection" },
1408 { OSQLParseNode::fold
, "fold" },
1409 { OSQLParseNode::char_substring_fct
, "char_substring_fct" },
1410 { OSQLParseNode::factor
, "factor" },
1411 { OSQLParseNode::base_table_def
, "base_table_def" },
1412 { OSQLParseNode::base_table_element_commalist
, "base_table_element_commalist" },
1413 { OSQLParseNode::data_type
, "data_type" },
1414 { OSQLParseNode::column_def
, "column_def" },
1415 { OSQLParseNode::table_node
, "table_node" },
1416 { OSQLParseNode::as
, "as" },
1417 { OSQLParseNode::op_column_commalist
, "op_column_commalist" },
1418 { OSQLParseNode::table_primary_as_range_column
, "table_primary_as_range_column" }
1420 size_t nRuleMapCount
= sizeof( aRuleDescriptions
) / sizeof( aRuleDescriptions
[0] );
1421 OSL_ENSURE( nRuleMapCount
== size_t( OSQLParseNode::rule_count
), "OSQLParser::OSQLParser: added a new rule? Adjust this map!" );
1423 for ( size_t mapEntry
= 0; mapEntry
< nRuleMapCount
; ++mapEntry
)
1425 // look up the rule description in the our identifier map
1426 sal_uInt32 nParserRuleID
= StrToRuleID( aRuleDescriptions
[ mapEntry
].sRuleName
);
1427 // map the parser's rule ID to the OSQLParseNode::Rule
1428 s_aReverseRuleIDLookup
[ nParserRuleID
] = aRuleDescriptions
[ mapEntry
].eRule
;
1429 // and map the OSQLParseNode::Rule to the parser's rule ID
1430 s_nRuleIDs
[ aRuleDescriptions
[ mapEntry
].eRule
] = nParserRuleID
;
1435 if (m_pContext
== NULL
)
1436 // take the default context
1437 m_pContext
= &s_aDefaultContext
;
1439 m_pData
->aLocale
= m_pContext
->getPreferredLocale();
1442 //-----------------------------------------------------------------------------
1443 OSQLParser::~OSQLParser()
1446 ::osl::MutexGuard
aGuard(getMutex());
1447 OSL_ENSURE(s_nRefCount
> 0, "OSQLParser::~OSQLParser() : suspicious call : have a refcount of 0 !");
1450 s_pScanner
->setScanner(sal_True
);
1454 delete s_pGarbageCollector
;
1455 s_pGarbageCollector
= NULL
;
1456 // is only set the first time so we should delete it only when there no more instances
1457 s_xLocaleData
= NULL
;
1460 s_aReverseRuleIDLookup
.swap( aEmpty
);
1462 m_pParseTree
= NULL
;
1465 // -----------------------------------------------------------------------------
1466 void OSQLParseNode::substituteParameterNames(OSQLParseNode
* _pNode
)
1468 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::substituteParameterNames" );
1469 sal_Int32 nCount
= _pNode
->count();
1470 for(sal_Int32 i
=0;i
< nCount
;++i
)
1472 OSQLParseNode
* pChildNode
= _pNode
->getChild(i
);
1473 if(SQL_ISRULE(pChildNode
,parameter
) && pChildNode
->count() > 1)
1475 OSQLParseNode
* pNewNode
= new OSQLParseNode(::rtl::OUString::createFromAscii("?") ,SQL_NODE_PUNCTUATION
,0);
1476 delete pChildNode
->replace(pChildNode
->getChild(0),pNewNode
);
1477 sal_Int32 nChildCount
= pChildNode
->count();
1478 for(sal_Int32 j
=1;j
< nChildCount
;++j
)
1479 delete pChildNode
->removeAt(1);
1482 substituteParameterNames(pChildNode
);
1486 // -----------------------------------------------------------------------------
1487 bool OSQLParser::extractDate(OSQLParseNode
* pLiteral
,double& _rfValue
)
1489 Reference
< XNumberFormatsSupplier
> xFormatSup
= m_xFormatter
->getNumberFormatsSupplier();
1490 Reference
< XNumberFormatTypes
> xFormatTypes
;
1491 if ( xFormatSup
.is() )
1492 xFormatTypes
= xFormatTypes
.query( xFormatSup
->getNumberFormats() );
1494 // if there is no format key, yet, make sure we have a feasible one for our locale
1497 if ( !m_nFormatKey
&& xFormatTypes
.is() )
1498 m_nFormatKey
= ::dbtools::getDefaultNumberFormat( m_xField
, xFormatTypes
, m_pData
->aLocale
);
1500 catch( Exception
& ) { }
1501 ::rtl::OUString sValue
= pLiteral
->getTokenValue();
1502 sal_Int32 nTryFormat
= m_nFormatKey
;
1503 bool bSuccess
= lcl_saveConvertToNumber( m_xFormatter
, nTryFormat
, sValue
, _rfValue
);
1505 // If our format key didn't do, try the default date format for our locale.
1506 if ( !bSuccess
&& xFormatTypes
.is() )
1510 nTryFormat
= xFormatTypes
->getStandardFormat( NumberFormat::DATE
, m_pData
->aLocale
);
1512 catch( Exception
& ) { }
1513 bSuccess
= lcl_saveConvertToNumber( m_xFormatter
, nTryFormat
, sValue
, _rfValue
);
1516 // if this also didn't do, try ISO format
1517 if ( !bSuccess
&& xFormatTypes
.is() )
1521 nTryFormat
= xFormatTypes
->getFormatIndex( NumberFormatIndex::DATE_DIN_YYYYMMDD
, m_pData
->aLocale
);
1523 catch( Exception
& ) { }
1524 bSuccess
= lcl_saveConvertToNumber( m_xFormatter
, nTryFormat
, sValue
, _rfValue
);
1527 // if this also didn't do, try fallback date format (en-US)
1530 nTryFormat
= m_nDateFormatKey
;
1531 bSuccess
= lcl_saveConvertToNumber( m_xFormatter
, nTryFormat
, sValue
, _rfValue
);
1535 // -----------------------------------------------------------------------------
1536 OSQLParseNode
* OSQLParser::buildDate(sal_Int32 _nType
,OSQLParseNode
*& pLiteral
)
1538 // try converting the string into a date, according to our format key
1539 double fValue
= 0.0;
1540 OSQLParseNode
* pFCTNode
= NULL
;
1542 if ( extractDate(pLiteral
,fValue
) )
1543 pFCTNode
= buildNode_Date( fValue
, _nType
);
1549 m_sErrorMessage
= m_pContext
->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE
);
1553 // -----------------------------------------------------------------------------
1554 //-----------------------------------------------------------------------------
1555 OSQLParseNode::OSQLParseNode(const sal_Char
* pNewValue
,
1556 SQLNodeType eNewNodeType
,
1557 sal_uInt32 nNewNodeID
)
1559 ,m_aNodeValue(pNewValue
,strlen(pNewValue
),RTL_TEXTENCODING_UTF8
)
1560 ,m_eNodeType(eNewNodeType
)
1561 ,m_nNodeID(nNewNodeID
)
1563 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1565 OSL_ENSURE(m_eNodeType
>= SQL_NODE_RULE
&& m_eNodeType
<= SQL_NODE_CONCAT
,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1567 //-----------------------------------------------------------------------------
1568 OSQLParseNode::OSQLParseNode(const ::rtl::OString
&_rNewValue
,
1569 SQLNodeType eNewNodeType
,
1570 sal_uInt32 nNewNodeID
)
1572 ,m_aNodeValue(_rNewValue
,_rNewValue
.getLength(),RTL_TEXTENCODING_UTF8
)
1573 ,m_eNodeType(eNewNodeType
)
1574 ,m_nNodeID(nNewNodeID
)
1576 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1578 OSL_ENSURE(m_eNodeType
>= SQL_NODE_RULE
&& m_eNodeType
<= SQL_NODE_CONCAT
,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1580 //-----------------------------------------------------------------------------
1581 OSQLParseNode::OSQLParseNode(const sal_Unicode
* pNewValue
,
1582 SQLNodeType eNewNodeType
,
1583 sal_uInt32 nNewNodeID
)
1585 ,m_aNodeValue(pNewValue
)
1586 ,m_eNodeType(eNewNodeType
)
1587 ,m_nNodeID(nNewNodeID
)
1589 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1591 OSL_ENSURE(m_eNodeType
>= SQL_NODE_RULE
&& m_eNodeType
<= SQL_NODE_CONCAT
,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1593 //-----------------------------------------------------------------------------
1594 OSQLParseNode::OSQLParseNode(const ::rtl::OUString
&_rNewValue
,
1595 SQLNodeType eNewNodeType
,
1596 sal_uInt32 nNewNodeID
)
1598 ,m_aNodeValue(_rNewValue
)
1599 ,m_eNodeType(eNewNodeType
)
1600 ,m_nNodeID(nNewNodeID
)
1602 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1604 OSL_ENSURE(m_eNodeType
>= SQL_NODE_RULE
&& m_eNodeType
<= SQL_NODE_CONCAT
,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1606 //-----------------------------------------------------------------------------
1607 OSQLParseNode::OSQLParseNode(const OSQLParseNode
& rParseNode
)
1609 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1611 // klemm den getParent auf NULL
1614 // kopiere die member
1615 m_aNodeValue
= rParseNode
.m_aNodeValue
;
1616 m_eNodeType
= rParseNode
.m_eNodeType
;
1617 m_nNodeID
= rParseNode
.m_nNodeID
;
1620 // denk dran, dass von Container abgeleitet wurde, laut SV-Help erzeugt
1621 // copy-Constructor des Containers einen neuen Container mit den gleichen
1622 // Zeigern als Inhalt -> d.h. nach dem Kopieren des Container wird fuer
1623 // alle Zeiger ungleich NULL eine Kopie hergestellt und anstelle des alten
1624 // Zeigers wieder eingehangen.
1626 // wenn kein Blatt, dann SubTrees bearbeiten
1627 for (OSQLParseNodes::const_iterator i
= rParseNode
.m_aChildren
.begin();
1628 i
!= rParseNode
.m_aChildren
.end(); i
++)
1629 append(new OSQLParseNode(**i
));
1631 // -----------------------------------------------------------------------------
1632 //-----------------------------------------------------------------------------
1633 OSQLParseNode
& OSQLParseNode::operator=(const OSQLParseNode
& rParseNode
)
1635 if (this != &rParseNode
)
1637 // kopiere die member - pParent bleibt der alte
1638 m_aNodeValue
= rParseNode
.m_aNodeValue
;
1639 m_eNodeType
= rParseNode
.m_eNodeType
;
1640 m_nNodeID
= rParseNode
.m_nNodeID
;
1642 for (OSQLParseNodes::const_iterator i
= m_aChildren
.begin();
1643 i
!= m_aChildren
.end(); i
++)
1646 m_aChildren
.clear();
1648 for (OSQLParseNodes::const_iterator j
= rParseNode
.m_aChildren
.begin();
1649 j
!= rParseNode
.m_aChildren
.end(); j
++)
1650 append(new OSQLParseNode(**j
));
1655 //-----------------------------------------------------------------------------
1656 sal_Bool
OSQLParseNode::operator==(OSQLParseNode
& rParseNode
) const
1658 // die member muessen gleich sein
1659 sal_Bool bResult
= (m_nNodeID
== rParseNode
.m_nNodeID
) &&
1660 (m_eNodeType
== rParseNode
.m_eNodeType
) &&
1661 (m_aNodeValue
== rParseNode
.m_aNodeValue
) &&
1662 count() == rParseNode
.count();
1664 // Parameters are not equal!
1665 bResult
= bResult
&& !SQL_ISRULE(this, parameter
);
1668 for (sal_uInt32 i
=0; bResult
&& i
< count(); i
++)
1669 bResult
= *getChild(i
) == *rParseNode
.getChild(i
);
1674 //-----------------------------------------------------------------------------
1675 OSQLParseNode::~OSQLParseNode()
1677 for (OSQLParseNodes::const_iterator i
= m_aChildren
.begin();
1678 i
!= m_aChildren
.end(); i
++)
1680 m_aChildren
.clear();
1683 //-----------------------------------------------------------------------------
1684 void OSQLParseNode::append(OSQLParseNode
* pNewNode
)
1686 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::append" );
1688 OSL_ENSURE(pNewNode
!= NULL
, "OSQLParseNode: ungueltiger NewSubTree");
1689 OSL_ENSURE(pNewNode
->getParent() == NULL
, "OSQLParseNode: Knoten ist kein Waise");
1690 OSL_ENSURE(::std::find(m_aChildren
.begin(), m_aChildren
.end(), pNewNode
) == m_aChildren
.end(),
1691 "OSQLParseNode::append() Node already element of parent");
1693 // stelle Verbindung zum getParent her:
1694 pNewNode
->setParent( this );
1695 // und haenge den SubTree hinten an
1696 m_aChildren
.push_back(pNewNode
);
1698 // -----------------------------------------------------------------------------
1699 sal_Bool
OSQLParseNode::addDateValue(::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
1701 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::addDateValue" );
1702 // special display for date/time values
1703 if (SQL_ISRULE(this,set_fct_spec
) && SQL_ISPUNCTUATION(m_aChildren
[0],"{"))
1705 const OSQLParseNode
* pODBCNode
= m_aChildren
[1];
1706 const OSQLParseNode
* pODBCNodeChild
= pODBCNode
->m_aChildren
[0];
1708 if (pODBCNodeChild
->getNodeType() == SQL_NODE_KEYWORD
&& (
1709 SQL_ISTOKEN(pODBCNodeChild
, D
) ||
1710 SQL_ISTOKEN(pODBCNodeChild
, T
) ||
1711 SQL_ISTOKEN(pODBCNodeChild
, TS
) ))
1713 ::rtl::OUString
suQuote(::rtl::OUString::createFromAscii("'"));
1714 if (rParam
.bPredicate
)
1716 if (rParam
.aMetaData
.shouldEscapeDateTime())
1718 suQuote
= ::rtl::OUString::createFromAscii("#");
1723 if (rParam
.aMetaData
.shouldEscapeDateTime())
1725 // suQuote = ::rtl::OUString::createFromAscii("'");
1730 if (rString
.getLength())
1731 rString
.appendAscii(" ");
1732 rString
.append(suQuote
);
1733 const ::rtl::OUString sTokenValue
= pODBCNode
->m_aChildren
[1]->getTokenValue();
1734 if (SQL_ISTOKEN(pODBCNodeChild
, D
))
1736 rString
.append(rParam
.bPredicate
? convertDateString(rParam
, sTokenValue
) : sTokenValue
);
1738 else if (SQL_ISTOKEN(pODBCNodeChild
, T
))
1740 rString
.append(rParam
.bPredicate
? convertTimeString(rParam
, sTokenValue
) : sTokenValue
);
1744 rString
.append(rParam
.bPredicate
? convertDateTimeString(rParam
, sTokenValue
) : sTokenValue
);
1746 rString
.append(suQuote
);
1752 // -----------------------------------------------------------------------------
1753 void OSQLParseNode::replaceNodeValue(const ::rtl::OUString
& rTableAlias
,const ::rtl::OUString
& rColumnName
)
1755 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replaceNodeValue" );
1756 for (sal_uInt32 i
=0;i
<count();++i
)
1758 if (SQL_ISRULE(this,column_ref
) && count() == 1 && getChild(0)->getTokenValue() == rColumnName
)
1760 OSQLParseNode
* pCol
= removeAt((sal_uInt32
)0);
1761 append(new OSQLParseNode(rTableAlias
,SQL_NODE_NAME
));
1762 append(new OSQLParseNode(::rtl::OUString::createFromAscii("."),SQL_NODE_PUNCTUATION
));
1766 getChild(i
)->replaceNodeValue(rTableAlias
,rColumnName
);
1769 //-----------------------------------------------------------------------------
1770 OSQLParseNode
* OSQLParseNode::getByRule(OSQLParseNode::Rule eRule
) const
1772 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getByRule" );
1773 OSQLParseNode
* pRetNode
= 0;
1774 if (isRule() && OSQLParser::RuleID(eRule
) == getRuleID())
1775 pRetNode
= (OSQLParseNode
*)this;
1778 for (OSQLParseNodes::const_iterator i
= m_aChildren
.begin();
1779 !pRetNode
&& i
!= m_aChildren
.end(); i
++)
1780 pRetNode
= (*i
)->getByRule(eRule
);
1784 //-----------------------------------------------------------------------------
1785 OSQLParseNode
* MakeANDNode(OSQLParseNode
*pLeftLeaf
,OSQLParseNode
*pRightLeaf
)
1787 OSQLParseNode
* pNewNode
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_term
));
1788 pNewNode
->append(pLeftLeaf
);
1789 pNewNode
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("AND"),SQL_NODE_KEYWORD
,SQL_TOKEN_AND
));
1790 pNewNode
->append(pRightLeaf
);
1793 //-----------------------------------------------------------------------------
1794 OSQLParseNode
* MakeORNode(OSQLParseNode
*pLeftLeaf
,OSQLParseNode
*pRightLeaf
)
1796 OSQLParseNode
* pNewNode
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::search_condition
));
1797 pNewNode
->append(pLeftLeaf
);
1798 pNewNode
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("OR"),SQL_NODE_KEYWORD
,SQL_TOKEN_OR
));
1799 pNewNode
->append(pRightLeaf
);
1802 //-----------------------------------------------------------------------------
1803 void OSQLParseNode::disjunctiveNormalForm(OSQLParseNode
*& pSearchCondition
)
1805 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::disjunctiveNormalForm" );
1806 if(!pSearchCondition
) // no where condition at entry point
1809 OSQLParseNode::absorptions(pSearchCondition
);
1810 // '(' search_condition ')'
1811 if (SQL_ISRULE(pSearchCondition
,boolean_primary
))
1813 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(1);
1814 disjunctiveNormalForm(pLeft
);
1816 // search_condition SQL_TOKEN_OR boolean_term
1817 else if (SQL_ISRULE(pSearchCondition
,search_condition
))
1819 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
1820 disjunctiveNormalForm(pLeft
);
1822 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
1823 disjunctiveNormalForm(pRight
);
1825 // boolean_term SQL_TOKEN_AND boolean_factor
1826 else if (SQL_ISRULE(pSearchCondition
,boolean_term
))
1828 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
1829 disjunctiveNormalForm(pLeft
);
1831 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
1832 disjunctiveNormalForm(pRight
);
1834 OSQLParseNode
* pNewNode
= NULL
;
1835 // '(' search_condition ')' on left side
1836 if(pLeft
->count() == 3 && SQL_ISRULE(pLeft
,boolean_primary
) && SQL_ISRULE(pLeft
->getChild(1),search_condition
))
1838 // and-or tree on left side
1839 OSQLParseNode
* pOr
= pLeft
->getChild(1);
1840 OSQLParseNode
* pNewLeft
= NULL
;
1841 OSQLParseNode
* pNewRight
= NULL
;
1843 // cut right from parent
1844 pSearchCondition
->removeAt(2);
1846 pNewRight
= MakeANDNode(pOr
->removeAt(2) ,pRight
);
1847 pNewLeft
= MakeANDNode(pOr
->removeAt((sal_uInt32
)0) ,new OSQLParseNode(*pRight
));
1848 pNewNode
= MakeORNode(pNewLeft
,pNewRight
);
1849 // and append new Node
1850 replaceAndReset(pSearchCondition
,pNewNode
);
1852 disjunctiveNormalForm(pSearchCondition
);
1854 else if(pRight
->count() == 3 && SQL_ISRULE(pRight
,boolean_primary
) && SQL_ISRULE(pRight
->getChild(1),search_condition
))
1855 { // '(' search_condition ')' on right side
1856 // and-or tree on right side
1858 OSQLParseNode
* pOr
= pRight
->getChild(1);
1859 OSQLParseNode
* pNewLeft
= NULL
;
1860 OSQLParseNode
* pNewRight
= NULL
;
1862 // cut left from parent
1863 pSearchCondition
->removeAt((sal_uInt32
)0);
1865 pNewRight
= MakeANDNode(pLeft
,pOr
->removeAt(2));
1866 pNewLeft
= MakeANDNode(new OSQLParseNode(*pLeft
),pOr
->removeAt((sal_uInt32
)0));
1867 pNewNode
= MakeORNode(pNewLeft
,pNewRight
);
1869 // and append new Node
1870 replaceAndReset(pSearchCondition
,pNewNode
);
1871 disjunctiveNormalForm(pSearchCondition
);
1873 else if(SQL_ISRULE(pLeft
,boolean_primary
) && (!SQL_ISRULE(pLeft
->getChild(1),search_condition
) || !SQL_ISRULE(pLeft
->getChild(1),boolean_term
)))
1874 pSearchCondition
->replace(pLeft
, pLeft
->removeAt(1));
1875 else if(SQL_ISRULE(pRight
,boolean_primary
) && (!SQL_ISRULE(pRight
->getChild(1),search_condition
) || !SQL_ISRULE(pRight
->getChild(1),boolean_term
)))
1876 pSearchCondition
->replace(pRight
, pRight
->removeAt(1));
1879 //-----------------------------------------------------------------------------
1880 void OSQLParseNode::negateSearchCondition(OSQLParseNode
*& pSearchCondition
,sal_Bool bNegate
)
1882 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::negateSearchCondition" );
1883 if(!pSearchCondition
) // no where condition at entry point
1885 // '(' search_condition ')'
1886 if (pSearchCondition
->count() == 3 && SQL_ISRULE(pSearchCondition
,boolean_primary
))
1888 OSQLParseNode
* pRight
= pSearchCondition
->getChild(1);
1889 negateSearchCondition(pRight
,bNegate
);
1891 // search_condition SQL_TOKEN_OR boolean_term
1892 else if (SQL_ISRULE(pSearchCondition
,search_condition
))
1894 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
1895 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
1898 OSQLParseNode
* pNewNode
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_term
));
1899 pNewNode
->append(pSearchCondition
->removeAt((sal_uInt32
)0));
1900 pNewNode
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("AND"),SQL_NODE_KEYWORD
,SQL_TOKEN_AND
));
1901 pNewNode
->append(pSearchCondition
->removeAt((sal_uInt32
)1));
1902 replaceAndReset(pSearchCondition
,pNewNode
);
1904 pLeft
= pNewNode
->getChild(0);
1905 pRight
= pNewNode
->getChild(2);
1908 negateSearchCondition(pLeft
,bNegate
);
1909 negateSearchCondition(pRight
,bNegate
);
1911 // boolean_term SQL_TOKEN_AND boolean_factor
1912 else if (SQL_ISRULE(pSearchCondition
,boolean_term
))
1914 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
1915 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
1918 OSQLParseNode
* pNewNode
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::search_condition
));
1919 pNewNode
->append(pSearchCondition
->removeAt((sal_uInt32
)0));
1920 pNewNode
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("OR"),SQL_NODE_KEYWORD
,SQL_TOKEN_OR
));
1921 pNewNode
->append(pSearchCondition
->removeAt((sal_uInt32
)1));
1922 replaceAndReset(pSearchCondition
,pNewNode
);
1924 pLeft
= pNewNode
->getChild(0);
1925 pRight
= pNewNode
->getChild(2);
1928 negateSearchCondition(pLeft
,bNegate
);
1929 negateSearchCondition(pRight
,bNegate
);
1931 // SQL_TOKEN_NOT ( boolean_test )
1932 else if (SQL_ISRULE(pSearchCondition
,boolean_factor
))
1934 OSQLParseNode
*pNot
= pSearchCondition
->removeAt((sal_uInt32
)0);
1936 OSQLParseNode
*pBooleanTest
= pSearchCondition
->removeAt((sal_uInt32
)0);
1937 // TODO is this needed // pBooleanTest->setParent(NULL);
1938 replaceAndReset(pSearchCondition
,pBooleanTest
);
1941 negateSearchCondition(pSearchCondition
,sal_True
); // negate all deeper values
1943 // row_value_constructor comparison row_value_constructor
1944 // row_value_constructor comparison any_all_some subquery
1945 else if(bNegate
&& (SQL_ISRULE(pSearchCondition
,comparison_predicate
) || SQL_ISRULE(pSearchCondition
,all_or_any_predicate
)))
1947 OSQLParseNode
* pComparison
= pSearchCondition
->getChild(1);
1948 OSQLParseNode
* pNewComparison
= NULL
;
1949 switch(pComparison
->getNodeType())
1951 case SQL_NODE_EQUAL
:
1952 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii("<>"),SQL_NODE_NOTEQUAL
,SQL_NOTEQUAL
);
1955 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii(">="),SQL_NODE_GREATEQ
,SQL_GREATEQ
);
1957 case SQL_NODE_GREAT
:
1958 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii("<="),SQL_NODE_LESSEQ
,SQL_LESSEQ
);
1960 case SQL_NODE_LESSEQ
:
1961 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii(">"),SQL_NODE_GREAT
,SQL_GREAT
);
1963 case SQL_NODE_GREATEQ
:
1964 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii("<"),SQL_NODE_LESS
,SQL_LESS
);
1966 case SQL_NODE_NOTEQUAL
:
1967 pNewComparison
= new OSQLParseNode(::rtl::OUString::createFromAscii("="),SQL_NODE_EQUAL
,SQL_EQUAL
);
1970 OSL_ENSURE( false, "OSQLParseNode::negateSearchCondition: unexpected node type!" );
1973 pSearchCondition
->replace(pComparison
, pNewComparison
);
1977 else if(bNegate
&& (SQL_ISRULE(pSearchCondition
,test_for_null
) || SQL_ISRULE(pSearchCondition
,in_predicate
) ||
1978 SQL_ISRULE(pSearchCondition
,between_predicate
) || SQL_ISRULE(pSearchCondition
,boolean_test
) ))
1980 sal_uInt32 nNotPos
= 0;
1981 // row_value_constructor not SQL_TOKEN_IN in_predicate_value
1982 // row_value_constructor not SQL_TOKEN_BETWEEN row_value_constructor SQL_TOKEN_AND row_value_constructor
1983 if ( SQL_ISRULE( pSearchCondition
, in_predicate
)
1984 || SQL_ISRULE( pSearchCondition
, between_predicate
)
1987 // row_value_constructor SQL_TOKEN_IS not SQL_TOKEN_NULL
1988 // boolean_primary SQL_TOKEN_IS not truth_value
1989 else if ( SQL_ISRULE( pSearchCondition
, test_for_null
)
1990 || SQL_ISRULE( pSearchCondition
, boolean_test
)
1994 OSQLParseNode
* pNot
= pSearchCondition
->getChild(nNotPos
);
1995 OSQLParseNode
* pNotNot
= NULL
;
1997 pNotNot
= new OSQLParseNode(::rtl::OUString::createFromAscii("NOT"),SQL_NODE_KEYWORD
,SQL_TOKEN_NOT
);
1999 pNotNot
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::sql_not
));
2000 pSearchCondition
->replace(pNot
, pNotNot
);
2003 else if(bNegate
&& (SQL_ISRULE(pSearchCondition
,like_predicate
)))
2005 OSQLParseNode
* pCheckForNOT
= pSearchCondition
->getChild( 1 );
2006 if ( SQL_ISTOKEN(pCheckForNOT
,NOT
) )
2007 delete pSearchCondition
->removeAt( 1 );
2010 OSQLParseNode
* pNot
= new OSQLParseNode( ::rtl::OUString::createFromAscii( "NOT" ), SQL_NODE_KEYWORD
, SQL_TOKEN_NOT
);
2011 pSearchCondition
->insert( 1, pNot
);
2015 //-----------------------------------------------------------------------------
2016 void OSQLParseNode::eraseBraces(OSQLParseNode
*& pSearchCondition
)
2018 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::eraseBraces" );
2019 if (pSearchCondition
&& (SQL_ISRULE(pSearchCondition
,boolean_primary
) || (pSearchCondition
->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition
->getChild(0),"(") &&
2020 SQL_ISPUNCTUATION(pSearchCondition
->getChild(2),")"))))
2022 OSQLParseNode
* pRight
= pSearchCondition
->getChild(1);
2023 absorptions(pRight
);
2024 // if child is not a or or and tree then delete () around child
2025 if(!(SQL_ISRULE(pSearchCondition
->getChild(1),boolean_term
) || SQL_ISRULE(pSearchCondition
->getChild(1),search_condition
)) ||
2026 SQL_ISRULE(pSearchCondition
->getChild(1),boolean_term
) || // and can always stand without ()
2027 (SQL_ISRULE(pSearchCondition
->getChild(1),search_condition
) && SQL_ISRULE(pSearchCondition
->getParent(),search_condition
)))
2029 OSQLParseNode
* pNode
= pSearchCondition
->removeAt(1);
2030 replaceAndReset(pSearchCondition
,pNode
);
2034 //-----------------------------------------------------------------------------
2035 void OSQLParseNode::absorptions(OSQLParseNode
*& pSearchCondition
)
2037 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::absorptions" );
2038 if(!pSearchCondition
) // no where condition at entry point
2041 eraseBraces(pSearchCondition
);
2043 if(SQL_ISRULE(pSearchCondition
,boolean_term
) || SQL_ISRULE(pSearchCondition
,search_condition
))
2045 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
2047 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
2048 absorptions(pRight
);
2051 sal_uInt32 nPos
= 0;
2052 // a and a || a or a
2053 OSQLParseNode
* pNewNode
= NULL
;
2054 if(( SQL_ISRULE(pSearchCondition
,boolean_term
) || SQL_ISRULE(pSearchCondition
,search_condition
))
2055 && *pSearchCondition
->getChild(0) == *pSearchCondition
->getChild(2))
2057 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)0);
2058 replaceAndReset(pSearchCondition
,pNewNode
);
2060 // (a or b) and a || ( b or c ) and a
2061 // a and ( a or b) || a and ( b or c )
2062 else if ( SQL_ISRULE(pSearchCondition
,boolean_term
)
2064 ( SQL_ISRULE(pSearchCondition
->getChild(nPos
= 0),boolean_primary
)
2065 || SQL_ISRULE(pSearchCondition
->getChild(nPos
),search_condition
)
2067 || ( SQL_ISRULE(pSearchCondition
->getChild(nPos
= 2),boolean_primary
)
2068 || SQL_ISRULE(pSearchCondition
->getChild(nPos
),search_condition
)
2073 OSQLParseNode
* p2ndSearch
= pSearchCondition
->getChild(nPos
);
2074 if ( SQL_ISRULE(p2ndSearch
,boolean_primary
) )
2075 p2ndSearch
= p2ndSearch
->getChild(1);
2077 if ( *p2ndSearch
->getChild(0) == *pSearchCondition
->getChild(2-nPos
) )
2079 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)0);
2080 replaceAndReset(pSearchCondition
,pNewNode
);
2083 else if ( *p2ndSearch
->getChild(2) == *pSearchCondition
->getChild(2-nPos
) )
2085 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)2);
2086 replaceAndReset(pSearchCondition
,pNewNode
);
2088 else if ( p2ndSearch
->getByRule(OSQLParseNode::boolean_term
) )
2090 // a and ( b or c ) -> ( a and b ) or ( a and c )
2091 // ( b or c ) and a -> ( a and b ) or ( a and c )
2092 OSQLParseNode
* pC
= p2ndSearch
->removeAt((sal_uInt32
)2);
2093 OSQLParseNode
* pB
= p2ndSearch
->removeAt((sal_uInt32
)0);
2094 OSQLParseNode
* pA
= pSearchCondition
->removeAt((sal_uInt32
)2-nPos
);
2096 OSQLParseNode
* p1stAnd
= MakeANDNode(pA
,pB
);
2097 OSQLParseNode
* p2ndAnd
= MakeANDNode(new OSQLParseNode(*pA
),pC
);
2098 pNewNode
= MakeORNode(p1stAnd
,p2ndAnd
);
2099 replaceAndReset(pSearchCondition
,pNewNode
);
2102 // a or a and b || a or b and a
2103 else if(SQL_ISRULE(pSearchCondition
,search_condition
) && SQL_ISRULE(pSearchCondition
->getChild(2),boolean_term
))
2105 if(*pSearchCondition
->getChild(2)->getChild(0) == *pSearchCondition
->getChild(0))
2107 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)0);
2108 replaceAndReset(pSearchCondition
,pNewNode
);
2110 else if(*pSearchCondition
->getChild(2)->getChild(2) == *pSearchCondition
->getChild(0))
2112 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)0);
2113 replaceAndReset(pSearchCondition
,pNewNode
);
2116 // a and b or a || b and a or a
2117 else if(SQL_ISRULE(pSearchCondition
,search_condition
) && SQL_ISRULE(pSearchCondition
->getChild(0),boolean_term
))
2119 if(*pSearchCondition
->getChild(0)->getChild(0) == *pSearchCondition
->getChild(2))
2121 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)2);
2122 replaceAndReset(pSearchCondition
,pNewNode
);
2124 else if(*pSearchCondition
->getChild(0)->getChild(2) == *pSearchCondition
->getChild(2))
2126 pNewNode
= pSearchCondition
->removeAt((sal_uInt32
)2);
2127 replaceAndReset(pSearchCondition
,pNewNode
);
2130 eraseBraces(pSearchCondition
);
2132 //-----------------------------------------------------------------------------
2133 void OSQLParseNode::compress(OSQLParseNode
*&pSearchCondition
)
2135 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::compress" );
2136 if(!pSearchCondition
) // no where condition at entry point
2139 OSQLParseNode::eraseBraces(pSearchCondition
);
2141 if(SQL_ISRULE(pSearchCondition
,boolean_term
) || SQL_ISRULE(pSearchCondition
,search_condition
))
2143 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0);
2146 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2);
2149 else if( SQL_ISRULE(pSearchCondition
,boolean_primary
) || (pSearchCondition
->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition
->getChild(0),"(") &&
2150 SQL_ISPUNCTUATION(pSearchCondition
->getChild(2),")")))
2152 OSQLParseNode
* pRight
= pSearchCondition
->getChild(1);
2154 // if child is not a or or and tree then delete () around child
2155 if(!(SQL_ISRULE(pSearchCondition
->getChild(1),boolean_term
) || SQL_ISRULE(pSearchCondition
->getChild(1),search_condition
)) ||
2156 (SQL_ISRULE(pSearchCondition
->getChild(1),boolean_term
) && SQL_ISRULE(pSearchCondition
->getParent(),boolean_term
)) ||
2157 (SQL_ISRULE(pSearchCondition
->getChild(1),search_condition
) && SQL_ISRULE(pSearchCondition
->getParent(),search_condition
)))
2159 OSQLParseNode
* pNode
= pSearchCondition
->removeAt(1);
2160 replaceAndReset(pSearchCondition
,pNode
);
2164 // or with two and trees where one element of the and trees are equal
2165 if(SQL_ISRULE(pSearchCondition
,search_condition
) && SQL_ISRULE(pSearchCondition
->getChild(0),boolean_term
) && SQL_ISRULE(pSearchCondition
->getChild(2),boolean_term
))
2167 if(*pSearchCondition
->getChild(0)->getChild(0) == *pSearchCondition
->getChild(2)->getChild(0))
2169 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0)->removeAt(2);
2170 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2)->removeAt(2);
2171 OSQLParseNode
* pNode
= MakeORNode(pLeft
,pRight
);
2173 OSQLParseNode
* pNewRule
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_primary
));
2174 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION
));
2175 pNewRule
->append(pNode
);
2176 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION
));
2178 OSQLParseNode::eraseBraces(pLeft
);
2179 OSQLParseNode::eraseBraces(pRight
);
2181 pNode
= MakeANDNode(pSearchCondition
->getChild(0)->removeAt((sal_uInt32
)0),pNewRule
);
2182 replaceAndReset(pSearchCondition
,pNode
);
2184 else if(*pSearchCondition
->getChild(0)->getChild(2) == *pSearchCondition
->getChild(2)->getChild(0))
2186 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0)->removeAt((sal_uInt32
)0);
2187 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2)->removeAt(2);
2188 OSQLParseNode
* pNode
= MakeORNode(pLeft
,pRight
);
2190 OSQLParseNode
* pNewRule
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_primary
));
2191 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION
));
2192 pNewRule
->append(pNode
);
2193 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION
));
2195 OSQLParseNode::eraseBraces(pLeft
);
2196 OSQLParseNode::eraseBraces(pRight
);
2198 pNode
= MakeANDNode(pSearchCondition
->getChild(0)->removeAt(1),pNewRule
);
2199 replaceAndReset(pSearchCondition
,pNode
);
2201 else if(*pSearchCondition
->getChild(0)->getChild(0) == *pSearchCondition
->getChild(2)->getChild(2))
2203 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0)->removeAt(2);
2204 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2)->removeAt((sal_uInt32
)0);
2205 OSQLParseNode
* pNode
= MakeORNode(pLeft
,pRight
);
2207 OSQLParseNode
* pNewRule
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_primary
));
2208 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION
));
2209 pNewRule
->append(pNode
);
2210 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION
));
2212 OSQLParseNode::eraseBraces(pLeft
);
2213 OSQLParseNode::eraseBraces(pRight
);
2215 pNode
= MakeANDNode(pSearchCondition
->getChild(0)->removeAt((sal_uInt32
)0),pNewRule
);
2216 replaceAndReset(pSearchCondition
,pNode
);
2218 else if(*pSearchCondition
->getChild(0)->getChild(2) == *pSearchCondition
->getChild(2)->getChild(2))
2220 OSQLParseNode
* pLeft
= pSearchCondition
->getChild(0)->removeAt((sal_uInt32
)0);
2221 OSQLParseNode
* pRight
= pSearchCondition
->getChild(2)->removeAt((sal_uInt32
)0);
2222 OSQLParseNode
* pNode
= MakeORNode(pLeft
,pRight
);
2224 OSQLParseNode
* pNewRule
= new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE
,OSQLParser::RuleID(OSQLParseNode::boolean_primary
));
2225 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION
));
2226 pNewRule
->append(pNode
);
2227 pNewRule
->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION
));
2229 OSQLParseNode::eraseBraces(pLeft
);
2230 OSQLParseNode::eraseBraces(pRight
);
2232 pNode
= MakeANDNode(pSearchCondition
->getChild(0)->removeAt(1),pNewRule
);
2233 replaceAndReset(pSearchCondition
,pNode
);
2237 #if OSL_DEBUG_LEVEL > 0
2238 // -----------------------------------------------------------------------------
2239 void OSQLParseNode::showParseTree( ::rtl::OUString
& rString
) const
2241 ::rtl::OUStringBuffer aBuf
;
2242 showParseTree( aBuf
, 0 );
2243 rString
= aBuf
.makeStringAndClear();
2246 // -----------------------------------------------------------------------------
2247 void OSQLParseNode::showParseTree( ::rtl::OUStringBuffer
& _inout_rBuffer
, sal_uInt32 nLevel
) const
2249 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::showParseTree" );
2251 for ( sal_uInt32 j
=0; j
<nLevel
; ++j
)
2252 _inout_rBuffer
.appendAscii( " " );
2256 // Regelnamen als rule: ...
2257 _inout_rBuffer
.appendAscii( "RULE_ID: " );
2258 _inout_rBuffer
.append( (sal_Int32
)getRuleID() );
2259 _inout_rBuffer
.append( sal_Unicode( '(' ) );
2260 _inout_rBuffer
.append( OSQLParser::RuleIDToStr( getRuleID() ) );
2261 _inout_rBuffer
.append( sal_Unicode( ')' ) );
2262 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2264 // hol dir den ersten Subtree
2265 for ( OSQLParseNodes::const_iterator i
= m_aChildren
.begin();
2266 i
!= m_aChildren
.end();
2269 (*i
)->showParseTree( _inout_rBuffer
, nLevel
+1 );
2273 // ein Token gefunden
2274 switch (m_eNodeType
)
2277 case SQL_NODE_KEYWORD
:
2278 _inout_rBuffer
.appendAscii( "SQL_KEYWORD: " );
2279 _inout_rBuffer
.append( ::rtl::OStringToOUString( OSQLParser::TokenIDToStr( getTokenID() ), RTL_TEXTENCODING_UTF8
) );
2280 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2283 case SQL_NODE_COMPARISON
:
2284 _inout_rBuffer
.appendAscii( "SQL_COMPARISON: " );
2285 _inout_rBuffer
.append( m_aNodeValue
);
2286 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2290 _inout_rBuffer
.appendAscii( "SQL_NAME: " );
2291 _inout_rBuffer
.append( sal_Unicode( '"' ) );
2292 _inout_rBuffer
.append( m_aNodeValue
);
2293 _inout_rBuffer
.append( sal_Unicode( '"' ) );
2294 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2297 case SQL_NODE_STRING
:
2298 _inout_rBuffer
.appendAscii( "SQL_STRING: " );
2299 _inout_rBuffer
.append( sal_Unicode( '\'' ) );
2300 _inout_rBuffer
.append( m_aNodeValue
);
2301 _inout_rBuffer
.append( sal_Unicode( '\'' ) );
2302 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2305 case SQL_NODE_INTNUM
:
2306 _inout_rBuffer
.appendAscii( "SQL_INTNUM: " );
2307 _inout_rBuffer
.append( m_aNodeValue
);
2308 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2311 case SQL_NODE_APPROXNUM
:
2312 _inout_rBuffer
.appendAscii( "SQL_APPROXNUM: " );
2313 _inout_rBuffer
.append( m_aNodeValue
);
2314 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2317 case SQL_NODE_PUNCTUATION
:
2318 _inout_rBuffer
.appendAscii( "SQL_PUNCTUATION: " );
2319 _inout_rBuffer
.append( m_aNodeValue
);
2320 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2323 case SQL_NODE_AMMSC
:
2324 _inout_rBuffer
.appendAscii( "SQL_AMMSC: " );
2325 _inout_rBuffer
.append( m_aNodeValue
);
2326 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2329 case SQL_NODE_EQUAL
:
2331 case SQL_NODE_GREAT
:
2332 case SQL_NODE_LESSEQ
:
2333 case SQL_NODE_GREATEQ
:
2334 case SQL_NODE_NOTEQUAL
:
2335 _inout_rBuffer
.append( m_aNodeValue
);
2336 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2339 case SQL_NODE_ACCESS_DATE
:
2340 _inout_rBuffer
.appendAscii( "SQL_ACCESS_DATE: " );
2341 _inout_rBuffer
.append( m_aNodeValue
);
2342 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2346 _inout_rBuffer
.appendAscii( "SQL_DATE: " );
2347 _inout_rBuffer
.append( m_aNodeValue
);
2348 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2351 case SQL_NODE_CONCAT
:
2352 _inout_rBuffer
.appendAscii( "||" );
2353 _inout_rBuffer
.append( sal_Unicode( '\n' ) );
2357 OSL_TRACE( "-- %i", int( m_eNodeType
) );
2358 OSL_ENSURE( false, "OSQLParser::ShowParseTree: unzulaessiger NodeType" );
2362 #endif // OSL_DEBUG_LEVEL > 0
2363 // -----------------------------------------------------------------------------
2365 //-----------------------------------------------------------------------------
2366 void OSQLParseNode::insert(sal_uInt32 nPos
, OSQLParseNode
* pNewSubTree
)
2368 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::insert" );
2369 OSL_ENSURE(pNewSubTree
!= NULL
, "OSQLParseNode: ungueltiger NewSubTree");
2370 OSL_ENSURE(pNewSubTree
->getParent() == NULL
, "OSQLParseNode: Knoten ist kein Waise");
2372 // stelle Verbindung zum getParent her:
2373 pNewSubTree
->setParent( this );
2374 m_aChildren
.insert(m_aChildren
.begin() + nPos
, pNewSubTree
);
2377 // removeAt-Methoden
2378 //-----------------------------------------------------------------------------
2379 OSQLParseNode
* OSQLParseNode::removeAt(sal_uInt32 nPos
)
2381 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::removeAt" );
2382 OSL_ENSURE(nPos
< m_aChildren
.size(),"Illegal position for removeAt");
2383 OSQLParseNodes::iterator
aPos(m_aChildren
.begin() + nPos
);
2384 OSQLParseNode
* pNode
= *aPos
;
2386 // setze den getParent des removeten auf NULL
2387 pNode
->setParent( NULL
);
2389 m_aChildren
.erase(aPos
);
2392 //-----------------------------------------------------------------------------
2393 OSQLParseNode
* OSQLParseNode::remove(OSQLParseNode
* pSubTree
)
2395 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::remove" );
2396 OSL_ENSURE(pSubTree
!= NULL
, "OSQLParseNode: ungueltiger SubTree");
2397 OSQLParseNodes::iterator aPos
= ::std::find(m_aChildren
.begin(), m_aChildren
.end(), pSubTree
);
2398 if (aPos
!= m_aChildren
.end())
2400 // setze den getParent des removeten auf NULL
2401 pSubTree
->setParent( NULL
);
2402 m_aChildren
.erase(aPos
);
2410 //-----------------------------------------------------------------------------
2411 OSQLParseNode
* OSQLParseNode::replaceAt(sal_uInt32 nPos
, OSQLParseNode
* pNewSubNode
)
2413 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replaceAt" );
2414 OSL_ENSURE(pNewSubNode
!= NULL
, "OSQLParseNode: invalid nodes");
2415 OSL_ENSURE(pNewSubNode
->getParent() == NULL
, "OSQLParseNode: node already has getParent");
2416 OSL_ENSURE(nPos
< m_aChildren
.size(), "OSQLParseNode: invalid position");
2417 OSL_ENSURE(::std::find(m_aChildren
.begin(), m_aChildren
.end(), pNewSubNode
) == m_aChildren
.end(),
2418 "OSQLParseNode::Replace() Node already element of parent");
2420 OSQLParseNode
* pOldSubNode
= m_aChildren
[nPos
];
2422 // stelle Verbindung zum getParent her:
2423 pNewSubNode
->setParent( this );
2424 pOldSubNode
->setParent( NULL
);
2426 m_aChildren
[nPos
] = pNewSubNode
;
2430 //-----------------------------------------------------------------------------
2431 OSQLParseNode
* OSQLParseNode::replace (OSQLParseNode
* pOldSubNode
, OSQLParseNode
* pNewSubNode
)
2433 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replace " );
2434 OSL_ENSURE(pOldSubNode
!= NULL
&& pNewSubNode
!= NULL
, "OSQLParseNode: invalid nodes");
2435 OSL_ENSURE(pNewSubNode
->getParent() == NULL
, "OSQLParseNode: node already has getParent");
2436 OSL_ENSURE(::std::find(m_aChildren
.begin(), m_aChildren
.end(), pOldSubNode
) != m_aChildren
.end(),
2437 "OSQLParseNode::Replace() Node not element of parent");
2438 OSL_ENSURE(::std::find(m_aChildren
.begin(), m_aChildren
.end(), pNewSubNode
) == m_aChildren
.end(),
2439 "OSQLParseNode::Replace() Node already element of parent");
2441 pOldSubNode
->setParent( NULL
);
2442 pNewSubNode
->setParent( this );
2443 ::std::replace(m_aChildren
.begin(), m_aChildren
.end(), pOldSubNode
, pNewSubNode
);
2446 // -----------------------------------------------------------------------------
2447 void OSQLParseNode::parseLeaf(::rtl::OUStringBuffer
& rString
, const SQLParseNodeParameter
& rParam
) const
2449 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseLeaf" );
2450 // ein Blatt ist gefunden
2451 // Inhalt dem Ausgabestring anfuegen
2452 switch (m_eNodeType
)
2454 case SQL_NODE_KEYWORD
:
2456 if (rString
.getLength())
2457 rString
.appendAscii(" ");
2459 const ::rtl::OString sT
= OSQLParser::TokenIDToStr(m_nNodeID
, &rParam
.m_rContext
);
2460 rString
.append(::rtl::OUString(sT
,sT
.getLength(),RTL_TEXTENCODING_UTF8
));
2462 case SQL_NODE_STRING
:
2463 if (rString
.getLength())
2464 rString
.appendAscii(" ");
2465 rString
.append(SetQuotation(m_aNodeValue
,::rtl::OUString::createFromAscii("\'"),::rtl::OUString::createFromAscii("\'\'")));
2468 if (rString
.getLength())
2470 switch(rString
.charAt(rString
.getLength()-1) )
2475 if ( !rParam
.aMetaData
.getCatalogSeparator().getLength()
2476 || rString
.charAt( rString
.getLength()-1 ) != rParam
.aMetaData
.getCatalogSeparator().toChar()
2478 rString
.appendAscii(" "); break;
2483 if (rParam
.bPredicate
)
2485 rString
.appendAscii("[");
2486 rString
.append(m_aNodeValue
);
2487 rString
.appendAscii("]");
2490 rString
.append(SetQuotation(m_aNodeValue
,
2491 rParam
.aMetaData
.getIdentifierQuoteString(), rParam
.aMetaData
.getIdentifierQuoteString() ));
2494 rString
.append(m_aNodeValue
);
2496 case SQL_NODE_ACCESS_DATE
:
2497 if (rString
.getLength())
2498 rString
.appendAscii(" ");
2499 rString
.appendAscii("#");
2500 rString
.append(m_aNodeValue
);
2501 rString
.appendAscii("#");
2503 case SQL_NODE_INTNUM
:
2504 case SQL_NODE_APPROXNUM
:
2506 ::rtl::OUString aTmp
= m_aNodeValue
;
2507 if (rParam
.bInternational
&& rParam
.bPredicate
&& rParam
.cDecSep
!= '.')
2508 aTmp
= aTmp
.replace('.', rParam
.cDecSep
);
2510 if (rString
.getLength())
2511 rString
.appendAscii(" ");
2512 rString
.append(aTmp
);
2517 if (rString
.getLength() && m_aNodeValue
.toChar() != '.' && m_aNodeValue
.toChar() != ':' )
2519 switch( rString
.charAt(rString
.getLength()-1) )
2524 if ( !rParam
.aMetaData
.getCatalogSeparator().getLength()
2525 || rString
.charAt( rString
.getLength()-1 ) != rParam
.aMetaData
.getCatalogSeparator().toChar()
2527 rString
.appendAscii(" "); break;
2530 rString
.append(m_aNodeValue
);
2534 // -----------------------------------------------------------------------------
2535 sal_Int32
OSQLParser::getFunctionReturnType(const ::rtl::OUString
& _sFunctionName
, const IParseContext
* pContext
)
2537 sal_Int32 nType
= DataType::VARCHAR
;
2538 ::rtl::OString
sFunctionName(_sFunctionName
,_sFunctionName
.getLength(),RTL_TEXTENCODING_UTF8
);
2540 if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASCII
,pContext
))) nType
= DataType::INTEGER
;
2541 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_BIT_LENGTH
,pContext
))) nType
= DataType::INTEGER
;
2542 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR
,pContext
))) nType
= DataType::VARCHAR
;
2543 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR_LENGTH
,pContext
))) nType
= DataType::INTEGER
;
2544 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHARACTER_LENGTH
,pContext
))) nType
= DataType::INTEGER
;
2545 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CONCAT
,pContext
))) nType
= DataType::VARCHAR
;
2546 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DIFFERENCE
,pContext
))) nType
= DataType::VARCHAR
;
2547 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_INSERT
,pContext
))) nType
= DataType::VARCHAR
;
2548 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LCASE
,pContext
))) nType
= DataType::VARCHAR
;
2549 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LEFT
,pContext
))) nType
= DataType::VARCHAR
;
2550 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LENGTH
,pContext
))) nType
= DataType::INTEGER
;
2551 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE
,pContext
))) nType
= DataType::VARCHAR
;
2552 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE_2
,pContext
))) nType
= DataType::VARCHAR
;
2553 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LTRIM
,pContext
))) nType
= DataType::VARCHAR
;
2554 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_OCTET_LENGTH
,pContext
))) nType
= DataType::INTEGER
;
2555 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POSITION
,pContext
))) nType
= DataType::INTEGER
;
2556 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPEAT
,pContext
))) nType
= DataType::VARCHAR
;
2557 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPLACE
,pContext
))) nType
= DataType::VARCHAR
;
2558 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RIGHT
,pContext
))) nType
= DataType::VARCHAR
;
2559 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RTRIM
,pContext
))) nType
= DataType::VARCHAR
;
2560 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SOUNDEX
,pContext
))) nType
= DataType::VARCHAR
;
2561 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SPACE
,pContext
))) nType
= DataType::VARCHAR
;
2562 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUBSTRING
,pContext
))) nType
= DataType::VARCHAR
;
2563 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UCASE
,pContext
))) nType
= DataType::VARCHAR
;
2564 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_DATE
,pContext
))) nType
= DataType::DATE
;
2565 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIME
,pContext
))) nType
= DataType::TIME
;
2566 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIMESTAMP
,pContext
))) nType
= DataType::TIMESTAMP
;
2567 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURDATE
,pContext
))) nType
= DataType::DATE
;
2568 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEDIFF
,pContext
))) nType
= DataType::INTEGER
;
2569 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEVALUE
,pContext
))) nType
= DataType::DATE
;
2570 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURTIME
,pContext
))) nType
= DataType::TIME
;
2571 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYNAME
,pContext
))) nType
= DataType::VARCHAR
;
2572 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFMONTH
,pContext
))) nType
= DataType::INTEGER
;
2573 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFWEEK
,pContext
))) nType
= DataType::INTEGER
;
2574 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFYEAR
,pContext
))) nType
= DataType::INTEGER
;
2575 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXTRACT
,pContext
))) nType
= DataType::VARCHAR
;
2576 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_HOUR
,pContext
))) nType
= DataType::INTEGER
;
2577 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MINUTE
,pContext
))) nType
= DataType::INTEGER
;
2578 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTH
,pContext
))) nType
= DataType::INTEGER
;
2579 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTHNAME
,pContext
))) nType
= DataType::VARCHAR
;
2580 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_NOW
,pContext
))) nType
= DataType::TIMESTAMP
;
2581 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_QUARTER
,pContext
))) nType
= DataType::INTEGER
;
2582 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SECOND
,pContext
))) nType
= DataType::INTEGER
;
2583 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPADD
,pContext
))) nType
= DataType::TIMESTAMP
;
2584 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPDIFF
,pContext
))) nType
= DataType::TIMESTAMP
;
2585 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMEVALUE
,pContext
))) nType
= DataType::TIMESTAMP
;
2586 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_WEEK
,pContext
))) nType
= DataType::INTEGER
;
2587 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_YEAR
,pContext
))) nType
= DataType::INTEGER
;
2588 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ABS
,pContext
))) nType
= DataType::DOUBLE
;
2589 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ACOS
,pContext
))) nType
= DataType::DOUBLE
;
2590 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASIN
,pContext
))) nType
= DataType::DOUBLE
;
2591 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN
,pContext
))) nType
= DataType::DOUBLE
;
2592 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN2
,pContext
))) nType
= DataType::DOUBLE
;
2593 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CEILING
,pContext
))) nType
= DataType::DOUBLE
;
2594 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COS
,pContext
))) nType
= DataType::DOUBLE
;
2595 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COT
,pContext
))) nType
= DataType::DOUBLE
;
2596 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DEGREES
,pContext
))) nType
= DataType::DOUBLE
;
2597 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXP
,pContext
))) nType
= DataType::DOUBLE
;
2598 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_FLOOR
,pContext
))) nType
= DataType::DOUBLE
;
2599 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOGF
,pContext
))) nType
= DataType::DOUBLE
;
2600 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG
,pContext
))) nType
= DataType::DOUBLE
;
2601 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG10
,pContext
))) nType
= DataType::DOUBLE
;
2602 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LN
,pContext
))) nType
= DataType::DOUBLE
;
2603 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MOD
,pContext
))) nType
= DataType::DOUBLE
;
2604 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_PI
,pContext
))) nType
= DataType::DOUBLE
;
2605 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POWER
,pContext
))) nType
= DataType::DOUBLE
;
2606 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RADIANS
,pContext
))) nType
= DataType::DOUBLE
;
2607 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RAND
,pContext
))) nType
= DataType::DOUBLE
;
2608 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUND
,pContext
))) nType
= DataType::DOUBLE
;
2609 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUNDMAGIC
,pContext
))) nType
= DataType::DOUBLE
;
2610 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIGN
,pContext
))) nType
= DataType::DOUBLE
;
2611 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIN
,pContext
))) nType
= DataType::DOUBLE
;
2612 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SQRT
,pContext
))) nType
= DataType::DOUBLE
;
2613 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TAN
,pContext
))) nType
= DataType::DOUBLE
;
2614 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TRUNCATE
,pContext
))) nType
= DataType::DOUBLE
;
2615 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COUNT
,pContext
))) nType
= DataType::INTEGER
;
2616 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MAX
,pContext
))) nType
= DataType::DOUBLE
;
2617 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MIN
,pContext
))) nType
= DataType::DOUBLE
;
2618 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_AVG
,pContext
))) nType
= DataType::DOUBLE
;
2619 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUM
,pContext
))) nType
= DataType::DOUBLE
;
2620 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOWER
,pContext
))) nType
= DataType::VARCHAR
;
2621 else if(sFunctionName
.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UPPER
,pContext
))) nType
= DataType::VARCHAR
;
2625 // -----------------------------------------------------------------------------
2626 sal_Int32
OSQLParser::getFunctionParameterType(sal_uInt32 _nTokenId
, sal_uInt32 _nPos
)
2628 sal_Int32 nType
= DataType::VARCHAR
;
2630 if(_nTokenId
== SQL_TOKEN_CHAR
) nType
= DataType::INTEGER
;
2631 else if(_nTokenId
== SQL_TOKEN_INSERT
)
2633 if ( _nPos
== 2 || _nPos
== 3 )
2634 nType
= DataType::INTEGER
;
2636 else if(_nTokenId
== SQL_TOKEN_LEFT
)
2639 nType
= DataType::INTEGER
;
2641 else if(_nTokenId
== SQL_TOKEN_LOCATE
)
2644 nType
= DataType::INTEGER
;
2646 else if(_nTokenId
== SQL_TOKEN_LOCATE_2
)
2649 nType
= DataType::INTEGER
;
2651 else if( _nTokenId
== SQL_TOKEN_REPEAT
|| _nTokenId
== SQL_TOKEN_RIGHT
)
2654 nType
= DataType::INTEGER
;
2656 else if(_nTokenId
== SQL_TOKEN_SPACE
)
2658 nType
= DataType::INTEGER
;
2660 else if(_nTokenId
== SQL_TOKEN_SUBSTRING
)
2663 nType
= DataType::INTEGER
;
2665 else if(_nTokenId
== SQL_TOKEN_DATEDIFF
)
2668 nType
= DataType::TIMESTAMP
;
2670 else if(_nTokenId
== SQL_TOKEN_DATEVALUE
)
2671 nType
= DataType::DATE
;
2672 else if(_nTokenId
== SQL_TOKEN_DAYNAME
)
2673 nType
= DataType::DATE
;
2674 else if(_nTokenId
== SQL_TOKEN_DAYOFMONTH
)
2675 nType
= DataType::DATE
;
2676 else if(_nTokenId
== SQL_TOKEN_DAYOFWEEK
)
2677 nType
= DataType::DATE
;
2678 else if(_nTokenId
== SQL_TOKEN_DAYOFYEAR
)
2679 nType
= DataType::DATE
;
2680 else if(_nTokenId
== SQL_TOKEN_EXTRACT
) nType
= DataType::VARCHAR
;
2681 else if(_nTokenId
== SQL_TOKEN_HOUR
) nType
= DataType::TIME
;
2682 else if(_nTokenId
== SQL_TOKEN_MINUTE
) nType
= DataType::TIME
;
2683 else if(_nTokenId
== SQL_TOKEN_MONTH
) nType
= DataType::DATE
;
2684 else if(_nTokenId
== SQL_TOKEN_MONTHNAME
) nType
= DataType::DATE
;
2685 else if(_nTokenId
== SQL_TOKEN_NOW
) nType
= DataType::TIMESTAMP
;
2686 else if(_nTokenId
== SQL_TOKEN_QUARTER
) nType
= DataType::DATE
;
2687 else if(_nTokenId
== SQL_TOKEN_SECOND
) nType
= DataType::TIME
;
2688 else if(_nTokenId
== SQL_TOKEN_TIMESTAMPADD
) nType
= DataType::TIMESTAMP
;
2689 else if(_nTokenId
== SQL_TOKEN_TIMESTAMPDIFF
) nType
= DataType::TIMESTAMP
;
2690 else if(_nTokenId
== SQL_TOKEN_TIMEVALUE
) nType
= DataType::TIMESTAMP
;
2691 else if(_nTokenId
== SQL_TOKEN_WEEK
) nType
= DataType::DATE
;
2692 else if(_nTokenId
== SQL_TOKEN_YEAR
) nType
= DataType::DATE
;
2694 else if(_nTokenId
== SQL_TOKEN_ABS
) nType
= DataType::DOUBLE
;
2695 else if(_nTokenId
== SQL_TOKEN_ACOS
) nType
= DataType::DOUBLE
;
2696 else if(_nTokenId
== SQL_TOKEN_ASIN
) nType
= DataType::DOUBLE
;
2697 else if(_nTokenId
== SQL_TOKEN_ATAN
) nType
= DataType::DOUBLE
;
2698 else if(_nTokenId
== SQL_TOKEN_ATAN2
) nType
= DataType::DOUBLE
;
2699 else if(_nTokenId
== SQL_TOKEN_CEILING
) nType
= DataType::DOUBLE
;
2700 else if(_nTokenId
== SQL_TOKEN_COS
) nType
= DataType::DOUBLE
;
2701 else if(_nTokenId
== SQL_TOKEN_COT
) nType
= DataType::DOUBLE
;
2702 else if(_nTokenId
== SQL_TOKEN_DEGREES
) nType
= DataType::DOUBLE
;
2703 else if(_nTokenId
== SQL_TOKEN_EXP
) nType
= DataType::DOUBLE
;
2704 else if(_nTokenId
== SQL_TOKEN_FLOOR
) nType
= DataType::DOUBLE
;
2705 else if(_nTokenId
== SQL_TOKEN_LOGF
) nType
= DataType::DOUBLE
;
2706 else if(_nTokenId
== SQL_TOKEN_LOG
) nType
= DataType::DOUBLE
;
2707 else if(_nTokenId
== SQL_TOKEN_LOG10
) nType
= DataType::DOUBLE
;
2708 else if(_nTokenId
== SQL_TOKEN_LN
) nType
= DataType::DOUBLE
;
2709 else if(_nTokenId
== SQL_TOKEN_MOD
) nType
= DataType::DOUBLE
;
2710 else if(_nTokenId
== SQL_TOKEN_PI
) nType
= DataType::DOUBLE
;
2711 else if(_nTokenId
== SQL_TOKEN_POWER
) nType
= DataType::DOUBLE
;
2712 else if(_nTokenId
== SQL_TOKEN_RADIANS
) nType
= DataType::DOUBLE
;
2713 else if(_nTokenId
== SQL_TOKEN_RAND
) nType
= DataType::DOUBLE
;
2714 else if(_nTokenId
== SQL_TOKEN_ROUND
) nType
= DataType::DOUBLE
;
2715 else if(_nTokenId
== SQL_TOKEN_ROUNDMAGIC
) nType
= DataType::DOUBLE
;
2716 else if(_nTokenId
== SQL_TOKEN_SIGN
) nType
= DataType::DOUBLE
;
2717 else if(_nTokenId
== SQL_TOKEN_SIN
) nType
= DataType::DOUBLE
;
2718 else if(_nTokenId
== SQL_TOKEN_SQRT
) nType
= DataType::DOUBLE
;
2719 else if(_nTokenId
== SQL_TOKEN_TAN
) nType
= DataType::DOUBLE
;
2720 else if(_nTokenId
== SQL_TOKEN_TRUNCATE
) nType
= DataType::DOUBLE
;
2721 else if(_nTokenId
== SQL_TOKEN_COUNT
) nType
= DataType::INTEGER
;
2722 else if(_nTokenId
== SQL_TOKEN_MAX
) nType
= DataType::DOUBLE
;
2723 else if(_nTokenId
== SQL_TOKEN_MIN
) nType
= DataType::DOUBLE
;
2724 else if(_nTokenId
== SQL_TOKEN_AVG
) nType
= DataType::DOUBLE
;
2725 else if(_nTokenId
== SQL_TOKEN_SUM
) nType
= DataType::DOUBLE
;
2727 else if(_nTokenId
== SQL_TOKEN_LOWER
) nType
= DataType::VARCHAR
;
2728 else if(_nTokenId
== SQL_TOKEN_UPPER
) nType
= DataType::VARCHAR
;
2733 // -----------------------------------------------------------------------------
2734 const SQLError
& OSQLParser::getErrorHelper() const
2736 return m_pData
->aErrors
;
2739 // -----------------------------------------------------------------------------
2740 OSQLParseNode::Rule
OSQLParseNode::getKnownRuleID() const
2743 return UNKNOWN_RULE
;
2744 return OSQLParser::RuleIDToRule( getRuleID() );
2746 // -----------------------------------------------------------------------------
2747 ::rtl::OUString
OSQLParseNode::getTableRange(const OSQLParseNode
* _pTableRef
)
2749 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableRange" );
2750 OSL_ENSURE(_pTableRef
&& _pTableRef
->count() > 1 && _pTableRef
->getKnownRuleID() == OSQLParseNode::table_ref
,"Invalid node give, only table ref is allowed!");
2751 const sal_uInt32 nCount
= _pTableRef
->count();
2752 ::rtl::OUString sTableRange
;
2753 if ( nCount
== 2 || (nCount
== 3 && !_pTableRef
->getChild(0)->isToken()) || nCount
== 5 )
2755 const OSQLParseNode
* pNode
= _pTableRef
->getChild(nCount
- (nCount
== 2 ? 1 : 2));
2756 OSL_ENSURE(pNode
&& (pNode
->getKnownRuleID() == OSQLParseNode::table_primary_as_range_column
2757 || pNode
->getKnownRuleID() == OSQLParseNode::range_variable
)
2758 ,"SQL grammar changed!");
2759 if ( !pNode
->isLeaf() )
2760 sTableRange
= pNode
->getChild(1)->getTokenValue();
2761 } // if ( nCount == 2 || nCount == 3 || nCount == 5)
2765 // -----------------------------------------------------------------------------
2766 OSQLParseNodesContainer::OSQLParseNodesContainer()
2769 // -----------------------------------------------------------------------------
2770 OSQLParseNodesContainer::~OSQLParseNodesContainer()
2773 // -----------------------------------------------------------------------------
2774 void OSQLParseNodesContainer::push_back(OSQLParseNode
* _pNode
)
2776 ::osl::MutexGuard
aGuard(m_aMutex
);
2777 m_aNodes
.push_back(_pNode
);
2779 // -----------------------------------------------------------------------------
2780 void OSQLParseNodesContainer::erase(OSQLParseNode
* _pNode
)
2782 ::osl::MutexGuard
aGuard(m_aMutex
);
2783 if ( !m_aNodes
.empty() )
2785 ::std::vector
< OSQLParseNode
* >::iterator aFind
= ::std::find(m_aNodes
.begin(), m_aNodes
.end(),_pNode
);
2786 if ( aFind
!= m_aNodes
.end() )
2787 m_aNodes
.erase(aFind
);
2790 // -----------------------------------------------------------------------------
2791 bool OSQLParseNodesContainer::empty() const
2793 return m_aNodes
.empty();
2795 // -----------------------------------------------------------------------------
2796 void OSQLParseNodesContainer::clear()
2798 ::osl::MutexGuard
aGuard(m_aMutex
);
2801 // -----------------------------------------------------------------------------
2802 void OSQLParseNodesContainer::clearAndDelete()
2804 ::osl::MutexGuard
aGuard(m_aMutex
);
2805 // clear the garbage collector
2806 while ( !m_aNodes
.empty() )
2808 OSQLParseNode
* pNode
= m_aNodes
[0];
2809 while ( pNode
->getParent() )
2811 pNode
= pNode
->getParent();
2816 } // namespace connectivity