1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <connectivity/sqlnode.hxx>
29cdf0e10cSrcweir #include <connectivity/sqlerror.hxx>
30cdf0e10cSrcweir #include <internalnode.hxx>
31cdf0e10cSrcweir #define YYBISON	  1
32cdf0e10cSrcweir #ifndef BISON_INCLUDED
33cdf0e10cSrcweir #define BISON_INCLUDED
34cdf0e10cSrcweir #include <sqlbison.hxx>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <connectivity/sqlparse.hxx>
37cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
38cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp>
39cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
40cdf0e10cSrcweir #include <com/sun/star/i18n/NumberFormatIndex.hpp>
41cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
42cdf0e10cSrcweir #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
43cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
44cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
45cdf0e10cSrcweir #include <com/sun/star/sdb/ErrorCondition.hpp>
46cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp>
47cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
48cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormats.hpp>
49cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
50cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
51cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
52cdf0e10cSrcweir #include <com/sun/star/i18n/KParseType.hpp>
53cdf0e10cSrcweir #include <com/sun/star/i18n/KParseTokens.hpp>
54cdf0e10cSrcweir #include "connectivity/dbconversion.hxx"
55cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
56cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
57cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
58cdf0e10cSrcweir #include "TConnection.hxx"
59cdf0e10cSrcweir #include "sqlscan.hxx"
60cdf0e10cSrcweir #include <comphelper/numbers.hxx>
61cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
62cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
63cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
64cdf0e10cSrcweir #include "connectivity/dbmetadata.hxx"
65cdf0e10cSrcweir #include "connectivity/sqlerror.hxx"
66cdf0e10cSrcweir #include <tools/diagnose_ex.h>
67cdf0e10cSrcweir #include <string.h>
68cdf0e10cSrcweir #include <boost/bind.hpp>
69cdf0e10cSrcweir #include <algorithm>
70cdf0e10cSrcweir #include <functional>
71cdf0e10cSrcweir #include <rtl/logfile.hxx>
72cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
73cdf0e10cSrcweir 
74cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
75cdf0e10cSrcweir using namespace ::com::sun::star::util;
76cdf0e10cSrcweir using namespace ::com::sun::star::beans;
77cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
78cdf0e10cSrcweir using namespace ::com::sun::star::uno;
79cdf0e10cSrcweir using namespace ::com::sun::star::lang;
80cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
81cdf0e10cSrcweir using namespace ::com::sun::star;
82cdf0e10cSrcweir using namespace ::osl;
83cdf0e10cSrcweir using namespace ::dbtools;
84cdf0e10cSrcweir using namespace ::comphelper;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir extern int SQLyyparse (void);
88cdf0e10cSrcweir extern ::rtl::OUString ConvertLikeToken(const ::connectivity::OSQLParseNode* pTokenNode, const ::connectivity::OSQLParseNode* pEscapeNode, sal_Bool bInternational);
89cdf0e10cSrcweir extern void setParser( ::connectivity::OSQLParser* );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir namespace
92cdf0e10cSrcweir {
93cdf0e10cSrcweir     // -----------------------------------------------------------------------------
94cdf0e10cSrcweir 	sal_Bool lcl_saveConvertToNumber(const Reference< XNumberFormatter > & _xFormatter,sal_Int32 _nKey,const ::rtl::OUString& _sValue,double& _nrValue)
95cdf0e10cSrcweir 	{
96cdf0e10cSrcweir 		sal_Bool bRet = sal_False;
97cdf0e10cSrcweir 		try
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 			_nrValue = _xFormatter->convertStringToNumber(_nKey, _sValue);
100cdf0e10cSrcweir 			bRet = sal_True;
101cdf0e10cSrcweir 		}
102cdf0e10cSrcweir 		catch(Exception&)
103cdf0e10cSrcweir 		{
104cdf0e10cSrcweir 		}
105cdf0e10cSrcweir 		return bRet;
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir     // -----------------------------------------------------------------------------
108cdf0e10cSrcweir     void replaceAndReset(connectivity::OSQLParseNode*& _pResetNode,connectivity::OSQLParseNode* _pNewNode)
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         _pResetNode->getParent()->replace(_pResetNode, _pNewNode);
111cdf0e10cSrcweir         delete _pResetNode;
112cdf0e10cSrcweir         _pResetNode = _pNewNode;
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir     // -----------------------------------------------------------------------------
115cdf0e10cSrcweir     /** quotes a string and search for quotes inside the string and replace them with the new quote
116cdf0e10cSrcweir         @param  rValue
117cdf0e10cSrcweir             The value to be quoted.
118cdf0e10cSrcweir         @param  rQuot
119cdf0e10cSrcweir             The quote
120cdf0e10cSrcweir         @param  rQuotToReplace
121cdf0e10cSrcweir             The quote to replace with
122cdf0e10cSrcweir         @return
123cdf0e10cSrcweir             The quoted string.
124cdf0e10cSrcweir     */
125cdf0e10cSrcweir     ::rtl::OUString SetQuotation(const ::rtl::OUString& rValue, const ::rtl::OUString& rQuot, const ::rtl::OUString& rQuotToReplace)
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         ::rtl::OUString rNewValue = rQuot;
128cdf0e10cSrcweir         rNewValue += rValue;
129cdf0e10cSrcweir         sal_Int32 nIndex = (sal_Int32)-1;   // Quotes durch zweifache Quotes ersetzen, sonst kriegt der Parser Probleme
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         if (rQuot.getLength())
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             do
134cdf0e10cSrcweir             {
135cdf0e10cSrcweir                 nIndex += 2;
136cdf0e10cSrcweir                 nIndex = rNewValue.indexOf(rQuot,nIndex);
137cdf0e10cSrcweir                 if(nIndex != -1)
138cdf0e10cSrcweir                     rNewValue = rNewValue.replaceAt(nIndex,rQuot.getLength(),rQuotToReplace);
139cdf0e10cSrcweir             } while (nIndex != -1);
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         rNewValue += rQuot;
143cdf0e10cSrcweir         return rNewValue;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir namespace connectivity
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 
150cdf0e10cSrcweir //=============================================================================
151cdf0e10cSrcweir struct OSQLParser_Data
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     ::com::sun::star::lang::Locale  aLocale;
154cdf0e10cSrcweir     ::connectivity::SQLError        aErrors;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     OSQLParser_Data( const Reference< XMultiServiceFactory >& _xServiceFactory )
157cdf0e10cSrcweir         :aErrors( _xServiceFactory )
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir };
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //=============================================================================
163cdf0e10cSrcweir //= SQLParseNodeParameter
164cdf0e10cSrcweir //=============================================================================
165cdf0e10cSrcweir //-----------------------------------------------------------------------------
166cdf0e10cSrcweir SQLParseNodeParameter::SQLParseNodeParameter( const Reference< XConnection >& _rxConnection,
167cdf0e10cSrcweir 		const Reference< XNumberFormatter >& _xFormatter, const Reference< XPropertySet >& _xField,
168cdf0e10cSrcweir 		const Locale& _rLocale, const IParseContext* _pContext,
169cdf0e10cSrcweir         bool _bIntl, bool _bQuote, sal_Char _cDecSep, bool _bPredicate, bool _bParseToSDBC )
170cdf0e10cSrcweir     :rLocale(_rLocale)
171cdf0e10cSrcweir     ,aMetaData( _rxConnection )
172cdf0e10cSrcweir     ,pParser( NULL )
173cdf0e10cSrcweir     ,pSubQueryHistory( new QueryNameSet )
174cdf0e10cSrcweir     ,xFormatter(_xFormatter)
175cdf0e10cSrcweir     ,xField(_xField)
176cdf0e10cSrcweir     ,m_rContext( _pContext ? (const IParseContext&)(*_pContext) : (const IParseContext&)OSQLParser::s_aDefaultContext )
177cdf0e10cSrcweir     ,cDecSep(_cDecSep)
178cdf0e10cSrcweir     ,bQuote(_bQuote)
179cdf0e10cSrcweir 	,bInternational(_bIntl)
180cdf0e10cSrcweir 	,bPredicate(_bPredicate)
181cdf0e10cSrcweir     ,bParseToSDBCLevel( _bParseToSDBC )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir //-----------------------------------------------------------------------------
186cdf0e10cSrcweir SQLParseNodeParameter::~SQLParseNodeParameter()
187cdf0e10cSrcweir {
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir //=============================================================================
191cdf0e10cSrcweir //= OSQLParseNode
192cdf0e10cSrcweir //=============================================================================
193cdf0e10cSrcweir //-----------------------------------------------------------------------------
194cdf0e10cSrcweir ::rtl::OUString OSQLParseNode::convertDateString(const SQLParseNodeParameter& rParam, const ::rtl::OUString& rString) const
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertDateString" );
197cdf0e10cSrcweir 	Date aDate = DBTypeConversion::toDate(rString);
198cdf0e10cSrcweir 	Reference< XNumberFormatsSupplier > xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
199cdf0e10cSrcweir 	Reference< XNumberFormatTypes >		xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	double fDate = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
202cdf0e10cSrcweir 	sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 36; // XXX hack
203cdf0e10cSrcweir 	return rParam.xFormatter->convertNumberToString(nKey, fDate);
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir //-----------------------------------------------------------------------------
207cdf0e10cSrcweir ::rtl::OUString OSQLParseNode::convertDateTimeString(const SQLParseNodeParameter& rParam, const ::rtl::OUString& rString) const
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertDateTimeString" );
210cdf0e10cSrcweir 	DateTime aDate = DBTypeConversion::toDateTime(rString);
211cdf0e10cSrcweir 	Reference< XNumberFormatsSupplier >  xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
212cdf0e10cSrcweir 	Reference< XNumberFormatTypes >  xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	double fDateTime = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
215cdf0e10cSrcweir 	sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 51; // XXX hack
216cdf0e10cSrcweir 	return rParam.xFormatter->convertNumberToString(nKey, fDateTime);
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //-----------------------------------------------------------------------------
220cdf0e10cSrcweir ::rtl::OUString OSQLParseNode::convertTimeString(const SQLParseNodeParameter& rParam, const ::rtl::OUString& rString) const
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::convertTimeString" );
223cdf0e10cSrcweir 	Time aTime = DBTypeConversion::toTime(rString);
224cdf0e10cSrcweir 	Reference< XNumberFormatsSupplier >  xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	Reference< XNumberFormatTypes >  xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	double fTime = DBTypeConversion::toDouble(aTime);
229cdf0e10cSrcweir 	sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 41; // XXX hack
230cdf0e10cSrcweir 	return rParam.xFormatter->convertNumberToString(nKey, fTime);
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir //-----------------------------------------------------------------------------
234cdf0e10cSrcweir void OSQLParseNode::parseNodeToStr(::rtl::OUString& rString,
235cdf0e10cSrcweir 								   const Reference< XConnection >& _rxConnection,
236cdf0e10cSrcweir 								   const IParseContext* pContext,
237cdf0e10cSrcweir 								   sal_Bool _bIntl,
238cdf0e10cSrcweir 								   sal_Bool _bQuote) const
239cdf0e10cSrcweir {
240cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToStr" );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	parseNodeToStr(
243cdf0e10cSrcweir 		rString, _rxConnection, NULL, NULL,
244cdf0e10cSrcweir 		pContext ? pContext->getPreferredLocale() : OParseContext::getDefaultLocale(),
245cdf0e10cSrcweir 		pContext, _bIntl, _bQuote, '.', false, false );
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir //-----------------------------------------------------------------------------
249cdf0e10cSrcweir void OSQLParseNode::parseNodeToPredicateStr(::rtl::OUString& rString,
250cdf0e10cSrcweir 											  const Reference< XConnection >& _rxConnection,
251cdf0e10cSrcweir 											  const Reference< XNumberFormatter > & xFormatter,
252cdf0e10cSrcweir 											  const ::com::sun::star::lang::Locale& rIntl,
253cdf0e10cSrcweir 											  sal_Char _cDec,
254cdf0e10cSrcweir 											  const IParseContext* pContext ) const
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToPredicateStr" );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	if (xFormatter.is())
261cdf0e10cSrcweir 		parseNodeToStr(rString, _rxConnection, xFormatter, NULL, rIntl, pContext, sal_True, sal_True, _cDec, true, false);
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir //-----------------------------------------------------------------------------
265cdf0e10cSrcweir void OSQLParseNode::parseNodeToPredicateStr(::rtl::OUString& rString,
266cdf0e10cSrcweir 											  const Reference< XConnection > & _rxConnection,
267cdf0e10cSrcweir 											  const Reference< XNumberFormatter > & xFormatter,
268cdf0e10cSrcweir 											  const Reference< XPropertySet > & _xField,
269cdf0e10cSrcweir 											  const ::com::sun::star::lang::Locale& rIntl,
270cdf0e10cSrcweir 											  sal_Char _cDec,
271cdf0e10cSrcweir 											  const IParseContext* pContext ) const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToPredicateStr" );
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	if (xFormatter.is())
278cdf0e10cSrcweir 		parseNodeToStr( rString, _rxConnection, xFormatter, _xField, rIntl, pContext, true, true, _cDec, true, false );
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir //-----------------------------------------------------------------------------
282cdf0e10cSrcweir void OSQLParseNode::parseNodeToStr(::rtl::OUString& rString,
283cdf0e10cSrcweir 					  const Reference< XConnection > & _rxConnection,
284cdf0e10cSrcweir 					  const Reference< XNumberFormatter > & xFormatter,
285cdf0e10cSrcweir 					  const Reference< XPropertySet > & _xField,
286cdf0e10cSrcweir 					  const ::com::sun::star::lang::Locale& rIntl,
287cdf0e10cSrcweir 					  const IParseContext* pContext,
288cdf0e10cSrcweir 					  bool _bIntl,
289cdf0e10cSrcweir 					  bool _bQuote,
290cdf0e10cSrcweir 					  sal_Char _cDecSep,
291cdf0e10cSrcweir 					  bool _bPredicate,
292cdf0e10cSrcweir                       bool _bSubstitute) const
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToStr" );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 	OSL_ENSURE( _rxConnection.is(), "OSQLParseNode::parseNodeToStr: invalid connection!" );
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	if ( _rxConnection.is() )
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir         ::rtl::OUStringBuffer sBuffer = rString;
301cdf0e10cSrcweir         try
302cdf0e10cSrcweir         {
303cdf0e10cSrcweir 		    OSQLParseNode::impl_parseNodeToString_throw( sBuffer,
304cdf0e10cSrcweir 			    SQLParseNodeParameter(
305cdf0e10cSrcweir                     _rxConnection, xFormatter, _xField, rIntl, pContext,
306cdf0e10cSrcweir                     _bIntl, _bQuote, _cDecSep, _bPredicate, _bSubstitute
307cdf0e10cSrcweir                 ) );
308cdf0e10cSrcweir         }
309cdf0e10cSrcweir         catch( const SQLException& )
310cdf0e10cSrcweir         {
311cdf0e10cSrcweir             OSL_ENSURE( false, "OSQLParseNode::parseNodeToStr: this should not throw!" );
312cdf0e10cSrcweir             // our callers don't expect this method to throw anything. The only known situation
313cdf0e10cSrcweir             // where impl_parseNodeToString_throw can throw is when there is a cyclic reference
314cdf0e10cSrcweir             // in the sub queries, but this cannot be the case here, as we do not parse to
315cdf0e10cSrcweir             // SDBC level.
316cdf0e10cSrcweir         }
317cdf0e10cSrcweir         rString = sBuffer.makeStringAndClear();
318cdf0e10cSrcweir 	}
319cdf0e10cSrcweir }
320cdf0e10cSrcweir //-----------------------------------------------------------------------------
321cdf0e10cSrcweir bool OSQLParseNode::parseNodeToExecutableStatement( ::rtl::OUString& _out_rString, const Reference< XConnection >& _rxConnection,
322cdf0e10cSrcweir     OSQLParser& _rParser, ::com::sun::star::sdbc::SQLException* _pErrorHolder ) const
323cdf0e10cSrcweir {
324cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseNodeToExecutableStatement" );
325cdf0e10cSrcweir     OSL_PRECOND( _rxConnection.is(), "OSQLParseNode::parseNodeToExecutableStatement: invalid connection!" );
326cdf0e10cSrcweir     SQLParseNodeParameter aParseParam( _rxConnection,
327cdf0e10cSrcweir         NULL, NULL, OParseContext::getDefaultLocale(), NULL, false, true, '.', false, true );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir     if ( aParseParam.aMetaData.supportsSubqueriesInFrom() )
330cdf0e10cSrcweir     {
331cdf0e10cSrcweir         Reference< XQueriesSupplier > xSuppQueries( _rxConnection, UNO_QUERY );
332cdf0e10cSrcweir         OSL_ENSURE( xSuppQueries.is(), "OSQLParseNode::parseNodeToExecutableStatement: cannot substitute everything without a QueriesSupplier!" );
333cdf0e10cSrcweir         if ( xSuppQueries.is() )
334cdf0e10cSrcweir             aParseParam.xQueries = xSuppQueries->getQueries();
335cdf0e10cSrcweir     }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     aParseParam.pParser = &_rParser;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     _out_rString = ::rtl::OUString();
340cdf0e10cSrcweir     ::rtl::OUStringBuffer sBuffer;
341cdf0e10cSrcweir     bool bSuccess = false;
342cdf0e10cSrcweir     try
343cdf0e10cSrcweir     {
344cdf0e10cSrcweir         impl_parseNodeToString_throw( sBuffer, aParseParam );
345cdf0e10cSrcweir         bSuccess = true;
346cdf0e10cSrcweir     }
347cdf0e10cSrcweir     catch( const SQLException& e )
348cdf0e10cSrcweir     {
349cdf0e10cSrcweir         if ( _pErrorHolder )
350cdf0e10cSrcweir             *_pErrorHolder = e;
351cdf0e10cSrcweir     }
352cdf0e10cSrcweir     _out_rString = sBuffer.makeStringAndClear();
353cdf0e10cSrcweir     return bSuccess;
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir //-----------------------------------------------------------------------------
357cdf0e10cSrcweir namespace
358cdf0e10cSrcweir {
359cdf0e10cSrcweir     bool lcl_isAliasNamePresent( const OSQLParseNode& _rTableNameNode )
360cdf0e10cSrcweir     {
361cdf0e10cSrcweir         return OSQLParseNode::getTableRange(_rTableNameNode.getParent()).getLength() != 0;
362cdf0e10cSrcweir     }
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir //-----------------------------------------------------------------------------
366cdf0e10cSrcweir void OSQLParseNode::impl_parseNodeToString_throw(::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableRange" );
369cdf0e10cSrcweir     if ( isToken() )
370cdf0e10cSrcweir     {
371cdf0e10cSrcweir 		parseLeaf(rString,rParam);
372cdf0e10cSrcweir         return;
373cdf0e10cSrcweir     }
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 	// einmal auswerten wieviel Subtrees dieser Knoten besitzt
376cdf0e10cSrcweir 	sal_uInt32 nCount = count();
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     bool bHandled = false;
379cdf0e10cSrcweir     switch ( getKnownRuleID() )
380cdf0e10cSrcweir     {
381cdf0e10cSrcweir     // special handling for parameters
382cdf0e10cSrcweir     case parameter:
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir 		if(rString.getLength())
385cdf0e10cSrcweir 			rString.appendAscii(" ");
386cdf0e10cSrcweir 		if (nCount == 1)	// ?
387cdf0e10cSrcweir 			m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam );
388cdf0e10cSrcweir 		else if (nCount == 2)	// :Name
389cdf0e10cSrcweir 		{
390cdf0e10cSrcweir 			m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam );
391cdf0e10cSrcweir 			rString.append(m_aChildren[1]->m_aNodeValue);
392cdf0e10cSrcweir 		}				    // [Name]
393cdf0e10cSrcweir 		else
394cdf0e10cSrcweir 		{
395cdf0e10cSrcweir 			m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam );
396cdf0e10cSrcweir 			rString.append(m_aChildren[1]->m_aNodeValue);
397cdf0e10cSrcweir 			rString.append(m_aChildren[2]->m_aNodeValue);
398cdf0e10cSrcweir 		}
399cdf0e10cSrcweir         bHandled = true;
400cdf0e10cSrcweir 	}
401cdf0e10cSrcweir     break;
402cdf0e10cSrcweir 
403cdf0e10cSrcweir     // table refs
404cdf0e10cSrcweir     case table_ref:
405cdf0e10cSrcweir         if (  ( nCount == 2 ) || ( nCount == 3 ) || ( nCount == 5 ) )
406cdf0e10cSrcweir         {
407cdf0e10cSrcweir 		    impl_parseTableRangeNodeToString_throw( rString, rParam );
408cdf0e10cSrcweir             bHandled = true;
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir         break;
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     // table name - might be a query name
413cdf0e10cSrcweir     case table_name:
414cdf0e10cSrcweir         bHandled = impl_parseTableNameNodeToString_throw( rString, rParam );
415cdf0e10cSrcweir         break;
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     case as:
418cdf0e10cSrcweir         if ( rParam.aMetaData.generateASBeforeCorrelationName() )
419cdf0e10cSrcweir             rString.append(::rtl::OUString::createFromAscii( " AS" ));
420cdf0e10cSrcweir         bHandled = true;
421cdf0e10cSrcweir         break;
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     case like_predicate:
424cdf0e10cSrcweir 	    // je nachdem ob international angegeben wird oder nicht wird like anders behandelt
425cdf0e10cSrcweir 	    // interanational: *, ? sind Platzhalter
426cdf0e10cSrcweir 	    // sonst SQL92 konform: %, _
427cdf0e10cSrcweir 		impl_parseLikeNodeToString_throw( rString, rParam );
428cdf0e10cSrcweir         bHandled = true;
429cdf0e10cSrcweir         break;
430cdf0e10cSrcweir 
431cdf0e10cSrcweir     case general_set_fct:
432cdf0e10cSrcweir     case set_fct_spec:
433cdf0e10cSrcweir     case position_exp:
434cdf0e10cSrcweir     case extract_exp:
435cdf0e10cSrcweir     case length_exp:
436cdf0e10cSrcweir     case char_value_fct:
437cdf0e10cSrcweir 	{
438cdf0e10cSrcweir 		if (!addDateValue(rString, rParam))
439cdf0e10cSrcweir 		{
440cdf0e10cSrcweir 			// Funktionsname nicht quoten
441cdf0e10cSrcweir 			SQLParseNodeParameter aNewParam(rParam);
442cdf0e10cSrcweir 			aNewParam.bQuote = ( SQL_ISRULE(this,length_exp)	|| SQL_ISRULE(this,char_value_fct) );
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 			m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam );
445cdf0e10cSrcweir 			aNewParam.bQuote = rParam.bQuote;
446cdf0e10cSrcweir 			//aNewParam.bPredicate = sal_False; // disable [ ] around names // look at i73215
447cdf0e10cSrcweir 			::rtl::OUStringBuffer aStringPara;
448cdf0e10cSrcweir 			for (sal_uInt32 i=1; i<nCount; i++)
449cdf0e10cSrcweir 			{
450cdf0e10cSrcweir 				const OSQLParseNode * pSubTree = m_aChildren[i];
451cdf0e10cSrcweir 				if (pSubTree)
452cdf0e10cSrcweir 				{
453cdf0e10cSrcweir 					pSubTree->impl_parseNodeToString_throw( aStringPara, aNewParam );
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 					// bei den CommaListen zwischen alle Subtrees Commas setzen
456cdf0e10cSrcweir 					if ((m_eNodeType == SQL_NODE_COMMALISTRULE) 	&& (i < (nCount - 1)))
457cdf0e10cSrcweir 						aStringPara.appendAscii(",");
458cdf0e10cSrcweir 				}
459cdf0e10cSrcweir                 else
460cdf0e10cSrcweir                     i++;
461cdf0e10cSrcweir 			}
462cdf0e10cSrcweir 			rString.append(aStringPara.makeStringAndClear());
463cdf0e10cSrcweir 		}
464cdf0e10cSrcweir         bHandled = true;
465cdf0e10cSrcweir 	}
466cdf0e10cSrcweir     break;
467cdf0e10cSrcweir     default:
468cdf0e10cSrcweir         break;
469cdf0e10cSrcweir     }   // switch ( getKnownRuleID() )
470cdf0e10cSrcweir 
471cdf0e10cSrcweir     if ( !bHandled )
472cdf0e10cSrcweir 	{
473cdf0e10cSrcweir 		for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
474cdf0e10cSrcweir 			i != m_aChildren.end();)
475cdf0e10cSrcweir 		{
476cdf0e10cSrcweir 			const OSQLParseNode* pSubTree = *i;
477cdf0e10cSrcweir 			if ( !pSubTree )
478cdf0e10cSrcweir             {
479cdf0e10cSrcweir                 ++i;
480cdf0e10cSrcweir                 continue;
481cdf0e10cSrcweir             }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir 			SQLParseNodeParameter aNewParam(rParam);
484cdf0e10cSrcweir 
485cdf0e10cSrcweir 			// don't replace the field for subqueries
486cdf0e10cSrcweir 			if (rParam.xField.is() && SQL_ISRULE(pSubTree,subquery))
487cdf0e10cSrcweir 				aNewParam.xField = NULL;
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 			// if there is a field given we don't display the fieldname, if there is any
490cdf0e10cSrcweir 			if (rParam.xField.is() && SQL_ISRULE(pSubTree,column_ref))
491cdf0e10cSrcweir 			{
492cdf0e10cSrcweir 				sal_Bool bFilter = sal_False;
493cdf0e10cSrcweir 				// retrieve the fields name
494cdf0e10cSrcweir 				::rtl::OUString aFieldName;
495cdf0e10cSrcweir 				try
496cdf0e10cSrcweir 				{
497cdf0e10cSrcweir 					sal_Int32 nNamePropertyId = PROPERTY_ID_NAME;
498cdf0e10cSrcweir 					if ( rParam.xField->getPropertySetInfo()->hasPropertyByName( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_REALNAME ) ) )
499cdf0e10cSrcweir 						nNamePropertyId = PROPERTY_ID_REALNAME;
500cdf0e10cSrcweir 					rParam.xField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( nNamePropertyId ) ) >>= aFieldName;
501cdf0e10cSrcweir 				}
502cdf0e10cSrcweir 				catch ( Exception& )
503cdf0e10cSrcweir 				{
504cdf0e10cSrcweir 				}
505cdf0e10cSrcweir 
506cdf0e10cSrcweir 				if(pSubTree->count())
507cdf0e10cSrcweir 				{
508cdf0e10cSrcweir 					const OSQLParseNode* pCol = pSubTree->m_aChildren[pSubTree->count()-1];
509cdf0e10cSrcweir 					if	(	(	SQL_ISRULE(pCol,column_val)
510cdf0e10cSrcweir 							&&	pCol->getChild(0)->getTokenValue().equalsIgnoreAsciiCase(aFieldName)
511cdf0e10cSrcweir 							)
512cdf0e10cSrcweir 						||	pCol->getTokenValue().equalsIgnoreAsciiCase(aFieldName)
513cdf0e10cSrcweir 						)
514cdf0e10cSrcweir 						bFilter = sal_True;
515cdf0e10cSrcweir 				}
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 				// ok we found the field, if the following node is the
518cdf0e10cSrcweir 				// comparision operator '=' we filter it as well
519cdf0e10cSrcweir 				if (bFilter)
520cdf0e10cSrcweir 				{
521cdf0e10cSrcweir 					if (SQL_ISRULE(this, comparison_predicate))
522cdf0e10cSrcweir 					{
523cdf0e10cSrcweir 						++i;
524cdf0e10cSrcweir 						if(i != m_aChildren.end())
525cdf0e10cSrcweir 						{
526cdf0e10cSrcweir 							pSubTree = *i;
527cdf0e10cSrcweir 							if (pSubTree && pSubTree->getNodeType() == SQL_NODE_EQUAL)
528cdf0e10cSrcweir 								i++;
529cdf0e10cSrcweir 						}
530cdf0e10cSrcweir 					}
531cdf0e10cSrcweir 					else
532cdf0e10cSrcweir 						i++;
533cdf0e10cSrcweir 				}
534cdf0e10cSrcweir 				else
535cdf0e10cSrcweir 				{
536cdf0e10cSrcweir 					pSubTree->impl_parseNodeToString_throw( rString, aNewParam );
537cdf0e10cSrcweir 					i++;
538cdf0e10cSrcweir 
539cdf0e10cSrcweir 					// bei den CommaListen zwischen alle Subtrees Commas setzen
540cdf0e10cSrcweir 					if ((m_eNodeType == SQL_NODE_COMMALISTRULE) 	&& (i != m_aChildren.end()))
541cdf0e10cSrcweir 						rString.appendAscii(",");
542cdf0e10cSrcweir 				}
543cdf0e10cSrcweir 			}
544cdf0e10cSrcweir 			else
545cdf0e10cSrcweir 			{
546cdf0e10cSrcweir 				pSubTree->impl_parseNodeToString_throw( rString, aNewParam );
547cdf0e10cSrcweir 				i++;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 				// bei den CommaListen zwischen alle Subtrees Commas setzen
550cdf0e10cSrcweir 				if ((m_eNodeType == SQL_NODE_COMMALISTRULE) 	&& (i != m_aChildren.end()))
551cdf0e10cSrcweir 				{
552cdf0e10cSrcweir 					if (SQL_ISRULE(this,value_exp_commalist) && rParam.bPredicate)
553cdf0e10cSrcweir 						rString.appendAscii(";");
554cdf0e10cSrcweir 					else
555cdf0e10cSrcweir 						rString.appendAscii(",");
556cdf0e10cSrcweir 				}
557cdf0e10cSrcweir 			}
558cdf0e10cSrcweir 		}
559cdf0e10cSrcweir 	}
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir //-----------------------------------------------------------------------------
563cdf0e10cSrcweir bool OSQLParseNode::impl_parseTableNameNodeToString_throw( ::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const
564cdf0e10cSrcweir {
565cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseTableNameNodeToString_throw" );
566cdf0e10cSrcweir     // is the table_name part of a table_ref?
567cdf0e10cSrcweir     OSL_ENSURE( getParent(), "OSQLParseNode::impl_parseTableNameNodeToString_throw: table_name without parent?" );
568cdf0e10cSrcweir     if ( !getParent() || ( getParent()->getKnownRuleID() != table_ref ) )
569cdf0e10cSrcweir         return false;
570cdf0e10cSrcweir 
571cdf0e10cSrcweir     // if it's a query, maybe we need to substitute the SQL statement ...
572cdf0e10cSrcweir     if ( !rParam.bParseToSDBCLevel )
573cdf0e10cSrcweir         return false;
574cdf0e10cSrcweir 
575cdf0e10cSrcweir     if ( !rParam.xQueries.is() )
576cdf0e10cSrcweir         // connection does not support queries in queries, or was no query supplier
577cdf0e10cSrcweir         return false;
578cdf0e10cSrcweir 
579cdf0e10cSrcweir     try
580cdf0e10cSrcweir     {
581cdf0e10cSrcweir         ::rtl::OUString sTableOrQueryName( getChild(0)->getTokenValue() );
582cdf0e10cSrcweir         bool bIsQuery = rParam.xQueries->hasByName( sTableOrQueryName );
583cdf0e10cSrcweir         if ( !bIsQuery )
584cdf0e10cSrcweir             return false;
585cdf0e10cSrcweir 
586cdf0e10cSrcweir         // avoid recursion (e.g. "foo" defined as "SELECT * FROM bar" and "bar" defined as "SELECT * FROM foo".
587cdf0e10cSrcweir         if ( rParam.pSubQueryHistory->find( sTableOrQueryName ) != rParam.pSubQueryHistory->end() )
588cdf0e10cSrcweir         {
589cdf0e10cSrcweir             ::rtl::OUString sMessage( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "cyclic sub queries" ) ) );
590cdf0e10cSrcweir             OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: no parser?" );
591cdf0e10cSrcweir             if ( rParam.pParser )
592cdf0e10cSrcweir             {
593cdf0e10cSrcweir                 const SQLError& rErrors( rParam.pParser->getErrorHelper() );
594cdf0e10cSrcweir                 rErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
595cdf0e10cSrcweir             }
596cdf0e10cSrcweir             else
597cdf0e10cSrcweir             {
598cdf0e10cSrcweir                 SQLError aErrors( ::comphelper::getProcessServiceFactory() );
599cdf0e10cSrcweir                 aErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
600cdf0e10cSrcweir             }
601cdf0e10cSrcweir         }
602cdf0e10cSrcweir         rParam.pSubQueryHistory->insert( sTableOrQueryName );
603cdf0e10cSrcweir 
604cdf0e10cSrcweir         Reference< XPropertySet > xQuery( rParam.xQueries->getByName( sTableOrQueryName ), UNO_QUERY_THROW );
605cdf0e10cSrcweir 
606cdf0e10cSrcweir         // substitute the query name with the constituting command
607cdf0e10cSrcweir         ::rtl::OUString sCommand;
608cdf0e10cSrcweir         OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND ) ) >>= sCommand );
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         sal_Bool bEscapeProcessing = sal_False;
611cdf0e10cSrcweir         OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ) ) >>= bEscapeProcessing );
612cdf0e10cSrcweir 
613cdf0e10cSrcweir         // the query we found here might itself be based on another query, so parse it recursively
614cdf0e10cSrcweir         OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: cannot analyze sub queries without a parser!" );
615cdf0e10cSrcweir         if ( bEscapeProcessing && rParam.pParser )
616cdf0e10cSrcweir         {
617cdf0e10cSrcweir             ::rtl::OUString sError;
618cdf0e10cSrcweir             ::std::auto_ptr< OSQLParseNode > pSubQueryNode( rParam.pParser->parseTree( sError, sCommand, sal_False ) );
619cdf0e10cSrcweir             if ( pSubQueryNode.get() )
620cdf0e10cSrcweir             {
621cdf0e10cSrcweir                 // parse the sub-select to SDBC level, too
622cdf0e10cSrcweir                 ::rtl::OUStringBuffer sSubSelect;
623cdf0e10cSrcweir                 pSubQueryNode->impl_parseNodeToString_throw( sSubSelect, rParam );
624cdf0e10cSrcweir                 if ( sSubSelect.getLength() )
625cdf0e10cSrcweir                     sCommand = sSubSelect.makeStringAndClear();
626cdf0e10cSrcweir             }
627cdf0e10cSrcweir         }
628cdf0e10cSrcweir 
629cdf0e10cSrcweir         rString.appendAscii( " ( " );
630cdf0e10cSrcweir         rString.append(sCommand);
631cdf0e10cSrcweir         rString.appendAscii( " )" );
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         // append the query name as table alias, since it might be referenced in other
634cdf0e10cSrcweir         // parts of the statement - but only if there's no other alias name present
635cdf0e10cSrcweir         if ( !lcl_isAliasNamePresent( *this ) )
636cdf0e10cSrcweir         {
637cdf0e10cSrcweir             rString.appendAscii( " AS " );
638cdf0e10cSrcweir             if ( rParam.bQuote )
639cdf0e10cSrcweir                 rString.append(SetQuotation( sTableOrQueryName,
640cdf0e10cSrcweir                     rParam.aMetaData.getIdentifierQuoteString(), rParam.aMetaData.getIdentifierQuoteString() ));
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir 
643cdf0e10cSrcweir         // don't forget to remove the query name from the history, else multiple inclusions
644cdf0e10cSrcweir         // won't work
645cdf0e10cSrcweir         // #i69227# / 2006-10-10 / frank.schoenheit@sun.com
646cdf0e10cSrcweir         rParam.pSubQueryHistory->erase( sTableOrQueryName );
647cdf0e10cSrcweir 
648cdf0e10cSrcweir         return true;
649cdf0e10cSrcweir     }
650cdf0e10cSrcweir     catch( const SQLException& )
651cdf0e10cSrcweir     {
652cdf0e10cSrcweir         throw;
653cdf0e10cSrcweir     }
654cdf0e10cSrcweir     catch( const Exception& )
655cdf0e10cSrcweir     {
656cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
657cdf0e10cSrcweir     }
658cdf0e10cSrcweir     return false;
659cdf0e10cSrcweir }
660cdf0e10cSrcweir 
661cdf0e10cSrcweir //-----------------------------------------------------------------------------
662cdf0e10cSrcweir void OSQLParseNode::impl_parseTableRangeNodeToString_throw(::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
663cdf0e10cSrcweir {
664cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseTableRangeNodeToString_throw" );
665cdf0e10cSrcweir     OSL_PRECOND(  ( count() == 2 ) || ( count() == 3 ) || ( count() == 5 ) ,"Illegal count");
666cdf0e10cSrcweir 
667cdf0e10cSrcweir 	// rString += ::rtl::OUString::createFromAscii(" ");
668cdf0e10cSrcweir     ::std::for_each(m_aChildren.begin(),m_aChildren.end(),
669cdf0e10cSrcweir         boost::bind( &OSQLParseNode::impl_parseNodeToString_throw, _1, boost::ref( rString ), boost::cref( rParam ) ));
670cdf0e10cSrcweir }
671cdf0e10cSrcweir 
672cdf0e10cSrcweir //-----------------------------------------------------------------------------
673cdf0e10cSrcweir void OSQLParseNode::impl_parseLikeNodeToString_throw( ::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const
674cdf0e10cSrcweir {
675cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::impl_parseLikeNodeToString_throw" );
676cdf0e10cSrcweir 	OSL_ENSURE(count() == 2,"count != 2: Prepare for GPF");
677cdf0e10cSrcweir 
678cdf0e10cSrcweir 	const OSQLParseNode* pEscNode = NULL;
679cdf0e10cSrcweir 	const OSQLParseNode* pParaNode = NULL;
680cdf0e10cSrcweir 
681cdf0e10cSrcweir 	SQLParseNodeParameter aNewParam(rParam);
682cdf0e10cSrcweir 	//aNewParam.bQuote = sal_True; // why setting this to true? @see http://www.openoffice.org/issues/show_bug.cgi?id=75557
683cdf0e10cSrcweir 
684cdf0e10cSrcweir 	// if there is a field given we don't display the fieldname, if there are any
685cdf0e10cSrcweir 	sal_Bool bAddName = sal_True;
686cdf0e10cSrcweir 	if (rParam.xField.is())
687cdf0e10cSrcweir 	{
688cdf0e10cSrcweir 		// retrieve the fields name
689cdf0e10cSrcweir 		::rtl::OUString aFieldName;
690cdf0e10cSrcweir 		try
691cdf0e10cSrcweir 		{
692cdf0e10cSrcweir 			// retrieve the fields name
693cdf0e10cSrcweir 			rtl::OUString aString;
694cdf0e10cSrcweir 			rParam.xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aString;
695cdf0e10cSrcweir 			aFieldName = aString.getStr();
696cdf0e10cSrcweir 		}
697cdf0e10cSrcweir 		catch ( Exception& )
698cdf0e10cSrcweir 		{
699cdf0e10cSrcweir 			OSL_ENSURE( false, "OSQLParseNode::impl_parseLikeNodeToString_throw Exception occured!" );
700cdf0e10cSrcweir 		}
701cdf0e10cSrcweir 		if ( !m_aChildren[0]->isLeaf() )
702cdf0e10cSrcweir 		{
703cdf0e10cSrcweir 			const OSQLParseNode* pCol = m_aChildren[0]->getChild(m_aChildren[0]->count()-1);
704cdf0e10cSrcweir 			if ((SQL_ISRULE(pCol,column_val) && pCol->getChild(0)->getTokenValue().equalsIgnoreAsciiCase(aFieldName)) ||
705cdf0e10cSrcweir 				pCol->getTokenValue().equalsIgnoreAsciiCase(aFieldName) )
706cdf0e10cSrcweir 				bAddName = sal_False;
707cdf0e10cSrcweir 		}
708cdf0e10cSrcweir 	}
709cdf0e10cSrcweir 
710cdf0e10cSrcweir 	if (bAddName)
711cdf0e10cSrcweir 		m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam );
712cdf0e10cSrcweir 
713cdf0e10cSrcweir     const OSQLParseNode* pPart2 = m_aChildren[1];
714cdf0e10cSrcweir 	pPart2->getChild(0)->impl_parseNodeToString_throw( rString, aNewParam );
715cdf0e10cSrcweir 	pPart2->getChild(1)->impl_parseNodeToString_throw( rString, aNewParam );
716cdf0e10cSrcweir     pParaNode = pPart2->getChild(2);
717cdf0e10cSrcweir 	pEscNode  = pPart2->getChild(3);
718cdf0e10cSrcweir 
719cdf0e10cSrcweir 	if (pParaNode->isToken())
720cdf0e10cSrcweir 	{
721cdf0e10cSrcweir 		::rtl::OUString aStr = ConvertLikeToken(pParaNode, pEscNode, rParam.bInternational);
722cdf0e10cSrcweir 		rString.appendAscii(" ");
723cdf0e10cSrcweir 		rString.append(SetQuotation(aStr,::rtl::OUString::createFromAscii("\'"),::rtl::OUString::createFromAscii("\'\'")));
724cdf0e10cSrcweir 	}
725cdf0e10cSrcweir 	else
726cdf0e10cSrcweir 		pParaNode->impl_parseNodeToString_throw( rString, aNewParam );
727cdf0e10cSrcweir 
728cdf0e10cSrcweir 	pEscNode->impl_parseNodeToString_throw( rString, aNewParam );
729cdf0e10cSrcweir }
730cdf0e10cSrcweir 
731cdf0e10cSrcweir 
732cdf0e10cSrcweir // -----------------------------------------------------------------------------
733cdf0e10cSrcweir sal_Bool OSQLParseNode::getTableComponents(const OSQLParseNode* _pTableNode,
734cdf0e10cSrcweir 											::com::sun::star::uno::Any &_rCatalog,
735cdf0e10cSrcweir 											::rtl::OUString &_rSchema,
736cdf0e10cSrcweir 											::rtl::OUString &_rTable,
737cdf0e10cSrcweir                                             const Reference< XDatabaseMetaData >& _xMetaData)
738cdf0e10cSrcweir {
739cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableComponents" );
740cdf0e10cSrcweir 	OSL_ENSURE(_pTableNode,"Wrong use of getTableComponents! _pTableNode is not allowed to be null!");
741cdf0e10cSrcweir 	if(_pTableNode)
742cdf0e10cSrcweir 	{
743cdf0e10cSrcweir         const sal_Bool bSupportsCatalog = _xMetaData.is() && _xMetaData->supportsCatalogsInDataManipulation();
744cdf0e10cSrcweir         const sal_Bool bSupportsSchema = _xMetaData.is() && _xMetaData->supportsSchemasInDataManipulation();
745cdf0e10cSrcweir 		const OSQLParseNode* pTableNode = _pTableNode;
746cdf0e10cSrcweir 		// clear the parameter given
747cdf0e10cSrcweir 		_rCatalog = Any();
748cdf0e10cSrcweir 		_rSchema = _rTable = ::rtl::OUString();
749cdf0e10cSrcweir 		// see rule catalog_name: in sqlbison.y
750cdf0e10cSrcweir 		if (SQL_ISRULE(pTableNode,catalog_name))
751cdf0e10cSrcweir 		{
752cdf0e10cSrcweir 			OSL_ENSURE(pTableNode->getChild(0) && pTableNode->getChild(0)->isToken(),"Invalid parsenode!");
753cdf0e10cSrcweir 			_rCatalog <<= pTableNode->getChild(0)->getTokenValue();
754cdf0e10cSrcweir 			pTableNode = pTableNode->getChild(2);
755cdf0e10cSrcweir 		}
756cdf0e10cSrcweir 		// check if we have schema_name rule
757cdf0e10cSrcweir 		if(SQL_ISRULE(pTableNode,schema_name))
758cdf0e10cSrcweir 		{
759cdf0e10cSrcweir             if ( bSupportsCatalog && !bSupportsSchema )
760cdf0e10cSrcweir                 _rCatalog <<= pTableNode->getChild(0)->getTokenValue();
761cdf0e10cSrcweir             else
762cdf0e10cSrcweir 			    _rSchema = pTableNode->getChild(0)->getTokenValue();
763cdf0e10cSrcweir 			pTableNode = pTableNode->getChild(2);
764cdf0e10cSrcweir 		}
765cdf0e10cSrcweir 		// check if we have table_name rule
766cdf0e10cSrcweir 		if(SQL_ISRULE(pTableNode,table_name))
767cdf0e10cSrcweir 		{
768cdf0e10cSrcweir 			_rTable = pTableNode->getChild(0)->getTokenValue();
769cdf0e10cSrcweir 		}
770cdf0e10cSrcweir 		else
771cdf0e10cSrcweir 		{
772cdf0e10cSrcweir 			OSL_ENSURE(0,"Error in parse tree!");
773cdf0e10cSrcweir 		}
774cdf0e10cSrcweir 	}
775cdf0e10cSrcweir 	return _rTable.getLength() != 0;
776cdf0e10cSrcweir }
777cdf0e10cSrcweir // -----------------------------------------------------------------------------
778cdf0e10cSrcweir void OSQLParser::killThousandSeparator(OSQLParseNode* pLiteral)
779cdf0e10cSrcweir {
780cdf0e10cSrcweir     if ( pLiteral )
781cdf0e10cSrcweir 	{
782cdf0e10cSrcweir         if ( s_xLocaleData->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
783cdf0e10cSrcweir 		{
784cdf0e10cSrcweir 		    pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace('.', sal_Unicode());
785cdf0e10cSrcweir 		    // and replace decimal
786cdf0e10cSrcweir 		    pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', '.');
787cdf0e10cSrcweir 		}
788cdf0e10cSrcweir 	    else
789cdf0e10cSrcweir 		    pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', sal_Unicode());
790cdf0e10cSrcweir 		}
791cdf0e10cSrcweir }
792cdf0e10cSrcweir // -----------------------------------------------------------------------------
793cdf0e10cSrcweir OSQLParseNode* OSQLParser::convertNode(sal_Int32 nType,OSQLParseNode*& pLiteral)
794cdf0e10cSrcweir {
795cdf0e10cSrcweir     if ( !pLiteral )
796cdf0e10cSrcweir         return NULL;
797cdf0e10cSrcweir 
798cdf0e10cSrcweir     OSQLParseNode* pReturn = pLiteral;
799cdf0e10cSrcweir 
800cdf0e10cSrcweir     if ( ( pLiteral->isRule() && !SQL_ISRULE(pLiteral,value_exp) ) || SQL_ISTOKEN(pLiteral,FALSE) || SQL_ISTOKEN(pLiteral,TRUE) )
801cdf0e10cSrcweir     {
802cdf0e10cSrcweir 		switch(nType)
803cdf0e10cSrcweir 		{
804cdf0e10cSrcweir 			case DataType::CHAR:
805cdf0e10cSrcweir 			case DataType::VARCHAR:
806cdf0e10cSrcweir 			case DataType::LONGVARCHAR:
807cdf0e10cSrcweir 			case DataType::CLOB:
808cdf0e10cSrcweir 				if ( !SQL_ISRULE(pReturn,char_value_exp) && !buildStringNodes(pReturn) )
809cdf0e10cSrcweir 					pReturn = NULL;
810cdf0e10cSrcweir 			default:
811cdf0e10cSrcweir 			    break;
812cdf0e10cSrcweir 		}
813cdf0e10cSrcweir 	}
814cdf0e10cSrcweir 	else
815cdf0e10cSrcweir 	{
816cdf0e10cSrcweir 		switch(pLiteral->getNodeType())
817cdf0e10cSrcweir 		{
818cdf0e10cSrcweir         case SQL_NODE_STRING:
819cdf0e10cSrcweir             switch(nType)
820cdf0e10cSrcweir             {
821cdf0e10cSrcweir                 case DataType::CHAR:
822cdf0e10cSrcweir                 case DataType::VARCHAR:
823cdf0e10cSrcweir                 case DataType::LONGVARCHAR:
824cdf0e10cSrcweir 				case DataType::CLOB:
825cdf0e10cSrcweir                     break;
826cdf0e10cSrcweir                 case DataType::DATE:
827cdf0e10cSrcweir                 case DataType::TIME:
828cdf0e10cSrcweir                 case DataType::TIMESTAMP:
829cdf0e10cSrcweir                     if (m_xFormatter.is())
830cdf0e10cSrcweir                     pReturn = buildDate( nType, pReturn);
831cdf0e10cSrcweir                     break;
832cdf0e10cSrcweir                 default:
833cdf0e10cSrcweir                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE);
834cdf0e10cSrcweir                     break;
835cdf0e10cSrcweir             }
836cdf0e10cSrcweir             break;
837cdf0e10cSrcweir         case SQL_NODE_ACCESS_DATE:
838cdf0e10cSrcweir             switch(nType)
839cdf0e10cSrcweir             {
840cdf0e10cSrcweir                 case DataType::DATE:
841cdf0e10cSrcweir                 case DataType::TIME:
842cdf0e10cSrcweir                 case DataType::TIMESTAMP:
843cdf0e10cSrcweir                 if ( m_xFormatter.is() )
844cdf0e10cSrcweir                     pReturn = buildDate( nType, pReturn);
845cdf0e10cSrcweir                     else
846cdf0e10cSrcweir                         m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE);
847cdf0e10cSrcweir                     break;
848cdf0e10cSrcweir                 default:
849cdf0e10cSrcweir                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE);
850cdf0e10cSrcweir                     break;
851cdf0e10cSrcweir             }
852cdf0e10cSrcweir             break;
853cdf0e10cSrcweir         case SQL_NODE_INTNUM:
854cdf0e10cSrcweir             switch(nType)
855cdf0e10cSrcweir             {
856cdf0e10cSrcweir                 case DataType::BIT:
857cdf0e10cSrcweir                 case DataType::BOOLEAN:
858cdf0e10cSrcweir                 case DataType::DECIMAL:
859cdf0e10cSrcweir                 case DataType::NUMERIC:
860cdf0e10cSrcweir                 case DataType::TINYINT:
861cdf0e10cSrcweir                 case DataType::SMALLINT:
862cdf0e10cSrcweir                 case DataType::INTEGER:
863cdf0e10cSrcweir                 case DataType::BIGINT:
864cdf0e10cSrcweir                 case DataType::FLOAT:
865cdf0e10cSrcweir                 case DataType::REAL:
866cdf0e10cSrcweir                 case DataType::DOUBLE:
867cdf0e10cSrcweir                     // kill thousand seperators if any
868cdf0e10cSrcweir 					killThousandSeparator(pReturn);
869cdf0e10cSrcweir                     break;
870cdf0e10cSrcweir                 case DataType::CHAR:
871cdf0e10cSrcweir                 case DataType::VARCHAR:
872cdf0e10cSrcweir                 case DataType::LONGVARCHAR:
873cdf0e10cSrcweir 				case DataType::CLOB:
874cdf0e10cSrcweir 					pReturn = buildNode_STR_NUM(pReturn);
875cdf0e10cSrcweir                     break;
876cdf0e10cSrcweir                 default:
877cdf0e10cSrcweir                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_INT_COMPARE);
878cdf0e10cSrcweir                     break;
879cdf0e10cSrcweir             }
880cdf0e10cSrcweir             break;
881cdf0e10cSrcweir         case SQL_NODE_APPROXNUM:
882cdf0e10cSrcweir             switch(nType)
883cdf0e10cSrcweir             {
884cdf0e10cSrcweir                 case DataType::DECIMAL:
885cdf0e10cSrcweir                 case DataType::NUMERIC:
886cdf0e10cSrcweir                 case DataType::FLOAT:
887cdf0e10cSrcweir                 case DataType::REAL:
888cdf0e10cSrcweir                 case DataType::DOUBLE:
889cdf0e10cSrcweir                         // kill thousand seperators if any
890cdf0e10cSrcweir 					killThousandSeparator(pReturn);
891cdf0e10cSrcweir                     break;
892cdf0e10cSrcweir                 case DataType::CHAR:
893cdf0e10cSrcweir                 case DataType::VARCHAR:
894cdf0e10cSrcweir                 case DataType::LONGVARCHAR:
895cdf0e10cSrcweir 				case DataType::CLOB:
896cdf0e10cSrcweir 					pReturn = buildNode_STR_NUM(pReturn);
897cdf0e10cSrcweir                     break;
898cdf0e10cSrcweir                 case DataType::INTEGER:
899cdf0e10cSrcweir                 default:
900cdf0e10cSrcweir                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_REAL_COMPARE);
901cdf0e10cSrcweir                     break;
902cdf0e10cSrcweir             }
903cdf0e10cSrcweir             break;
904cdf0e10cSrcweir         default:
905cdf0e10cSrcweir             ;
906cdf0e10cSrcweir 		}
907cdf0e10cSrcweir 	}
908cdf0e10cSrcweir     return pReturn;
909cdf0e10cSrcweir }
910cdf0e10cSrcweir // -----------------------------------------------------------------------------
911cdf0e10cSrcweir sal_Int16 OSQLParser::buildPredicateRule(OSQLParseNode*& pAppend,OSQLParseNode* pLiteral,OSQLParseNode*& pCompare,OSQLParseNode* pLiteral2)
912cdf0e10cSrcweir {
913cdf0e10cSrcweir     OSL_ENSURE(inPredicateCheck(),"Only in predicate check allowed!");
914cdf0e10cSrcweir 	sal_Int16 nErg = 0;
915cdf0e10cSrcweir 	if ( m_xField.is() )
916cdf0e10cSrcweir 	{
917cdf0e10cSrcweir         sal_Int32 nType = 0;
918cdf0e10cSrcweir 	    try
919cdf0e10cSrcweir 	    {
920cdf0e10cSrcweir 		    m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
921cdf0e10cSrcweir 	    }
922cdf0e10cSrcweir 	    catch( Exception& )
923cdf0e10cSrcweir 	    {
924cdf0e10cSrcweir 		    return nErg;
925cdf0e10cSrcweir 		}
926cdf0e10cSrcweir 
927cdf0e10cSrcweir         OSQLParseNode* pNode1 = convertNode(nType,pLiteral);
928cdf0e10cSrcweir         if ( pNode1 )
929cdf0e10cSrcweir         {
930cdf0e10cSrcweir             OSQLParseNode* pNode2 = convertNode(nType,pLiteral2);
931cdf0e10cSrcweir             if ( !m_sErrorMessage.getLength() )
932cdf0e10cSrcweir                 nErg = buildNode(pAppend,pCompare,pNode1,pNode2);
933cdf0e10cSrcweir 		}
934cdf0e10cSrcweir 	}
935cdf0e10cSrcweir 	if (!pCompare->getParent()) // I have no parent so I was not used and I must die :-)
936cdf0e10cSrcweir 		delete pCompare;
937cdf0e10cSrcweir 	return nErg;
938cdf0e10cSrcweir }
939cdf0e10cSrcweir // -----------------------------------------------------------------------------
940cdf0e10cSrcweir sal_Int16 OSQLParser::buildLikeRule(OSQLParseNode*& pAppend, OSQLParseNode*& pLiteral, const OSQLParseNode* pEscape)
941cdf0e10cSrcweir {
942cdf0e10cSrcweir 	sal_Int16 nErg = 0;
943cdf0e10cSrcweir 	sal_Int32 nType = 0;
944cdf0e10cSrcweir 
945cdf0e10cSrcweir 	if (!m_xField.is())
946cdf0e10cSrcweir 		return nErg;
947cdf0e10cSrcweir 	try
948cdf0e10cSrcweir 	{
949cdf0e10cSrcweir 		Any aValue;
950cdf0e10cSrcweir 		{
951cdf0e10cSrcweir 			aValue = m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE));
952cdf0e10cSrcweir 			aValue >>= nType;
953cdf0e10cSrcweir 		}
954cdf0e10cSrcweir 	}
955cdf0e10cSrcweir 	catch( Exception& )
956cdf0e10cSrcweir 	{
957cdf0e10cSrcweir 		return nErg;
958cdf0e10cSrcweir 	}
959cdf0e10cSrcweir 
960cdf0e10cSrcweir 	switch (nType)
961cdf0e10cSrcweir 	{
962cdf0e10cSrcweir 		case DataType::CHAR:
963cdf0e10cSrcweir 		case DataType::VARCHAR:
964cdf0e10cSrcweir 		case DataType::LONGVARCHAR:
965cdf0e10cSrcweir 		case DataType::CLOB:
966cdf0e10cSrcweir 			if(pLiteral->isRule())
967cdf0e10cSrcweir 			{
968cdf0e10cSrcweir 				pAppend->append(pLiteral);
969cdf0e10cSrcweir 				nErg = 1;
970cdf0e10cSrcweir 			}
971cdf0e10cSrcweir 			else
972cdf0e10cSrcweir 			{
973cdf0e10cSrcweir 				switch(pLiteral->getNodeType())
974cdf0e10cSrcweir 				{
975cdf0e10cSrcweir 					case SQL_NODE_STRING:
976cdf0e10cSrcweir 						pLiteral->m_aNodeValue = ConvertLikeToken(pLiteral, pEscape, sal_False);
977cdf0e10cSrcweir 						pAppend->append(pLiteral);
978cdf0e10cSrcweir 						nErg = 1;
979cdf0e10cSrcweir 						break;
980cdf0e10cSrcweir 					case SQL_NODE_APPROXNUM:
981cdf0e10cSrcweir 						if (m_xFormatter.is() && m_nFormatKey)
982cdf0e10cSrcweir 						{
983cdf0e10cSrcweir 							sal_Int16 nScale = 0;
984cdf0e10cSrcweir 							try
985cdf0e10cSrcweir 							{
986cdf0e10cSrcweir 								Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, ::rtl::OUString::createFromAscii("Decimals") );
987cdf0e10cSrcweir 								aValue >>= nScale;
988cdf0e10cSrcweir 							}
989cdf0e10cSrcweir 							catch( Exception& )
990cdf0e10cSrcweir 							{
991cdf0e10cSrcweir 							}
992cdf0e10cSrcweir 
993cdf0e10cSrcweir 							pAppend->append(new OSQLInternalNode(stringToDouble(pLiteral->getTokenValue(),nScale),SQL_NODE_STRING));
994cdf0e10cSrcweir 						}
995cdf0e10cSrcweir 						else
996cdf0e10cSrcweir 							pAppend->append(new OSQLInternalNode(pLiteral->getTokenValue(),SQL_NODE_STRING));
997cdf0e10cSrcweir 
998cdf0e10cSrcweir 						delete pLiteral;
999cdf0e10cSrcweir 						nErg = 1;
1000cdf0e10cSrcweir 						break;
1001cdf0e10cSrcweir 					default:
1002cdf0e10cSrcweir 						m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_VALUE_NO_LIKE);
1003cdf0e10cSrcweir 						m_sErrorMessage = m_sErrorMessage.replaceAt(m_sErrorMessage.indexOf(::rtl::OUString::createFromAscii("#1")),2,pLiteral->getTokenValue());
1004cdf0e10cSrcweir                         break;
1005cdf0e10cSrcweir 				}
1006cdf0e10cSrcweir 			}
1007cdf0e10cSrcweir 			break;
1008cdf0e10cSrcweir 		default:
1009cdf0e10cSrcweir 			m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_FIELD_NO_LIKE);
1010cdf0e10cSrcweir             break;
1011cdf0e10cSrcweir 	}
1012cdf0e10cSrcweir 	return nErg;
1013cdf0e10cSrcweir }
1014cdf0e10cSrcweir //-----------------------------------------------------------------------------
1015cdf0e10cSrcweir OSQLParseNode* OSQLParser::buildNode_Date(const double& fValue, sal_Int32 nType)
1016cdf0e10cSrcweir {
1017cdf0e10cSrcweir     ::rtl::OUString aEmptyString;
1018cdf0e10cSrcweir     OSQLParseNode* pNewNode = new OSQLInternalNode(aEmptyString, SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::set_fct_spec));
1019cdf0e10cSrcweir 	pNewNode->append(new OSQLInternalNode(::rtl::OUString::createFromAscii("{"), SQL_NODE_PUNCTUATION));
1020cdf0e10cSrcweir 	OSQLParseNode* pDateNode = new OSQLInternalNode(aEmptyString, SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::odbc_fct_spec));
1021cdf0e10cSrcweir 	pNewNode->append(pDateNode);
1022cdf0e10cSrcweir 	pNewNode->append(new OSQLInternalNode(::rtl::OUString::createFromAscii("}"), SQL_NODE_PUNCTUATION));
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir 	switch (nType)
1025cdf0e10cSrcweir 	{
1026cdf0e10cSrcweir 		case DataType::DATE:
1027cdf0e10cSrcweir 		{
1028cdf0e10cSrcweir 			Date aDate = DBTypeConversion::toDate(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
1029cdf0e10cSrcweir 			::rtl::OUString aString = DBTypeConversion::toDateString(aDate);
1030cdf0e10cSrcweir 			pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_D));
1031cdf0e10cSrcweir 			pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
1032cdf0e10cSrcweir 			break;
1033cdf0e10cSrcweir 		}
1034cdf0e10cSrcweir 		case DataType::TIME:
1035cdf0e10cSrcweir 		{
1036cdf0e10cSrcweir 			Time aTime = DBTypeConversion::toTime(fValue);
1037cdf0e10cSrcweir 			::rtl::OUString aString = DBTypeConversion::toTimeString(aTime);
1038cdf0e10cSrcweir 			pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_T));
1039cdf0e10cSrcweir 			pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
1040cdf0e10cSrcweir 			break;
1041cdf0e10cSrcweir 		}
1042cdf0e10cSrcweir 		case DataType::TIMESTAMP:
1043cdf0e10cSrcweir 		{
1044cdf0e10cSrcweir 			DateTime aDateTime = DBTypeConversion::toDateTime(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
1045cdf0e10cSrcweir 			if (aDateTime.Seconds || aDateTime.Minutes || aDateTime.Hours)
1046cdf0e10cSrcweir 			{
1047cdf0e10cSrcweir 				::rtl::OUString aString = DBTypeConversion::toDateTimeString(aDateTime);
1048cdf0e10cSrcweir 				pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_TS));
1049cdf0e10cSrcweir 				pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
1050cdf0e10cSrcweir 			}
1051cdf0e10cSrcweir 			else
1052cdf0e10cSrcweir 			{
1053cdf0e10cSrcweir 				Date aDate(aDateTime.Day,aDateTime.Month,aDateTime.Year);
1054cdf0e10cSrcweir 				pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_D));
1055cdf0e10cSrcweir 				pDateNode->append(new OSQLInternalNode(DBTypeConversion::toDateString(aDate), SQL_NODE_STRING));
1056cdf0e10cSrcweir 			}
1057cdf0e10cSrcweir 			break;
1058cdf0e10cSrcweir 		}
1059cdf0e10cSrcweir 	}
1060cdf0e10cSrcweir 
1061cdf0e10cSrcweir 	return pNewNode;
1062cdf0e10cSrcweir }
1063cdf0e10cSrcweir // -----------------------------------------------------------------------------
1064cdf0e10cSrcweir OSQLParseNode* OSQLParser::buildNode_STR_NUM(OSQLParseNode*& _pLiteral)
1065cdf0e10cSrcweir {
1066cdf0e10cSrcweir 	OSQLParseNode* pReturn = NULL;
1067cdf0e10cSrcweir     if ( _pLiteral )
1068cdf0e10cSrcweir     {
1069cdf0e10cSrcweir 	    if (m_nFormatKey)
1070cdf0e10cSrcweir 	    {
1071cdf0e10cSrcweir 		    sal_Int16 nScale = 0;
1072cdf0e10cSrcweir 		    ::rtl::OUString aDec;
1073cdf0e10cSrcweir 		    try
1074cdf0e10cSrcweir 		    {
1075cdf0e10cSrcweir                 Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Decimals")) );
1076cdf0e10cSrcweir 			    aValue >>= nScale;
1077cdf0e10cSrcweir 		    }
1078cdf0e10cSrcweir 		    catch( Exception& )
1079cdf0e10cSrcweir 		    {
1080cdf0e10cSrcweir 		    }
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir             pReturn = new OSQLInternalNode(stringToDouble(_pLiteral->getTokenValue(),nScale),SQL_NODE_STRING);
1083cdf0e10cSrcweir 	    }
1084cdf0e10cSrcweir 	    else
1085cdf0e10cSrcweir             pReturn = new OSQLInternalNode(_pLiteral->getTokenValue(),SQL_NODE_STRING);
1086cdf0e10cSrcweir 
1087cdf0e10cSrcweir         delete _pLiteral;
1088cdf0e10cSrcweir         _pLiteral = NULL;
1089cdf0e10cSrcweir     }
1090cdf0e10cSrcweir     return pReturn;
1091cdf0e10cSrcweir }
1092cdf0e10cSrcweir // -----------------------------------------------------------------------------
1093cdf0e10cSrcweir ::rtl::OUString OSQLParser::stringToDouble(const ::rtl::OUString& _rValue,sal_Int16 _nScale)
1094cdf0e10cSrcweir {
1095cdf0e10cSrcweir 	::rtl::OUString aValue;
1096cdf0e10cSrcweir 	if(!m_xCharClass.is())
1097cdf0e10cSrcweir 		m_xCharClass  = Reference<XCharacterClassification>(m_xServiceFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.i18n.CharacterClassification")),UNO_QUERY);
1098cdf0e10cSrcweir 	if(m_xCharClass.is() && s_xLocaleData.is())
1099cdf0e10cSrcweir 	{
1100cdf0e10cSrcweir 		try
1101cdf0e10cSrcweir 		{
1102cdf0e10cSrcweir 			ParseResult aResult = m_xCharClass->parsePredefinedToken(KParseType::ANY_NUMBER,_rValue,0,m_pData->aLocale,0,::rtl::OUString(),KParseType::ANY_NUMBER,::rtl::OUString());
1103cdf0e10cSrcweir 			if((aResult.TokenType & KParseType::IDENTNAME) && aResult.EndPos == _rValue.getLength())
1104cdf0e10cSrcweir 			{
1105cdf0e10cSrcweir 				aValue = ::rtl::OUString::valueOf(aResult.Value);
1106cdf0e10cSrcweir 				sal_Int32 nPos = aValue.lastIndexOf(::rtl::OUString::createFromAscii("."));
1107cdf0e10cSrcweir 				if((nPos+_nScale) < aValue.getLength())
1108cdf0e10cSrcweir 					aValue = aValue.replaceAt(nPos+_nScale,aValue.getLength()-nPos-_nScale,::rtl::OUString());
1109cdf0e10cSrcweir 				aValue = aValue.replaceAt(aValue.lastIndexOf(::rtl::OUString::createFromAscii(".")),1,s_xLocaleData->getLocaleItem(m_pData->aLocale).decimalSeparator);
1110cdf0e10cSrcweir 				return aValue;
1111cdf0e10cSrcweir 			}
1112cdf0e10cSrcweir 		}
1113cdf0e10cSrcweir 		catch(Exception&)
1114cdf0e10cSrcweir 		{
1115cdf0e10cSrcweir 		}
1116cdf0e10cSrcweir 	}
1117cdf0e10cSrcweir 	return aValue;
1118cdf0e10cSrcweir }
1119cdf0e10cSrcweir // -----------------------------------------------------------------------------
1120cdf0e10cSrcweir 
1121cdf0e10cSrcweir ::osl::Mutex& OSQLParser::getMutex()
1122cdf0e10cSrcweir {
1123cdf0e10cSrcweir 	static ::osl::Mutex aMutex;
1124cdf0e10cSrcweir 	return aMutex;
1125cdf0e10cSrcweir }
1126cdf0e10cSrcweir 
1127cdf0e10cSrcweir //-----------------------------------------------------------------------------
1128cdf0e10cSrcweir OSQLParseNode* OSQLParser::predicateTree(::rtl::OUString& rErrorMessage, const ::rtl::OUString& rStatement,
1129cdf0e10cSrcweir  										 const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter,
1130cdf0e10cSrcweir 										 const Reference< XPropertySet > & xField)
1131cdf0e10cSrcweir {
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir 	// mutex for parsing
1135cdf0e10cSrcweir 	static ::osl::Mutex aMutex;
1136cdf0e10cSrcweir 
1137cdf0e10cSrcweir 	// Guard the parsing
1138cdf0e10cSrcweir 	::osl::MutexGuard aGuard(getMutex());
1139cdf0e10cSrcweir 	// must be reset
1140cdf0e10cSrcweir 	setParser(this);
1141cdf0e10cSrcweir 
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir 	// reset the parser
1144cdf0e10cSrcweir 	m_xField		= xField;
1145cdf0e10cSrcweir 	m_xFormatter	= xFormatter;
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir 	if (m_xField.is())
1148cdf0e10cSrcweir 	{
1149cdf0e10cSrcweir 		sal_Int32 nType=0;
1150cdf0e10cSrcweir 		try
1151cdf0e10cSrcweir 		{
1152cdf0e10cSrcweir 			// get the field name
1153cdf0e10cSrcweir 			rtl::OUString aString;
1154cdf0e10cSrcweir 
1155cdf0e10cSrcweir 			// retrieve the fields name
1156cdf0e10cSrcweir 			// #75243# use the RealName of the column if there is any otherwise the name which could be the alias
1157cdf0e10cSrcweir 			// of the field
1158cdf0e10cSrcweir             Reference< XPropertySetInfo> xInfo = m_xField->getPropertySetInfo();
1159cdf0e10cSrcweir 			if ( xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)))
1160cdf0e10cSrcweir 				m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)) >>= aString;
1161cdf0e10cSrcweir 			else
1162cdf0e10cSrcweir 				m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aString;
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir 			m_sFieldName = aString;
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir 			// get the field format key
1167cdf0e10cSrcweir 			if ( xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)))
1168cdf0e10cSrcweir 				m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= m_nFormatKey;
1169cdf0e10cSrcweir 			else
1170cdf0e10cSrcweir 				m_nFormatKey = 0;
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir 			// get the field type
1173cdf0e10cSrcweir 			m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
1174cdf0e10cSrcweir 		}
1175cdf0e10cSrcweir 		catch ( Exception& )
1176cdf0e10cSrcweir 		{
1177cdf0e10cSrcweir 			OSL_ASSERT(0);
1178cdf0e10cSrcweir 		}
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir 		if (m_nFormatKey && m_xFormatter.is())
1181cdf0e10cSrcweir 		{
1182cdf0e10cSrcweir 			Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_LOCALE) );
1183cdf0e10cSrcweir 			OSL_ENSURE(aValue.getValueType() == ::getCppuType((const ::com::sun::star::lang::Locale*)0), "OSQLParser::PredicateTree : invalid language property !");
1184cdf0e10cSrcweir 
1185cdf0e10cSrcweir 			if (aValue.getValueType() == ::getCppuType((const ::com::sun::star::lang::Locale*)0))
1186cdf0e10cSrcweir 				aValue >>= m_pData->aLocale;
1187cdf0e10cSrcweir 		}
1188cdf0e10cSrcweir 		else
1189cdf0e10cSrcweir 			m_pData->aLocale = m_pContext->getPreferredLocale();
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir 		if ( m_xFormatter.is() )
1192cdf0e10cSrcweir 		{
1193cdf0e10cSrcweir 			try
1194cdf0e10cSrcweir 			{
1195cdf0e10cSrcweir 				Reference< ::com::sun::star::util::XNumberFormatsSupplier >  xFormatSup = m_xFormatter->getNumberFormatsSupplier();
1196cdf0e10cSrcweir 				if ( xFormatSup.is() )
1197cdf0e10cSrcweir 				{
1198cdf0e10cSrcweir 					Reference< ::com::sun::star::util::XNumberFormats >  xFormats = xFormatSup->getNumberFormats();
1199cdf0e10cSrcweir 					if ( xFormats.is() )
1200cdf0e10cSrcweir 					{
1201cdf0e10cSrcweir 						::com::sun::star::lang::Locale aLocale;
1202cdf0e10cSrcweir 						aLocale.Language = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en"));
1203cdf0e10cSrcweir 						aLocale.Country = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("US"));
1204cdf0e10cSrcweir 						::rtl::OUString sFormat(RTL_CONSTASCII_USTRINGPARAM("YYYY-MM-DD"));
1205cdf0e10cSrcweir 						m_nDateFormatKey = xFormats->queryKey(sFormat,aLocale,sal_False);
1206cdf0e10cSrcweir 						if ( m_nDateFormatKey == sal_Int32(-1) )
1207cdf0e10cSrcweir 							m_nDateFormatKey = xFormats->addNew(sFormat, aLocale);
1208cdf0e10cSrcweir 					}
1209cdf0e10cSrcweir 				}
1210cdf0e10cSrcweir 			}
1211cdf0e10cSrcweir 			catch ( Exception& )
1212cdf0e10cSrcweir 			{
1213cdf0e10cSrcweir 				OSL_ENSURE(0,"DateFormatKey");
1214cdf0e10cSrcweir 			}
1215cdf0e10cSrcweir 		}
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir 		switch (nType)
1218cdf0e10cSrcweir 		{
1219cdf0e10cSrcweir 			case DataType::DATE:
1220cdf0e10cSrcweir 			case DataType::TIME:
1221cdf0e10cSrcweir 			case DataType::TIMESTAMP:
1222cdf0e10cSrcweir 				s_pScanner->SetRule(s_pScanner->GetDATERule());
1223cdf0e10cSrcweir 				break;
1224cdf0e10cSrcweir 			case DataType::CHAR:
1225cdf0e10cSrcweir 			case DataType::VARCHAR:
1226cdf0e10cSrcweir 			case DataType::LONGVARCHAR:
1227cdf0e10cSrcweir 			case DataType::CLOB:
1228cdf0e10cSrcweir 				s_pScanner->SetRule(s_pScanner->GetSTRINGRule());
1229cdf0e10cSrcweir 				break;
1230cdf0e10cSrcweir 			default:
1231cdf0e10cSrcweir 				if ( s_xLocaleData->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
1232cdf0e10cSrcweir 					s_pScanner->SetRule(s_pScanner->GetGERRule());
1233cdf0e10cSrcweir 				else
1234cdf0e10cSrcweir 					s_pScanner->SetRule(s_pScanner->GetENGRule());
1235cdf0e10cSrcweir 		}
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir 	}
1238cdf0e10cSrcweir 	else
1239cdf0e10cSrcweir 		s_pScanner->SetRule(s_pScanner->GetSQLRule());
1240cdf0e10cSrcweir 
1241cdf0e10cSrcweir 	s_pScanner->prepareScan(rStatement, m_pContext, sal_True);
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir 	SQLyylval.pParseNode = NULL;
1244cdf0e10cSrcweir 	//	SQLyypvt = NULL;
1245cdf0e10cSrcweir 	m_pParseTree = NULL;
1246cdf0e10cSrcweir 	m_sErrorMessage= ::rtl::OUString();
1247cdf0e10cSrcweir 
1248cdf0e10cSrcweir 	// ... und den Parser anwerfen ...
1249cdf0e10cSrcweir 	if (SQLyyparse() != 0)
1250cdf0e10cSrcweir 	{
1251cdf0e10cSrcweir 		m_sFieldName= ::rtl::OUString();
1252cdf0e10cSrcweir 	m_xField.clear();
1253cdf0e10cSrcweir 	m_xFormatter.clear();
1254cdf0e10cSrcweir 		m_nFormatKey = 0;
1255cdf0e10cSrcweir 		m_nDateFormatKey = 0;
1256cdf0e10cSrcweir 
1257cdf0e10cSrcweir 		if (!m_sErrorMessage.getLength())
1258cdf0e10cSrcweir 			m_sErrorMessage = s_pScanner->getErrorMessage();
1259cdf0e10cSrcweir 		if (!m_sErrorMessage.getLength())
1260cdf0e10cSrcweir 			m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_GENERAL);
1261cdf0e10cSrcweir 
1262cdf0e10cSrcweir 		rErrorMessage = m_sErrorMessage;
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir 		// clear the garbage collector
1265cdf0e10cSrcweir         (*s_pGarbageCollector)->clearAndDelete();
1266cdf0e10cSrcweir 		return NULL;
1267cdf0e10cSrcweir 	}
1268cdf0e10cSrcweir 	else
1269cdf0e10cSrcweir 	{
1270cdf0e10cSrcweir 		(*s_pGarbageCollector)->clear();
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir 		m_sFieldName= ::rtl::OUString();
1273cdf0e10cSrcweir 	m_xField.clear();
1274cdf0e10cSrcweir 	m_xFormatter.clear();
1275cdf0e10cSrcweir 		m_nFormatKey = 0;
1276cdf0e10cSrcweir 		m_nDateFormatKey = 0;
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir 		// Das Ergebnis liefern (den Root Parse Node):
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir 		// Stattdessen setzt die Parse-Routine jetzt den Member pParseTree
1281cdf0e10cSrcweir 		// - einfach diesen zurueckliefern:
1282cdf0e10cSrcweir 		OSL_ENSURE(m_pParseTree != NULL,"OSQLParser: Parser hat keinen ParseTree geliefert");
1283cdf0e10cSrcweir 		return m_pParseTree;
1284cdf0e10cSrcweir 	}
1285cdf0e10cSrcweir }
1286cdf0e10cSrcweir 
1287cdf0e10cSrcweir //=============================================================================
1288cdf0e10cSrcweir //-----------------------------------------------------------------------------
1289cdf0e10cSrcweir OSQLParser::OSQLParser(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xServiceFactory,const IParseContext* _pContext)
1290cdf0e10cSrcweir 	:m_pContext(_pContext)
1291cdf0e10cSrcweir 	,m_pParseTree(NULL)
1292cdf0e10cSrcweir 	,m_pData( new OSQLParser_Data( _xServiceFactory ) )
1293cdf0e10cSrcweir 	,m_nFormatKey(0)
1294cdf0e10cSrcweir 	,m_nDateFormatKey(0)
1295cdf0e10cSrcweir 	,m_xServiceFactory(_xServiceFactory)
1296cdf0e10cSrcweir {
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir 
1299cdf0e10cSrcweir 	setParser(this);
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir #ifdef SQLYYDEBUG
1302cdf0e10cSrcweir #ifdef SQLYYDEBUG_ON
1303cdf0e10cSrcweir 	SQLyydebug = 1;
1304cdf0e10cSrcweir #endif
1305cdf0e10cSrcweir #endif
1306cdf0e10cSrcweir 
1307cdf0e10cSrcweir 	::osl::MutexGuard aGuard(getMutex());
1308cdf0e10cSrcweir 	// do we have to initialize the data
1309cdf0e10cSrcweir 	if (s_nRefCount == 0)
1310cdf0e10cSrcweir 	{
1311cdf0e10cSrcweir 		s_pScanner = new OSQLScanner();
1312cdf0e10cSrcweir 		s_pScanner->setScanner();
1313cdf0e10cSrcweir 		s_pGarbageCollector = new OSQLParseNodesGarbageCollector();
1314cdf0e10cSrcweir 
1315cdf0e10cSrcweir 		if(!s_xLocaleData.is())
1316cdf0e10cSrcweir 			s_xLocaleData = Reference<XLocaleData>(m_xServiceFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.i18n.LocaleData")),UNO_QUERY);
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir 		// auf 0 zuruecksetzen
1319cdf0e10cSrcweir 		memset(OSQLParser::s_nRuleIDs,0,sizeof(OSQLParser::s_nRuleIDs[0]) * (OSQLParseNode::rule_count+1));
1320cdf0e10cSrcweir 
1321cdf0e10cSrcweir         struct
1322cdf0e10cSrcweir         {
1323cdf0e10cSrcweir             OSQLParseNode::Rule eRule;      // the parse node's ID for the rule
1324cdf0e10cSrcweir             ::rtl::OString      sRuleName;  // the name of the rule ("select_statement")
1325cdf0e10cSrcweir         }   aRuleDescriptions[] =
1326cdf0e10cSrcweir         {
1327cdf0e10cSrcweir             { OSQLParseNode::select_statement, "select_statement" },
1328cdf0e10cSrcweir             { OSQLParseNode::table_exp, "table_exp" },
1329cdf0e10cSrcweir             { OSQLParseNode::table_ref_commalist, "table_ref_commalist" },
1330cdf0e10cSrcweir             { OSQLParseNode::table_ref, "table_ref" },
1331cdf0e10cSrcweir             { OSQLParseNode::catalog_name, "catalog_name" },
1332cdf0e10cSrcweir             { OSQLParseNode::schema_name, "schema_name" },
1333cdf0e10cSrcweir             { OSQLParseNode::table_name, "table_name" },
1334cdf0e10cSrcweir             { OSQLParseNode::opt_column_commalist, "opt_column_commalist" },
1335cdf0e10cSrcweir             { OSQLParseNode::column_commalist, "column_commalist" },
1336cdf0e10cSrcweir             { OSQLParseNode::column_ref_commalist, "column_ref_commalist" },
1337cdf0e10cSrcweir             { OSQLParseNode::column_ref, "column_ref" },
1338cdf0e10cSrcweir             { OSQLParseNode::opt_order_by_clause, "opt_order_by_clause" },
1339cdf0e10cSrcweir             { OSQLParseNode::ordering_spec_commalist, "ordering_spec_commalist" },
1340cdf0e10cSrcweir             { OSQLParseNode::ordering_spec, "ordering_spec" },
1341cdf0e10cSrcweir             { OSQLParseNode::opt_asc_desc, "opt_asc_desc" },
1342cdf0e10cSrcweir             { OSQLParseNode::where_clause, "where_clause" },
1343cdf0e10cSrcweir             { OSQLParseNode::opt_where_clause, "opt_where_clause" },
1344cdf0e10cSrcweir             { OSQLParseNode::search_condition, "search_condition" },
1345cdf0e10cSrcweir             { OSQLParseNode::comparison_predicate, "comparison_predicate" },
1346cdf0e10cSrcweir             { OSQLParseNode::between_predicate, "between_predicate" },
1347cdf0e10cSrcweir             { OSQLParseNode::like_predicate, "like_predicate" },
1348cdf0e10cSrcweir             { OSQLParseNode::opt_escape, "opt_escape" },
1349cdf0e10cSrcweir             { OSQLParseNode::test_for_null, "test_for_null" },
1350cdf0e10cSrcweir             { OSQLParseNode::scalar_exp_commalist, "scalar_exp_commalist" },
1351cdf0e10cSrcweir             { OSQLParseNode::scalar_exp, "scalar_exp" },
1352cdf0e10cSrcweir             { OSQLParseNode::parameter_ref, "parameter_ref" },
1353cdf0e10cSrcweir             { OSQLParseNode::parameter, "parameter" },
1354cdf0e10cSrcweir             { OSQLParseNode::general_set_fct, "general_set_fct" },
1355cdf0e10cSrcweir             { OSQLParseNode::range_variable, "range_variable" },
1356cdf0e10cSrcweir             { OSQLParseNode::column, "column" },
1357cdf0e10cSrcweir             { OSQLParseNode::delete_statement_positioned, "delete_statement_positioned" },
1358cdf0e10cSrcweir             { OSQLParseNode::delete_statement_searched, "delete_statement_searched" },
1359cdf0e10cSrcweir             { OSQLParseNode::update_statement_positioned, "update_statement_positioned" },
1360cdf0e10cSrcweir             { OSQLParseNode::update_statement_searched, "update_statement_searched" },
1361cdf0e10cSrcweir             { OSQLParseNode::assignment_commalist, "assignment_commalist" },
1362cdf0e10cSrcweir             { OSQLParseNode::assignment, "assignment" },
1363cdf0e10cSrcweir             { OSQLParseNode::values_or_query_spec, "values_or_query_spec" },
1364cdf0e10cSrcweir             { OSQLParseNode::insert_statement, "insert_statement" },
1365cdf0e10cSrcweir             { OSQLParseNode::insert_atom_commalist, "insert_atom_commalist" },
1366cdf0e10cSrcweir             { OSQLParseNode::insert_atom, "insert_atom" },
1367cdf0e10cSrcweir             { OSQLParseNode::predicate_check, "predicate_check" },
1368cdf0e10cSrcweir             { OSQLParseNode::from_clause, "from_clause" },
1369cdf0e10cSrcweir             { OSQLParseNode::qualified_join, "qualified_join" },
1370cdf0e10cSrcweir             { OSQLParseNode::cross_union, "cross_union" },
1371cdf0e10cSrcweir             { OSQLParseNode::select_sublist, "select_sublist" },
1372cdf0e10cSrcweir             { OSQLParseNode::derived_column, "derived_column" },
1373cdf0e10cSrcweir             { OSQLParseNode::column_val, "column_val" },
1374cdf0e10cSrcweir             { OSQLParseNode::set_fct_spec, "set_fct_spec" },
1375cdf0e10cSrcweir             { OSQLParseNode::boolean_term, "boolean_term" },
1376cdf0e10cSrcweir             { OSQLParseNode::boolean_primary, "boolean_primary" },
1377cdf0e10cSrcweir             { OSQLParseNode::num_value_exp, "num_value_exp" },
1378cdf0e10cSrcweir             { OSQLParseNode::join_type, "join_type" },
1379cdf0e10cSrcweir             { OSQLParseNode::position_exp, "position_exp" },
1380cdf0e10cSrcweir             { OSQLParseNode::extract_exp, "extract_exp" },
1381cdf0e10cSrcweir             { OSQLParseNode::length_exp, "length_exp" },
1382cdf0e10cSrcweir             { OSQLParseNode::char_value_fct, "char_value_fct" },
1383cdf0e10cSrcweir             { OSQLParseNode::odbc_call_spec, "odbc_call_spec" },
1384cdf0e10cSrcweir             { OSQLParseNode::in_predicate, "in_predicate" },
1385cdf0e10cSrcweir             { OSQLParseNode::existence_test, "existence_test" },
1386cdf0e10cSrcweir             { OSQLParseNode::unique_test, "unique_test" },
1387cdf0e10cSrcweir             { OSQLParseNode::all_or_any_predicate, "all_or_any_predicate" },
1388cdf0e10cSrcweir             { OSQLParseNode::named_columns_join, "named_columns_join" },
1389cdf0e10cSrcweir             { OSQLParseNode::join_condition, "join_condition" },
1390cdf0e10cSrcweir             { OSQLParseNode::joined_table, "joined_table" },
1391cdf0e10cSrcweir             { OSQLParseNode::boolean_factor, "boolean_factor" },
1392cdf0e10cSrcweir             { OSQLParseNode::sql_not, "sql_not" },
1393cdf0e10cSrcweir             { OSQLParseNode::boolean_test, "boolean_test" },
1394cdf0e10cSrcweir             { OSQLParseNode::manipulative_statement, "manipulative_statement" },
1395cdf0e10cSrcweir             { OSQLParseNode::subquery, "subquery" },
1396cdf0e10cSrcweir             { OSQLParseNode::value_exp_commalist, "value_exp_commalist" },
1397cdf0e10cSrcweir             { OSQLParseNode::odbc_fct_spec, "odbc_fct_spec" },
1398cdf0e10cSrcweir             { OSQLParseNode::union_statement, "union_statement" },
1399cdf0e10cSrcweir             { OSQLParseNode::outer_join_type, "outer_join_type" },
1400cdf0e10cSrcweir             { OSQLParseNode::char_value_exp, "char_value_exp" },
1401cdf0e10cSrcweir             { OSQLParseNode::term, "term" },
1402cdf0e10cSrcweir             { OSQLParseNode::value_exp_primary, "value_exp_primary" },
1403cdf0e10cSrcweir             { OSQLParseNode::value_exp, "value_exp" },
1404cdf0e10cSrcweir             { OSQLParseNode::selection, "selection" },
1405cdf0e10cSrcweir             { OSQLParseNode::fold, "fold" },
1406cdf0e10cSrcweir             { OSQLParseNode::char_substring_fct, "char_substring_fct" },
1407cdf0e10cSrcweir             { OSQLParseNode::factor, "factor" },
1408cdf0e10cSrcweir             { OSQLParseNode::base_table_def, "base_table_def" },
1409cdf0e10cSrcweir             { OSQLParseNode::base_table_element_commalist, "base_table_element_commalist" },
1410cdf0e10cSrcweir             { OSQLParseNode::data_type, "data_type" },
1411cdf0e10cSrcweir             { OSQLParseNode::column_def, "column_def" },
1412cdf0e10cSrcweir             { OSQLParseNode::table_node, "table_node" },
1413cdf0e10cSrcweir             { OSQLParseNode::as, "as" },
1414cdf0e10cSrcweir             { OSQLParseNode::op_column_commalist, "op_column_commalist" },
1415cdf0e10cSrcweir             { OSQLParseNode::table_primary_as_range_column, "table_primary_as_range_column" },
1416cdf0e10cSrcweir             { OSQLParseNode::datetime_primary, "datetime_primary" },
1417cdf0e10cSrcweir             { OSQLParseNode::concatenation, "concatenation" },
1418cdf0e10cSrcweir             { OSQLParseNode::char_factor, "char_factor" },
1419cdf0e10cSrcweir             { OSQLParseNode::bit_value_fct, "bit_value_fct" },
1420cdf0e10cSrcweir             { OSQLParseNode::comparison_predicate_part_2, "comparison_predicate_part_2" },
1421cdf0e10cSrcweir             { OSQLParseNode::parenthesized_boolean_value_expression, "parenthesized_boolean_value_expression" },
1422cdf0e10cSrcweir             { OSQLParseNode::character_string_type, "character_string_type" },
1423cdf0e10cSrcweir             { OSQLParseNode::other_like_predicate_part_2, "other_like_predicate_part_2" },
1424cdf0e10cSrcweir             { OSQLParseNode::between_predicate_part_2, "between_predicate_part_2" },
1425cdf0e10cSrcweir             { OSQLParseNode::cast_spec, "cast_spec" }
1426cdf0e10cSrcweir         };
1427cdf0e10cSrcweir         size_t nRuleMapCount = sizeof( aRuleDescriptions ) / sizeof( aRuleDescriptions[0] );
1428cdf0e10cSrcweir         OSL_ENSURE( nRuleMapCount == size_t( OSQLParseNode::rule_count ), "OSQLParser::OSQLParser: added a new rule? Adjust this map!" );
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir         for ( size_t mapEntry = 0; mapEntry < nRuleMapCount; ++mapEntry )
1431cdf0e10cSrcweir         {
1432cdf0e10cSrcweir             // look up the rule description in the our identifier map
1433cdf0e10cSrcweir             sal_uInt32 nParserRuleID = StrToRuleID( aRuleDescriptions[ mapEntry ].sRuleName );
1434cdf0e10cSrcweir             // map the parser's rule ID to the OSQLParseNode::Rule
1435cdf0e10cSrcweir             s_aReverseRuleIDLookup[ nParserRuleID ] = aRuleDescriptions[ mapEntry ].eRule;
1436cdf0e10cSrcweir             // and map the OSQLParseNode::Rule to the parser's rule ID
1437cdf0e10cSrcweir             s_nRuleIDs[ aRuleDescriptions[ mapEntry ].eRule ] = nParserRuleID;
1438cdf0e10cSrcweir         }
1439cdf0e10cSrcweir 	}
1440cdf0e10cSrcweir 	++s_nRefCount;
1441cdf0e10cSrcweir 
1442cdf0e10cSrcweir 	if (m_pContext == NULL)
1443cdf0e10cSrcweir 		// take the default context
1444cdf0e10cSrcweir 		m_pContext = &s_aDefaultContext;
1445cdf0e10cSrcweir 
1446cdf0e10cSrcweir     m_pData->aLocale = m_pContext->getPreferredLocale();
1447cdf0e10cSrcweir }
1448cdf0e10cSrcweir 
1449cdf0e10cSrcweir //-----------------------------------------------------------------------------
1450cdf0e10cSrcweir OSQLParser::~OSQLParser()
1451cdf0e10cSrcweir {
1452cdf0e10cSrcweir 	{
1453cdf0e10cSrcweir 		::osl::MutexGuard aGuard(getMutex());
1454cdf0e10cSrcweir 		OSL_ENSURE(s_nRefCount > 0, "OSQLParser::~OSQLParser() : suspicious call : have a refcount of 0 !");
1455cdf0e10cSrcweir 		if (!--s_nRefCount)
1456cdf0e10cSrcweir 		{
1457cdf0e10cSrcweir 			s_pScanner->setScanner(sal_True);
1458cdf0e10cSrcweir 			delete s_pScanner;
1459cdf0e10cSrcweir 			s_pScanner = NULL;
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir 			delete s_pGarbageCollector;
1462cdf0e10cSrcweir 			s_pGarbageCollector = NULL;
1463cdf0e10cSrcweir 			// is only set the first time so we should delete it only when there no more instances
1464cdf0e10cSrcweir 			s_xLocaleData = NULL;
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir             RuleIDMap aEmpty;
1467cdf0e10cSrcweir             s_aReverseRuleIDLookup.swap( aEmpty );
1468cdf0e10cSrcweir 		}
1469cdf0e10cSrcweir 		m_pParseTree = NULL;
1470cdf0e10cSrcweir 	}
1471cdf0e10cSrcweir }
1472cdf0e10cSrcweir // -----------------------------------------------------------------------------
1473cdf0e10cSrcweir void OSQLParseNode::substituteParameterNames(OSQLParseNode* _pNode)
1474cdf0e10cSrcweir {
1475cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::substituteParameterNames" );
1476cdf0e10cSrcweir 	sal_Int32 nCount = _pNode->count();
1477cdf0e10cSrcweir 	for(sal_Int32 i=0;i < nCount;++i)
1478cdf0e10cSrcweir 	{
1479cdf0e10cSrcweir 		OSQLParseNode* pChildNode = _pNode->getChild(i);
1480cdf0e10cSrcweir 		if(SQL_ISRULE(pChildNode,parameter) && pChildNode->count() > 1)
1481cdf0e10cSrcweir 		{
1482cdf0e10cSrcweir 			OSQLParseNode* pNewNode = new OSQLParseNode(::rtl::OUString::createFromAscii("?") ,SQL_NODE_PUNCTUATION,0);
1483cdf0e10cSrcweir 			delete pChildNode->replace(pChildNode->getChild(0),pNewNode);
1484cdf0e10cSrcweir 			sal_Int32 nChildCount = pChildNode->count();
1485cdf0e10cSrcweir 			for(sal_Int32 j=1;j < nChildCount;++j)
1486cdf0e10cSrcweir 				delete pChildNode->removeAt(1);
1487cdf0e10cSrcweir 		}
1488cdf0e10cSrcweir 		else
1489cdf0e10cSrcweir 			substituteParameterNames(pChildNode);
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir 	}
1492cdf0e10cSrcweir }
1493cdf0e10cSrcweir // -----------------------------------------------------------------------------
1494cdf0e10cSrcweir bool OSQLParser::extractDate(OSQLParseNode* pLiteral,double& _rfValue)
1495cdf0e10cSrcweir {
1496cdf0e10cSrcweir     Reference< XNumberFormatsSupplier > xFormatSup = m_xFormatter->getNumberFormatsSupplier();
1497cdf0e10cSrcweir 	Reference< XNumberFormatTypes > xFormatTypes;
1498cdf0e10cSrcweir     if ( xFormatSup.is() )
1499cdf0e10cSrcweir         xFormatTypes = xFormatTypes.query( xFormatSup->getNumberFormats() );
1500cdf0e10cSrcweir 
1501cdf0e10cSrcweir     // if there is no format key, yet, make sure we have a feasible one for our locale
1502cdf0e10cSrcweir 	try
1503cdf0e10cSrcweir 	{
1504cdf0e10cSrcweir 		if ( !m_nFormatKey && xFormatTypes.is() )
1505cdf0e10cSrcweir 			m_nFormatKey = ::dbtools::getDefaultNumberFormat( m_xField, xFormatTypes, m_pData->aLocale );
1506cdf0e10cSrcweir     }
1507cdf0e10cSrcweir     catch( Exception& ) { }
1508cdf0e10cSrcweir     ::rtl::OUString sValue = pLiteral->getTokenValue();
1509cdf0e10cSrcweir     sal_Int32 nTryFormat = m_nFormatKey;
1510cdf0e10cSrcweir     bool bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1511cdf0e10cSrcweir 
1512cdf0e10cSrcweir     // If our format key didn't do, try the default date format for our locale.
1513cdf0e10cSrcweir     if ( !bSuccess && xFormatTypes.is() )
1514cdf0e10cSrcweir     {
1515cdf0e10cSrcweir 		try
1516cdf0e10cSrcweir 		{
1517cdf0e10cSrcweir             nTryFormat = xFormatTypes->getStandardFormat( NumberFormat::DATE, m_pData->aLocale );
1518cdf0e10cSrcweir 		}
1519cdf0e10cSrcweir         catch( Exception& ) { }
1520cdf0e10cSrcweir         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1521cdf0e10cSrcweir     }
1522cdf0e10cSrcweir 
1523cdf0e10cSrcweir     // if this also didn't do, try ISO format
1524cdf0e10cSrcweir     if ( !bSuccess && xFormatTypes.is() )
1525cdf0e10cSrcweir     {
1526cdf0e10cSrcweir 		try
1527cdf0e10cSrcweir 		{
1528cdf0e10cSrcweir             nTryFormat = xFormatTypes->getFormatIndex( NumberFormatIndex::DATE_DIN_YYYYMMDD, m_pData->aLocale );
1529cdf0e10cSrcweir 		}
1530cdf0e10cSrcweir         catch( Exception& ) { }
1531cdf0e10cSrcweir         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1532cdf0e10cSrcweir     }
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir     // if this also didn't do, try fallback date format (en-US)
1535cdf0e10cSrcweir     if ( !bSuccess )
1536cdf0e10cSrcweir     {
1537cdf0e10cSrcweir         nTryFormat = m_nDateFormatKey;
1538cdf0e10cSrcweir         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1539cdf0e10cSrcweir     }
1540cdf0e10cSrcweir     return bSuccess;
1541cdf0e10cSrcweir }
1542cdf0e10cSrcweir // -----------------------------------------------------------------------------
1543cdf0e10cSrcweir OSQLParseNode* OSQLParser::buildDate(sal_Int32 _nType,OSQLParseNode*& pLiteral)
1544cdf0e10cSrcweir {
1545cdf0e10cSrcweir     // try converting the string into a date, according to our format key
1546cdf0e10cSrcweir 	double fValue = 0.0;
1547cdf0e10cSrcweir     OSQLParseNode* pFCTNode = NULL;
1548cdf0e10cSrcweir 
1549cdf0e10cSrcweir     if ( extractDate(pLiteral,fValue) )
1550cdf0e10cSrcweir 		pFCTNode = buildNode_Date( fValue, _nType);
1551cdf0e10cSrcweir 
1552cdf0e10cSrcweir     delete pLiteral;
1553cdf0e10cSrcweir     pLiteral = NULL;
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir     if ( !pFCTNode )
1556cdf0e10cSrcweir 		m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE);
1557cdf0e10cSrcweir 
1558cdf0e10cSrcweir     return pFCTNode;
1559cdf0e10cSrcweir }
1560cdf0e10cSrcweir // -----------------------------------------------------------------------------
1561cdf0e10cSrcweir //-----------------------------------------------------------------------------
1562cdf0e10cSrcweir OSQLParseNode::OSQLParseNode(const sal_Char * pNewValue,
1563cdf0e10cSrcweir                              SQLNodeType eNewNodeType,
1564cdf0e10cSrcweir                              sal_uInt32 nNewNodeID)
1565cdf0e10cSrcweir         :m_pParent(NULL)
1566cdf0e10cSrcweir         ,m_aNodeValue(pNewValue,strlen(pNewValue),RTL_TEXTENCODING_UTF8)
1567cdf0e10cSrcweir         ,m_eNodeType(eNewNodeType)
1568cdf0e10cSrcweir         ,m_nNodeID(nNewNodeID)
1569cdf0e10cSrcweir {
1570cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1571cdf0e10cSrcweir 
1572cdf0e10cSrcweir     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1573cdf0e10cSrcweir }
1574cdf0e10cSrcweir //-----------------------------------------------------------------------------
1575cdf0e10cSrcweir OSQLParseNode::OSQLParseNode(const ::rtl::OString &_rNewValue,
1576cdf0e10cSrcweir                              SQLNodeType eNewNodeType,
1577cdf0e10cSrcweir                              sal_uInt32 nNewNodeID)
1578cdf0e10cSrcweir         :m_pParent(NULL)
1579cdf0e10cSrcweir         ,m_aNodeValue(_rNewValue,_rNewValue.getLength(),RTL_TEXTENCODING_UTF8)
1580cdf0e10cSrcweir         ,m_eNodeType(eNewNodeType)
1581cdf0e10cSrcweir         ,m_nNodeID(nNewNodeID)
1582cdf0e10cSrcweir {
1583cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1584cdf0e10cSrcweir 
1585cdf0e10cSrcweir     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1586cdf0e10cSrcweir }
1587cdf0e10cSrcweir //-----------------------------------------------------------------------------
1588cdf0e10cSrcweir OSQLParseNode::OSQLParseNode(const sal_Unicode * pNewValue,
1589cdf0e10cSrcweir                                  SQLNodeType eNewNodeType,
1590cdf0e10cSrcweir                                  sal_uInt32 nNewNodeID)
1591cdf0e10cSrcweir         :m_pParent(NULL)
1592cdf0e10cSrcweir         ,m_aNodeValue(pNewValue)
1593cdf0e10cSrcweir         ,m_eNodeType(eNewNodeType)
1594cdf0e10cSrcweir         ,m_nNodeID(nNewNodeID)
1595cdf0e10cSrcweir {
1596cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1597cdf0e10cSrcweir 
1598cdf0e10cSrcweir     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1599cdf0e10cSrcweir }
1600cdf0e10cSrcweir //-----------------------------------------------------------------------------
1601cdf0e10cSrcweir OSQLParseNode::OSQLParseNode(const ::rtl::OUString &_rNewValue,
1602cdf0e10cSrcweir                                  SQLNodeType eNewNodeType,
1603cdf0e10cSrcweir                                  sal_uInt32 nNewNodeID)
1604cdf0e10cSrcweir         :m_pParent(NULL)
1605cdf0e10cSrcweir         ,m_aNodeValue(_rNewValue)
1606cdf0e10cSrcweir         ,m_eNodeType(eNewNodeType)
1607cdf0e10cSrcweir         ,m_nNodeID(nNewNodeID)
1608cdf0e10cSrcweir {
1609cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1610cdf0e10cSrcweir 
1611cdf0e10cSrcweir     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: mit unzulaessigem NodeType konstruiert");
1612cdf0e10cSrcweir }
1613cdf0e10cSrcweir //-----------------------------------------------------------------------------
1614cdf0e10cSrcweir OSQLParseNode::OSQLParseNode(const OSQLParseNode& rParseNode)
1615cdf0e10cSrcweir {
1616cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::OSQLParseNode" );
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir     // klemm den getParent auf NULL
1619cdf0e10cSrcweir     m_pParent = NULL;
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir     // kopiere die member
1622cdf0e10cSrcweir     m_aNodeValue = rParseNode.m_aNodeValue;
1623cdf0e10cSrcweir     m_eNodeType  = rParseNode.m_eNodeType;
1624cdf0e10cSrcweir     m_nNodeID    = rParseNode.m_nNodeID;
1625cdf0e10cSrcweir 
1626cdf0e10cSrcweir 
1627cdf0e10cSrcweir     // denk dran, dass von Container abgeleitet wurde, laut SV-Help erzeugt
1628cdf0e10cSrcweir     // copy-Constructor des Containers einen neuen Container mit den gleichen
1629cdf0e10cSrcweir     // Zeigern als Inhalt -> d.h. nach dem Kopieren des Container wird fuer
1630cdf0e10cSrcweir     // alle Zeiger ungleich NULL eine Kopie hergestellt und anstelle des alten
1631cdf0e10cSrcweir     // Zeigers wieder eingehangen.
1632cdf0e10cSrcweir 
1633cdf0e10cSrcweir     // wenn kein Blatt, dann SubTrees bearbeiten
1634cdf0e10cSrcweir     for (OSQLParseNodes::const_iterator i = rParseNode.m_aChildren.begin();
1635cdf0e10cSrcweir          i != rParseNode.m_aChildren.end(); i++)
1636cdf0e10cSrcweir         append(new OSQLParseNode(**i));
1637cdf0e10cSrcweir }
1638cdf0e10cSrcweir // -----------------------------------------------------------------------------
1639cdf0e10cSrcweir //-----------------------------------------------------------------------------
1640cdf0e10cSrcweir OSQLParseNode& OSQLParseNode::operator=(const OSQLParseNode& rParseNode)
1641cdf0e10cSrcweir {
1642cdf0e10cSrcweir     if (this != &rParseNode)
1643cdf0e10cSrcweir     {
1644cdf0e10cSrcweir         // kopiere die member - pParent bleibt der alte
1645cdf0e10cSrcweir         m_aNodeValue = rParseNode.m_aNodeValue;
1646cdf0e10cSrcweir         m_eNodeType  = rParseNode.m_eNodeType;
1647cdf0e10cSrcweir         m_nNodeID    = rParseNode.m_nNodeID;
1648cdf0e10cSrcweir 
1649cdf0e10cSrcweir         for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
1650cdf0e10cSrcweir             i != m_aChildren.end(); i++)
1651cdf0e10cSrcweir             delete *i;
1652cdf0e10cSrcweir 
1653cdf0e10cSrcweir         m_aChildren.clear();
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir         for (OSQLParseNodes::const_iterator j = rParseNode.m_aChildren.begin();
1656cdf0e10cSrcweir              j != rParseNode.m_aChildren.end(); j++)
1657cdf0e10cSrcweir             append(new OSQLParseNode(**j));
1658cdf0e10cSrcweir     }
1659cdf0e10cSrcweir     return *this;
1660cdf0e10cSrcweir }
1661cdf0e10cSrcweir 
1662cdf0e10cSrcweir //-----------------------------------------------------------------------------
1663cdf0e10cSrcweir sal_Bool OSQLParseNode::operator==(OSQLParseNode& rParseNode) const
1664cdf0e10cSrcweir {
1665cdf0e10cSrcweir     // die member muessen gleich sein
1666cdf0e10cSrcweir     sal_Bool bResult = (m_nNodeID  == rParseNode.m_nNodeID) &&
1667cdf0e10cSrcweir                    (m_eNodeType == rParseNode.m_eNodeType) &&
1668cdf0e10cSrcweir                    (m_aNodeValue == rParseNode.m_aNodeValue) &&
1669cdf0e10cSrcweir                     count() == rParseNode.count();
1670cdf0e10cSrcweir 
1671cdf0e10cSrcweir     // Parameters are not equal!
1672cdf0e10cSrcweir     bResult = bResult && !SQL_ISRULE(this, parameter);
1673cdf0e10cSrcweir 
1674cdf0e10cSrcweir     // compare childs
1675cdf0e10cSrcweir     for (sal_uInt32 i=0; bResult && i < count(); i++)
1676cdf0e10cSrcweir         bResult = *getChild(i) == *rParseNode.getChild(i);
1677cdf0e10cSrcweir 
1678cdf0e10cSrcweir     return bResult;
1679cdf0e10cSrcweir }
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir //-----------------------------------------------------------------------------
1682cdf0e10cSrcweir OSQLParseNode::~OSQLParseNode()
1683cdf0e10cSrcweir {
1684cdf0e10cSrcweir     for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
1685cdf0e10cSrcweir          i != m_aChildren.end(); i++)
1686cdf0e10cSrcweir         delete *i;
1687cdf0e10cSrcweir     m_aChildren.clear();
1688cdf0e10cSrcweir }
1689cdf0e10cSrcweir 
1690cdf0e10cSrcweir //-----------------------------------------------------------------------------
1691cdf0e10cSrcweir void OSQLParseNode::append(OSQLParseNode* pNewNode)
1692cdf0e10cSrcweir {
1693cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::append" );
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir     OSL_ENSURE(pNewNode != NULL, "OSQLParseNode: ungueltiger NewSubTree");
1696cdf0e10cSrcweir     OSL_ENSURE(pNewNode->getParent() == NULL, "OSQLParseNode: Knoten ist kein Waise");
1697cdf0e10cSrcweir     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pNewNode) == m_aChildren.end(),
1698cdf0e10cSrcweir             "OSQLParseNode::append() Node already element of parent");
1699cdf0e10cSrcweir 
1700cdf0e10cSrcweir     // stelle Verbindung zum getParent her:
1701cdf0e10cSrcweir     pNewNode->setParent( this );
1702cdf0e10cSrcweir     // und haenge den SubTree hinten an
1703cdf0e10cSrcweir     m_aChildren.push_back(pNewNode);
1704cdf0e10cSrcweir }
1705cdf0e10cSrcweir // -----------------------------------------------------------------------------
1706cdf0e10cSrcweir sal_Bool OSQLParseNode::addDateValue(::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
1707cdf0e10cSrcweir {
1708cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::addDateValue" );
1709cdf0e10cSrcweir     // special display for date/time values
1710cdf0e10cSrcweir     if (SQL_ISRULE(this,set_fct_spec) && SQL_ISPUNCTUATION(m_aChildren[0],"{"))
1711cdf0e10cSrcweir     {
1712cdf0e10cSrcweir         const OSQLParseNode* pODBCNode = m_aChildren[1];
1713cdf0e10cSrcweir         const OSQLParseNode* pODBCNodeChild = pODBCNode->m_aChildren[0];
1714cdf0e10cSrcweir 
1715cdf0e10cSrcweir         if (pODBCNodeChild->getNodeType() == SQL_NODE_KEYWORD && (
1716cdf0e10cSrcweir             SQL_ISTOKEN(pODBCNodeChild, D) ||
1717cdf0e10cSrcweir             SQL_ISTOKEN(pODBCNodeChild, T) ||
1718cdf0e10cSrcweir             SQL_ISTOKEN(pODBCNodeChild, TS) ))
1719cdf0e10cSrcweir         {
1720cdf0e10cSrcweir 			::rtl::OUString suQuote(::rtl::OUString::createFromAscii("'"));
1721cdf0e10cSrcweir 			if (rParam.bPredicate)
1722cdf0e10cSrcweir 			{
1723cdf0e10cSrcweir 				 if (rParam.aMetaData.shouldEscapeDateTime())
1724cdf0e10cSrcweir 				 {
1725cdf0e10cSrcweir 					 suQuote = ::rtl::OUString::createFromAscii("#");
1726cdf0e10cSrcweir 				 }
1727cdf0e10cSrcweir 			}
1728cdf0e10cSrcweir 			else
1729cdf0e10cSrcweir 			{
1730cdf0e10cSrcweir 				 if (rParam.aMetaData.shouldEscapeDateTime())
1731cdf0e10cSrcweir 				 {
1732cdf0e10cSrcweir 					 // suQuote = ::rtl::OUString::createFromAscii("'");
1733cdf0e10cSrcweir 					 return sal_False;
1734cdf0e10cSrcweir 				 }
1735cdf0e10cSrcweir 			}
1736cdf0e10cSrcweir 
1737cdf0e10cSrcweir 			if (rString.getLength())
1738cdf0e10cSrcweir                 rString.appendAscii(" ");
1739cdf0e10cSrcweir 			rString.append(suQuote);
1740cdf0e10cSrcweir 			const ::rtl::OUString sTokenValue = pODBCNode->m_aChildren[1]->getTokenValue();
1741cdf0e10cSrcweir             if (SQL_ISTOKEN(pODBCNodeChild, D))
1742cdf0e10cSrcweir 			{
1743cdf0e10cSrcweir 				rString.append(rParam.bPredicate ? convertDateString(rParam, sTokenValue) : sTokenValue);
1744cdf0e10cSrcweir 			}
1745cdf0e10cSrcweir             else if (SQL_ISTOKEN(pODBCNodeChild, T))
1746cdf0e10cSrcweir 			{
1747cdf0e10cSrcweir 				rString.append(rParam.bPredicate ? convertTimeString(rParam, sTokenValue) : sTokenValue);
1748cdf0e10cSrcweir 			}
1749cdf0e10cSrcweir             else
1750cdf0e10cSrcweir 			{
1751cdf0e10cSrcweir 				rString.append(rParam.bPredicate ? convertDateTimeString(rParam, sTokenValue) : sTokenValue);
1752cdf0e10cSrcweir 			}
1753cdf0e10cSrcweir 			rString.append(suQuote);
1754cdf0e10cSrcweir             return sal_True;
1755cdf0e10cSrcweir         }
1756cdf0e10cSrcweir     }
1757cdf0e10cSrcweir     return sal_False;
1758cdf0e10cSrcweir }
1759cdf0e10cSrcweir // -----------------------------------------------------------------------------
1760cdf0e10cSrcweir void OSQLParseNode::replaceNodeValue(const ::rtl::OUString& rTableAlias,const ::rtl::OUString& rColumnName)
1761cdf0e10cSrcweir {
1762cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replaceNodeValue" );
1763cdf0e10cSrcweir     for (sal_uInt32 i=0;i<count();++i)
1764cdf0e10cSrcweir     {
1765cdf0e10cSrcweir         if (SQL_ISRULE(this,column_ref) && count() == 1 && getChild(0)->getTokenValue() == rColumnName)
1766cdf0e10cSrcweir         {
1767cdf0e10cSrcweir             OSQLParseNode * pCol = removeAt((sal_uInt32)0);
1768cdf0e10cSrcweir             append(new OSQLParseNode(rTableAlias,SQL_NODE_NAME));
1769cdf0e10cSrcweir             append(new OSQLParseNode(::rtl::OUString::createFromAscii("."),SQL_NODE_PUNCTUATION));
1770cdf0e10cSrcweir             append(pCol);
1771cdf0e10cSrcweir         }
1772cdf0e10cSrcweir         else
1773cdf0e10cSrcweir             getChild(i)->replaceNodeValue(rTableAlias,rColumnName);
1774cdf0e10cSrcweir     }
1775cdf0e10cSrcweir }
1776cdf0e10cSrcweir //-----------------------------------------------------------------------------
1777cdf0e10cSrcweir OSQLParseNode* OSQLParseNode::getByRule(OSQLParseNode::Rule eRule) const
1778cdf0e10cSrcweir {
1779cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getByRule" );
1780cdf0e10cSrcweir     OSQLParseNode* pRetNode = 0;
1781cdf0e10cSrcweir     if (isRule() && OSQLParser::RuleID(eRule) == getRuleID())
1782cdf0e10cSrcweir         pRetNode = (OSQLParseNode*)this;
1783cdf0e10cSrcweir     else
1784cdf0e10cSrcweir     {
1785cdf0e10cSrcweir         for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
1786cdf0e10cSrcweir             !pRetNode && i != m_aChildren.end(); i++)
1787cdf0e10cSrcweir             pRetNode = (*i)->getByRule(eRule);
1788cdf0e10cSrcweir     }
1789cdf0e10cSrcweir     return pRetNode;
1790cdf0e10cSrcweir }
1791cdf0e10cSrcweir //-----------------------------------------------------------------------------
1792cdf0e10cSrcweir OSQLParseNode* MakeANDNode(OSQLParseNode *pLeftLeaf,OSQLParseNode *pRightLeaf)
1793cdf0e10cSrcweir {
1794cdf0e10cSrcweir     OSQLParseNode* pNewNode = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_term));
1795cdf0e10cSrcweir     pNewNode->append(pLeftLeaf);
1796cdf0e10cSrcweir     pNewNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii("AND"),SQL_NODE_KEYWORD,SQL_TOKEN_AND));
1797cdf0e10cSrcweir     pNewNode->append(pRightLeaf);
1798cdf0e10cSrcweir     return pNewNode;
1799cdf0e10cSrcweir }
1800cdf0e10cSrcweir //-----------------------------------------------------------------------------
1801cdf0e10cSrcweir OSQLParseNode* MakeORNode(OSQLParseNode *pLeftLeaf,OSQLParseNode *pRightLeaf)
1802cdf0e10cSrcweir {
1803cdf0e10cSrcweir     OSQLParseNode* pNewNode = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::search_condition));
1804cdf0e10cSrcweir     pNewNode->append(pLeftLeaf);
1805cdf0e10cSrcweir     pNewNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii("OR"),SQL_NODE_KEYWORD,SQL_TOKEN_OR));
1806cdf0e10cSrcweir     pNewNode->append(pRightLeaf);
1807cdf0e10cSrcweir     return pNewNode;
1808cdf0e10cSrcweir }
1809cdf0e10cSrcweir //-----------------------------------------------------------------------------
1810cdf0e10cSrcweir void OSQLParseNode::disjunctiveNormalForm(OSQLParseNode*& pSearchCondition)
1811cdf0e10cSrcweir {
1812cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::disjunctiveNormalForm" );
1813cdf0e10cSrcweir     if(!pSearchCondition) // no where condition at entry point
1814cdf0e10cSrcweir         return;
1815cdf0e10cSrcweir 
1816cdf0e10cSrcweir     OSQLParseNode::absorptions(pSearchCondition);
1817cdf0e10cSrcweir     // '(' search_condition ')'
1818cdf0e10cSrcweir     if (SQL_ISRULE(pSearchCondition,boolean_primary))
1819cdf0e10cSrcweir     {
1820cdf0e10cSrcweir         OSQLParseNode* pLeft    = pSearchCondition->getChild(1);
1821cdf0e10cSrcweir         disjunctiveNormalForm(pLeft);
1822cdf0e10cSrcweir     }
1823cdf0e10cSrcweir     // search_condition SQL_TOKEN_OR boolean_term
1824cdf0e10cSrcweir     else if (SQL_ISRULE(pSearchCondition,search_condition))
1825cdf0e10cSrcweir     {
1826cdf0e10cSrcweir         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
1827cdf0e10cSrcweir         disjunctiveNormalForm(pLeft);
1828cdf0e10cSrcweir 
1829cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
1830cdf0e10cSrcweir         disjunctiveNormalForm(pRight);
1831cdf0e10cSrcweir     }
1832cdf0e10cSrcweir     // boolean_term SQL_TOKEN_AND boolean_factor
1833cdf0e10cSrcweir     else if (SQL_ISRULE(pSearchCondition,boolean_term))
1834cdf0e10cSrcweir     {
1835cdf0e10cSrcweir         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
1836cdf0e10cSrcweir         disjunctiveNormalForm(pLeft);
1837cdf0e10cSrcweir 
1838cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
1839cdf0e10cSrcweir         disjunctiveNormalForm(pRight);
1840cdf0e10cSrcweir 
1841cdf0e10cSrcweir         OSQLParseNode* pNewNode = NULL;
1842cdf0e10cSrcweir         // '(' search_condition ')' on left side
1843cdf0e10cSrcweir         if(pLeft->count() == 3 && SQL_ISRULE(pLeft,boolean_primary) && SQL_ISRULE(pLeft->getChild(1),search_condition))
1844cdf0e10cSrcweir         {
1845cdf0e10cSrcweir             // and-or tree  on left side
1846cdf0e10cSrcweir             OSQLParseNode* pOr = pLeft->getChild(1);
1847cdf0e10cSrcweir             OSQLParseNode* pNewLeft = NULL;
1848cdf0e10cSrcweir             OSQLParseNode* pNewRight = NULL;
1849cdf0e10cSrcweir 
1850cdf0e10cSrcweir             // cut right from parent
1851cdf0e10cSrcweir             pSearchCondition->removeAt(2);
1852cdf0e10cSrcweir 
1853cdf0e10cSrcweir             pNewRight   = MakeANDNode(pOr->removeAt(2)      ,pRight);
1854cdf0e10cSrcweir             pNewLeft    = MakeANDNode(pOr->removeAt((sal_uInt32)0)  ,new OSQLParseNode(*pRight));
1855cdf0e10cSrcweir             pNewNode    = MakeORNode(pNewLeft,pNewRight);
1856cdf0e10cSrcweir             // and append new Node
1857cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
1858cdf0e10cSrcweir 
1859cdf0e10cSrcweir             disjunctiveNormalForm(pSearchCondition);
1860cdf0e10cSrcweir         }
1861cdf0e10cSrcweir         else if(pRight->count() == 3 && SQL_ISRULE(pRight,boolean_primary) && SQL_ISRULE(pRight->getChild(1),search_condition))
1862cdf0e10cSrcweir         {   // '(' search_condition ')' on right side
1863cdf0e10cSrcweir             // and-or tree  on right side
1864cdf0e10cSrcweir             // a and (b or c)
1865cdf0e10cSrcweir             OSQLParseNode* pOr = pRight->getChild(1);
1866cdf0e10cSrcweir             OSQLParseNode* pNewLeft = NULL;
1867cdf0e10cSrcweir             OSQLParseNode* pNewRight = NULL;
1868cdf0e10cSrcweir 
1869cdf0e10cSrcweir             // cut left from parent
1870cdf0e10cSrcweir             pSearchCondition->removeAt((sal_uInt32)0);
1871cdf0e10cSrcweir 
1872cdf0e10cSrcweir             pNewRight   = MakeANDNode(pLeft,pOr->removeAt(2));
1873cdf0e10cSrcweir             pNewLeft    = MakeANDNode(new OSQLParseNode(*pLeft),pOr->removeAt((sal_uInt32)0));
1874cdf0e10cSrcweir             pNewNode    = MakeORNode(pNewLeft,pNewRight);
1875cdf0e10cSrcweir 
1876cdf0e10cSrcweir             // and append new Node
1877cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
1878cdf0e10cSrcweir             disjunctiveNormalForm(pSearchCondition);
1879cdf0e10cSrcweir         }
1880cdf0e10cSrcweir         else if(SQL_ISRULE(pLeft,boolean_primary) && (!SQL_ISRULE(pLeft->getChild(1),search_condition) || !SQL_ISRULE(pLeft->getChild(1),boolean_term)))
1881cdf0e10cSrcweir             pSearchCondition->replace(pLeft, pLeft->removeAt(1));
1882cdf0e10cSrcweir         else if(SQL_ISRULE(pRight,boolean_primary) && (!SQL_ISRULE(pRight->getChild(1),search_condition) || !SQL_ISRULE(pRight->getChild(1),boolean_term)))
1883cdf0e10cSrcweir             pSearchCondition->replace(pRight, pRight->removeAt(1));
1884cdf0e10cSrcweir     }
1885cdf0e10cSrcweir }
1886cdf0e10cSrcweir //-----------------------------------------------------------------------------
1887cdf0e10cSrcweir void OSQLParseNode::negateSearchCondition(OSQLParseNode*& pSearchCondition,sal_Bool bNegate)
1888cdf0e10cSrcweir {
1889cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::negateSearchCondition" );
1890cdf0e10cSrcweir     if(!pSearchCondition) // no where condition at entry point
1891cdf0e10cSrcweir         return;
1892cdf0e10cSrcweir     // '(' search_condition ')'
1893cdf0e10cSrcweir     if (pSearchCondition->count() == 3 && SQL_ISRULE(pSearchCondition,boolean_primary))
1894cdf0e10cSrcweir     {
1895cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(1);
1896cdf0e10cSrcweir         negateSearchCondition(pRight,bNegate);
1897cdf0e10cSrcweir     }
1898cdf0e10cSrcweir     // search_condition SQL_TOKEN_OR boolean_term
1899cdf0e10cSrcweir     else if (SQL_ISRULE(pSearchCondition,search_condition))
1900cdf0e10cSrcweir     {
1901cdf0e10cSrcweir         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
1902cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
1903cdf0e10cSrcweir         if(bNegate)
1904cdf0e10cSrcweir         {
1905cdf0e10cSrcweir             OSQLParseNode* pNewNode = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_term));
1906cdf0e10cSrcweir             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)0));
1907cdf0e10cSrcweir             pNewNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii("AND"),SQL_NODE_KEYWORD,SQL_TOKEN_AND));
1908cdf0e10cSrcweir             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)1));
1909cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
1910cdf0e10cSrcweir 
1911cdf0e10cSrcweir             pLeft   = pNewNode->getChild(0);
1912cdf0e10cSrcweir             pRight  = pNewNode->getChild(2);
1913cdf0e10cSrcweir         }
1914cdf0e10cSrcweir 
1915cdf0e10cSrcweir         negateSearchCondition(pLeft,bNegate);
1916cdf0e10cSrcweir         negateSearchCondition(pRight,bNegate);
1917cdf0e10cSrcweir     }
1918cdf0e10cSrcweir     // boolean_term SQL_TOKEN_AND boolean_factor
1919cdf0e10cSrcweir     else if (SQL_ISRULE(pSearchCondition,boolean_term))
1920cdf0e10cSrcweir     {
1921cdf0e10cSrcweir         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
1922cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
1923cdf0e10cSrcweir         if(bNegate)
1924cdf0e10cSrcweir         {
1925cdf0e10cSrcweir             OSQLParseNode* pNewNode = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::search_condition));
1926cdf0e10cSrcweir             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)0));
1927cdf0e10cSrcweir             pNewNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii("OR"),SQL_NODE_KEYWORD,SQL_TOKEN_OR));
1928cdf0e10cSrcweir             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)1));
1929cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
1930cdf0e10cSrcweir 
1931cdf0e10cSrcweir             pLeft   = pNewNode->getChild(0);
1932cdf0e10cSrcweir             pRight  = pNewNode->getChild(2);
1933cdf0e10cSrcweir         }
1934cdf0e10cSrcweir 
1935cdf0e10cSrcweir         negateSearchCondition(pLeft,bNegate);
1936cdf0e10cSrcweir         negateSearchCondition(pRight,bNegate);
1937cdf0e10cSrcweir     }
1938cdf0e10cSrcweir     // SQL_TOKEN_NOT ( boolean_test )
1939cdf0e10cSrcweir     else if (SQL_ISRULE(pSearchCondition,boolean_factor))
1940cdf0e10cSrcweir     {
1941cdf0e10cSrcweir         OSQLParseNode *pNot = pSearchCondition->removeAt((sal_uInt32)0);
1942cdf0e10cSrcweir         delete pNot;
1943cdf0e10cSrcweir         OSQLParseNode *pBooleanTest = pSearchCondition->removeAt((sal_uInt32)0);
1944cdf0e10cSrcweir         // TODO is this needed // pBooleanTest->setParent(NULL);
1945cdf0e10cSrcweir         replaceAndReset(pSearchCondition,pBooleanTest);
1946cdf0e10cSrcweir 
1947cdf0e10cSrcweir         if (!bNegate)
1948cdf0e10cSrcweir             negateSearchCondition(pSearchCondition,sal_True);   //  negate all deeper values
1949cdf0e10cSrcweir     }
1950cdf0e10cSrcweir     // row_value_constructor comparison row_value_constructor
1951cdf0e10cSrcweir     // row_value_constructor comparison any_all_some subquery
1952cdf0e10cSrcweir     else if(bNegate && (SQL_ISRULE(pSearchCondition,comparison_predicate) || SQL_ISRULE(pSearchCondition,all_or_any_predicate)))
1953cdf0e10cSrcweir     {
1954cdf0e10cSrcweir         OSQLParseNode* pComparison = pSearchCondition->getChild(1);
1955cdf0e10cSrcweir         OSQLParseNode* pNewComparison = NULL;
1956cdf0e10cSrcweir         switch(pComparison->getNodeType())
1957cdf0e10cSrcweir         {
1958cdf0e10cSrcweir             case SQL_NODE_EQUAL:
1959cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii("<>"),SQL_NODE_NOTEQUAL,SQL_NOTEQUAL);
1960cdf0e10cSrcweir                 break;
1961cdf0e10cSrcweir             case SQL_NODE_LESS:
1962cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii(">="),SQL_NODE_GREATEQ,SQL_GREATEQ);
1963cdf0e10cSrcweir                 break;
1964cdf0e10cSrcweir             case SQL_NODE_GREAT:
1965cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii("<="),SQL_NODE_LESSEQ,SQL_LESSEQ);
1966cdf0e10cSrcweir                 break;
1967cdf0e10cSrcweir             case SQL_NODE_LESSEQ:
1968cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii(">"),SQL_NODE_GREAT,SQL_GREAT);
1969cdf0e10cSrcweir                 break;
1970cdf0e10cSrcweir             case SQL_NODE_GREATEQ:
1971cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii("<"),SQL_NODE_LESS,SQL_LESS);
1972cdf0e10cSrcweir                 break;
1973cdf0e10cSrcweir             case SQL_NODE_NOTEQUAL:
1974cdf0e10cSrcweir                 pNewComparison = new OSQLParseNode(::rtl::OUString::createFromAscii("="),SQL_NODE_EQUAL,SQL_EQUAL);
1975cdf0e10cSrcweir                 break;
1976cdf0e10cSrcweir             default:
1977cdf0e10cSrcweir                 OSL_ENSURE( false, "OSQLParseNode::negateSearchCondition: unexpected node type!" );
1978cdf0e10cSrcweir                 break;
1979cdf0e10cSrcweir         }
1980cdf0e10cSrcweir         pSearchCondition->replace(pComparison, pNewComparison);
1981cdf0e10cSrcweir         delete pComparison;
1982cdf0e10cSrcweir     }
1983cdf0e10cSrcweir 
1984cdf0e10cSrcweir     else if(bNegate && (SQL_ISRULE(pSearchCondition,test_for_null) || SQL_ISRULE(pSearchCondition,in_predicate) ||
1985cdf0e10cSrcweir                         SQL_ISRULE(pSearchCondition,between_predicate) || SQL_ISRULE(pSearchCondition,boolean_test) ))
1986cdf0e10cSrcweir     {
1987cdf0e10cSrcweir         OSQLParseNode* pPart2 = pSearchCondition;
1988cdf0e10cSrcweir         if ( !SQL_ISRULE(pSearchCondition,boolean_test) )
1989cdf0e10cSrcweir             pPart2 = pSearchCondition->getChild(1);
1990cdf0e10cSrcweir         sal_uInt32 nNotPos = 0;
1991cdf0e10cSrcweir         if  ( SQL_ISRULE( pSearchCondition, test_for_null ) )
1992cdf0e10cSrcweir             nNotPos = 1;
1993cdf0e10cSrcweir         else if ( SQL_ISRULE( pSearchCondition, boolean_test ) )
1994cdf0e10cSrcweir             nNotPos = 2;
1995cdf0e10cSrcweir 
1996cdf0e10cSrcweir         OSQLParseNode* pNot = pPart2->getChild(nNotPos);
1997cdf0e10cSrcweir         OSQLParseNode* pNotNot = NULL;
1998cdf0e10cSrcweir         if(pNot->isRule())
1999cdf0e10cSrcweir             pNotNot = new OSQLParseNode(::rtl::OUString::createFromAscii("NOT"),SQL_NODE_KEYWORD,SQL_TOKEN_NOT);
2000cdf0e10cSrcweir         else
2001cdf0e10cSrcweir             pNotNot = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::sql_not));
2002cdf0e10cSrcweir         pPart2->replace(pNot, pNotNot);
2003cdf0e10cSrcweir         delete pNot;
2004cdf0e10cSrcweir     }
2005cdf0e10cSrcweir     else if(bNegate && (SQL_ISRULE(pSearchCondition,like_predicate)))
2006cdf0e10cSrcweir     {
2007cdf0e10cSrcweir         OSQLParseNode* pNot = pSearchCondition->getChild( 1 )->getChild( 0 );
2008cdf0e10cSrcweir         OSQLParseNode* pNotNot = NULL;
2009cdf0e10cSrcweir         if(pNot->isRule())
2010cdf0e10cSrcweir             pNotNot = new OSQLParseNode(::rtl::OUString::createFromAscii("NOT"),SQL_NODE_KEYWORD,SQL_TOKEN_NOT);
2011cdf0e10cSrcweir         else
2012cdf0e10cSrcweir             pNotNot = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::sql_not));
2013cdf0e10cSrcweir         pSearchCondition->getChild( 1 )->replace(pNot, pNotNot);
2014cdf0e10cSrcweir         delete pNot;
2015cdf0e10cSrcweir     }
2016cdf0e10cSrcweir }
2017cdf0e10cSrcweir //-----------------------------------------------------------------------------
2018cdf0e10cSrcweir void OSQLParseNode::eraseBraces(OSQLParseNode*& pSearchCondition)
2019cdf0e10cSrcweir {
2020cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::eraseBraces" );
2021cdf0e10cSrcweir     if (pSearchCondition && (SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
2022cdf0e10cSrcweir          SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")"))))
2023cdf0e10cSrcweir     {
2024cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(1);
2025cdf0e10cSrcweir         absorptions(pRight);
2026cdf0e10cSrcweir         // if child is not a or or and tree then delete () around child
2027cdf0e10cSrcweir         if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
2028cdf0e10cSrcweir             SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || // and can always stand without ()
2029cdf0e10cSrcweir             (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
2030cdf0e10cSrcweir         {
2031cdf0e10cSrcweir             OSQLParseNode* pNode = pSearchCondition->removeAt(1);
2032cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2033cdf0e10cSrcweir         }
2034cdf0e10cSrcweir     }
2035cdf0e10cSrcweir }
2036cdf0e10cSrcweir //-----------------------------------------------------------------------------
2037cdf0e10cSrcweir void OSQLParseNode::absorptions(OSQLParseNode*& pSearchCondition)
2038cdf0e10cSrcweir {
2039cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::absorptions" );
2040cdf0e10cSrcweir     if(!pSearchCondition) // no where condition at entry point
2041cdf0e10cSrcweir         return;
2042cdf0e10cSrcweir 
2043cdf0e10cSrcweir     eraseBraces(pSearchCondition);
2044cdf0e10cSrcweir 
2045cdf0e10cSrcweir     if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2046cdf0e10cSrcweir     {
2047cdf0e10cSrcweir         OSQLParseNode* pLeft = pSearchCondition->getChild(0);
2048cdf0e10cSrcweir         absorptions(pLeft);
2049cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
2050cdf0e10cSrcweir         absorptions(pRight);
2051cdf0e10cSrcweir     }
2052cdf0e10cSrcweir 
2053cdf0e10cSrcweir     sal_uInt32 nPos = 0;
2054cdf0e10cSrcweir     // a and a || a or a
2055cdf0e10cSrcweir     OSQLParseNode* pNewNode = NULL;
2056cdf0e10cSrcweir     if(( SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2057cdf0e10cSrcweir         && *pSearchCondition->getChild(0) == *pSearchCondition->getChild(2))
2058cdf0e10cSrcweir     {
2059cdf0e10cSrcweir         pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
2060cdf0e10cSrcweir         replaceAndReset(pSearchCondition,pNewNode);
2061cdf0e10cSrcweir     }
2062cdf0e10cSrcweir     // (a or b) and a || ( b or c ) and a
2063cdf0e10cSrcweir     // a and ( a or b) || a and ( b or c )
2064cdf0e10cSrcweir     else if (   SQL_ISRULE(pSearchCondition,boolean_term)
2065cdf0e10cSrcweir             &&  (
2066cdf0e10cSrcweir                     (       SQL_ISRULE(pSearchCondition->getChild(nPos = 0),boolean_primary)
2067cdf0e10cSrcweir                         ||  SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
2068cdf0e10cSrcweir                     )
2069cdf0e10cSrcweir                 ||  (       SQL_ISRULE(pSearchCondition->getChild(nPos = 2),boolean_primary)
2070cdf0e10cSrcweir                         ||  SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
2071cdf0e10cSrcweir                     )
2072cdf0e10cSrcweir                 )
2073cdf0e10cSrcweir             )
2074cdf0e10cSrcweir     {
2075cdf0e10cSrcweir         OSQLParseNode* p2ndSearch = pSearchCondition->getChild(nPos);
2076cdf0e10cSrcweir         if ( SQL_ISRULE(p2ndSearch,boolean_primary) )
2077cdf0e10cSrcweir             p2ndSearch = p2ndSearch->getChild(1);
2078cdf0e10cSrcweir 
2079cdf0e10cSrcweir         if ( *p2ndSearch->getChild(0) == *pSearchCondition->getChild(2-nPos) ) // a and ( a or b) -> a or b
2080cdf0e10cSrcweir         {
2081cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
2082cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2083cdf0e10cSrcweir 
2084cdf0e10cSrcweir         }
2085cdf0e10cSrcweir         else if ( *p2ndSearch->getChild(2) == *pSearchCondition->getChild(2-nPos) ) // a and ( b or a) -> a or b
2086cdf0e10cSrcweir         {
2087cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
2088cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2089cdf0e10cSrcweir         }
2090cdf0e10cSrcweir         else if ( p2ndSearch->getByRule(OSQLParseNode::search_condition) )
2091cdf0e10cSrcweir         {
2092cdf0e10cSrcweir             // a and ( b or c ) -> ( a and b ) or ( a and c )
2093cdf0e10cSrcweir             // ( b or c ) and a -> ( a and b ) or ( a and c )
2094cdf0e10cSrcweir             OSQLParseNode* pC = p2ndSearch->removeAt((sal_uInt32)2);
2095cdf0e10cSrcweir             OSQLParseNode* pB = p2ndSearch->removeAt((sal_uInt32)0);
2096cdf0e10cSrcweir             OSQLParseNode* pA = pSearchCondition->removeAt((sal_uInt32)2-nPos);
2097cdf0e10cSrcweir 
2098cdf0e10cSrcweir             OSQLParseNode* p1stAnd = MakeANDNode(pA,pB);
2099cdf0e10cSrcweir             OSQLParseNode* p2ndAnd = MakeANDNode(new OSQLParseNode(*pA),pC);
2100cdf0e10cSrcweir             pNewNode = MakeORNode(p1stAnd,p2ndAnd);
2101cdf0e10cSrcweir 			OSQLParseNode* pNode = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
2102cdf0e10cSrcweir             pNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION));
2103cdf0e10cSrcweir             pNode->append(pNewNode);
2104cdf0e10cSrcweir             pNode->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION));
2105cdf0e10cSrcweir 			OSQLParseNode::eraseBraces(p1stAnd);
2106cdf0e10cSrcweir             OSQLParseNode::eraseBraces(p2ndAnd);
2107cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2108cdf0e10cSrcweir         }
2109cdf0e10cSrcweir     }
2110cdf0e10cSrcweir     // a or a and b || a or b and a
2111cdf0e10cSrcweir     else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term))
2112cdf0e10cSrcweir     {
2113cdf0e10cSrcweir         if(*pSearchCondition->getChild(2)->getChild(0) == *pSearchCondition->getChild(0))
2114cdf0e10cSrcweir         {
2115cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
2116cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2117cdf0e10cSrcweir         }
2118cdf0e10cSrcweir         else if(*pSearchCondition->getChild(2)->getChild(2) == *pSearchCondition->getChild(0))
2119cdf0e10cSrcweir         {
2120cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
2121cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2122cdf0e10cSrcweir         }
2123cdf0e10cSrcweir     }
2124cdf0e10cSrcweir     // a and b or a || b and a or a
2125cdf0e10cSrcweir     else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term))
2126cdf0e10cSrcweir     {
2127cdf0e10cSrcweir         if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2))
2128cdf0e10cSrcweir         {
2129cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
2130cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2131cdf0e10cSrcweir         }
2132cdf0e10cSrcweir         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2))
2133cdf0e10cSrcweir         {
2134cdf0e10cSrcweir             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
2135cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNewNode);
2136cdf0e10cSrcweir         }
2137cdf0e10cSrcweir     }
2138cdf0e10cSrcweir     eraseBraces(pSearchCondition);
2139cdf0e10cSrcweir }
2140cdf0e10cSrcweir //-----------------------------------------------------------------------------
2141cdf0e10cSrcweir void OSQLParseNode::compress(OSQLParseNode *&pSearchCondition)
2142cdf0e10cSrcweir {
2143cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::compress" );
2144cdf0e10cSrcweir     if(!pSearchCondition) // no where condition at entry point
2145cdf0e10cSrcweir         return;
2146cdf0e10cSrcweir 
2147cdf0e10cSrcweir     OSQLParseNode::eraseBraces(pSearchCondition);
2148cdf0e10cSrcweir 
2149cdf0e10cSrcweir     if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2150cdf0e10cSrcweir     {
2151cdf0e10cSrcweir         OSQLParseNode* pLeft = pSearchCondition->getChild(0);
2152cdf0e10cSrcweir         compress(pLeft);
2153cdf0e10cSrcweir 
2154cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(2);
2155cdf0e10cSrcweir         compress(pRight);
2156cdf0e10cSrcweir     }
2157cdf0e10cSrcweir     else if( SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
2158cdf0e10cSrcweir              SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")")))
2159cdf0e10cSrcweir     {
2160cdf0e10cSrcweir         OSQLParseNode* pRight = pSearchCondition->getChild(1);
2161cdf0e10cSrcweir         compress(pRight);
2162cdf0e10cSrcweir         // if child is not a or or and tree then delete () around child
2163cdf0e10cSrcweir         if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
2164cdf0e10cSrcweir             (SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) && SQL_ISRULE(pSearchCondition->getParent(),boolean_term)) ||
2165cdf0e10cSrcweir             (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
2166cdf0e10cSrcweir         {
2167cdf0e10cSrcweir             OSQLParseNode* pNode = pSearchCondition->removeAt(1);
2168cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2169cdf0e10cSrcweir         }
2170cdf0e10cSrcweir     }
2171cdf0e10cSrcweir 
2172cdf0e10cSrcweir     // or with two and trees where one element of the and trees are equal
2173cdf0e10cSrcweir     if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term))
2174cdf0e10cSrcweir     {
2175cdf0e10cSrcweir         if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(0))
2176cdf0e10cSrcweir         {
2177cdf0e10cSrcweir             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt(2);
2178cdf0e10cSrcweir             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
2179cdf0e10cSrcweir             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
2180cdf0e10cSrcweir 
2181cdf0e10cSrcweir             OSQLParseNode* pNewRule = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
2182cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION));
2183cdf0e10cSrcweir             pNewRule->append(pNode);
2184cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION));
2185cdf0e10cSrcweir 
2186cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pLeft);
2187cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pRight);
2188cdf0e10cSrcweir 
2189cdf0e10cSrcweir             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt((sal_uInt32)0),pNewRule);
2190cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2191cdf0e10cSrcweir         }
2192cdf0e10cSrcweir         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(0))
2193cdf0e10cSrcweir         {
2194cdf0e10cSrcweir             OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt((sal_uInt32)0);
2195cdf0e10cSrcweir             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
2196cdf0e10cSrcweir             OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
2197cdf0e10cSrcweir 
2198cdf0e10cSrcweir             OSQLParseNode* pNewRule = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
2199cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION));
2200cdf0e10cSrcweir             pNewRule->append(pNode);
2201cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION));
2202cdf0e10cSrcweir 
2203cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pLeft);
2204cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pRight);
2205cdf0e10cSrcweir 
2206cdf0e10cSrcweir             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
2207cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2208cdf0e10cSrcweir         }
2209cdf0e10cSrcweir         else if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(2))
2210cdf0e10cSrcweir         {
2211cdf0e10cSrcweir             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt(2);
2212cdf0e10cSrcweir             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt((sal_uInt32)0);
2213cdf0e10cSrcweir             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
2214cdf0e10cSrcweir 
2215cdf0e10cSrcweir             OSQLParseNode* pNewRule = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
2216cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION));
2217cdf0e10cSrcweir             pNewRule->append(pNode);
2218cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION));
2219cdf0e10cSrcweir 
2220cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pLeft);
2221cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pRight);
2222cdf0e10cSrcweir 
2223cdf0e10cSrcweir             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt((sal_uInt32)0),pNewRule);
2224cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2225cdf0e10cSrcweir         }
2226cdf0e10cSrcweir         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(2))
2227cdf0e10cSrcweir         {
2228cdf0e10cSrcweir             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt((sal_uInt32)0);
2229cdf0e10cSrcweir             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt((sal_uInt32)0);
2230cdf0e10cSrcweir             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
2231cdf0e10cSrcweir 
2232cdf0e10cSrcweir             OSQLParseNode* pNewRule = new OSQLParseNode(::rtl::OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
2233cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii("("),SQL_NODE_PUNCTUATION));
2234cdf0e10cSrcweir             pNewRule->append(pNode);
2235cdf0e10cSrcweir             pNewRule->append(new OSQLParseNode(::rtl::OUString::createFromAscii(")"),SQL_NODE_PUNCTUATION));
2236cdf0e10cSrcweir 
2237cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pLeft);
2238cdf0e10cSrcweir             OSQLParseNode::eraseBraces(pRight);
2239cdf0e10cSrcweir 
2240cdf0e10cSrcweir             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
2241cdf0e10cSrcweir             replaceAndReset(pSearchCondition,pNode);
2242cdf0e10cSrcweir         }
2243cdf0e10cSrcweir     }
2244cdf0e10cSrcweir }
2245cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
2246cdf0e10cSrcweir // -----------------------------------------------------------------------------
2247cdf0e10cSrcweir void OSQLParseNode::showParseTree( ::rtl::OUString& rString ) const
2248cdf0e10cSrcweir {
2249cdf0e10cSrcweir     ::rtl::OUStringBuffer aBuf;
2250cdf0e10cSrcweir     showParseTree( aBuf, 0 );
2251cdf0e10cSrcweir     rString = aBuf.makeStringAndClear();
2252cdf0e10cSrcweir }
2253cdf0e10cSrcweir 
2254cdf0e10cSrcweir // -----------------------------------------------------------------------------
2255cdf0e10cSrcweir void OSQLParseNode::showParseTree( ::rtl::OUStringBuffer& _inout_rBuffer, sal_uInt32 nLevel ) const
2256cdf0e10cSrcweir {
2257cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::showParseTree" );
2258cdf0e10cSrcweir 
2259cdf0e10cSrcweir     for ( sal_uInt32 j=0; j<nLevel; ++j)
2260cdf0e10cSrcweir         _inout_rBuffer.appendAscii( "  " );
2261cdf0e10cSrcweir 
2262cdf0e10cSrcweir     if ( !isToken() )
2263cdf0e10cSrcweir     {
2264cdf0e10cSrcweir         // Regelnamen als rule: ...
2265cdf0e10cSrcweir         _inout_rBuffer.appendAscii( "RULE_ID: " );
2266cdf0e10cSrcweir         _inout_rBuffer.append( (sal_Int32)getRuleID() );
2267cdf0e10cSrcweir         _inout_rBuffer.append( sal_Unicode( '(' ) );
2268cdf0e10cSrcweir         _inout_rBuffer.append( OSQLParser::RuleIDToStr( getRuleID() ) );
2269cdf0e10cSrcweir         _inout_rBuffer.append( sal_Unicode( ')' ) );
2270cdf0e10cSrcweir         _inout_rBuffer.append( sal_Unicode( '\n' ) );
2271cdf0e10cSrcweir 
2272cdf0e10cSrcweir         // hol dir den ersten Subtree
2273cdf0e10cSrcweir         for (   OSQLParseNodes::const_iterator i = m_aChildren.begin();
2274cdf0e10cSrcweir                 i != m_aChildren.end();
2275cdf0e10cSrcweir                 ++i
2276cdf0e10cSrcweir              )
2277cdf0e10cSrcweir             (*i)->showParseTree( _inout_rBuffer, nLevel+1 );
2278cdf0e10cSrcweir     }
2279cdf0e10cSrcweir     else
2280cdf0e10cSrcweir     {
2281cdf0e10cSrcweir         // ein Token gefunden
2282cdf0e10cSrcweir         switch (m_eNodeType)
2283cdf0e10cSrcweir         {
2284cdf0e10cSrcweir 
2285cdf0e10cSrcweir         case SQL_NODE_KEYWORD:
2286cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_KEYWORD: " );
2287cdf0e10cSrcweir             _inout_rBuffer.append( ::rtl::OStringToOUString( OSQLParser::TokenIDToStr( getTokenID() ), RTL_TEXTENCODING_UTF8 ) );
2288cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2289cdf0e10cSrcweir             break;
2290cdf0e10cSrcweir 
2291cdf0e10cSrcweir         case SQL_NODE_COMPARISON:
2292cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_COMPARISON: " );
2293cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2294cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2295cdf0e10cSrcweir             break;
2296cdf0e10cSrcweir 
2297cdf0e10cSrcweir         case SQL_NODE_NAME:
2298cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_NAME: " );
2299cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '"' ) );
2300cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2301cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '"' ) );
2302cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2303cdf0e10cSrcweir              break;
2304cdf0e10cSrcweir 
2305cdf0e10cSrcweir         case SQL_NODE_STRING:
2306cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_STRING: " );
2307cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\'' ) );
2308cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2309cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\'' ) );
2310cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2311cdf0e10cSrcweir             break;
2312cdf0e10cSrcweir 
2313cdf0e10cSrcweir         case SQL_NODE_INTNUM:
2314cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_INTNUM: " );
2315cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2316cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2317cdf0e10cSrcweir             break;
2318cdf0e10cSrcweir 
2319cdf0e10cSrcweir         case SQL_NODE_APPROXNUM:
2320cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_APPROXNUM: " );
2321cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2322cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2323cdf0e10cSrcweir              break;
2324cdf0e10cSrcweir 
2325cdf0e10cSrcweir         case SQL_NODE_PUNCTUATION:
2326cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_PUNCTUATION: " );
2327cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2328cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2329cdf0e10cSrcweir             break;
2330cdf0e10cSrcweir 
2331cdf0e10cSrcweir         case SQL_NODE_AMMSC:
2332cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_AMMSC: " );
2333cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2334cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2335cdf0e10cSrcweir             break;
2336cdf0e10cSrcweir 
2337cdf0e10cSrcweir         case SQL_NODE_EQUAL:
2338cdf0e10cSrcweir         case SQL_NODE_LESS:
2339cdf0e10cSrcweir         case SQL_NODE_GREAT:
2340cdf0e10cSrcweir         case SQL_NODE_LESSEQ:
2341cdf0e10cSrcweir         case SQL_NODE_GREATEQ:
2342cdf0e10cSrcweir         case SQL_NODE_NOTEQUAL:
2343cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2344cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2345cdf0e10cSrcweir             break;
2346cdf0e10cSrcweir 
2347cdf0e10cSrcweir         case SQL_NODE_ACCESS_DATE:
2348cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_ACCESS_DATE: " );
2349cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2350cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2351cdf0e10cSrcweir             break;
2352cdf0e10cSrcweir 
2353cdf0e10cSrcweir         case SQL_NODE_DATE:
2354cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "SQL_DATE: " );
2355cdf0e10cSrcweir             _inout_rBuffer.append( m_aNodeValue );
2356cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2357cdf0e10cSrcweir             break;
2358cdf0e10cSrcweir 
2359cdf0e10cSrcweir         case SQL_NODE_CONCAT:
2360cdf0e10cSrcweir             _inout_rBuffer.appendAscii( "||" );
2361cdf0e10cSrcweir             _inout_rBuffer.append( sal_Unicode( '\n' ) );
2362cdf0e10cSrcweir             break;
2363cdf0e10cSrcweir 
2364cdf0e10cSrcweir         default:
2365cdf0e10cSrcweir             OSL_TRACE( "-- %i", int( m_eNodeType ) );
2366cdf0e10cSrcweir             OSL_ENSURE( false, "OSQLParser::ShowParseTree: unzulaessiger NodeType" );
2367cdf0e10cSrcweir         }
2368cdf0e10cSrcweir     }
2369cdf0e10cSrcweir }
2370cdf0e10cSrcweir #endif // OSL_DEBUG_LEVEL > 0
2371cdf0e10cSrcweir // -----------------------------------------------------------------------------
2372cdf0e10cSrcweir // Insert-Methoden
2373cdf0e10cSrcweir //-----------------------------------------------------------------------------
2374cdf0e10cSrcweir void OSQLParseNode::insert(sal_uInt32 nPos, OSQLParseNode* pNewSubTree)
2375cdf0e10cSrcweir {
2376cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::insert" );
2377cdf0e10cSrcweir     OSL_ENSURE(pNewSubTree != NULL, "OSQLParseNode: ungueltiger NewSubTree");
2378cdf0e10cSrcweir     OSL_ENSURE(pNewSubTree->getParent() == NULL, "OSQLParseNode: Knoten ist kein Waise");
2379cdf0e10cSrcweir 
2380cdf0e10cSrcweir     // stelle Verbindung zum getParent her:
2381cdf0e10cSrcweir     pNewSubTree->setParent( this );
2382cdf0e10cSrcweir     m_aChildren.insert(m_aChildren.begin() + nPos, pNewSubTree);
2383cdf0e10cSrcweir }
2384cdf0e10cSrcweir 
2385cdf0e10cSrcweir // removeAt-Methoden
2386cdf0e10cSrcweir //-----------------------------------------------------------------------------
2387cdf0e10cSrcweir OSQLParseNode* OSQLParseNode::removeAt(sal_uInt32 nPos)
2388cdf0e10cSrcweir {
2389cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::removeAt" );
2390cdf0e10cSrcweir     OSL_ENSURE(nPos < m_aChildren.size(),"Illegal position for removeAt");
2391cdf0e10cSrcweir     OSQLParseNodes::iterator aPos(m_aChildren.begin() + nPos);
2392cdf0e10cSrcweir     OSQLParseNode* pNode = *aPos;
2393cdf0e10cSrcweir 
2394cdf0e10cSrcweir     // setze den getParent des removeten auf NULL
2395cdf0e10cSrcweir     pNode->setParent( NULL );
2396cdf0e10cSrcweir 
2397cdf0e10cSrcweir     m_aChildren.erase(aPos);
2398cdf0e10cSrcweir     return pNode;
2399cdf0e10cSrcweir }
2400cdf0e10cSrcweir //-----------------------------------------------------------------------------
2401cdf0e10cSrcweir OSQLParseNode* OSQLParseNode::remove(OSQLParseNode* pSubTree)
2402cdf0e10cSrcweir {
2403cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::remove" );
2404cdf0e10cSrcweir     OSL_ENSURE(pSubTree != NULL, "OSQLParseNode: ungueltiger SubTree");
2405cdf0e10cSrcweir     OSQLParseNodes::iterator aPos = ::std::find(m_aChildren.begin(), m_aChildren.end(), pSubTree);
2406cdf0e10cSrcweir     if (aPos != m_aChildren.end())
2407cdf0e10cSrcweir     {
2408cdf0e10cSrcweir         // setze den getParent des removeten auf NULL
2409cdf0e10cSrcweir         pSubTree->setParent( NULL );
2410cdf0e10cSrcweir         m_aChildren.erase(aPos);
2411cdf0e10cSrcweir         return pSubTree;
2412cdf0e10cSrcweir     }
2413cdf0e10cSrcweir     else
2414cdf0e10cSrcweir         return NULL;
2415cdf0e10cSrcweir }
2416cdf0e10cSrcweir 
2417cdf0e10cSrcweir // Replace-Methoden
2418cdf0e10cSrcweir //-----------------------------------------------------------------------------
2419cdf0e10cSrcweir OSQLParseNode* OSQLParseNode::replaceAt(sal_uInt32 nPos, OSQLParseNode* pNewSubNode)
2420cdf0e10cSrcweir {
2421cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replaceAt" );
2422cdf0e10cSrcweir     OSL_ENSURE(pNewSubNode != NULL, "OSQLParseNode: invalid nodes");
2423cdf0e10cSrcweir     OSL_ENSURE(pNewSubNode->getParent() == NULL, "OSQLParseNode: node already has getParent");
2424cdf0e10cSrcweir     OSL_ENSURE(nPos < m_aChildren.size(), "OSQLParseNode: invalid position");
2425cdf0e10cSrcweir     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pNewSubNode) == m_aChildren.end(),
2426cdf0e10cSrcweir             "OSQLParseNode::Replace() Node already element of parent");
2427cdf0e10cSrcweir 
2428cdf0e10cSrcweir     OSQLParseNode* pOldSubNode = m_aChildren[nPos];
2429cdf0e10cSrcweir 
2430cdf0e10cSrcweir     // stelle Verbindung zum getParent her:
2431cdf0e10cSrcweir     pNewSubNode->setParent( this );
2432cdf0e10cSrcweir     pOldSubNode->setParent( NULL );
2433cdf0e10cSrcweir 
2434cdf0e10cSrcweir     m_aChildren[nPos] = pNewSubNode;
2435cdf0e10cSrcweir     return pOldSubNode;
2436cdf0e10cSrcweir }
2437cdf0e10cSrcweir 
2438cdf0e10cSrcweir //-----------------------------------------------------------------------------
2439cdf0e10cSrcweir OSQLParseNode* OSQLParseNode::replace (OSQLParseNode* pOldSubNode, OSQLParseNode* pNewSubNode )
2440cdf0e10cSrcweir {
2441cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::replace " );
2442cdf0e10cSrcweir     OSL_ENSURE(pOldSubNode != NULL && pNewSubNode != NULL, "OSQLParseNode: invalid nodes");
2443cdf0e10cSrcweir     OSL_ENSURE(pNewSubNode->getParent() == NULL, "OSQLParseNode: node already has getParent");
2444cdf0e10cSrcweir     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pOldSubNode) != m_aChildren.end(),
2445cdf0e10cSrcweir             "OSQLParseNode::Replace() Node not element of parent");
2446cdf0e10cSrcweir     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pNewSubNode) == m_aChildren.end(),
2447cdf0e10cSrcweir             "OSQLParseNode::Replace() Node already element of parent");
2448cdf0e10cSrcweir 
2449cdf0e10cSrcweir     pOldSubNode->setParent( NULL );
2450cdf0e10cSrcweir     pNewSubNode->setParent( this );
2451cdf0e10cSrcweir     ::std::replace(m_aChildren.begin(), m_aChildren.end(), pOldSubNode, pNewSubNode);
2452cdf0e10cSrcweir     return pOldSubNode;
2453cdf0e10cSrcweir }
2454cdf0e10cSrcweir // -----------------------------------------------------------------------------
2455cdf0e10cSrcweir void OSQLParseNode::parseLeaf(::rtl::OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
2456cdf0e10cSrcweir {
2457cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::parseLeaf" );
2458cdf0e10cSrcweir     // ein Blatt ist gefunden
2459cdf0e10cSrcweir     // Inhalt dem Ausgabestring anfuegen
2460cdf0e10cSrcweir     switch (m_eNodeType)
2461cdf0e10cSrcweir     {
2462cdf0e10cSrcweir         case SQL_NODE_KEYWORD:
2463cdf0e10cSrcweir         {
2464cdf0e10cSrcweir             if (rString.getLength())
2465cdf0e10cSrcweir                 rString.appendAscii(" ");
2466cdf0e10cSrcweir 
2467cdf0e10cSrcweir             const ::rtl::OString sT = OSQLParser::TokenIDToStr(m_nNodeID, rParam.bInternational ? &rParam.m_rContext :  NULL);
2468cdf0e10cSrcweir             rString.append(::rtl::OUString(sT,sT.getLength(),RTL_TEXTENCODING_UTF8));
2469cdf0e10cSrcweir         }   break;
2470cdf0e10cSrcweir         case SQL_NODE_STRING:
2471cdf0e10cSrcweir             if (rString.getLength())
2472cdf0e10cSrcweir                 rString.appendAscii(" ");
2473cdf0e10cSrcweir             rString.append(SetQuotation(m_aNodeValue,::rtl::OUString::createFromAscii("\'"),::rtl::OUString::createFromAscii("\'\'")));
2474cdf0e10cSrcweir             break;
2475cdf0e10cSrcweir         case SQL_NODE_NAME:
2476cdf0e10cSrcweir             if (rString.getLength())
2477cdf0e10cSrcweir             {
2478cdf0e10cSrcweir                 switch(rString.charAt(rString.getLength()-1) )
2479cdf0e10cSrcweir                 {
2480cdf0e10cSrcweir                     case ' ' :
2481cdf0e10cSrcweir                     case '.' : break;
2482cdf0e10cSrcweir                     default  :
2483cdf0e10cSrcweir                         if  (   !rParam.aMetaData.getCatalogSeparator().getLength()
2484cdf0e10cSrcweir                             ||  rString.charAt( rString.getLength()-1 ) != rParam.aMetaData.getCatalogSeparator().toChar()
2485cdf0e10cSrcweir                             )
2486cdf0e10cSrcweir                             rString.appendAscii(" "); break;
2487cdf0e10cSrcweir                 }
2488cdf0e10cSrcweir             }
2489cdf0e10cSrcweir             if (rParam.bQuote)
2490cdf0e10cSrcweir             {
2491cdf0e10cSrcweir                 if (rParam.bPredicate)
2492cdf0e10cSrcweir                 {
2493cdf0e10cSrcweir                     rString.appendAscii("[");
2494cdf0e10cSrcweir                     rString.append(m_aNodeValue);
2495cdf0e10cSrcweir                     rString.appendAscii("]");
2496cdf0e10cSrcweir                 }
2497cdf0e10cSrcweir                 else
2498cdf0e10cSrcweir                     rString.append(SetQuotation(m_aNodeValue,
2499cdf0e10cSrcweir                         rParam.aMetaData.getIdentifierQuoteString(), rParam.aMetaData.getIdentifierQuoteString() ));
2500cdf0e10cSrcweir             }
2501cdf0e10cSrcweir             else
2502cdf0e10cSrcweir                 rString.append(m_aNodeValue);
2503cdf0e10cSrcweir             break;
2504cdf0e10cSrcweir         case SQL_NODE_ACCESS_DATE:
2505cdf0e10cSrcweir             if (rString.getLength())
2506cdf0e10cSrcweir                 rString.appendAscii(" ");
2507cdf0e10cSrcweir             rString.appendAscii("#");
2508cdf0e10cSrcweir             rString.append(m_aNodeValue);
2509cdf0e10cSrcweir             rString.appendAscii("#");
2510cdf0e10cSrcweir             break;
2511cdf0e10cSrcweir 
2512cdf0e10cSrcweir         case SQL_NODE_INTNUM:
2513cdf0e10cSrcweir         case SQL_NODE_APPROXNUM:
2514cdf0e10cSrcweir             {
2515cdf0e10cSrcweir                 ::rtl::OUString aTmp = m_aNodeValue;
2516cdf0e10cSrcweir                 if (rParam.bInternational && rParam.bPredicate && rParam.cDecSep != '.')
2517cdf0e10cSrcweir                     aTmp = aTmp.replace('.', rParam.cDecSep);
2518cdf0e10cSrcweir 
2519cdf0e10cSrcweir                 if (rString.getLength())
2520cdf0e10cSrcweir                     rString.appendAscii(" ");
2521cdf0e10cSrcweir                 rString.append(aTmp);
2522cdf0e10cSrcweir 
2523cdf0e10cSrcweir             }   break;
2524cdf0e10cSrcweir         case SQL_NODE_PUNCTUATION:
2525cdf0e10cSrcweir             if ( getParent() && SQL_ISRULE(getParent(),cast_spec) && m_aNodeValue.toChar() == '(' ) // no spaces in front of '('
2526cdf0e10cSrcweir             {
2527cdf0e10cSrcweir                 rString.append(m_aNodeValue);
2528cdf0e10cSrcweir                 break;
2529cdf0e10cSrcweir             }
2530cdf0e10cSrcweir             // fall through
2531cdf0e10cSrcweir         default:
2532cdf0e10cSrcweir             if (rString.getLength() && m_aNodeValue.toChar() != '.' && m_aNodeValue.toChar() != ':' )
2533cdf0e10cSrcweir             {
2534cdf0e10cSrcweir                 switch( rString.charAt(rString.getLength()-1) )
2535cdf0e10cSrcweir                 {
2536cdf0e10cSrcweir                     case ' ' :
2537cdf0e10cSrcweir                     case '.' : break;
2538cdf0e10cSrcweir                     default  :
2539cdf0e10cSrcweir                         if  (   !rParam.aMetaData.getCatalogSeparator().getLength()
2540cdf0e10cSrcweir                             ||  rString.charAt( rString.getLength()-1 ) != rParam.aMetaData.getCatalogSeparator().toChar()
2541cdf0e10cSrcweir                             )
2542cdf0e10cSrcweir                             rString.appendAscii(" "); break;
2543cdf0e10cSrcweir                 }
2544cdf0e10cSrcweir             }
2545cdf0e10cSrcweir             rString.append(m_aNodeValue);
2546cdf0e10cSrcweir     }
2547cdf0e10cSrcweir }
2548cdf0e10cSrcweir 
2549cdf0e10cSrcweir // -----------------------------------------------------------------------------
2550cdf0e10cSrcweir sal_Int32 OSQLParser::getFunctionReturnType(const ::rtl::OUString& _sFunctionName, const IParseContext* pContext)
2551cdf0e10cSrcweir {
2552cdf0e10cSrcweir     sal_Int32 nType = DataType::VARCHAR;
2553cdf0e10cSrcweir     ::rtl::OString sFunctionName(_sFunctionName,_sFunctionName.getLength(),RTL_TEXTENCODING_UTF8);
2554cdf0e10cSrcweir 
2555cdf0e10cSrcweir     if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASCII,pContext)))                     nType = DataType::INTEGER;
2556cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_BIT_LENGTH,pContext)))           nType = DataType::INTEGER;
2557cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR,pContext)))                 nType = DataType::VARCHAR;
2558cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR_LENGTH,pContext)))          nType = DataType::INTEGER;
2559cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CONCAT,pContext)))               nType = DataType::VARCHAR;
2560cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DIFFERENCE,pContext)))           nType = DataType::VARCHAR;
2561cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_INSERT,pContext)))               nType = DataType::VARCHAR;
2562cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LCASE,pContext)))                nType = DataType::VARCHAR;
2563cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LEFT,pContext)))                 nType = DataType::VARCHAR;
2564cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LENGTH,pContext)))               nType = DataType::INTEGER;
2565cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE,pContext)))               nType = DataType::VARCHAR;
2566cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE_2,pContext)))             nType = DataType::VARCHAR;
2567cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LTRIM,pContext)))                nType = DataType::VARCHAR;
2568cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_OCTET_LENGTH,pContext)))         nType = DataType::INTEGER;
2569cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POSITION,pContext)))             nType = DataType::INTEGER;
2570cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPEAT,pContext)))               nType = DataType::VARCHAR;
2571cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPLACE,pContext)))              nType = DataType::VARCHAR;
2572cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RIGHT,pContext)))                nType = DataType::VARCHAR;
2573cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RTRIM,pContext)))                nType = DataType::VARCHAR;
2574cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SOUNDEX,pContext)))              nType = DataType::VARCHAR;
2575cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SPACE,pContext)))                nType = DataType::VARCHAR;
2576cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUBSTRING,pContext)))            nType = DataType::VARCHAR;
2577cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UCASE,pContext)))                nType = DataType::VARCHAR;
2578cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_DATE,pContext)))         nType = DataType::DATE;
2579cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIME,pContext)))         nType = DataType::TIME;
2580cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIMESTAMP,pContext)))    nType = DataType::TIMESTAMP;
2581cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURDATE,pContext)))              nType = DataType::DATE;
2582cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEDIFF,pContext)))             nType = DataType::INTEGER;
2583cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEVALUE,pContext)))            nType = DataType::DATE;
2584cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURTIME,pContext)))              nType = DataType::TIME;
2585cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYNAME,pContext)))              nType = DataType::VARCHAR;
2586cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFMONTH,pContext)))           nType = DataType::INTEGER;
2587cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFWEEK,pContext)))            nType = DataType::INTEGER;
2588cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFYEAR,pContext)))            nType = DataType::INTEGER;
2589cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXTRACT,pContext)))              nType = DataType::VARCHAR;
2590cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_HOUR,pContext)))                 nType = DataType::INTEGER;
2591cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MINUTE,pContext)))               nType = DataType::INTEGER;
2592cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTH,pContext)))                nType = DataType::INTEGER;
2593cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTHNAME,pContext)))            nType = DataType::VARCHAR;
2594cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_NOW,pContext)))                  nType = DataType::TIMESTAMP;
2595cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_QUARTER,pContext)))              nType = DataType::INTEGER;
2596cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SECOND,pContext)))               nType = DataType::INTEGER;
2597cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPADD,pContext)))         nType = DataType::TIMESTAMP;
2598cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPDIFF,pContext)))        nType = DataType::TIMESTAMP;
2599cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMEVALUE,pContext)))            nType = DataType::TIMESTAMP;
2600cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_WEEK,pContext)))                 nType = DataType::INTEGER;
2601cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_YEAR,pContext)))                 nType = DataType::INTEGER;
2602cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ABS,pContext)))                  nType = DataType::DOUBLE;
2603cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ACOS,pContext)))                 nType = DataType::DOUBLE;
2604cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASIN,pContext)))                 nType = DataType::DOUBLE;
2605cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN,pContext)))                 nType = DataType::DOUBLE;
2606cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN2,pContext)))                nType = DataType::DOUBLE;
2607cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CEILING,pContext)))              nType = DataType::DOUBLE;
2608cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COS,pContext)))                  nType = DataType::DOUBLE;
2609cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COT,pContext)))                  nType = DataType::DOUBLE;
2610cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DEGREES,pContext)))              nType = DataType::DOUBLE;
2611cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXP,pContext)))                  nType = DataType::DOUBLE;
2612cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_FLOOR,pContext)))                nType = DataType::DOUBLE;
2613cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOGF,pContext)))                 nType = DataType::DOUBLE;
2614cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG,pContext)))                  nType = DataType::DOUBLE;
2615cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG10,pContext)))                nType = DataType::DOUBLE;
2616cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LN,pContext)))                   nType = DataType::DOUBLE;
2617cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MOD,pContext)))                  nType = DataType::DOUBLE;
2618cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_PI,pContext)))                   nType = DataType::DOUBLE;
2619cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POWER,pContext)))                nType = DataType::DOUBLE;
2620cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RADIANS,pContext)))              nType = DataType::DOUBLE;
2621cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RAND,pContext)))                 nType = DataType::DOUBLE;
2622cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUND,pContext)))                nType = DataType::DOUBLE;
2623cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUNDMAGIC,pContext)))           nType = DataType::DOUBLE;
2624cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIGN,pContext)))                 nType = DataType::DOUBLE;
2625cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIN,pContext)))                  nType = DataType::DOUBLE;
2626cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SQRT,pContext)))                 nType = DataType::DOUBLE;
2627cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TAN,pContext)))                  nType = DataType::DOUBLE;
2628cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TRUNCATE,pContext)))             nType = DataType::DOUBLE;
2629cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COUNT,pContext)))                nType = DataType::INTEGER;
2630cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MAX,pContext)))                  nType = DataType::DOUBLE;
2631cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MIN,pContext)))                  nType = DataType::DOUBLE;
2632cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_AVG,pContext)))                  nType = DataType::DOUBLE;
2633cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUM,pContext)))                  nType = DataType::DOUBLE;
2634cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOWER,pContext)))                nType = DataType::VARCHAR;
2635cdf0e10cSrcweir     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UPPER,pContext)))                nType = DataType::VARCHAR;
2636cdf0e10cSrcweir 
2637cdf0e10cSrcweir     return nType;
2638cdf0e10cSrcweir }
2639cdf0e10cSrcweir // -----------------------------------------------------------------------------
2640cdf0e10cSrcweir sal_Int32 OSQLParser::getFunctionParameterType(sal_uInt32 _nTokenId, sal_uInt32 _nPos)
2641cdf0e10cSrcweir {
2642cdf0e10cSrcweir     sal_Int32 nType = DataType::VARCHAR;
2643cdf0e10cSrcweir 
2644cdf0e10cSrcweir     if(_nTokenId == SQL_TOKEN_CHAR)                 nType = DataType::INTEGER;
2645cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_INSERT)
2646cdf0e10cSrcweir     {
2647cdf0e10cSrcweir         if ( _nPos == 2 || _nPos == 3 )
2648cdf0e10cSrcweir             nType = DataType::INTEGER;
2649cdf0e10cSrcweir     }
2650cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LEFT)
2651cdf0e10cSrcweir     {
2652cdf0e10cSrcweir         if ( _nPos == 2 )
2653cdf0e10cSrcweir             nType = DataType::INTEGER;
2654cdf0e10cSrcweir     }
2655cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOCATE)
2656cdf0e10cSrcweir     {
2657cdf0e10cSrcweir         if ( _nPos == 3 )
2658cdf0e10cSrcweir             nType = DataType::INTEGER;
2659cdf0e10cSrcweir     }
2660cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOCATE_2)
2661cdf0e10cSrcweir     {
2662cdf0e10cSrcweir         if ( _nPos == 3 )
2663cdf0e10cSrcweir             nType = DataType::INTEGER;
2664cdf0e10cSrcweir     }
2665cdf0e10cSrcweir     else if( _nTokenId == SQL_TOKEN_REPEAT || _nTokenId == SQL_TOKEN_RIGHT )
2666cdf0e10cSrcweir     {
2667cdf0e10cSrcweir         if ( _nPos == 2 )
2668cdf0e10cSrcweir             nType = DataType::INTEGER;
2669cdf0e10cSrcweir     }
2670cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SPACE )
2671cdf0e10cSrcweir     {
2672cdf0e10cSrcweir         nType = DataType::INTEGER;
2673cdf0e10cSrcweir     }
2674cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SUBSTRING)
2675cdf0e10cSrcweir     {
2676cdf0e10cSrcweir         if ( _nPos != 1 )
2677cdf0e10cSrcweir             nType = DataType::INTEGER;
2678cdf0e10cSrcweir     }
2679cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DATEDIFF)
2680cdf0e10cSrcweir     {
2681cdf0e10cSrcweir         if ( _nPos != 1 )
2682cdf0e10cSrcweir             nType = DataType::TIMESTAMP;
2683cdf0e10cSrcweir     }
2684cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DATEVALUE)
2685cdf0e10cSrcweir         nType = DataType::DATE;
2686cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DAYNAME)
2687cdf0e10cSrcweir         nType = DataType::DATE;
2688cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DAYOFMONTH)
2689cdf0e10cSrcweir         nType = DataType::DATE;
2690cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DAYOFWEEK)
2691cdf0e10cSrcweir         nType = DataType::DATE;
2692cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DAYOFYEAR)
2693cdf0e10cSrcweir         nType = DataType::DATE;
2694cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_EXTRACT)              nType = DataType::VARCHAR;
2695cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_HOUR)                 nType = DataType::TIME;
2696cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MINUTE)               nType = DataType::TIME;
2697cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MONTH)                nType = DataType::DATE;
2698cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MONTHNAME)            nType = DataType::DATE;
2699cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_NOW)                  nType = DataType::TIMESTAMP;
2700cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_QUARTER)              nType = DataType::DATE;
2701cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SECOND)               nType = DataType::TIME;
2702cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_TIMESTAMPADD)         nType = DataType::TIMESTAMP;
2703cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_TIMESTAMPDIFF)        nType = DataType::TIMESTAMP;
2704cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_TIMEVALUE)            nType = DataType::TIMESTAMP;
2705cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_WEEK)                 nType = DataType::DATE;
2706cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_YEAR)                 nType = DataType::DATE;
2707cdf0e10cSrcweir 
2708cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ABS)                  nType = DataType::DOUBLE;
2709cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ACOS)                 nType = DataType::DOUBLE;
2710cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ASIN)                 nType = DataType::DOUBLE;
2711cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ATAN)                 nType = DataType::DOUBLE;
2712cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ATAN2)                nType = DataType::DOUBLE;
2713cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_CEILING)              nType = DataType::DOUBLE;
2714cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_COS)                  nType = DataType::DOUBLE;
2715cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_COT)                  nType = DataType::DOUBLE;
2716cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_DEGREES)              nType = DataType::DOUBLE;
2717cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_EXP)                  nType = DataType::DOUBLE;
2718cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_FLOOR)                nType = DataType::DOUBLE;
2719cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOGF)                 nType = DataType::DOUBLE;
2720cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOG)                  nType = DataType::DOUBLE;
2721cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOG10)                nType = DataType::DOUBLE;
2722cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LN)                   nType = DataType::DOUBLE;
2723cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MOD)                  nType = DataType::DOUBLE;
2724cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_PI)                   nType = DataType::DOUBLE;
2725cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_POWER)                nType = DataType::DOUBLE;
2726cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_RADIANS)              nType = DataType::DOUBLE;
2727cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_RAND)                 nType = DataType::DOUBLE;
2728cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ROUND)                nType = DataType::DOUBLE;
2729cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_ROUNDMAGIC)           nType = DataType::DOUBLE;
2730cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SIGN)                 nType = DataType::DOUBLE;
2731cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SIN)                  nType = DataType::DOUBLE;
2732cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SQRT)                 nType = DataType::DOUBLE;
2733cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_TAN)                  nType = DataType::DOUBLE;
2734cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_TRUNCATE)             nType = DataType::DOUBLE;
2735cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_COUNT)                nType = DataType::INTEGER;
2736cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MAX)                  nType = DataType::DOUBLE;
2737cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_MIN)                  nType = DataType::DOUBLE;
2738cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_AVG)                  nType = DataType::DOUBLE;
2739cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_SUM)                  nType = DataType::DOUBLE;
2740cdf0e10cSrcweir 
2741cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_LOWER)                nType = DataType::VARCHAR;
2742cdf0e10cSrcweir     else if(_nTokenId == SQL_TOKEN_UPPER)                nType = DataType::VARCHAR;
2743cdf0e10cSrcweir 
2744cdf0e10cSrcweir     return nType;
2745cdf0e10cSrcweir }
2746cdf0e10cSrcweir 
2747cdf0e10cSrcweir // -----------------------------------------------------------------------------
2748cdf0e10cSrcweir const SQLError& OSQLParser::getErrorHelper() const
2749cdf0e10cSrcweir {
2750cdf0e10cSrcweir     return m_pData->aErrors;
2751cdf0e10cSrcweir }
2752cdf0e10cSrcweir 
2753cdf0e10cSrcweir // -----------------------------------------------------------------------------
2754cdf0e10cSrcweir OSQLParseNode::Rule OSQLParseNode::getKnownRuleID() const
2755cdf0e10cSrcweir {
2756cdf0e10cSrcweir     if ( !isRule() )
2757cdf0e10cSrcweir         return UNKNOWN_RULE;
2758cdf0e10cSrcweir     return OSQLParser::RuleIDToRule( getRuleID() );
2759cdf0e10cSrcweir }
2760cdf0e10cSrcweir // -----------------------------------------------------------------------------
2761cdf0e10cSrcweir ::rtl::OUString OSQLParseNode::getTableRange(const OSQLParseNode* _pTableRef)
2762cdf0e10cSrcweir {
2763cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "parse", "Ocke.Janssen@sun.com", "OSQLParseNode::getTableRange" );
2764cdf0e10cSrcweir     OSL_ENSURE(_pTableRef && _pTableRef->count() > 1 && _pTableRef->getKnownRuleID() == OSQLParseNode::table_ref,"Invalid node give, only table ref is allowed!");
2765cdf0e10cSrcweir     const sal_uInt32 nCount = _pTableRef->count();
2766cdf0e10cSrcweir     ::rtl::OUString sTableRange;
2767cdf0e10cSrcweir     if ( nCount == 2 || (nCount == 3 && !_pTableRef->getChild(0)->isToken()) || nCount == 5 )
2768cdf0e10cSrcweir     {
2769cdf0e10cSrcweir         const OSQLParseNode* pNode = _pTableRef->getChild(nCount - (nCount == 2 ? 1 : 2));
2770cdf0e10cSrcweir         OSL_ENSURE(pNode && (pNode->getKnownRuleID() == OSQLParseNode::table_primary_as_range_column
2771cdf0e10cSrcweir                           || pNode->getKnownRuleID() == OSQLParseNode::range_variable)
2772cdf0e10cSrcweir                          ,"SQL grammar changed!");
2773cdf0e10cSrcweir         if ( !pNode->isLeaf() )
2774cdf0e10cSrcweir             sTableRange = pNode->getChild(1)->getTokenValue();
2775cdf0e10cSrcweir     } // if ( nCount == 2 || nCount == 3 || nCount == 5)
2776cdf0e10cSrcweir 
2777cdf0e10cSrcweir     return sTableRange;
2778cdf0e10cSrcweir }
2779cdf0e10cSrcweir // -----------------------------------------------------------------------------
2780cdf0e10cSrcweir OSQLParseNodesContainer::OSQLParseNodesContainer()
2781cdf0e10cSrcweir {
2782cdf0e10cSrcweir }
2783cdf0e10cSrcweir // -----------------------------------------------------------------------------
2784cdf0e10cSrcweir OSQLParseNodesContainer::~OSQLParseNodesContainer()
2785cdf0e10cSrcweir {
2786cdf0e10cSrcweir }
2787cdf0e10cSrcweir // -----------------------------------------------------------------------------
2788cdf0e10cSrcweir void OSQLParseNodesContainer::push_back(OSQLParseNode* _pNode)
2789cdf0e10cSrcweir {
2790cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
2791cdf0e10cSrcweir     m_aNodes.push_back(_pNode);
2792cdf0e10cSrcweir }
2793cdf0e10cSrcweir // -----------------------------------------------------------------------------
2794cdf0e10cSrcweir void OSQLParseNodesContainer::erase(OSQLParseNode* _pNode)
2795cdf0e10cSrcweir {
2796cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
2797cdf0e10cSrcweir     if ( !m_aNodes.empty() )
2798cdf0e10cSrcweir     {
2799cdf0e10cSrcweir         ::std::vector< OSQLParseNode* >::iterator aFind = ::std::find(m_aNodes.begin(), m_aNodes.end(),_pNode);
2800cdf0e10cSrcweir         if ( aFind != m_aNodes.end() )
2801cdf0e10cSrcweir             m_aNodes.erase(aFind);
2802cdf0e10cSrcweir     }
2803cdf0e10cSrcweir }
2804cdf0e10cSrcweir // -----------------------------------------------------------------------------
2805cdf0e10cSrcweir bool OSQLParseNodesContainer::empty() const
2806cdf0e10cSrcweir {
2807cdf0e10cSrcweir     return m_aNodes.empty();
2808cdf0e10cSrcweir }
2809cdf0e10cSrcweir // -----------------------------------------------------------------------------
2810cdf0e10cSrcweir void OSQLParseNodesContainer::clear()
2811cdf0e10cSrcweir {
2812cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
2813cdf0e10cSrcweir     m_aNodes.clear();
2814cdf0e10cSrcweir }
2815cdf0e10cSrcweir // -----------------------------------------------------------------------------
2816cdf0e10cSrcweir void OSQLParseNodesContainer::clearAndDelete()
2817cdf0e10cSrcweir {
2818cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
2819cdf0e10cSrcweir     // clear the garbage collector
2820cdf0e10cSrcweir     while ( !m_aNodes.empty() )
2821cdf0e10cSrcweir     {
2822cdf0e10cSrcweir 	    OSQLParseNode* pNode = m_aNodes[0];
2823cdf0e10cSrcweir         while ( pNode->getParent() )
2824cdf0e10cSrcweir         {
2825cdf0e10cSrcweir             pNode = pNode->getParent();
2826cdf0e10cSrcweir         }
2827cdf0e10cSrcweir         delete pNode;
2828cdf0e10cSrcweir     }
2829cdf0e10cSrcweir }
2830cdf0e10cSrcweir }   // namespace connectivity
2831