LibreOffice Module ucbhelper (master)  1
propertyvalueset.cxx
Go to the documentation of this file.
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 /**************************************************************************
22  TODO
23  **************************************************************************
24 
25  *************************************************************************/
26 
27 #include <vector>
28 #include <com/sun/star/beans/Property.hpp>
29 #include <com/sun/star/beans/XPropertyAccess.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/XPropertySetInfo.hpp>
32 #include <com/sun/star/script/CannotConvertException.hpp>
33 #include <com/sun/star/script/Converter.hpp>
34 
35 #include <osl/diagnose.h>
36 #include <osl/mutex.hxx>
38 #include <ucbhelper/macros.hxx>
39 #include <o3tl/typed_flags_set.hxx>
41 
42 using namespace com::sun::star::beans;
43 using namespace com::sun::star::container;
44 using namespace com::sun::star::io;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::script;
47 using namespace com::sun::star::sdbc;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::util;
50 
51 enum class PropsSet {
52  NONE = 0x00000000,
53  String = 0x00000001,
54  Boolean = 0x00000002,
55  Byte = 0x00000004,
56  Short = 0x00000008,
57  Int = 0x00000010,
58  Long = 0x00000020,
59  Float = 0x00000040,
60  Double = 0x00000080,
61  Bytes = 0x00000100,
62  Date = 0x00000200,
63  Time = 0x00000400,
64  Timestamp = 0x00000800,
65  BinaryStream = 0x00001000,
66  CharacterStream = 0x00002000,
67  Ref = 0x00004000,
68  Blob = 0x00008000,
69  Clob = 0x00010000,
70  Array = 0x00020000,
71  Object = 0x00040000
72 };
73 namespace o3tl {
74  template<> struct typed_flags<PropsSet> : is_typed_flags<PropsSet, 0x0007ffff> {};
75 }
76 
77 namespace ucbhelper_impl
78 {
79 
80 
82 {
83  OUString sPropertyName;
84 
87 
88  OUString aString; // getString
89  bool bBoolean; // getBoolean
90  sal_Int8 nByte; // getByte
91  sal_Int16 nShort; // getShort
92  sal_Int32 nInt; // getInt
93  sal_Int64 nLong; // getLong
94  float nFloat; // getFloat
95  double nDouble; // getDouble
96 
98  Date aDate; // getDate
99  Time aTime; // getTime
100  DateTime aTimestamp; // getTimestamp
107  Any aObject; // getObject
108 
110  : nPropsSet( PropsSet::NONE ), nOrigValue( PropsSet::NONE ),
111  bBoolean(false),
112  nByte(0),
113  nShort(0),
114  nInt(0),
115  nLong(0),
116  nFloat(0.0),
117  nDouble(0.0)
118  {}
119 };
120 } // namespace ucbhelper_impl
121 
122 using namespace ucbhelper_impl;
123 
124 namespace ucbhelper
125 {
126 
127 class PropertyValues : public std::vector< ucbhelper_impl::PropertyValue > {};
128 
129 } // namespace ucbhelper
130 
131 
132 namespace ucbhelper {
133 
134 
135 // PropertyValueSet Implementation.
136 
137 
138 PropertyValueSet::PropertyValueSet(
139  const Reference< XComponentContext >& rxContext )
140 : m_xContext( rxContext ),
141  m_pValues( new PropertyValues ),
142  m_bWasNull( false ),
143  m_bTriedToGetTypeConverter( false )
144 
145 {
146 }
147 
148 
149 // virtual
151 {
152 }
153 
154 
155 // XRow methods.
156 
157 
158 template <class T, T ucbhelper_impl::PropertyValue::*_member_name_>
159 T PropertyValueSet::getValue(PropsSet nTypeName, sal_Int32 columnIndex)
160 {
161  osl::MutexGuard aGuard( m_aMutex );
162 
163  T aValue {}; /* default ctor */
164 
165  m_bWasNull = true;
166 
167  if ( ( columnIndex < 1 ) || ( columnIndex > sal_Int32( m_pValues->size() ) ) )
168  {
169  OSL_FAIL( "PropertyValueSet - index out of range!" );
170  return aValue;
171  }
172  ucbhelper_impl::PropertyValue& rValue = (*m_pValues)[ columnIndex - 1 ];
173 
174  if ( rValue.nOrigValue == PropsSet::NONE )
175  return aValue;
176 
177  if ( rValue.nPropsSet & nTypeName )
178  {
179  /* Values is present natively... */
180  aValue = rValue.*_member_name_;
181  m_bWasNull = false;
182  return aValue;
183  }
184 
185  if ( !(rValue.nPropsSet & PropsSet::Object) )
186  {
187  /* Value is not (yet) available as Any. Create it. */
188  getObject( columnIndex, Reference< XNameAccess >() );
189  }
190 
191  if ( rValue.nPropsSet & PropsSet::Object )
192  {
193  /* Value is available as Any. */
194 
195  if ( rValue.aObject.hasValue() )
196  {
197  /* Try to convert into native value. */
198  if ( rValue.aObject >>= aValue )
199  {
200  rValue.*_member_name_ = aValue;
201  rValue.nPropsSet |= nTypeName;
202  m_bWasNull = false;
203  }
204  else
205  {
206  /* Last chance. Try type converter service... */
207 
209  if ( xConverter.is() )
210  {
211  try
212  {
213  Any aConvAny = xConverter->convertTo(
214  rValue.aObject,
216 
217  if ( aConvAny >>= aValue )
218  {
219  rValue.*_member_name_ = aValue;
220  rValue.nPropsSet |= nTypeName;
221  m_bWasNull = false;
222  }
223  }
224  catch (const IllegalArgumentException&)
225  {
226  }
227  catch (const CannotConvertException&)
228  {
229  }
230  }
231  }
232  }
233  }
234 
235  return aValue;
236 }
237 
238 
239 // virtual
241 {
242  // This method can not be implemented correctly!!! Imagine different
243  // threads doing a getXYZ - wasNull calling sequence on the same
244  // implementation object...
245  return m_bWasNull;
246 }
247 
248 
249 // virtual
250 OUString SAL_CALL PropertyValueSet::getString( sal_Int32 columnIndex )
251 {
252  return getValue<OUString, &ucbhelper_impl::PropertyValue::aString>(PropsSet::String, columnIndex);
253 }
254 
255 
256 // virtual
257 sal_Bool SAL_CALL PropertyValueSet::getBoolean( sal_Int32 columnIndex )
258 {
259  return getValue<bool, &ucbhelper_impl::PropertyValue::bBoolean>(PropsSet::Boolean, columnIndex);
260 }
261 
262 
263 // virtual
264 sal_Int8 SAL_CALL PropertyValueSet::getByte( sal_Int32 columnIndex )
265 {
266  return getValue<sal_Int8, &ucbhelper_impl::PropertyValue::nByte>(PropsSet::Byte, columnIndex);
267 }
268 
269 
270 // virtual
271 sal_Int16 SAL_CALL PropertyValueSet::getShort( sal_Int32 columnIndex )
272 {
273  return getValue<sal_Int16, &ucbhelper_impl::PropertyValue::nShort>(PropsSet::Short, columnIndex);
274 }
275 
276 
277 // virtual
278 sal_Int32 SAL_CALL PropertyValueSet::getInt( sal_Int32 columnIndex )
279 {
280  return getValue<sal_Int32, &ucbhelper_impl::PropertyValue::nInt>(PropsSet::Int, columnIndex);
281 }
282 
283 
284 // virtual
285 sal_Int64 SAL_CALL PropertyValueSet::getLong( sal_Int32 columnIndex )
286 {
287  return getValue<sal_Int64, &ucbhelper_impl::PropertyValue::nLong>(PropsSet::Long, columnIndex);
288 }
289 
290 
291 // virtual
292 float SAL_CALL PropertyValueSet::getFloat( sal_Int32 columnIndex )
293 {
294  return getValue<float, &ucbhelper_impl::PropertyValue::nFloat>(PropsSet::Float, columnIndex);
295 }
296 
297 
298 // virtual
299 double SAL_CALL PropertyValueSet::getDouble( sal_Int32 columnIndex )
300 {
301  return getValue<double, &ucbhelper_impl::PropertyValue::nDouble>(PropsSet::Double, columnIndex);
302 }
303 
304 
305 // virtual
306 Sequence< sal_Int8 > SAL_CALL
307 PropertyValueSet::getBytes( sal_Int32 columnIndex )
308 {
309  return getValue<Sequence< sal_Int8 >, &ucbhelper_impl::PropertyValue::aBytes>(PropsSet::Bytes, columnIndex);
310 }
311 
312 
313 // virtual
314 Date SAL_CALL PropertyValueSet::getDate( sal_Int32 columnIndex )
315 {
316  return getValue<Date, &ucbhelper_impl::PropertyValue::aDate>(PropsSet::Date, columnIndex);
317 }
318 
319 
320 // virtual
321 Time SAL_CALL PropertyValueSet::getTime( sal_Int32 columnIndex )
322 {
323  return getValue<Time, &ucbhelper_impl::PropertyValue::aTime>(PropsSet::Time, columnIndex);
324 }
325 
326 
327 // virtual
328 DateTime SAL_CALL PropertyValueSet::getTimestamp( sal_Int32 columnIndex )
329 {
330  return getValue<DateTime, &ucbhelper_impl::PropertyValue::aTimestamp>(PropsSet::Timestamp, columnIndex);
331 }
332 
333 
334 // virtual
336 PropertyValueSet::getBinaryStream( sal_Int32 columnIndex )
337 {
338  return getValue<Reference< XInputStream >, &ucbhelper_impl::PropertyValue::xBinaryStream>(PropsSet::BinaryStream, columnIndex);
339 }
340 
341 
342 // virtual
344 PropertyValueSet::getCharacterStream( sal_Int32 columnIndex )
345 {
346  return getValue<Reference< XInputStream >, &ucbhelper_impl::PropertyValue::xCharacterStream>(PropsSet::CharacterStream, columnIndex);
347 }
348 
349 
350 // virtual
352  sal_Int32 columnIndex,
353  const Reference< XNameAccess >& )
354 {
355  osl::MutexGuard aGuard( m_aMutex );
356 
357  Any aValue;
358 
359  m_bWasNull = true;
360 
361  if ( ( columnIndex < 1 )
362  || ( columnIndex > sal_Int32( m_pValues->size() ) ) )
363  {
364  OSL_FAIL( "PropertyValueSet - index out of range!" );
365  }
366  else
367  {
369  = (*m_pValues)[ columnIndex - 1 ];
370 
371  if ( rValue.nPropsSet & PropsSet::Object )
372  {
373  // Values is present natively...
374  aValue = rValue.aObject;
375  m_bWasNull = false;
376  }
377  else
378  {
379  // Make Any from original value.
380 
381  switch ( rValue.nOrigValue )
382  {
383  case PropsSet::NONE:
384  break;
385 
386  case PropsSet::String:
387  aValue <<= rValue.aString;
388  break;
389 
390  case PropsSet::Boolean:
391  aValue <<= rValue.bBoolean;
392  break;
393 
394  case PropsSet::Byte:
395  aValue <<= rValue.nByte;
396  break;
397 
398  case PropsSet::Short:
399  aValue <<= rValue.nShort;
400  break;
401 
402  case PropsSet::Int:
403  aValue <<= rValue.nInt;
404  break;
405 
406  case PropsSet::Long:
407  aValue <<= rValue.nLong;
408  break;
409 
410  case PropsSet::Float:
411  aValue <<= rValue.nFloat;
412  break;
413 
414  case PropsSet::Double:
415  aValue <<= rValue.nDouble;
416  break;
417 
418  case PropsSet::Bytes:
419  aValue <<= rValue.aBytes;
420  break;
421 
422  case PropsSet::Date:
423  aValue <<= rValue.aDate;
424  break;
425 
426  case PropsSet::Time:
427  aValue <<= rValue.aTime;
428  break;
429 
430  case PropsSet::Timestamp:
431  aValue <<= rValue.aTimestamp;
432  break;
433 
435  aValue <<= rValue.xBinaryStream;
436  break;
437 
439  aValue <<= rValue.xCharacterStream;
440  break;
441 
442  case PropsSet::Ref:
443  aValue <<= rValue.xRef;
444  break;
445 
446  case PropsSet::Blob:
447  aValue <<= rValue.xBlob;
448  break;
449 
450  case PropsSet::Clob:
451  aValue <<= rValue.xClob;
452  break;
453 
454  case PropsSet::Array:
455  aValue <<= rValue.xArray;
456  break;
457 
458  case PropsSet::Object:
459  // Fall-through is intended!
460  default:
461  OSL_FAIL( "PropertyValueSet::getObject - "
462  "Wrong original type" );
463  break;
464  }
465 
466  if ( aValue.hasValue() )
467  {
468  rValue.aObject = aValue;
469  rValue.nPropsSet |= PropsSet::Object;
470  m_bWasNull = false;
471  }
472  }
473  }
474 
475  return aValue;
476 }
477 
478 
479 // virtual
480 Reference< XRef > SAL_CALL PropertyValueSet::getRef( sal_Int32 columnIndex )
481 {
482  return getValue<Reference< XRef >, &ucbhelper_impl::PropertyValue::xRef>(PropsSet::Ref, columnIndex);
483 }
484 
485 
486 // virtual
487 Reference< XBlob > SAL_CALL PropertyValueSet::getBlob( sal_Int32 columnIndex )
488 {
489  return getValue<Reference< XBlob >, &ucbhelper_impl::PropertyValue::xBlob>(PropsSet::Blob, columnIndex);
490 }
491 
492 
493 // virtual
494 Reference< XClob > SAL_CALL PropertyValueSet::getClob( sal_Int32 columnIndex )
495 {
496  return getValue<Reference< XClob >, &ucbhelper_impl::PropertyValue::xClob>(PropsSet::Clob, columnIndex);
497 }
498 
499 
500 // virtual
501 Reference< XArray > SAL_CALL PropertyValueSet::getArray( sal_Int32 columnIndex )
502 {
503  return getValue<Reference< XArray >, &ucbhelper_impl::PropertyValue::xArray>(PropsSet::Array, columnIndex);
504 }
505 
506 
507 // XColumnLocate methods.
508 
509 
510 // virtual
511 sal_Int32 SAL_CALL PropertyValueSet::findColumn( const OUString& columnName )
512 {
513  osl::MutexGuard aGuard( m_aMutex );
514 
515  if ( !columnName.isEmpty() )
516  {
517  sal_Int32 nCount = m_pValues->size();
518  for ( sal_Int32 n = 0; n < nCount; ++n )
519  {
520  if ( (*m_pValues)[ n ].sPropertyName == columnName )
521  return n + 1; // Index is 1-based.
522  }
523  }
524  return 0;
525 }
526 
527 
528 // Non-interface methods.
529 
530 
532 {
533  osl::MutexGuard aGuard( m_aMutex );
534 
536  {
538  m_xTypeConverter = Converter::create(m_xContext);
539 
540  OSL_ENSURE( m_xTypeConverter.is(),
541  "PropertyValueSet::getTypeConverter() - "
542  "Service 'com.sun.star.script.Converter' n/a!" );
543  }
544  return m_xTypeConverter;
545 }
546 
547 
548 template <class T, T ucbhelper_impl::PropertyValue::*_member_name_>
549 void PropertyValueSet::appendValue(const OUString& rPropName, PropsSet nTypeName, const T& rValue)
550 {
551  osl::MutexGuard aGuard( m_aMutex );
552 
554  aNewValue.sPropertyName = rPropName;
555  aNewValue.nPropsSet = nTypeName;
556  aNewValue.nOrigValue = nTypeName;
557  aNewValue.*_member_name_ = rValue;
558 
559  m_pValues->push_back( aNewValue );
560 }
561 
562 
563 void PropertyValueSet::appendString( const OUString& rPropName,
564  const OUString& rValue )
565 {
566  appendValue<OUString, &ucbhelper_impl::PropertyValue::aString>(rPropName, PropsSet::String, rValue);
567 }
568 
569 
570 void PropertyValueSet::appendBoolean( const OUString& rPropName,
571  bool bValue )
572 {
573  appendValue<bool, &ucbhelper_impl::PropertyValue::bBoolean>(rPropName, PropsSet::Boolean, bValue);
574 }
575 
576 
577 void PropertyValueSet::appendLong( const OUString& rPropName,
578  sal_Int64 nValue )
579 {
580  appendValue<sal_Int64, &ucbhelper_impl::PropertyValue::nLong>(rPropName, PropsSet::Long, nValue);
581 }
582 
583 
584 void PropertyValueSet::appendTimestamp( const OUString& rPropName,
585  const DateTime& rValue )
586 {
587  appendValue<DateTime, &ucbhelper_impl::PropertyValue::aTimestamp>(rPropName, PropsSet::Timestamp, rValue);
588 }
589 
590 
591 void PropertyValueSet::appendObject( const OUString& rPropName,
592  const Any& rValue )
593 {
594  appendValue<Any, &ucbhelper_impl::PropertyValue::aObject>(rPropName, PropsSet::Object, rValue);
595 }
596 
597 
598 void PropertyValueSet::appendVoid( const OUString& rPropName )
599 {
600  appendValue<Any, &ucbhelper_impl::PropertyValue::aObject>(rPropName, PropsSet::NONE, Any());
601 }
602 
603 
605  const Reference< XPropertySet >& rxSet )
606 {
607  if ( !rxSet.is() )
608  return;
609 
610  Reference< XPropertySetInfo > xInfo = rxSet->getPropertySetInfo();
611  if ( !xInfo.is() )
612  return;
613 
614  const Sequence< Property > aProps = xInfo->getProperties();
615 
616  Reference< XPropertyAccess > xPropertyAccess( rxSet, UNO_QUERY );
617  if ( xPropertyAccess.is() )
618  {
619  // Efficient: Get all prop values with one ( remote) call.
620 
621  const Sequence< css::beans::PropertyValue > aPropValues
622  = xPropertyAccess->getPropertyValues();
623 
624  for ( const css::beans::PropertyValue& rPropValue : aPropValues )
625  {
626  // Find info for current property value.
627  auto pProp = std::find_if(aProps.begin(), aProps.end(),
628  [&rPropValue](const Property& rProp) { return rProp.Name == rPropValue.Name; });
629  if (pProp != aProps.end())
630  {
631  // Found!
632  appendObject( *pProp, rPropValue.Value );
633  }
634  }
635  }
636  else
637  {
638  // Get every single prop value with one ( remote) call.
639 
640  for ( const Property& rProp : aProps )
641  {
642  try
643  {
644  Any aValue = rxSet->getPropertyValue( rProp.Name );
645 
646  if ( aValue.hasValue() )
647  appendObject( rProp, aValue );
648  }
649  catch (const UnknownPropertyException&)
650  {
651  }
652  catch (const WrappedTargetException&)
653  {
654  }
655  }
656  }
657 }
658 
659 
661  const Reference< XPropertySet >& rxSet,
662  const Property& rProperty )
663 {
664  if ( rxSet.is() )
665  {
666  try
667  {
668  Any aValue = rxSet->getPropertyValue( rProperty.Name );
669  if ( aValue.hasValue() )
670  {
671  appendObject( rProperty, aValue );
672  return true;
673  }
674  }
675  catch (const UnknownPropertyException&)
676  {
677  }
678  catch (const WrappedTargetException&)
679  {
680  }
681  }
682 
683  // Error.
684  return false;
685 }
686 
687 } // namespace ucbhelper
688 
689 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XTypeConverter > xConverter
std::unique_ptr< KeyedValues > m_pValues
css::uno::Reference< css::script::XTypeConverter > m_xTypeConverter
T getValue(PropsSet nTypeName, sal_Int32 columnIndex)
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getBinaryStream(sal_Int32 columnIndex) override
virtual css::util::Time SAL_CALL getTime(sal_Int32 columnIndex) override
void appendBoolean(const OUString &rPropName, bool bValue)
signed char sal_Int8
virtual sal_Int32 SAL_CALL findColumn(const OUString &columnName) override
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getCharacterStream(sal_Int32 columnIndex) override
virtual sal_Int16 SAL_CALL getShort(sal_Int32 columnIndex) override
long Long
UCBHELPER_DLLPRIVATE const css::uno::Reference< css::script::XTypeConverter > & getTypeConverter()
virtual css::uno::Reference< css::sdbc::XRef > SAL_CALL getRef(sal_Int32 columnIndex) override
sal_Int64 n
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual float SAL_CALL getFloat(sal_Int32 columnIndex) override
virtual sal_Int64 SAL_CALL getLong(sal_Int32 columnIndex) override
virtual OUString SAL_CALL getString(sal_Int32 columnIndex) override
virtual sal_Bool SAL_CALL wasNull() override
virtual sal_Int8 SAL_CALL getByte(sal_Int32 columnIndex) override
void appendLong(const OUString &rPropName, sal_Int64 nValue)
NONE
css::uno::Any const & rValue
int nCount
virtual css::uno::Reference< css::sdbc::XClob > SAL_CALL getClob(sal_Int32 columnIndex) override
void appendVoid(const OUString &rPropName)
virtual ~PropertyValueSet() override
void appendString(const OUString &rPropName, const OUString &rValue)
void appendTimestamp(const OUString &rPropName, const css::util::DateTime &rValue)
virtual sal_Int32 SAL_CALL getInt(sal_Int32 columnIndex) override
Array
virtual sal_Bool SAL_CALL getBoolean(sal_Int32 columnIndex) override
virtual css::uno::Reference< css::sdbc::XBlob > SAL_CALL getBlob(sal_Int32 columnIndex) override
void appendPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rSet)
This method tries to append all property values contained in a property set to the value set...
Reference< XInputStream > xBinaryStream
unsigned char sal_Bool
Reference< XInputStream > xCharacterStream
void appendObject(const OUString &rPropName, const css::uno::Any &rValue)
std::unique_ptr< PropertyValues > m_pValues
virtual css::uno::Reference< css::sdbc::XArray > SAL_CALL getArray(sal_Int32 columnIndex) override
void appendValue(const OUString &rPropName, PropsSet nTypeName, const T &rValue)
bool appendPropertySetValue(const css::uno::Reference< css::beans::XPropertySet > &rSet, const css::beans::Property &rProperty)
This method tries to append a single property value contained in a property set to the value set...
Reference< XComponentContext > m_xContext
virtual css::uno::Any SAL_CALL getObject(sal_Int32 columnIndex, const css::uno::Reference< css::container::XNameAccess > &typeMap) override
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBytes(sal_Int32 columnIndex) override
virtual css::util::Date SAL_CALL getDate(sal_Int32 columnIndex) override
virtual css::util::DateTime SAL_CALL getTimestamp(sal_Int32 columnIndex) override
virtual double SAL_CALL getDouble(sal_Int32 columnIndex) override