update dev300-m58
[ooovba.git] / dbaccess / source / core / api / querycomposer.cxx
blob2e30ceaa7fe817b97cd30636366bc8bae8abfec3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: querycomposer.cxx,v $
10 * $Revision: 1.68 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _COM_SUN_STAR_LANG_DISPOSEDEXCEPTION_HPP_
35 #include <com/sun/star/lang/DisposedException.hpp>
36 #endif
37 #ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTER_HPP_
38 #include <com/sun/star/util/XNumberFormatter.hpp>
39 #endif
40 #ifndef _COM_SUN_STAR_SDBC_COLUMNSEARCH_HPP_
41 #include <com/sun/star/sdbc/ColumnSearch.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
44 #include <com/sun/star/sdbc/DataType.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
47 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATA_HPP_
50 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_LANG_SERVICENOTREGISTEREDEXCEPTION_HPP_
53 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
54 #endif
55 #ifndef _COMPHELPER_SEQUENCE_HXX_
56 #include <comphelper/sequence.hxx>
57 #endif
58 #ifndef _COM_SUN_STAR_UNO_XAGGREGATION_HPP_
59 #include <com/sun/star/uno/XAggregation.hpp>
60 #endif
61 #ifndef _COMPHELPER_SEQUENCE_HXX_
62 #include <comphelper/sequence.hxx>
63 #endif
64 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
65 #include <comphelper/processfactory.hxx>
66 #endif
67 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
68 #include "dbastrings.hrc"
69 #endif
70 #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
71 #include <cppuhelper/typeprovider.hxx>
72 #endif
73 #ifndef _UTL_CONFIGMGR_HXX_
74 #include <unotools/configmgr.hxx>
75 #endif
76 #ifndef _COMPHELPER_TYPES_HXX_
77 #include <comphelper/types.hxx>
78 #endif
79 #ifndef _TOOLS_DEBUG_HXX
80 #include <tools/debug.hxx>
81 #endif
82 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HPP_
83 #include <com/sun/star/beans/PropertyAttribute.hpp>
84 #endif
85 #ifndef _COM_SUN_STAR_I18N_XLOCALEDATA_HPP_
86 #include <com/sun/star/i18n/XLocaleData.hpp>
87 #endif
88 #ifndef INCLUDED_SVTOOLS_SYSLOCALE_HXX
89 #include <svtools/syslocale.hxx>
90 #endif
91 #ifndef _COM_SUN_STAR_CONTAINER_XCHILD_HPP_
92 #include <com/sun/star/container/XChild.hpp>
93 #endif
94 #ifndef DBACCESS_CORE_API_QUERYCOMPOSER_HXX
95 #include "querycomposer.hxx"
96 #endif
97 #ifndef DBA_HELPERCOLLECTIONS_HXX
98 #include "HelperCollections.hxx"
99 #endif
100 #ifndef DBACCESS_SOURCE_CORE_INC_COMPOSERTOOLS_HXX
101 #include "composertools.hxx"
102 #endif
103 #include <algorithm>
104 #include <rtl/logfile.hxx>
106 using namespace dbaccess;
107 using namespace dbtools;
108 using namespace comphelper;
109 using namespace connectivity;
110 using namespace ::com::sun::star::uno;
111 using namespace ::com::sun::star::beans;
112 using namespace ::com::sun::star::sdbc;
113 using namespace ::com::sun::star::sdb;
114 using namespace ::com::sun::star::sdbcx;
115 using namespace ::com::sun::star::container;
116 using namespace ::com::sun::star::i18n;
117 using namespace ::com::sun::star::lang;
118 using namespace ::com::sun::star::script;
119 using namespace ::cppu;
120 using namespace ::osl;
121 using namespace ::utl;
124 DBG_NAME(OQueryComposer)
125 // -------------------------------------------------------------------------
126 OQueryComposer::OQueryComposer(const Reference< XConnection>& _xConnection)
127 : OSubComponent(m_aMutex,_xConnection)
129 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::OQueryComposer" );
130 DBG_CTOR(OQueryComposer,NULL);
131 OSL_ENSURE(_xConnection.is()," Connection cant be null!");
133 Reference<XMultiServiceFactory> xFac( _xConnection, UNO_QUERY_THROW );
134 m_xComposer.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
135 m_xComposerHelper.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
137 // -------------------------------------------------------------------------
138 OQueryComposer::~OQueryComposer()
140 DBG_DTOR(OQueryComposer,NULL);
142 // -----------------------------------------------------------------------------
143 void SAL_CALL OQueryComposer::disposing()
145 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::disposing" );
146 ::comphelper::disposeComponent(m_xComposerHelper);
147 ::comphelper::disposeComponent(m_xComposer);
149 // -------------------------------------------------------------------------
150 // ::com::sun::star::lang::XTypeProvider
151 Sequence< Type > SAL_CALL OQueryComposer::getTypes() throw (RuntimeException)
153 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTypes" );
154 return ::comphelper::concatSequences(OSubComponent::getTypes(),OQueryComposer_BASE::getTypes());
156 // -------------------------------------------------------------------------
157 Sequence< sal_Int8 > SAL_CALL OQueryComposer::getImplementationId() throw (RuntimeException)
159 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationId" );
160 static OImplementationId * pId = 0;
161 if (! pId)
163 MutexGuard aGuard( Mutex::getGlobalMutex() );
164 if (! pId)
166 static OImplementationId aId;
167 pId = &aId;
170 return pId->getImplementationId();
172 // -------------------------------------------------------------------------
173 // com::sun::star::lang::XUnoTunnel
174 sal_Int64 SAL_CALL OQueryComposer::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
176 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSomething" );
177 if (rId.getLength() == 16 && 0 == rtl_compareMemory(getImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
178 return reinterpret_cast<sal_Int64>(this);
180 return 0;
182 // -------------------------------------------------------------------------
183 Any SAL_CALL OQueryComposer::queryInterface( const Type & rType ) throw(RuntimeException)
185 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::queryInterface" );
186 Any aRet = OSubComponent::queryInterface(rType);
187 if(!aRet.hasValue())
188 aRet = OQueryComposer_BASE::queryInterface(rType);
189 return aRet;
191 // -------------------------------------------------------------------------
192 // XServiceInfo
193 //------------------------------------------------------------------------------
194 rtl::OUString OQueryComposer::getImplementationName( ) throw(RuntimeException)
196 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationName" );
197 return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.dbaccess.OQueryComposer"));
199 //------------------------------------------------------------------------------
200 sal_Bool OQueryComposer::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
202 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::supportsService" );
203 return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
205 //------------------------------------------------------------------------------
206 Sequence< ::rtl::OUString > OQueryComposer::getSupportedServiceNames( ) throw (RuntimeException)
208 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSupportedServiceNames" );
209 Sequence< rtl::OUString > aSNS( 1 );
210 aSNS[0] = SERVICE_SDB_SQLQUERYCOMPOSER;
211 return aSNS;
213 // -------------------------------------------------------------------------
214 // XSQLQueryComposer
215 ::rtl::OUString SAL_CALL OQueryComposer::getQuery( ) throw(RuntimeException)
217 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getQuery" );
218 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
220 ::osl::MutexGuard aGuard( m_aMutex );
221 Reference<XPropertySet> xProp(m_xComposer,UNO_QUERY);
222 ::rtl::OUString sQuery;
223 if ( xProp.is() )
224 xProp->getPropertyValue(PROPERTY_ORIGINAL) >>= sQuery;
225 return sQuery;
227 // -------------------------------------------------------------------------
228 void SAL_CALL OQueryComposer::setQuery( const ::rtl::OUString& command ) throw(SQLException, RuntimeException)
230 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setQuery" );
231 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
233 ::osl::MutexGuard aGuard( m_aMutex );
234 m_aFilters.clear();
235 m_xComposer->setQuery(command);
236 m_sOrgFilter = m_xComposer->getFilter();
237 m_sOrgOrder = m_xComposer->getOrder();
239 // -------------------------------------------------------------------------
240 ::rtl::OUString SAL_CALL OQueryComposer::getComposedQuery( ) throw(RuntimeException)
242 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getComposedQuery" );
243 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
245 MutexGuard aGuard(m_aMutex);
247 return m_xComposer->getQuery();
249 // -------------------------------------------------------------------------
250 ::rtl::OUString SAL_CALL OQueryComposer::getFilter( ) throw(RuntimeException)
252 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getFilter" );
253 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
254 MutexGuard aGuard(m_aMutex);
255 FilterCreator aFilterCreator;
256 aFilterCreator = ::std::for_each(m_aFilters.begin(),m_aFilters.end(),aFilterCreator);
257 return aFilterCreator.getComposedAndClear();
259 // -------------------------------------------------------------------------
260 Sequence< Sequence< PropertyValue > > SAL_CALL OQueryComposer::getStructuredFilter( ) throw(RuntimeException)
262 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getStructuredFilter" );
263 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
265 MutexGuard aGuard(m_aMutex);
266 return m_xComposer->getStructuredFilter();
268 // -------------------------------------------------------------------------
269 ::rtl::OUString SAL_CALL OQueryComposer::getOrder( ) throw(RuntimeException)
271 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getOrder" );
272 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
274 ::osl::MutexGuard aGuard( m_aMutex );
275 OrderCreator aOrderCreator;
276 aOrderCreator = ::std::for_each(m_aOrders.begin(),m_aOrders.end(),aOrderCreator);
277 return aOrderCreator.getComposedAndClear();
279 // -----------------------------------------------------------------------------
280 void SAL_CALL OQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column ) throw(SQLException, RuntimeException)
282 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendFilterByColumn" );
283 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
284 ::osl::MutexGuard aGuard( m_aMutex );
286 m_xComposerHelper->setQuery(getQuery());
287 m_xComposerHelper->setFilter(::rtl::OUString());
288 m_xComposerHelper->appendFilterByColumn(column,sal_True);
290 FilterCreator aFilterCreator;
291 aFilterCreator.append(getFilter());
292 aFilterCreator.append(m_xComposerHelper->getFilter());
294 setFilter( aFilterCreator.getComposedAndClear() );
296 // -------------------------------------------------------------------------
297 void SAL_CALL OQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException)
299 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendOrderByColumn" );
300 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
301 ::osl::MutexGuard aGuard( m_aMutex );
303 m_xComposerHelper->setQuery(getQuery());
304 m_xComposerHelper->setOrder(::rtl::OUString());
305 m_xComposerHelper->appendOrderByColumn(column,ascending);
307 OrderCreator aOrderCreator;
308 aOrderCreator.append(getOrder());
309 aOrderCreator.append(m_xComposerHelper->getOrder());
311 setOrder(aOrderCreator.getComposedAndClear());
313 // -------------------------------------------------------------------------
314 void SAL_CALL OQueryComposer::setFilter( const ::rtl::OUString& filter ) throw(SQLException, RuntimeException)
316 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setFilter" );
317 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
319 ::osl::MutexGuard aGuard( m_aMutex );
320 FilterCreator aFilterCreator;
321 aFilterCreator.append(m_sOrgFilter);
322 aFilterCreator.append(filter);
324 m_aFilters.clear();
325 if ( filter.getLength() )
326 m_aFilters.push_back(filter);
328 m_xComposer->setFilter( aFilterCreator.getComposedAndClear() );
330 // -------------------------------------------------------------------------
331 void SAL_CALL OQueryComposer::setOrder( const ::rtl::OUString& order ) throw(SQLException, RuntimeException)
333 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setOrder" );
334 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
336 ::osl::MutexGuard aGuard( m_aMutex );
338 OrderCreator aOrderCreator;
339 aOrderCreator.append(m_sOrgOrder);
340 aOrderCreator.append(order);
342 m_aOrders.clear();
343 if ( order.getLength() )
344 m_aOrders.push_back(order);
346 m_xComposer->setOrder(aOrderCreator.getComposedAndClear());
348 // -------------------------------------------------------------------------
349 // XTablesSupplier
350 Reference< XNameAccess > SAL_CALL OQueryComposer::getTables( ) throw(RuntimeException)
352 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTables" );
353 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
355 ::osl::MutexGuard aGuard( m_aMutex );
356 return Reference<XTablesSupplier>(m_xComposer,UNO_QUERY)->getTables();
358 // -------------------------------------------------------------------------
359 // XColumnsSupplier
360 Reference< XNameAccess > SAL_CALL OQueryComposer::getColumns( ) throw(RuntimeException)
362 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getColumns" );
363 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
365 ::osl::MutexGuard aGuard( m_aMutex );
366 return Reference<XColumnsSupplier>(m_xComposer,UNO_QUERY)->getColumns();
368 // -------------------------------------------------------------------------
369 Reference< XIndexAccess > SAL_CALL OQueryComposer::getParameters( ) throw(RuntimeException)
371 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getParameters" );
372 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
374 ::osl::MutexGuard aGuard( m_aMutex );
375 return Reference<XParametersSupplier>(m_xComposer,UNO_QUERY)->getParameters();
377 // -----------------------------------------------------------------------------
378 void SAL_CALL OQueryComposer::acquire() throw()
380 OSubComponent::acquire();
382 // -----------------------------------------------------------------------------
383 void SAL_CALL OQueryComposer::release() throw()
385 OSubComponent::release();
387 // -----------------------------------------------------------------------------