bump product version to 4.1.6.2
[LibreOffice.git] / include / connectivity / FValue.hxx
blob34e73612bfa1bab1e9fb7c63eba9b50c3fa61c63
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 _CONNECTIVITY_FILE_VALUE_HXX_
21 #define _CONNECTIVITY_FILE_VALUE_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 <comphelper/stl_types.hxx>
29 #include <rtl/ref.hxx>
30 #include "connectivity/dbtoolsdllapi.hxx"
31 #include "connectivity/CommonTools.hxx"
32 #include <com/sun/star/util/DateTime.hpp>
33 #include <com/sun/star/util/Date.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdb/XColumn.hpp>
39 namespace connectivity
41 namespace detail
43 class IValueSource;
46 class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
48 union
50 bool m_bBool;
52 sal_Int8 m_nInt8;
53 sal_uInt8 m_uInt8;
55 sal_Int16 m_nInt16;
56 sal_uInt16 m_uInt16;
58 sal_Int32 m_nInt32;
59 sal_uInt32 m_uInt32;
61 sal_Int64 m_nInt64;
62 sal_uInt64 m_uInt64;
64 float m_nFloat;
65 double m_nDouble;
67 rtl_uString* m_pString;
69 void* m_pValue; // date/time/timestamp/sequence
70 } m_aValue;
72 sal_Int32 m_eTypeKind; // the database type
73 bool m_bNull : 1; // value is null
74 bool m_bBound : 1; // is bound
75 bool m_bModified : 1; // value was changed
76 bool m_bSigned : 1; // value is signed
78 void free();
80 public:
81 ORowSetValue()
82 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
83 ,m_bNull(true)
84 ,m_bBound(true)
85 ,m_bModified(false)
86 ,m_bSigned(true)
88 m_aValue.m_pString = NULL;
91 ORowSetValue(const ORowSetValue& _rRH)
92 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
93 ,m_bNull(true)
94 ,m_bBound(true)
95 ,m_bModified(false)
96 ,m_bSigned(true)
98 m_aValue.m_pString = NULL;
99 operator=(_rRH);
102 ORowSetValue(const OUString& _rRH)
103 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
104 ,m_bNull(true)
105 ,m_bBound(true)
106 ,m_bModified(false)
107 ,m_bSigned(true)
109 m_aValue.m_pString = NULL;
110 operator=(_rRH);
113 ORowSetValue(const double& _rRH)
114 :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE)
115 ,m_bNull(true)
116 ,m_bBound(true)
117 ,m_bModified(false)
118 ,m_bSigned(true)
120 m_aValue.m_pString = NULL;
121 operator=(_rRH);
124 ORowSetValue(const float& _rRH)
125 :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT)
126 ,m_bNull(true)
127 ,m_bBound(true)
128 ,m_bModified(false)
129 ,m_bSigned(true)
131 m_aValue.m_pString = NULL;
132 operator=(_rRH);
135 ORowSetValue(const sal_Int8& _rRH)
136 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
137 ,m_bNull(true)
138 ,m_bBound(true)
139 ,m_bModified(false)
140 ,m_bSigned(true)
142 m_aValue.m_pString = NULL;
143 operator=(_rRH);
146 ORowSetValue(const sal_uInt8& _rRH)
147 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
148 ,m_bNull(true)
149 ,m_bBound(true)
150 ,m_bModified(false)
151 ,m_bSigned(false)
153 m_aValue.m_pString = NULL;
154 operator=(_rRH);
156 ORowSetValue(const sal_Int16& _rRH)
157 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
158 ,m_bNull(true)
159 ,m_bBound(true)
160 ,m_bModified(false)
161 ,m_bSigned(true)
163 m_aValue.m_pString = NULL;
164 operator=(_rRH);
166 ORowSetValue(const sal_uInt16& _rRH)
167 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
168 ,m_bNull(true)
169 ,m_bBound(true)
170 ,m_bModified(false)
171 ,m_bSigned(false)
173 m_aValue.m_pString = NULL;
174 operator=(_rRH);
176 ORowSetValue(const sal_Int32& _rRH)
177 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
178 ,m_bNull(true)
179 ,m_bBound(true)
180 ,m_bModified(false)
181 ,m_bSigned(true)
183 m_aValue.m_pString = NULL;
184 operator=(_rRH);
186 ORowSetValue(const sal_uInt32& _rRH)
187 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
188 ,m_bNull(true)
189 ,m_bBound(true)
190 ,m_bModified(false)
191 ,m_bSigned(false)
193 m_aValue.m_pString = NULL;
194 operator=(_rRH);
196 ORowSetValue(const sal_Int64& _rRH)
197 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
198 ,m_bNull(true)
199 ,m_bBound(true)
200 ,m_bModified(false)
201 ,m_bSigned(true)
203 m_aValue.m_pString = NULL;
204 operator=(_rRH);
206 ORowSetValue(const sal_uInt64& _rRH)
207 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
208 ,m_bNull(true)
209 ,m_bBound(true)
210 ,m_bModified(false)
211 ,m_bSigned(false)
213 m_aValue.m_pString = NULL;
214 operator=(_rRH);
217 ORowSetValue(const bool& _rRH)
218 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT)
219 ,m_bNull(true)
220 ,m_bBound(true)
221 ,m_bModified(false)
222 ,m_bSigned(true)
224 m_aValue.m_pString = NULL;
225 operator=(_rRH);
228 ORowSetValue(const ::com::sun::star::util::Date& _rRH)
229 :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE)
230 ,m_bNull(true)
231 ,m_bBound(true)
232 ,m_bModified(false)
233 ,m_bSigned(true)
235 m_aValue.m_pString = NULL;
236 operator=(_rRH);
239 ORowSetValue(const ::com::sun::star::util::Time& _rRH)
240 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME)
241 ,m_bNull(true)
242 ,m_bBound(true)
243 ,m_bModified(false)
244 ,m_bSigned(true)
246 m_aValue.m_pString = NULL;
247 operator=(_rRH);
250 ORowSetValue(const ::com::sun::star::util::DateTime& _rRH)
251 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP)
252 ,m_bNull(true)
253 ,m_bBound(true)
254 ,m_bModified(false)
255 ,m_bSigned(true)
257 m_aValue.m_pString = NULL;
258 operator=(_rRH);
261 ORowSetValue(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH)
262 :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY)
263 ,m_bNull(true)
264 ,m_bBound(true)
265 ,m_bModified(false)
266 ,m_bSigned(true)
268 m_aValue.m_pString = NULL;
269 operator=(_rRH);
272 ~ORowSetValue()
274 free();
277 inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
278 { return ::rtl_allocateMemory( nSize ); }
279 inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW(())
280 { return _pHint; }
281 inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
282 { ::rtl_freeMemory( pMem ); }
283 inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW(())
286 ORowSetValue& operator=(const ORowSetValue& _rRH);
288 // simple types
289 ORowSetValue& operator=(const bool _rRH);
291 ORowSetValue& operator=(const sal_Int8& _rRH);
292 ORowSetValue& operator=(const sal_uInt8& _rRH);
294 ORowSetValue& operator=(const sal_Int16& _rRH);
295 ORowSetValue& operator=(const sal_uInt16& _rRH);
297 ORowSetValue& operator=(const sal_Int32& _rRH);
298 ORowSetValue& operator=(const sal_uInt32& _rRH);
300 ORowSetValue& operator=(const sal_Int64& _rRH);
301 ORowSetValue& operator=(const sal_uInt64& _rRH);
303 ORowSetValue& operator=(const double& _rRH);
304 ORowSetValue& operator=(const float& _rRH);
306 // ADT's
307 ORowSetValue& operator=(const ::com::sun::star::util::Date& _rRH);
308 ORowSetValue& operator=(const ::com::sun::star::util::Time& _rRH);
309 ORowSetValue& operator=(const ::com::sun::star::util::DateTime& _rRH);
311 ORowSetValue& operator=(const OUString& _rRH);
312 // the type isn't set it will be set to VARCHAR if the type is different change it
313 ORowSetValue& operator=(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH);
314 // we the possiblity to save a any for bookmarks
315 ORowSetValue& operator=(const ::com::sun::star::uno::Any& _rAny);
317 operator bool() const { return isNull() ? false : getBool(); }
318 operator sal_Int8() const { return isNull() ? static_cast<sal_Int8>(0) : getInt8(); }
319 operator sal_uInt8() const { return isNull() ? static_cast<sal_uInt8>(0) : getUInt8(); }
321 operator sal_Int16() const { return isNull() ? static_cast<sal_Int16>(0) : getInt16(); }
322 operator sal_uInt16() const { return isNull() ? static_cast<sal_uInt16>(0) : getUInt16(); }
324 operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
325 operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
327 operator sal_Int64() const { return isNull() ? 0 : getLong(); }
328 operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
330 operator float() const { return isNull() ? (float)0.0: getFloat(); }
331 operator double() const { return isNull() ? 0.0 : getDouble(); }
333 operator OUString() const
335 return isNull() ? OUString() : getString();
338 operator ::com::sun::star::util::Date() const
340 return isNull() ? ::com::sun::star::util::Date() : getDate();
343 operator ::com::sun::star::util::Time() const
345 return isNull() ? ::com::sun::star::util::Time() : getTime();
348 operator ::com::sun::star::util::DateTime() const
350 return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
353 operator ::com::sun::star::uno::Sequence<sal_Int8>() const
355 return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
358 bool operator==(const ORowSetValue& _rRH) const;
359 bool operator!=(const ORowSetValue& _rRH) const
361 return !( *this == _rRH );
364 bool isNull() const
366 return m_bNull;
368 void setNull()
370 free();
371 m_bNull = true;
372 m_aValue.m_pString = NULL;
375 bool isBound() const { return m_bBound; }
376 void setBound(bool _bBound) { m_bBound = _bBound ? 1 : 0; }
378 bool isModified() const { return m_bModified; }
379 void setModified(bool _bMod=true) { m_bModified = _bMod ? 1 : 0; }
381 bool isSigned() const { return m_bSigned; }
382 void setSigned(bool _bSig=true);
384 sal_Int32 getTypeKind() const { return m_eTypeKind; }
385 void setTypeKind(sal_Int32 _eType);
387 // before calling one of this methods, be sure that the value is not null
388 void* getValue() const { OSL_ENSURE(m_bBound,"Value is not bound!");return m_aValue.m_pValue; }
389 bool getBool() const;
391 sal_Int8 getInt8() const;
392 sal_uInt8 getUInt8() const;
394 sal_Int16 getInt16() const;
395 sal_uInt16 getUInt16() const;
397 sal_Int32 getInt32() const;
398 sal_uInt32 getUInt32() const;
400 sal_Int64 getLong() const;
401 sal_uInt64 getULong() const;
403 double getDouble() const;
404 float getFloat() const;
406 OUString getString() const; // makes a automatic conversion if type isn't a string
407 ::com::sun::star::util::Date getDate() const;
408 ::com::sun::star::util::Time getTime() const;
409 ::com::sun::star::util::DateTime getDateTime() const;
410 ::com::sun::star::uno::Sequence<sal_Int8> getSequence() const;
411 // only use for anys
412 ::com::sun::star::uno::Any getAny() const { return *(::com::sun::star::uno::Any*)m_aValue.m_pValue; }
413 ::com::sun::star::uno::Any makeAny() const;
416 fetches a single value out of the row
417 @param _nPos the current column position
418 @param _nType the type of the current column
419 @param _xRow the row where to fetch the data from
421 void fill(sal_Int32 _nPos,
422 sal_Int32 _nType,
423 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
426 fetches a single value out of the row
427 @param _nPos the current column position
428 @param _nType the type of the current column
429 @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
430 @param _xRow the row where to fetch the data from
432 void fill(sal_Int32 _nPos,
433 sal_Int32 _nType,
434 bool _bNullable,
435 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
437 void fill(const ::com::sun::star::uno::Any& _rValue);
439 void fill( const sal_Int32 _nType,
440 const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
442 private:
443 void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
446 /// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
447 class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::salhelper::SimpleReferenceObject
449 ORowSetValue m_aValue; // my own value
450 public:
451 ORowSetValueDecorator(){m_aValue.setBound(true);}
452 ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(true);}
453 ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
455 inline operator const ORowSetValue&() const { return m_aValue; }
456 inline bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
457 inline const ORowSetValue& getValue() const { return m_aValue; }
458 inline ORowSetValue& get() { return m_aValue; }
459 inline void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
460 inline void setNull() { m_aValue.setNull(); }
461 inline void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
462 inline bool isBound( ) const { return m_aValue.isBound();}
463 inline void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
464 inline void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
467 typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
469 // -------------------------------------------------------------------------
470 /// TSetBound is a unary_function to set the bound value with e.q. for_each call
471 struct OOO_DLLPUBLIC_DBTOOLS TSetBound : ::std::unary_function<ORowSetValue,void>
473 bool m_bBound;
474 TSetBound(bool _bBound) : m_bBound(_bBound){}
475 void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
479 // -------------------------------------------------------------------------
480 /// TSetBound is a unary_function to set the bound value with e.q. for_each call
481 struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound : ::std::unary_function<ORowSetValueDecoratorRef,void>
483 bool m_bBound;
484 TSetRefBound(bool _bBound) : m_bBound(_bBound){}
485 void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
489 // ----------------------------------------------------------------------------
490 // Vector for file based rows
491 // ----------------------------------------------------------------------------
492 template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
494 bool m_bDeleted;
495 public:
496 ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
497 ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
499 bool isDeleted() const { return m_bDeleted; }
500 void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
503 typedef ODeleteVector< ORowSetValue > OValueVector;
505 class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
507 public:
508 OValueRefVector(){}
509 OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
511 for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
512 *aIter = new ORowSetValueDecorator;
516 #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
517 class OAssignValues : public OValueRefVector
519 ::std::vector<sal_Int32> m_nParameterIndexes;
520 public:
521 OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
522 OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
524 void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
525 sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
528 typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
532 typedef ::rtl::Reference< OValueVector > OValueRow;
533 typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
536 #endif // #ifndef _CONNECTIVITY_FILE_VALUE_HXX_
539 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */