1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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"
45 #include <rtl/logfile.hxx>
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
;
65 DBG_NAME(OQueryComposer
)
67 OQueryComposer::OQueryComposer(const Reference
< XConnection
>& _xConnection
)
68 : OSubComponent(m_aMutex
,_xConnection
)
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!");
74 Reference
<XMultiServiceFactory
> xFac( _xConnection
, UNO_QUERY_THROW
);
75 m_xComposer
.set( xFac
->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER
), UNO_QUERY_THROW
);
76 m_xComposerHelper
.set( xFac
->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER
), UNO_QUERY_THROW
);
79 OQueryComposer::~OQueryComposer()
81 DBG_DTOR(OQueryComposer
,NULL
);
84 void SAL_CALL
OQueryComposer::disposing()
86 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::disposing" );
87 ::comphelper::disposeComponent(m_xComposerHelper
);
88 ::comphelper::disposeComponent(m_xComposer
);
91 // ::com::sun::star::lang::XTypeProvider
92 Sequence
< Type
> SAL_CALL
OQueryComposer::getTypes() throw (RuntimeException
)
94 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTypes" );
95 return ::comphelper::concatSequences(OSubComponent::getTypes(),OQueryComposer_BASE::getTypes());
98 Sequence
< sal_Int8
> SAL_CALL
OQueryComposer::getImplementationId() throw (RuntimeException
)
100 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationId" );
101 static OImplementationId
* pId
= 0;
104 MutexGuard
aGuard( Mutex::getGlobalMutex() );
107 static OImplementationId aId
;
111 return pId
->getImplementationId();
114 // com::sun::star::lang::XUnoTunnel
115 sal_Int64 SAL_CALL
OQueryComposer::getSomething( const Sequence
< sal_Int8
>& rId
) throw(RuntimeException
)
117 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSomething" );
118 if (rId
.getLength() == 16 && 0 == memcmp(getImplementationId().getConstArray(), rId
.getConstArray(), 16 ) )
119 return reinterpret_cast<sal_Int64
>(this);
124 Any SAL_CALL
OQueryComposer::queryInterface( const Type
& rType
) throw(RuntimeException
)
126 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::queryInterface" );
127 Any aRet
= OSubComponent::queryInterface(rType
);
129 aRet
= OQueryComposer_BASE::queryInterface(rType
);
134 rtl::OUString
OQueryComposer::getImplementationName( ) throw(RuntimeException
)
136 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getImplementationName" );
137 return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.dbaccess.OQueryComposer"));
140 sal_Bool
OQueryComposer::supportsService( const ::rtl::OUString
& _rServiceName
) throw (RuntimeException
)
142 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::supportsService" );
143 return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName
, sal_True
).getLength() != 0;
146 Sequence
< ::rtl::OUString
> OQueryComposer::getSupportedServiceNames( ) throw (RuntimeException
)
148 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getSupportedServiceNames" );
149 Sequence
< rtl::OUString
> aSNS( 1 );
150 aSNS
[0] = SERVICE_SDB_SQLQUERYCOMPOSER
;
155 ::rtl::OUString SAL_CALL
OQueryComposer::getQuery( ) throw(RuntimeException
)
157 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getQuery" );
158 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
160 ::osl::MutexGuard
aGuard( m_aMutex
);
161 Reference
<XPropertySet
> xProp(m_xComposer
,UNO_QUERY
);
162 ::rtl::OUString sQuery
;
164 xProp
->getPropertyValue(PROPERTY_ORIGINAL
) >>= sQuery
;
168 void SAL_CALL
OQueryComposer::setQuery( const ::rtl::OUString
& command
) throw(SQLException
, RuntimeException
)
170 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setQuery" );
171 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
173 ::osl::MutexGuard
aGuard( m_aMutex
);
175 m_xComposer
->setQuery(command
);
176 m_sOrgFilter
= m_xComposer
->getFilter();
177 m_sOrgOrder
= m_xComposer
->getOrder();
180 ::rtl::OUString SAL_CALL
OQueryComposer::getComposedQuery( ) throw(RuntimeException
)
182 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getComposedQuery" );
183 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
185 MutexGuard
aGuard(m_aMutex
);
187 return m_xComposer
->getQuery();
190 ::rtl::OUString SAL_CALL
OQueryComposer::getFilter( ) throw(RuntimeException
)
192 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getFilter" );
193 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
194 MutexGuard
aGuard(m_aMutex
);
195 FilterCreator aFilterCreator
;
196 aFilterCreator
= ::std::for_each(m_aFilters
.begin(),m_aFilters
.end(),aFilterCreator
);
197 return aFilterCreator
.getComposedAndClear();
200 Sequence
< Sequence
< PropertyValue
> > SAL_CALL
OQueryComposer::getStructuredFilter( ) throw(RuntimeException
)
202 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getStructuredFilter" );
203 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
205 MutexGuard
aGuard(m_aMutex
);
206 return m_xComposer
->getStructuredFilter();
209 ::rtl::OUString SAL_CALL
OQueryComposer::getOrder( ) throw(RuntimeException
)
211 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getOrder" );
212 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
214 ::osl::MutexGuard
aGuard( m_aMutex
);
215 OrderCreator aOrderCreator
;
216 aOrderCreator
= ::std::for_each(m_aOrders
.begin(),m_aOrders
.end(),aOrderCreator
);
217 return aOrderCreator
.getComposedAndClear();
220 void SAL_CALL
OQueryComposer::appendFilterByColumn( const Reference
< XPropertySet
>& column
) throw(SQLException
, RuntimeException
)
222 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendFilterByColumn" );
223 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
224 ::osl::MutexGuard
aGuard( m_aMutex
);
226 m_xComposerHelper
->setQuery(getQuery());
227 m_xComposerHelper
->setFilter(::rtl::OUString());
228 m_xComposerHelper
->appendFilterByColumn(column
, sal_True
, SQLFilterOperator::EQUAL
);
230 FilterCreator aFilterCreator
;
231 aFilterCreator
.append(getFilter());
232 aFilterCreator
.append(m_xComposerHelper
->getFilter());
234 setFilter( aFilterCreator
.getComposedAndClear() );
237 void SAL_CALL
OQueryComposer::appendOrderByColumn( const Reference
< XPropertySet
>& column
, sal_Bool ascending
) throw(SQLException
, RuntimeException
)
239 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::appendOrderByColumn" );
240 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
241 ::osl::MutexGuard
aGuard( m_aMutex
);
243 m_xComposerHelper
->setQuery(getQuery());
244 m_xComposerHelper
->setOrder(::rtl::OUString());
245 m_xComposerHelper
->appendOrderByColumn(column
,ascending
);
247 OrderCreator aOrderCreator
;
248 aOrderCreator
.append(getOrder());
249 aOrderCreator
.append(m_xComposerHelper
->getOrder());
251 setOrder(aOrderCreator
.getComposedAndClear());
254 void SAL_CALL
OQueryComposer::setFilter( const ::rtl::OUString
& filter
) throw(SQLException
, RuntimeException
)
256 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setFilter" );
257 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
259 ::osl::MutexGuard
aGuard( m_aMutex
);
260 FilterCreator aFilterCreator
;
261 aFilterCreator
.append(m_sOrgFilter
);
262 aFilterCreator
.append(filter
);
265 if ( !filter
.isEmpty() )
266 m_aFilters
.push_back(filter
);
268 m_xComposer
->setFilter( aFilterCreator
.getComposedAndClear() );
271 void SAL_CALL
OQueryComposer::setOrder( const ::rtl::OUString
& order
) throw(SQLException
, RuntimeException
)
273 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::setOrder" );
274 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
276 ::osl::MutexGuard
aGuard( m_aMutex
);
278 OrderCreator aOrderCreator
;
279 aOrderCreator
.append(m_sOrgOrder
);
280 aOrderCreator
.append(order
);
283 if ( !order
.isEmpty() )
284 m_aOrders
.push_back(order
);
286 m_xComposer
->setOrder(aOrderCreator
.getComposedAndClear());
290 Reference
< XNameAccess
> SAL_CALL
OQueryComposer::getTables( ) throw(RuntimeException
)
292 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getTables" );
293 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
295 ::osl::MutexGuard
aGuard( m_aMutex
);
296 return Reference
<XTablesSupplier
>(m_xComposer
,UNO_QUERY
)->getTables();
300 Reference
< XNameAccess
> SAL_CALL
OQueryComposer::getColumns( ) throw(RuntimeException
)
302 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getColumns" );
303 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
305 ::osl::MutexGuard
aGuard( m_aMutex
);
306 return Reference
<XColumnsSupplier
>(m_xComposer
,UNO_QUERY
)->getColumns();
309 Reference
< XIndexAccess
> SAL_CALL
OQueryComposer::getParameters( ) throw(RuntimeException
)
311 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OQueryComposer::getParameters" );
312 ::connectivity::checkDisposed(OSubComponent::rBHelper
.bDisposed
);
314 ::osl::MutexGuard
aGuard( m_aMutex
);
315 return Reference
<XParametersSupplier
>(m_xComposer
,UNO_QUERY
)->getParameters();
318 void SAL_CALL
OQueryComposer::acquire() throw()
320 OSubComponent::acquire();
323 void SAL_CALL
OQueryComposer::release() throw()
325 OSubComponent::release();
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */