Bump version to 6.4-15
[LibreOffice.git] / include / connectivity / FValue.hxx
blob44ec1e2393d6b5bda33b8b0d49000063516b4ca3
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 <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
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() noexcept;
79 public:
80 ORowSetValue()
81 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
82 ,m_bNull(true)
83 ,m_bBound(true)
84 ,m_bModified(false)
85 ,m_bSigned(true)
87 m_aValue.m_pString = nullptr;
90 ORowSetValue(const ORowSetValue& _rRH)
91 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
92 ,m_bNull(true)
93 ,m_bBound(true)
94 ,m_bModified(false)
95 ,m_bSigned(true)
97 m_aValue.m_pString = nullptr;
98 operator=(_rRH);
101 ORowSetValue(ORowSetValue&& _rRH) noexcept
102 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
103 ,m_bNull(true)
104 ,m_bBound(true)
105 ,m_bModified(false)
106 ,m_bSigned(true)
108 m_aValue.m_pString = nullptr;
109 operator=(std::move(_rRH));
112 ORowSetValue(const OUString& _rRH)
113 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
114 ,m_bNull(true)
115 ,m_bBound(true)
116 ,m_bModified(false)
117 ,m_bSigned(true)
119 m_aValue.m_pString = nullptr;
120 operator=(_rRH);
123 ORowSetValue(const double& _rRH)
124 :m_eTypeKind(css::sdbc::DataType::DOUBLE)
125 ,m_bNull(true)
126 ,m_bBound(true)
127 ,m_bModified(false)
128 ,m_bSigned(true)
130 m_aValue.m_pString = nullptr;
131 operator=(_rRH);
134 ORowSetValue(float _rRH)
135 :m_eTypeKind(css::sdbc::DataType::FLOAT)
136 ,m_bNull(true)
137 ,m_bBound(true)
138 ,m_bModified(false)
139 ,m_bSigned(true)
141 m_aValue.m_pString = nullptr;
142 operator=(_rRH);
145 ORowSetValue(sal_Int8 _rRH)
146 :m_eTypeKind(css::sdbc::DataType::TINYINT)
147 ,m_bNull(true)
148 ,m_bBound(true)
149 ,m_bModified(false)
150 ,m_bSigned(true)
152 m_aValue.m_pString = nullptr;
153 operator=(_rRH);
156 ORowSetValue(sal_Int16 _rRH)
157 :m_eTypeKind(css::sdbc::DataType::SMALLINT)
158 ,m_bNull(true)
159 ,m_bBound(true)
160 ,m_bModified(false)
161 ,m_bSigned(true)
163 m_aValue.m_pString = nullptr;
164 operator=(_rRH);
166 ORowSetValue(sal_uInt16 _rRH)
167 :m_eTypeKind(css::sdbc::DataType::SMALLINT)
168 ,m_bNull(true)
169 ,m_bBound(true)
170 ,m_bModified(false)
171 ,m_bSigned(false)
173 m_aValue.m_pString = nullptr;
174 operator=(_rRH);
176 ORowSetValue(sal_Int32 _rRH)
177 :m_eTypeKind(css::sdbc::DataType::INTEGER)
178 ,m_bNull(true)
179 ,m_bBound(true)
180 ,m_bModified(false)
181 ,m_bSigned(true)
183 m_aValue.m_pString = nullptr;
184 operator=(_rRH);
186 ORowSetValue(sal_uInt32 _rRH)
187 :m_eTypeKind(css::sdbc::DataType::INTEGER)
188 ,m_bNull(true)
189 ,m_bBound(true)
190 ,m_bModified(false)
191 ,m_bSigned(false)
193 m_aValue.m_pString = nullptr;
194 operator=(_rRH);
196 ORowSetValue(sal_Int64 _rRH)
197 :m_eTypeKind(css::sdbc::DataType::BIGINT)
198 ,m_bNull(true)
199 ,m_bBound(true)
200 ,m_bModified(false)
201 ,m_bSigned(true)
203 m_aValue.m_pString = nullptr;
204 operator=(_rRH);
206 ORowSetValue(sal_uInt64 _rRH)
207 :m_eTypeKind(css::sdbc::DataType::BIGINT)
208 ,m_bNull(true)
209 ,m_bBound(true)
210 ,m_bModified(false)
211 ,m_bSigned(false)
213 m_aValue.m_pString = nullptr;
214 operator=(_rRH);
217 ORowSetValue(bool _rRH)
218 :m_eTypeKind(css::sdbc::DataType::BIT)
219 ,m_bNull(true)
220 ,m_bBound(true)
221 ,m_bModified(false)
222 ,m_bSigned(true)
224 m_aValue.m_pString = nullptr;
225 operator=(_rRH);
227 ORowSetValue(sal_Bool) = delete; // aka sal_uInt8
229 ORowSetValue(const css::util::Date& _rRH)
230 :m_eTypeKind(css::sdbc::DataType::DATE)
231 ,m_bNull(true)
232 ,m_bBound(true)
233 ,m_bModified(false)
234 ,m_bSigned(true)
236 m_aValue.m_pString = nullptr;
237 operator=(_rRH);
240 ORowSetValue(const css::util::Time& _rRH)
241 :m_eTypeKind(css::sdbc::DataType::TIME)
242 ,m_bNull(true)
243 ,m_bBound(true)
244 ,m_bModified(false)
245 ,m_bSigned(true)
247 m_aValue.m_pString = nullptr;
248 operator=(_rRH);
251 ORowSetValue(const css::util::DateTime& _rRH)
252 :m_eTypeKind(css::sdbc::DataType::TIMESTAMP)
253 ,m_bNull(true)
254 ,m_bBound(true)
255 ,m_bModified(false)
256 ,m_bSigned(true)
258 m_aValue.m_pString = nullptr;
259 operator=(_rRH);
262 ORowSetValue(const css::uno::Sequence<sal_Int8>& _rRH)
263 :m_eTypeKind(css::sdbc::DataType::LONGVARBINARY)
264 ,m_bNull(true)
265 ,m_bBound(true)
266 ,m_bModified(false)
267 ,m_bSigned(true)
269 m_aValue.m_pString = nullptr;
270 operator=(_rRH);
273 // Avoid accidental uses of ORowSetValue(bool const &):
274 template<typename T> ORowSetValue(T const *) = delete;
276 ~ORowSetValue()
278 free();
281 ORowSetValue& operator=(const ORowSetValue& _rRH);
282 ORowSetValue& operator=(ORowSetValue&& _rRH) noexcept;
284 // simple types
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);
302 // ADT's
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 );
360 bool isNull() const
362 return m_bNull;
364 void setNull()
366 free();
367 m_bNull = true;
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;
406 // only use for anys
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,
417 sal_Int32 _nType,
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,
428 sal_Int32 _nType,
429 bool _bNullable,
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 );
437 private:
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
445 public:
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
468 bool m_bBound;
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
478 bool m_bBound;
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 >
489 bool m_bDeleted;
490 public:
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 >
502 public:
503 OValueRefVector(){}
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;
515 public:
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: */