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 0 : ORowSetValue()
81 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
82 : ,m_bNull(true)
83 : ,m_bBound(true)
84 : ,m_bModified(false)
85 0 : ,m_bSigned(true)
86 : {
87 0 : m_aValue.m_pString = NULL;
88 0 : }
89 :
90 0 : 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 0 : ,m_bSigned(true)
96 : {
97 0 : m_aValue.m_pString = NULL;
98 0 : operator=(_rRH);
99 0 : }
100 :
101 0 : 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 0 : ,m_bSigned(true)
107 : {
108 0 : m_aValue.m_pString = NULL;
109 0 : operator=(_rRH);
110 0 : }
111 :
112 0 : 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 0 : ,m_bSigned(true)
118 : {
119 0 : m_aValue.m_pString = NULL;
120 0 : operator=(_rRH);
121 0 : }
122 :
123 0 : 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 0 : ,m_bSigned(true)
129 : {
130 0 : m_aValue.m_pString = NULL;
131 0 : operator=(_rRH);
132 0 : }
133 :
134 0 : 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 0 : ,m_bSigned(true)
140 : {
141 0 : m_aValue.m_pString = NULL;
142 0 : operator=(_rRH);
143 0 : }
144 :
145 0 : 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 0 : ,m_bSigned(false)
151 : {
152 0 : m_aValue.m_pString = NULL;
153 0 : operator=(_rRH);
154 0 : }
155 0 : 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 0 : ,m_bSigned(true)
161 : {
162 0 : m_aValue.m_pString = NULL;
163 0 : operator=(_rRH);
164 0 : }
165 : 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 : ,m_bSigned(false)
171 : {
172 : m_aValue.m_pString = NULL;
173 : operator=(_rRH);
174 : }
175 0 : 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 0 : ,m_bSigned(true)
181 : {
182 0 : m_aValue.m_pString = NULL;
183 0 : operator=(_rRH);
184 0 : }
185 : 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 : ,m_bSigned(false)
191 : {
192 : m_aValue.m_pString = NULL;
193 : operator=(_rRH);
194 : }
195 0 : 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 0 : ,m_bSigned(true)
201 : {
202 0 : m_aValue.m_pString = NULL;
203 0 : operator=(_rRH);
204 0 : }
205 : 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 : ,m_bSigned(false)
211 : {
212 : m_aValue.m_pString = NULL;
213 : operator=(_rRH);
214 : }
215 :
216 0 : 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 0 : ,m_bSigned(true)
222 : {
223 0 : m_aValue.m_pString = NULL;
224 0 : operator=(_rRH);
225 0 : }
226 :
227 0 : 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 0 : ,m_bSigned(true)
233 : {
234 0 : m_aValue.m_pString = NULL;
235 0 : operator=(_rRH);
236 0 : }
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 0 : ~ORowSetValue()
272 : {
273 0 : free();
274 0 : }
275 :
276 : inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
277 : { return ::rtl_allocateMemory( nSize ); }
278 : inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW(())
279 : { return _pHint; }
280 : inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
281 : { ::rtl_freeMemory( pMem ); }
282 : inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW(())
283 : { }
284 :
285 : ORowSetValue& operator=(const ORowSetValue& _rRH);
286 :
287 : // simple types
288 : ORowSetValue& operator=(const bool _rRH);
289 :
290 : ORowSetValue& operator=(const sal_Int8& _rRH);
291 : ORowSetValue& operator=(const sal_uInt8& _rRH);
292 :
293 : ORowSetValue& operator=(const sal_Int16& _rRH);
294 : ORowSetValue& operator=(const sal_uInt16& _rRH);
295 :
296 : ORowSetValue& operator=(const sal_Int32& _rRH);
297 : ORowSetValue& operator=(const sal_uInt32& _rRH);
298 :
299 : ORowSetValue& operator=(const sal_Int64& _rRH);
300 : ORowSetValue& operator=(const sal_uInt64& _rRH);
301 :
302 : ORowSetValue& operator=(const double& _rRH);
303 : ORowSetValue& operator=(const float& _rRH);
304 :
305 : // ADT's
306 : ORowSetValue& operator=(const ::com::sun::star::util::Date& _rRH);
307 : ORowSetValue& operator=(const ::com::sun::star::util::Time& _rRH);
308 : ORowSetValue& operator=(const ::com::sun::star::util::DateTime& _rRH);
309 :
310 : ORowSetValue& operator=(const OUString& _rRH);
311 : // the type isn't set it will be set to VARCHAR if the type is different change it
312 : ORowSetValue& operator=(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH);
313 : // we the possiblity to save a any for bookmarks
314 : ORowSetValue& operator=(const ::com::sun::star::uno::Any& _rAny);
315 :
316 0 : operator bool() const { return isNull() ? false : getBool(); }
317 0 : operator sal_Int8() const { return isNull() ? static_cast<sal_Int8>(0) : getInt8(); }
318 0 : operator sal_uInt8() const { return isNull() ? static_cast<sal_uInt8>(0) : getUInt8(); }
319 :
320 0 : operator sal_Int16() const { return isNull() ? static_cast<sal_Int16>(0) : getInt16(); }
321 : operator sal_uInt16() const { return isNull() ? static_cast<sal_uInt16>(0) : getUInt16(); }
322 :
323 0 : operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
324 0 : operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
325 :
326 0 : operator sal_Int64() const { return isNull() ? 0 : getLong(); }
327 0 : operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
328 :
329 0 : operator float() const { return isNull() ? (float)0.0: getFloat(); }
330 0 : operator double() const { return isNull() ? 0.0 : getDouble(); }
331 :
332 0 : operator OUString() const
333 : {
334 0 : return isNull() ? OUString() : getString();
335 : }
336 :
337 0 : operator ::com::sun::star::util::Date() const
338 : {
339 0 : return isNull() ? ::com::sun::star::util::Date() : getDate();
340 : }
341 :
342 0 : operator ::com::sun::star::util::Time() const
343 : {
344 0 : return isNull() ? ::com::sun::star::util::Time() : getTime();
345 : }
346 :
347 0 : operator ::com::sun::star::util::DateTime() const
348 : {
349 0 : return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
350 : }
351 :
352 0 : operator ::com::sun::star::uno::Sequence<sal_Int8>() const
353 : {
354 0 : return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
355 : }
356 :
357 : bool operator==(const ORowSetValue& _rRH) const;
358 0 : bool operator!=(const ORowSetValue& _rRH) const
359 : {
360 0 : return !( *this == _rRH );
361 : }
362 :
363 0 : bool isNull() const
364 : {
365 0 : return m_bNull;
366 : }
367 0 : void setNull()
368 : {
369 0 : free();
370 0 : m_bNull = true;
371 0 : m_aValue.m_pString = NULL;
372 0 : }
373 :
374 0 : bool isBound() const { return m_bBound; }
375 0 : void setBound(bool _bBound) { m_bBound = _bBound ? 1 : 0; }
376 :
377 0 : bool isModified() const { return m_bModified; }
378 0 : void setModified(bool _bMod=true) { m_bModified = _bMod ? 1 : 0; }
379 :
380 0 : bool isSigned() const { return m_bSigned; }
381 : void setSigned(bool _bSig=true);
382 :
383 0 : sal_Int32 getTypeKind() const { return m_eTypeKind; }
384 : void setTypeKind(sal_Int32 _eType);
385 :
386 : // before calling one of this methods, be sure that the value is not null
387 : void* getValue() const { OSL_ENSURE(m_bBound,"Value is not bound!");return m_aValue.m_pValue; }
388 : bool getBool() const;
389 :
390 : sal_Int8 getInt8() const;
391 : sal_uInt8 getUInt8() const;
392 :
393 : sal_Int16 getInt16() const;
394 : sal_uInt16 getUInt16() const;
395 :
396 : sal_Int32 getInt32() const;
397 : sal_uInt32 getUInt32() const;
398 :
399 : sal_Int64 getLong() const;
400 : sal_uInt64 getULong() const;
401 :
402 : double getDouble() const;
403 : float getFloat() const;
404 :
405 : OUString getString() const; // makes a automatic conversion if type isn't a string
406 : ::com::sun::star::util::Date getDate() const;
407 : ::com::sun::star::util::Time getTime() const;
408 : ::com::sun::star::util::DateTime getDateTime() const;
409 : ::com::sun::star::uno::Sequence<sal_Int8> getSequence() const;
410 : // only use for anys
411 0 : ::com::sun::star::uno::Any getAny() const { return *(::com::sun::star::uno::Any*)m_aValue.m_pValue; }
412 : ::com::sun::star::uno::Any makeAny() const;
413 :
414 : /**
415 : fetches a single value out of the row
416 : @param _nPos the current column position
417 : @param _nType the type of the current column
418 : @param _xRow the row where to fetch the data from
419 : */
420 : void fill(sal_Int32 _nPos,
421 : sal_Int32 _nType,
422 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
423 :
424 : /**
425 : fetches a single value out of the row
426 : @param _nPos the current column position
427 : @param _nType the type of the current column
428 : @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
429 : @param _xRow the row where to fetch the data from
430 : */
431 : void fill(sal_Int32 _nPos,
432 : sal_Int32 _nType,
433 : bool _bNullable,
434 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
435 :
436 : void fill(const ::com::sun::star::uno::Any& _rValue);
437 :
438 : void fill( const sal_Int32 _nType,
439 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
440 :
441 : private:
442 : void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
443 : };
444 :
445 : /// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
446 0 : class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::salhelper::SimpleReferenceObject
447 : {
448 : ORowSetValue m_aValue; // my own value
449 : public:
450 0 : ORowSetValueDecorator(){m_aValue.setBound(true);}
451 0 : ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(true);}
452 : ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
453 :
454 0 : inline operator const ORowSetValue&() const { return m_aValue; }
455 0 : inline bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
456 0 : inline const ORowSetValue& getValue() const { return m_aValue; }
457 0 : inline ORowSetValue& get() { return m_aValue; }
458 0 : inline void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
459 0 : inline void setNull() { m_aValue.setNull(); }
460 0 : inline void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
461 0 : inline bool isBound( ) const { return m_aValue.isBound();}
462 0 : inline void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
463 0 : inline void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
464 :
465 : };
466 : typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
467 :
468 :
469 : /// TSetBound is a unary_function to set the bound value with e.q. for_each call
470 : struct OOO_DLLPUBLIC_DBTOOLS TSetBound : ::std::unary_function<ORowSetValue,void>
471 : {
472 : bool m_bBound;
473 0 : TSetBound(bool _bBound) : m_bBound(_bBound){}
474 0 : void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
475 :
476 : };
477 :
478 :
479 : /// TSetBound is a unary_function to set the bound value with e.q. for_each call
480 : struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound : ::std::unary_function<ORowSetValueDecoratorRef,void>
481 : {
482 : bool m_bBound;
483 0 : TSetRefBound(bool _bBound) : m_bBound(_bBound){}
484 0 : void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
485 :
486 : };
487 :
488 :
489 : // Vector for file based rows
490 :
491 0 : template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
492 : {
493 : bool m_bDeleted;
494 : public:
495 0 : ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
496 0 : ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
497 :
498 0 : bool isDeleted() const { return m_bDeleted; }
499 0 : void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
500 : };
501 :
502 : typedef ODeleteVector< ORowSetValue > OValueVector;
503 :
504 0 : class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
505 : {
506 : public:
507 0 : OValueRefVector(){}
508 0 : OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
509 : {
510 0 : for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
511 0 : *aIter = new ORowSetValueDecorator;
512 0 : }
513 : };
514 :
515 : #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
516 0 : class OAssignValues : public OValueRefVector
517 : {
518 : ::std::vector<sal_Int32> m_nParameterIndexes;
519 : public:
520 : OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
521 0 : OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
522 :
523 0 : void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
524 0 : sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
525 : };
526 :
527 : typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
528 :
529 :
530 :
531 : typedef ::rtl::Reference< OValueVector > OValueRow;
532 : typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
533 : }
534 :
535 : #endif // INCLUDED_CONNECTIVITY_FVALUE_HXX
536 :
537 :
538 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|