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