1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _CONNECTIVITY_FILE_VALUE_HXX_
25 #define _CONNECTIVITY_FILE_VALUE_HXX_
26 
27 #include <com/sun/star/sdbc/DataType.hpp>
28 #include <com/sun/star/uno/Any.hxx>
29 #include <rtl/ustring.hxx>
30 #include <osl/diagnose.h>
31 #include <comphelper/stl_types.hxx>
32 #include <vos/ref.hxx>
33 #include "connectivity/dbtoolsdllapi.hxx"
34 #include "connectivity/CommonTools.hxx"
35 #include <com/sun/star/util/DateTime.hpp>
36 #include <com/sun/star/util/Date.hpp>
37 #include <com/sun/star/util/Time.hpp>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdb/XColumn.hpp>
41 
42 namespace connectivity
43 {
44     namespace detail
45     {
46         class IValueSource;
47     }
48 
49 	class OOO_DLLPUBLIC_DBTOOLS ORowSetValue
50 	{
51 		union
52 		{
53 			sal_Bool		m_bBool;
54 			sal_Int8		m_nInt8;
55 			sal_Int16		m_nInt16;
56 			sal_Int32		m_nInt32;
57 			rtl_uString*	m_pString;
58 
59 			void*			m_pValue;			// can contains double, etc
60 		} m_aValue;
61 
62 		sal_Int32			m_eTypeKind;		// the database type
63 		sal_Bool			m_bNull		: 1;	// value is null
64 		sal_Bool			m_bBound	: 1;	// is bound
65 		sal_Bool			m_bModified : 1;	// value was changed
66 		sal_Bool			m_bSigned	: 1;	// value is signed
67 
68 		void free();
69 
70 	public:
ORowSetValue()71 		ORowSetValue()
72 			:m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
73             ,m_bNull(sal_True)
74 			,m_bBound(sal_True)
75             ,m_bModified(sal_False)
76 			,m_bSigned(sal_True)
77 		{
78 			m_aValue.m_pString = NULL;
79 		}
80 
ORowSetValue(const ORowSetValue & _rRH)81 		ORowSetValue(const ORowSetValue& _rRH)
82             :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
83             ,m_bNull(sal_True)
84             ,m_bBound(sal_True)
85             ,m_bModified(sal_False)
86             ,m_bSigned(sal_True)
87 		{
88 			m_aValue.m_pString = NULL;
89 			operator=(_rRH);
90 		}
91 
ORowSetValue(const::rtl::OUString & _rRH)92 		ORowSetValue(const ::rtl::OUString& _rRH)
93             :m_eTypeKind(::com::sun::star::sdbc::DataType::VARCHAR)
94             ,m_bNull(sal_True)
95             ,m_bBound(sal_True)
96             ,m_bModified(sal_False)
97             ,m_bSigned(sal_True)
98 		{
99 			m_aValue.m_pString = NULL;
100 			operator=(_rRH);
101 		}
102 
ORowSetValue(const double & _rRH)103 		ORowSetValue(const double& _rRH)
104             :m_eTypeKind(::com::sun::star::sdbc::DataType::DOUBLE)
105             ,m_bNull(sal_True)
106             ,m_bBound(sal_True)
107             ,m_bModified(sal_False)
108             ,m_bSigned(sal_True)
109 		{
110 			m_aValue.m_pString = NULL;
111 			operator=(_rRH);
112 		}
113 
ORowSetValue(const float & _rRH)114 		ORowSetValue(const float& _rRH)
115             :m_eTypeKind(::com::sun::star::sdbc::DataType::FLOAT)
116             ,m_bNull(sal_True)
117             ,m_bBound(sal_True)
118             ,m_bModified(sal_False)
119             ,m_bSigned(sal_True)
120 		{
121 			m_aValue.m_pString = NULL;
122 			operator=(_rRH);
123 		}
124 
ORowSetValue(const sal_Int8 & _rRH)125 		ORowSetValue(const sal_Int8& _rRH)
126             :m_eTypeKind(::com::sun::star::sdbc::DataType::TINYINT)
127             ,m_bNull(sal_True)
128             ,m_bBound(sal_True)
129             ,m_bModified(sal_False)
130             ,m_bSigned(sal_True)
131 		{
132 			m_aValue.m_pString = NULL;
133 			operator=(_rRH);
134 		}
ORowSetValue(const sal_Int16 & _rRH)135 		ORowSetValue(const sal_Int16& _rRH)
136             :m_eTypeKind(::com::sun::star::sdbc::DataType::SMALLINT)
137             ,m_bNull(sal_True)
138             ,m_bBound(sal_True)
139             ,m_bModified(sal_False)
140             ,m_bSigned(sal_True)
141 		{
142 			m_aValue.m_pString = NULL;
143 			operator=(_rRH);
144 		}
ORowSetValue(const sal_Int32 & _rRH)145 		ORowSetValue(const sal_Int32& _rRH)
146             :m_eTypeKind(::com::sun::star::sdbc::DataType::INTEGER)
147             ,m_bNull(sal_True)
148             ,m_bBound(sal_True)
149             ,m_bModified(sal_False)
150             ,m_bSigned(sal_True)
151 		{
152 			m_aValue.m_pString = NULL;
153 			operator=(_rRH);
154 		}
ORowSetValue(const sal_Int64 & _rRH)155 		ORowSetValue(const sal_Int64& _rRH)
156             :m_eTypeKind(::com::sun::star::sdbc::DataType::BIGINT)
157             ,m_bNull(sal_True)
158             ,m_bBound(sal_True)
159             ,m_bModified(sal_False)
160             ,m_bSigned(sal_True)
161 		{
162 			m_aValue.m_pString = NULL;
163 			operator=(_rRH);
164 		}
165 
ORowSetValue(const sal_Bool & _rRH)166 		ORowSetValue(const sal_Bool& _rRH)
167             :m_eTypeKind(::com::sun::star::sdbc::DataType::BIT)
168             ,m_bNull(sal_True)
169             ,m_bBound(sal_True)
170             ,m_bModified(sal_False)
171             ,m_bSigned(sal_True)
172 		{
173 			m_aValue.m_pString = NULL;
174 			operator=(_rRH);
175 		}
176 
ORowSetValue(const::com::sun::star::util::Date & _rRH)177 		ORowSetValue(const ::com::sun::star::util::Date& _rRH)
178             :m_eTypeKind(::com::sun::star::sdbc::DataType::DATE)
179             ,m_bNull(sal_True)
180             ,m_bBound(sal_True)
181             ,m_bModified(sal_False)
182             ,m_bSigned(sal_True)
183 		{
184 			m_aValue.m_pString = NULL;
185 			operator=(_rRH);
186 		}
187 
ORowSetValue(const::com::sun::star::util::Time & _rRH)188 		ORowSetValue(const ::com::sun::star::util::Time& _rRH)
189 			:m_eTypeKind(::com::sun::star::sdbc::DataType::TIME)
190             ,m_bNull(sal_True)
191             ,m_bBound(sal_True)
192             ,m_bModified(sal_False)
193             ,m_bSigned(sal_True)
194 		{
195 			m_aValue.m_pString = NULL;
196 			operator=(_rRH);
197 		}
198 
ORowSetValue(const::com::sun::star::util::DateTime & _rRH)199 		ORowSetValue(const ::com::sun::star::util::DateTime& _rRH)
200 			:m_eTypeKind(::com::sun::star::sdbc::DataType::TIMESTAMP)
201             ,m_bNull(sal_True)
202             ,m_bBound(sal_True)
203             ,m_bModified(sal_False)
204             ,m_bSigned(sal_True)
205 		{
206 			m_aValue.m_pString = NULL;
207 			operator=(_rRH);
208 		}
209 
ORowSetValue(const::com::sun::star::uno::Sequence<sal_Int8> & _rRH)210 		ORowSetValue(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH)
211 			:m_eTypeKind(::com::sun::star::sdbc::DataType::LONGVARBINARY)
212             ,m_bNull(sal_True)
213             ,m_bBound(sal_True)
214             ,m_bModified(sal_False)
215             ,m_bSigned(sal_True)
216 		{
217 			m_aValue.m_pString = NULL;
218 			operator=(_rRH);
219 		}
220 
~ORowSetValue()221 		~ORowSetValue()
222 		{
223 			free();
224 		}
225 
operator new(size_t nSize)226 		inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
227 			{ return ::rtl_allocateMemory( nSize ); }
operator new(size_t,void * _pHint)228 		inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW( () )
229 			{ return _pHint; }
operator delete(void * pMem)230 		inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
231 			{ ::rtl_freeMemory( pMem ); }
operator delete(void *,void *)232 		inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW( () )
233 			{  }
234 
235 		ORowSetValue& operator=(const ORowSetValue& _rRH);
236 
237 		// simple types
238 		ORowSetValue& operator=(const sal_Bool _rRH);
239 		ORowSetValue& operator=(const sal_Int8& _rRH);
240 		ORowSetValue& operator=(const sal_Int16& _rRH);
241 		ORowSetValue& operator=(const sal_Int32& _rRH);
242 		ORowSetValue& operator=(const sal_Int64& _rRH);
243 		ORowSetValue& operator=(const double& _rRH);
244 		ORowSetValue& operator=(const float& _rRH);
245 
246 		// ADT's
247 		ORowSetValue& operator=(const ::com::sun::star::util::Date& _rRH);
248 		ORowSetValue& operator=(const ::com::sun::star::util::Time& _rRH);
249 		ORowSetValue& operator=(const ::com::sun::star::util::DateTime& _rRH);
250 
251 		ORowSetValue& operator=(const ::rtl::OUString& _rRH);
252 		// the type isn't set it will be set to VARCHAR if the type is different change it
253 		ORowSetValue& operator=(const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH);
254 		// we the possiblity to save a any for bookmarks
255 		ORowSetValue& operator=(const ::com::sun::star::uno::Any& _rAny);
256 
operator sal_Bool() const257 		operator sal_Bool() const	{	return isNull() ? sal_False : getBool();	}
operator sal_Int8() const258 		operator sal_Int8() const	{	return isNull() ? static_cast<sal_Int8>(0) : getInt8();	}
operator sal_Int16() const259 		operator sal_Int16() const	{	return isNull() ? static_cast<sal_Int16>(0) : getInt16();	}
operator sal_Int32() const260 		operator sal_Int32() const	{	return isNull() ? 0			: getInt32();	}
operator sal_Int64() const261 		operator sal_Int64() const	{	return isNull() ? 0			: getLong();	}
262 		operator float() const		{	return isNull() ? (float)0.0: getFloat();	}
263 		operator double() const		{	return isNull() ? 0.0		: getDouble();	}
264 
operator ::rtl::OUString() const265 		operator ::rtl::OUString() const
266 		{
267 			return isNull() ? ::rtl::OUString() : getString();
268 		}
269 
operator ::com::sun::star::util::Date() const270 		operator ::com::sun::star::util::Date() const
271 		{
272 			return isNull() ? ::com::sun::star::util::Date() : getDate();
273 		}
274 
operator ::com::sun::star::util::Time() const275 		operator ::com::sun::star::util::Time() const
276 		{
277 			return isNull() ? ::com::sun::star::util::Time() : getTime();
278 		}
279 
operator ::com::sun::star::util::DateTime() const280 		operator ::com::sun::star::util::DateTime() const
281 		{
282 			return isNull() ? ::com::sun::star::util::DateTime() : getDateTime();
283 		}
284 
operator ::com::sun::star::uno::Sequence<sal_Int8>() const285 		operator ::com::sun::star::uno::Sequence<sal_Int8>() const
286 		{
287 			return isNull() ? ::com::sun::star::uno::Sequence<sal_Int8>() : getSequence();
288 		}
289 
290 		bool operator==(const ORowSetValue& _rRH) const;
operator !=(const ORowSetValue & _rRH) const291 		bool operator!=(const ORowSetValue& _rRH) const
292         {
293             return !( *this == _rRH );
294         }
295 
isNull() const296 		sal_Bool	isNull() const
297 		{
298 			return m_bNull;
299 		}
setNull()300 		void		setNull()
301 		{
302 			free();
303 			m_bNull = sal_True;
304 			m_aValue.m_pString = NULL;
305 		}
306 
isBound() const307 		sal_Bool	isBound() const						{ return m_bBound;		}
setBound(sal_Bool _bBound)308 		void		setBound(sal_Bool _bBound)			{ m_bBound = _bBound ? true : false; }
309 
isModified() const310 		sal_Bool	isModified() const					{ return m_bModified;	}
setModified(sal_Bool _bMod=sal_True)311 		void		setModified(sal_Bool _bMod=sal_True){ m_bModified = _bMod ? true : false;	}
312 
isSigned() const313 		sal_Bool	isSigned() const					{ return m_bSigned;	}
314 		void		setSigned(sal_Bool _bMod=sal_True);
315 
getTypeKind() const316 		sal_Int32	getTypeKind() const					{ return m_eTypeKind;	}
317 		void		setTypeKind(sal_Int32 _eType);
318 
319 		// before calling one of this methods, be sure that the value is not null
getValue() const320 		void*			getValue()	const				{ OSL_ENSURE(m_bBound,"Value is not bound!");return m_aValue.m_pValue;				}
321 		sal_Bool		getBool()	const;
322 		sal_Int8		getInt8()	const;
323 		sal_Int16		getInt16()	const;
324 		sal_Int32		getInt32()	const;
325 		sal_Int64		getLong()	const;
326 		double			getDouble() const;
327 		float			getFloat() const;
328 		// convert the double to the type _nDataType
329 		void			setFromDouble(const double& _rVal,sal_Int32 _nDatatype);
330 
331 		::rtl::OUString getString() const;		// makes a automatic conversion if type isn't a string
332 		::com::sun::star::util::Date				getDate()		const;
333 		::com::sun::star::util::Time				getTime()		const;
334 		::com::sun::star::util::DateTime			getDateTime()	const;
335 		::com::sun::star::uno::Sequence<sal_Int8>	getSequence()	const;
336 		// only use for anys
getAny() const337 		::com::sun::star::uno::Any					getAny()		const { return *(::com::sun::star::uno::Any*)m_aValue.m_pValue; }
338 		::com::sun::star::uno::Any					makeAny()		const;
339 
340         /**
341 			fetches a single value out of the row
342 			@param _nPos	the current column position
343 			@param _nType	the type of the current column
344 			@param _xRow	the row where to fetch the data from
345 		*/
346         void fill(sal_Int32 _nPos,
347 				  sal_Int32 _nType,
348 				  const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
349 
350         /**
351 			fetches a single value out of the row
352 			@param _nPos	the current column position
353 			@param _nType	the type of the current column
354             @param _bNullable   if true then it will be checked if the result could be NULL, otherwise not.
355 			@param _xRow	the row where to fetch the data from
356 		*/
357         void fill(sal_Int32 _nPos,
358 				  sal_Int32 _nType,
359                   sal_Bool  _bNullable,
360 				  const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>& _xRow);
361 
362         void fill(const ::com::sun::star::uno::Any& _rValue);
363 
364         void fill( const sal_Int32 _nType,
365                    const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn >& _rxColumn );
366 
367     private:
368         void impl_fill( const sal_Int32 _nType, sal_Bool _bNullable, const detail::IValueSource& _rValueSource );
369 	};
370 
371 	/// ORowSetValueDecorator decorates a ORowSetValue so the value is "refcounted"
372 	class OOO_DLLPUBLIC_DBTOOLS ORowSetValueDecorator : public ::vos::OReference
373 	{
374 		ORowSetValue	m_aValue;	// my own value
375 	public:
ORowSetValueDecorator()376 		ORowSetValueDecorator(){m_aValue.setBound(sal_True);}
ORowSetValueDecorator(const ORowSetValue & _aValue)377 		ORowSetValueDecorator(const ORowSetValue& _aValue) : m_aValue(_aValue){m_aValue.setBound(sal_True);}
378 		ORowSetValueDecorator& operator=(const ORowSetValue& _aValue);
379 
380 		inline operator const ORowSetValue&()	const				{ return m_aValue; }
operator ==(const ORowSetValue & _rRH)381 		inline bool operator ==( const ORowSetValue & _rRH )	    { return m_aValue == _rRH; }
getValue() const382 		inline const ORowSetValue& getValue()	const				{ return m_aValue; }
get()383 		inline ORowSetValue& get()									{ return m_aValue; }
setValue(const ORowSetValue & _aValue)384 		inline void setValue(const ORowSetValue& _aValue)			{ m_aValue = _aValue; }
setNull()385 		inline void setNull()										{ m_aValue.setNull(); }
setBound(sal_Bool _bBound)386 		inline void setBound(sal_Bool _bBound )						{ m_aValue.setBound(_bBound);}
isBound() const387 		inline sal_Bool isBound( ) const							{ return m_aValue.isBound();}
setTypeKind(sal_Int32 _nType)388 		inline void setTypeKind(sal_Int32 _nType)					{ m_aValue.setTypeKind(_nType); }
setModified(sal_Bool _bModified)389 		inline void setModified(sal_Bool _bModified)				{ m_aValue.setModified(_bModified); }
390 
391 	};
392 	typedef ::vos::ORef<ORowSetValueDecorator> ORowSetValueDecoratorRef;
393 
394 	// -------------------------------------------------------------------------
395 	/// TSetBound is a unary_function to set the bound value with e.q. for_each call
396 	struct OOO_DLLPUBLIC_DBTOOLS TSetBound : ::std::unary_function<ORowSetValue,void>
397 	{
398 		sal_Bool m_bBound;
TSetBoundconnectivity::TSetBound399 		TSetBound(sal_Bool _bBound) : m_bBound(_bBound){}
operator ()connectivity::TSetBound400 		void operator()(ORowSetValue& _rValue) const { _rValue.setBound(m_bBound); }
401 
402 	};
403 
404 	// -------------------------------------------------------------------------
405 	/// TSetBound is a unary_function to set the bound value with e.q. for_each call
406 	struct OOO_DLLPUBLIC_DBTOOLS TSetRefBound : ::std::unary_function<ORowSetValueDecoratorRef,void>
407 	{
408 		sal_Bool m_bBound;
TSetRefBoundconnectivity::TSetRefBound409 		TSetRefBound(sal_Bool _bBound) : m_bBound(_bBound){}
operator ()connectivity::TSetRefBound410 		void operator()(ORowSetValueDecoratorRef& _rValue) const { _rValue->setBound(m_bBound); }
411 
412 	};
413 
414 	// ----------------------------------------------------------------------------
415 	// Vector for file based rows
416 	// ----------------------------------------------------------------------------
417 	template< class VectorVal > class  ODeleteVector : public connectivity::ORowVector< VectorVal >
418 	{
419 		sal_Bool	m_bDeleted;
420 	public:
ODeleteVector()421 		ODeleteVector()				: connectivity::ORowVector< VectorVal >()		,m_bDeleted(sal_False)	{}
ODeleteVector(size_t _st)422 		ODeleteVector(size_t _st)	: connectivity::ORowVector< VectorVal >(_st)	,m_bDeleted(sal_False)	{}
423 
isDeleted() const424 		sal_Bool	isDeleted() const				{ return m_bDeleted;		}
setDeleted(sal_Bool _bDeleted)425 		void		setDeleted(sal_Bool _bDeleted)	{ m_bDeleted = _bDeleted;	}
426 	};
427 
428 	typedef	ODeleteVector< ORowSetValue >				OValueVector;
429 
430 	class OOO_DLLPUBLIC_DBTOOLS OValueRefVector : public ODeleteVector< ORowSetValueDecoratorRef >
431 	{
432 	public:
OValueRefVector()433 		OValueRefVector(){}
OValueRefVector(size_t _st)434 		OValueRefVector(size_t _st)	: ODeleteVector< ORowSetValueDecoratorRef >(_st)
435 		{
436 			for(OValueRefVector::Vector::iterator aIter = get().begin() ; aIter != get().end() ;++aIter)
437 				*aIter = new ORowSetValueDecorator;
438 		}
439 	};
440 
441 #define SQL_NO_PARAMETER (SAL_MAX_UINT32)
442 	class OAssignValues : public OValueRefVector
443 	{
444 		::std::vector<sal_Int32> m_nParameterIndexes;
445 	public:
OAssignValues()446 		OAssignValues() : m_nParameterIndexes(1,SQL_NO_PARAMETER){}
OAssignValues(Vector::size_type n)447 		OAssignValues(Vector::size_type n) : OValueRefVector(n),m_nParameterIndexes(n+1,SQL_NO_PARAMETER){}
448 
setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex)449 		void setParameterIndex(sal_Int32 _nId,sal_Int32 _nParameterIndex) { m_nParameterIndexes[_nId] = _nParameterIndex;}
getParameterIndex(sal_Int32 _nId) const450 		sal_Int32 getParameterIndex(sal_Int32 _nId) const { return m_nParameterIndexes[_nId]; }
451 	};
452 
453 	typedef ::vos::ORef< OAssignValues > ORefAssignValues;
454 
455 
456 
457 	typedef ::vos::ORef< OValueVector >					OValueRow;
458 	typedef ::vos::ORef< OValueRefVector >				OValueRefRow;
459 }
460 
461 #endif // #ifndef _CONNECTIVITY_FILE_VALUE_HXX_
462 
463 
464