Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / ado / Aolevariant.cxx
blob650c77a244c6c134c143e78781f15c56d6d71e6b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Aolevariant.cxx,v $
10 * $Revision: 1.15.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "ado/Aolevariant.hxx"
34 #include "connectivity/dbconversion.hxx"
35 #include <com/sun/star/sdbc/SQLException.hpp>
36 #include <com/sun/star/util/Time.hpp>
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/util/DateTime.hpp>
39 #include "diagnose_ex.h"
40 #include "resource/sharedresources.hxx"
41 #include "resource/ado_res.hrc"
43 using namespace connectivity::ado;
44 OLEString::OLEString()
45 :m_sStr(NULL)
48 OLEString::OLEString(const BSTR& _sBStr)
49 :m_sStr(_sBStr)
52 OLEString::OLEString(const ::rtl::OUString& _sBStr)
54 m_sStr = ::SysAllocString(reinterpret_cast<LPCOLESTR>(_sBStr.getStr()));
56 OLEString::~OLEString()
58 if(m_sStr)
59 ::SysFreeString(m_sStr);
61 OLEString& OLEString::operator=(const ::rtl::OUString& _rSrc)
63 if(m_sStr)
64 ::SysFreeString(m_sStr);
65 m_sStr = ::SysAllocString(reinterpret_cast<LPCOLESTR>(_rSrc.getStr()));
66 return *this;
68 OLEString& OLEString::operator=(const OLEString& _rSrc)
70 if(this != &_rSrc)
72 if(m_sStr)
73 ::SysFreeString(m_sStr);
74 m_sStr = ::SysAllocString(_rSrc.m_sStr);
76 return *this;
78 OLEString& OLEString::operator=(const BSTR& _rSrc)
80 if(m_sStr)
81 ::SysFreeString(m_sStr);
82 m_sStr = _rSrc;
83 return *this;
85 OLEString::operator ::rtl::OUString() const
87 return (m_sStr != NULL) ? ::rtl::OUString(reinterpret_cast<const sal_Unicode*>(LPCOLESTR(m_sStr)),::SysStringLen(m_sStr)) : ::rtl::OUString();
89 OLEString::operator BSTR() const
91 return m_sStr;
93 BSTR* OLEString::operator &()
95 return &m_sStr;
97 sal_Int32 OLEString::length() const
99 return (m_sStr != NULL) ? ::SysStringLen(m_sStr) : 0;
102 OLEVariant::OLEVariant()
104 VariantInit(this);
106 OLEVariant::OLEVariant(const VARIANT& varSrc)
108 ::VariantInit(this);
109 HRESULT eRet = ::VariantCopy(this, const_cast<VARIANT*>(&varSrc));
110 OSL_ENSURE(eRet == S_OK,"Error while copying an ado variant!");
111 OSL_UNUSED(eRet);
113 OLEVariant::OLEVariant(const OLEVariant& varSrc)
115 ::VariantInit(this);
116 HRESULT eRet = ::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)));
117 OSL_ENSURE(eRet == S_OK,"Error while copying an ado variant!");
118 OSL_UNUSED(eRet);
121 OLEVariant::OLEVariant(sal_Bool x) { VariantInit(this); vt = VT_BOOL; boolVal = (x ? VARIANT_TRUE : VARIANT_FALSE);}
122 OLEVariant::OLEVariant(sal_Int8 n) { VariantInit(this); vt = VT_I1; bVal = n;}
123 OLEVariant::OLEVariant(sal_Int16 n) { VariantInit(this); vt = VT_I2; intVal = n;}
124 OLEVariant::OLEVariant(sal_Int32 n) { VariantInit(this); vt = VT_I4; lVal = n;}
125 OLEVariant::OLEVariant(sal_Int64 x) { VariantInit(this); vt = VT_I4; lVal = (LONG)x;}
127 OLEVariant::OLEVariant(const rtl::OUString& us)
129 ::VariantInit(this);
130 vt = VT_BSTR;
131 bstrVal = SysAllocString(reinterpret_cast<LPCOLESTR>(us.getStr()));
133 OLEVariant::~OLEVariant()
135 HRESULT eRet = ::VariantClear(this);
136 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
137 OSL_UNUSED(eRet);
138 } // clears all the memory that was allocated before
140 OLEVariant::OLEVariant(const ::com::sun::star::util::Date& x )
142 VariantInit(this);
143 vt = VT_DATE;
144 dblVal = ::dbtools::DBTypeConversion::toDouble(x,::com::sun::star::util::Date(30,12,1899));
146 OLEVariant::OLEVariant(const ::com::sun::star::util::Time& x )
148 VariantInit(this);
149 vt = VT_DATE;
150 dblVal = ::dbtools::DBTypeConversion::toDouble(x);
152 OLEVariant::OLEVariant(const ::com::sun::star::util::DateTime& x )
154 VariantInit(this);
155 vt = VT_DATE;
156 dblVal = ::dbtools::DBTypeConversion::toDouble(x,::com::sun::star::util::Date(30,12,1899));
158 OLEVariant::OLEVariant(const float &x)
160 VariantInit(this);
161 vt = VT_R4;
162 fltVal = x;
164 OLEVariant::OLEVariant(const double &x)
166 VariantInit(this);
167 vt = VT_R8;
168 dblVal = x;
172 OLEVariant::OLEVariant(IDispatch* pDispInterface)
174 VariantInit(this);
175 setIDispatch( pDispInterface );
178 OLEVariant::OLEVariant(const ::com::sun::star::uno::Sequence< sal_Int8 >& x)
180 VariantInit(this);
182 vt = VT_ARRAY|VT_UI1;
184 SAFEARRAYBOUND rgsabound[1];
185 rgsabound[0].lLbound = 0;
186 rgsabound[0].cElements = x.getLength();
187 // parray = SafeArrayCreate(VT_UI1,1,rgsabound);
188 parray = SafeArrayCreateVector(VT_UI1, 0, x.getLength());
189 const sal_Int8* pBegin = x.getConstArray();
190 const sal_Int8* pEnd = pBegin + x.getLength();
192 for(sal_Int32 i=0;pBegin != pEnd;++i,++pBegin)
194 sal_Int32 nData = *pBegin;
195 HRESULT rs = SafeArrayPutElement(parray,&i,&nData);
196 OSL_ENSURE(S_OK == rs,"Error while copy byte data");
197 OSL_UNUSED(rs);
201 OLEVariant& OLEVariant::operator=(const OLEVariant& varSrc)
203 HRESULT eRet = ::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)));
204 OSL_ENSURE(eRet == S_OK,"Error while copying an ado variant!");
205 OSL_UNUSED(eRet);
206 return *this;
208 // Assign a const VARIANT& (::VariantCopy handles everything)
210 OLEVariant& OLEVariant::operator=(const tagVARIANT& varSrc)
212 HRESULT eRet = ::VariantCopy(this, const_cast<VARIANT*>(&varSrc));
213 OSL_ENSURE(eRet == S_OK,"Error while copying an ado variant!");
214 OSL_UNUSED(eRet);
216 return *this;
219 // Assign a const VARIANT* (::VariantCopy handles everything)
221 OLEVariant& OLEVariant::operator=(const VARIANT* pSrc)
223 HRESULT eRet = ::VariantCopy(this, const_cast<VARIANT*>(pSrc));
224 OSL_ENSURE(eRet == S_OK,"Error while copying an ado variant!");
225 OSL_UNUSED(eRet);
227 return *this;
230 void OLEVariant::setByte(sal_uInt8 n)
232 HRESULT eRet = VariantClear(this);
233 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
234 OSL_UNUSED(eRet);
235 vt = VT_UI1;
236 bVal = n;
238 void OLEVariant::setInt16(sal_Int16 n)
240 HRESULT eRet = VariantClear(this);
241 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
242 OSL_UNUSED(eRet);
243 vt = VT_I2;
244 iVal = n;
246 void OLEVariant::setInt32(sal_Int32 n)
248 HRESULT eRet = VariantClear(this);
249 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
250 OSL_UNUSED(eRet);
251 vt = VT_I4;
252 lVal = n;
254 void OLEVariant::setFloat(float f)
255 { HRESULT eRet = VariantClear(this);
256 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
257 OSL_UNUSED(eRet);
258 vt = VT_R4;
259 fltVal = f;
261 void OLEVariant::setDouble(double d)
263 HRESULT eRet = VariantClear(this);
264 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
265 OSL_UNUSED(eRet);
266 vt = VT_R8;
267 dblVal = d;
269 void OLEVariant::setDate(DATE d)
270 { HRESULT eRet = VariantClear(this);
271 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
272 OSL_UNUSED(eRet);
273 vt = VT_DATE;
274 date = d;
276 void OLEVariant::setChar(unsigned char a)
278 HRESULT eRet = VariantClear(this);
279 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
280 OSL_UNUSED(eRet);
281 vt = VT_UI1;
282 bVal = a;
284 void OLEVariant::setCurrency(double aCur)
286 HRESULT eRet = VariantClear(this);
287 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
288 OSL_UNUSED(eRet);
289 vt = VT_CY;
290 set(aCur*10000);
292 void OLEVariant::setBool(sal_Bool b)
294 HRESULT eRet = VariantClear(this);
295 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
296 OSL_UNUSED(eRet);
297 vt = VT_BOOL;
298 boolVal = b ? VARIANT_TRUE : VARIANT_FALSE;
300 void OLEVariant::setString(const rtl::OUString& us)
302 HRESULT eRet = VariantClear(this);
303 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
304 OSL_UNUSED(eRet);
305 vt = VT_BSTR;
306 bstrVal = ::SysAllocString(reinterpret_cast<LPCOLESTR>(us.getStr()));
308 void OLEVariant::setNoArg()
310 HRESULT eRet = VariantClear(this);
311 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
312 OSL_UNUSED(eRet);
313 vt = VT_ERROR;
314 scode = DISP_E_PARAMNOTFOUND;
317 void OLEVariant::setNull()
319 HRESULT eRet = VariantClear(this);
320 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
321 OSL_UNUSED(eRet);
322 vt = VT_NULL;
324 void OLEVariant::setEmpty()
326 HRESULT eRet = VariantClear(this);
327 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
328 OSL_UNUSED(eRet);
329 vt = VT_EMPTY;
332 void OLEVariant::setUI1SAFEARRAYPtr(SAFEARRAY* pSafeAr)
334 HRESULT eRet = VariantClear(this);
335 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
336 OSL_UNUSED(eRet);
337 vt = VT_ARRAY|VT_UI1; parray = pSafeAr;
340 void OLEVariant::setArray(SAFEARRAY* pSafeArray, VARTYPE vtType)
342 HRESULT eRet = VariantClear(this);
343 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
344 OSL_UNUSED(eRet);
345 vt = (VARTYPE)(VT_ARRAY|vtType);
346 parray = pSafeArray;
349 void OLEVariant::setIDispatch(IDispatch* pDispInterface)
351 HRESULT eRet = VariantClear(this);
352 OSL_ENSURE(eRet == S_OK,"Error while clearing an ado variant!");
353 OSL_UNUSED(eRet);
355 vt = VT_DISPATCH;
356 pdispVal = pDispInterface;
358 if ( pDispInterface )
359 pDispInterface->AddRef();
363 sal_Bool OLEVariant::isNull() const { return (vt == VT_NULL); }
364 sal_Bool OLEVariant::isEmpty() const { return (vt == VT_EMPTY); }
366 VARTYPE OLEVariant::getType() const { return vt; }
368 OLEVariant::operator ::com::sun::star::util::Date() const
370 return isNull() ? ::com::sun::star::util::Date(30,12,1899) : ::dbtools::DBTypeConversion::toDate(getDate(),::com::sun::star::util::Date(30,12,1899));
372 OLEVariant::operator ::com::sun::star::util::Time() const
374 return isNull() ? ::com::sun::star::util::Time() : ::dbtools::DBTypeConversion::toTime(getDate());
376 OLEVariant::operator ::com::sun::star::util::DateTime()const
378 return isNull() ? ::com::sun::star::util::DateTime() : ::dbtools::DBTypeConversion::toDateTime(getDate(),::com::sun::star::util::Date(30,12,1899));
381 VARIANT_BOOL OLEVariant::VariantBool(sal_Bool bEinBoolean)
383 return (bEinBoolean ? VARIANT_TRUE : VARIANT_FALSE);
386 void OLEVariant::CHS()
388 cyVal.Lo ^= (sal_uInt32)-1;
389 cyVal.Hi ^= -1;
390 cyVal.Lo++;
391 if( !cyVal.Lo )
392 cyVal.Hi++;
395 void OLEVariant::set(double n)
397 if( n >= 0 )
399 cyVal.Hi = (sal_Int32)(n / (double)4294967296.0);
400 cyVal.Lo = (sal_uInt32)(n - ((double)cyVal.Hi * (double)4294967296.0));
402 else {
403 cyVal.Hi = (sal_Int32)(-n / (double)4294967296.0);
404 cyVal.Lo = (sal_uInt32)(-n - ((double)cyVal.Hi * (double)4294967296.0));
405 CHS();
409 OLEVariant::operator rtl::OUString() const
411 if (V_VT(this) == VT_BSTR)
412 return reinterpret_cast<const sal_Unicode*>(LPCOLESTR(V_BSTR(this)));
414 if(isNull())
415 return ::rtl::OUString();
417 OLEVariant varDest;
419 varDest.ChangeType(VT_BSTR, this);
421 return reinterpret_cast<const sal_Unicode*>(LPCOLESTR(V_BSTR(&varDest)));
424 // -----------------------------------------------------------------------------
425 void OLEVariant::ChangeType(VARTYPE vartype, const OLEVariant* pSrc)
428 // If pDest is NULL, convert type in place
430 if (pSrc == NULL)
431 pSrc = this;
433 if ( ( this != pSrc )
434 || ( vartype != V_VT( this ) )
437 if ( FAILED( ::VariantChangeType( static_cast< VARIANT* >( this ),
438 const_cast< VARIANT* >( static_cast< const VARIANT* >( pSrc ) ),
440 vartype ) ) )
442 ::connectivity::SharedResources aResources;
443 const ::rtl::OUString sError( aResources.getResourceString(STR_TYPE_NOT_CONVERT));
444 throw ::com::sun::star::sdbc::SQLException(
445 sError,
446 NULL,
447 ::rtl::OUString::createFromAscii( "S1000" ),
448 1000,
449 ::com::sun::star::uno::Any()
455 // -----------------------------------------------------------------------------
456 OLEVariant::operator ::com::sun::star::uno::Sequence< sal_Int8 >() const
458 ::com::sun::star::uno::Sequence< sal_Int8 > aRet;
459 if(V_VT(this) == VT_BSTR)
461 OLEString sStr(V_BSTR(this));
462 aRet = ::com::sun::star::uno::Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>((const wchar_t*)sStr),sizeof(sal_Unicode)*sStr.length());
464 else if(!isNull())
466 SAFEARRAY* pArray = getUI1SAFEARRAYPtr();
468 if(pArray)
470 HRESULT hresult1,hresult2;
471 long lBound,uBound;
472 // Verify that the SafeArray is the proper shape.
473 hresult1 = ::SafeArrayGetLBound(pArray, 1, &lBound);
474 hresult2 = ::SafeArrayGetUBound(pArray, 1, &uBound);
475 if ( SUCCEEDED(hresult1) && SUCCEEDED(hresult2) )
477 long nCount = uBound-lBound+1;
478 aRet.realloc(nCount);
479 sal_Int8* pData = aRet.getArray();
480 for(long i=0; SUCCEEDED(hresult1) && lBound <= uBound ;++i,++lBound)
482 sal_Int32 nData = 0;
483 hresult1 = ::SafeArrayGetElement(pArray,&lBound,&nData);
484 if ( SUCCEEDED(hresult1) )
486 *pData = static_cast<sal_Int8>(nData);
487 ++pData;
494 return aRet;
496 // -----------------------------------------------------------------------------
497 ::rtl::OUString OLEVariant::getString() const
499 if(isNull())
500 return ::rtl::OUString();
501 else
502 return *this;
504 // -----------------------------------------------------------------------------
505 sal_Bool OLEVariant::getBool() const
507 if (V_VT(this) == VT_BOOL)
508 return V_BOOL(this) == VARIANT_TRUE ? sal_True : sal_False;
509 if(isNull())
510 return sal_False;
512 OLEVariant varDest;
514 varDest.ChangeType(VT_BOOL, this);
516 return V_BOOL(&varDest) == VARIANT_TRUE ? sal_True : sal_False;
518 // -----------------------------------------------------------------------------
519 IUnknown* OLEVariant::getIUnknown() const
521 if (V_VT(this) == VT_UNKNOWN)
523 return V_UNKNOWN(this);
525 if(isNull())
526 return NULL;
528 OLEVariant varDest;
530 varDest.ChangeType(VT_UNKNOWN, this);
532 V_UNKNOWN(&varDest)->AddRef();
533 return V_UNKNOWN(&varDest);
535 // -----------------------------------------------------------------------------
536 IDispatch* OLEVariant::getIDispatch() const
538 if (V_VT(this) == VT_DISPATCH)
540 return V_DISPATCH(this);
543 if(isNull())
544 return NULL;
546 OLEVariant varDest;
548 varDest.ChangeType(VT_DISPATCH, this);
550 V_DISPATCH(&varDest)->AddRef();
551 return V_DISPATCH(&varDest);
553 // -----------------------------------------------------------------------------
554 sal_uInt8 OLEVariant::getByte() const
556 if (V_VT(this) == VT_UI1)
557 return V_UI1(this);
559 if(isNull())
560 return sal_Int8(0);
561 OLEVariant varDest;
563 varDest.ChangeType(VT_UI1, this);
565 return V_UI1(&varDest);
567 // -----------------------------------------------------------------------------
568 sal_Int16 OLEVariant::getInt16() const
570 if (V_VT(this) == VT_I2)
571 return V_I2(this);
573 if(isNull())
574 return sal_Int16(0);
575 OLEVariant varDest;
577 varDest.ChangeType(VT_I2, this);
579 return V_I2(&varDest);
581 // -----------------------------------------------------------------------------
582 sal_Int8 OLEVariant::getInt8() const
584 if (V_VT(this) == VT_I1)
585 return V_I1(this);
587 if(isNull())
588 return sal_Int8(0);
590 OLEVariant varDest;
592 varDest.ChangeType(VT_I1, this);
594 return V_I1(&varDest);
596 // -----------------------------------------------------------------------------
597 sal_Int32 OLEVariant::getInt32() const
599 if (V_VT(this) == VT_I4)
600 return V_I4(this);
602 if(isNull())
603 return sal_Int32(0);
605 OLEVariant varDest;
607 varDest.ChangeType(VT_I4, this);
609 return V_I4(&varDest);
611 // -----------------------------------------------------------------------------
612 sal_uInt32 OLEVariant::getUInt32() const
614 if (V_VT(this) == VT_UI4)
615 return V_UI4(this);
617 if(isNull())
618 return sal_uInt32(0);
620 OLEVariant varDest;
622 varDest.ChangeType(VT_UI4, this);
624 return V_UI4(&varDest);
626 // -----------------------------------------------------------------------------
627 float OLEVariant::getFloat() const
629 if (V_VT(this) == VT_R4)
630 return V_R4(this);
632 if(isNull())
633 return float(0);
634 OLEVariant varDest;
636 varDest.ChangeType(VT_R4, this);
638 return V_R4(&varDest);
640 // -----------------------------------------------------------------------------
641 double OLEVariant::getDouble() const
643 if (V_VT(this) == VT_R8)
644 return V_R8(this);
646 if(isNull())
647 return double(0);
648 OLEVariant varDest;
650 varDest.ChangeType(VT_R8, this);
652 return V_R8(&varDest);
654 // -----------------------------------------------------------------------------
655 double OLEVariant::getDate() const
657 if (V_VT(this) == VT_DATE)
658 return V_DATE(this);
660 if(isNull())
661 return double(0);
662 OLEVariant varDest;
664 varDest.ChangeType(VT_DATE, this);
666 return V_DATE(&varDest);
668 // -----------------------------------------------------------------------------
669 CY OLEVariant::getCurrency() const
671 if (V_VT(this) == VT_CY)
672 return V_CY(this);
674 if(isNull())
676 CY aVar;
677 aVar.int64 = sal_Int64(0);
678 return aVar;
680 OLEVariant varDest;
682 varDest.ChangeType(VT_CY, this);
684 return V_CY(&varDest);
686 // -----------------------------------------------------------------------------
687 SAFEARRAY* OLEVariant::getUI1SAFEARRAYPtr() const
689 if (V_VT(this) == (VT_ARRAY|VT_UI1))
690 return V_ARRAY(this);
692 if(isNull())
693 return (0);
694 OLEVariant varDest;
696 varDest.ChangeType((VT_ARRAY|VT_UI1), this);
698 return V_ARRAY(&varDest);
700 // -----------------------------------------------------------------------------
701 // -----------------------------------------------------------------------------
702 // -----------------------------------------------------------------------------
703 // -----------------------------------------------------------------------------