Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <com/sun/star/lang/DisposedException.hpp>
22 : : #include <com/sun/star/util/XNumberFormatter.hpp>
23 : : #include <com/sun/star/sdbc/ColumnSearch.hpp>
24 : : #include <com/sun/star/sdbc/DataType.hpp>
25 : : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
26 : : #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
27 : : #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
28 : : #include <comphelper/sequence.hxx>
29 : : #include <com/sun/star/uno/XAggregation.hpp>
30 : : #include <comphelper/processfactory.hxx>
31 : : #include "dbastrings.hrc"
32 : : #include <cppuhelper/typeprovider.hxx>
33 : : #include <unotools/configmgr.hxx>
34 : : #include <comphelper/types.hxx>
35 : : #include <tools/debug.hxx>
36 : : #include <com/sun/star/beans/PropertyAttribute.hpp>
37 : : #include <com/sun/star/i18n/XLocaleData.hpp>
38 : : #include <unotools/syslocale.hxx>
39 : : #include <com/sun/star/container/XChild.hpp>
40 : : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
41 : : #include "querycomposer.hxx"
42 : : #include "HelperCollections.hxx"
43 : : #include "composertools.hxx"
44 : : #include <algorithm>
45 : : #include <rtl/logfile.hxx>
46 : :
47 : : using namespace dbaccess;
48 : : using namespace dbtools;
49 : : using namespace comphelper;
50 : : using namespace connectivity;
51 : : using namespace ::com::sun::star::uno;
52 : : using namespace ::com::sun::star::beans;
53 : : using namespace ::com::sun::star::sdbc;
54 : : using namespace ::com::sun::star::sdb;
55 : : using namespace ::com::sun::star::sdbcx;
56 : : using namespace ::com::sun::star::container;
57 : : using namespace ::com::sun::star::i18n;
58 : : using namespace ::com::sun::star::lang;
59 : : using namespace ::com::sun::star::script;
60 : : using namespace ::cppu;
61 : : using namespace ::osl;
62 : : using namespace ::utl;
63 : :
64 : :
65 : : DBG_NAME(OQueryComposer)
66 : :
67 : 0 : OQueryComposer::OQueryComposer(const Reference< XConnection>& _xConnection)
68 [ # # ][ # # ]: 0 : : OSubComponent(m_aMutex,_xConnection)
[ # # ]
69 : : {
70 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::OQueryComposer" );
71 : : DBG_CTOR(OQueryComposer,NULL);
72 : : OSL_ENSURE(_xConnection.is()," Connection cant be null!");
73 : :
74 [ # # ]: 0 : Reference<XMultiServiceFactory> xFac( _xConnection, UNO_QUERY_THROW );
75 [ # # ][ # # ]: 0 : m_xComposer.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
[ # # ][ # # ]
76 [ # # ][ # # ]: 0 : m_xComposerHelper.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
[ # # ][ # # ]
77 : 0 : }
78 : :
79 [ # # ]: 0 : OQueryComposer::~OQueryComposer()
80 : : {
81 : : DBG_DTOR(OQueryComposer,NULL);
82 [ # # ]: 0 : }
83 : :
84 : 0 : void SAL_CALL OQueryComposer::disposing()
85 : : {
86 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::disposing" );
87 : 0 : ::comphelper::disposeComponent(m_xComposerHelper);
88 : 0 : ::comphelper::disposeComponent(m_xComposer);
89 : 0 : }
90 : :
91 : : // ::com::sun::star::lang::XTypeProvider
92 : 0 : Sequence< Type > SAL_CALL OQueryComposer::getTypes() throw (RuntimeException)
93 : : {
94 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTypes" );
95 [ # # ][ # # ]: 0 : return ::comphelper::concatSequences(OSubComponent::getTypes(),OQueryComposer_BASE::getTypes());
[ # # ]
96 : : }
97 : :
98 : 0 : Sequence< sal_Int8 > SAL_CALL OQueryComposer::getImplementationId() throw (RuntimeException)
99 : : {
100 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationId" );
101 : : static OImplementationId * pId = 0;
102 [ # # ]: 0 : if (! pId)
103 : : {
104 [ # # ][ # # ]: 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
105 [ # # ]: 0 : if (! pId)
106 : : {
107 [ # # ][ # # ]: 0 : static OImplementationId aId;
108 : 0 : pId = &aId;
109 [ # # ]: 0 : }
110 : : }
111 : 0 : return pId->getImplementationId();
112 : : }
113 : :
114 : : // com::sun::star::lang::XUnoTunnel
115 : 0 : sal_Int64 SAL_CALL OQueryComposer::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
116 : : {
117 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSomething" );
118 [ # # ][ # # ]: 0 : if (rId.getLength() == 16 && 0 == rtl_compareMemory(getImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
[ # # ][ # # ]
[ # # ]
[ # # # # ]
119 : 0 : return reinterpret_cast<sal_Int64>(this);
120 : :
121 : 0 : return 0;
122 : : }
123 : :
124 : 0 : Any SAL_CALL OQueryComposer::queryInterface( const Type & rType ) throw(RuntimeException)
125 : : {
126 : : //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::queryInterface" );
127 : 0 : Any aRet = OSubComponent::queryInterface(rType);
128 [ # # ]: 0 : if(!aRet.hasValue())
129 [ # # ]: 0 : aRet = OQueryComposer_BASE::queryInterface(rType);
130 : 0 : return aRet;
131 : : }
132 : :
133 : : // XServiceInfo
134 : 0 : rtl::OUString OQueryComposer::getImplementationName( ) throw(RuntimeException)
135 : : {
136 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationName" );
137 : 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.dbaccess.OQueryComposer"));
138 : : }
139 : :
140 : 0 : sal_Bool OQueryComposer::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
141 : : {
142 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::supportsService" );
143 [ # # ][ # # ]: 0 : return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
144 : : }
145 : :
146 : 0 : Sequence< ::rtl::OUString > OQueryComposer::getSupportedServiceNames( ) throw (RuntimeException)
147 : : {
148 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSupportedServiceNames" );
149 : 0 : Sequence< rtl::OUString > aSNS( 1 );
150 [ # # ][ # # ]: 0 : aSNS[0] = SERVICE_SDB_SQLQUERYCOMPOSER;
151 : 0 : return aSNS;
152 : : }
153 : :
154 : : // XSQLQueryComposer
155 : 0 : ::rtl::OUString SAL_CALL OQueryComposer::getQuery( ) throw(RuntimeException)
156 : : {
157 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getQuery" );
158 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
159 : :
160 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
161 [ # # ]: 0 : Reference<XPropertySet> xProp(m_xComposer,UNO_QUERY);
162 : 0 : ::rtl::OUString sQuery;
163 [ # # ]: 0 : if ( xProp.is() )
164 [ # # ][ # # ]: 0 : xProp->getPropertyValue(PROPERTY_ORIGINAL) >>= sQuery;
[ # # ]
165 [ # # ]: 0 : return sQuery;
166 : : }
167 : :
168 : 0 : void SAL_CALL OQueryComposer::setQuery( const ::rtl::OUString& command ) throw(SQLException, RuntimeException)
169 : : {
170 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setQuery" );
171 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
172 : :
173 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
174 : 0 : m_aFilters.clear();
175 [ # # ][ # # ]: 0 : m_xComposer->setQuery(command);
176 [ # # ][ # # ]: 0 : m_sOrgFilter = m_xComposer->getFilter();
177 [ # # ][ # # ]: 0 : m_sOrgOrder = m_xComposer->getOrder();
[ # # ]
178 : 0 : }
179 : :
180 : 0 : ::rtl::OUString SAL_CALL OQueryComposer::getComposedQuery( ) throw(RuntimeException)
181 : : {
182 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getComposedQuery" );
183 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
184 : :
185 [ # # ]: 0 : MutexGuard aGuard(m_aMutex);
186 : :
187 [ # # ][ # # ]: 0 : return m_xComposer->getQuery();
[ # # ]
188 : : }
189 : :
190 : 0 : ::rtl::OUString SAL_CALL OQueryComposer::getFilter( ) throw(RuntimeException)
191 : : {
192 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getFilter" );
193 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
194 [ # # ]: 0 : MutexGuard aGuard(m_aMutex);
195 : 0 : FilterCreator aFilterCreator;
196 [ # # ][ # # ]: 0 : aFilterCreator = ::std::for_each(m_aFilters.begin(),m_aFilters.end(),aFilterCreator);
[ # # ]
197 [ # # ][ # # ]: 0 : return aFilterCreator.getComposedAndClear();
198 : : }
199 : :
200 : 0 : Sequence< Sequence< PropertyValue > > SAL_CALL OQueryComposer::getStructuredFilter( ) throw(RuntimeException)
201 : : {
202 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getStructuredFilter" );
203 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
204 : :
205 [ # # ]: 0 : MutexGuard aGuard(m_aMutex);
206 [ # # ][ # # ]: 0 : return m_xComposer->getStructuredFilter();
[ # # ]
207 : : }
208 : :
209 : 0 : ::rtl::OUString SAL_CALL OQueryComposer::getOrder( ) throw(RuntimeException)
210 : : {
211 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getOrder" );
212 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
213 : :
214 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
215 : 0 : OrderCreator aOrderCreator;
216 [ # # ][ # # ]: 0 : aOrderCreator = ::std::for_each(m_aOrders.begin(),m_aOrders.end(),aOrderCreator);
[ # # ]
217 [ # # ][ # # ]: 0 : return aOrderCreator.getComposedAndClear();
218 : : }
219 : :
220 : 0 : void SAL_CALL OQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column ) throw(SQLException, RuntimeException)
221 : : {
222 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendFilterByColumn" );
223 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
224 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
225 : :
226 [ # # ][ # # ]: 0 : m_xComposerHelper->setQuery(getQuery());
[ # # ]
227 [ # # ][ # # ]: 0 : m_xComposerHelper->setFilter(::rtl::OUString());
228 : 0 : sal_Int32 nOp = SQLFilterOperator::EQUAL;
229 [ # # ]: 0 : if ( column.is() )
230 : : {
231 : 0 : sal_Int32 nType = 0;
232 [ # # ][ # # ]: 0 : column->getPropertyValue(PROPERTY_TYPE) >>= nType;
[ # # ]
233 [ # # ]: 0 : switch(nType)
234 : : {
235 : : case DataType::VARCHAR:
236 : : case DataType::CHAR:
237 : : case DataType::LONGVARCHAR:
238 : 0 : nOp = SQLFilterOperator::LIKE;
239 : 0 : break;
240 : : default:
241 : 0 : nOp = SQLFilterOperator::EQUAL;
242 : : }
243 : : }
244 [ # # ][ # # ]: 0 : m_xComposerHelper->appendFilterByColumn(column,sal_True,nOp);
245 : :
246 : 0 : FilterCreator aFilterCreator;
247 [ # # ][ # # ]: 0 : aFilterCreator.append(getFilter());
248 [ # # ][ # # ]: 0 : aFilterCreator.append(m_xComposerHelper->getFilter());
[ # # ]
249 : :
250 [ # # ][ # # ]: 0 : setFilter( aFilterCreator.getComposedAndClear() );
[ # # ]
251 : 0 : }
252 : :
253 : 0 : void SAL_CALL OQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException)
254 : : {
255 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendOrderByColumn" );
256 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
257 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
258 : :
259 [ # # ][ # # ]: 0 : m_xComposerHelper->setQuery(getQuery());
[ # # ]
260 [ # # ][ # # ]: 0 : m_xComposerHelper->setOrder(::rtl::OUString());
261 [ # # ][ # # ]: 0 : m_xComposerHelper->appendOrderByColumn(column,ascending);
262 : :
263 : 0 : OrderCreator aOrderCreator;
264 [ # # ][ # # ]: 0 : aOrderCreator.append(getOrder());
265 [ # # ][ # # ]: 0 : aOrderCreator.append(m_xComposerHelper->getOrder());
[ # # ]
266 : :
267 [ # # ][ # # ]: 0 : setOrder(aOrderCreator.getComposedAndClear());
[ # # ]
268 : 0 : }
269 : :
270 : 0 : void SAL_CALL OQueryComposer::setFilter( const ::rtl::OUString& filter ) throw(SQLException, RuntimeException)
271 : : {
272 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setFilter" );
273 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
274 : :
275 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
276 : 0 : FilterCreator aFilterCreator;
277 [ # # ]: 0 : aFilterCreator.append(m_sOrgFilter);
278 [ # # ]: 0 : aFilterCreator.append(filter);
279 : :
280 : 0 : m_aFilters.clear();
281 [ # # ]: 0 : if ( !filter.isEmpty() )
282 [ # # ]: 0 : m_aFilters.push_back(filter);
283 : :
284 [ # # ][ # # ]: 0 : m_xComposer->setFilter( aFilterCreator.getComposedAndClear() );
[ # # ][ # # ]
285 : 0 : }
286 : :
287 : 0 : void SAL_CALL OQueryComposer::setOrder( const ::rtl::OUString& order ) throw(SQLException, RuntimeException)
288 : : {
289 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setOrder" );
290 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
291 : :
292 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
293 : :
294 : 0 : OrderCreator aOrderCreator;
295 [ # # ]: 0 : aOrderCreator.append(m_sOrgOrder);
296 [ # # ]: 0 : aOrderCreator.append(order);
297 : :
298 : 0 : m_aOrders.clear();
299 [ # # ]: 0 : if ( !order.isEmpty() )
300 [ # # ]: 0 : m_aOrders.push_back(order);
301 : :
302 [ # # ][ # # ]: 0 : m_xComposer->setOrder(aOrderCreator.getComposedAndClear());
[ # # ][ # # ]
303 : 0 : }
304 : :
305 : : // XTablesSupplier
306 : 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getTables( ) throw(RuntimeException)
307 : : {
308 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTables" );
309 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
310 : :
311 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
312 [ # # ][ # # ]: 0 : return Reference<XTablesSupplier>(m_xComposer,UNO_QUERY)->getTables();
[ # # ][ # # ]
313 : : }
314 : :
315 : : // XColumnsSupplier
316 : 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getColumns( ) throw(RuntimeException)
317 : : {
318 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getColumns" );
319 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
320 : :
321 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
322 [ # # ][ # # ]: 0 : return Reference<XColumnsSupplier>(m_xComposer,UNO_QUERY)->getColumns();
[ # # ][ # # ]
323 : : }
324 : :
325 : 0 : Reference< XIndexAccess > SAL_CALL OQueryComposer::getParameters( ) throw(RuntimeException)
326 : : {
327 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getParameters" );
328 [ # # ]: 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
329 : :
330 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
331 [ # # ][ # # ]: 0 : return Reference<XParametersSupplier>(m_xComposer,UNO_QUERY)->getParameters();
[ # # ][ # # ]
332 : : }
333 : :
334 : 0 : void SAL_CALL OQueryComposer::acquire() throw()
335 : : {
336 : 0 : OSubComponent::acquire();
337 : 0 : }
338 : :
339 : 0 : void SAL_CALL OQueryComposer::release() throw()
340 : : {
341 : 0 : OSubComponent::release();
342 : 0 : }
343 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|