Branch libreoffice-5-0-4
[LibreOffice.git] / include / connectivity / FValue.hxx
blob658e3394fd94ddebca833dc5eb99df577b3d4665
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_CONNECTIVITY_FVALUE_HXX
21 #define INCLUDED_CONNECTIVITY_FVALUE_HXX
23 #include <com/sun/star/sdbc/DataType.hpp>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <rtl/ustring.hxx>
26 #include <salhelper/simplereferenceobject.hxx>
27 #include <osl/diagnose.h>
28 #include <rtl/ref.hxx>
29 #include <connectivity/dbtoolsdllapi.hxx>
30 #include <connectivity/CommonTools.hxx>
31 #include <com/sun/star/util/DateTime.hpp>
32 #include <com/sun/star/util/Date.hpp>
33 #include <com/sun/star/util/Time.hpp>
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdb/XColumn.hpp>
38 namespace connectivity
40 namespace detail
42 class IValueSource;
45 class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
47 union
49 bool m_bBool;
51 sal_Int8 m_nInt8;
52 sal_uInt8 m_uInt8;
54 sal_Int16 m_nInt16;
55 sal_uInt16 m_uInt16;
57 sal_Int32 m_nInt32;
58 sal_uInt32 m_uInt32;
60 sal_Int64 m_nInt64;
61 sal_uInt64 m_uInt64;
63 float m_nFloat;
64 double m_nDouble;
66 rtl_uString* m_pString;
68 void* m_pValue; // date/time/timestamp/sequence
69 } m_aValue;
71 sal_Int32 m_eTypeKind; // the database type
72 bool m_bNull : 1; // value is null
73 bool m_bBound : 1; // is bound
74 bool m_bModified : 1; // value was changed
75 bool m_bSigned : 1; // value is signed
77 void free();
79 public:
80 ORowSetValue()
81 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
82 ,m_bNull(true)
83 ,m_bBound(true)
84 ,m_bModified(false)
85 ,m_bSigned(true)
87 m_aValue.m_pString = NULL;
90 ORowSetValue(const ORowSetValue& _rRH)
91 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
92 ,m_bNull(true)
93 ,m_bBound(true)
94 ,m_bModified(false)
95 ,m_bSigned(true)
97 m_aValue.m_pString = NULL;
98 operator=(_rRH);
101 ORowSetValue(const OUString& _rRH)
102 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
103 ,m_bNull(true)
104 ,m_bBound(true)
105 ,m_bModified(false)
106 ,m_bSigned(true)
108 m_aValue.m_pString = NULL;
109 operator=(_rRH);
112 ORowSetValue(const double& _rRH)
113 :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE)
114 ,m_bNull(true)
115 ,m_bBound(true)
116 ,m_bModified(false)
117 ,m_bSigned(true)
119 m_aValue.m_pString = NULL;
120 operator=(_rRH);
123 ORowSetValue(const float& _rRH)
124 :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT)
125 ,m_bNull(true)
126 ,m_bBound(true)
127 ,m_bModified(false)
128 ,m_bSigned(true)
130 m_aValue.m_pString = NULL;
131 operator=(_rRH);
134 ORowSetValue(const sal_Int8& _rRH)
135 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
136 ,m_bNull(true)
137 ,m_bBound(true)
138 ,m_bModified(false)
139 ,m_bSigned(true)
141 m_aValue.m_pString = NULL;
142 operator=(_rRH);
145 ORowSetValue(const sal_uInt8& _rRH)
146 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
147 ,m_bNull(true)
148 ,m_bBound(true)
149 ,m_bModified(false)
150 ,m_bSigned(false)
152 m_aValue.m_pString = NULL;
153 operator=(_rRH);
155 ORowSetValue(const sal_Int16& _rRH)
156 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
157 ,m_bNull(true)
158 ,m_bBound(true)
159 ,m_bModified(false)
160 ,m_bSigned(true)
162 m_aValue.m_pString = NULL;
163 operator=(_rRH);
165 ORowSetValue(const sal_uInt16& _rRH)
166 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
167 ,m_bNull(true)
168 ,m_bBound(true)
169 ,m_bModified(false)
170 ,m_bSigned(false)
172 m_aValue.m_pString = NULL;
173 operator=(_rRH);
175 ORowSetValue(const sal_Int32& _rRH)
176 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
177 ,m_bNull(true)
178 ,m_bBound(true)
179 ,m_bModified(false)
180 ,m_bSigned(true)
182 m_aValue.m_pString = NULL;
183 operator=(_rRH);
185 ORowSetValue(const sal_uInt32& _rRH)
186 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
187 ,m_bNull(true)
188 ,m_bBound(true)
189 ,m_bModified(false)
190 ,m_bSigned(false)
192 m_aValue.m_pString = NULL;
193 operator=(_rRH);
195 ORowSetValue(const sal_Int64& _rRH)
196 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
197 ,m_bNull(true)
198 ,m_bBound(true)
199 ,m_bModified(false)
200 ,m_bSigned(true)
202 m_aValue.m_pString = NULL;
203 operator=(_rRH);
205 ORowSetValue(const sal_uInt64& _rRH)
206 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
207 ,m_bNull(true)
208 ,m_bBound(true)
209 ,m_bModified(false)
210 ,m_bSigned(false)
212 m_aValue.m_pString = NULL;
213 operator=(_rRH);
216 ORowSetValue(const bool& _rRH)
217 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT)
218 ,m_bNull(true)
219 ,m_bBound(true)
220 ,m_bModified(false)
221 ,m_bSigned(true)
223 m_aValue.m_pString = NULL;
224 operator=(_rRH);
227 ORowSetValue(const ::com::sun::star::util::Date& _rRH)
228 :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE)
229 ,m_bNull(true)
230 ,m_bBound(true)
231 ,m_bModified(false)
232 ,m_bSigned(true)
234 m_aValue.m_pString = NULL;
235 operator=(_rRH);
238 ORowSetValue(const ::com::sun::star::util::Time& _rRH)
239 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME)
240 ,m_bNull(true)
241 ,m_bBound(true)
242 ,m_bModified(false)
243 ,m_bSigned(true)
245 m_aValue.m_pString = NULL;
246 operator=(_rRH);
249 ORowSetValue(const ::com::sun::star::util::DateTime& _rRH)
250 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP)
251 ,m_bNull(true)
252 ,m_bBound(true)
253 ,m_bModified(false)
254 ,m_bSigned(true)
256 m_aValue.m_pString = NULL;
257 operator=(_rRH);
260 ORowSetValue(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH)
261 :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY)
262 ,m_bNull(true)
263 ,m_bBound(true)
264 ,m_bModified(false)
265 ,m_bSigned(true)
267 m_aValue.m_pString = NULL;
268 operator=(_rRH);
271 // Avoid accidental uses of ORowSetValue(bool const &):
272 template<typename T> ORowSetValue(T const *) = delete;
274 ~ORowSetValue()
276 free();
279 inline static void * SAL_CALL operator new( size_t nSize )
280 { return ::rtl_allocateMemory( nSize ); }
281 inline static void * SAL_CALL operator new( size_t,void* _pHint )
282 { return _pHint; }
283 inline static void SAL_CALL operator delete( void * pMem )
284 { ::rtl_freeMemory( pMem ); }
285 inline static void SAL_CALL operator delete( void *,void* )
288 ORowSetValue& operator=(const ORowSetValue& _rRH);
290 // simple types
291 ORowSetValue& operator=(const bool _rRH);
293 ORowSetValue& operator=(const sal_Int8& _rRH);
294 ORowSetValue& operator=(const sal_uInt8& _rRH);
296 ORowSetValue& operator=(const sal_Int16& _rRH);
297 ORowSetValue& operator=(const sal_uInt16& _rRH);
299 ORowSetValue& operator=(const sal_Int32& _rRH);
300 ORowSetValue& operator=(const sal_uInt32& _rRH);
302 ORowSetValue& operator=(const sal_Int64& _rRH);
303 ORowSetValue& operator=(const sal_uInt64& _rRH);
305 ORowSetValue& operator=(const double& _rRH);
306 ORowSetValue& operator=(const float& _rRH);
308 // ADT's
309 ORowSetValue& operator=(const ::com::sun::star::util::Date& _rRH);
310 ORowSetValue& operator=(const ::com::sun::star::util::Time& _rRH);
311 ORowSetValue& operator=(const ::com::sun::star::util::DateTime& _rRH);
313 ORowSetValue& operator=(const OUString& _rRH);
314 // the type isn't set it will be set to VARCHAR if the type is different change it
315 ORowSetValue& operator=(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH);
316 // we the possibility to save a any for bookmarks
317 ORowSetValue& operator=(const ::com::sun::star::uno::Any& _rAny);
319 operator bool() const { return !isNull() && getBool(); }
320 operator sal_Int8() const { return isNull() ? static_cast<sal_Int8>(0) : getInt8(); }
321 operator sal_uInt8() const { return isNull() ? static_cast<sal_uInt8>(0) : getUInt8(); }
323 operator sal_Int16() const { return isNull() ? static_cast<sal_Int16>(0) : getInt16(); }
324 operator sal_uInt16() const { return isNull() ? static_cast<sal_uInt16>(0) : getUInt16(); }
326 operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
327 operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
329 operator sal_Int64() const { return isNull() ? 0 : getLong(); }
330 operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
332 operator float() const { return isNull() ? (float)0.0: getFloat(); }
333 operator double() const { return isNull() ? 0.0 : getDouble(); }
335 operator OUString() const
337 return isNull() ? OUString() : getString();
340 operator ::com::sun::star::util::Date() const
342 return isNull() ? ::com::sun::star::util::Date() : getDate();
345 operator ::com::sun::star::util::Time() const
347 return isNull() ? ::com::sun::star::util::Time() : getTime();
350 operator ::com::sun::star::util::DateTime() const
352 return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
355 operator ::com::sun::star::uno::Sequence<sal_Int8>() const
357 return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
360 bool operator==(const ORowSetValue& _rRH) const;
361 bool operator!=(const ORowSetValue& _rRH) const
363 return !( *this == _rRH );
366 bool isNull() const
368 return m_bNull;
370 void setNull()
372 free();
373 m_bNull = true;
374 m_aValue.m_pString = NULL;
377 bool isBound() const { return m_bBound; }
378 void setBound(bool _bBound) { m_bBound = _bBound; }
380 bool isModified() const { return m_bModified; }
381 void setModified(bool _bMod=true) { m_bModified = _bMod; }
383 bool isSigned() const { return m_bSigned; }
384 void setSigned(bool _bSig=true);
386 sal_Int32 getTypeKind() const { return m_eTypeKind; }
387 void setTypeKind(sal_Int32 _eType);
389 // before calling one of this methods, be sure that the value is not null
390 void* getValue() const { OSL_ENSURE(m_bBound,"Value is not bound!");return m_aValue.m_pValue; }
391 bool getBool() const;
393 sal_Int8 getInt8() const;
394 sal_uInt8 getUInt8() const;
396 sal_Int16 getInt16() const;
397 sal_uInt16 getUInt16() const;
399 sal_Int32 getInt32() const;
400 sal_uInt32 getUInt32() const;
402 sal_Int64 getLong() const;
403 sal_uInt64 getULong() const;
405 double getDouble() const;
406 float getFloat() const;
408 OUString getString() const; // makes a automatic conversion if type isn't a string
409 ::com::sun::star::util::Date getDate() const;
410 ::com::sun::star::util::Time getTime() const;
411 ::com::sun::star::util::DateTime getDateTime() const;
412 ::com::sun::star::uno::Sequence<sal_Int8> getSequence() const;
413 // only use for anys
414 ::com::sun::star::uno::Any getAny() const { return *static_cast<css::uno::Any*>(m_aValue.m_pValue); }
415 ::com::sun::star::uno::Any makeAny() const;
418 fetches a single value out of the row
419 @param _nPos the current column position
420 @param _nType the type of the current column
421 @param _xRow the row where to fetch the data from
423 void fill(sal_Int32 _nPos,
424 sal_Int32 _nType,
425 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
428 fetches a single value out of the row
429 @param _nPos the current column position
430 @param _nType the type of the current column
431 @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
432 @param _xRow the row where to fetch the data from
434 void fill(sal_Int32 _nPos,
435 sal_Int32 _nType,
436 bool _bNullable,
437 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
439 void fill(const ::com::sun::star::uno::Any& _rValue);
441 void fill( const sal_Int32 _nType,
442 const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
444 private:
445 void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
448 /// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
449 class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::salhelper::SimpleReferenceObject
451 ORowSetValue m_aValue; // my own value
452 public:
453 ORowSetValueDecorator(){m_aValue.setBound(true);}
454 ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(true);}
455 ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
457 inline operator const ORowSetValue&() const { return m_aValue; }
458 inline bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
459 inline const ORowSetValue& getValue() const { return m_aValue; }
460 inline ORowSetValue& get() { return m_aValue; }
461 inline void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
462 inline void setNull() { m_aValue.setNull(); }
463 inline void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
464 inline bool isBound( ) const { return m_aValue.isBound();}
465 inline void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
466 inline void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
469 typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
472 /// TSetBound is a unary_function to set the bound value with e.q. for_each call
473 struct OOO_DLLPUBLIC_DBTOOLS TSetBound : ::std::unary_function<ORowSetValue,void>
475 bool m_bBound;
476 TSetBound(bool _bBound) : m_bBound(_bBound){}
477 void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
482 /// TSetBound is a unary_function to set the bound value with e.q. for_each call
483 struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound : ::std::unary_function<ORowSetValueDecoratorRef,void>
485 bool m_bBound;
486 TSetRefBound(bool _bBound) : m_bBound(_bBound){}
487 void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
492 // Vector for file based rows
494 template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
496 bool m_bDeleted;
497 public:
498 ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
499 ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
501 bool isDeleted() const { return m_bDeleted; }
502 void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
505 typedef ODeleteVector< ORowSetValue > OValueVector;
507 class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
509 public:
510 OValueRefVector(){}
511 OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
513 for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
514 *aIter = new ORowSetValueDecorator;
518 #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
519 class OAssignValues : public OValueRefVector
521 ::std::vector<sal_Int32> m_nParameterIndexes;
522 public:
523 OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
524 OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
526 void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
527 sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
530 typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
534 typedef ::rtl::Reference< OValueVector > OValueRow;
535 typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
538 #endif // INCLUDED_CONNECTIVITY_FVALUE_HXX
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */