Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_CONNECTIVITY_FVALUE_HXX
21 : #define INCLUDED_CONNECTIVITY_FVALUE_HXX
22 :
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>
37 :
38 : namespace connectivity
39 : {
40 : namespace detail
41 : {
42 : class IValueSource;
43 : }
44 :
45 : class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
46 : {
47 : union
48 : {
49 : bool m_bBool;
50 :
51 : sal_Int8 m_nInt8;
52 : sal_uInt8 m_uInt8;
53 :
54 : sal_Int16 m_nInt16;
55 : sal_uInt16 m_uInt16;
56 :
57 : sal_Int32 m_nInt32;
58 : sal_uInt32 m_uInt32;
59 :
60 : sal_Int64 m_nInt64;
61 : sal_uInt64 m_uInt64;
62 :
63 : float m_nFloat;
64 : double m_nDouble;
65 :
66 : rtl_uString* m_pString;
67 :
68 : void* m_pValue; // date/time/timestamp/sequence
69 : } m_aValue;
70 :
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
76 :
77 : void free();
78 :
79 : public:
80 11468 : ORowSetValue()
81 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
82 : ,m_bNull(true)
83 : ,m_bBound(true)
84 : ,m_bModified(false)
85 11468 : ,m_bSigned(true)
86 : {
87 11468 : m_aValue.m_pString = NULL;
88 11468 : }
89 :
90 148279 : ORowSetValue(const ORowSetValue& _rRH)
91 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
92 : ,m_bNull(true)
93 : ,m_bBound(true)
94 : ,m_bModified(false)
95 148279 : ,m_bSigned(true)
96 : {
97 148279 : m_aValue.m_pString = NULL;
98 148279 : operator=(_rRH);
99 148279 : }
100 :
101 8431 : ORowSetValue(const OUString& _rRH)
102 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
103 : ,m_bNull(true)
104 : ,m_bBound(true)
105 : ,m_bModified(false)
106 8431 : ,m_bSigned(true)
107 : {
108 8431 : m_aValue.m_pString = NULL;
109 8431 : operator=(_rRH);
110 8431 : }
111 :
112 1 : ORowSetValue(const double& _rRH)
113 : :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE)
114 : ,m_bNull(true)
115 : ,m_bBound(true)
116 : ,m_bModified(false)
117 1 : ,m_bSigned(true)
118 : {
119 1 : m_aValue.m_pString = NULL;
120 1 : operator=(_rRH);
121 1 : }
122 :
123 1 : ORowSetValue(const float& _rRH)
124 : :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT)
125 : ,m_bNull(true)
126 : ,m_bBound(true)
127 : ,m_bModified(false)
128 1 : ,m_bSigned(true)
129 : {
130 1 : m_aValue.m_pString = NULL;
131 1 : operator=(_rRH);
132 1 : }
133 :
134 1 : ORowSetValue(const sal_Int8& _rRH)
135 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
136 : ,m_bNull(true)
137 : ,m_bBound(true)
138 : ,m_bModified(false)
139 1 : ,m_bSigned(true)
140 : {
141 1 : m_aValue.m_pString = NULL;
142 1 : operator=(_rRH);
143 1 : }
144 :
145 3 : ORowSetValue(const sal_uInt8& _rRH)
146 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
147 : ,m_bNull(true)
148 : ,m_bBound(true)
149 : ,m_bModified(false)
150 3 : ,m_bSigned(false)
151 : {
152 3 : m_aValue.m_pString = NULL;
153 3 : operator=(_rRH);
154 3 : }
155 77 : ORowSetValue(const sal_Int16& _rRH)
156 : :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
157 : ,m_bNull(true)
158 : ,m_bBound(true)
159 : ,m_bModified(false)
160 77 : ,m_bSigned(true)
161 : {
162 77 : m_aValue.m_pString = NULL;
163 77 : operator=(_rRH);
164 77 : }
165 1 : ORowSetValue(const sal_uInt16& _rRH)
166 : :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
167 : ,m_bNull(true)
168 : ,m_bBound(true)
169 : ,m_bModified(false)
170 1 : ,m_bSigned(false)
171 : {
172 1 : m_aValue.m_pString = NULL;
173 1 : operator=(_rRH);
174 1 : }
175 5049 : ORowSetValue(const sal_Int32& _rRH)
176 : :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
177 : ,m_bNull(true)
178 : ,m_bBound(true)
179 : ,m_bModified(false)
180 5049 : ,m_bSigned(true)
181 : {
182 5049 : m_aValue.m_pString = NULL;
183 5049 : operator=(_rRH);
184 5049 : }
185 1 : ORowSetValue(const sal_uInt32& _rRH)
186 : :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
187 : ,m_bNull(true)
188 : ,m_bBound(true)
189 : ,m_bModified(false)
190 1 : ,m_bSigned(false)
191 : {
192 1 : m_aValue.m_pString = NULL;
193 1 : operator=(_rRH);
194 1 : }
195 1 : ORowSetValue(const sal_Int64& _rRH)
196 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
197 : ,m_bNull(true)
198 : ,m_bBound(true)
199 : ,m_bModified(false)
200 1 : ,m_bSigned(true)
201 : {
202 1 : m_aValue.m_pString = NULL;
203 1 : operator=(_rRH);
204 1 : }
205 1 : ORowSetValue(const sal_uInt64& _rRH)
206 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
207 : ,m_bNull(true)
208 : ,m_bBound(true)
209 : ,m_bModified(false)
210 1 : ,m_bSigned(false)
211 : {
212 1 : m_aValue.m_pString = NULL;
213 1 : operator=(_rRH);
214 1 : }
215 :
216 7 : ORowSetValue(const bool& _rRH)
217 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT)
218 : ,m_bNull(true)
219 : ,m_bBound(true)
220 : ,m_bModified(false)
221 7 : ,m_bSigned(true)
222 : {
223 7 : m_aValue.m_pString = NULL;
224 7 : operator=(_rRH);
225 7 : }
226 :
227 54 : ORowSetValue(const ::com::sun::star::util::Date& _rRH)
228 : :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE)
229 : ,m_bNull(true)
230 : ,m_bBound(true)
231 : ,m_bModified(false)
232 54 : ,m_bSigned(true)
233 : {
234 54 : m_aValue.m_pString = NULL;
235 54 : operator=(_rRH);
236 54 : }
237 :
238 0 : ORowSetValue(const ::com::sun::star::util::Time& _rRH)
239 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME)
240 : ,m_bNull(true)
241 : ,m_bBound(true)
242 : ,m_bModified(false)
243 0 : ,m_bSigned(true)
244 : {
245 0 : m_aValue.m_pString = NULL;
246 0 : operator=(_rRH);
247 0 : }
248 :
249 0 : ORowSetValue(const ::com::sun::star::util::DateTime& _rRH)
250 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP)
251 : ,m_bNull(true)
252 : ,m_bBound(true)
253 : ,m_bModified(false)
254 0 : ,m_bSigned(true)
255 : {
256 0 : m_aValue.m_pString = NULL;
257 0 : operator=(_rRH);
258 0 : }
259 :
260 0 : ORowSetValue(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH)
261 : :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY)
262 : ,m_bNull(true)
263 : ,m_bBound(true)
264 : ,m_bModified(false)
265 0 : ,m_bSigned(true)
266 : {
267 0 : m_aValue.m_pString = NULL;
268 0 : operator=(_rRH);
269 0 : }
270 :
271 : // Avoid accidental uses of ORowSetValue(bool const &):
272 : template<typename T> ORowSetValue(T const *) = delete;
273 :
274 173369 : ~ORowSetValue()
275 : {
276 173369 : free();
277 173369 : }
278 :
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 )
282 : { return _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* )
286 : { }
287 :
288 : ORowSetValue& operator=(const ORowSetValue& _rRH);
289 :
290 : // simple types
291 : ORowSetValue& operator=(const bool _rRH);
292 :
293 : ORowSetValue& operator=(const sal_Int8& _rRH);
294 : ORowSetValue& operator=(const sal_uInt8& _rRH);
295 :
296 : ORowSetValue& operator=(const sal_Int16& _rRH);
297 : ORowSetValue& operator=(const sal_uInt16& _rRH);
298 :
299 : ORowSetValue& operator=(const sal_Int32& _rRH);
300 : ORowSetValue& operator=(const sal_uInt32& _rRH);
301 :
302 : ORowSetValue& operator=(const sal_Int64& _rRH);
303 : ORowSetValue& operator=(const sal_uInt64& _rRH);
304 :
305 : ORowSetValue& operator=(const double& _rRH);
306 : ORowSetValue& operator=(const float& _rRH);
307 :
308 : // ADT's
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);
312 :
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);
318 :
319 4 : operator bool() const { return !isNull() && getBool(); }
320 0 : operator sal_Int8() const { return isNull() ? static_cast<sal_Int8>(0) : getInt8(); }
321 68 : operator sal_uInt8() const { return isNull() ? static_cast<sal_uInt8>(0) : getUInt8(); }
322 :
323 0 : 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(); }
325 :
326 16542 : operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
327 0 : operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
328 :
329 0 : operator sal_Int64() const { return isNull() ? 0 : getLong(); }
330 0 : operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
331 :
332 0 : operator float() const { return isNull() ? (float)0.0: getFloat(); }
333 4 : operator double() const { return isNull() ? 0.0 : getDouble(); }
334 :
335 6324 : operator OUString() const
336 : {
337 6324 : return isNull() ? OUString() : getString();
338 : }
339 :
340 56 : operator ::com::sun::star::util::Date() const
341 : {
342 56 : return isNull() ? ::com::sun::star::util::Date() : getDate();
343 : }
344 :
345 0 : operator ::com::sun::star::util::Time() const
346 : {
347 0 : return isNull() ? ::com::sun::star::util::Time() : getTime();
348 : }
349 :
350 0 : operator ::com::sun::star::util::DateTime() const
351 : {
352 0 : return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
353 : }
354 :
355 0 : operator ::com::sun::star::uno::Sequence<sal_Int8>() const
356 : {
357 0 : return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
358 : }
359 :
360 : bool operator==(const ORowSetValue& _rRH) const;
361 6119 : bool operator!=(const ORowSetValue& _rRH) const
362 : {
363 6119 : return !( *this == _rRH );
364 : }
365 :
366 106644 : bool isNull() const
367 : {
368 106644 : return m_bNull;
369 : }
370 24453 : void setNull()
371 : {
372 24453 : free();
373 24453 : m_bNull = true;
374 24453 : m_aValue.m_pString = NULL;
375 24453 : }
376 :
377 75435 : bool isBound() const { return m_bBound; }
378 12706 : void setBound(bool _bBound) { m_bBound = _bBound; }
379 :
380 35 : bool isModified() const { return m_bModified; }
381 224 : void setModified(bool _bMod=true) { m_bModified = _bMod; }
382 :
383 27810 : bool isSigned() const { return m_bSigned; }
384 : void setSigned(bool _bSig=true);
385 :
386 37822 : sal_Int32 getTypeKind() const { return m_eTypeKind; }
387 : void setTypeKind(sal_Int32 _eType);
388 :
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;
392 :
393 : sal_Int8 getInt8() const;
394 : sal_uInt8 getUInt8() const;
395 :
396 : sal_Int16 getInt16() const;
397 : sal_uInt16 getUInt16() const;
398 :
399 : sal_Int32 getInt32() const;
400 : sal_uInt32 getUInt32() const;
401 :
402 : sal_Int64 getLong() const;
403 : sal_uInt64 getULong() const;
404 :
405 : double getDouble() const;
406 : float getFloat() const;
407 :
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;
413 : // only use for anys
414 8340 : ::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;
416 :
417 : /**
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
422 : */
423 : void fill(sal_Int32 _nPos,
424 : sal_Int32 _nType,
425 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
426 :
427 : /**
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
433 : */
434 : void fill(sal_Int32 _nPos,
435 : sal_Int32 _nType,
436 : bool _bNullable,
437 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
438 :
439 : void fill(const ::com::sun::star::uno::Any& _rValue);
440 :
441 : void fill( const sal_Int32 _nType,
442 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
443 :
444 : private:
445 : void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
446 : };
447 :
448 : /// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
449 10476 : class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::salhelper::SimpleReferenceObject
450 : {
451 : ORowSetValue m_aValue; // my own value
452 : public:
453 3574 : ORowSetValueDecorator(){m_aValue.setBound(true);}
454 1664 : ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(true);}
455 : ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
456 :
457 5515 : inline operator const ORowSetValue&() const { return m_aValue; }
458 0 : inline bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
459 5089 : inline const ORowSetValue& getValue() const { return m_aValue; }
460 17319 : inline ORowSetValue& get() { return m_aValue; }
461 122 : inline void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
462 21947 : inline void setNull() { m_aValue.setNull(); }
463 7226 : inline void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
464 65481 : inline bool isBound( ) const { return m_aValue.isBound();}
465 16737 : inline void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
466 80 : inline void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
467 :
468 : };
469 : typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
470 :
471 :
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>
474 : {
475 : bool m_bBound;
476 2 : TSetBound(bool _bBound) : m_bBound(_bBound){}
477 74 : void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
478 :
479 : };
480 :
481 :
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>
484 : {
485 : bool m_bBound;
486 203 : TSetRefBound(bool _bBound) : m_bBound(_bBound){}
487 3384 : void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
488 :
489 : };
490 :
491 :
492 : // Vector for file based rows
493 :
494 266 : template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
495 : {
496 : bool m_bDeleted;
497 : public:
498 58 : ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
499 206 : ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
500 :
501 2056 : bool isDeleted() const { return m_bDeleted; }
502 2343 : void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
503 : };
504 :
505 : typedef ODeleteVector< ORowSetValue > OValueVector;
506 :
507 524 : class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
508 : {
509 : public:
510 58 : OValueRefVector(){}
511 204 : OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
512 : {
513 3765 : for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
514 3561 : *aIter = new ORowSetValueDecorator;
515 204 : }
516 : };
517 :
518 : #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
519 0 : class OAssignValues : public OValueRefVector
520 : {
521 : ::std::vector<sal_Int32> m_nParameterIndexes;
522 : public:
523 : OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
524 0 : OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
525 :
526 0 : void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
527 0 : sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
528 : };
529 :
530 : typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
531 :
532 :
533 :
534 : typedef ::rtl::Reference< OValueVector > OValueRow;
535 : typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
536 : }
537 :
538 : #endif // INCLUDED_CONNECTIVITY_FVALUE_HXX
539 :
540 :
541 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|