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 "query.hxx"
21 : #include "dbastrings.hrc"
22 : #include <connectivity/warningscontainer.hxx>
23 : #include "HelperCollections.hxx"
24 : #include "core_resource.hxx"
25 : #include "core_resource.hrc"
26 :
27 : #include <cppuhelper/queryinterface.hxx>
28 : #include <tools/debug.hxx>
29 : #include <tools/diagnose_ex.h>
30 : #include <osl/diagnose.h>
31 : #include <comphelper/propagg.hxx>
32 : #include <comphelper/sequence.hxx>
33 :
34 : #include <com/sun/star/sdbc/XConnection.hpp>
35 : #include <com/sun/star/lang/DisposedException.hpp>
36 : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
37 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
38 :
39 : #include <comphelper/types.hxx>
40 : #include <comphelper/property.hxx>
41 : #include <unotools/sharedunocomponent.hxx>
42 : #include "definitioncolumn.hxx"
43 :
44 : #include <functional>
45 :
46 : #include "sdbcoretools.hxx"
47 : #include "querycomposer.hxx"
48 : #include <com/sun/star/beans/PropertyAttribute.hpp>
49 : #include "ContainerMediator.hxx"
50 :
51 : using namespace dbaccess;
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::sdbc;
54 : using namespace ::com::sun::star::sdbcx;
55 : using namespace ::com::sun::star::sdb;
56 : using namespace ::com::sun::star::lang;
57 : using namespace ::com::sun::star::util;
58 : using namespace ::com::sun::star::beans;
59 : using namespace ::com::sun::star::container;
60 : using namespace ::comphelper;
61 : using namespace ::osl;
62 : using namespace ::cppu;
63 : using namespace ::utl;
64 :
65 : namespace dbaccess
66 : {
67 :
68 : // OQuery
69 :
70 1 : OQuery::OQuery( const Reference< XPropertySet >& _rxCommandDefinition
71 : ,const Reference< XConnection >& _rxConn
72 : ,const Reference< XComponentContext >& _xORB)
73 1 : :OContentHelper(_xORB,NULL,TContentPtr(new OContentHelper_Impl))
74 : ,OQueryDescriptor_Base(m_aMutex,*this)
75 : ,ODataSettings(OContentHelper::rBHelper,true)
76 : ,m_xCommandDefinition(_rxCommandDefinition)
77 : ,m_xConnection(_rxConn)
78 : ,m_pColumnMediator( NULL )
79 : ,m_pWarnings( NULL )
80 : ,m_bCaseSensitiv(true)
81 2 : ,m_eDoingCurrently(NONE)
82 : {
83 1 : registerProperties();
84 1 : ODataSettings::registerPropertiesFor(this);
85 :
86 1 : osl_atomic_increment(&m_refCount);
87 : OSL_ENSURE(m_xCommandDefinition.is(), "OQuery::OQuery : invalid CommandDefinition object !");
88 1 : if ( m_xCommandDefinition.is() )
89 : {
90 : try
91 : {
92 1 : ::comphelper::copyProperties(_rxCommandDefinition,this);
93 : }
94 0 : catch(Exception&)
95 : {
96 : OSL_FAIL("OQueryDescriptor_Base::OQueryDescriptor_Base: caught an exception!");
97 : }
98 :
99 1 : m_xCommandDefinition->addPropertyChangeListener(OUString(), this);
100 : // m_xCommandDefinition->addPropertyChangeListener(PROPERTY_NAME, this);
101 1 : m_xCommandPropInfo = m_xCommandDefinition->getPropertySetInfo();
102 : }
103 : OSL_ENSURE(m_xConnection.is(), "OQuery::OQuery : invalid connection !");
104 1 : osl_atomic_decrement(&m_refCount);
105 1 : }
106 :
107 2 : OQuery::~OQuery()
108 : {
109 2 : }
110 :
111 0 : css::uno::Sequence<sal_Int8> OQuery::getImplementationId()
112 : throw (css::uno::RuntimeException, std::exception)
113 : {
114 0 : return css::uno::Sequence<sal_Int8>();
115 : }
116 :
117 0 : IMPLEMENT_GETTYPES3(OQuery,OQueryDescriptor_Base,ODataSettings,OContentHelper);
118 213 : IMPLEMENT_FORWARD_XINTERFACE3( OQuery,OContentHelper,OQueryDescriptor_Base,ODataSettings)
119 :
120 1 : void OQuery::rebuildColumns()
121 : {
122 : OSL_PRECOND( getColumnCount() == 0, "OQuery::rebuildColumns: column container should be empty!" );
123 : // the base class' definition of rebuildColumns promised that clearColumns is called before rebuildColumns
124 :
125 : try
126 : {
127 1 : m_pColumnMediator = NULL;
128 :
129 1 : Reference<XColumnsSupplier> xColSup(m_xCommandDefinition,UNO_QUERY);
130 2 : Reference< XNameAccess > xColumnDefinitions;
131 1 : if ( xColSup.is() )
132 : {
133 1 : xColumnDefinitions = xColSup->getColumns();
134 1 : if ( xColumnDefinitions.is() )
135 1 : m_pColumnMediator = new OContainerMediator( m_pColumns, xColumnDefinitions, m_xConnection );
136 : }
137 :
138 : // fill the columns with columns from the statement
139 2 : Reference< XMultiServiceFactory > xFactory( m_xConnection, UNO_QUERY_THROW );
140 : SharedUNOComponent< XSingleSelectQueryComposer, DisposableComponent > xComposer(
141 2 : Reference< XSingleSelectQueryComposer >( xFactory->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW ) );
142 :
143 2 : Reference< XNameAccess > xColumns;
144 2 : Reference< XIndexAccess > xColumnsIndexed;
145 : try
146 : {
147 1 : xComposer->setQuery( m_sCommand );
148 1 : Reference< XColumnsSupplier > xCols( xComposer, UNO_QUERY_THROW );
149 1 : xColumns.set( xCols->getColumns(), UNO_QUERY_THROW );
150 1 : xColumnsIndexed.set( xColumns, UNO_QUERY_THROW );
151 : }
152 0 : catch( const SQLException& ) { }
153 :
154 2 : SharedUNOComponent< XPreparedStatement, DisposableComponent > xPreparedStatement;
155 1 : if ( !xColumns.is() || ( xColumnsIndexed->getCount() == 0 ) )
156 : { // the QueryComposer could not parse it. Try a lean version.
157 0 : xPreparedStatement.set( m_xConnection->prepareStatement( m_sCommand ), UNO_QUERY_THROW );
158 0 : Reference< XResultSetMetaDataSupplier > xResMetaDataSup( xPreparedStatement, UNO_QUERY_THROW );
159 0 : Reference< XResultSetMetaData > xResultSetMeta( xResMetaDataSup->getMetaData() );
160 0 : if ( !xResultSetMeta.is() )
161 : {
162 0 : OUString sError( DBA_RES( RID_STR_STATEMENT_WITHOUT_RESULT_SET ) );
163 0 : ::dbtools::throwSQLException( sError, SQL_GENERAL_ERROR, *this );
164 : }
165 :
166 0 : Reference< XDatabaseMetaData > xDBMeta( m_xConnection->getMetaData(), UNO_QUERY_THROW );
167 : ::rtl::Reference< OSQLColumns > aParseColumns(
168 0 : ::connectivity::parse::OParseColumn::createColumnsForResultSet( xResultSetMeta, xDBMeta,xColumnDefinitions ) );
169 0 : xColumns = OPrivateColumns::createWithIntrinsicNames(
170 0 : aParseColumns, xDBMeta->supportsMixedCaseQuotedIdentifiers(), *this, m_aMutex );
171 0 : if ( !xColumns.is() )
172 0 : throw RuntimeException();
173 : }
174 :
175 2 : Sequence< OUString> aNames = xColumns->getElementNames();
176 1 : const OUString* pIter = aNames.getConstArray();
177 1 : const OUString* pEnd = pIter + aNames.getLength();
178 4 : for ( sal_Int32 i = 0;pIter != pEnd; ++pIter,++i)
179 : {
180 3 : Reference<XPropertySet> xSource(xColumns->getByName( *pIter ),UNO_QUERY);
181 6 : OUString sLabel = *pIter;
182 3 : if ( xColumnDefinitions.is() && xColumnDefinitions->hasByName(*pIter) )
183 : {
184 0 : Reference<XPropertySet> xCommandColumn(xColumnDefinitions->getByName( *pIter ),UNO_QUERY);
185 0 : xCommandColumn->getPropertyValue(PROPERTY_LABEL) >>= sLabel;
186 : }
187 3 : OQueryColumn* pColumn = new OQueryColumn( xSource, m_xConnection, sLabel);
188 6 : Reference< XChild > xChild( *pColumn, UNO_QUERY_THROW );
189 3 : xChild->setParent( *this );
190 :
191 3 : implAppendColumn( *pIter, pColumn );
192 6 : Reference< XPropertySet > xDest( *pColumn, UNO_QUERY_THROW );
193 3 : if ( m_pColumnMediator.is() )
194 3 : m_pColumnMediator->notifyElementCreated( *pIter, xDest );
195 4 : }
196 : }
197 0 : catch( const SQLContext& e )
198 : {
199 0 : if ( m_pWarnings )
200 0 : m_pWarnings->appendWarning( e );
201 : }
202 0 : catch( const SQLWarning& e )
203 : {
204 0 : if ( m_pWarnings )
205 0 : m_pWarnings->appendWarning( e );
206 : }
207 0 : catch( const SQLException& e )
208 : {
209 0 : if ( m_pWarnings )
210 0 : m_pWarnings->appendWarning( e );
211 : }
212 0 : catch( const Exception& )
213 : {
214 : DBG_UNHANDLED_EXCEPTION();
215 : }
216 1 : }
217 :
218 : // XServiceInfo
219 0 : IMPLEMENT_SERVICE_INFO3(OQuery, "com.sun.star.sdb.dbaccess.OQuery", SERVICE_SDB_DATASETTINGS, SERVICE_SDB_QUERY, "com.sun.star.sdb.QueryDefinition")
220 :
221 : // ::com::sun::star::beans::XPropertyChangeListener
222 0 : void SAL_CALL OQuery::propertyChange( const PropertyChangeEvent& _rSource ) throw(RuntimeException, std::exception)
223 : {
224 0 : sal_Int32 nOwnHandle = -1;
225 : {
226 0 : MutexGuard aGuard(m_aMutex);
227 :
228 : OSL_ENSURE(_rSource.Source.get() == Reference< XInterface >(m_xCommandDefinition, UNO_QUERY).get(),
229 : "OQuery::propertyChange : where did this call come from ?");
230 :
231 0 : if (m_eDoingCurrently == SETTING_PROPERTIES)
232 : // we're setting the property ourself, so we will do the necessary notifications later
233 0 : return;
234 :
235 : // forward this to our own member holding a copy of the property value
236 0 : if (getArrayHelper()->hasPropertyByName(_rSource.PropertyName))
237 : {
238 0 : Property aOwnProp = getArrayHelper()->getPropertyByName(_rSource.PropertyName);
239 0 : nOwnHandle = aOwnProp.Handle;
240 0 : ODataSettings::setFastPropertyValue_NoBroadcast(nOwnHandle, _rSource.NewValue);
241 : // don't use our own setFastPropertyValue_NoBroadcast, this would forward it to the CommandSettings,
242 : // again
243 : // and don't use the "real" setPropertyValue, this is to expensive and not sure to succeed
244 : }
245 : else
246 : {
247 : OSL_FAIL("OQuery::propertyChange : my CommandDefinition has more properties than I do !");
248 0 : }
249 : }
250 :
251 0 : fire(&nOwnHandle, &_rSource.NewValue, &_rSource.OldValue, 1, sal_False);
252 : }
253 :
254 0 : void SAL_CALL OQuery::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
255 : {
256 0 : MutexGuard aGuard(m_aMutex);
257 :
258 : (void)_rSource;
259 : OSL_ENSURE(_rSource.Source.get() == Reference< XInterface >(m_xCommandDefinition, UNO_QUERY).get(),
260 : "OQuery::disposing : where did this call come from ?");
261 :
262 0 : m_xCommandDefinition->removePropertyChangeListener(OUString(), this);
263 0 : m_xCommandDefinition = NULL;
264 0 : }
265 :
266 : // XDataDescriptorFactory
267 0 : Reference< XPropertySet > SAL_CALL OQuery::createDataDescriptor( ) throw(RuntimeException, std::exception)
268 : {
269 0 : return new OQueryDescriptor(*this);
270 : }
271 :
272 : // pseudo-XComponent
273 1 : void SAL_CALL OQuery::disposing()
274 : {
275 1 : MutexGuard aGuard(m_aMutex);
276 1 : if (m_xCommandDefinition.is())
277 : {
278 1 : m_xCommandDefinition->removePropertyChangeListener(OUString(), this);
279 1 : m_xCommandDefinition = NULL;
280 : }
281 1 : disposeColumns();
282 :
283 1 : m_pWarnings = NULL;
284 1 : }
285 :
286 2 : void OQuery::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception, std::exception)
287 : {
288 2 : ODataSettings::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
289 2 : OUString sAggPropName;
290 2 : sal_Int16 nAttr = 0;
291 6 : if (getInfoHelper().fillPropertyMembersByHandle(&sAggPropName,&nAttr,_nHandle) &&
292 2 : m_xCommandPropInfo.is() &&
293 0 : m_xCommandPropInfo->hasPropertyByName(sAggPropName))
294 : { // the base class holds the property values itself, but we have to forward this to our CommandDefinition
295 :
296 0 : m_eDoingCurrently = SETTING_PROPERTIES;
297 0 : OAutoActionReset aAutoReset(this);
298 0 : m_xCommandDefinition->setPropertyValue(sAggPropName, _rValue);
299 :
300 0 : if ( PROPERTY_ID_COMMAND == _nHandle )
301 : // the columns are out of date if we are based on a new statement ....
302 0 : setColumnsOutOfDate();
303 2 : }
304 2 : }
305 :
306 1 : Reference< XPropertySetInfo > SAL_CALL OQuery::getPropertySetInfo( ) throw(RuntimeException, std::exception)
307 : {
308 1 : return createPropertySetInfo( getInfoHelper() ) ;
309 : }
310 :
311 102 : ::cppu::IPropertyArrayHelper& OQuery::getInfoHelper()
312 : {
313 102 : return *getArrayHelper();
314 : }
315 :
316 1 : ::cppu::IPropertyArrayHelper* OQuery::createArrayHelper( ) const
317 : {
318 1 : Sequence< Property > aProps;
319 : // our own props
320 1 : describeProperties(aProps);
321 1 : return new ::cppu::OPropertyArrayHelper(aProps);
322 : }
323 :
324 0 : OColumn* OQuery::createColumn(const OUString& /*_rName*/) const
325 : {
326 0 : return NULL;
327 : }
328 :
329 0 : void SAL_CALL OQuery::rename( const OUString& newName ) throw (SQLException, ElementExistException, RuntimeException, std::exception)
330 : {
331 0 : MutexGuard aGuard(m_aMutex);
332 0 : Reference<XRename> xRename(m_xCommandDefinition,UNO_QUERY);
333 : OSL_ENSURE(xRename.is(),"No XRename interface!");
334 0 : if(xRename.is())
335 0 : xRename->rename(newName);
336 0 : }
337 :
338 1 : void OQuery::registerProperties()
339 : {
340 : // the properties which OCommandBase supplies (it has no own registration, as it's not derived from
341 : // a OPropertyStateContainer)
342 : registerProperty(PROPERTY_NAME, PROPERTY_ID_NAME, PropertyAttribute::BOUND|PropertyAttribute::CONSTRAINED,
343 1 : &m_sElementName, cppu::UnoType<decltype(m_sElementName)>::get());
344 :
345 : registerProperty(PROPERTY_COMMAND, PROPERTY_ID_COMMAND, PropertyAttribute::BOUND,
346 1 : &m_sCommand, cppu::UnoType<decltype(m_sCommand)>::get());
347 :
348 : registerProperty(PROPERTY_ESCAPE_PROCESSING, PROPERTY_ID_ESCAPE_PROCESSING, PropertyAttribute::BOUND,
349 1 : &m_bEscapeProcessing, cppu::UnoType<bool>::get());
350 :
351 : registerProperty(PROPERTY_UPDATE_TABLENAME, PROPERTY_ID_UPDATE_TABLENAME, PropertyAttribute::BOUND,
352 1 : &m_sUpdateTableName, cppu::UnoType<decltype(m_sUpdateTableName)>::get());
353 :
354 : registerProperty(PROPERTY_UPDATE_SCHEMANAME, PROPERTY_ID_UPDATE_SCHEMANAME, PropertyAttribute::BOUND,
355 1 : &m_sUpdateSchemaName, cppu::UnoType<decltype(m_sUpdateSchemaName)>::get());
356 :
357 : registerProperty(PROPERTY_UPDATE_CATALOGNAME, PROPERTY_ID_UPDATE_CATALOGNAME, PropertyAttribute::BOUND,
358 1 : &m_sUpdateCatalogName, cppu::UnoType<decltype(m_sUpdateCatalogName)>::get());
359 :
360 : registerProperty(PROPERTY_LAYOUTINFORMATION, PROPERTY_ID_LAYOUTINFORMATION, PropertyAttribute::BOUND,
361 1 : &m_aLayoutInformation, cppu::UnoType<decltype(m_aLayoutInformation)>::get());
362 1 : }
363 :
364 0 : OUString OQuery::determineContentType() const
365 : {
366 0 : return OUString( "application/vnd.org.openoffice.DatabaseQuery" );
367 : }
368 :
369 : } // namespace dbaccess
370 :
371 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|