Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / querycomposer.cxx
blob1fdb99b6c4c99301910a5266ca7e6f31c7d6afdc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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;
66 OQueryComposer::OQueryComposer(const Reference< XConnection>& _xConnection)
67 : OSubComponent(m_aMutex,_xConnection)
69 OSL_ENSURE(_xConnection.is()," Connection can't be null!");
71 Reference<XMultiServiceFactory> xFac( _xConnection, UNO_QUERY_THROW );
72 m_xComposer.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
73 m_xComposerHelper.set( xFac->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
76 OQueryComposer::~OQueryComposer()
80 void SAL_CALL OQueryComposer::disposing()
82 ::comphelper::disposeComponent(m_xComposerHelper);
83 ::comphelper::disposeComponent(m_xComposer);
86 // ::com::sun::star::lang::XTypeProvider
87 Sequence< Type > SAL_CALL OQueryComposer::getTypes() throw (RuntimeException, std::exception)
89 return ::comphelper::concatSequences(OSubComponent::getTypes(),OQueryComposer_BASE::getTypes());
92 Sequence< sal_Int8 > SAL_CALL OQueryComposer::getImplementationId() throw (RuntimeException, std::exception)
94 return css::uno::Sequence<sal_Int8>();
97 Any SAL_CALL OQueryComposer::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
99 Any aRet = OSubComponent::queryInterface(rType);
100 if(!aRet.hasValue())
101 aRet = OQueryComposer_BASE::queryInterface(rType);
102 return aRet;
105 // XServiceInfo
106 OUString OQueryComposer::getImplementationName( ) throw(RuntimeException, std::exception)
108 return OUString("com.sun.star.sdb.dbaccess.OQueryComposer");
111 sal_Bool OQueryComposer::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
113 return cppu::supportsService(this, _rServiceName);
116 Sequence< OUString > OQueryComposer::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
118 Sequence< OUString > aSNS( 1 );
119 aSNS[0] = SERVICE_SDB_SQLQUERYCOMPOSER;
120 return aSNS;
123 // XSQLQueryComposer
124 OUString SAL_CALL OQueryComposer::getQuery( ) throw(RuntimeException, std::exception)
126 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
128 ::osl::MutexGuard aGuard( m_aMutex );
129 Reference<XPropertySet> xProp(m_xComposer,UNO_QUERY);
130 OUString sQuery;
131 if ( xProp.is() )
132 xProp->getPropertyValue(PROPERTY_ORIGINAL) >>= sQuery;
133 return sQuery;
136 void SAL_CALL OQueryComposer::setQuery( const OUString& command ) throw(SQLException, RuntimeException, std::exception)
138 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
140 ::osl::MutexGuard aGuard( m_aMutex );
141 m_aFilters.clear();
142 m_xComposer->setQuery(command);
143 m_sOrgFilter = m_xComposer->getFilter();
144 m_sOrgOrder = m_xComposer->getOrder();
147 OUString SAL_CALL OQueryComposer::getComposedQuery( ) throw(RuntimeException, std::exception)
149 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
151 MutexGuard aGuard(m_aMutex);
153 return m_xComposer->getQuery();
156 OUString SAL_CALL OQueryComposer::getFilter( ) throw(RuntimeException, std::exception)
158 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
159 MutexGuard aGuard(m_aMutex);
160 FilterCreator aFilterCreator;
161 aFilterCreator = ::std::for_each(m_aFilters.begin(),m_aFilters.end(),aFilterCreator);
162 return aFilterCreator.getComposedAndClear();
165 Sequence< Sequence< PropertyValue > > SAL_CALL OQueryComposer::getStructuredFilter( ) throw(RuntimeException, std::exception)
167 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
169 MutexGuard aGuard(m_aMutex);
170 return m_xComposer->getStructuredFilter();
173 OUString SAL_CALL OQueryComposer::getOrder( ) throw(RuntimeException, std::exception)
175 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
177 ::osl::MutexGuard aGuard( m_aMutex );
178 OrderCreator aOrderCreator;
179 aOrderCreator = ::std::for_each(m_aOrders.begin(),m_aOrders.end(),aOrderCreator);
180 return aOrderCreator.getComposedAndClear();
183 void SAL_CALL OQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column ) throw(SQLException, RuntimeException, std::exception)
185 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
186 ::osl::MutexGuard aGuard( m_aMutex );
188 m_xComposerHelper->setQuery(getQuery());
189 m_xComposerHelper->setFilter(OUString());
190 m_xComposerHelper->appendFilterByColumn(column, sal_True, SQLFilterOperator::EQUAL);
192 FilterCreator aFilterCreator;
193 aFilterCreator.append(getFilter());
194 aFilterCreator.append(m_xComposerHelper->getFilter());
196 setFilter( aFilterCreator.getComposedAndClear() );
199 void SAL_CALL OQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException, std::exception)
201 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
202 ::osl::MutexGuard aGuard( m_aMutex );
204 m_xComposerHelper->setQuery(getQuery());
205 m_xComposerHelper->setOrder(OUString());
206 m_xComposerHelper->appendOrderByColumn(column,ascending);
208 OrderCreator aOrderCreator;
209 aOrderCreator.append(getOrder());
210 aOrderCreator.append(m_xComposerHelper->getOrder());
212 setOrder(aOrderCreator.getComposedAndClear());
215 void SAL_CALL OQueryComposer::setFilter( const OUString& filter ) throw(SQLException, RuntimeException, std::exception)
217 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
219 ::osl::MutexGuard aGuard( m_aMutex );
220 FilterCreator aFilterCreator;
221 aFilterCreator.append(m_sOrgFilter);
222 aFilterCreator.append(filter);
224 m_aFilters.clear();
225 if ( !filter.isEmpty() )
226 m_aFilters.push_back(filter);
228 m_xComposer->setFilter( aFilterCreator.getComposedAndClear() );
231 void SAL_CALL OQueryComposer::setOrder( const OUString& order ) throw(SQLException, RuntimeException, std::exception)
233 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
235 ::osl::MutexGuard aGuard( m_aMutex );
237 OrderCreator aOrderCreator;
238 aOrderCreator.append(m_sOrgOrder);
239 aOrderCreator.append(order);
241 m_aOrders.clear();
242 if ( !order.isEmpty() )
243 m_aOrders.push_back(order);
245 m_xComposer->setOrder(aOrderCreator.getComposedAndClear());
248 // XTablesSupplier
249 Reference< XNameAccess > SAL_CALL OQueryComposer::getTables( ) throw(RuntimeException, std::exception)
251 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
253 ::osl::MutexGuard aGuard( m_aMutex );
254 return Reference<XTablesSupplier>(m_xComposer,UNO_QUERY)->getTables();
257 // XColumnsSupplier
258 Reference< XNameAccess > SAL_CALL OQueryComposer::getColumns( ) throw(RuntimeException, std::exception)
260 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
262 ::osl::MutexGuard aGuard( m_aMutex );
263 return Reference<XColumnsSupplier>(m_xComposer,UNO_QUERY)->getColumns();
266 Reference< XIndexAccess > SAL_CALL OQueryComposer::getParameters( ) throw(RuntimeException, std::exception)
268 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
270 ::osl::MutexGuard aGuard( m_aMutex );
271 return Reference<XParametersSupplier>(m_xComposer,UNO_QUERY)->getParameters();
274 void SAL_CALL OQueryComposer::acquire() throw()
276 OSubComponent::acquire();
279 void SAL_CALL OQueryComposer::release() throw()
281 OSubComponent::release();
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */