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 <rtl/ref.hxx>
28 #include <connectivity/dbtoolsdllapi.hxx>
29 #include <connectivity/CommonTools.hxx>
30 #include <com/sun/star/util/DateTime.hpp>
31 #include <com/sun/star/util/Date.hpp>
32 #include <com/sun/star/util/Time.hpp>
33 #include <com/sun/star/uno/Sequence.hxx>
35 namespace com::sun::star::sdb
{ class XColumn
; }
36 namespace com::sun::star::sdbc
{ class XRow
; }
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(css::sdbc::DataType::VARCHAR
)
87 m_aValue
.m_pString
= nullptr;
90 ORowSetValue(const ORowSetValue
& _rRH
)
91 :m_eTypeKind(css::sdbc::DataType::VARCHAR
)
97 m_aValue
.m_pString
= nullptr;
101 ORowSetValue(ORowSetValue
&& _rRH
) noexcept
102 :m_eTypeKind(css::sdbc::DataType::VARCHAR
)
108 m_aValue
.m_pString
= nullptr;
109 operator=(std::move(_rRH
));
112 ORowSetValue(const OUString
& _rRH
)
113 :m_eTypeKind(css::sdbc::DataType::VARCHAR
)
119 m_aValue
.m_pString
= nullptr;
123 ORowSetValue(const double& _rRH
)
124 :m_eTypeKind(css::sdbc::DataType::DOUBLE
)
130 m_aValue
.m_pString
= nullptr;
134 ORowSetValue(float _rRH
)
135 :m_eTypeKind(css::sdbc::DataType::FLOAT
)
141 m_aValue
.m_pString
= nullptr;
145 ORowSetValue(sal_Int8 _rRH
)
146 :m_eTypeKind(css::sdbc::DataType::TINYINT
)
152 m_aValue
.m_pString
= nullptr;
156 ORowSetValue(sal_Int16 _rRH
)
157 :m_eTypeKind(css::sdbc::DataType::SMALLINT
)
163 m_aValue
.m_pString
= nullptr;
166 ORowSetValue(sal_uInt16 _rRH
)
167 :m_eTypeKind(css::sdbc::DataType::SMALLINT
)
173 m_aValue
.m_pString
= nullptr;
176 ORowSetValue(sal_Int32 _rRH
)
177 :m_eTypeKind(css::sdbc::DataType::INTEGER
)
183 m_aValue
.m_pString
= nullptr;
186 ORowSetValue(sal_uInt32 _rRH
)
187 :m_eTypeKind(css::sdbc::DataType::INTEGER
)
193 m_aValue
.m_pString
= nullptr;
196 ORowSetValue(sal_Int64 _rRH
)
197 :m_eTypeKind(css::sdbc::DataType::BIGINT
)
203 m_aValue
.m_pString
= nullptr;
206 ORowSetValue(sal_uInt64 _rRH
)
207 :m_eTypeKind(css::sdbc::DataType::BIGINT
)
213 m_aValue
.m_pString
= nullptr;
217 ORowSetValue(bool _rRH
)
218 :m_eTypeKind(css::sdbc::DataType::BIT
)
224 m_aValue
.m_pString
= nullptr;
227 ORowSetValue(sal_Bool
) = delete; // aka sal_uInt8
229 ORowSetValue(const css::util::Date
& _rRH
)
230 :m_eTypeKind(css::sdbc::DataType::DATE
)
236 m_aValue
.m_pString
= nullptr;
240 ORowSetValue(const css::util::Time
& _rRH
)
241 :m_eTypeKind(css::sdbc::DataType::TIME
)
247 m_aValue
.m_pString
= nullptr;
251 ORowSetValue(const css::util::DateTime
& _rRH
)
252 :m_eTypeKind(css::sdbc::DataType::TIMESTAMP
)
258 m_aValue
.m_pString
= nullptr;
262 ORowSetValue(const css::uno::Sequence
<sal_Int8
>& _rRH
)
263 :m_eTypeKind(css::sdbc::DataType::LONGVARBINARY
)
269 m_aValue
.m_pString
= nullptr;
273 // Avoid accidental uses of ORowSetValue(bool const &):
274 template<typename T
> ORowSetValue(T
const *) = delete;
281 ORowSetValue
& operator=(const ORowSetValue
& _rRH
);
282 ORowSetValue
& operator=(ORowSetValue
&& _rRH
) noexcept
;
285 ORowSetValue
& operator=(bool _rRH
);
286 void operator =(sal_Bool
) = delete; // aka sal_uInt8
288 ORowSetValue
& operator=(sal_Int8 _rRH
);
290 ORowSetValue
& operator=(sal_Int16 _rRH
);
291 ORowSetValue
& operator=(sal_uInt16 _rRH
);
293 ORowSetValue
& operator=(sal_Int32 _rRH
);
294 ORowSetValue
& operator=(sal_uInt32 _rRH
);
296 ORowSetValue
& operator=(sal_Int64 _rRH
);
297 ORowSetValue
& operator=(sal_uInt64 _rRH
);
299 ORowSetValue
& operator=(double _rRH
);
300 ORowSetValue
& operator=(float _rRH
);
303 ORowSetValue
& operator=(const css::util::Date
& _rRH
);
304 ORowSetValue
& operator=(const css::util::Time
& _rRH
);
305 ORowSetValue
& operator=(const css::util::DateTime
& _rRH
);
307 ORowSetValue
& operator=(const OUString
& _rRH
);
308 // the type isn't set it will be set to VARCHAR if the type is different change it
309 ORowSetValue
& operator=(const css::uno::Sequence
<sal_Int8
>& _rRH
);
310 // with the possibility to save an any for bookmarks
311 ORowSetValue
& operator=(const css::uno::Any
& _rAny
);
313 operator bool() const { return !isNull() && getBool(); }
314 operator sal_Bool() const = delete; // aka sal_uInt8
315 operator sal_Int8() const { return isNull() ? static_cast<sal_Int8
>(0) : getInt8(); }
317 operator sal_Int16() const { return isNull() ? static_cast<sal_Int16
>(0) : getInt16(); }
318 operator sal_uInt16() const { return isNull() ? static_cast<sal_uInt16
>(0) : getUInt16(); }
320 operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
321 operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
323 operator sal_Int64() const { return isNull() ? 0 : getLong(); }
324 operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
326 operator float() const { return isNull() ? float(0.0): getFloat(); }
327 operator double() const { return isNull() ? 0.0 : getDouble(); }
329 operator OUString() const
331 return isNull() ? OUString() : getString();
334 operator css::util::Date() const
336 return isNull() ? css::util::Date() : getDate();
339 operator css::util::Time() const
341 return isNull() ? css::util::Time() : getTime();
344 operator css::util::DateTime() const
346 return isNull() ? css::util::DateTime() : getDateTime();
349 operator css::uno::Sequence
<sal_Int8
>() const
351 return isNull() ? css::uno::Sequence
<sal_Int8
>() : getSequence();
354 bool operator==(const ORowSetValue
& _rRH
) const;
355 bool operator!=(const ORowSetValue
& _rRH
) const
357 return !( *this == _rRH
);
368 m_aValue
.m_pString
= nullptr;
371 bool isBound() const { return m_bBound
; }
372 void setBound(bool _bBound
) { m_bBound
= _bBound
; }
374 bool isModified() const { return m_bModified
; }
375 void setModified(bool _bMod
) { m_bModified
= _bMod
; }
377 bool isSigned() const { return m_bSigned
; }
378 void setSigned(bool _bSig
);
380 sal_Int32
getTypeKind() const { return m_eTypeKind
; }
381 void setTypeKind(sal_Int32 _eType
);
383 // before calling one of this methods, be sure that the value is not null
384 bool getBool() const;
386 sal_Int8
getInt8() const;
387 sal_uInt8
getUInt8() const;
389 sal_Int16
getInt16() const;
390 sal_uInt16
getUInt16() const;
392 sal_Int32
getInt32() const;
393 sal_uInt32
getUInt32() const;
395 sal_Int64
getLong() const;
396 sal_uInt64
getULong() const;
398 double getDouble() const;
399 float getFloat() const;
401 OUString
getString() const; // makes an automatic conversion if type isn't a string
402 css::util::Date
getDate() const;
403 css::util::Time
getTime() const;
404 css::util::DateTime
getDateTime() const;
405 css::uno::Sequence
<sal_Int8
> getSequence() const;
407 const css::uno::Any
& getAny() const { return *static_cast<css::uno::Any
*>(m_aValue
.m_pValue
); }
408 css::uno::Any
makeAny() const;
411 fetches a single value out of the row
412 @param _nPos the current column position
413 @param _nType the type of the current column
414 @param _xRow the row where to fetch the data from
416 void fill(sal_Int32 _nPos
,
418 const css::uno::Reference
< css::sdbc::XRow
>& _xRow
);
421 fetches a single value out of the row
422 @param _nPos the current column position
423 @param _nType the type of the current column
424 @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
425 @param _xRow the row where to fetch the data from
427 void fill(sal_Int32 _nPos
,
430 const css::uno::Reference
< css::sdbc::XRow
>& _xRow
);
432 void fill(const css::uno::Any
& _rValue
);
434 void fill( const sal_Int32 _nType
,
435 const css::uno::Reference
< css::sdb::XColumn
>& _rxColumn
);
438 void impl_fill( const sal_Int32 _nType
, bool _bNullable
, const detail::IValueSource
& _rValueSource
);
441 /// ORowSetValueDecorator decorates an ORowSetValue so the value is "refcounted"
442 class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator final
: public ::salhelper::SimpleReferenceObject
444 ORowSetValue m_aValue
; // my own value
446 ORowSetValueDecorator(){m_aValue
.setBound(true);}
447 ORowSetValueDecorator(const ORowSetValue
& _aValue
) : m_aValue(_aValue
){m_aValue
.setBound(true);}
448 ORowSetValueDecorator
& operator=(const ORowSetValue
& _aValue
);
450 operator const ORowSetValue
&() const { return m_aValue
; }
451 bool operator ==( const ORowSetValue
& _rRH
) { return m_aValue
== _rRH
; }
452 const ORowSetValue
& getValue() const { return m_aValue
; }
453 ORowSetValue
& get() { return m_aValue
; }
454 void setValue(const ORowSetValue
& _aValue
) { m_aValue
= _aValue
; }
455 void setNull() { m_aValue
.setNull(); }
456 void setBound(bool _bBound
) { m_aValue
.setBound(_bBound
);}
457 bool isBound( ) const { return m_aValue
.isBound();}
458 void setTypeKind(sal_Int32 _nType
) { m_aValue
.setTypeKind(_nType
); }
459 void setModified(bool _bModified
) { m_aValue
.setModified(_bModified
); }
462 typedef ::rtl::Reference
<ORowSetValueDecorator
> ORowSetValueDecoratorRef
;
465 /// TSetBound is a functor to set the bound value with e.q. for_each call
466 struct OOO_DLLPUBLIC_DBTOOLS TSetBound
469 TSetBound(bool _bBound
) : m_bBound(_bBound
){}
470 void operator()(ORowSetValue
& _rValue
) const { _rValue
.setBound(m_bBound
); }
475 /// TSetBound is a functor to set the bound value with e.q. for_each call
476 struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound
479 TSetRefBound(bool _bBound
) : m_bBound(_bBound
){}
480 void operator()(ORowSetValueDecoratorRef
const & _rValue
) const { _rValue
->setBound(m_bBound
); }
485 // Vector for file based rows
487 template< class VectorVal
> class ODeleteVector
: public connectivity::ORowVector
< VectorVal
>
491 ODeleteVector() : connectivity::ORowVector
< VectorVal
>() ,m_bDeleted(false) {}
492 ODeleteVector(size_t _st
) : connectivity::ORowVector
< VectorVal
>(_st
) ,m_bDeleted(false) {}
494 bool isDeleted() const { return m_bDeleted
; }
495 void setDeleted(bool _bDeleted
) { m_bDeleted
= _bDeleted
; }
498 typedef ODeleteVector
< ORowSetValue
> OValueVector
;
500 class OOO_DLLPUBLIC_DBTOOLS OValueRefVector
: public ODeleteVector
< ORowSetValueDecoratorRef
>
504 OValueRefVector(size_t _st
) : ODeleteVector
< ORowSetValueDecoratorRef
>(_st
)
506 for (auto & elem
: get())
507 elem
= new ORowSetValueDecorator
;
511 #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
512 class OAssignValues final
: public OValueRefVector
514 ::std::vector
<sal_Int32
> m_nParameterIndexes
;
516 OAssignValues(Vector::size_type n
) : OValueRefVector(n
),m_nParameterIndexes(n
+1,SQL_NO_PARAMETER
){}
518 void setParameterIndex(sal_Int32 _nId
,sal_Int32 _nParameterIndex
) { m_nParameterIndexes
[_nId
] = _nParameterIndex
;}
519 sal_Int32
getParameterIndex(sal_Int32 _nId
) const { return m_nParameterIndexes
[_nId
]; }
522 typedef ::rtl::Reference
< OAssignValues
> ORefAssignValues
;
525 typedef ::rtl::Reference
< OValueVector
> OValueRow
;
526 typedef ::rtl::Reference
< OValueRefVector
> OValueRefRow
;
529 #endif // INCLUDED_CONNECTIVITY_FVALUE_HXX
532 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */