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/supportsservice.hxx>
33 : #include <cppuhelper/typeprovider.hxx>
34 : #include <unotools/configmgr.hxx>
35 : #include <comphelper/types.hxx>
36 : #include <tools/debug.hxx>
37 : #include <com/sun/star/beans/PropertyAttribute.hpp>
38 : #include <com/sun/star/i18n/XLocaleData.hpp>
39 : #include <unotools/syslocale.hxx>
40 : #include <com/sun/star/container/XChild.hpp>
41 : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
42 : #include "querycomposer.hxx"
43 : #include "HelperCollections.hxx"
44 : #include "composertools.hxx"
45 : #include <algorithm>
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 :
66 0 : OQueryComposer::OQueryComposer(const Reference< XConnection>& _xConnection)
67 0 : : OSubComponent(m_aMutex,_xConnection)
68 : {
69 : SAL_INFO("dbaccess", "OQueryComposer::OQueryComposer" );
70 : OSL_ENSURE(_xConnection.is()," Connection cant be null!");
71 :
72 0 : Reference<XMultiServiceFactory> xFac( _xConnection, UNO_QUERY_THROW );
73 0 : m_xComposer.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
74 0 : m_xComposerHelper.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
75 0 : }
76 :
77 0 : OQueryComposer::~OQueryComposer()
78 : {
79 0 : }
80 :
81 0 : void SAL_CALL OQueryComposer::disposing()
82 : {
83 : SAL_INFO("dbaccess", "OQueryComposer::disposing" );
84 0 : ::comphelper::disposeComponent(m_xComposerHelper);
85 0 : ::comphelper::disposeComponent(m_xComposer);
86 0 : }
87 :
88 : // ::com::sun::star::lang::XTypeProvider
89 0 : Sequence< Type > SAL_CALL OQueryComposer::getTypes() throw (RuntimeException, std::exception)
90 : {
91 : SAL_INFO("dbaccess", "OQueryComposer::getTypes" );
92 0 : return ::comphelper::concatSequences(OSubComponent::getTypes(),OQueryComposer_BASE::getTypes());
93 : }
94 :
95 0 : Sequence< sal_Int8 > SAL_CALL OQueryComposer::getImplementationId() throw (RuntimeException, std::exception)
96 : {
97 0 : return css::uno::Sequence<sal_Int8>();
98 : }
99 :
100 0 : Any SAL_CALL OQueryComposer::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
101 : {
102 : //SAL_INFO("dbaccess", "OQueryComposer::queryInterface" );
103 0 : Any aRet = OSubComponent::queryInterface(rType);
104 0 : if(!aRet.hasValue())
105 0 : aRet = OQueryComposer_BASE::queryInterface(rType);
106 0 : return aRet;
107 : }
108 :
109 : // XServiceInfo
110 0 : OUString OQueryComposer::getImplementationName( ) throw(RuntimeException, std::exception)
111 : {
112 : SAL_INFO("dbaccess", "OQueryComposer::getImplementationName" );
113 0 : return OUString("com.sun.star.sdb.dbaccess.OQueryComposer");
114 : }
115 :
116 0 : sal_Bool OQueryComposer::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
117 : {
118 0 : return cppu::supportsService(this, _rServiceName);
119 : }
120 :
121 0 : Sequence< OUString > OQueryComposer::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
122 : {
123 : SAL_INFO("dbaccess", "OQueryComposer::getSupportedServiceNames" );
124 0 : Sequence< OUString > aSNS( 1 );
125 0 : aSNS[0] = SERVICE_SDB_SQLQUERYCOMPOSER;
126 0 : return aSNS;
127 : }
128 :
129 : // XSQLQueryComposer
130 0 : OUString SAL_CALL OQueryComposer::getQuery( ) throw(RuntimeException, std::exception)
131 : {
132 : SAL_INFO("dbaccess", "OQueryComposer::getQuery" );
133 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
134 :
135 0 : ::osl::MutexGuard aGuard( m_aMutex );
136 0 : Reference<XPropertySet> xProp(m_xComposer,UNO_QUERY);
137 0 : OUString sQuery;
138 0 : if ( xProp.is() )
139 0 : xProp->getPropertyValue(PROPERTY_ORIGINAL) >>= sQuery;
140 0 : return sQuery;
141 : }
142 :
143 0 : void SAL_CALL OQueryComposer::setQuery( const OUString& command ) throw(SQLException, RuntimeException, std::exception)
144 : {
145 : SAL_INFO("dbaccess", "OQueryComposer::setQuery" );
146 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
147 :
148 0 : ::osl::MutexGuard aGuard( m_aMutex );
149 0 : m_aFilters.clear();
150 0 : m_xComposer->setQuery(command);
151 0 : m_sOrgFilter = m_xComposer->getFilter();
152 0 : m_sOrgOrder = m_xComposer->getOrder();
153 0 : }
154 :
155 0 : OUString SAL_CALL OQueryComposer::getComposedQuery( ) throw(RuntimeException, std::exception)
156 : {
157 : SAL_INFO("dbaccess", "OQueryComposer::getComposedQuery" );
158 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
159 :
160 0 : MutexGuard aGuard(m_aMutex);
161 :
162 0 : return m_xComposer->getQuery();
163 : }
164 :
165 0 : OUString SAL_CALL OQueryComposer::getFilter( ) throw(RuntimeException, std::exception)
166 : {
167 : SAL_INFO("dbaccess", "OQueryComposer::getFilter" );
168 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
169 0 : MutexGuard aGuard(m_aMutex);
170 0 : FilterCreator aFilterCreator;
171 0 : aFilterCreator = ::std::for_each(m_aFilters.begin(),m_aFilters.end(),aFilterCreator);
172 0 : return aFilterCreator.getComposedAndClear();
173 : }
174 :
175 0 : Sequence< Sequence< PropertyValue > > SAL_CALL OQueryComposer::getStructuredFilter( ) throw(RuntimeException, std::exception)
176 : {
177 : SAL_INFO("dbaccess", "OQueryComposer::getStructuredFilter" );
178 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
179 :
180 0 : MutexGuard aGuard(m_aMutex);
181 0 : return m_xComposer->getStructuredFilter();
182 : }
183 :
184 0 : OUString SAL_CALL OQueryComposer::getOrder( ) throw(RuntimeException, std::exception)
185 : {
186 : SAL_INFO("dbaccess", "OQueryComposer::getOrder" );
187 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
188 :
189 0 : ::osl::MutexGuard aGuard( m_aMutex );
190 0 : OrderCreator aOrderCreator;
191 0 : aOrderCreator = ::std::for_each(m_aOrders.begin(),m_aOrders.end(),aOrderCreator);
192 0 : return aOrderCreator.getComposedAndClear();
193 : }
194 :
195 0 : void SAL_CALL OQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column ) throw(SQLException, RuntimeException, std::exception)
196 : {
197 : SAL_INFO("dbaccess", "OQueryComposer::appendFilterByColumn" );
198 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
199 0 : ::osl::MutexGuard aGuard( m_aMutex );
200 :
201 0 : m_xComposerHelper->setQuery(getQuery());
202 0 : m_xComposerHelper->setFilter(OUString());
203 0 : m_xComposerHelper->appendFilterByColumn(column, sal_True, SQLFilterOperator::EQUAL);
204 :
205 0 : FilterCreator aFilterCreator;
206 0 : aFilterCreator.append(getFilter());
207 0 : aFilterCreator.append(m_xComposerHelper->getFilter());
208 :
209 0 : setFilter( aFilterCreator.getComposedAndClear() );
210 0 : }
211 :
212 0 : void SAL_CALL OQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException, std::exception)
213 : {
214 : SAL_INFO("dbaccess", "OQueryComposer::appendOrderByColumn" );
215 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
216 0 : ::osl::MutexGuard aGuard( m_aMutex );
217 :
218 0 : m_xComposerHelper->setQuery(getQuery());
219 0 : m_xComposerHelper->setOrder(OUString());
220 0 : m_xComposerHelper->appendOrderByColumn(column,ascending);
221 :
222 0 : OrderCreator aOrderCreator;
223 0 : aOrderCreator.append(getOrder());
224 0 : aOrderCreator.append(m_xComposerHelper->getOrder());
225 :
226 0 : setOrder(aOrderCreator.getComposedAndClear());
227 0 : }
228 :
229 0 : void SAL_CALL OQueryComposer::setFilter( const OUString& filter ) throw(SQLException, RuntimeException, std::exception)
230 : {
231 : SAL_INFO("dbaccess", "OQueryComposer::setFilter" );
232 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
233 :
234 0 : ::osl::MutexGuard aGuard( m_aMutex );
235 0 : FilterCreator aFilterCreator;
236 0 : aFilterCreator.append(m_sOrgFilter);
237 0 : aFilterCreator.append(filter);
238 :
239 0 : m_aFilters.clear();
240 0 : if ( !filter.isEmpty() )
241 0 : m_aFilters.push_back(filter);
242 :
243 0 : m_xComposer->setFilter( aFilterCreator.getComposedAndClear() );
244 0 : }
245 :
246 0 : void SAL_CALL OQueryComposer::setOrder( const OUString& order ) throw(SQLException, RuntimeException, std::exception)
247 : {
248 : SAL_INFO("dbaccess", "OQueryComposer::setOrder" );
249 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
250 :
251 0 : ::osl::MutexGuard aGuard( m_aMutex );
252 :
253 0 : OrderCreator aOrderCreator;
254 0 : aOrderCreator.append(m_sOrgOrder);
255 0 : aOrderCreator.append(order);
256 :
257 0 : m_aOrders.clear();
258 0 : if ( !order.isEmpty() )
259 0 : m_aOrders.push_back(order);
260 :
261 0 : m_xComposer->setOrder(aOrderCreator.getComposedAndClear());
262 0 : }
263 :
264 : // XTablesSupplier
265 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getTables( ) throw(RuntimeException, std::exception)
266 : {
267 : SAL_INFO("dbaccess", "OQueryComposer::getTables" );
268 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
269 :
270 0 : ::osl::MutexGuard aGuard( m_aMutex );
271 0 : return Reference<XTablesSupplier>(m_xComposer,UNO_QUERY)->getTables();
272 : }
273 :
274 : // XColumnsSupplier
275 0 : Reference< XNameAccess > SAL_CALL OQueryComposer::getColumns( ) throw(RuntimeException, std::exception)
276 : {
277 : SAL_INFO("dbaccess", "OQueryComposer::getColumns" );
278 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
279 :
280 0 : ::osl::MutexGuard aGuard( m_aMutex );
281 0 : return Reference<XColumnsSupplier>(m_xComposer,UNO_QUERY)->getColumns();
282 : }
283 :
284 0 : Reference< XIndexAccess > SAL_CALL OQueryComposer::getParameters( ) throw(RuntimeException, std::exception)
285 : {
286 : SAL_INFO("dbaccess", "OQueryComposer::getParameters" );
287 0 : ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
288 :
289 0 : ::osl::MutexGuard aGuard( m_aMutex );
290 0 : return Reference<XParametersSupplier>(m_xComposer,UNO_QUERY)->getParameters();
291 : }
292 :
293 0 : void SAL_CALL OQueryComposer::acquire() throw()
294 : {
295 0 : OSubComponent::acquire();
296 0 : }
297 :
298 0 : void SAL_CALL OQueryComposer::release() throw()
299 : {
300 0 : OSubComponent::release();
301 0 : }
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|