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 0 : OQuery::OQuery( const Reference< XPropertySet >& _rxCommandDefinition
71 : ,const Reference< XConnection >& _rxConn
72 : ,const Reference< XComponentContext >& _xORB)
73 0 : :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 0 : ,m_eDoingCurrently(NONE)
82 : {
83 0 : registerProperties();
84 0 : ODataSettings::registerPropertiesFor(this);
85 :
86 0 : osl_atomic_increment(&m_refCount);
87 : OSL_ENSURE(m_xCommandDefinition.is(), "OQuery::OQuery : invalid CommandDefinition object !");
88 0 : if ( m_xCommandDefinition.is() )
89 : {
90 : try
91 : {
92 0 : ::comphelper::copyProperties(_rxCommandDefinition,this);
93 : }
94 0 : catch(Exception&)
95 : {
96 : OSL_FAIL("OQueryDescriptor_Base::OQueryDescriptor_Base: caught an exception!");
97 : }
98 :
99 0 : m_xCommandDefinition->addPropertyChangeListener(OUString(), this);
100 : // m_xCommandDefinition->addPropertyChangeListener(PROPERTY_NAME, this);
101 0 : m_xCommandPropInfo = m_xCommandDefinition->getPropertySetInfo();
102 : }
103 : OSL_ENSURE(m_xConnection.is(), "OQuery::OQuery : invalid connection !");
104 0 : osl_atomic_decrement(&m_refCount);
105 0 : }
106 :
107 0 : OQuery::~OQuery()
108 : {
109 0 : }
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 0 : IMPLEMENT_FORWARD_XINTERFACE3( OQuery,OContentHelper,OQueryDescriptor_Base,ODataSettings)
119 :
120 0 : 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 0 : m_pColumnMediator = NULL;
128 :
129 0 : Reference<XColumnsSupplier> xColSup(m_xCommandDefinition,UNO_QUERY);
130 0 : Reference< XNameAccess > xColumnDefinitions;
131 0 : if ( xColSup.is() )
132 : {
133 0 : xColumnDefinitions = xColSup->getColumns();
134 0 : if ( xColumnDefinitions.is() )
135 0 : m_pColumnMediator = new OContainerMediator( m_pColumns, xColumnDefinitions, m_xConnection );
136 : }
137 :
138 : // fill the columns with columns from the statement
139 0 : Reference< XMultiServiceFactory > xFactory( m_xConnection, UNO_QUERY_THROW );
140 : SharedUNOComponent< XSingleSelectQueryComposer, DisposableComponent > xComposer(
141 0 : Reference< XSingleSelectQueryComposer >( xFactory->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW ) );
142 :
143 0 : Reference< XNameAccess > xColumns;
144 0 : Reference< XIndexAccess > xColumnsIndexed;
145 : try
146 : {
147 0 : xComposer->setQuery( m_sCommand );
148 0 : Reference< XColumnsSupplier > xCols( xComposer, UNO_QUERY_THROW );
149 0 : xColumns.set( xCols->getColumns(), UNO_QUERY_THROW );
150 0 : xColumnsIndexed.set( xColumns, UNO_QUERY_THROW );
151 : }
152 0 : catch( const SQLException& ) { }
153 :
154 0 : SharedUNOComponent< XPreparedStatement, DisposableComponent > xPreparedStatement;
155 0 : 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 0 : Sequence< OUString> aNames = xColumns->getElementNames();
176 0 : const OUString* pIter = aNames.getConstArray();
177 0 : const OUString* pEnd = pIter + aNames.getLength();
178 0 : for ( sal_Int32 i = 0;pIter != pEnd; ++pIter,++i)
179 : {
180 0 : Reference<XPropertySet> xSource(xColumns->getByName( *pIter ),UNO_QUERY);
181 0 : OUString sLabel = *pIter;
182 0 : 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 0 : OQueryColumn* pColumn = new OQueryColumn( xSource, m_xConnection, sLabel);
188 0 : Reference< XChild > xChild( *pColumn, UNO_QUERY_THROW );
189 0 : xChild->setParent( *this );
190 :
191 0 : implAppendColumn( *pIter, pColumn );
192 0 : Reference< XPropertySet > xDest( *pColumn, UNO_QUERY_THROW );
193 0 : if ( m_pColumnMediator.is() )
194 0 : m_pColumnMediator->notifyElementCreated( *pIter, xDest );
195 0 : }
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 0 : }
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 0 : void SAL_CALL OQuery::disposing()
274 : {
275 0 : MutexGuard aGuard(m_aMutex);
276 0 : if (m_xCommandDefinition.is())
277 : {
278 0 : m_xCommandDefinition->removePropertyChangeListener(OUString(), this);
279 0 : m_xCommandDefinition = NULL;
280 : }
281 0 : disposeColumns();
282 :
283 0 : m_pWarnings = NULL;
284 0 : }
285 :
286 0 : void OQuery::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception, std::exception)
287 : {
288 0 : ODataSettings::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
289 0 : OUString sAggPropName;
290 0 : sal_Int16 nAttr = 0;
291 0 : if (getInfoHelper().fillPropertyMembersByHandle(&sAggPropName,&nAttr,_nHandle) &&
292 0 : 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 0 : }
304 0 : }
305 :
306 0 : Reference< XPropertySetInfo > SAL_CALL OQuery::getPropertySetInfo( ) throw(RuntimeException, std::exception)
307 : {
308 0 : return createPropertySetInfo( getInfoHelper() ) ;
309 : }
310 :
311 0 : ::cppu::IPropertyArrayHelper& OQuery::getInfoHelper()
312 : {
313 0 : return *getArrayHelper();
314 : }
315 :
316 0 : ::cppu::IPropertyArrayHelper* OQuery::createArrayHelper( ) const
317 : {
318 0 : Sequence< Property > aProps;
319 : // our own props
320 0 : describeProperties(aProps);
321 0 : 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 0 : 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 0 : &m_sElementName, ::getCppuType(&m_sElementName));
344 :
345 : registerProperty(PROPERTY_COMMAND, PROPERTY_ID_COMMAND, PropertyAttribute::BOUND,
346 0 : &m_sCommand, ::getCppuType(&m_sCommand));
347 :
348 : registerProperty(PROPERTY_ESCAPE_PROCESSING, PROPERTY_ID_ESCAPE_PROCESSING, PropertyAttribute::BOUND,
349 0 : &m_bEscapeProcessing, ::getBooleanCppuType());
350 :
351 : registerProperty(PROPERTY_UPDATE_TABLENAME, PROPERTY_ID_UPDATE_TABLENAME, PropertyAttribute::BOUND,
352 0 : &m_sUpdateTableName, ::getCppuType(&m_sUpdateTableName));
353 :
354 : registerProperty(PROPERTY_UPDATE_SCHEMANAME, PROPERTY_ID_UPDATE_SCHEMANAME, PropertyAttribute::BOUND,
355 0 : &m_sUpdateSchemaName, ::getCppuType(&m_sUpdateSchemaName));
356 :
357 : registerProperty(PROPERTY_UPDATE_CATALOGNAME, PROPERTY_ID_UPDATE_CATALOGNAME, PropertyAttribute::BOUND,
358 0 : &m_sUpdateCatalogName, ::getCppuType(&m_sUpdateCatalogName));
359 :
360 : registerProperty(PROPERTY_LAYOUTINFORMATION, PROPERTY_ID_LAYOUTINFORMATION, PropertyAttribute::BOUND,
361 0 : &m_aLayoutInformation, ::getCppuType(&m_aLayoutInformation));
362 0 : }
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: */
|