cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / include / connectivity / FValue.hxx
blob6bd54fb29ddcefb61354cd8780f637c698c8b2a7
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>
34 #include <utility>
36 namespace com::sun::star::sdb { class XColumn; }
37 namespace com::sun::star::sdbc { class XRow; }
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() noexcept;
80 public:
81 ORowSetValue()
82 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
83 ,m_bNull(true)
84 ,m_bBound(true)
85 ,m_bModified(false)
86 ,m_bSigned(true)
88 m_aValue.m_pString = nullptr;
91 ORowSetValue(const ORowSetValue& _rRH)
92 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
93 ,m_bNull(true)
94 ,m_bBound(true)
95 ,m_bModified(false)
96 ,m_bSigned(true)
98 m_aValue.m_pString = nullptr;
99 operator=(_rRH);
102 ORowSetValue(ORowSetValue&& _rRH) noexcept
103 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
104 ,m_bNull(true)
105 ,m_bBound(true)
106 ,m_bModified(false)
107 ,m_bSigned(true)
109 m_aValue.m_pString = nullptr;
110 operator=(std::move(_rRH));
113 ORowSetValue(const OUString& _rRH)
114 :m_eTypeKind(css::sdbc::DataType::VARCHAR)
115 ,m_bNull(true)
116 ,m_bBound(true)
117 ,m_bModified(false)
118 ,m_bSigned(true)
120 m_aValue.m_pString = nullptr;
121 operator=(_rRH);
124 ORowSetValue(const double& _rRH)
125 :m_eTypeKind(css::sdbc::DataType::DOUBLE)
126 ,m_bNull(true)
127 ,m_bBound(true)
128 ,m_bModified(false)
129 ,m_bSigned(true)
131 m_aValue.m_pString = nullptr;
132 operator=(_rRH);
135 ORowSetValue(float _rRH)
136 :m_eTypeKind(css::sdbc::DataType::FLOAT)
137 ,m_bNull(true)
138 ,m_bBound(true)
139 ,m_bModified(false)
140 ,m_bSigned(true)
142 m_aValue.m_pString = nullptr;
143 operator=(_rRH);
146 ORowSetValue(sal_Int8 _rRH)
147 :m_eTypeKind(css::sdbc::DataType::TINYINT)
148 ,m_bNull(true)
149 ,m_bBound(true)
150 ,m_bModified(false)
151 ,m_bSigned(true)
153 m_aValue.m_pString = nullptr;
154 operator=(_rRH);
157 ORowSetValue(sal_Int16 _rRH)
158 :m_eTypeKind(css::sdbc::DataType::SMALLINT)
159 ,m_bNull(true)
160 ,m_bBound(true)
161 ,m_bModified(false)
162 ,m_bSigned(true)
164 m_aValue.m_pString = nullptr;
165 operator=(_rRH);
167 ORowSetValue(sal_uInt16 _rRH)
168 :m_eTypeKind(css::sdbc::DataType::SMALLINT)
169 ,m_bNull(true)
170 ,m_bBound(true)
171 ,m_bModified(false)
172 ,m_bSigned(false)
174 m_aValue.m_pString = nullptr;
175 operator=(_rRH);
177 ORowSetValue(sal_Int32 _rRH)
178 :m_eTypeKind(css::sdbc::DataType::INTEGER)
179 ,m_bNull(true)
180 ,m_bBound(true)
181 ,m_bModified(false)
182 ,m_bSigned(true)
184 m_aValue.m_pString = nullptr;
185 operator=(_rRH);
187 ORowSetValue(sal_uInt32 _rRH)
188 :m_eTypeKind(css::sdbc::DataType::INTEGER)
189 ,m_bNull(true)
190 ,m_bBound(true)
191 ,m_bModified(false)
192 ,m_bSigned(false)
194 m_aValue.m_pString = nullptr;
195 operator=(_rRH);
197 ORowSetValue(sal_Int64 _rRH)
198 :m_eTypeKind(css::sdbc::DataType::BIGINT)
199 ,m_bNull(true)
200 ,m_bBound(true)
201 ,m_bModified(false)
202 ,m_bSigned(true)
204 m_aValue.m_pString = nullptr;
205 operator=(_rRH);
207 ORowSetValue(sal_uInt64 _rRH)
208 :m_eTypeKind(css::sdbc::DataType::BIGINT)
209 ,m_bNull(true)
210 ,m_bBound(true)
211 ,m_bModified(false)
212 ,m_bSigned(false)
214 m_aValue.m_pString = nullptr;
215 operator=(_rRH);
218 ORowSetValue(bool _rRH)
219 :m_eTypeKind(css::sdbc::DataType::BIT)
220 ,m_bNull(true)
221 ,m_bBound(true)
222 ,m_bModified(false)
223 ,m_bSigned(true)
225 m_aValue.m_pString = nullptr;
226 operator=(_rRH);
228 ORowSetValue(sal_Bool) = delete; // aka sal_uInt8
230 ORowSetValue(const css::util::Date& _rRH)
231 :m_eTypeKind(css::sdbc::DataType::DATE)
232 ,m_bNull(true)
233 ,m_bBound(true)
234 ,m_bModified(false)
235 ,m_bSigned(true)
237 m_aValue.m_pString = nullptr;
238 operator=(_rRH);
241 ORowSetValue(const css::util::Time& _rRH)
242 :m_eTypeKind(css::sdbc::DataType::TIME)
243 ,m_bNull(true)
244 ,m_bBound(true)
245 ,m_bModified(false)
246 ,m_bSigned(true)
248 m_aValue.m_pString = nullptr;
249 operator=(_rRH);
252 ORowSetValue(const css::util::DateTime& _rRH)
253 :m_eTypeKind(css::sdbc::DataType::TIMESTAMP)
254 ,m_bNull(true)
255 ,m_bBound(true)
256 ,m_bModified(false)
257 ,m_bSigned(true)
259 m_aValue.m_pString = nullptr;
260 operator=(_rRH);
263 ORowSetValue(const css::uno::Sequence<sal_Int8>& _rRH)
264 :m_eTypeKind(css::sdbc::DataType::LONGVARBINARY)
265 ,m_bNull(true)
266 ,m_bBound(true)
267 ,m_bModified(false)
268 ,m_bSigned(true)
270 m_aValue.m_pString = nullptr;
271 operator=(_rRH);
274 // Avoid accidental uses of ORowSetValue(bool const &):
275 template<typename T> ORowSetValue(T const *) = delete;
277 ~ORowSetValue()
279 free();
282 ORowSetValue& operator=(const ORowSetValue& _rRH);
283 ORowSetValue& operator=(ORowSetValue&& _rRH) noexcept;
285 // simple types
286 ORowSetValue& operator=(bool _rRH);
287 void operator =(sal_Bool) = delete; // aka sal_uInt8
289 ORowSetValue& operator=(sal_Int8 _rRH);
291 ORowSetValue& operator=(sal_Int16 _rRH);
292 ORowSetValue& operator=(sal_uInt16 _rRH);
294 ORowSetValue& operator=(sal_Int32 _rRH);
295 ORowSetValue& operator=(sal_uInt32 _rRH);
297 ORowSetValue& operator=(sal_Int64 _rRH);
298 ORowSetValue& operator=(sal_uInt64 _rRH);
300 ORowSetValue& operator=(double _rRH);
301 ORowSetValue& operator=(float _rRH);
303 // ADT's
304 ORowSetValue& operator=(const css::util::Date& _rRH);
305 ORowSetValue& operator=(const css::util::Time& _rRH);
306 ORowSetValue& operator=(const css::util::DateTime& _rRH);
308 ORowSetValue& operator=(const OUString& _rRH);
309 // the type isn't set it will be set to VARCHAR if the type is different change it
310 ORowSetValue& operator=(const css::uno::Sequence<sal_Int8>& _rRH);
311 // with the possibility to save an any for bookmarks
312 ORowSetValue& operator=(const css::uno::Any& _rAny);
314 bool operator==(const ORowSetValue& _rRH) const;
315 bool operator!=(const ORowSetValue& _rRH) const
317 return !( *this == _rRH );
320 bool isNull() const
322 return m_bNull;
324 void setNull()
326 free();
327 m_bNull = true;
328 m_aValue.m_pString = nullptr;
331 bool isBound() const { return m_bBound; }
332 void setBound(bool _bBound) { m_bBound = _bBound; }
334 bool isModified() const { return m_bModified; }
335 void setModified(bool _bMod) { m_bModified = _bMod; }
337 bool isSigned() const { return m_bSigned; }
338 void setSigned(bool _bSig);
340 sal_Int32 getTypeKind() const { return m_eTypeKind; }
341 void setTypeKind(sal_Int32 _eType);
343 bool getBool() const;
345 sal_Int8 getInt8() const;
346 sal_uInt8 getUInt8() const;
348 sal_Int16 getInt16() const;
349 sal_uInt16 getUInt16() const;
351 sal_Int32 getInt32() const;
352 sal_uInt32 getUInt32() const;
354 sal_Int64 getLong() const;
355 sal_uInt64 getULong() const;
357 double getDouble() const;
358 float getFloat() const;
360 OUString getString() const; // makes an automatic conversion if type isn't a string
361 css::util::Date getDate() const;
362 css::util::Time getTime() const;
363 css::util::DateTime getDateTime() const;
364 css::uno::Sequence<sal_Int8> getSequence() const;
365 // only use for anys
366 const css::uno::Any& getAny() const { return *static_cast<css::uno::Any*>(m_aValue.m_pValue); }
367 css::uno::Any makeAny() const;
370 fetches a single value out of the row
371 @param _nPos the current column position
372 @param _nType the type of the current column
373 @param _xRow the row where to fetch the data from
375 void fill(sal_Int32 _nPos,
376 sal_Int32 _nType,
377 const css::uno::Reference< css::sdbc::XRow>& _xRow);
380 fetches a single value out of the row
381 @param _nPos the current column position
382 @param _nType the type of the current column
383 @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
384 @param _xRow the row where to fetch the data from
386 void fill(sal_Int32 _nPos,
387 sal_Int32 _nType,
388 bool _bNullable,
389 const css::uno::Reference< css::sdbc::XRow>& _xRow);
391 void fill(const css::uno::Any& _rValue);
393 void fill( const sal_Int32 _nType,
394 const css::uno::Reference< css::sdb::XColumn >& _rxColumn );
396 private:
397 void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
400 /// ORowSetValueDecorator decorates an ORowSetValue so the value is "refcounted"
401 class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator final : public ::salhelper::SimpleReferenceObject
403 ORowSetValue m_aValue; // my own value
404 public:
405 ORowSetValueDecorator(){m_aValue.setBound(true);}
406 ORowSetValueDecorator(ORowSetValue _aValue) : m_aValue(std::move(_aValue)){m_aValue.setBound(true);}
407 ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
409 operator const ORowSetValue&() const { return m_aValue; }
410 bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
411 const ORowSetValue& getValue() const { return m_aValue; }
412 ORowSetValue& get() { return m_aValue; }
413 void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
414 void setNull() { m_aValue.setNull(); }
415 void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
416 bool isBound( ) const { return m_aValue.isBound();}
417 void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
418 void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
421 typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
424 /// TSetBound is a functor to set the bound value with e.q. for_each call
425 struct OOO_DLLPUBLIC_DBTOOLS TSetBound
427 bool m_bBound;
428 TSetBound(bool _bBound) : m_bBound(_bBound){}
429 void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
434 /// TSetBound is a functor to set the bound value with e.q. for_each call
435 struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound
437 bool m_bBound;
438 TSetRefBound(bool _bBound) : m_bBound(_bBound){}
439 void operator()(ORowSetValueDecoratorRef const & _rValue) const { _rValue->setBound(m_bBound); }
444 // Vector for file based rows
446 template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
448 bool m_bDeleted;
449 public:
450 ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
451 ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
453 bool isDeleted() const { return m_bDeleted; }
454 void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
457 typedef ODeleteVector< ORowSetValue > OValueVector;
459 class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
461 public:
462 OValueRefVector(){}
463 OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
465 for (auto & elem : *this)
466 elem = new ORowSetValueDecorator;
470 #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
471 class OAssignValues final : public OValueRefVector
473 ::std::vector<sal_Int32> m_nParameterIndexes;
474 public:
475 OAssignValues(size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
477 void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
478 sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
481 typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
484 typedef ::rtl::Reference< OValueVector > OValueRow;
485 typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
488 #endif // INCLUDED_CONNECTIVITY_FVALUE_HXX
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */