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