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 _CONNECTIVITY_FILE_VALUE_HXX_
21 : #define _CONNECTIVITY_FILE_VALUE_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 <comphelper/stl_types.hxx>
29 : #include <rtl/ref.hxx>
30 : #include "connectivity/dbtoolsdllapi.hxx"
31 : #include "connectivity/CommonTools.hxx"
32 : #include <com/sun/star/util/DateTime.hpp>
33 : #include <com/sun/star/util/Date.hpp>
34 : #include <com/sun/star/util/Time.hpp>
35 : #include <com/sun/star/uno/Sequence.hxx>
36 : #include <com/sun/star/sdbc/XRow.hpp>
37 : #include <com/sun/star/sdb/XColumn.hpp>
38 :
39 : namespace connectivity
40 : {
41 : namespace detail
42 : {
43 : class IValueSource;
44 : }
45 :
46 : class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
47 : {
48 : union
49 : {
50 : bool m_bBool;
51 :
52 : sal_Int8 m_nInt8;
53 : sal_uInt8 m_uInt8;
54 :
55 : sal_Int16 m_nInt16;
56 : sal_uInt16 m_uInt16;
57 :
58 : sal_Int32 m_nInt32;
59 : sal_uInt32 m_uInt32;
60 :
61 : sal_Int64 m_nInt64;
62 : sal_uInt64 m_uInt64;
63 :
64 : float m_nFloat;
65 : double m_nDouble;
66 :
67 : rtl_uString* m_pString;
68 :
69 : void* m_pValue; // date/time/timestamp/sequence
70 : } m_aValue;
71 :
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
77 :
78 : void free();
79 :
80 : public:
81 7416 : ORowSetValue()
82 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
83 : ,m_bNull(true)
84 : ,m_bBound(true)
85 : ,m_bModified(false)
86 7416 : ,m_bSigned(true)
87 : {
88 7416 : m_aValue.m_pString = NULL;
89 7416 : }
90 :
91 6504 : ORowSetValue(const ORowSetValue& _rRH)
92 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
93 : ,m_bNull(true)
94 : ,m_bBound(true)
95 : ,m_bModified(false)
96 6504 : ,m_bSigned(true)
97 : {
98 6504 : m_aValue.m_pString = NULL;
99 6504 : operator=(_rRH);
100 6504 : }
101 :
102 5579 : ORowSetValue(const OUString& _rRH)
103 : :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
104 : ,m_bNull(true)
105 : ,m_bBound(true)
106 : ,m_bModified(false)
107 5579 : ,m_bSigned(true)
108 : {
109 5579 : m_aValue.m_pString = NULL;
110 5579 : operator=(_rRH);
111 5579 : }
112 :
113 1 : ORowSetValue(const double& _rRH)
114 : :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE)
115 : ,m_bNull(true)
116 : ,m_bBound(true)
117 : ,m_bModified(false)
118 1 : ,m_bSigned(true)
119 : {
120 1 : m_aValue.m_pString = NULL;
121 1 : operator=(_rRH);
122 1 : }
123 :
124 1 : ORowSetValue(const float& _rRH)
125 : :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT)
126 : ,m_bNull(true)
127 : ,m_bBound(true)
128 : ,m_bModified(false)
129 1 : ,m_bSigned(true)
130 : {
131 1 : m_aValue.m_pString = NULL;
132 1 : operator=(_rRH);
133 1 : }
134 :
135 1 : ORowSetValue(const sal_Int8& _rRH)
136 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
137 : ,m_bNull(true)
138 : ,m_bBound(true)
139 : ,m_bModified(false)
140 1 : ,m_bSigned(true)
141 : {
142 1 : m_aValue.m_pString = NULL;
143 1 : operator=(_rRH);
144 1 : }
145 :
146 3 : ORowSetValue(const sal_uInt8& _rRH)
147 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
148 : ,m_bNull(true)
149 : ,m_bBound(true)
150 : ,m_bModified(false)
151 3 : ,m_bSigned(false)
152 : {
153 3 : m_aValue.m_pString = NULL;
154 3 : operator=(_rRH);
155 3 : }
156 77 : ORowSetValue(const sal_Int16& _rRH)
157 : :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
158 : ,m_bNull(true)
159 : ,m_bBound(true)
160 : ,m_bModified(false)
161 77 : ,m_bSigned(true)
162 : {
163 77 : m_aValue.m_pString = NULL;
164 77 : operator=(_rRH);
165 77 : }
166 1 : ORowSetValue(const sal_uInt16& _rRH)
167 : :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
168 : ,m_bNull(true)
169 : ,m_bBound(true)
170 : ,m_bModified(false)
171 1 : ,m_bSigned(false)
172 : {
173 1 : m_aValue.m_pString = NULL;
174 1 : operator=(_rRH);
175 1 : }
176 3757 : ORowSetValue(const sal_Int32& _rRH)
177 : :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
178 : ,m_bNull(true)
179 : ,m_bBound(true)
180 : ,m_bModified(false)
181 3757 : ,m_bSigned(true)
182 : {
183 3757 : m_aValue.m_pString = NULL;
184 3757 : operator=(_rRH);
185 3757 : }
186 1 : ORowSetValue(const sal_uInt32& _rRH)
187 : :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
188 : ,m_bNull(true)
189 : ,m_bBound(true)
190 : ,m_bModified(false)
191 1 : ,m_bSigned(false)
192 : {
193 1 : m_aValue.m_pString = NULL;
194 1 : operator=(_rRH);
195 1 : }
196 1 : ORowSetValue(const sal_Int64& _rRH)
197 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
198 : ,m_bNull(true)
199 : ,m_bBound(true)
200 : ,m_bModified(false)
201 1 : ,m_bSigned(true)
202 : {
203 1 : m_aValue.m_pString = NULL;
204 1 : operator=(_rRH);
205 1 : }
206 1 : ORowSetValue(const sal_uInt64& _rRH)
207 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
208 : ,m_bNull(true)
209 : ,m_bBound(true)
210 : ,m_bModified(false)
211 1 : ,m_bSigned(false)
212 : {
213 1 : m_aValue.m_pString = NULL;
214 1 : operator=(_rRH);
215 1 : }
216 :
217 1 : ORowSetValue(const bool& _rRH)
218 : :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT)
219 : ,m_bNull(true)
220 : ,m_bBound(true)
221 : ,m_bModified(false)
222 1 : ,m_bSigned(true)
223 : {
224 1 : m_aValue.m_pString = NULL;
225 1 : operator=(_rRH);
226 1 : }
227 :
228 54 : ORowSetValue(const ::com::sun::star::util::Date& _rRH)
229 : :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE)
230 : ,m_bNull(true)
231 : ,m_bBound(true)
232 : ,m_bModified(false)
233 54 : ,m_bSigned(true)
234 : {
235 54 : m_aValue.m_pString = NULL;
236 54 : operator=(_rRH);
237 54 : }
238 :
239 0 : ORowSetValue(const ::com::sun::star::util::Time& _rRH)
240 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TIME)
241 : ,m_bNull(true)
242 : ,m_bBound(true)
243 : ,m_bModified(false)
244 0 : ,m_bSigned(true)
245 : {
246 0 : m_aValue.m_pString = NULL;
247 0 : operator=(_rRH);
248 0 : }
249 :
250 0 : ORowSetValue(const ::com::sun::star::util::DateTime& _rRH)
251 : :m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP)
252 : ,m_bNull(true)
253 : ,m_bBound(true)
254 : ,m_bModified(false)
255 0 : ,m_bSigned(true)
256 : {
257 0 : m_aValue.m_pString = NULL;
258 0 : operator=(_rRH);
259 0 : }
260 :
261 0 : ORowSetValue(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH)
262 : :m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY)
263 : ,m_bNull(true)
264 : ,m_bBound(true)
265 : ,m_bModified(false)
266 0 : ,m_bSigned(true)
267 : {
268 0 : m_aValue.m_pString = NULL;
269 0 : operator=(_rRH);
270 0 : }
271 :
272 23313 : ~ORowSetValue()
273 : {
274 23313 : free();
275 23313 : }
276 :
277 : inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
278 : { return ::rtl_allocateMemory( nSize ); }
279 : inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW(())
280 : { return _pHint; }
281 : inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
282 : { ::rtl_freeMemory( pMem ); }
283 : inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW(())
284 : { }
285 :
286 : ORowSetValue& operator=(const ORowSetValue& _rRH);
287 :
288 : // simple types
289 : ORowSetValue& operator=(const bool _rRH);
290 :
291 : ORowSetValue& operator=(const sal_Int8& _rRH);
292 : ORowSetValue& operator=(const sal_uInt8& _rRH);
293 :
294 : ORowSetValue& operator=(const sal_Int16& _rRH);
295 : ORowSetValue& operator=(const sal_uInt16& _rRH);
296 :
297 : ORowSetValue& operator=(const sal_Int32& _rRH);
298 : ORowSetValue& operator=(const sal_uInt32& _rRH);
299 :
300 : ORowSetValue& operator=(const sal_Int64& _rRH);
301 : ORowSetValue& operator=(const sal_uInt64& _rRH);
302 :
303 : ORowSetValue& operator=(const double& _rRH);
304 : ORowSetValue& operator=(const float& _rRH);
305 :
306 : // ADT's
307 : ORowSetValue& operator=(const ::com::sun::star::util::Date& _rRH);
308 : ORowSetValue& operator=(const ::com::sun::star::util::Time& _rRH);
309 : ORowSetValue& operator=(const ::com::sun::star::util::DateTime& _rRH);
310 :
311 : ORowSetValue& operator=(const OUString& _rRH);
312 : // the type isn't set it will be set to VARCHAR if the type is different change it
313 : ORowSetValue& operator=(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH);
314 : // we the possiblity to save a any for bookmarks
315 : ORowSetValue& operator=(const ::com::sun::star::uno::Any& _rAny);
316 :
317 0 : operator bool() const { return isNull() ? false : getBool(); }
318 0 : operator sal_Int8() const { return isNull() ? static_cast<sal_Int8>(0) : getInt8(); }
319 68 : operator sal_uInt8() const { return isNull() ? static_cast<sal_uInt8>(0) : getUInt8(); }
320 :
321 0 : operator sal_Int16() const { return isNull() ? static_cast<sal_Int16>(0) : getInt16(); }
322 : operator sal_uInt16() const { return isNull() ? static_cast<sal_uInt16>(0) : getUInt16(); }
323 :
324 1345 : operator sal_Int32() const { return isNull() ? 0 : getInt32(); }
325 0 : operator sal_uInt32() const { return isNull() ? 0 : getUInt32(); }
326 :
327 0 : operator sal_Int64() const { return isNull() ? 0 : getLong(); }
328 0 : operator sal_uInt64() const { return isNull() ? 0 : getULong(); }
329 :
330 0 : operator float() const { return isNull() ? (float)0.0: getFloat(); }
331 0 : operator double() const { return isNull() ? 0.0 : getDouble(); }
332 :
333 4570 : operator OUString() const
334 : {
335 4570 : return isNull() ? OUString() : getString();
336 : }
337 :
338 56 : operator ::com::sun::star::util::Date() const
339 : {
340 56 : return isNull() ? ::com::sun::star::util::Date() : getDate();
341 : }
342 :
343 0 : operator ::com::sun::star::util::Time() const
344 : {
345 0 : return isNull() ? ::com::sun::star::util::Time() : getTime();
346 : }
347 :
348 0 : operator ::com::sun::star::util::DateTime() const
349 : {
350 0 : return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
351 : }
352 :
353 0 : operator ::com::sun::star::uno::Sequence<sal_Int8>() const
354 : {
355 0 : return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
356 : }
357 :
358 : bool operator==(const ORowSetValue& _rRH) const;
359 1400 : bool operator!=(const ORowSetValue& _rRH) const
360 : {
361 1400 : return !( *this == _rRH );
362 : }
363 :
364 14364 : bool isNull() const
365 : {
366 14364 : return m_bNull;
367 : }
368 16431 : void setNull()
369 : {
370 16431 : free();
371 16431 : m_bNull = true;
372 16431 : m_aValue.m_pString = NULL;
373 16431 : }
374 :
375 48735 : bool isBound() const { return m_bBound; }
376 10505 : void setBound(bool _bBound) { m_bBound = _bBound ? 1 : 0; }
377 :
378 25 : bool isModified() const { return m_bModified; }
379 188 : void setModified(bool _bMod=true) { m_bModified = _bMod ? 1 : 0; }
380 :
381 114 : bool isSigned() const { return m_bSigned; }
382 : void setSigned(bool _bSig=true);
383 :
384 5103 : sal_Int32 getTypeKind() const { return m_eTypeKind; }
385 : void setTypeKind(sal_Int32 _eType);
386 :
387 : // before calling one of this methods, be sure that the value is not null
388 : void* getValue() const { OSL_ENSURE(m_bBound,"Value is not bound!");return m_aValue.m_pValue; }
389 : bool getBool() const;
390 :
391 : sal_Int8 getInt8() const;
392 : sal_uInt8 getUInt8() const;
393 :
394 : sal_Int16 getInt16() const;
395 : sal_uInt16 getUInt16() const;
396 :
397 : sal_Int32 getInt32() const;
398 : sal_uInt32 getUInt32() const;
399 :
400 : sal_Int64 getLong() const;
401 : sal_uInt64 getULong() const;
402 :
403 : double getDouble() const;
404 : float getFloat() const;
405 :
406 : OUString getString() const; // makes a automatic conversion if type isn't a string
407 : ::com::sun::star::util::Date getDate() const;
408 : ::com::sun::star::util::Time getTime() const;
409 : ::com::sun::star::util::DateTime getDateTime() const;
410 : ::com::sun::star::uno::Sequence<sal_Int8> getSequence() const;
411 : // only use for anys
412 286 : ::com::sun::star::uno::Any getAny() const { return *(::com::sun::star::uno::Any*)m_aValue.m_pValue; }
413 : ::com::sun::star::uno::Any makeAny() const;
414 :
415 : /**
416 : fetches a single value out of the row
417 : @param _nPos the current column position
418 : @param _nType the type of the current column
419 : @param _xRow the row where to fetch the data from
420 : */
421 : void fill(sal_Int32 _nPos,
422 : sal_Int32 _nType,
423 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
424 :
425 : /**
426 : fetches a single value out of the row
427 : @param _nPos the current column position
428 : @param _nType the type of the current column
429 : @param _bNullable if true then it will be checked if the result could be NULL, otherwise not.
430 : @param _xRow the row where to fetch the data from
431 : */
432 : void fill(sal_Int32 _nPos,
433 : sal_Int32 _nType,
434 : bool _bNullable,
435 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
436 :
437 : void fill(const ::com::sun::star::uno::Any& _rValue);
438 :
439 : void fill( const sal_Int32 _nType,
440 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
441 :
442 : private:
443 : void impl_fill( const sal_Int32 _nType, bool _bNullable, const detail::IValueSource& _rValueSource );
444 : };
445 :
446 : /// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
447 8764 : class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::salhelper::SimpleReferenceObject
448 : {
449 : ORowSetValue m_aValue; // my own value
450 : public:
451 2811 : ORowSetValueDecorator(){m_aValue.setBound(true);}
452 1571 : ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(true);}
453 : ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
454 :
455 4421 : inline operator const ORowSetValue&() const { return m_aValue; }
456 0 : inline bool operator ==( const ORowSetValue & _rRH ) { return m_aValue == _rRH; }
457 4016 : inline const ORowSetValue& getValue() const { return m_aValue; }
458 9517 : inline ORowSetValue& get() { return m_aValue; }
459 122 : inline void setValue(const ORowSetValue& _aValue) { m_aValue = _aValue; }
460 14297 : inline void setNull() { m_aValue.setNull(); }
461 5909 : inline void setBound(bool _bBound ) { m_aValue.setBound(_bBound);}
462 47086 : inline bool isBound( ) const { return m_aValue.isBound();}
463 10126 : inline void setTypeKind(sal_Int32 _nType) { m_aValue.setTypeKind(_nType); }
464 80 : inline void setModified(bool _bModified) { m_aValue.setModified(_bModified); }
465 :
466 : };
467 : typedef ::rtl::Reference<ORowSetValueDecorator> ORowSetValueDecoratorRef;
468 :
469 : // -------------------------------------------------------------------------
470 : /// TSetBound is a unary_function to set the bound value with e.q. for_each call
471 : struct OOO_DLLPUBLIC_DBTOOLS TSetBound : ::std::unary_function<ORowSetValue,void>
472 : {
473 : bool m_bBound;
474 2 : TSetBound(bool _bBound) : m_bBound(_bBound){}
475 74 : void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
476 :
477 : };
478 :
479 : // -------------------------------------------------------------------------
480 : /// TSetBound is a unary_function to set the bound value with e.q. for_each call
481 : struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound : ::std::unary_function<ORowSetValueDecoratorRef,void>
482 : {
483 : bool m_bBound;
484 159 : TSetRefBound(bool _bBound) : m_bBound(_bBound){}
485 2659 : void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
486 :
487 : };
488 :
489 : // ----------------------------------------------------------------------------
490 : // Vector for file based rows
491 : // ----------------------------------------------------------------------------
492 206 : template< class VectorVal > class ODeleteVector : public connectivity::ORowVector< VectorVal >
493 : {
494 : bool m_bDeleted;
495 : public:
496 46 : ODeleteVector() : connectivity::ORowVector< VectorVal >() ,m_bDeleted(false) {}
497 162 : ODeleteVector(size_t _st) : connectivity::ORowVector< VectorVal >(_st) ,m_bDeleted(false) {}
498 :
499 1518 : bool isDeleted() const { return m_bDeleted; }
500 1704 : void setDeleted(bool _bDeleted) { m_bDeleted = _bDeleted; }
501 : };
502 :
503 : typedef ODeleteVector< ORowSetValue > OValueVector;
504 :
505 412 : class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
506 : {
507 : public:
508 46 : OValueRefVector(){}
509 160 : OValueRefVector(size_t _st) : ODeleteVector< ORowSetValueDecoratorRef >(_st)
510 : {
511 2964 : for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
512 2804 : *aIter = new ORowSetValueDecorator;
513 160 : }
514 : };
515 :
516 : #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
517 0 : class OAssignValues : public OValueRefVector
518 : {
519 : ::std::vector<sal_Int32> m_nParameterIndexes;
520 : public:
521 : OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
522 0 : OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
523 :
524 0 : void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
525 0 : sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
526 : };
527 :
528 : typedef ::rtl::Reference< OAssignValues > ORefAssignValues;
529 :
530 :
531 :
532 : typedef ::rtl::Reference< OValueVector > OValueRow;
533 : typedef ::rtl::Reference< OValueRefVector > OValueRefRow;
534 : }
535 :
536 : #endif // #ifndef _CONNECTIVITY_FILE_VALUE_HXX_
537 :
538 :
539 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|