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 .
20 #include <svx/dataaccessdescriptor.hxx>
21 #include <comphelper/propertysetinfo.hxx>
22 #include <comphelper/genericpropertyset.hxx>
23 #include <osl/diagnose.h>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <com/sun/star/ucb/XContent.hpp>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <tools/urlobj.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::sdbc
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::ucb
;
38 using namespace ::comphelper
;
40 class ODADescriptorImpl
43 bool m_bSetOutOfDate
: 1;
44 bool m_bSequenceOutOfDate
: 1;
47 typedef ::std::map
< DataAccessDescriptorProperty
, Any
> DescriptorValues
;
48 DescriptorValues m_aValues
;
49 Sequence
< PropertyValue
> m_aAsSequence
;
50 Reference
< XPropertySet
> m_xAsSet
;
52 typedef ::std::map
< OUString
, PropertyMapEntry
const * > MapString2PropertyEntry
;
56 ODADescriptorImpl(const ODADescriptorImpl
& _rSource
);
58 void invalidateExternRepresentations();
60 void updateSequence();
62 /** builds the descriptor from a property value sequence
64 if and only if the sequence contained valid properties only
66 bool buildFrom( const Sequence
< PropertyValue
>& _rValues
);
68 /** builds the descriptor from a property set
70 if and only if the set contained valid properties only
72 bool buildFrom( const Reference
< XPropertySet
>& _rValues
);
75 static PropertyValue
buildPropertyValue( const DescriptorValues::const_iterator
& _rPos
);
76 static const MapString2PropertyEntry
& getPropertyMap( );
77 static PropertyMapEntry
const * getPropertyMapEntry( const DescriptorValues::const_iterator
& _rPos
);
81 ODADescriptorImpl::ODADescriptorImpl()
82 :m_bSetOutOfDate(true)
83 ,m_bSequenceOutOfDate(true)
88 ODADescriptorImpl::ODADescriptorImpl(const ODADescriptorImpl
& _rSource
)
89 :m_bSetOutOfDate( _rSource
.m_bSetOutOfDate
)
90 ,m_bSequenceOutOfDate( _rSource
.m_bSequenceOutOfDate
)
91 ,m_aValues( _rSource
.m_aValues
)
94 m_xAsSet
= _rSource
.m_xAsSet
;
95 if (!m_bSequenceOutOfDate
)
96 m_aAsSequence
= _rSource
.m_aAsSequence
;
100 bool ODADescriptorImpl::buildFrom( const Sequence
< PropertyValue
>& _rValues
)
102 const MapString2PropertyEntry
& rProperties
= getPropertyMap();
104 bool bValidPropsOnly
= true;
106 // loop through the sequence, and fill our m_aValues
107 const PropertyValue
* pValues
= _rValues
.getConstArray();
108 const PropertyValue
* pValuesEnd
= pValues
+ _rValues
.getLength();
109 for (;pValues
!= pValuesEnd
; ++pValues
)
111 MapString2PropertyEntry::const_iterator aPropPos
= rProperties
.find( pValues
->Name
);
112 if ( aPropPos
!= rProperties
.end() )
114 DataAccessDescriptorProperty eProperty
= (DataAccessDescriptorProperty
)aPropPos
->second
->mnHandle
;
115 m_aValues
[eProperty
] = pValues
->Value
;
119 bValidPropsOnly
= false;
124 m_aAsSequence
= _rValues
;
125 m_bSequenceOutOfDate
= false;
128 m_bSequenceOutOfDate
= true;
130 return bValidPropsOnly
;
134 bool ODADescriptorImpl::buildFrom( const Reference
< XPropertySet
>& _rxValues
)
136 Reference
< XPropertySetInfo
> xPropInfo
;
138 xPropInfo
= _rxValues
->getPropertySetInfo();
141 OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
145 // build a PropertyValue sequence with the current values
146 Sequence
< Property
> aProperties
= xPropInfo
->getProperties();
147 const Property
* pProperty
= aProperties
.getConstArray();
148 const Property
* pPropertyEnd
= pProperty
+ aProperties
.getLength();
150 Sequence
< PropertyValue
> aValues(aProperties
.getLength());
151 PropertyValue
* pValues
= aValues
.getArray();
153 for (;pProperty
!= pPropertyEnd
; ++pProperty
, ++pValues
)
155 pValues
->Name
= pProperty
->Name
;
156 pValues
->Value
= _rxValues
->getPropertyValue(pProperty
->Name
);
159 bool bValidPropsOnly
= buildFrom(aValues
);
162 m_xAsSet
= _rxValues
;
163 m_bSetOutOfDate
= false;
166 m_bSetOutOfDate
= true;
168 return bValidPropsOnly
;
172 void ODADescriptorImpl::invalidateExternRepresentations()
174 m_bSetOutOfDate
= true;
175 m_bSequenceOutOfDate
= true;
179 const ODADescriptorImpl::MapString2PropertyEntry
& ODADescriptorImpl::getPropertyMap( )
181 // the properties we know
182 static MapString2PropertyEntry s_aProperties
;
183 if ( s_aProperties
.empty() )
185 static PropertyMapEntry
const s_aDesriptorProperties
[] =
187 { OUString("ActiveConnection"), daConnection
, cppu::UnoType
<XConnection
>::get(), PropertyAttribute::TRANSIENT
, 0 },
188 { OUString("BookmarkSelection"), daBookmarkSelection
, cppu::UnoType
<bool>::get(), PropertyAttribute::TRANSIENT
, 0 },
189 { OUString("Column"), daColumnObject
, cppu::UnoType
<XPropertySet
>::get(), PropertyAttribute::TRANSIENT
, 0 },
190 { OUString("ColumnName"), daColumnName
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
191 { OUString("Command"), daCommand
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
192 { OUString("CommandType"), daCommandType
, ::cppu::UnoType
<sal_Int32
>::get(), PropertyAttribute::TRANSIENT
, 0 },
193 { OUString("Component"), daComponent
, cppu::UnoType
<XContent
>::get(), PropertyAttribute::TRANSIENT
, 0 },
194 { OUString("ConnectionResource"), daConnectionResource
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
195 { OUString("Cursor"), daCursor
, cppu::UnoType
<XResultSet
>::get(), PropertyAttribute::TRANSIENT
, 0 },
196 { OUString("DataSourceName"), daDataSource
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
197 { OUString("DatabaseLocation"), daDatabaseLocation
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
198 { OUString("EscapeProcessing"), daEscapeProcessing
, cppu::UnoType
<bool>::get(), PropertyAttribute::TRANSIENT
, 0 },
199 { OUString("Filter"), daFilter
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::TRANSIENT
, 0 },
200 { OUString("Selection"), daSelection
, cppu::UnoType
<Sequence
< Any
>>::get(), PropertyAttribute::TRANSIENT
, 0 },
201 { OUString(), 0, css::uno::Type(), 0, 0 }
204 PropertyMapEntry
const * pEntry
= s_aDesriptorProperties
;
205 while ( !pEntry
->maName
.isEmpty() )
207 s_aProperties
[ pEntry
->maName
] = pEntry
;
212 return s_aProperties
;
216 PropertyMapEntry
const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator
& _rPos
)
218 const MapString2PropertyEntry
& rProperties
= getPropertyMap();
220 sal_Int32 nNeededHandle
= (sal_Int32
)(_rPos
->first
);
222 for ( MapString2PropertyEntry::const_iterator loop
= rProperties
.begin();
223 loop
!= rProperties
.end();
227 if ( nNeededHandle
== loop
->second
->mnHandle
)
230 throw RuntimeException();
234 PropertyValue
ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator
& _rPos
)
237 PropertyMapEntry
const * pProperty
= getPropertyMapEntry( _rPos
);
239 // build the property value
240 PropertyValue aReturn
;
241 aReturn
.Name
= pProperty
->maName
;
242 aReturn
.Handle
= pProperty
->mnHandle
;
243 aReturn
.Value
= _rPos
->second
;
244 aReturn
.State
= PropertyState_DIRECT_VALUE
;
251 void ODADescriptorImpl::updateSequence()
253 if (!m_bSequenceOutOfDate
)
256 m_aAsSequence
.realloc(m_aValues
.size());
257 PropertyValue
* pValue
= m_aAsSequence
.getArray();
259 // loop through all our values
260 for ( DescriptorValues::const_iterator aLoop
= m_aValues
.begin();
261 aLoop
!= m_aValues
.end();
265 *pValue
= buildPropertyValue(aLoop
);
268 // don't need to rebuild next time
269 m_bSequenceOutOfDate
= false;
272 ODataAccessDescriptor::ODataAccessDescriptor()
273 :m_pImpl(new ODADescriptorImpl
)
278 ODataAccessDescriptor::ODataAccessDescriptor( const ODataAccessDescriptor
& _rSource
)
279 :m_pImpl(new ODADescriptorImpl(*_rSource
.m_pImpl
))
284 const ODataAccessDescriptor
& ODataAccessDescriptor::operator=(const ODataAccessDescriptor
& _rSource
)
287 m_pImpl
= new ODADescriptorImpl(*_rSource
.m_pImpl
);
292 ODataAccessDescriptor::ODataAccessDescriptor( const Reference
< XPropertySet
>& _rValues
)
293 :m_pImpl(new ODADescriptorImpl
)
295 m_pImpl
->buildFrom(_rValues
);
299 ODataAccessDescriptor::ODataAccessDescriptor( const Any
& _rValues
)
300 :m_pImpl(new ODADescriptorImpl
)
302 // check if we know the format in the Any
303 Sequence
< PropertyValue
> aValues
;
304 Reference
< XPropertySet
> xValues
;
305 if ( _rValues
>>= aValues
)
306 m_pImpl
->buildFrom( aValues
);
307 else if ( _rValues
>>= xValues
)
308 m_pImpl
->buildFrom( xValues
);
312 ODataAccessDescriptor::ODataAccessDescriptor( const Sequence
< PropertyValue
>& _rValues
)
313 :m_pImpl(new ODADescriptorImpl
)
315 m_pImpl
->buildFrom(_rValues
);
319 ODataAccessDescriptor::~ODataAccessDescriptor()
325 void ODataAccessDescriptor::clear()
327 m_pImpl
->m_aValues
.clear();
331 void ODataAccessDescriptor::erase(DataAccessDescriptorProperty _eWhich
)
333 OSL_ENSURE(has(_eWhich
), "ODataAccessDescriptor::erase: invalid call!");
335 m_pImpl
->m_aValues
.erase(_eWhich
);
339 bool ODataAccessDescriptor::has(DataAccessDescriptorProperty _eWhich
) const
341 return m_pImpl
->m_aValues
.find(_eWhich
) != m_pImpl
->m_aValues
.end();
345 const Any
& ODataAccessDescriptor::operator [] ( DataAccessDescriptorProperty _eWhich
) const
349 OSL_FAIL("ODataAccessDescriptor::operator[]: invalid acessor!");
350 static const Any aDummy
;
354 return m_pImpl
->m_aValues
[_eWhich
];
358 Any
& ODataAccessDescriptor::operator[] ( DataAccessDescriptorProperty _eWhich
)
360 m_pImpl
->invalidateExternRepresentations();
361 return m_pImpl
->m_aValues
[_eWhich
];
365 void ODataAccessDescriptor::initializeFrom(const Sequence
< PropertyValue
>& _rValues
, bool _bClear
)
369 m_pImpl
->buildFrom(_rValues
);
373 Sequence
< PropertyValue
> ODataAccessDescriptor::createPropertyValueSequence()
375 m_pImpl
->updateSequence();
376 return m_pImpl
->m_aAsSequence
;
380 OUString
ODataAccessDescriptor::getDataSource() const
382 OUString sDataSourceName
;
383 if ( has(daDataSource
) )
384 (*this)[daDataSource
] >>= sDataSourceName
;
385 else if ( has(daDatabaseLocation
) )
386 (*this)[daDatabaseLocation
] >>= sDataSourceName
;
387 return sDataSourceName
;
390 void ODataAccessDescriptor::setDataSource(const OUString
& _sDataSourceNameOrLocation
)
392 if ( !_sDataSourceNameOrLocation
.isEmpty() )
394 INetURLObject
aURL(_sDataSourceNameOrLocation
);
395 (*this)[ (( aURL
.GetProtocol() == INetProtocol::File
) ? daDatabaseLocation
: daDataSource
)] <<= _sDataSourceNameOrLocation
;
398 (*this)[ daDataSource
] <<= OUString();
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */