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 #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
45 class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
66 rtl_uString
* m_pString
;
68 void* m_pValue
; // date/time/timestamp/sequence
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
81 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
87 m_aValue
.m_pString
= NULL
;
90 ORowSetValue(const ORowSetValue
& _rRH
)
91 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
97 m_aValue
.m_pString
= NULL
;
101 ORowSetValue(const OUString
& _rRH
)
102 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
108 m_aValue
.m_pString
= NULL
;
112 ORowSetValue(const double& _rRH
)
113 :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE
)
119 m_aValue
.m_pString
= NULL
;
123 ORowSetValue(const float& _rRH
)
124 :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT
)
130 m_aValue
.m_pString
= NULL
;
134 ORowSetValue(const sal_Int8
& _rRH
)
135 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT
)
141 m_aValue
.m_pString
= NULL
;
145 ORowSetValue(const sal_uInt8
& _rRH
)
146 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT
)
152 m_aValue
.m_pString
= NULL
;
155 ORowSetValue(const sal_Int16
& _rRH
)
156 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT
)
162 m_aValue
.m_pString
= NULL
;
165 ORowSetValue(const sal_uInt16
& _rRH
)
166 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT
)
172 m_aValue
.m_pString
= NULL
;
175 ORowSetValue(const sal_Int32
& _rRH
)
176 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER
)
182 m_aValue
.m_pString
= NULL
;
185 ORowSetValue(const sal_uInt32
& _rRH
)
186 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER
)
192 m_aValue
.m_pString
= NULL
;
195 ORowSetValue(const sal_Int64
& _rRH
)
196 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT
)
202 m_aValue
.m_pString
= NULL
;
205 ORowSetValue(const sal_uInt64
& _rRH
)
206 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT
)
212 m_aValue
.m_pString
= NULL
;
216 ORowSetValue(const bool& _rRH
)
217 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT
)
223 m_aValue
.m_pString
= NULL
;
227 ORowSetValue(const ::com::sun::star::util::Date
& _rRH
)
228 :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE
)
234 m_aValue
.m_pString
= NULL
;
238 ORowSetValue(const ::com::sun::star::util::Time
& _rRH
)
239 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME
)
245 m_aValue
.m_pString
= NULL
;
249 ORowSetValue(const ::com::sun::star::util::DateTime
& _rRH
)
250 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP
)
256 m_aValue
.m_pString
= NULL
;
260 ORowSetValue(const ::com::sun::star::uno::Sequence
<sal_Int8
>& _rRH
)
261 :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY
)
267 m_aValue
.m_pString
= NULL
;
271 // Avoid accidental uses of ORowSetValue(bool const &):
272 template<typename T
> ORowSetValue(T
const *) = delete;
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
)
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
);
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
);
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
);
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;
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
,
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
,
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
);
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
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>
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>
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
>
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
>
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
;
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: */