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