1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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()
48 OLEString::OLEString(const BSTR
& _sBStr
)
52 OLEString::OLEString(const ::rtl::OUString
& _sBStr
)
54 m_sStr
= ::SysAllocString(reinterpret_cast<LPCOLESTR
>(_sBStr
.getStr()));
56 OLEString::~OLEString()
59 ::SysFreeString(m_sStr
);
61 OLEString
& OLEString::operator=(const ::rtl::OUString
& _rSrc
)
64 ::SysFreeString(m_sStr
);
65 m_sStr
= ::SysAllocString(reinterpret_cast<LPCOLESTR
>(_rSrc
.getStr()));
68 OLEString
& OLEString::operator=(const OLEString
& _rSrc
)
73 ::SysFreeString(m_sStr
);
74 m_sStr
= ::SysAllocString(_rSrc
.m_sStr
);
78 OLEString
& OLEString::operator=(const BSTR
& _rSrc
)
81 ::SysFreeString(m_sStr
);
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
93 BSTR
* OLEString::operator &()
97 sal_Int32
OLEString::length() const
99 return (m_sStr
!= NULL
) ? ::SysStringLen(m_sStr
) : 0;
102 OLEVariant::OLEVariant()
106 OLEVariant::OLEVariant(const VARIANT
& varSrc
)
109 HRESULT eRet
= ::VariantCopy(this, const_cast<VARIANT
*>(&varSrc
));
110 OSL_ENSURE(eRet
== S_OK
,"Error while copying an ado variant!");
113 OLEVariant::OLEVariant(const OLEVariant
& varSrc
)
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!");
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
)
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!");
138 } // clears all the memory that was allocated before
140 OLEVariant::OLEVariant(const ::com::sun::star::util::Date
& x
)
144 dblVal
= ::dbtools::DBTypeConversion::toDouble(x
,::com::sun::star::util::Date(30,12,1899));
146 OLEVariant::OLEVariant(const ::com::sun::star::util::Time
& x
)
150 dblVal
= ::dbtools::DBTypeConversion::toDouble(x
);
152 OLEVariant::OLEVariant(const ::com::sun::star::util::DateTime
& x
)
156 dblVal
= ::dbtools::DBTypeConversion::toDouble(x
,::com::sun::star::util::Date(30,12,1899));
158 OLEVariant::OLEVariant(const float &x
)
164 OLEVariant::OLEVariant(const double &x
)
172 OLEVariant::OLEVariant(IDispatch
* pDispInterface
)
175 setIDispatch( pDispInterface
);
178 OLEVariant::OLEVariant(const ::com::sun::star::uno::Sequence
< sal_Int8
>& x
)
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");
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!");
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!");
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!");
230 void OLEVariant::setByte(sal_uInt8 n
)
232 HRESULT eRet
= VariantClear(this);
233 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
238 void OLEVariant::setInt16(sal_Int16 n
)
240 HRESULT eRet
= VariantClear(this);
241 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
246 void OLEVariant::setInt32(sal_Int32 n
)
248 HRESULT eRet
= VariantClear(this);
249 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
254 void OLEVariant::setFloat(float f
)
255 { HRESULT eRet
= VariantClear(this);
256 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
261 void OLEVariant::setDouble(double d
)
263 HRESULT eRet
= VariantClear(this);
264 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
269 void OLEVariant::setDate(DATE d
)
270 { HRESULT eRet
= VariantClear(this);
271 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
276 void OLEVariant::setChar(unsigned char a
)
278 HRESULT eRet
= VariantClear(this);
279 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
284 void OLEVariant::setCurrency(double aCur
)
286 HRESULT eRet
= VariantClear(this);
287 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
292 void OLEVariant::setBool(sal_Bool b
)
294 HRESULT eRet
= VariantClear(this);
295 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
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!");
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!");
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!");
324 void OLEVariant::setEmpty()
326 HRESULT eRet
= VariantClear(this);
327 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
332 void OLEVariant::setUI1SAFEARRAYPtr(SAFEARRAY
* pSafeAr
)
334 HRESULT eRet
= VariantClear(this);
335 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
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!");
345 vt
= (VARTYPE
)(VT_ARRAY
|vtType
);
349 void OLEVariant::setIDispatch(IDispatch
* pDispInterface
)
351 HRESULT eRet
= VariantClear(this);
352 OSL_ENSURE(eRet
== S_OK
,"Error while clearing an ado variant!");
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;
395 void OLEVariant::set(double n
)
399 cyVal
.Hi
= (sal_Int32
)(n
/ (double)4294967296.0);
400 cyVal
.Lo
= (sal_uInt32
)(n
- ((double)cyVal
.Hi
* (double)4294967296.0));
403 cyVal
.Hi
= (sal_Int32
)(-n
/ (double)4294967296.0);
404 cyVal
.Lo
= (sal_uInt32
)(-n
- ((double)cyVal
.Hi
* (double)4294967296.0));
409 OLEVariant::operator rtl::OUString() const
411 if (V_VT(this) == VT_BSTR
)
412 return reinterpret_cast<const sal_Unicode
*>(LPCOLESTR(V_BSTR(this)));
415 return ::rtl::OUString();
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
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
) ),
442 ::connectivity::SharedResources aResources
;
443 const ::rtl::OUString
sError( aResources
.getResourceString(STR_TYPE_NOT_CONVERT
));
444 throw ::com::sun::star::sdbc::SQLException(
447 ::rtl::OUString::createFromAscii( "S1000" ),
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());
466 SAFEARRAY
* pArray
= getUI1SAFEARRAYPtr();
470 HRESULT hresult1
,hresult2
;
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
)
483 hresult1
= ::SafeArrayGetElement(pArray
,&lBound
,&nData
);
484 if ( SUCCEEDED(hresult1
) )
486 *pData
= static_cast<sal_Int8
>(nData
);
496 // -----------------------------------------------------------------------------
497 ::rtl::OUString
OLEVariant::getString() const
500 return ::rtl::OUString();
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
;
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);
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);
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
)
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
)
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
)
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
)
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
)
618 return sal_uInt32(0);
622 varDest
.ChangeType(VT_UI4
, this);
624 return V_UI4(&varDest
);
626 // -----------------------------------------------------------------------------
627 float OLEVariant::getFloat() const
629 if (V_VT(this) == VT_R4
)
636 varDest
.ChangeType(VT_R4
, this);
638 return V_R4(&varDest
);
640 // -----------------------------------------------------------------------------
641 double OLEVariant::getDouble() const
643 if (V_VT(this) == VT_R8
)
650 varDest
.ChangeType(VT_R8
, this);
652 return V_R8(&varDest
);
654 // -----------------------------------------------------------------------------
655 double OLEVariant::getDate() const
657 if (V_VT(this) == VT_DATE
)
664 varDest
.ChangeType(VT_DATE
, this);
666 return V_DATE(&varDest
);
668 // -----------------------------------------------------------------------------
669 CY
OLEVariant::getCurrency() const
671 if (V_VT(this) == VT_CY
)
677 aVar
.int64
= sal_Int64(0);
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);
696 varDest
.ChangeType((VT_ARRAY
|VT_UI1
), this);
698 return V_ARRAY(&varDest
);
700 // -----------------------------------------------------------------------------
701 // -----------------------------------------------------------------------------
702 // -----------------------------------------------------------------------------
703 // -----------------------------------------------------------------------------