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 : #include <string.h>
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 == memcmp(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 : m_xComposerHelper->appendFilterByColumn(column, sal_True, SQLFilterOperator::EQUAL);
229 :
230 0 : FilterCreator aFilterCreator;
231 0 : aFilterCreator.append(getFilter());
232 0 : aFilterCreator.append(m_xComposerHelper->getFilter());
233 :
234 0 : setFilter( aFilterCreator.getComposedAndClear() );
235 0 : }
236 :
237 0 : void SAL_CALL OQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException)
238 : {
239 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendOrderByColumn" );
240 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
241 0 : ::osl::MutexGuard aGuard( m_aMutex );
242 :
243 0 : m_xComposerHelper->setQuery(getQuery());
244 0 : m_xComposerHelper->setOrder(::rtl::OUString());
245 0 : m_xComposerHelper->appendOrderByColumn(column,ascending);
246 :
247 0 : OrderCreator aOrderCreator;
248 0 : aOrderCreator.append(getOrder());
249 0 : aOrderCreator.append(m_xComposerHelper->getOrder());
250 :
251 0 : setOrder(aOrderCreator.getComposedAndClear());
252 0 : }
253 :
254 0 : void SAL_CALL OQueryComposer::setFilter( const ::rtl::OUString& filter ) throw(SQLException, RuntimeException)
255 : {
256 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setFilter" );
257 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
258 :
259 0 : ::osl::MutexGuard aGuard( m_aMutex );
260 0 : FilterCreator aFilterCreator;
261 0 : aFilterCreator.append(m_sOrgFilter);
262 0 : aFilterCreator.append(filter);
263 :
264 0 : m_aFilters.clear();
265 0 : if ( !filter.isEmpty() )
266 0 : m_aFilters.push_back(filter);
267 :
268 0 : m_xComposer->setFilter( aFilterCreator.getComposedAndClear() );
269 0 : }
270 :
271 0 : void SAL_CALL OQueryComposer::setOrder( const ::rtl::OUString& order ) throw(SQLException, RuntimeException)
272 : {
273 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setOrder" );
274 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
275 :
276 0 : ::osl::MutexGuard aGuard( m_aMutex );
277 :
278 0 : OrderCreator aOrderCreator;
279 0 : aOrderCreator.append(m_sOrgOrder);
280 0 : aOrderCreator.append(order);
281 :
282 0 : m_aOrders.clear();
283 0 : if ( !order.isEmpty() )
284 0 : m_aOrders.push_back(order);
285 :
286 0 : m_xComposer->setOrder(aOrderCreator.getComposedAndClear());
287 0 : }
288 :
289 : // XTablesSupplier
290 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getTables( ) throw(RuntimeException)
291 : {
292 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTables" );
293 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
294 :
295 0 : ::osl::MutexGuard aGuard( m_aMutex );
296 0 : return Reference<XTablesSupplier>(m_xComposer,UNO_QUERY)->getTables();
297 : }
298 :
299 : // XColumnsSupplier
300 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getColumns( ) throw(RuntimeException)
301 : {
302 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getColumns" );
303 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
304 :
305 0 : ::osl::MutexGuard aGuard( m_aMutex );
306 0 : return Reference<XColumnsSupplier>(m_xComposer,UNO_QUERY)->getColumns();
307 : }
308 :
309 0 : Reference< XIndexAccess > SAL_CALL OQueryComposer::getParameters( ) throw(RuntimeException)
310 : {
311 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getParameters" );
312 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
313 :
314 0 : ::osl::MutexGuard aGuard( m_aMutex );
315 0 : return Reference<XParametersSupplier>(m_xComposer,UNO_QUERY)->getParameters();
316 : }
317 :
318 0 : void SAL_CALL OQueryComposer::acquire() throw()
319 : {
320 0 : OSubComponent::acquire();
321 0 : }
322 :
323 0 : void SAL_CALL OQueryComposer::release() throw()
324 : {
325 0 : OSubComponent::release();
326 0 : }
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|