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 _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
46 class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
67 rtl_uString
* m_pString
;
69 void* m_pValue
; // date/time/timestamp/sequence
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
82 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
88 m_aValue
.m_pString
= NULL
;
91 ORowSetValue(const ORowSetValue
& _rRH
)
92 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
98 m_aValue
.m_pString
= NULL
;
102 ORowSetValue(const OUString
& _rRH
)
103 :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR
)
109 m_aValue
.m_pString
= NULL
;
113 ORowSetValue(const double& _rRH
)
114 :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE
)
120 m_aValue
.m_pString
= NULL
;
124 ORowSetValue(const float& _rRH
)
125 :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT
)
131 m_aValue
.m_pString
= NULL
;
135 ORowSetValue(const sal_Int8
& _rRH
)
136 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT
)
142 m_aValue
.m_pString
= NULL
;
146 ORowSetValue(const sal_uInt8
& _rRH
)
147 :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT
)
153 m_aValue
.m_pString
= NULL
;
156 ORowSetValue(const sal_Int16
& _rRH
)
157 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT
)
163 m_aValue
.m_pString
= NULL
;
166 ORowSetValue(const sal_uInt16
& _rRH
)
167 :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT
)
173 m_aValue
.m_pString
= NULL
;
176 ORowSetValue(const sal_Int32
& _rRH
)
177 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER
)
183 m_aValue
.m_pString
= NULL
;
186 ORowSetValue(const sal_uInt32
& _rRH
)
187 :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER
)
193 m_aValue
.m_pString
= NULL
;
196 ORowSetValue(const sal_Int64
& _rRH
)
197 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT
)
203 m_aValue
.m_pString
= NULL
;
206 ORowSetValue(const sal_uInt64
& _rRH
)
207 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT
)
213 m_aValue
.m_pString
= NULL
;
217 ORowSetValue(const bool& _rRH
)
218 :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT
)
224 m_aValue
.m_pString
= NULL
;
228 ORowSetValue(const ::com::sun::star::util::Date
& _rRH
)
229 :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE
)
235 m_aValue
.m_pString
= NULL
;
239 ORowSetValue(const ::com::sun::star::util::Time
& _rRH
)
240 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME
)
246 m_aValue
.m_pString
= NULL
;
250 ORowSetValue(const ::com::sun::star::util::DateTime
& _rRH
)
251 :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP
)
257 m_aValue
.m_pString
= NULL
;
261 ORowSetValue(const ::com::sun::star::uno::Sequence
<sal_Int8
>& _rRH
)
262 :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY
)
268 m_aValue
.m_pString
= NULL
;
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(())
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
);
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
);
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
);
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;
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
,
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
,
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
);
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
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>
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>
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
>
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
>
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
;
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: */