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 .
19 #ifndef CONNECTIVITY_PARAMETERS_HXX
20 #define CONNECTIVITY_PARAMETERS_HXX
25 #include <com/sun/star/uno/XAggregation.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/form/XDatabaseParameterListener.hpp>
28 #include <com/sun/star/sdbc/XConnection.hpp>
29 #include <com/sun/star/task/XInteractionHandler.hpp>
30 #include <com/sun/star/sdbc/XParameters.hpp>
31 #include <com/sun/star/container/XIndexAccess.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
35 #include "connectivity/dbtoolsdllapi.hxx"
36 #include "connectivity/paramwrapper.hxx"
37 #include <unotools/sharedunocomponent.hxx>
38 #include <comphelper/implementationreference.hxx>
39 #include <cppuhelper/interfacecontainer.hxx>
41 //........................................................................
44 //........................................................................
46 typedef ::utl::SharedUNOComponent
< ::com::sun::star::sdb::XSingleSelectQueryComposer
, ::utl::DisposableComponent
>
49 //====================================================================
51 //====================================================================
53 class OOO_DLLPUBLIC_DBTOOLS ParameterManager
56 /// classifies the origin of the data to fill a parameter
57 enum ParameterClassification
59 /** parameters which are filled from the master-detail relationship, where the detail
60 name is an explicit parameter name
63 /** parameters which are filled from the master-detail relationship, where the detail
64 name is a column name, so an implicit parameter had to be generated for it
67 /** parameters which are filled externally (i.e. by XParameters::setXXX, or by the parameter listeners)
71 /** meta data about an inner parameter
74 struct ParameterMetaData
76 /// the type of the parameter
77 ParameterClassification eType
;
78 /// the column object for this parameter, as returned by the query composer
79 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>
81 /// the indicies of inner parameters which need to be filled when this concrete parameter is set
82 ::std::vector
< sal_Int32
> aInnerIndexes
;
86 :eType( eFilledExternally
)
90 /// ctor with composer column
91 ParameterMetaData( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxColumn
)
92 :eType ( eFilledExternally
)
93 ,xComposerColumn ( _rxColumn
)
98 typedef ::std::map
< OUString
, ParameterMetaData
> ParameterInformation
;
101 ::osl::Mutex
& m_rMutex
;
102 ::cppu::OInterfaceContainerHelper m_aParameterListeners
;
104 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>
107 ::com::sun::star::uno::WeakReference
< ::com::sun::star::beans::XPropertySet
>
108 m_xComponent
; // the database component whose parameters we're handling
109 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XAggregation
>
110 m_xAggregatedRowSet
; // the aggregated row set - necessary for unwrapped access to some interfaces
111 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XParameters
>
112 m_xInnerParamUpdate
; // write access to the inner parameters
113 SharedQueryComposer m_xComposer
; // query composer wrapping the statement which the *aggregate* is based on
114 SharedQueryComposer m_xParentComposer
; // query composer wrapping the statement of our parent database component
115 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexAccess
>
116 m_xInnerParamColumns
; // index access to the parameter columns, as got from the query composer
118 ::dbtools::param::ParametersContainerRef
119 m_pOuterParameters
; // the container of parameters which still need to be filled in by
120 // external instances
121 sal_Int32 m_nInnerCount
; // overall number of parameters as required by the database component's aggregate
123 ParameterInformation m_aParameterInformation
;
125 ::com::sun::star::uno::Sequence
< OUString
> m_aMasterFields
;
126 ::com::sun::star::uno::Sequence
< OUString
> m_aDetailFields
;
128 OUString m_sIdentifierQuoteString
;
129 OUString m_sSpecialCharacters
;
131 ::std::vector
< bool > m_aParametersVisited
;
138 explicit ParameterManager(
139 ::osl::Mutex
& _rMutex
,
140 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
145 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxComponent
,
146 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XAggregation
>& _rxComponentAggregate
149 /// makes the object forgetting the references to the database component
152 /// clears the instance data
153 void clearAllParameterInformation();
155 /// checks whether the parameter information are up-to-date
156 inline bool isUpToDate() const { return m_bUpToDate
; }
158 /** updates all parameter information represented by the instance
160 void updateParameterInfo( FilterManager
& _rFilterManager
);
162 /** fills parameter values, as extensive as possible
164 <p>In particular, all values which can be filled from the master-detail relationship of
165 between our database component and it's parent are filled in.</p>
167 @param _rxCompletionHandler
168 an interaction handler which should be used to fill all parameters which
169 cannot be filled by other means. May be <NULL/>
170 @param _rClearForNotifies
171 the mutex guard to be (temporarily) cleared for notifications
174 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
177 <TRUE/> if and only if the parameter filling has <em>not</em> been cancelled by the user
179 bool fillParameterValues(
180 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& _rxCompletionHandler
,
181 ::osl::ResettableMutexGuard
& _rClearForNotifies
184 /** sets all parameter values to null (via <member>XParameters::setNull</member>)
187 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
189 void setAllParametersNull() SAL_THROW( ( ::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
) );
191 /** resets all detail columns which are, via a parameter, linked to a master column, to
192 the value of this master column.
194 For instance, if the database component is bound to a statement <code>SELECT * from invoice where inv_id = :cid</code>,
195 and there is <em>one</em> master-detail link from
198 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
200 void resetParameterValues() SAL_THROW(());
202 /** tells the object that it's database component is being disposed
204 The object then fires the <member>XEventListener::disposing</member> notification to
205 the parameter listeners
207 void disposing( const ::com::sun::star::lang::EventObject
& _rDisposingEvent
);
209 /** adds the given listener to the list of parameter listeners
211 void addParameterListener(
212 const ::com::sun::star::uno::Reference
< ::com::sun::star::form::XDatabaseParameterListener
>& _rxListener
215 /** removes the given listener from the list of parameter listeners
217 void removeParameterListener(
218 const ::com::sun::star::uno::Reference
< ::com::sun::star::form::XDatabaseParameterListener
>& _rxListener
221 // XParameters equivalents
222 void setNull ( sal_Int32 _nIndex
, sal_Int32 sqlType
);
223 void setObjectNull ( sal_Int32 _nIndex
, sal_Int32 sqlType
, const OUString
& typeName
);
224 void setBoolean ( sal_Int32 _nIndex
, sal_Bool x
);
225 void setByte ( sal_Int32 _nIndex
, sal_Int8 x
);
226 void setShort ( sal_Int32 _nIndex
, sal_Int16 x
);
227 void setInt ( sal_Int32 _nIndex
, sal_Int32 x
);
228 void setLong ( sal_Int32 _nIndex
, sal_Int64 x
);
229 void setFloat ( sal_Int32 _nIndex
, float x
);
230 void setDouble ( sal_Int32 _nIndex
, double x
);
231 void setString ( sal_Int32 _nIndex
, const OUString
& x
);
232 void setBytes ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Sequence
< sal_Int8
>& x
);
233 void setDate ( sal_Int32 _nIndex
, const ::com::sun::star::util::Date
& x
);
234 void setTime ( sal_Int32 _nIndex
, const ::com::sun::star::util::Time
& x
);
235 void setTimestamp ( sal_Int32 _nIndex
, const ::com::sun::star::util::DateTime
& x
);
236 void setBinaryStream ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
);
237 void setCharacterStream ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
);
238 void setObject ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Any
& x
);
239 void setObjectWithInfo ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Any
& x
, sal_Int32 targetSqlType
, sal_Int32 scale
);
240 void setRef ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XRef
>& x
);
241 void setBlob ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XBlob
>& x
);
242 void setClob ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XClob
>& x
);
243 void setArray ( sal_Int32 _nIndex
, const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XArray
>& x
);
244 void clearParameters();
247 /// checkes whether the object is already initialized, and not yet disposed
248 inline bool isAlive() const { return m_xComponent
.get().is() && m_xInnerParamUpdate
.is(); }
250 /** creates a filter expression from a master-detail link where the detail denotes a column name
253 createFilterConditionFromColumnLink(
254 const OUString
& /* [in] */ _rMasterColumn
,
255 const OUString
& /* [in] */ _rDetailColumn
,
256 OUString
& /* [out] */ _rNewParamName
259 /** initializes our query composer, and the collection of inner parameter columns
262 the database component to initialize from. Must not be <NULL/>
264 <TRUE/> if and only if the initialization was successful
267 if and only if <TRUE/> is returned, then <member>m_xInnerParamColumns</member> contains the collection of
270 bool initializeComposerByComponent(
271 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxComponent
274 /** collects initial meta information about inner parameters (i.e. it initially fills
275 <member>m_aParameterInformation</member>).
278 if <TRUE/>, this is the second run, because we ourself previously extended the filter of
282 <member>m_xInnerParamColumns</member> is not <NULL/>
284 void collectInnerParameters( bool _bSecondRun
);
286 /** analyzes the master-detail links for our database component, and initializes m_aMasterFields and m_aDetailFields
288 @param _rFilterManager
289 the filter manager of the database component
290 @param _rColumnsInLinkDetails
291 will be set to <TRUE/> if and only if there were link pairs where the detail field denoted
292 a column name of our database component
295 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
297 void analyzeFieldLinks( FilterManager
& _rFilterManager
, bool& /* [out] */ _rColumnsInLinkDetails
);
299 /** classifies the link pairs
301 @param _rxParentColumns
302 the columns of the parent database component
305 the columns of our own database component
307 @param _out_rAdditionalFilterComponents
308 the additional filter components which are required for master-detail relationships where
309 the detail part denotes a column name. In such a case, an additional filter needs to be created,
310 containing a new parameter.
313 <member>m_aMasterFields</member> and <member>m_aDetailFields</member> have the same length
316 const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& _rxParentColumns
,
317 const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& _rxColumns
,
318 ::std::vector
< OUString
>& _out_rAdditionalFilterComponents
319 ) SAL_THROW(( ::com::sun::star::uno::Exception
));
321 /** finalizes our <member>m_pOuterParameters</member> so that it can be used for
322 external parameter listeners
325 <member>m_pOuterParameters</member> is <NULL/>
327 <member>m_xInnerParamUpdate</member> is not <NULL/>
329 void createOuterParameters();
331 /** fills in the parameters values which result from the master-detail relationship
332 between the database component and it's parent
334 @param _rxParentColumns
335 the columns of the parameter database component. Must not be <NULL/>
337 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
339 void fillLinkedParameters(
340 const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& _rxParentColumns
343 /** completes all missing parameters via an interaction handler
346 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
349 <TRUE/> if and only if the parameter filling has <em>not</em> been cancelled by the user
351 bool completeParameters(
352 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& _rxCompletionHandler
,
353 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> _rxConnection
356 /** asks the parameter listeners to fill in final values
359 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
362 <TRUE/> if and only if the parameter filling has <em>not</em> been cancelled by the user
364 bool consultParameterListeners( ::osl::ResettableMutexGuard
& _rClearForNotifies
);
366 /** mark an externally filled parameter asvisited
368 void externalParameterVisited( sal_Int32 _nIndex
);
371 /** retrieves the columns of the parent database component
374 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
376 <TRUE/> if and only if the columns could be successfully retrieved
378 bool getParentColumns(
379 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& /* [out] */ _out_rxParentColumns
,
383 /** retrieves the columns of our database component
385 @param _bFromComposer
386 if <TRUE/>, the columns are obtained from the composer, else from the living database component itself
388 <TRUE/> if and only if the columns could be successfully retrieved
391 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& /* [out] */ _rxColumns
,
393 ) SAL_THROW(( ::com::sun::star::uno::Exception
));
395 /** retrieves the active connection of the database component
398 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& /* [out] */ _rxConnection
401 /** caches some info about the connection of our database component
403 void cacheConnectionInfo() SAL_THROW(( ));
406 ParameterManager(); // never implemented
407 ParameterManager( const ParameterManager
& ); // never implemented
408 ParameterManager
& operator=( const ParameterManager
& ); // never implemented
411 //........................................................................
413 //........................................................................
415 #endif // CONNECTIVITY_PARAMETERS_HXX
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */