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 INCLUDED_CONNECTIVITY_PARAMETERS_HXX
20 #define INCLUDED_CONNECTIVITY_PARAMETERS_HXX
26 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
28 #include <connectivity/dbtoolsdllapi.hxx>
29 #include <connectivity/paramwrapper.hxx>
30 #include <unotools/sharedunocomponent.hxx>
31 #include <comphelper/interfacecontainer3.hxx>
33 namespace com::sun::star::beans
{ class XPropertySet
; }
34 namespace com::sun::star::container
{ class XIndexAccess
; }
35 namespace com::sun::star::form
{ class XDatabaseParameterListener
; }
36 namespace com::sun::star::io
{ class XInputStream
; }
37 namespace com::sun::star::sdbc
{ class XArray
; }
38 namespace com::sun::star::sdbc
{ class XBlob
; }
39 namespace com::sun::star::sdbc
{ class XClob
; }
40 namespace com::sun::star::sdbc
{ class XConnection
; }
41 namespace com::sun::star::sdbc
{ class XDatabaseMetaData
; }
42 namespace com::sun::star::sdbc
{ class XParameters
; }
43 namespace com::sun::star::sdbc
{ class XRef
; }
44 namespace com::sun::star::task
{ class XInteractionHandler
; }
45 namespace com::sun::star::uno
{ class XAggregation
; }
46 namespace com::sun::star::uno
{ class XComponentContext
; }
52 typedef ::utl::SharedUNOComponent
< css::sdb::XSingleSelectQueryComposer
, ::utl::DisposableComponent
>
59 class UNLESS_MERGELIBS_MORE(OOO_DLLPUBLIC_DBTOOLS
) ParameterManager
62 /// classifies the origin of the data to fill a parameter
63 enum class ParameterClassification
65 /** parameters which are filled from the master-detail relationship, where the detail
66 name is an explicit parameter name
69 /** parameters which are filled from the master-detail relationship, where the detail
70 name is a column name, so an implicit parameter had to be generated for it
73 /** parameters which are filled externally (i.e. by XParameters::setXXX, or by the parameter listeners)
77 /** meta data about an inner parameter
80 struct ParameterMetaData
82 /// the type of the parameter
83 ParameterClassification eType
;
84 /// the column object for this parameter, as returned by the query composer
85 css::uno::Reference
< css::beans::XPropertySet
>
87 /// the indices of inner parameters which need to be filled when this concrete parameter is set
88 ::std::vector
< sal_Int32
> aInnerIndexes
;
90 /// ctor with composer column
91 ParameterMetaData( css::uno::Reference
< css::beans::XPropertySet
> _xColumn
)
92 :eType ( ParameterClassification::FilledExternally
)
93 ,xComposerColumn (std::move( _xColumn
))
98 typedef ::std::map
< OUString
, ParameterMetaData
> ParameterInformation
;
101 ::osl::Mutex
& m_rMutex
;
102 ::comphelper::OInterfaceContainerHelper3
<css::form::XDatabaseParameterListener
> m_aParameterListeners
;
104 css::uno::Reference
< css::uno::XComponentContext
>
107 css::uno::WeakReference
< css::beans::XPropertySet
>
108 m_xComponent
; // the database component whose parameters we're handling
109 css::uno::Reference
< css::uno::XAggregation
>
110 m_xAggregatedRowSet
; // the aggregated row set - necessary for unwrapped access to some interfaces
111 css::uno::Reference
< css::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 css::uno::Reference
< css::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 std::vector
< OUString
> m_aMasterFields
;
126 std::vector
< OUString
> m_aDetailFields
;
128 OUString m_sIdentifierQuoteString
;
129 OUString m_sSpecialCharacters
;
130 css::uno::Reference
< css::sdbc::XDatabaseMetaData
> m_xConnectionMetadata
;
132 ::std::vector
< bool > m_aParametersVisited
;
139 explicit ParameterManager(
140 ::osl::Mutex
& _rMutex
,
141 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
146 const css::uno::Reference
< css::beans::XPropertySet
>& _rxComponent
,
147 const css::uno::Reference
< css::uno::XAggregation
>& _rxComponentAggregate
150 /// makes the object forgetting the references to the database component
153 /// clears the instance data
154 void clearAllParameterInformation();
156 /// checks whether the parameter information is up-to-date
157 bool isUpToDate() const { return m_bUpToDate
; }
159 /** updates all parameter information represented by the instance
161 void updateParameterInfo( FilterManager
& _rFilterManager
);
163 /** fills parameter values, as extensive as possible
165 <p>In particular, all values which can be filled from the master-detail relationship of
166 between our database component and its parent are filled in.</p>
168 @param _rxCompletionHandler
169 an interaction handler which should be used to fill all parameters which
170 cannot be filled by other means. May be <NULL/>
171 @param _rClearForNotifies
172 the mutex guard to be (temporarily) cleared for notifications
175 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
178 <TRUE/> if and only if the parameter filling has <em>not</em> been cancelled by the user
180 bool fillParameterValues(
181 const css::uno::Reference
< css::task::XInteractionHandler
>& _rxCompletionHandler
,
182 ::osl::ResettableMutexGuard
& _rClearForNotifies
185 /** sets all parameter values to null (via <member>XParameters::setNull</member>)
188 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
190 void setAllParametersNull();
192 /** resets all detail columns which are, via a parameter, linked to a master column, to
193 the value of this master column.
195 For instance, if the database component is bound to a statement <code>SELECT * from invoice where inv_id = :cid</code>,
196 and there is <em>one</em> master-detail link from
199 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
201 void resetParameterValues();
203 /** adds the given listener to the list of parameter listeners
205 void addParameterListener(
206 const css::uno::Reference
< css::form::XDatabaseParameterListener
>& _rxListener
209 /** removes the given listener from the list of parameter listeners
211 void removeParameterListener(
212 const css::uno::Reference
< css::form::XDatabaseParameterListener
>& _rxListener
215 // XParameters equivalents
216 void setNull ( sal_Int32 _nIndex
, sal_Int32 sqlType
);
217 void setObjectNull ( sal_Int32 _nIndex
, sal_Int32 sqlType
, const OUString
& typeName
);
218 void setBoolean ( sal_Int32 _nIndex
, bool x
);
219 void setByte ( sal_Int32 _nIndex
, sal_Int8 x
);
220 void setShort ( sal_Int32 _nIndex
, sal_Int16 x
);
221 void setInt ( sal_Int32 _nIndex
, sal_Int32 x
);
222 void setLong ( sal_Int32 _nIndex
, sal_Int64 x
);
223 void setFloat ( sal_Int32 _nIndex
, float x
);
224 void setDouble ( sal_Int32 _nIndex
, double x
);
225 void setString ( sal_Int32 _nIndex
, const OUString
& x
);
226 void setBytes ( sal_Int32 _nIndex
, const css::uno::Sequence
< sal_Int8
>& x
);
227 void setDate ( sal_Int32 _nIndex
, const css::util::Date
& x
);
228 void setTime ( sal_Int32 _nIndex
, const css::util::Time
& x
);
229 void setTimestamp ( sal_Int32 _nIndex
, const css::util::DateTime
& x
);
230 void setBinaryStream ( sal_Int32 _nIndex
, const css::uno::Reference
< css::io::XInputStream
>& x
, sal_Int32 length
);
231 void setCharacterStream ( sal_Int32 _nIndex
, const css::uno::Reference
< css::io::XInputStream
>& x
, sal_Int32 length
);
232 void setObject ( sal_Int32 _nIndex
, const css::uno::Any
& x
);
233 void setObjectWithInfo ( sal_Int32 _nIndex
, const css::uno::Any
& x
, sal_Int32 targetSqlType
, sal_Int32 scale
);
234 void setRef ( sal_Int32 _nIndex
, const css::uno::Reference
< css::sdbc::XRef
>& x
);
235 void setBlob ( sal_Int32 _nIndex
, const css::uno::Reference
< css::sdbc::XBlob
>& x
);
236 void setClob ( sal_Int32 _nIndex
, const css::uno::Reference
< css::sdbc::XClob
>& x
);
237 void setArray ( sal_Int32 _nIndex
, const css::uno::Reference
< css::sdbc::XArray
>& x
);
238 void clearParameters();
241 /// checks whether the object is already initialized, and not yet disposed
242 bool isAlive() const { return m_xComponent
.get().is() && m_xInnerParamUpdate
.is(); }
244 /** creates a filter expression from a master-detail link where the detail denotes a column name
247 createFilterConditionFromColumnLink(
248 const OUString
& /* [in] */ _rMasterColumn
,
249 const css::uno::Reference
< css::beans::XPropertySet
>& /* [in] */ xDetailColumn
,
250 OUString
& /* [out] */ _rNewParamName
253 /** initializes our query composer, and the collection of inner parameter columns
256 the database component to initialize from. Must not be <NULL/>
258 <TRUE/> if and only if the initialization was successful
261 if and only if <TRUE/> is returned, then <member>m_xInnerParamColumns</member> contains the collection of
264 bool initializeComposerByComponent(
265 const css::uno::Reference
< css::beans::XPropertySet
>& _rxComponent
268 /** collects initial meta information about inner parameters (i.e. it initially fills
269 <member>m_aParameterInformation</member>).
272 if <TRUE/>, this is the second run, because we ourself previously extended the filter of
276 <member>m_xInnerParamColumns</member> is not <NULL/>
278 void collectInnerParameters( bool _bSecondRun
);
280 /** analyzes the master-detail links for our database component, and initializes m_aMasterFields and m_aDetailFields
282 @param _rFilterManager
283 the filter manager of the database component
284 @param _rColumnsInLinkDetails
285 will be set to <TRUE/> if and only if there were link pairs where the detail field denoted
286 a column name of our database component
289 the instance is alive, i.e. <member>isAlive</member> returns <TRUE/>
291 void analyzeFieldLinks( FilterManager
& _rFilterManager
, bool& /* [out] */ _rColumnsInLinkDetails
);
293 /** classifies the link pairs
295 @param _rxParentColumns
296 the columns of the parent database component
299 the columns of our own database component
301 @param _out_rAdditionalFilterComponents
302 the additional filter components which are required for master-detail relationships where
303 the detail part denotes a column name. In such a case, an additional filter needs to be created,
304 containing a new parameter.
306 @param _out_rAdditionalHavingComponents
307 the additional having clause components which are required for master-detail relationships where
308 the detail part denotes a column name. In such a case, an additional filter needs to be created,
309 containing a new parameter.
312 <member>m_aMasterFields</member> and <member>m_aDetailFields</member> have the same length
315 const css::uno::Reference
< css::container::XNameAccess
>& _rxParentColumns
,
316 const css::uno::Reference
< css::container::XNameAccess
>& _rxColumns
,
317 ::std::vector
< OUString
>& _out_rAdditionalFilterComponents
,
318 ::std::vector
< OUString
>& _out_rAdditionalHavingComponents
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 its 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 css::uno::Reference
< css::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 css::uno::Reference
< css::task::XInteractionHandler
>& _rxCompletionHandler
,
353 const css::uno::Reference
< css::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 as visited
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 css::uno::Reference
< css::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 css::uno::Reference
< css::container::XNameAccess
>& /* [out] */ _rxColumns
,
395 /** retrieves the active connection of the database component
398 css::uno::Reference
< css::sdbc::XConnection
>& /* [out] */ _rxConnection
401 /** caches some info about the connection of our database component
403 void cacheConnectionInfo();
406 ParameterManager( const ParameterManager
& ) = delete;
407 ParameterManager
& operator=( const ParameterManager
& ) = delete;
414 #endif // INCLUDED_CONNECTIVITY_PARAMETERS_HXX
416 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */