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: CommonTools.cxx,v $
10 * $Revision: 1.26.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"
35 #include "connectivity/CommonTools.hxx"
36 #include "connectivity/dbtools.hxx"
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/util/Time.hpp>
39 #include <com/sun/star/util/DateTime.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <comphelper/extract.hxx>
43 #include <cppuhelper/interfacecontainer.h>
44 #include "TConnection.hxx"
45 #include <comphelper/types.hxx>
46 #include <com/sun/star/java/XJavaVM.hpp>
47 #include <rtl/process.h>
49 using namespace ::comphelper
;
50 inline sal_Unicode
rtl_ascii_toUpperCase( sal_Unicode ch
)
52 return ch
>= 0x0061 && ch
<= 0x007a ? ch
+ 0x20 : ch
;
55 namespace connectivity
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star::lang
;
59 using namespace ::com::sun::star::beans
;
60 using namespace dbtools
;
61 namespace starjava
= com::sun::star::java
;
62 //------------------------------------------------------------------------------
63 const sal_Unicode CHAR_PLACE
= '_';
64 const sal_Unicode CHAR_WILD
= '%';
65 // -------------------------------------------------------------------------
66 sal_Bool
match(const sal_Unicode
* pWild
, const sal_Unicode
* pStr
, const sal_Unicode cEscape
)
71 while ( *pWild
|| flag
)
80 if (*pWild
&& (*pWild
== cEscape
) && ((*(pWild
+1)== CHAR_PLACE
) || (*(pWild
+1) == CHAR_WILD
)) )
82 if ( rtl_ascii_toUpperCase(*pWild
) != rtl_ascii_toUpperCase(*pStr
) )
88 break; // ACHTUNG laeuft unter bestimmten
89 // Umstaenden in den nachsten case rein!!
91 while ( *pWild
== CHAR_WILD
)
98 return ( *pWild
== 0 );
99 while ( *pStr
&& *pStr
!= *pWild
)
101 if ( *pWild
== CHAR_PLACE
) {
103 while ( *pWild
== CHAR_WILD
)
108 return ( *pWild
== 0 );
121 return ( *pStr
== 0 ) && ( *pWild
== 0 );
123 //------------------------------------------------------------------
124 rtl::OUString
toDateString(const ::com::sun::star::util::Date
& rDate
)
134 return rtl::OUString::createFromAscii(s
);
137 //------------------------------------------------------------------
138 rtl::OUString
toTimeString(const ::com::sun::star::util::Time
& rTime
)
148 return rtl::OUString::createFromAscii(s
);
151 //------------------------------------------------------------------
152 rtl::OUString
toDateTimeString(const ::com::sun::star::util::DateTime
& rDateTime
)
157 "%04d-%02d-%02d %02d:%02d:%02d",
159 (int)rDateTime
.Month
,
161 (int)rDateTime
.Hours
,
162 (int)rDateTime
.Minutes
,
163 (int)rDateTime
.Seconds
);
165 return rtl::OUString::createFromAscii(s
);
169 //--------------------------------------------------------------------------------------------------
170 rtl::OUString
toString(const Any
& rValue
)
173 TypeClass aDestinationClass
= rValue
.getValueType().getTypeClass();
175 switch (aDestinationClass
)
178 aRes
= ::rtl::OUString::valueOf(*(sal_Unicode
*)rValue
.getValue());
180 case TypeClass_FLOAT
:
181 aRes
= ::rtl::OUString::valueOf(*(float*)rValue
.getValue());
183 case TypeClass_DOUBLE
:
184 aRes
= ::rtl::OUString::valueOf(*(double*)rValue
.getValue());
186 case TypeClass_BOOLEAN
:
187 aRes
= ::rtl::OUString::valueOf((sal_Int32
)*(sal_Bool
*)rValue
.getValue());
190 case TypeClass_SHORT
:
192 aRes
= ::rtl::OUString::valueOf(*(sal_Int32
*)rValue
.getValue());
194 case TypeClass_HYPER
:
196 sal_Int64 nValue
= 0;
197 OSL_VERIFY( rValue
>>= nValue
);
198 aRes
= ::rtl::OUString::valueOf(nValue
);
200 case TypeClass_STRING
:
203 case TypeClass_STRUCT
:
204 if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::Date
*)0))
206 ::com::sun::star::util::Date aDate
;
208 aRes
= toDateString(aDate
);
210 else if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::DateTime
*)0))
212 ::com::sun::star::util::DateTime aDT
;
214 aRes
= toDateTimeString(aDT
);
216 else if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::Time
*)0))
218 ::com::sun::star::util::Time aTime
;
220 aRes
= toTimeString(aTime
);
230 // -----------------------------------------------------------------------------
231 ::rtl::Reference
< jvmaccess::VirtualMachine
> getJavaVM(const Reference
<XMultiServiceFactory
>& _rxFactory
)
233 ::rtl::Reference
< jvmaccess::VirtualMachine
> aRet
;
234 OSL_ENSURE(_rxFactory
.is(),"No XMultiServiceFactory a.v.!");
240 Reference
< starjava::XJavaVM
> xVM(_rxFactory
->createInstance(
241 rtl::OUString::createFromAscii("com.sun.star.java.JavaVirtualMachine")), UNO_QUERY
);
243 OSL_ENSURE(_rxFactory
.is(),"InitJava: I have no factory!");
244 if (!xVM
.is() || !_rxFactory
.is())
245 throw Exception(); // -2;
247 Sequence
<sal_Int8
> processID(16);
248 rtl_getGlobalProcessId( (sal_uInt8
*) processID
.getArray() );
249 processID
.realloc(17);
252 Any uaJVM
= xVM
->getJavaVM( processID
);
254 if (!uaJVM
.hasValue())
255 throw Exception(); // -5
258 sal_Int32 nValue
= 0;
259 jvmaccess::VirtualMachine
* pJVM
= NULL
;
260 if ( uaJVM
>>= nValue
)
261 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nValue
);
266 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nTemp
);
277 //------------------------------------------------------------------------------
278 sal_Bool
existsJavaClassByName( const ::rtl::Reference
< jvmaccess::VirtualMachine
>& _pJVM
,const ::rtl::OUString
& _sClassName
)
280 sal_Bool bRet
= sal_False
;
284 jvmaccess::VirtualMachine::AttachGuard
aGuard(_pJVM
);
285 JNIEnv
* pEnv
= aGuard
.getEnvironment();
288 ::rtl::OString sClassName
= ::rtl::OUStringToOString(_sClassName
, RTL_TEXTENCODING_ASCII_US
);
289 sClassName
= sClassName
.replace('.','/');
290 jobject out
= pEnv
->FindClass(sClassName
);
292 pEnv
->DeleteLocalRef( out
);
301 #include <ctype.h> //isdigit
304 //------------------------------------------------------------------
305 sal_Bool
isCharOk(sal_Unicode c
,const ::rtl::OUString
& _rSpecials
)
308 return ( ((c
>= 97) && (c
<= 122)) || ((c
>= 65) && (c
<= 90)) || ((c
>= 48) && (c
<= 57)) ||
309 c
== '_' || _rSpecials
.indexOf(c
) != -1);
312 //------------------------------------------------------------------------------
313 sal_Bool
isValidSQLName(const ::rtl::OUString
& rName
,const ::rtl::OUString
& _rSpecials
)
315 // Ueberpruefung auf korrekte Namensgebung im SQL Sinne
316 // Dieses ist wichtig fuer Tabellennamen beispielsweise
317 const sal_Unicode
* pStr
= rName
.getStr();
318 if (*pStr
> 127 || isdigit(*pStr
))
321 for (; *pStr
; ++pStr
)
322 if(!isCharOk(*pStr
,_rSpecials
))
325 if ( rName
.getLength()
326 && ( (rName
.toChar() == '_')
327 || ( (rName
.toChar() >= '0')
328 && (rName
.toChar() <= '9')
333 // the SQL-Standard requires the first character to be an alphabetic character, which
334 // isn't easy to decide in UniCode ...
335 // So we just prohibit the characters which already lead to problems ....
336 // 11.04.00 - 74902 - FS
340 //------------------------------------------------------------------
341 // Erzeugt einen neuen Namen falls noetig
342 ::rtl::OUString
convertName2SQLName(const ::rtl::OUString
& rName
,const ::rtl::OUString
& _rSpecials
)
344 if(isValidSQLName(rName
,_rSpecials
))
346 ::rtl::OUString
aNewName(rName
);
347 const sal_Unicode
* pStr
= rName
.getStr();
348 sal_Int32 nLength
= rName
.getLength();
349 sal_Bool
bValid(*pStr
< 128 && !isdigit(*pStr
));
350 for (sal_Int32 i
=0; bValid
&& i
< nLength
; ++pStr
,++i
)
351 if(!isCharOk(*pStr
,_rSpecials
))
353 aNewName
= aNewName
.replace(*pStr
,'_');
354 pStr
= aNewName
.getStr() + i
;
358 aNewName
= ::rtl::OUString();
362 //------------------------------------------------------------------------------
363 ::rtl::OUString
quoteName(const ::rtl::OUString
& _rQuote
, const ::rtl::OUString
& _rName
)
365 ::rtl::OUString sName
= _rName
;
366 if(_rQuote
.getLength() && _rQuote
.toChar() != ' ')
367 sName
= _rQuote
+ _rName
+ _rQuote
;