xref: /AOO41X/main/dbaccess/source/core/api/SingleSelectQueryComposer.cxx (revision 0d4d71398ddb4fb5c7389f9bc621c934b9a02c81)
196de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
396de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
496de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
596de5490SAndrew Rist  * distributed with this work for additional information
696de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
796de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
896de5490SAndrew Rist  * "License"); you may not use this file except in compliance
996de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1196de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1396de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1496de5490SAndrew Rist  * software distributed under the License is distributed on an
1596de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1696de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
1796de5490SAndrew Rist  * specific language governing permissions and limitations
1896de5490SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2096de5490SAndrew Rist  *************************************************************/
2196de5490SAndrew Rist 
2296de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "composertools.hxx"
28cdf0e10cSrcweir #include "core_resource.hrc"
29cdf0e10cSrcweir #include "core_resource.hxx"
30cdf0e10cSrcweir #include "dbastrings.hrc"
31cdf0e10cSrcweir #include "HelperCollections.hxx"
32cdf0e10cSrcweir #include "SingleSelectQueryComposer.hxx"
33cdf0e10cSrcweir #include "sdbcoretools.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** === begin UNO includes === **/
36cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
37cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
38cdf0e10cSrcweir #include <com/sun/star/i18n/XLocaleData.hpp>
39cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
41cdf0e10cSrcweir #include <com/sun/star/sdb/SQLFilterOperator.hpp>
42cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
43cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
44cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnSearch.hpp>
45cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
46cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
47cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
48cdf0e10cSrcweir #include <com/sun/star/sdbc/XParameters.hpp>
49cdf0e10cSrcweir #include <com/sun/star/uno/XAggregation.hpp>
50cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp>
51cdf0e10cSrcweir /** === end UNO includes === **/
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
54cdf0e10cSrcweir #include <comphelper/sequence.hxx>
55cdf0e10cSrcweir #include <comphelper/types.hxx>
56cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
57cdf0e10cSrcweir #include <connectivity/predicateinput.hxx>
58cdf0e10cSrcweir #include <rtl/logfile.hxx>
59cdf0e10cSrcweir #include <unotools/syslocale.hxx>
60cdf0e10cSrcweir #include <tools/debug.hxx>
61cdf0e10cSrcweir #include <tools/diagnose_ex.h>
62cdf0e10cSrcweir #include <unotools/configmgr.hxx>
63cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #include <memory>
66cdf0e10cSrcweir 
67cdf0e10cSrcweir using namespace ::dbaccess;
68cdf0e10cSrcweir using namespace ::dbtools;
69cdf0e10cSrcweir using namespace ::comphelper;
70cdf0e10cSrcweir using namespace ::connectivity;
71cdf0e10cSrcweir using namespace ::com::sun::star::uno;
72cdf0e10cSrcweir using namespace ::com::sun::star::beans;
73cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
74cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
75cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
76cdf0e10cSrcweir using namespace ::com::sun::star::container;
77cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
78cdf0e10cSrcweir using namespace ::com::sun::star::lang;
79cdf0e10cSrcweir using namespace ::com::sun::star::script;
80cdf0e10cSrcweir using namespace ::com::sun::star::util;
81cdf0e10cSrcweir using namespace ::cppu;
82cdf0e10cSrcweir using namespace ::osl;
83cdf0e10cSrcweir using namespace ::utl;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir namespace dbaccess {
86cdf0e10cSrcweir namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir #define STR_SELECT		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SELECT "))
90cdf0e10cSrcweir #define STR_FROM		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" FROM "))
91cdf0e10cSrcweir #define STR_WHERE		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" WHERE "))
92cdf0e10cSrcweir #define STR_GROUP_BY	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" GROUP BY "))
93cdf0e10cSrcweir #define STR_HAVING		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" HAVING "))
94cdf0e10cSrcweir #define STR_ORDER_BY	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ORDER BY "))
95cdf0e10cSrcweir #define STR_AND			::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" AND "))
96cdf0e10cSrcweir #define STR_OR			::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" OR "))
97cdf0e10cSrcweir #define STR_LIKE		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" LIKE "))
98cdf0e10cSrcweir #define STR_EQUAL		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" = "))
99cdf0e10cSrcweir #define L_BRACKET		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("("))
100cdf0e10cSrcweir #define R_BRACKET		::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")"))
101cdf0e10cSrcweir #define COMMA			::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","))
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // -------------------------------------------------------------------------
104cdf0e10cSrcweir namespace
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     // .....................................................................
107cdf0e10cSrcweir     /** parses the given statement, using the given parser, returns a parse node representing
108cdf0e10cSrcweir         the statement
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         If the statement cannot be parsed, an error is thrown.
111cdf0e10cSrcweir     */
parseStatement_throwError(OSQLParser & _rParser,const::rtl::OUString & _rStatement,const Reference<XInterface> & _rxContext)112cdf0e10cSrcweir     const OSQLParseNode* parseStatement_throwError( OSQLParser& _rParser, const ::rtl::OUString& _rStatement, const Reference< XInterface >& _rxContext )
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "frank.schoenheit@sun.com", "SingleSelectQueryComposer.cxx::parseStatement_throwError" );
115cdf0e10cSrcweir 	    ::rtl::OUString aErrorMsg;
116cdf0e10cSrcweir 	    const OSQLParseNode* pNewSqlParseNode = _rParser.parseTree( aErrorMsg, _rStatement );
117cdf0e10cSrcweir 	    if ( !pNewSqlParseNode )
118cdf0e10cSrcweir 	    {
119cdf0e10cSrcweir             ::rtl::OUString sSQLStateGeneralError( getStandardSQLState( SQL_GENERAL_ERROR ) );
120cdf0e10cSrcweir 		    SQLException aError2( aErrorMsg, _rxContext, sSQLStateGeneralError, 1000, Any() );
121cdf0e10cSrcweir 		    SQLException aError1( _rStatement, _rxContext, sSQLStateGeneralError, 1000, makeAny( aError2 ) );
122cdf0e10cSrcweir 		    throw SQLException(_rParser.getContext().getErrorMessage(OParseContext::ERROR_GENERAL),_rxContext,sSQLStateGeneralError,1000,makeAny(aError1));
123cdf0e10cSrcweir 	    }
124cdf0e10cSrcweir         return pNewSqlParseNode;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     // .....................................................................
128cdf0e10cSrcweir     /** checks whether the given parse node describes a valid single select statement, throws
129cdf0e10cSrcweir         an error if not
130cdf0e10cSrcweir     */
checkForSingleSelect_throwError(const OSQLParseNode * pStatementNode,OSQLParseTreeIterator & _rIterator,const Reference<XInterface> & _rxContext,const::rtl::OUString & _rOriginatingCommand)131cdf0e10cSrcweir     void checkForSingleSelect_throwError( const OSQLParseNode* pStatementNode, OSQLParseTreeIterator& _rIterator,
132cdf0e10cSrcweir         const Reference< XInterface >& _rxContext, const ::rtl::OUString& _rOriginatingCommand )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         const OSQLParseNode* pOldNode = _rIterator.getParseTree();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         // determine the statement type
137cdf0e10cSrcweir 	    _rIterator.setParseTree( pStatementNode );
138cdf0e10cSrcweir 	    _rIterator.traverseAll();
139cdf0e10cSrcweir         bool bIsSingleSelect = ( _rIterator.getStatementType() == SQL_STATEMENT_SELECT );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         // throw the error, if necessary
142cdf0e10cSrcweir 	    if ( !bIsSingleSelect || SQL_ISRULE( pStatementNode, union_statement ) ) // #i4229# OJ
143cdf0e10cSrcweir 	    {
144cdf0e10cSrcweir             // restore the old node before throwing the exception
145cdf0e10cSrcweir             _rIterator.setParseTree( pOldNode );
146cdf0e10cSrcweir             // and now really ...
147cdf0e10cSrcweir 		    SQLException aError1( _rOriginatingCommand, _rxContext, getStandardSQLState( SQL_GENERAL_ERROR ), 1000, Any() );
148cdf0e10cSrcweir 		    throw SQLException( DBACORE_RESSTRING( RID_STR_ONLY_QUERY ), _rxContext,
149cdf0e10cSrcweir 			    getStandardSQLState( SQL_GENERAL_ERROR ), 1000, makeAny( aError1 ) );
150cdf0e10cSrcweir 	    }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         delete pOldNode;
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     // .....................................................................
156cdf0e10cSrcweir     /** combines parseStatement_throwError and checkForSingleSelect_throwError
157cdf0e10cSrcweir     */
parseAndCheck_throwError(OSQLParser & _rParser,const::rtl::OUString & _rStatement,OSQLParseTreeIterator & _rIterator,const Reference<XInterface> & _rxContext)158cdf0e10cSrcweir     void parseAndCheck_throwError( OSQLParser& _rParser, const ::rtl::OUString& _rStatement,
159cdf0e10cSrcweir         OSQLParseTreeIterator& _rIterator, const Reference< XInterface >& _rxContext )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "frank.schoenheit@sun.com", "SingleSelectQueryComposer.cxx::parseAndCheck_throwError" );
162cdf0e10cSrcweir         const OSQLParseNode* pNode = parseStatement_throwError( _rParser, _rStatement, _rxContext );
163cdf0e10cSrcweir         checkForSingleSelect_throwError( pNode, _rIterator, _rxContext, _rStatement );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     // .....................................................................
167cdf0e10cSrcweir     /** transforms a parse node describing a complete statement into a pure select
168cdf0e10cSrcweir         statement, without any filter/order/groupby/having clauses
169cdf0e10cSrcweir     */
getPureSelectStatement(const OSQLParseNode * _pRootNode,Reference<XConnection> _rxConnection)170cdf0e10cSrcweir     ::rtl::OUString getPureSelectStatement( const OSQLParseNode* _pRootNode, Reference< XConnection > _rxConnection )
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         ::rtl::OUString sSQL = STR_SELECT;
173cdf0e10cSrcweir 	    _pRootNode->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
174cdf0e10cSrcweir 	    _pRootNode->getChild(2)->parseNodeToStr( sSQL, _rxConnection );
175cdf0e10cSrcweir 	    sSQL += STR_FROM;
176cdf0e10cSrcweir 	    _pRootNode->getChild(3)->getChild(0)->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
177cdf0e10cSrcweir         return sSQL;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     /** resets an SQL iterator, including deletion of the parse tree, and disposal if desired
181cdf0e10cSrcweir     */
resetIterator(OSQLParseTreeIterator & _rIterator,bool _bDispose)182cdf0e10cSrcweir     void resetIterator( OSQLParseTreeIterator& _rIterator, bool _bDispose )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir 	    const OSQLParseNode* pSqlParseNode = _rIterator.getParseTree();
185cdf0e10cSrcweir 	    _rIterator.setParseTree(NULL);
186cdf0e10cSrcweir 	    delete pSqlParseNode;
187cdf0e10cSrcweir         if ( _bDispose )
188cdf0e10cSrcweir 	        _rIterator.dispose();
189cdf0e10cSrcweir     }
lcl_addFilterCriteria_throw(sal_Int32 i_nFilterOperator,const::rtl::OUString & i_sValue,::rtl::OUStringBuffer & o_sRet)190cdf0e10cSrcweir     void lcl_addFilterCriteria_throw(sal_Int32 i_nFilterOperator,const ::rtl::OUString& i_sValue,::rtl::OUStringBuffer& o_sRet)
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         switch( i_nFilterOperator )
193cdf0e10cSrcweir 	    {
194cdf0e10cSrcweir 		    case SQLFilterOperator::EQUAL:
195cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" = ")));
196cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
197cdf0e10cSrcweir 			    break;
198cdf0e10cSrcweir 		    case SQLFilterOperator::NOT_EQUAL:
199cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" <> ")));
200cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
201cdf0e10cSrcweir 			    break;
202cdf0e10cSrcweir 		    case SQLFilterOperator::LESS:
203cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" < ")));
204cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
205cdf0e10cSrcweir 			    break;
206cdf0e10cSrcweir 		    case SQLFilterOperator::GREATER:
207cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" > ")));
208cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
209cdf0e10cSrcweir 			    break;
210cdf0e10cSrcweir 		    case SQLFilterOperator::LESS_EQUAL:
211cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" <= ")));
212cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
213cdf0e10cSrcweir 			    break;
214cdf0e10cSrcweir 		    case SQLFilterOperator::GREATER_EQUAL:
215cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" >= ")));
216cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
217cdf0e10cSrcweir 			    break;
218cdf0e10cSrcweir 		    case SQLFilterOperator::LIKE:
219cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" LIKE ")));
220cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
221cdf0e10cSrcweir 			    break;
222cdf0e10cSrcweir 		    case SQLFilterOperator::NOT_LIKE:
223cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" NOT LIKE ")));
224cdf0e10cSrcweir 			    o_sRet.append(i_sValue);
225cdf0e10cSrcweir 			    break;
226cdf0e10cSrcweir 		    case SQLFilterOperator::SQLNULL:
227cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" IS NULL")) );
228cdf0e10cSrcweir 			    break;
229cdf0e10cSrcweir 		    case SQLFilterOperator::NOT_SQLNULL:
230cdf0e10cSrcweir 			    o_sRet.append(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" IS NOT NULL")) );
231cdf0e10cSrcweir 			    break;
232cdf0e10cSrcweir 		    default:
233cdf0e10cSrcweir 			    throw SQLException();
234cdf0e10cSrcweir 	    }
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
DBG_NAME(OSingleSelectQueryComposer)239cdf0e10cSrcweir DBG_NAME(OSingleSelectQueryComposer)
240cdf0e10cSrcweir // -------------------------------------------------------------------------
241cdf0e10cSrcweir OSingleSelectQueryComposer::OSingleSelectQueryComposer(const Reference< XNameAccess>& _rxTables,
242cdf0e10cSrcweir 							   const Reference< XConnection>& _xConnection,
243cdf0e10cSrcweir                                const ::comphelper::ComponentContext& _rContext )
244cdf0e10cSrcweir     :OSubComponent(m_aMutex,_xConnection)
245cdf0e10cSrcweir     ,OPropertyContainer(m_aBHelper)
246cdf0e10cSrcweir     ,m_aSqlParser( _rContext.getLegacyServiceFactory() )
247cdf0e10cSrcweir     ,m_aSqlIterator( _xConnection, _rxTables, m_aSqlParser, NULL )
248cdf0e10cSrcweir     ,m_aAdditiveIterator( _xConnection, _rxTables, m_aSqlParser, NULL )
249cdf0e10cSrcweir     ,m_aElementaryParts( (size_t)SQLPartCount )
250cdf0e10cSrcweir     ,m_xConnection(_xConnection)
251cdf0e10cSrcweir     ,m_xMetaData(_xConnection->getMetaData())
252cdf0e10cSrcweir     ,m_xConnectionTables( _rxTables )
253cdf0e10cSrcweir     ,m_aContext( _rContext )
254cdf0e10cSrcweir     ,m_pTables(NULL)
255cdf0e10cSrcweir     ,m_nBoolCompareMode( BooleanComparisonMode::EQUAL_INTEGER )
256cdf0e10cSrcweir     ,m_nCommandType(CommandType::COMMAND)
257cdf0e10cSrcweir {
258cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::OSingleSelectQueryComposer" );
259cdf0e10cSrcweir 	DBG_CTOR(OSingleSelectQueryComposer,NULL);
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     if ( !m_aContext.is() || !m_xConnection.is() || !m_xConnectionTables.is() )
262cdf0e10cSrcweir         throw IllegalArgumentException();
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     registerProperty(PROPERTY_ORIGINAL,PROPERTY_ID_ORIGINAL,PropertyAttribute::BOUND|PropertyAttribute::READONLY,&m_sOrignal,::getCppuType(&m_sOrignal));
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	m_aCurrentColumns.resize(4);
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 	m_aLocale = SvtSysLocale().GetLocaleData().getLocale();
269cdf0e10cSrcweir 	m_xNumberFormatsSupplier = dbtools::getNumberFormats( m_xConnection, sal_True, m_aContext.getLegacyServiceFactory() );
270cdf0e10cSrcweir     Reference< XLocaleData > xLocaleData;
271cdf0e10cSrcweir     m_aContext.createComponent( "com.sun.star.i18n.LocaleData", xLocaleData );
272cdf0e10cSrcweir 	LocaleDataItem aData = xLocaleData->getLocaleItem(m_aLocale);
273cdf0e10cSrcweir 	m_sDecimalSep = aData.decimalSeparator;
274cdf0e10cSrcweir 	OSL_ENSURE(m_sDecimalSep.getLength() == 1,"OSingleSelectQueryComposer::OSingleSelectQueryComposer decimal separator is not 1 length");
275cdf0e10cSrcweir 	try
276cdf0e10cSrcweir 	{
277cdf0e10cSrcweir         Any aValue;
278cdf0e10cSrcweir         Reference<XInterface> xDs = dbaccess::getDataSource(_xConnection);
279cdf0e10cSrcweir         if ( dbtools::getDataSourceSetting(xDs,static_cast <rtl::OUString> (PROPERTY_BOOLEANCOMPARISONMODE),aValue) )
280cdf0e10cSrcweir         {
281cdf0e10cSrcweir             OSL_VERIFY( aValue >>= m_nBoolCompareMode );
282cdf0e10cSrcweir         }
283cdf0e10cSrcweir         Reference< XQueriesSupplier >  xQueriesAccess(m_xConnection, UNO_QUERY);
284cdf0e10cSrcweir 		if (xQueriesAccess.is())
285cdf0e10cSrcweir 			m_xConnectionQueries = xQueriesAccess->getQueries();
286cdf0e10cSrcweir 	}
287cdf0e10cSrcweir 	catch(Exception&)
288cdf0e10cSrcweir 	{
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir }
291cdf0e10cSrcweir // -------------------------------------------------------------------------
~OSingleSelectQueryComposer()292cdf0e10cSrcweir OSingleSelectQueryComposer::~OSingleSelectQueryComposer()
293cdf0e10cSrcweir {
294cdf0e10cSrcweir 	DBG_DTOR(OSingleSelectQueryComposer,NULL);
295cdf0e10cSrcweir 	::std::vector<OPrivateColumns*>::iterator aColIter = m_aColumnsCollection.begin();
296cdf0e10cSrcweir 	::std::vector<OPrivateColumns*>::iterator aEnd = m_aColumnsCollection.end();
297cdf0e10cSrcweir 	for(;aColIter != aEnd;++aColIter)
298cdf0e10cSrcweir 		delete *aColIter;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	::std::vector<OPrivateTables*>::iterator aTabIter = m_aTablesCollection.begin();
301cdf0e10cSrcweir     ::std::vector<OPrivateTables*>::iterator aTabEnd = m_aTablesCollection.end();
302cdf0e10cSrcweir 	for(;aTabIter != aTabEnd;++aTabIter)
303cdf0e10cSrcweir 		delete *aTabIter;
304cdf0e10cSrcweir }
305cdf0e10cSrcweir // -------------------------------------------------------------------------
306cdf0e10cSrcweir // OComponentHelper
disposing(void)307cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::disposing(void)
308cdf0e10cSrcweir {
309cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::disposing" );
310cdf0e10cSrcweir 	OSubComponent::disposing();
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     resetIterator( m_aSqlIterator, true );
315cdf0e10cSrcweir     resetIterator( m_aAdditiveIterator, true );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 	m_xConnectionTables	= NULL;
318cdf0e10cSrcweir 	m_xConnection		= NULL;
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 	clearCurrentCollections();
321cdf0e10cSrcweir }
IMPLEMENT_FORWARD_XINTERFACE3(OSingleSelectQueryComposer,OSubComponent,OSingleSelectQueryComposer_BASE,OPropertyContainer)322cdf0e10cSrcweir IMPLEMENT_FORWARD_XINTERFACE3(OSingleSelectQueryComposer,OSubComponent,OSingleSelectQueryComposer_BASE,OPropertyContainer)
323cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO1(OSingleSelectQueryComposer,"org.openoffice.comp.dba.OSingleSelectQueryComposer",SERVICE_NAME_SINGLESELECTQUERYCOMPOSER)
324cdf0e10cSrcweir IMPLEMENT_TYPEPROVIDER3(OSingleSelectQueryComposer,OSubComponent,OSingleSelectQueryComposer_BASE,OPropertyContainer)
325cdf0e10cSrcweir IMPLEMENT_PROPERTYCONTAINER_DEFAULTS(OSingleSelectQueryComposer)
326cdf0e10cSrcweir 
327cdf0e10cSrcweir // -------------------------------------------------------------------------
328cdf0e10cSrcweir // com::sun::star::lang::XUnoTunnel
329cdf0e10cSrcweir sal_Int64 SAL_CALL OSingleSelectQueryComposer::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
330cdf0e10cSrcweir {
331cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getSomething" );
332cdf0e10cSrcweir 	if (rId.getLength() == 16 && 0 == rtl_compareMemory(getImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
333cdf0e10cSrcweir 		return reinterpret_cast<sal_Int64>(this);
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 	return sal_Int64(0);
336cdf0e10cSrcweir }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir // -------------------------------------------------------------------------
339cdf0e10cSrcweir // XSingleSelectQueryAnalyzer
getQuery()340cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getQuery(  ) throw(RuntimeException)
341cdf0e10cSrcweir {
342cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getQuery" );
343cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
344cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 	TGetParseNode F_tmp(&OSQLParseTreeIterator::getParseTree);
347cdf0e10cSrcweir 	return getStatementPart(F_tmp,m_aSqlIterator);
348cdf0e10cSrcweir }
349cdf0e10cSrcweir 
350cdf0e10cSrcweir // -------------------------------------------------------------------------
setQuery(const::rtl::OUString & command)351cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setQuery( const ::rtl::OUString& command ) throw(SQLException, RuntimeException)
352cdf0e10cSrcweir {
353cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "frank.schoenheit@sun.com", "OSingleSelectQueryComposer::setQuery" );
354cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
357cdf0e10cSrcweir     m_nCommandType = CommandType::COMMAND;
358cdf0e10cSrcweir 	// first clear the tables and columns
359cdf0e10cSrcweir 	clearCurrentCollections();
360cdf0e10cSrcweir 	// now set the new one
361cdf0e10cSrcweir 	setQuery_Impl(command);
362cdf0e10cSrcweir     m_sOrignal = command;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir     // reset the additive iterator to the same statement
365cdf0e10cSrcweir     parseAndCheck_throwError( m_aSqlParser, m_sOrignal, m_aAdditiveIterator, *this );
366cdf0e10cSrcweir 
367cdf0e10cSrcweir     // we have no "elementary" parts anymore (means filter/groupby/having/order clauses)
368cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
369cdf0e10cSrcweir         m_aElementaryParts[ eLoopParts ] = ::rtl::OUString();
370cdf0e10cSrcweir }
371cdf0e10cSrcweir // -------------------------------------------------------------------------
setCommand(const::rtl::OUString & Command,sal_Int32 _nCommandType)372cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setCommand( const ::rtl::OUString& Command,sal_Int32 _nCommandType ) throw(SQLException, RuntimeException)
373cdf0e10cSrcweir {
374cdf0e10cSrcweir     ::rtl::OUStringBuffer sSQL;
375cdf0e10cSrcweir     switch(_nCommandType)
376cdf0e10cSrcweir     {
377cdf0e10cSrcweir         case CommandType::COMMAND:
378cdf0e10cSrcweir             setElementaryQuery(Command);
379cdf0e10cSrcweir             return;
380cdf0e10cSrcweir         case CommandType::TABLE:
381cdf0e10cSrcweir             if ( m_xConnectionTables->hasByName(Command) )
382cdf0e10cSrcweir             {
383cdf0e10cSrcweir                 sSQL.appendAscii("SELECT * FROM ");
384cdf0e10cSrcweir                 Reference< XPropertySet > xTable;
385cdf0e10cSrcweir 			    try
386cdf0e10cSrcweir 			    {
387cdf0e10cSrcweir                     m_xConnectionTables->getByName( Command ) >>= xTable;
388cdf0e10cSrcweir 			    }
389cdf0e10cSrcweir 			    catch(const WrappedTargetException& e)
390cdf0e10cSrcweir 			    {
391cdf0e10cSrcweir 				    SQLException e2;
392cdf0e10cSrcweir 				    if ( e.TargetException >>= e2 )
393cdf0e10cSrcweir 					    throw e2;
394cdf0e10cSrcweir 			    }
395cdf0e10cSrcweir 			    catch(Exception&)
396cdf0e10cSrcweir 			    {
397cdf0e10cSrcweir                     DBG_UNHANDLED_EXCEPTION();
398cdf0e10cSrcweir 			    }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir                 sSQL.append(dbtools::composeTableNameForSelect(m_xConnection,xTable));
401cdf0e10cSrcweir             }
402cdf0e10cSrcweir             else
403cdf0e10cSrcweir             {
404cdf0e10cSrcweir                 String sMessage( DBACORE_RESSTRING( RID_STR_TABLE_DOES_NOT_EXIST ) );
405cdf0e10cSrcweir                 sMessage.SearchAndReplaceAscii( "$table$", Command );
406cdf0e10cSrcweir                 throwGenericSQLException(sMessage,*this);
407cdf0e10cSrcweir 		    }
408cdf0e10cSrcweir             break;
409cdf0e10cSrcweir         case CommandType::QUERY:
410cdf0e10cSrcweir             if ( m_xConnectionQueries->hasByName(Command) )
411cdf0e10cSrcweir             {
412cdf0e10cSrcweir 
413cdf0e10cSrcweir                 Reference<XPropertySet> xQuery(m_xConnectionQueries->getByName(Command),UNO_QUERY);
414cdf0e10cSrcweir                 ::rtl::OUString sCommand;
415cdf0e10cSrcweir                 xQuery->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
416cdf0e10cSrcweir                 sSQL.append(sCommand);
417cdf0e10cSrcweir             }
418cdf0e10cSrcweir             else
419cdf0e10cSrcweir             {
420cdf0e10cSrcweir                 String sMessage( DBACORE_RESSTRING( RID_STR_QUERY_DOES_NOT_EXIST ) );
421cdf0e10cSrcweir                 sMessage.SearchAndReplaceAscii( "$table$", Command );
422cdf0e10cSrcweir                 throwGenericSQLException(sMessage,*this);
423cdf0e10cSrcweir             }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir             break;
426cdf0e10cSrcweir         default:
427cdf0e10cSrcweir             break;
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
432cdf0e10cSrcweir     m_nCommandType = _nCommandType;
433cdf0e10cSrcweir     m_sCommand = Command;
434cdf0e10cSrcweir 	// first clear the tables and columns
435cdf0e10cSrcweir 	clearCurrentCollections();
436cdf0e10cSrcweir 	// now set the new one
437cdf0e10cSrcweir     ::rtl::OUString sCommand = sSQL.makeStringAndClear();
438cdf0e10cSrcweir 	setElementaryQuery(sCommand);
439cdf0e10cSrcweir     m_sOrignal = sCommand;
440cdf0e10cSrcweir /*
441cdf0e10cSrcweir     // reset the additive iterator to the same statement
442cdf0e10cSrcweir     parseAndCheck_throwError( m_aSqlParser, m_sOrignal, m_aAdditiveIterator, *this );
443cdf0e10cSrcweir 
444cdf0e10cSrcweir     // we have no "elementary" parts anymore (means filter/groupby/having/order clauses)
445cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
446cdf0e10cSrcweir         m_aElementaryParts[ eLoopParts ] = ::rtl::OUString();
447cdf0e10cSrcweir */
448cdf0e10cSrcweir }
449cdf0e10cSrcweir // -----------------------------------------------------------------------------
setQuery_Impl(const::rtl::OUString & command)450cdf0e10cSrcweir void OSingleSelectQueryComposer::setQuery_Impl( const ::rtl::OUString& command )
451cdf0e10cSrcweir {
452cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "frank.schoenheit@sun.com", "OSingleSelectQueryComposer::setQuery_Impl" );
453cdf0e10cSrcweir     // parse this
454cdf0e10cSrcweir     parseAndCheck_throwError( m_aSqlParser, command, m_aSqlIterator, *this );
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     // strip it from all clauses, to have the pure SELECT statement
457cdf0e10cSrcweir     m_aPureSelectSQL = getPureSelectStatement( m_aSqlIterator.getParseTree(), m_xConnection );
458cdf0e10cSrcweir 
459cdf0e10cSrcweir     // update columns and tables
460cdf0e10cSrcweir     // why? Shouldn't this be done on request only?
461cdf0e10cSrcweir 	// otherwise nothing is working anymore :-)
462cdf0e10cSrcweir //	getColumns();
463cdf0e10cSrcweir 	getTables();
464cdf0e10cSrcweir }
465cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStructuredHavingClause()466cdf0e10cSrcweir Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredHavingClause(  ) throw (RuntimeException)
467cdf0e10cSrcweir {
468cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getStructuredHavingClause" );
469cdf0e10cSrcweir 	TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleHavingTree);
470cdf0e10cSrcweir 	return getStructuredCondition(F_tmp);
471cdf0e10cSrcweir }
472cdf0e10cSrcweir // -------------------------------------------------------------------------
getStructuredFilter()473cdf0e10cSrcweir Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredFilter(  ) throw(RuntimeException)
474cdf0e10cSrcweir {
475cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getStructuredFilter" );
476cdf0e10cSrcweir 	TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleWhereTree);
477cdf0e10cSrcweir 	return getStructuredCondition(F_tmp);
478cdf0e10cSrcweir }
479cdf0e10cSrcweir // -----------------------------------------------------------------------------
appendHavingClauseByColumn(const Reference<XPropertySet> & column,sal_Bool andCriteria,sal_Int32 filterOperator)480cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::appendHavingClauseByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator ) throw (SQLException, RuntimeException)
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::appendHavingClauseByColumn" );
483cdf0e10cSrcweir 	::std::mem_fun1_t<bool,OSingleSelectQueryComposer,::rtl::OUString> F_tmp(&OSingleSelectQueryComposer::implSetHavingClause);
484cdf0e10cSrcweir 	setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
485cdf0e10cSrcweir }
486cdf0e10cSrcweir // -----------------------------------------------------------------------------
appendFilterByColumn(const Reference<XPropertySet> & column,sal_Bool andCriteria,sal_Int32 filterOperator)487cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator ) throw(SQLException, RuntimeException)
488cdf0e10cSrcweir {
489cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::appendFilterByColumn" );
490cdf0e10cSrcweir 	::std::mem_fun1_t<bool,OSingleSelectQueryComposer,::rtl::OUString> F_tmp(&OSingleSelectQueryComposer::implSetFilter);
491cdf0e10cSrcweir 	setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
492cdf0e10cSrcweir }
493cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_getColumnName_throw(const Reference<XPropertySet> & column)494cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::impl_getColumnName_throw(const Reference< XPropertySet >& column)
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
497cdf0e10cSrcweir 
498cdf0e10cSrcweir     getColumns();
499cdf0e10cSrcweir 	if ( !column.is()
500cdf0e10cSrcweir 		|| !m_aCurrentColumns[SelectColumns]
501cdf0e10cSrcweir 		|| !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
502cdf0e10cSrcweir 		)
503cdf0e10cSrcweir 		{
504cdf0e10cSrcweir 			String sError(DBACORE_RESSTRING(RID_STR_COLUMN_UNKNOWN_PROP));
505cdf0e10cSrcweir 			sError.SearchAndReplaceAscii("%value", ::rtl::OUString(PROPERTY_NAME));
506cdf0e10cSrcweir 			SQLException aErr(sError,*this,SQLSTATE_GENERAL,1000,Any() );
507cdf0e10cSrcweir 			throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,makeAny(aErr) );
508cdf0e10cSrcweir 		}
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 	::rtl::OUString aName,aNewName;
511cdf0e10cSrcweir 	column->getPropertyValue(PROPERTY_NAME)			>>= aName;
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 	if ( !m_xMetaData->supportsOrderByUnrelated() && m_aCurrentColumns[SelectColumns] && !m_aCurrentColumns[SelectColumns]->hasByName(aName))
514cdf0e10cSrcweir 	{
515cdf0e10cSrcweir 		String sError(DBACORE_RESSTRING(RID_STR_COLUMN_MUST_VISIBLE));
516cdf0e10cSrcweir 		sError.SearchAndReplaceAscii("%name", aName);
517cdf0e10cSrcweir 		throw SQLException(sError,*this,SQLSTATE_GENERAL,1000,Any() );
518cdf0e10cSrcweir 	}
519cdf0e10cSrcweir 
520cdf0e10cSrcweir 	// filter anhaengen
521cdf0e10cSrcweir 	// select ohne where und order by aufbauen
522cdf0e10cSrcweir 	::rtl::OUString aQuote	= m_xMetaData->getIdentifierQuoteString();
523cdf0e10cSrcweir 	if ( m_aCurrentColumns[SelectColumns]->hasByName(aName) )
524cdf0e10cSrcweir 	{
525cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
526cdf0e10cSrcweir 		m_aCurrentColumns[SelectColumns]->getByName(aName) >>= xColumn;
527cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
528cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
529cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Function"))),"Property FUNCTION not available!");
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 		::rtl::OUString sRealName,sTableName;
532cdf0e10cSrcweir 		xColumn->getPropertyValue(PROPERTY_REALNAME)	>>= sRealName;
533cdf0e10cSrcweir 		xColumn->getPropertyValue(PROPERTY_TABLENAME)	>>= sTableName;
534cdf0e10cSrcweir 		sal_Bool bFunction = sal_False;
535cdf0e10cSrcweir 		xColumn->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Function")))	>>= bFunction;
536cdf0e10cSrcweir 		if ( sRealName == aName )
537cdf0e10cSrcweir 		{
538cdf0e10cSrcweir 			if ( bFunction )
539cdf0e10cSrcweir 				aNewName = aName;
540cdf0e10cSrcweir 			else
541cdf0e10cSrcweir 			{
542cdf0e10cSrcweir 				if(sTableName.indexOf('.',0) != -1)
543cdf0e10cSrcweir 				{
544cdf0e10cSrcweir 					::rtl::OUString aCatlog,aSchema,aTable;
545cdf0e10cSrcweir 					::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatlog,aSchema,aTable,::dbtools::eInDataManipulation);
546cdf0e10cSrcweir 					sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, sal_True, ::dbtools::eInDataManipulation );
547cdf0e10cSrcweir 				}
548cdf0e10cSrcweir 				else
549cdf0e10cSrcweir 					sTableName = ::dbtools::quoteName(aQuote,sTableName);
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 				aNewName =  sTableName;
552cdf0e10cSrcweir 				aNewName += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("."));
553cdf0e10cSrcweir 				aNewName += ::dbtools::quoteName(aQuote,sRealName);
554cdf0e10cSrcweir 			}
555cdf0e10cSrcweir 		}
556cdf0e10cSrcweir 		else
557cdf0e10cSrcweir 			aNewName = ::dbtools::quoteName(aQuote,aName);
558cdf0e10cSrcweir 	}
559cdf0e10cSrcweir 	else
560cdf0e10cSrcweir 		aNewName = getTableAlias(column) + ::dbtools::quoteName(aQuote,aName);
561cdf0e10cSrcweir     return aNewName;
562cdf0e10cSrcweir }
563cdf0e10cSrcweir // -------------------------------------------------------------------------
appendOrderByColumn(const Reference<XPropertySet> & column,sal_Bool ascending)564cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException)
565cdf0e10cSrcweir {
566cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::appendOrderByColumn" );
567cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
568cdf0e10cSrcweir     ::rtl::OUString sColumnName( impl_getColumnName_throw(column) );
569cdf0e10cSrcweir 	::rtl::OUString sOrder = getOrder();
570cdf0e10cSrcweir 	if ( (sOrder.getLength() != 0) && sColumnName.getLength() )
571cdf0e10cSrcweir 		sOrder += COMMA;
572cdf0e10cSrcweir 	sOrder += sColumnName;
573cdf0e10cSrcweir 	if ( !ascending && sColumnName.getLength() )
574cdf0e10cSrcweir 		sOrder += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DESC "));
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 	setOrder(sOrder);
577cdf0e10cSrcweir }
578cdf0e10cSrcweir 
579cdf0e10cSrcweir // -------------------------------------------------------------------------
appendGroupByColumn(const Reference<XPropertySet> & column)580cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::appendGroupByColumn( const Reference< XPropertySet >& column) throw(SQLException, RuntimeException)
581cdf0e10cSrcweir {
582cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::appendGroupByColumn" );
583cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
584cdf0e10cSrcweir     ::rtl::OUString sColumnName( impl_getColumnName_throw(column) );
585cdf0e10cSrcweir     OrderCreator aComposer;
586cdf0e10cSrcweir     aComposer.append( getGroup() );
587cdf0e10cSrcweir     aComposer.append( sColumnName );
588cdf0e10cSrcweir     setGroup( aComposer.getComposedAndClear() );
589cdf0e10cSrcweir }
590cdf0e10cSrcweir // -------------------------------------------------------------------------
composeStatementFromParts(const::std::vector<::rtl::OUString> & _rParts)591cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::composeStatementFromParts( const ::std::vector< ::rtl::OUString >& _rParts )
592cdf0e10cSrcweir {
593cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::composeStatementFromParts" );
594cdf0e10cSrcweir     DBG_ASSERT( _rParts.size() == (size_t)SQLPartCount, "OSingleSelectQueryComposer::composeStatementFromParts: invalid parts array!" );
595cdf0e10cSrcweir 
596cdf0e10cSrcweir     ::rtl::OUStringBuffer aSql( m_aPureSelectSQL );
597cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
598cdf0e10cSrcweir         if ( _rParts[ eLoopParts ].getLength() )
599cdf0e10cSrcweir         {
600cdf0e10cSrcweir             aSql.append( getKeyword( eLoopParts ) );
601cdf0e10cSrcweir             aSql.append( _rParts[ eLoopParts ] );
602cdf0e10cSrcweir         }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     return aSql.makeStringAndClear();
605cdf0e10cSrcweir }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir // -------------------------------------------------------------------------
getElementaryQuery()608cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getElementaryQuery() throw (::com::sun::star::uno::RuntimeException)
609cdf0e10cSrcweir {
610cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getElementaryQuery" );
611cdf0e10cSrcweir     return composeStatementFromParts( m_aElementaryParts );
612cdf0e10cSrcweir }
613cdf0e10cSrcweir 
614cdf0e10cSrcweir // -------------------------------------------------------------------------
setElementaryQuery(const::rtl::OUString & _rElementary)615cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setElementaryQuery( const ::rtl::OUString& _rElementary ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
616cdf0e10cSrcweir {
617cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "frank.schoenheit@sun.com", "OSingleSelectQueryComposer::setElementaryQuery" );
618cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
619cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
620cdf0e10cSrcweir 
621cdf0e10cSrcweir     // remember the 4 current "additive" clauses
622cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aAdditiveClauses( SQLPartCount );
623cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
624cdf0e10cSrcweir         aAdditiveClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, sal_False );
625cdf0e10cSrcweir 
626cdf0e10cSrcweir     // clear the tables and columns
627cdf0e10cSrcweir     clearCurrentCollections();
628cdf0e10cSrcweir     // set and parse the new query
629cdf0e10cSrcweir     setQuery_Impl( _rElementary );
630cdf0e10cSrcweir 
631cdf0e10cSrcweir     // get the 4 elementary parts of the statement
632cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
633cdf0e10cSrcweir         m_aElementaryParts[ eLoopParts ] = getSQLPart( eLoopParts, m_aSqlIterator, sal_False );
634cdf0e10cSrcweir 
635cdf0e10cSrcweir     // reset the the AdditiveIterator: m_aPureSelectSQL may have changed
636cdf0e10cSrcweir     try
637cdf0e10cSrcweir     {
638cdf0e10cSrcweir         parseAndCheck_throwError( m_aSqlParser, composeStatementFromParts( aAdditiveClauses ), m_aAdditiveIterator, *this );
639cdf0e10cSrcweir     }
640cdf0e10cSrcweir     catch( const Exception& e )
641cdf0e10cSrcweir     {
642cdf0e10cSrcweir         (void)e;
643cdf0e10cSrcweir         DBG_ERROR( "OSingleSelectQueryComposer::setElementaryQuery: there should be no error anymore for the additive statement!" );
644cdf0e10cSrcweir         // every part of the additive statement should have passed other tests already, and should not
645cdf0e10cSrcweir         // be able to cause any errors ... me thinks
646cdf0e10cSrcweir     }
647cdf0e10cSrcweir }
648cdf0e10cSrcweir 
649cdf0e10cSrcweir // -------------------------------------------------------------------------
650cdf0e10cSrcweir namespace
651cdf0e10cSrcweir {
getComposedClause(const::rtl::OUString _rElementaryClause,const::rtl::OUString _rAdditionalClause,TokenComposer & _rComposer,const::rtl::OUString _rKeyword)652cdf0e10cSrcweir     ::rtl::OUString getComposedClause( const ::rtl::OUString _rElementaryClause, const ::rtl::OUString _rAdditionalClause,
653cdf0e10cSrcweir         TokenComposer& _rComposer, const ::rtl::OUString _rKeyword )
654cdf0e10cSrcweir     {
655cdf0e10cSrcweir         _rComposer.clear();
656cdf0e10cSrcweir         _rComposer.append( _rElementaryClause );
657cdf0e10cSrcweir         _rComposer.append( _rAdditionalClause );
658cdf0e10cSrcweir         ::rtl::OUString sComposed = _rComposer.getComposedAndClear();
659cdf0e10cSrcweir         if ( sComposed.getLength() )
660cdf0e10cSrcweir             sComposed = _rKeyword + sComposed;
661cdf0e10cSrcweir         return sComposed;
662cdf0e10cSrcweir     }
663cdf0e10cSrcweir }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir // -------------------------------------------------------------------------
setSingleAdditiveClause(SQLPart _ePart,const::rtl::OUString & _rClause)666cdf0e10cSrcweir void OSingleSelectQueryComposer::setSingleAdditiveClause( SQLPart _ePart, const ::rtl::OUString& _rClause )
667cdf0e10cSrcweir {
668cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setSingleAdditiveClause" );
669cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
670cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
671cdf0e10cSrcweir 
672cdf0e10cSrcweir     // if nothing is changed, do nothing
673cdf0e10cSrcweir     if ( getSQLPart( _ePart, m_aAdditiveIterator, sal_False ) == _rClause )
674cdf0e10cSrcweir         return;
675cdf0e10cSrcweir 
676cdf0e10cSrcweir     // collect the 4 single parts as they're currently set
677cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aClauses;
678cdf0e10cSrcweir     aClauses.reserve( (size_t)SQLPartCount );
679cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
680cdf0e10cSrcweir         aClauses.push_back( getSQLPart( eLoopParts, m_aSqlIterator, sal_True ) );
681cdf0e10cSrcweir 
682cdf0e10cSrcweir     // overwrite the one part in question here
683cdf0e10cSrcweir     ::std::auto_ptr< TokenComposer > pComposer;
684cdf0e10cSrcweir     if ( ( _ePart == Where ) || ( _ePart == Having ) )
685cdf0e10cSrcweir         pComposer.reset( new FilterCreator );
686cdf0e10cSrcweir     else
687cdf0e10cSrcweir         pComposer.reset( new OrderCreator );
688cdf0e10cSrcweir     aClauses[ _ePart ] = getComposedClause( m_aElementaryParts[ _ePart ], _rClause,
689cdf0e10cSrcweir         *pComposer, getKeyword( _ePart ) );
690cdf0e10cSrcweir 
691cdf0e10cSrcweir     // construct the complete statement
692cdf0e10cSrcweir     ::rtl::OUStringBuffer aSql(m_aPureSelectSQL);
693cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
694cdf0e10cSrcweir         aSql.append(aClauses[ eLoopParts ]);
695cdf0e10cSrcweir 
696cdf0e10cSrcweir     // set the query
697cdf0e10cSrcweir 	setQuery_Impl(aSql.makeStringAndClear());
698cdf0e10cSrcweir 
699cdf0e10cSrcweir     // clear column collections which (might) have changed
700cdf0e10cSrcweir     clearColumns( ParameterColumns );
701cdf0e10cSrcweir     if ( _ePart == Order )
702cdf0e10cSrcweir         clearColumns( OrderColumns );
703cdf0e10cSrcweir     if ( _ePart == Group )
704cdf0e10cSrcweir         clearColumns( GroupByColumns );
705cdf0e10cSrcweir 
706cdf0e10cSrcweir     // also, since the "additive filter" change, we need to rebuild our "additive" statement
707cdf0e10cSrcweir     aSql = m_aPureSelectSQL;
708cdf0e10cSrcweir     // again, first get all the old additive parts
709cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
710cdf0e10cSrcweir         aClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, sal_True );
711cdf0e10cSrcweir     // then overwrite the one in question
712cdf0e10cSrcweir     aClauses[ _ePart ] = getComposedClause( ::rtl::OUString(), _rClause, *pComposer, getKeyword( _ePart ) );
713cdf0e10cSrcweir     // and parse it, so that m_aAdditiveIterator is up to date
714cdf0e10cSrcweir     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
715cdf0e10cSrcweir         aSql.append(aClauses[ eLoopParts ]);
716cdf0e10cSrcweir     try
717cdf0e10cSrcweir     {
718cdf0e10cSrcweir         parseAndCheck_throwError( m_aSqlParser, aSql.makeStringAndClear(), m_aAdditiveIterator, *this );
719cdf0e10cSrcweir     }
720cdf0e10cSrcweir     catch( const Exception& e )
721cdf0e10cSrcweir     {
722cdf0e10cSrcweir         (void)e;
723cdf0e10cSrcweir         DBG_ERROR( "OSingleSelectQueryComposer::setSingleAdditiveClause: there should be no error anymore for the additive statement!" );
724cdf0e10cSrcweir         // every part of the additive statement should have passed other tests already, and should not
725cdf0e10cSrcweir         // be able to cause any errors ... me thinks
726cdf0e10cSrcweir     }
727cdf0e10cSrcweir }
728cdf0e10cSrcweir 
729cdf0e10cSrcweir // -------------------------------------------------------------------------
setFilter(const::rtl::OUString & filter)730cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setFilter( const ::rtl::OUString& filter ) throw(SQLException, RuntimeException)
731cdf0e10cSrcweir {
732cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setFilter" );
733cdf0e10cSrcweir     setSingleAdditiveClause( Where, filter );
734cdf0e10cSrcweir }
735cdf0e10cSrcweir 
736cdf0e10cSrcweir // -------------------------------------------------------------------------
setOrder(const::rtl::OUString & order)737cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setOrder( const ::rtl::OUString& order ) throw(SQLException, RuntimeException)
738cdf0e10cSrcweir {
739cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setOrder" );
740cdf0e10cSrcweir     setSingleAdditiveClause( Order, order );
741cdf0e10cSrcweir }
742cdf0e10cSrcweir // -----------------------------------------------------------------------------
setGroup(const::rtl::OUString & group)743cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setGroup( const ::rtl::OUString& group ) throw (SQLException, RuntimeException)
744cdf0e10cSrcweir {
745cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setGroup" );
746cdf0e10cSrcweir     setSingleAdditiveClause( Group, group );
747cdf0e10cSrcweir }
748cdf0e10cSrcweir // -------------------------------------------------------------------------
setHavingClause(const::rtl::OUString & filter)749cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setHavingClause( const ::rtl::OUString& filter ) throw(SQLException, RuntimeException)
750cdf0e10cSrcweir {
751cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setHavingClause" );
752cdf0e10cSrcweir     setSingleAdditiveClause( Having, filter );
753cdf0e10cSrcweir }
754cdf0e10cSrcweir 
755cdf0e10cSrcweir // -------------------------------------------------------------------------
756cdf0e10cSrcweir // XTablesSupplier
getTables()757cdf0e10cSrcweir Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getTables(  ) throw(RuntimeException)
758cdf0e10cSrcweir {
759cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getTables" );
760cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
761cdf0e10cSrcweir 
762cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
763cdf0e10cSrcweir 	if ( !m_pTables )
764cdf0e10cSrcweir 	{
765cdf0e10cSrcweir 		const OSQLTables& aTables = m_aSqlIterator.getTables();
766cdf0e10cSrcweir 		::std::vector< ::rtl::OUString> aNames;
767cdf0e10cSrcweir         OSQLTables::const_iterator aEnd = aTables.end();
768cdf0e10cSrcweir 		for(OSQLTables::const_iterator aIter = aTables.begin(); aIter != aEnd;++aIter)
769cdf0e10cSrcweir 			aNames.push_back(aIter->first);
770cdf0e10cSrcweir 
771cdf0e10cSrcweir 		m_pTables = new OPrivateTables(aTables,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames);
772cdf0e10cSrcweir 	}
773cdf0e10cSrcweir 
774cdf0e10cSrcweir 	return m_pTables;
775cdf0e10cSrcweir }
776cdf0e10cSrcweir // -------------------------------------------------------------------------
777cdf0e10cSrcweir // XColumnsSupplier
getColumns()778cdf0e10cSrcweir Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getColumns(  ) throw(RuntimeException)
779cdf0e10cSrcweir {
780cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getColumns" );
781cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
782cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
783cdf0e10cSrcweir 	if ( !!m_aCurrentColumns[SelectColumns] )
784cdf0e10cSrcweir         return m_aCurrentColumns[SelectColumns];
785cdf0e10cSrcweir 
786cdf0e10cSrcweir     ::std::vector< ::rtl::OUString> aNames;
787cdf0e10cSrcweir 	::vos::ORef< OSQLColumns> aSelectColumns;
788cdf0e10cSrcweir 	sal_Bool bCase = sal_True;
789cdf0e10cSrcweir     Reference< XNameAccess>	xQueryColumns;
790cdf0e10cSrcweir     if ( m_nCommandType == CommandType::QUERY )
791cdf0e10cSrcweir     {
792cdf0e10cSrcweir         Reference<XColumnsSupplier> xSup(m_xConnectionQueries->getByName(m_sCommand),UNO_QUERY);
793cdf0e10cSrcweir 	    if(xSup.is())
794cdf0e10cSrcweir 		    xQueryColumns = xSup->getColumns();
795cdf0e10cSrcweir     }
796cdf0e10cSrcweir 
797cdf0e10cSrcweir     do {
798cdf0e10cSrcweir 
799cdf0e10cSrcweir 	try
800cdf0e10cSrcweir 	{
801cdf0e10cSrcweir         SharedUNOComponent< XStatement, DisposableComponent > xStatement;
802cdf0e10cSrcweir         SharedUNOComponent< XPreparedStatement, DisposableComponent > xPreparedStatement;
803cdf0e10cSrcweir 
804cdf0e10cSrcweir         bCase = m_xMetaData->supportsMixedCaseQuotedIdentifiers();
805cdf0e10cSrcweir 		aSelectColumns = m_aSqlIterator.getSelectColumns();
806cdf0e10cSrcweir 
807cdf0e10cSrcweir         ::rtl::OUStringBuffer aSQL;
808cdf0e10cSrcweir         aSQL.append( m_aPureSelectSQL );
809cdf0e10cSrcweir         aSQL.append( STR_WHERE );
810cdf0e10cSrcweir 
811cdf0e10cSrcweir         // preserve the original WHERE clause
812cdf0e10cSrcweir         // #i102234# / 2009-06-02 / frank.schoenheit@sun.com
813cdf0e10cSrcweir         ::rtl::OUString sOriginalWhereClause = getSQLPart( Where, m_aSqlIterator, sal_False );
814cdf0e10cSrcweir 		if ( sOriginalWhereClause.getLength() )
815cdf0e10cSrcweir         {
816cdf0e10cSrcweir             aSQL.appendAscii( " ( 0 = 1 ) AND ( " );
817cdf0e10cSrcweir             aSQL.append( sOriginalWhereClause );
818cdf0e10cSrcweir             aSQL.appendAscii( " ) " );
819cdf0e10cSrcweir         }
820cdf0e10cSrcweir         else
821cdf0e10cSrcweir         {
822cdf0e10cSrcweir             aSQL.appendAscii( " ( 0 = 1 ) " );
823cdf0e10cSrcweir         }
824cdf0e10cSrcweir 
825cdf0e10cSrcweir         ::rtl::OUString sGroupBy = getSQLPart( Group, m_aSqlIterator, sal_True );
826cdf0e10cSrcweir 		if ( sGroupBy.getLength() )
827cdf0e10cSrcweir 			aSQL.append( sGroupBy );
828cdf0e10cSrcweir 
829cdf0e10cSrcweir         ::rtl::OUString sSQL( aSQL.makeStringAndClear() );
830cdf0e10cSrcweir         // normalize the statement so that it doesn't contain any application-level features anymore
831cdf0e10cSrcweir         ::rtl::OUString sError;
832cdf0e10cSrcweir         const ::std::auto_ptr< OSQLParseNode > pStatementTree( m_aSqlParser.parseTree( sError, sSQL, false ) );
833cdf0e10cSrcweir         DBG_ASSERT( pStatementTree.get(), "OSingleSelectQueryComposer::getColumns: could not parse the column retrieval statement!" );
834cdf0e10cSrcweir         if ( pStatementTree.get() )
835cdf0e10cSrcweir             if ( !pStatementTree->parseNodeToExecutableStatement( sSQL, m_xConnection, m_aSqlParser, NULL ) )
836cdf0e10cSrcweir                 break;
837cdf0e10cSrcweir 
838cdf0e10cSrcweir 		Reference< XResultSetMetaData > xResultSetMeta;
839cdf0e10cSrcweir         Reference< XResultSetMetaDataSupplier > xResMetaDataSup;
840cdf0e10cSrcweir         try
841cdf0e10cSrcweir         {
842cdf0e10cSrcweir             xPreparedStatement.set( m_xConnection->prepareStatement( sSQL ), UNO_QUERY_THROW );
843cdf0e10cSrcweir 		    xResMetaDataSup.set( xPreparedStatement, UNO_QUERY_THROW );
844cdf0e10cSrcweir             xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
845cdf0e10cSrcweir         }
846cdf0e10cSrcweir         catch( const Exception& ) { }
847cdf0e10cSrcweir 
848cdf0e10cSrcweir         try
849cdf0e10cSrcweir         {
850cdf0e10cSrcweir             if ( !xResultSetMeta.is() )
851cdf0e10cSrcweir             {
852cdf0e10cSrcweir                 xStatement.reset( Reference< XStatement >( m_xConnection->createStatement(), UNO_QUERY_THROW ) );
853cdf0e10cSrcweir                 Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
854cdf0e10cSrcweir                 try { xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, makeAny( sal_False ) ); }
855cdf0e10cSrcweir                 catch ( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
856cdf0e10cSrcweir 		        xResMetaDataSup.set( xStatement->executeQuery( sSQL ), UNO_QUERY_THROW );
857cdf0e10cSrcweir                 xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
858cdf0e10cSrcweir             }
859cdf0e10cSrcweir         }
860cdf0e10cSrcweir         catch( const Exception& )
861cdf0e10cSrcweir         {
862cdf0e10cSrcweir             //@see issue http://qa.openoffice.org/issues/show_bug.cgi?id=110111
863cdf0e10cSrcweir             // access returns a different order of column names when executing select * from
864cdf0e10cSrcweir             // and asking the columns from the metadata.
865cdf0e10cSrcweir             Reference< XParameters > xParameters( xPreparedStatement, UNO_QUERY_THROW );
866cdf0e10cSrcweir             Reference< XIndexAccess > xPara = getParameters();
867cdf0e10cSrcweir             for(sal_Int32 i = 1;i <= xPara->getCount();++i)
868cdf0e10cSrcweir                 xParameters->setNull(i,DataType::VARCHAR);
869cdf0e10cSrcweir             xResMetaDataSup.set(xPreparedStatement->executeQuery(), UNO_QUERY_THROW );
870cdf0e10cSrcweir             xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
871cdf0e10cSrcweir         }
872cdf0e10cSrcweir 
873cdf0e10cSrcweir         if ( aSelectColumns->get().empty() )
874cdf0e10cSrcweir         {
875cdf0e10cSrcweir             // This is a valid case. If we can syntactically parse the query, but not semantically
876cdf0e10cSrcweir             // (e.g. because it is based on a table we do not know), then there will be no SelectColumns
877cdf0e10cSrcweir             aSelectColumns = ::connectivity::parse::OParseColumn::createColumnsForResultSet( xResultSetMeta, m_xMetaData ,xQueryColumns);
878cdf0e10cSrcweir             break;
879cdf0e10cSrcweir         }
880cdf0e10cSrcweir 
881cdf0e10cSrcweir         const ::comphelper::UStringMixEqual aCaseCompare( bCase );
882cdf0e10cSrcweir 		const ::comphelper::TStringMixEqualFunctor aCaseCompareFunctor( bCase );
883cdf0e10cSrcweir         typedef ::std::set< size_t > SizeTSet;
884cdf0e10cSrcweir         SizeTSet aUsedSelectColumns;
885cdf0e10cSrcweir         ::connectivity::parse::OParseColumn::StringMap aColumnNames;
886cdf0e10cSrcweir 
887cdf0e10cSrcweir 		sal_Int32 nCount = xResultSetMeta->getColumnCount();
888cdf0e10cSrcweir         OSL_ENSURE( (size_t) nCount == aSelectColumns->get().size(), "OSingleSelectQueryComposer::getColumns: inconsistent column counts, this might result in wrong columns!" );
889cdf0e10cSrcweir 		for(sal_Int32 i=1;i<=nCount;++i)
890cdf0e10cSrcweir 		{
891cdf0e10cSrcweir             ::rtl::OUString sColumnName = xResultSetMeta->getColumnName(i);
892cdf0e10cSrcweir             ::rtl::OUString sColumnLabel;
893cdf0e10cSrcweir             if ( xQueryColumns.is() && xQueryColumns->hasByName(sColumnName) )
894cdf0e10cSrcweir             {
895cdf0e10cSrcweir                 Reference<XPropertySet> xQueryColumn(xQueryColumns->getByName(sColumnName),UNO_QUERY_THROW);
896cdf0e10cSrcweir                 xQueryColumn->getPropertyValue(PROPERTY_LABEL) >>= sColumnLabel;
897cdf0e10cSrcweir             }
898cdf0e10cSrcweir             else
899cdf0e10cSrcweir 			    sColumnLabel = xResultSetMeta->getColumnLabel(i);
900cdf0e10cSrcweir             sal_Bool bFound = sal_False;
901cdf0e10cSrcweir 			OSQLColumns::Vector::const_iterator aFind = ::connectivity::find(aSelectColumns->get().begin(),aSelectColumns->get().end(),sColumnLabel,aCaseCompare);
902cdf0e10cSrcweir             size_t nFoundSelectColumnPos = aFind - aSelectColumns->get().begin();
903cdf0e10cSrcweir 			if ( aFind != aSelectColumns->get().end() )
904cdf0e10cSrcweir 			{
905cdf0e10cSrcweir                 if ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
906cdf0e10cSrcweir 				{	// we found a column name which exists twice
907cdf0e10cSrcweir 					// so we start after the first found
908cdf0e10cSrcweir 					do
909cdf0e10cSrcweir 					{
910cdf0e10cSrcweir 						aFind = ::connectivity::findRealName(++aFind,aSelectColumns->get().end(),sColumnName,aCaseCompare);
911cdf0e10cSrcweir                         nFoundSelectColumnPos = aFind - aSelectColumns->get().begin();
912cdf0e10cSrcweir 					}
913cdf0e10cSrcweir 					while   (   ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
914cdf0e10cSrcweir                                 &&  ( aFind != aSelectColumns->get().end() )
915cdf0e10cSrcweir                             );
916cdf0e10cSrcweir 				}
917cdf0e10cSrcweir 				if ( aFind != aSelectColumns->get().end() )
918cdf0e10cSrcweir 				{
919cdf0e10cSrcweir 					(*aFind)->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
920cdf0e10cSrcweir 					aUsedSelectColumns.insert( nFoundSelectColumnPos );
921cdf0e10cSrcweir 					aNames.push_back(sColumnName);
922cdf0e10cSrcweir                     bFound = sal_True;
923cdf0e10cSrcweir 				}
924cdf0e10cSrcweir 			}
925cdf0e10cSrcweir 
926cdf0e10cSrcweir             if ( bFound )
927cdf0e10cSrcweir                 continue;
928cdf0e10cSrcweir 
929cdf0e10cSrcweir 			OSQLColumns::Vector::const_iterator aRealFind = ::connectivity::findRealName(
930cdf0e10cSrcweir                 aSelectColumns->get().begin(), aSelectColumns->get().end(), sColumnName, aCaseCompare );
931cdf0e10cSrcweir 
932cdf0e10cSrcweir             if ( i > static_cast< sal_Int32>( aSelectColumns->get().size() ) )
933cdf0e10cSrcweir             {
934cdf0e10cSrcweir                 aSelectColumns->get().push_back(
935cdf0e10cSrcweir                     ::connectivity::parse::OParseColumn::createColumnForResultSet( xResultSetMeta, m_xMetaData, i ,aColumnNames)
936cdf0e10cSrcweir                 );
937cdf0e10cSrcweir                 OSL_ENSURE( aSelectColumns->get().size() == (size_t)i, "OSingleSelectQueryComposer::getColumns: inconsistency!" );
938cdf0e10cSrcweir             }
939cdf0e10cSrcweir             else if ( aRealFind == aSelectColumns->get().end() )
940cdf0e10cSrcweir 			{
941cdf0e10cSrcweir                 // we can now only look if we found it under the realname propertery
942cdf0e10cSrcweir                 // here we have to make the assumption that the position is correct
943cdf0e10cSrcweir 				OSQLColumns::Vector::iterator aFind2 = aSelectColumns->get().begin() + i-1;
944cdf0e10cSrcweir 				Reference<XPropertySet> xProp(*aFind2,UNO_QUERY);
945cdf0e10cSrcweir 				if ( !xProp.is() || !xProp->getPropertySetInfo()->hasPropertyByName( PROPERTY_REALNAME ) )
946cdf0e10cSrcweir                     continue;
947cdf0e10cSrcweir 
948cdf0e10cSrcweir 				::connectivity::parse::OParseColumn* pColumn = new ::connectivity::parse::OParseColumn(xProp,bCase);
949cdf0e10cSrcweir                 pColumn->setFunction(::comphelper::getBOOL(xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Function")))));
950cdf0e10cSrcweir 				pColumn->setAggregateFunction(::comphelper::getBOOL(xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AggregateFunction")))));
951cdf0e10cSrcweir 
952cdf0e10cSrcweir                 ::rtl::OUString sRealName;
953cdf0e10cSrcweir                 xProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
954cdf0e10cSrcweir                 ::std::vector< ::rtl::OUString>::iterator aFindName;
955cdf0e10cSrcweir 				if ( !sColumnName.getLength() )
956cdf0e10cSrcweir 					xProp->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 
959cdf0e10cSrcweir                 aFindName = ::std::find_if(aNames.begin(),aNames.end(),::std::bind2nd(aCaseCompareFunctor,sColumnName));
960cdf0e10cSrcweir                 sal_Int32 j = 0;
961cdf0e10cSrcweir                 while ( aFindName != aNames.end() )
962cdf0e10cSrcweir                 {
963cdf0e10cSrcweir                     sColumnName += ::rtl::OUString::valueOf(++j);
964cdf0e10cSrcweir                     aFindName = ::std::find_if(aNames.begin(),aNames.end(),::std::bind2nd(aCaseCompareFunctor,sColumnName));
965cdf0e10cSrcweir                 }
966cdf0e10cSrcweir 
967cdf0e10cSrcweir                 pColumn->setName(sColumnName);
968cdf0e10cSrcweir 				pColumn->setRealName(sRealName);
969cdf0e10cSrcweir 				pColumn->setTableName(::comphelper::getString(xProp->getPropertyValue(PROPERTY_TABLENAME)));
970cdf0e10cSrcweir 
971cdf0e10cSrcweir 				(aSelectColumns->get())[i-1] = pColumn;
972cdf0e10cSrcweir 			}
973cdf0e10cSrcweir             else
974cdf0e10cSrcweir                 continue;
975cdf0e10cSrcweir 
976cdf0e10cSrcweir             aUsedSelectColumns.insert( (size_t)(i - 1) );
977cdf0e10cSrcweir             aNames.push_back( sColumnName );
978cdf0e10cSrcweir 		}
979cdf0e10cSrcweir 	}
980cdf0e10cSrcweir 	catch(const Exception&)
981cdf0e10cSrcweir 	{
982cdf0e10cSrcweir 	}
983cdf0e10cSrcweir 
984cdf0e10cSrcweir     } while ( false );
985cdf0e10cSrcweir 
986cdf0e10cSrcweir     if ( aNames.empty() )
987cdf0e10cSrcweir         m_aCurrentColumns[ SelectColumns ] = OPrivateColumns::createWithIntrinsicNames( aSelectColumns, bCase, *this, m_aMutex );
988cdf0e10cSrcweir     else
989cdf0e10cSrcweir         m_aCurrentColumns[ SelectColumns ] = new OPrivateColumns( aSelectColumns, bCase, *this, m_aMutex, aNames );
990cdf0e10cSrcweir 
991cdf0e10cSrcweir 	return m_aCurrentColumns[SelectColumns];
992cdf0e10cSrcweir }
993cdf0e10cSrcweir // -------------------------------------------------------------------------
setORCriteria(OSQLParseNode * pCondition,OSQLParseTreeIterator & _rIterator,::std::vector<::std::vector<PropertyValue>> & rFilters,const Reference<::com::sun::star::util::XNumberFormatter> & xFormatter) const994cdf0e10cSrcweir sal_Bool OSingleSelectQueryComposer::setORCriteria(OSQLParseNode* pCondition, OSQLParseTreeIterator& _rIterator,
995cdf0e10cSrcweir 									::std::vector< ::std::vector < PropertyValue > >& rFilters, const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const
996cdf0e10cSrcweir {
997cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setORCriteria" );
998cdf0e10cSrcweir 	// Runde Klammern um den Ausdruck
999cdf0e10cSrcweir 	if (pCondition->count() == 3 &&
1000cdf0e10cSrcweir 		SQL_ISPUNCTUATION(pCondition->getChild(0),"(") &&
1001cdf0e10cSrcweir 		SQL_ISPUNCTUATION(pCondition->getChild(2),")"))
1002cdf0e10cSrcweir 	{
1003cdf0e10cSrcweir 		return setORCriteria(pCondition->getChild(1), _rIterator, rFilters, xFormatter);
1004cdf0e10cSrcweir 	}
1005cdf0e10cSrcweir 	// oder Verknuepfung
1006cdf0e10cSrcweir 	// a searchcondition can only look like this: search_condition SQL_TOKEN_OR boolean_term
1007cdf0e10cSrcweir 	else if (SQL_ISRULE(pCondition,search_condition))
1008cdf0e10cSrcweir 	{
1009cdf0e10cSrcweir 		sal_Bool bResult = sal_True;
1010cdf0e10cSrcweir 		for (int i = 0; bResult && i < 3; i+=2)
1011cdf0e10cSrcweir 		{
1012cdf0e10cSrcweir 			// Ist das erste Element wieder eine OR-Verknuepfung?
1013cdf0e10cSrcweir 			// Dann rekursiv absteigen ...
1014cdf0e10cSrcweir 			//if (!i && SQL_ISRULE(pCondition->getChild(i),search_condition))
1015cdf0e10cSrcweir 			if (SQL_ISRULE(pCondition->getChild(i),search_condition))
1016cdf0e10cSrcweir 				bResult = setORCriteria(pCondition->getChild(i), _rIterator, rFilters, xFormatter);
1017cdf0e10cSrcweir 			else
1018cdf0e10cSrcweir 			{
1019cdf0e10cSrcweir 				rFilters.push_back( ::std::vector < PropertyValue >());
1020cdf0e10cSrcweir 				bResult = setANDCriteria(pCondition->getChild(i), _rIterator, rFilters[rFilters.size() - 1], xFormatter);
1021cdf0e10cSrcweir 			}
1022cdf0e10cSrcweir 		}
1023cdf0e10cSrcweir 		return bResult;
1024cdf0e10cSrcweir 	}
1025cdf0e10cSrcweir 	else
1026cdf0e10cSrcweir 	{
1027cdf0e10cSrcweir 		rFilters.push_back(::std::vector < PropertyValue >());
1028cdf0e10cSrcweir 		return setANDCriteria(pCondition, _rIterator, rFilters[rFilters.size() - 1], xFormatter);
1029cdf0e10cSrcweir 	}
1030cdf0e10cSrcweir }
1031cdf0e10cSrcweir 
1032cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
setANDCriteria(OSQLParseNode * pCondition,OSQLParseTreeIterator & _rIterator,::std::vector<PropertyValue> & rFilter,const Reference<XNumberFormatter> & xFormatter) const1033cdf0e10cSrcweir sal_Bool OSingleSelectQueryComposer::setANDCriteria( OSQLParseNode * pCondition,
1034cdf0e10cSrcweir     OSQLParseTreeIterator& _rIterator, ::std::vector < PropertyValue >& rFilter, const Reference< XNumberFormatter > & xFormatter) const
1035cdf0e10cSrcweir {
1036cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setANDCriteria" );
1037cdf0e10cSrcweir 	// Runde Klammern
1038cdf0e10cSrcweir 	if (SQL_ISRULE(pCondition,boolean_primary))
1039cdf0e10cSrcweir 	{
1040cdf0e10cSrcweir 		// this should not occur
1041cdf0e10cSrcweir 		DBG_ERROR("boolean_primary in And-Criteria");
1042cdf0e10cSrcweir 		return sal_False;
1043cdf0e10cSrcweir 	}
1044cdf0e10cSrcweir 	// Das erste Element ist (wieder) eine AND-Verknuepfung
1045cdf0e10cSrcweir 	else if ( SQL_ISRULE(pCondition,boolean_term) && pCondition->count() == 3 )
1046cdf0e10cSrcweir 	{
1047cdf0e10cSrcweir 		return setANDCriteria(pCondition->getChild(0), _rIterator, rFilter, xFormatter) &&
1048cdf0e10cSrcweir 			   setANDCriteria(pCondition->getChild(2), _rIterator, rFilter, xFormatter);
1049cdf0e10cSrcweir 	}
1050cdf0e10cSrcweir 	else if (SQL_ISRULE(pCondition, comparison_predicate))
1051cdf0e10cSrcweir 	{
1052cdf0e10cSrcweir 		return setComparsionPredicate(pCondition,_rIterator,rFilter,xFormatter);
1053cdf0e10cSrcweir 	}
1054cdf0e10cSrcweir 	else if (SQL_ISRULE(pCondition,like_predicate) ||
1055cdf0e10cSrcweir 			 SQL_ISRULE(pCondition,test_for_null) ||
1056cdf0e10cSrcweir 			 SQL_ISRULE(pCondition,in_predicate) ||
1057cdf0e10cSrcweir 			 SQL_ISRULE(pCondition,all_or_any_predicate) ||
1058cdf0e10cSrcweir 			 SQL_ISRULE(pCondition,between_predicate))
1059cdf0e10cSrcweir 	{
1060cdf0e10cSrcweir 		if (SQL_ISRULE(pCondition->getChild(0), column_ref))
1061cdf0e10cSrcweir 		{
1062cdf0e10cSrcweir 			PropertyValue aItem;
1063cdf0e10cSrcweir 			::rtl::OUString aValue;
1064cdf0e10cSrcweir 			::rtl::OUString aColumnName;
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir 
1067cdf0e10cSrcweir 			//	pCondition->parseNodeToStr(aValue,m_xMetaData, xFormatter, m_aLocale,static_cast<sal_Char>(m_sDecimalSep.toChar()));
1068cdf0e10cSrcweir 			pCondition->parseNodeToStr( aValue, m_xConnection, NULL );
1069cdf0e10cSrcweir 			//	pCondition->getChild(0)->parseNodeToStr(aColumnName,m_xMetaData, xFormatter, m_aLocale,static_cast<sal_Char>(m_sDecimalSep.toChar()));
1070cdf0e10cSrcweir 			pCondition->getChild(0)->parseNodeToStr( aColumnName, m_xConnection, NULL );
1071cdf0e10cSrcweir 
1072cdf0e10cSrcweir 			// don't display the column name
1073cdf0e10cSrcweir 			aValue = aValue.copy(aColumnName.getLength());
1074cdf0e10cSrcweir 			aValue = aValue.trim();
1075cdf0e10cSrcweir 
1076cdf0e10cSrcweir 			aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
1077cdf0e10cSrcweir 			aItem.Value <<= aValue;
1078cdf0e10cSrcweir 			aItem.Handle = 0; // just to know that this is not one the known ones
1079cdf0e10cSrcweir 			if ( SQL_ISRULE(pCondition,like_predicate) )
1080cdf0e10cSrcweir 			{
1081cdf0e10cSrcweir                 if ( SQL_ISTOKEN(pCondition->getChild(1)->getChild(0),NOT) )
1082cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::NOT_LIKE;
1083cdf0e10cSrcweir 				else
1084cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::LIKE;
1085cdf0e10cSrcweir 			}
1086cdf0e10cSrcweir 			else if (SQL_ISRULE(pCondition,test_for_null))
1087cdf0e10cSrcweir 			{
1088cdf0e10cSrcweir 				if (SQL_ISTOKEN(pCondition->getChild(1)->getChild(1),NOT) )
1089cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::NOT_SQLNULL;
1090cdf0e10cSrcweir 				else
1091cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::SQLNULL;
1092cdf0e10cSrcweir 			}
1093cdf0e10cSrcweir 			else if (SQL_ISRULE(pCondition,in_predicate))
1094cdf0e10cSrcweir 			{
1095cdf0e10cSrcweir                 OSL_ENSURE( false, "OSingleSelectQueryComposer::setANDCriteria: in_predicate not implemented!" );
1096cdf0e10cSrcweir 			}
1097cdf0e10cSrcweir 			else if (SQL_ISRULE(pCondition,all_or_any_predicate))
1098cdf0e10cSrcweir 			{
1099cdf0e10cSrcweir                 OSL_ENSURE( false, "OSingleSelectQueryComposer::setANDCriteria: all_or_any_predicate not implemented!" );
1100cdf0e10cSrcweir 			}
1101cdf0e10cSrcweir 			else if (SQL_ISRULE(pCondition,between_predicate))
1102cdf0e10cSrcweir 			{
1103cdf0e10cSrcweir                 OSL_ENSURE( false, "OSingleSelectQueryComposer::setANDCriteria: between_predicate not implemented!" );
1104cdf0e10cSrcweir 			}
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir 			rFilter.push_back(aItem);
1107cdf0e10cSrcweir 		}
1108cdf0e10cSrcweir 		else
1109cdf0e10cSrcweir 			return sal_False;
1110cdf0e10cSrcweir 	}
1111cdf0e10cSrcweir 	else if (SQL_ISRULE(pCondition,existence_test) ||
1112cdf0e10cSrcweir 			 SQL_ISRULE(pCondition,unique_test))
1113cdf0e10cSrcweir 	{
1114cdf0e10cSrcweir 		// this couldn't be handled here, too complex
1115cdf0e10cSrcweir 		// as we need a field name
1116cdf0e10cSrcweir 		return sal_False;
1117cdf0e10cSrcweir 	}
1118cdf0e10cSrcweir 	else
1119cdf0e10cSrcweir 		return sal_False;
1120cdf0e10cSrcweir 
1121cdf0e10cSrcweir 	return sal_True;
1122cdf0e10cSrcweir }
1123cdf0e10cSrcweir // -----------------------------------------------------------------------------
getPredicateType(OSQLParseNode * _pPredicate) const1124cdf0e10cSrcweir sal_Int32 OSingleSelectQueryComposer::getPredicateType(OSQLParseNode * _pPredicate) const
1125cdf0e10cSrcweir {
1126cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getPredicateType" );
1127cdf0e10cSrcweir 	sal_Int32 nPredicate = SQLFilterOperator::EQUAL;
1128cdf0e10cSrcweir 	switch (_pPredicate->getNodeType())
1129cdf0e10cSrcweir 	{
1130cdf0e10cSrcweir 		case SQL_NODE_EQUAL:
1131cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::EQUAL;
1132cdf0e10cSrcweir 			break;
1133cdf0e10cSrcweir 		case SQL_NODE_NOTEQUAL:
1134cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::NOT_EQUAL;
1135cdf0e10cSrcweir 			break;
1136cdf0e10cSrcweir 		case SQL_NODE_LESS:
1137cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::LESS;
1138cdf0e10cSrcweir 			break;
1139cdf0e10cSrcweir 		case SQL_NODE_LESSEQ:
1140cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::LESS_EQUAL;
1141cdf0e10cSrcweir 			break;
1142cdf0e10cSrcweir 		case SQL_NODE_GREAT:
1143cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::GREATER;
1144cdf0e10cSrcweir 			break;
1145cdf0e10cSrcweir 		case SQL_NODE_GREATEQ:
1146cdf0e10cSrcweir 			nPredicate = SQLFilterOperator::GREATER_EQUAL;
1147cdf0e10cSrcweir 			break;
1148cdf0e10cSrcweir 		default:
1149cdf0e10cSrcweir 			OSL_ENSURE(0,"Wrong NodeType!");
1150cdf0e10cSrcweir 	}
1151cdf0e10cSrcweir 	return nPredicate;
1152cdf0e10cSrcweir }
1153cdf0e10cSrcweir //------------------------------------------------------------------------------
setComparsionPredicate(OSQLParseNode * pCondition,OSQLParseTreeIterator & _rIterator,::std::vector<PropertyValue> & rFilter,const Reference<::com::sun::star::util::XNumberFormatter> & xFormatter) const1154cdf0e10cSrcweir sal_Bool OSingleSelectQueryComposer::setComparsionPredicate(OSQLParseNode * pCondition, OSQLParseTreeIterator& _rIterator,
1155cdf0e10cSrcweir 										    ::std::vector < PropertyValue >& rFilter, const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const
1156cdf0e10cSrcweir {
1157cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setComparsionPredicate" );
1158cdf0e10cSrcweir 	DBG_ASSERT(SQL_ISRULE(pCondition, comparison_predicate),"setComparsionPredicate: pCondition ist kein ComparsionPredicate");
1159cdf0e10cSrcweir 	if (SQL_ISRULE(pCondition->getChild(0), column_ref) ||
1160cdf0e10cSrcweir 		SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
1161cdf0e10cSrcweir 	{
1162cdf0e10cSrcweir 		PropertyValue aItem;
1163cdf0e10cSrcweir 		::rtl::OUString aValue;
1164cdf0e10cSrcweir 		sal_uInt32 nPos;
1165cdf0e10cSrcweir 		if (SQL_ISRULE(pCondition->getChild(0), column_ref))
1166cdf0e10cSrcweir 		{
1167cdf0e10cSrcweir 			nPos = 0;
1168cdf0e10cSrcweir 			sal_uInt32 i=1;
1169cdf0e10cSrcweir 
1170cdf0e10cSrcweir 			aItem.Handle = getPredicateType(pCondition->getChild(i));
1171cdf0e10cSrcweir 			// don't display the equal
1172cdf0e10cSrcweir 			if (pCondition->getChild(i)->getNodeType() == SQL_NODE_EQUAL)
1173cdf0e10cSrcweir 				i++;
1174cdf0e10cSrcweir 
1175cdf0e10cSrcweir 			// go forward
1176cdf0e10cSrcweir 			for (;i < pCondition->count();i++)
1177cdf0e10cSrcweir 				pCondition->getChild(i)->parseNodeToPredicateStr(
1178cdf0e10cSrcweir                     aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>(m_sDecimalSep.toChar() ) );
1179cdf0e10cSrcweir 		}
1180cdf0e10cSrcweir 		else if (SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
1181cdf0e10cSrcweir 		{
1182cdf0e10cSrcweir 			nPos = pCondition->count()-1;
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir 			sal_Int32 i = pCondition->count() - 2;
1185cdf0e10cSrcweir 			switch (pCondition->getChild(i)->getNodeType())
1186cdf0e10cSrcweir 			{
1187cdf0e10cSrcweir 				case SQL_NODE_EQUAL:
1188cdf0e10cSrcweir 					// don't display the equal
1189cdf0e10cSrcweir 					i--;
1190cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::EQUAL;
1191cdf0e10cSrcweir 					break;
1192cdf0e10cSrcweir 				case SQL_NODE_NOTEQUAL:
1193cdf0e10cSrcweir 					i--;
1194cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::NOT_EQUAL;
1195cdf0e10cSrcweir 					break;
1196cdf0e10cSrcweir 				case SQL_NODE_LESS:
1197cdf0e10cSrcweir 					// take the opposite as we change the order
1198cdf0e10cSrcweir 					i--;
1199cdf0e10cSrcweir 					aValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(">="));
1200cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::GREATER_EQUAL;
1201cdf0e10cSrcweir 					break;
1202cdf0e10cSrcweir 				case SQL_NODE_LESSEQ:
1203cdf0e10cSrcweir 					// take the opposite as we change the order
1204cdf0e10cSrcweir 					i--;
1205cdf0e10cSrcweir 					aValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(">"));
1206cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::GREATER;
1207cdf0e10cSrcweir 					break;
1208cdf0e10cSrcweir 				case SQL_NODE_GREAT:
1209cdf0e10cSrcweir 					// take the opposite as we change the order
1210cdf0e10cSrcweir 					i--;
1211cdf0e10cSrcweir 					aValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("<="));
1212cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::LESS_EQUAL;
1213cdf0e10cSrcweir 					break;
1214cdf0e10cSrcweir 				case SQL_NODE_GREATEQ:
1215cdf0e10cSrcweir 					// take the opposite as we change the order
1216cdf0e10cSrcweir 					i--;
1217cdf0e10cSrcweir 					aValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("<"));
1218cdf0e10cSrcweir 					aItem.Handle = SQLFilterOperator::LESS;
1219cdf0e10cSrcweir 					break;
1220cdf0e10cSrcweir                 default:
1221cdf0e10cSrcweir                     break;
1222cdf0e10cSrcweir 			}
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir 			// go backward
1225cdf0e10cSrcweir 			for (; i >= 0; i--)
1226cdf0e10cSrcweir 				pCondition->getChild(i)->parseNodeToPredicateStr(
1227cdf0e10cSrcweir                     aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
1228cdf0e10cSrcweir 		}
1229cdf0e10cSrcweir 		else
1230cdf0e10cSrcweir 			return sal_False;
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 		aItem.Name = getColumnName(pCondition->getChild(nPos),_rIterator);
1233cdf0e10cSrcweir 		aItem.Value <<= aValue;
1234cdf0e10cSrcweir 		rFilter.push_back(aItem);
1235cdf0e10cSrcweir 	}
1236cdf0e10cSrcweir 	else if (SQL_ISRULE(pCondition->getChild(0), set_fct_spec ) ||
1237cdf0e10cSrcweir 			 SQL_ISRULE(pCondition->getChild(0), general_set_fct))
1238cdf0e10cSrcweir 	{
1239cdf0e10cSrcweir 		PropertyValue aItem;
1240cdf0e10cSrcweir 		::rtl::OUString aValue;
1241cdf0e10cSrcweir 		::rtl::OUString aColumnName;
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir 		pCondition->getChild(2)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
1244cdf0e10cSrcweir 		pCondition->getChild(0)->parseNodeToPredicateStr( aColumnName, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep .toChar() ) );
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir 		aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
1247cdf0e10cSrcweir 		aItem.Value <<= aValue;
1248cdf0e10cSrcweir 		aItem.Handle = getPredicateType(pCondition->getChild(1));
1249cdf0e10cSrcweir 		rFilter.push_back(aItem);
1250cdf0e10cSrcweir 	}
1251cdf0e10cSrcweir 	else // kann sich nur um einen Expr. Ausdruck handeln
1252cdf0e10cSrcweir 	{
1253cdf0e10cSrcweir 		PropertyValue aItem;
1254cdf0e10cSrcweir 		::rtl::OUString aName, aValue;
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir 		OSQLParseNode *pLhs = pCondition->getChild(0);
1257cdf0e10cSrcweir 		OSQLParseNode *pRhs = pCondition->getChild(2);
1258cdf0e10cSrcweir 
1259cdf0e10cSrcweir 		// Feldnamen
1260cdf0e10cSrcweir 		sal_uInt16 i;
1261cdf0e10cSrcweir 		for (i=0;i< pLhs->count();i++)
1262cdf0e10cSrcweir 			 pLhs->getChild(i)->parseNodeToPredicateStr( aName, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir 		// Kriterium
1265cdf0e10cSrcweir 		aItem.Handle = getPredicateType(pCondition->getChild(1));
1266cdf0e10cSrcweir 		aValue		 = pCondition->getChild(1)->getTokenValue();
1267cdf0e10cSrcweir 		for(i=0;i< pRhs->count();i++)
1268cdf0e10cSrcweir 			pRhs->getChild(i)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
1269cdf0e10cSrcweir 
1270cdf0e10cSrcweir 		aItem.Name = aName;
1271cdf0e10cSrcweir 		aItem.Value <<= aValue;
1272cdf0e10cSrcweir 		rFilter.push_back(aItem);
1273cdf0e10cSrcweir 	}
1274cdf0e10cSrcweir 	return sal_True;
1275cdf0e10cSrcweir }
1276cdf0e10cSrcweir // functions for analysing SQL
1277cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
getColumnName(::connectivity::OSQLParseNode * pColumnRef,OSQLParseTreeIterator & _rIterator) const1278cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getColumnName( ::connectivity::OSQLParseNode* pColumnRef, OSQLParseTreeIterator& _rIterator ) const
1279cdf0e10cSrcweir {
1280cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getColumnName" );
1281cdf0e10cSrcweir 	::rtl::OUString aTableRange, aColumnName;
1282cdf0e10cSrcweir 	_rIterator.getColumnRange(pColumnRef,aColumnName,aTableRange);
1283cdf0e10cSrcweir 	return aColumnName;
1284cdf0e10cSrcweir }
1285cdf0e10cSrcweir //------------------------------------------------------------------------------
getFilter()1286cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getFilter(  ) throw(RuntimeException)
1287cdf0e10cSrcweir {
1288cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getFilter" );
1289cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1290cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1291cdf0e10cSrcweir 	return getSQLPart(Where,m_aAdditiveIterator,sal_False);
1292cdf0e10cSrcweir }
1293cdf0e10cSrcweir // -------------------------------------------------------------------------
getOrder()1294cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getOrder(  ) throw(RuntimeException)
1295cdf0e10cSrcweir {
1296cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getOrder" );
1297cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1298cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1299cdf0e10cSrcweir 	return getSQLPart(Order,m_aAdditiveIterator,sal_False);
1300cdf0e10cSrcweir }
1301cdf0e10cSrcweir // -------------------------------------------------------------------------
getGroup()1302cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getGroup(  ) throw (RuntimeException)
1303cdf0e10cSrcweir {
1304cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getGroup" );
1305cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1306cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1307cdf0e10cSrcweir 	return getSQLPart(Group,m_aAdditiveIterator,sal_False);
1308cdf0e10cSrcweir }
1309cdf0e10cSrcweir // -----------------------------------------------------------------------------
getHavingClause()1310cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getHavingClause() throw (RuntimeException)
1311cdf0e10cSrcweir {
1312cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getHavingClause" );
1313cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1314cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1315cdf0e10cSrcweir 	return getSQLPart(Having,m_aAdditiveIterator,sal_False);
1316cdf0e10cSrcweir }
1317cdf0e10cSrcweir // -----------------------------------------------------------------------------
getTableAlias(const Reference<XPropertySet> & column) const1318cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getTableAlias(const Reference< XPropertySet >& column) const
1319cdf0e10cSrcweir {
1320cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getTableAlias" );
1321cdf0e10cSrcweir 	::rtl::OUString sReturn;
1322cdf0e10cSrcweir 	if(m_pTables && m_pTables->getCount() > 1)
1323cdf0e10cSrcweir 	{
1324cdf0e10cSrcweir 		::rtl::OUString aCatalog,aSchema,aTable,aComposedName,aColumnName;
1325cdf0e10cSrcweir 		column->getPropertyValue(PROPERTY_CATALOGNAME)	>>= aCatalog;
1326cdf0e10cSrcweir 		column->getPropertyValue(PROPERTY_SCHEMANAME)	>>= aSchema;
1327cdf0e10cSrcweir 		column->getPropertyValue(PROPERTY_TABLENAME)	>>= aTable;
1328cdf0e10cSrcweir 		column->getPropertyValue(PROPERTY_NAME)			>>= aColumnName;
1329cdf0e10cSrcweir 
1330cdf0e10cSrcweir 		Sequence< ::rtl::OUString> aNames(m_pTables->getElementNames());
1331cdf0e10cSrcweir 		const ::rtl::OUString* pBegin	= aNames.getConstArray();
1332cdf0e10cSrcweir 		const ::rtl::OUString* pEnd		= pBegin + aNames.getLength();
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir 		if(!aTable.getLength())
1335cdf0e10cSrcweir 		{ // we don't found a table name, now we must search every table for this column
1336cdf0e10cSrcweir 			for(;pBegin != pEnd;++pBegin)
1337cdf0e10cSrcweir 			{
1338cdf0e10cSrcweir 				Reference<XColumnsSupplier> xColumnsSupp;
1339cdf0e10cSrcweir 				m_pTables->getByName(*pBegin) >>= xColumnsSupp;
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir 				if(xColumnsSupp.is() && xColumnsSupp->getColumns()->hasByName(aColumnName))
1342cdf0e10cSrcweir 				{
1343cdf0e10cSrcweir //					Reference<XPropertySet> xTableProp(xColumnsSupp,UNO_QUERY);
1344cdf0e10cSrcweir //					xTableProp->getPropertyValue(PROPERTY_CATALOGNAME)	>>= aCatalog;
1345cdf0e10cSrcweir //					xTableProp->getPropertyValue(PROPERTY_SCHEMANAME)	>>= aSchema;
1346cdf0e10cSrcweir //					xTableProp->getPropertyValue(PROPERTY_NAME)			>>= aTable;
1347cdf0e10cSrcweir 					aTable = *pBegin;
1348cdf0e10cSrcweir 					break;
1349cdf0e10cSrcweir 				}
1350cdf0e10cSrcweir 			}
1351cdf0e10cSrcweir 		}
1352cdf0e10cSrcweir 		else
1353cdf0e10cSrcweir 		{
1354cdf0e10cSrcweir 			aComposedName = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, sal_False, ::dbtools::eInDataManipulation );
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir 			// first check if this is the table we want to or has it a tablealias
1357cdf0e10cSrcweir 
1358cdf0e10cSrcweir 			if(!m_pTables->hasByName(aComposedName))
1359cdf0e10cSrcweir 			{
1360cdf0e10cSrcweir                 ::comphelper::UStringMixLess aTmp(m_aAdditiveIterator.getTables().key_comp());
1361cdf0e10cSrcweir 				::comphelper::UStringMixEqual aComp(static_cast< ::comphelper::UStringMixLess*>(&aTmp)->isCaseSensitive());
1362cdf0e10cSrcweir 				for(;pBegin != pEnd;++pBegin)
1363cdf0e10cSrcweir 				{
1364cdf0e10cSrcweir 					Reference<XPropertySet> xTableProp;
1365cdf0e10cSrcweir 					m_pTables->getByName(*pBegin) >>= xTableProp;
1366cdf0e10cSrcweir 					OSL_ENSURE(xTableProp.is(),"Table isn't a propertyset!");
1367cdf0e10cSrcweir 					if(xTableProp.is())
1368cdf0e10cSrcweir 					{
1369cdf0e10cSrcweir 						::rtl::OUString aCatalog2,aSchema2,aTable2;
1370cdf0e10cSrcweir 						xTableProp->getPropertyValue(PROPERTY_CATALOGNAME)	>>= aCatalog2;
1371cdf0e10cSrcweir 						xTableProp->getPropertyValue(PROPERTY_SCHEMANAME)	>>= aSchema2;
1372cdf0e10cSrcweir 						xTableProp->getPropertyValue(PROPERTY_NAME)			>>= aTable2;
1373cdf0e10cSrcweir 						if(aComp(aCatalog,aCatalog2) && aComp(aSchema,aSchema2) && aComp(aTable,aTable2))
1374cdf0e10cSrcweir 						{
1375cdf0e10cSrcweir 							aCatalog	= aCatalog2;
1376cdf0e10cSrcweir 							aSchema		= aSchema2;
1377cdf0e10cSrcweir 							aTable		= aTable2;
1378cdf0e10cSrcweir 							break;
1379cdf0e10cSrcweir 						}
1380cdf0e10cSrcweir 					}
1381cdf0e10cSrcweir 				}
1382cdf0e10cSrcweir 			}
1383cdf0e10cSrcweir 		}
1384cdf0e10cSrcweir 		if(pBegin != pEnd)
1385cdf0e10cSrcweir 		{
1386cdf0e10cSrcweir 			sReturn = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, sal_True, ::dbtools::eInDataManipulation );
1387cdf0e10cSrcweir 			sReturn += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("."));
1388cdf0e10cSrcweir 		}
1389cdf0e10cSrcweir 	}
1390cdf0e10cSrcweir 	return sReturn;
1391cdf0e10cSrcweir }
1392cdf0e10cSrcweir // -----------------------------------------------------------------------------
getParameters()1393cdf0e10cSrcweir Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getParameters(  ) throw(RuntimeException)
1394cdf0e10cSrcweir {
1395cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getParameters" );
1396cdf0e10cSrcweir 	// now set the Parameters
1397cdf0e10cSrcweir 	if ( !m_aCurrentColumns[ParameterColumns] )
1398cdf0e10cSrcweir 	{
1399cdf0e10cSrcweir 		::vos::ORef< OSQLColumns> aCols = m_aSqlIterator.getParameters();
1400cdf0e10cSrcweir 		::std::vector< ::rtl::OUString> aNames;
1401cdf0e10cSrcweir         OSQLColumns::Vector::const_iterator aEnd = aCols->get().end();
1402cdf0e10cSrcweir 		for(OSQLColumns::Vector::const_iterator aIter = aCols->get().begin(); aIter != aEnd;++aIter)
1403cdf0e10cSrcweir 			aNames.push_back(getString((*aIter)->getPropertyValue(PROPERTY_NAME)));
1404cdf0e10cSrcweir 		m_aCurrentColumns[ParameterColumns] = new OPrivateColumns(aCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,sal_True);
1405cdf0e10cSrcweir 	}
1406cdf0e10cSrcweir 
1407cdf0e10cSrcweir 	return m_aCurrentColumns[ParameterColumns];
1408cdf0e10cSrcweir }
1409cdf0e10cSrcweir // -----------------------------------------------------------------------------
clearColumns(const EColumnType _eType)1410cdf0e10cSrcweir void OSingleSelectQueryComposer::clearColumns( const EColumnType _eType )
1411cdf0e10cSrcweir {
1412cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::clearColumns" );
1413cdf0e10cSrcweir     OPrivateColumns* pColumns = m_aCurrentColumns[ _eType ];
1414cdf0e10cSrcweir 	if ( pColumns != NULL )
1415cdf0e10cSrcweir 	{
1416cdf0e10cSrcweir 		pColumns->disposing();
1417cdf0e10cSrcweir 		m_aColumnsCollection.push_back( pColumns );
1418cdf0e10cSrcweir 		m_aCurrentColumns[ _eType ] = NULL;
1419cdf0e10cSrcweir 	}
1420cdf0e10cSrcweir }
1421cdf0e10cSrcweir // -----------------------------------------------------------------------------
clearCurrentCollections()1422cdf0e10cSrcweir void OSingleSelectQueryComposer::clearCurrentCollections()
1423cdf0e10cSrcweir {
1424cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::clearCurrentCollections" );
1425cdf0e10cSrcweir 	::std::vector<OPrivateColumns*>::iterator aIter = m_aCurrentColumns.begin();
1426cdf0e10cSrcweir 	::std::vector<OPrivateColumns*>::iterator aEnd = m_aCurrentColumns.end();
1427cdf0e10cSrcweir 	for (;aIter != aEnd;++aIter)
1428cdf0e10cSrcweir 	{
1429cdf0e10cSrcweir 		if ( *aIter )
1430cdf0e10cSrcweir 		{
1431cdf0e10cSrcweir 			(*aIter)->disposing();
1432cdf0e10cSrcweir 			m_aColumnsCollection.push_back(*aIter);
1433cdf0e10cSrcweir 			*aIter = NULL;
1434cdf0e10cSrcweir 		}
1435cdf0e10cSrcweir 	}
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 	if(m_pTables)
1438cdf0e10cSrcweir 	{
1439cdf0e10cSrcweir 		m_pTables->disposing();
1440cdf0e10cSrcweir 		m_aTablesCollection.push_back(m_pTables);
1441cdf0e10cSrcweir 		m_pTables = NULL;
1442cdf0e10cSrcweir 	}
1443cdf0e10cSrcweir }
1444cdf0e10cSrcweir // -----------------------------------------------------------------------------
setCurrentColumns(EColumnType _eType,const::vos::ORef<OSQLColumns> & _rCols)1445cdf0e10cSrcweir Reference< XIndexAccess > OSingleSelectQueryComposer::setCurrentColumns( EColumnType _eType,
1446cdf0e10cSrcweir     const ::vos::ORef< OSQLColumns >& _rCols )
1447cdf0e10cSrcweir {
1448cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setCurrentColumns" );
1449cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1452cdf0e10cSrcweir 	// now set the group columns
1453cdf0e10cSrcweir 	if ( !m_aCurrentColumns[_eType] )
1454cdf0e10cSrcweir 	{
1455cdf0e10cSrcweir 		::std::vector< ::rtl::OUString> aNames;
1456cdf0e10cSrcweir         OSQLColumns::Vector::const_iterator aEnd = _rCols->get().end();
1457cdf0e10cSrcweir 		for(OSQLColumns::Vector::const_iterator aIter = _rCols->get().begin(); aIter != aEnd;++aIter)
1458cdf0e10cSrcweir 			aNames.push_back(getString((*aIter)->getPropertyValue(PROPERTY_NAME)));
1459cdf0e10cSrcweir 		m_aCurrentColumns[_eType] = new OPrivateColumns(_rCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,sal_True);
1460cdf0e10cSrcweir 	}
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir 	return m_aCurrentColumns[_eType];
1463cdf0e10cSrcweir }
1464cdf0e10cSrcweir // -----------------------------------------------------------------------------
getGroupColumns()1465cdf0e10cSrcweir Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getGroupColumns(  ) throw(RuntimeException)
1466cdf0e10cSrcweir {
1467cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getGroupColumns" );
1468cdf0e10cSrcweir 	return setCurrentColumns( GroupByColumns, m_aAdditiveIterator.getGroupColumns() );
1469cdf0e10cSrcweir }
1470cdf0e10cSrcweir // -------------------------------------------------------------------------
getOrderColumns()1471cdf0e10cSrcweir Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getOrderColumns(  ) throw(RuntimeException)
1472cdf0e10cSrcweir {
1473cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getOrderColumns" );
1474cdf0e10cSrcweir 	return setCurrentColumns( OrderColumns, m_aAdditiveIterator.getOrderColumns() );
1475cdf0e10cSrcweir }
1476cdf0e10cSrcweir // -----------------------------------------------------------------------------
getQueryWithSubstitution()1477cdf0e10cSrcweir ::rtl::OUString SAL_CALL OSingleSelectQueryComposer::getQueryWithSubstitution(  ) throw (SQLException, RuntimeException)
1478cdf0e10cSrcweir {
1479cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getQueryWithSubstitution" );
1480cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1481cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1482cdf0e10cSrcweir 
1483cdf0e10cSrcweir     ::rtl::OUString sSqlStatement( getQuery() );
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir     const OSQLParseNode* pStatementNode = m_aSqlIterator.getParseTree();
1486cdf0e10cSrcweir     if ( pStatementNode )
1487cdf0e10cSrcweir     {
1488cdf0e10cSrcweir         SQLException aError;
1489cdf0e10cSrcweir         if ( !pStatementNode->parseNodeToExecutableStatement( sSqlStatement, m_xConnection, m_aSqlParser, &aError ) )
1490cdf0e10cSrcweir             throw SQLException( aError );
1491cdf0e10cSrcweir     }
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir     return sSqlStatement;
1494cdf0e10cSrcweir }
1495cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStatementPart(TGetParseNode & _aGetFunctor,OSQLParseTreeIterator & _rIterator)1496cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getStatementPart( TGetParseNode& _aGetFunctor, OSQLParseTreeIterator& _rIterator )
1497cdf0e10cSrcweir {
1498cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getStatementPart" );
1499cdf0e10cSrcweir 	::rtl::OUString sResult;
1500cdf0e10cSrcweir 
1501cdf0e10cSrcweir     const OSQLParseNode* pNode = _aGetFunctor( &_rIterator );
1502cdf0e10cSrcweir 	if ( pNode )
1503cdf0e10cSrcweir 		pNode->parseNodeToStr( sResult, m_xConnection );
1504cdf0e10cSrcweir 
1505cdf0e10cSrcweir 	return sResult;
1506cdf0e10cSrcweir }
1507cdf0e10cSrcweir // -----------------------------------------------------------------------------
1508cdf0e10cSrcweir namespace
1509cdf0e10cSrcweir {
lcl_getDecomposedColumnName(const::rtl::OUString & rComposedName,const::rtl::OUString & rQuoteString)1510*0d4d7139SKay Schenk     ::rtl::OUString lcl_getDecomposedColumnName( const ::rtl::OUString& rComposedName, const ::rtl::OUString& rQuoteString )
1511*0d4d7139SKay Schenk     {
1512*0d4d7139SKay Schenk         const sal_Int32 nQuoteLength = rQuoteString.getLength();
1513*0d4d7139SKay Schenk         ::rtl::OUString sName = rComposedName.trim();
1514*0d4d7139SKay Schenk         ::rtl::OUString sColumnName;
1515*0d4d7139SKay Schenk         sal_Int32 nPos, nRPos = 0;
1516*0d4d7139SKay Schenk 
1517*0d4d7139SKay Schenk         for (;;)
1518*0d4d7139SKay Schenk         {
1519*0d4d7139SKay Schenk             nPos = sName.indexOf( rQuoteString, nRPos );
1520*0d4d7139SKay Schenk             if ( nPos >= 0 )
1521*0d4d7139SKay Schenk             {
1522*0d4d7139SKay Schenk                 nRPos = sName.indexOf( rQuoteString, nPos + nQuoteLength );
1523*0d4d7139SKay Schenk                 if ( nRPos > nPos )
1524*0d4d7139SKay Schenk                 {
1525*0d4d7139SKay Schenk                     if ( nRPos + nQuoteLength < sName.getLength() )
1526*0d4d7139SKay Schenk                     {
1527*0d4d7139SKay Schenk                         nRPos += nQuoteLength; // -1 + 1 skip dot
1528*0d4d7139SKay Schenk                     }
1529*0d4d7139SKay Schenk                     else
1530*0d4d7139SKay Schenk                     {
1531*0d4d7139SKay Schenk                         sColumnName = sName.copy( nPos + nQuoteLength, nRPos - nPos - nQuoteLength );
1532*0d4d7139SKay Schenk                         break;
1533*0d4d7139SKay Schenk                     }
1534*0d4d7139SKay Schenk                 }
1535*0d4d7139SKay Schenk                 else
1536*0d4d7139SKay Schenk                     break;
1537*0d4d7139SKay Schenk             }
1538*0d4d7139SKay Schenk             else
1539*0d4d7139SKay Schenk                 break;
1540*0d4d7139SKay Schenk         }
1541*0d4d7139SKay Schenk         return sColumnName.isEmpty() ? rComposedName : sColumnName;
1542*0d4d7139SKay Schenk     }
1543*0d4d7139SKay Schenk 
lcl_getCondition(const Sequence<Sequence<PropertyValue>> & filter,const OPredicateInputController & i_aPredicateInputController,const Reference<XNameAccess> & i_xSelectColumns,const::rtl::OUString & rQuoteString)1544*0d4d7139SKay Schenk 	::rtl::OUString lcl_getCondition(const Sequence< Sequence< PropertyValue > >& filter,
1545*0d4d7139SKay Schenk             const OPredicateInputController& i_aPredicateInputController,
1546*0d4d7139SKay Schenk             const Reference< XNameAccess >& i_xSelectColumns,
1547*0d4d7139SKay Schenk             const ::rtl::OUString& rQuoteString )
1548cdf0e10cSrcweir 	{
1549cdf0e10cSrcweir 		::rtl::OUStringBuffer sRet;
1550cdf0e10cSrcweir 		const Sequence< PropertyValue >* pOrIter = filter.getConstArray();
1551cdf0e10cSrcweir 		const Sequence< PropertyValue >* pOrEnd = pOrIter + filter.getLength();
1552cdf0e10cSrcweir 		while ( pOrIter != pOrEnd )
1553cdf0e10cSrcweir 		{
1554cdf0e10cSrcweir 			if ( pOrIter->getLength() )
1555cdf0e10cSrcweir 			{
1556cdf0e10cSrcweir 				sRet.append(L_BRACKET);
1557cdf0e10cSrcweir 				const PropertyValue* pAndIter = pOrIter->getConstArray();
1558cdf0e10cSrcweir 				const PropertyValue* pAndEnd = pAndIter + pOrIter->getLength();
1559cdf0e10cSrcweir 				while ( pAndIter != pAndEnd )
1560cdf0e10cSrcweir 				{
1561cdf0e10cSrcweir 					sRet.append(pAndIter->Name);
1562cdf0e10cSrcweir 					::rtl::OUString sValue;
1563cdf0e10cSrcweir 					pAndIter->Value >>= sValue;
1564*0d4d7139SKay Schenk                     const ::rtl::OUString sColumnName = lcl_getDecomposedColumnName( pAndIter->Name, rQuoteString );
1565*0d4d7139SKay Schenk                     if ( i_xSelectColumns.is() && i_xSelectColumns->hasByName(sColumnName) )
1566cdf0e10cSrcweir                     {
1567*0d4d7139SKay Schenk                         Reference<XPropertySet> xColumn(i_xSelectColumns->getByName(sColumnName),UNO_QUERY);
1568cdf0e10cSrcweir                         sValue = i_aPredicateInputController.getPredicateValue(sValue,xColumn,sal_True);
1569cdf0e10cSrcweir                     }
1570cdf0e10cSrcweir                     else
1571cdf0e10cSrcweir                     {
1572cdf0e10cSrcweir                         sValue = i_aPredicateInputController.getPredicateValue(pAndIter->Name,sValue,sal_True);
1573cdf0e10cSrcweir                     }
1574cdf0e10cSrcweir 					lcl_addFilterCriteria_throw(pAndIter->Handle,sValue,sRet);
1575cdf0e10cSrcweir 					++pAndIter;
1576cdf0e10cSrcweir 					if ( pAndIter != pAndEnd )
1577cdf0e10cSrcweir 						sRet.append(STR_AND);
1578cdf0e10cSrcweir 				}
1579cdf0e10cSrcweir 				sRet.append(R_BRACKET);
1580cdf0e10cSrcweir 			}
1581cdf0e10cSrcweir 			++pOrIter;
1582cdf0e10cSrcweir 			if ( pOrIter != pOrEnd && sRet.getLength() )
1583cdf0e10cSrcweir 				sRet.append(STR_OR);
1584cdf0e10cSrcweir 		}
1585cdf0e10cSrcweir 		return sRet.makeStringAndClear();
1586cdf0e10cSrcweir 	}
1587cdf0e10cSrcweir }
1588cdf0e10cSrcweir // -----------------------------------------------------------------------------
setStructuredFilter(const Sequence<Sequence<PropertyValue>> & filter)1589cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setStructuredFilter( const Sequence< Sequence< PropertyValue > >& filter ) throw (SQLException, ::com::sun::star::lang::IllegalArgumentException, RuntimeException)
1590cdf0e10cSrcweir {
1591cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setStructuredFilter" );
1592cdf0e10cSrcweir     OPredicateInputController aPredicateInput(m_aContext.getLegacyServiceFactory(),m_xConnection);
1593*0d4d7139SKay Schenk 	setFilter(lcl_getCondition(filter,aPredicateInput,getColumns(), m_xMetaData->getIdentifierQuoteString()));
1594cdf0e10cSrcweir }
1595cdf0e10cSrcweir // -----------------------------------------------------------------------------
setStructuredHavingClause(const Sequence<Sequence<PropertyValue>> & filter)1596cdf0e10cSrcweir void SAL_CALL OSingleSelectQueryComposer::setStructuredHavingClause( const Sequence< Sequence< PropertyValue > >& filter ) throw (SQLException, RuntimeException)
1597cdf0e10cSrcweir {
1598cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setStructuredHavingClause" );
1599cdf0e10cSrcweir     OPredicateInputController aPredicateInput(m_aContext.getLegacyServiceFactory(),m_xConnection);
1600*0d4d7139SKay Schenk 	setHavingClause(lcl_getCondition(filter,aPredicateInput,getColumns(), m_xMetaData->getIdentifierQuoteString()));
1601cdf0e10cSrcweir }
1602cdf0e10cSrcweir // -----------------------------------------------------------------------------
setConditionByColumn(const Reference<XPropertySet> & column,sal_Bool andCriteria,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,::rtl::OUString> & _aSetFunctor,sal_Int32 filterOperator)1603cdf0e10cSrcweir void OSingleSelectQueryComposer::setConditionByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria ,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,::rtl::OUString>& _aSetFunctor,sal_Int32 filterOperator)
1604cdf0e10cSrcweir {
1605cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::setConditionByColumn" );
1606cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1607cdf0e10cSrcweir 
1608cdf0e10cSrcweir 	if ( !column.is()
1609cdf0e10cSrcweir 		|| !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_VALUE)
1610cdf0e10cSrcweir 		|| !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
1611cdf0e10cSrcweir         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_TYPE))
1612cdf0e10cSrcweir 		throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
1613cdf0e10cSrcweir 
1614cdf0e10cSrcweir 	sal_Int32 nType = 0;
1615cdf0e10cSrcweir 	column->getPropertyValue(PROPERTY_TYPE) >>= nType;
1616cdf0e10cSrcweir 	sal_Int32 nSearchable = dbtools::getSearchColumnFlag(m_xConnection,nType);
1617cdf0e10cSrcweir 	if(nSearchable == ColumnSearch::NONE)
1618cdf0e10cSrcweir 		throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_SEARCHABLE),*this,SQLSTATE_GENERAL,1000,Any() );
1619cdf0e10cSrcweir 
1620cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir 	::rtl::OUString aName;
1623cdf0e10cSrcweir 	column->getPropertyValue(PROPERTY_NAME) >>= aName;
1624cdf0e10cSrcweir 
1625cdf0e10cSrcweir 	Any aValue;
1626cdf0e10cSrcweir 	column->getPropertyValue(PROPERTY_VALUE) >>= aValue;
1627cdf0e10cSrcweir 
1628cdf0e10cSrcweir 	::rtl::OUStringBuffer aSQL;
1629cdf0e10cSrcweir 	const ::rtl::OUString aQuote	= m_xMetaData->getIdentifierQuoteString();
1630cdf0e10cSrcweir     getColumns();
1631cdf0e10cSrcweir 
1632cdf0e10cSrcweir 	if ( m_aCurrentColumns[SelectColumns] && m_aCurrentColumns[SelectColumns]->hasByName(aName) )
1633cdf0e10cSrcweir 	{
1634cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
1635cdf0e10cSrcweir 		m_aCurrentColumns[SelectColumns]->getByName(aName) >>= xColumn;
1636cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
1637cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
1638cdf0e10cSrcweir 		OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AggregateFunction"))),"Property AggregateFunctionnot available!");
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir 		::rtl::OUString sRealName,sTableName;
1641cdf0e10cSrcweir 		xColumn->getPropertyValue(PROPERTY_REALNAME)	>>= sRealName;
1642cdf0e10cSrcweir 		xColumn->getPropertyValue(PROPERTY_TABLENAME)	>>= sTableName;
1643cdf0e10cSrcweir 		if(sTableName.indexOf('.',0) != -1)
1644cdf0e10cSrcweir 		{
1645cdf0e10cSrcweir 			::rtl::OUString aCatlog,aSchema,aTable;
1646cdf0e10cSrcweir 			::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatlog,aSchema,aTable,::dbtools::eInDataManipulation);
1647cdf0e10cSrcweir 			sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, sal_True, ::dbtools::eInDataManipulation );
1648cdf0e10cSrcweir 		}
1649cdf0e10cSrcweir 		else
1650cdf0e10cSrcweir 			sTableName = ::dbtools::quoteName(aQuote,sTableName);
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir 		if ( !::comphelper::getBOOL(xColumn->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Function")))) )
1653cdf0e10cSrcweir 		{
1654cdf0e10cSrcweir 			aSQL =  sTableName;
1655cdf0e10cSrcweir 			aSQL.appendAscii( "." );
1656cdf0e10cSrcweir 			aSQL.append( ::dbtools::quoteName( aQuote, sRealName ) );
1657cdf0e10cSrcweir 		}
1658cdf0e10cSrcweir 		else
1659cdf0e10cSrcweir 			aSQL = sRealName;
1660cdf0e10cSrcweir 
1661cdf0e10cSrcweir 	}
1662cdf0e10cSrcweir 	else
1663cdf0e10cSrcweir     {
1664cdf0e10cSrcweir 		aSQL = getTableAlias( column );
1665cdf0e10cSrcweir         aSQL.append( ::dbtools::quoteName( aQuote, aName ) );
1666cdf0e10cSrcweir     }
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir 
1669cdf0e10cSrcweir 	if ( aValue.hasValue() )
1670cdf0e10cSrcweir 	{
1671cdf0e10cSrcweir 		if(  !m_xTypeConverter.is() )
1672cdf0e10cSrcweir             m_aContext.createComponent( "com.sun.star.script.Converter", m_xTypeConverter );
1673cdf0e10cSrcweir 		OSL_ENSURE(m_xTypeConverter.is(),"NO typeconverter!");
1674cdf0e10cSrcweir 
1675cdf0e10cSrcweir         if ( nType != DataType::BOOLEAN && DataType::BIT != nType )
1676cdf0e10cSrcweir         {
1677cdf0e10cSrcweir             ::rtl::OUString sEmpty;
1678cdf0e10cSrcweir             lcl_addFilterCriteria_throw(filterOperator,sEmpty,aSQL);
1679cdf0e10cSrcweir         }
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir 		switch(nType)
1682cdf0e10cSrcweir 		{
1683cdf0e10cSrcweir 			case DataType::VARCHAR:
1684cdf0e10cSrcweir 			case DataType::CHAR:
1685cdf0e10cSrcweir 			case DataType::LONGVARCHAR:
1686cdf0e10cSrcweir 				aSQL.append( DBTypeConversion::toSQLString( nType, aValue, sal_True, m_xTypeConverter ) );
1687cdf0e10cSrcweir 				break;
1688cdf0e10cSrcweir 			case DataType::CLOB:
1689cdf0e10cSrcweir 				{
1690cdf0e10cSrcweir 					Reference< XClob > xClob(aValue,UNO_QUERY);
1691cdf0e10cSrcweir 					if ( xClob.is() )
1692cdf0e10cSrcweir 					{
1693cdf0e10cSrcweir 						const ::sal_Int64 nLength = xClob->length();
1694cdf0e10cSrcweir 						if ( sal_Int64(nLength + aSQL.getLength() + STR_LIKE.getLength() ) < sal_Int64(SAL_MAX_INT32) )
1695cdf0e10cSrcweir 						{
1696cdf0e10cSrcweir 							aSQL.appendAscii("'");
1697cdf0e10cSrcweir 							aSQL.append( xClob->getSubString(1,(sal_Int32)nLength) );
1698cdf0e10cSrcweir 							aSQL.appendAscii("'");
1699cdf0e10cSrcweir 						}
1700cdf0e10cSrcweir 					}
1701cdf0e10cSrcweir 					else
1702cdf0e10cSrcweir 					{
1703cdf0e10cSrcweir 						aSQL.append( DBTypeConversion::toSQLString( nType, aValue, sal_True, m_xTypeConverter ) );
1704cdf0e10cSrcweir 					}
1705cdf0e10cSrcweir 				}
1706cdf0e10cSrcweir 				break;
1707cdf0e10cSrcweir 			case DataType::VARBINARY:
1708cdf0e10cSrcweir 			case DataType::BINARY:
1709cdf0e10cSrcweir 			case DataType::LONGVARBINARY:
1710cdf0e10cSrcweir 				{
1711cdf0e10cSrcweir 					Sequence<sal_Int8> aSeq;
1712cdf0e10cSrcweir 					if(aValue >>= aSeq)
1713cdf0e10cSrcweir 					{
1714cdf0e10cSrcweir 						if(nSearchable == ColumnSearch::CHAR)
1715cdf0e10cSrcweir 						{
1716cdf0e10cSrcweir                             aSQL.appendAscii( "\'" );
1717cdf0e10cSrcweir 						}
1718cdf0e10cSrcweir                         aSQL.appendAscii( "0x" );
1719cdf0e10cSrcweir 						const sal_Int8* pBegin	= aSeq.getConstArray();
1720cdf0e10cSrcweir 						const sal_Int8* pEnd	= pBegin + aSeq.getLength();
1721cdf0e10cSrcweir 						for(;pBegin != pEnd;++pBegin)
1722cdf0e10cSrcweir 						{
1723cdf0e10cSrcweir 							aSQL.append( (sal_Int32)*pBegin, 16 ).getStr();
1724cdf0e10cSrcweir 						}
1725cdf0e10cSrcweir 						if(nSearchable == ColumnSearch::CHAR)
1726cdf0e10cSrcweir 							aSQL.appendAscii( "\'" );
1727cdf0e10cSrcweir 					}
1728cdf0e10cSrcweir 					else
1729cdf0e10cSrcweir 						throw SQLException(DBACORE_RESSTRING(RID_STR_NOT_SEQUENCE_INT8),*this,SQLSTATE_GENERAL,1000,Any() );
1730cdf0e10cSrcweir 				}
1731cdf0e10cSrcweir 				break;
1732cdf0e10cSrcweir 			case DataType::BIT:
1733cdf0e10cSrcweir             case DataType::BOOLEAN:
1734cdf0e10cSrcweir 				{
1735cdf0e10cSrcweir 					sal_Bool bValue = sal_False;
1736cdf0e10cSrcweir 					m_xTypeConverter->convertToSimpleType(aValue, TypeClass_BOOLEAN) >>= bValue;
1737cdf0e10cSrcweir 
1738cdf0e10cSrcweir                     ::rtl::OUString sColumnExp = aSQL.makeStringAndClear();
1739cdf0e10cSrcweir                     getBoleanComparisonPredicate( sColumnExp, bValue, m_nBoolCompareMode, aSQL );
1740cdf0e10cSrcweir 				}
1741cdf0e10cSrcweir                 break;
1742cdf0e10cSrcweir 			default:
1743cdf0e10cSrcweir 				aSQL.append( DBTypeConversion::toSQLString( nType, aValue, sal_True, m_xTypeConverter ) );
1744cdf0e10cSrcweir                 break;
1745cdf0e10cSrcweir 		}
1746cdf0e10cSrcweir 	}
1747cdf0e10cSrcweir 	else
1748cdf0e10cSrcweir     {
1749cdf0e10cSrcweir         sal_Int32 nFilterOp = filterOperator;
1750cdf0e10cSrcweir         if ( filterOperator != SQLFilterOperator::SQLNULL && filterOperator != SQLFilterOperator::NOT_SQLNULL )
1751cdf0e10cSrcweir             nFilterOp = SQLFilterOperator::SQLNULL;
1752cdf0e10cSrcweir         ::rtl::OUString sEmpty;
1753cdf0e10cSrcweir         lcl_addFilterCriteria_throw(nFilterOp,sEmpty,aSQL);
1754cdf0e10cSrcweir     }
1755cdf0e10cSrcweir 
1756cdf0e10cSrcweir 	// filter anhaengen
1757cdf0e10cSrcweir 	// select ohne where und order by aufbauen
1758cdf0e10cSrcweir 	::rtl::OUString sFilter = getFilter();
1759cdf0e10cSrcweir 
1760cdf0e10cSrcweir 	if ( sFilter.getLength() && aSQL.getLength() )
1761cdf0e10cSrcweir 	{
1762cdf0e10cSrcweir 		::rtl::OUString sTemp(L_BRACKET);
1763cdf0e10cSrcweir 		sTemp += sFilter;
1764cdf0e10cSrcweir 		sTemp += R_BRACKET;
1765cdf0e10cSrcweir 		sTemp += andCriteria ? STR_AND : STR_OR;
1766cdf0e10cSrcweir 		sFilter = sTemp;
1767cdf0e10cSrcweir 	}
1768cdf0e10cSrcweir 	sFilter += aSQL.makeStringAndClear();
1769cdf0e10cSrcweir 
1770cdf0e10cSrcweir 	// add the filter and the sort order
1771cdf0e10cSrcweir 	_aSetFunctor(this,sFilter);
1772cdf0e10cSrcweir }
1773cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStructuredCondition(TGetParseNode & _aGetFunctor)1774cdf0e10cSrcweir Sequence< Sequence< PropertyValue > > OSingleSelectQueryComposer::getStructuredCondition( TGetParseNode& _aGetFunctor )
1775cdf0e10cSrcweir {
1776cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getStructuredCondition" );
1777cdf0e10cSrcweir 	::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1778cdf0e10cSrcweir 
1779cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
1780cdf0e10cSrcweir 
1781cdf0e10cSrcweir 	Sequence< Sequence< PropertyValue > > aFilterSeq;
1782cdf0e10cSrcweir 	::rtl::OUString sFilter = getStatementPart( _aGetFunctor, m_aAdditiveIterator );
1783cdf0e10cSrcweir 
1784cdf0e10cSrcweir 	if ( sFilter.getLength() != 0 )
1785cdf0e10cSrcweir 	{
1786cdf0e10cSrcweir 		::rtl::OUString aSql(m_aPureSelectSQL);
1787cdf0e10cSrcweir 		// build a temporary parse node
1788cdf0e10cSrcweir 		const OSQLParseNode* pTempNode = m_aAdditiveIterator.getParseTree();
1789cdf0e10cSrcweir 
1790cdf0e10cSrcweir 		aSql += STR_WHERE;
1791cdf0e10cSrcweir 		aSql += sFilter;
1792cdf0e10cSrcweir 
1793cdf0e10cSrcweir 		::rtl::OUString aErrorMsg;
1794cdf0e10cSrcweir 		::std::auto_ptr<OSQLParseNode> pSqlParseNode( m_aSqlParser.parseTree(aErrorMsg,aSql));
1795cdf0e10cSrcweir 		if ( pSqlParseNode.get() )
1796cdf0e10cSrcweir 		{
1797cdf0e10cSrcweir 			m_aAdditiveIterator.setParseTree(pSqlParseNode.get());
1798cdf0e10cSrcweir 			// normalize the filter
1799cdf0e10cSrcweir 			OSQLParseNode* pWhereNode = const_cast<OSQLParseNode*>(m_aAdditiveIterator.getWhereTree());
1800cdf0e10cSrcweir 
1801cdf0e10cSrcweir 			OSQLParseNode* pCondition = pWhereNode->getChild(1);
1802cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
1803cdf0e10cSrcweir             ::rtl::OUString sCondition;
1804cdf0e10cSrcweir             pCondition->parseNodeToStr( sCondition, m_xConnection );
1805cdf0e10cSrcweir         #endif
1806cdf0e10cSrcweir 			OSQLParseNode::negateSearchCondition(pCondition);
1807cdf0e10cSrcweir 
1808cdf0e10cSrcweir 			pCondition = pWhereNode->getChild(1);
1809cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
1810cdf0e10cSrcweir             sCondition = ::rtl::OUString();
1811cdf0e10cSrcweir             pCondition->parseNodeToStr( sCondition, m_xConnection );
1812cdf0e10cSrcweir         #endif
1813cdf0e10cSrcweir 			OSQLParseNode::disjunctiveNormalForm(pCondition);
1814cdf0e10cSrcweir 
1815cdf0e10cSrcweir 			pCondition = pWhereNode->getChild(1);
1816cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
1817cdf0e10cSrcweir             sCondition = ::rtl::OUString();
1818cdf0e10cSrcweir             pCondition->parseNodeToStr( sCondition, m_xConnection );
1819cdf0e10cSrcweir         #endif
1820cdf0e10cSrcweir 			OSQLParseNode::absorptions(pCondition);
1821cdf0e10cSrcweir 
1822cdf0e10cSrcweir 			pCondition = pWhereNode->getChild(1);
1823cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
1824cdf0e10cSrcweir             sCondition = ::rtl::OUString();
1825cdf0e10cSrcweir             pCondition->parseNodeToStr( sCondition, m_xConnection );
1826cdf0e10cSrcweir         #endif
1827cdf0e10cSrcweir 			if ( pCondition )
1828cdf0e10cSrcweir 			{
1829cdf0e10cSrcweir 				::std::vector< ::std::vector < PropertyValue > > aFilters;
1830cdf0e10cSrcweir                 Reference< XNumberFormatter > xFormatter;
1831cdf0e10cSrcweir                 m_aContext.createComponent( "com.sun.star.util.NumberFormatter", xFormatter );
1832cdf0e10cSrcweir 				xFormatter->attachNumberFormatsSupplier( m_xNumberFormatsSupplier );
1833cdf0e10cSrcweir 
1834cdf0e10cSrcweir 				if (setORCriteria(pCondition, m_aAdditiveIterator, aFilters, xFormatter))
1835cdf0e10cSrcweir 				{
1836cdf0e10cSrcweir 					aFilterSeq.realloc(aFilters.size());
1837cdf0e10cSrcweir 					Sequence<PropertyValue>* pFilters = aFilterSeq.getArray();
1838cdf0e10cSrcweir 					::std::vector< ::std::vector < PropertyValue > >::const_iterator aEnd = aFilters.end();
1839cdf0e10cSrcweir 					::std::vector< ::std::vector < PropertyValue > >::const_iterator i = aFilters.begin();
1840cdf0e10cSrcweir 					for ( ; i != aEnd ; ++i)
1841cdf0e10cSrcweir 					{
1842cdf0e10cSrcweir 						const ::std::vector < PropertyValue >& rProperties = *i;
1843cdf0e10cSrcweir 						pFilters->realloc(rProperties.size());
1844cdf0e10cSrcweir 						PropertyValue* pFilter = pFilters->getArray();
1845cdf0e10cSrcweir 						::std::vector < PropertyValue >::const_iterator j = rProperties.begin();
1846cdf0e10cSrcweir 						::std::vector < PropertyValue >::const_iterator aEnd2 = rProperties.end();
1847cdf0e10cSrcweir 						for ( ; j != aEnd2 ; ++j)
1848cdf0e10cSrcweir 						{
1849cdf0e10cSrcweir 							*pFilter = *j;
1850cdf0e10cSrcweir 							++pFilter;
1851cdf0e10cSrcweir 						}
1852cdf0e10cSrcweir 						++pFilters;
1853cdf0e10cSrcweir 					}
1854cdf0e10cSrcweir 				}
1855cdf0e10cSrcweir 			}
1856cdf0e10cSrcweir 			// restore
1857cdf0e10cSrcweir 			m_aAdditiveIterator.setParseTree(pTempNode);
1858cdf0e10cSrcweir 		}
1859cdf0e10cSrcweir 	}
1860cdf0e10cSrcweir 	return aFilterSeq;
1861cdf0e10cSrcweir }
1862cdf0e10cSrcweir // -----------------------------------------------------------------------------
getKeyword(SQLPart _ePart) const1863cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getKeyword( SQLPart _ePart ) const
1864cdf0e10cSrcweir {
1865cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getKeyword" );
1866cdf0e10cSrcweir 	::rtl::OUString sKeyword;
1867cdf0e10cSrcweir 	switch(_ePart)
1868cdf0e10cSrcweir 	{
1869cdf0e10cSrcweir 		default:
1870cdf0e10cSrcweir             OSL_ENSURE( 0, "OSingleSelectQueryComposer::getKeyWord: Invalid enum value!" );
1871cdf0e10cSrcweir             // no break, fallback to WHERE
1872cdf0e10cSrcweir 		case Where:
1873cdf0e10cSrcweir 			sKeyword = STR_WHERE;
1874cdf0e10cSrcweir 			break;
1875cdf0e10cSrcweir 		case Group:
1876cdf0e10cSrcweir 			sKeyword = STR_GROUP_BY;
1877cdf0e10cSrcweir 			break;
1878cdf0e10cSrcweir 		case Having:
1879cdf0e10cSrcweir 			sKeyword = STR_HAVING;
1880cdf0e10cSrcweir 			break;
1881cdf0e10cSrcweir 		case Order:
1882cdf0e10cSrcweir 			sKeyword = STR_ORDER_BY;
1883cdf0e10cSrcweir 			break;
1884cdf0e10cSrcweir 	}
1885cdf0e10cSrcweir 	return sKeyword;
1886cdf0e10cSrcweir }
1887cdf0e10cSrcweir 
1888cdf0e10cSrcweir // -----------------------------------------------------------------------------
getSQLPart(SQLPart _ePart,OSQLParseTreeIterator & _rIterator,sal_Bool _bWithKeyword)1889cdf0e10cSrcweir ::rtl::OUString OSingleSelectQueryComposer::getSQLPart( SQLPart _ePart, OSQLParseTreeIterator& _rIterator, sal_Bool _bWithKeyword )
1890cdf0e10cSrcweir {
1891cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OSingleSelectQueryComposer::getSQLPart" );
1892cdf0e10cSrcweir 	TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleWhereTree);
1893cdf0e10cSrcweir 	::rtl::OUString sKeyword( getKeyword( _ePart ) );
1894cdf0e10cSrcweir 	switch(_ePart)
1895cdf0e10cSrcweir 	{
1896cdf0e10cSrcweir 		case Where:
1897cdf0e10cSrcweir 			F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleWhereTree);
1898cdf0e10cSrcweir 			break;
1899cdf0e10cSrcweir 		case Group:
1900cdf0e10cSrcweir 			F_tmp = TGetParseNode (&OSQLParseTreeIterator::getSimpleGroupByTree);
1901cdf0e10cSrcweir 			break;
1902cdf0e10cSrcweir 		case Having:
1903cdf0e10cSrcweir 			F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleHavingTree);
1904cdf0e10cSrcweir 			break;
1905cdf0e10cSrcweir 		case Order:
1906cdf0e10cSrcweir 			F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleOrderTree);
1907cdf0e10cSrcweir 			break;
1908cdf0e10cSrcweir 		default:
1909cdf0e10cSrcweir 			OSL_ENSURE(0,"Invalid enum value!");
1910cdf0e10cSrcweir 	}
1911cdf0e10cSrcweir 
1912cdf0e10cSrcweir 	::rtl::OUString sRet = getStatementPart( F_tmp, _rIterator );
1913cdf0e10cSrcweir 	if ( _bWithKeyword && sRet.getLength() )
1914cdf0e10cSrcweir 		sRet = sKeyword + sRet;
1915cdf0e10cSrcweir 	return sRet;
1916cdf0e10cSrcweir }
1917cdf0e10cSrcweir // -----------------------------------------------------------------------------
1918