1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include "connectivity/CommonTools.hxx"
32 #include "connectivity/dbtools.hxx"
33 #include <com/sun/star/util/Date.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/util/DateTime.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <comphelper/extract.hxx>
39 #include <cppuhelper/interfacecontainer.h>
40 #include "TConnection.hxx"
41 #include <comphelper/types.hxx>
42 #include <com/sun/star/java/XJavaVM.hpp>
43 #include <rtl/process.h>
45 using namespace ::comphelper
;
46 inline sal_Unicode
rtl_ascii_toUpperCase( sal_Unicode ch
)
48 return ch
>= 0x0061 && ch
<= 0x007a ? ch
+ 0x20 : ch
;
51 namespace connectivity
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::beans
;
56 using namespace dbtools
;
57 namespace starjava
= com::sun::star::java
;
58 //------------------------------------------------------------------------------
59 const sal_Unicode CHAR_PLACE
= '_';
60 const sal_Unicode CHAR_WILD
= '%';
61 // -------------------------------------------------------------------------
62 sal_Bool
match(const sal_Unicode
* pWild
, const sal_Unicode
* pStr
, const sal_Unicode cEscape
)
67 while ( *pWild
|| flag
)
76 if (*pWild
&& (*pWild
== cEscape
) && ((*(pWild
+1)== CHAR_PLACE
) || (*(pWild
+1) == CHAR_WILD
)) )
78 if ( rtl_ascii_toUpperCase(*pWild
) != rtl_ascii_toUpperCase(*pStr
) )
84 break; // WARNING in certain circumstances
85 // it will run into the next 'case'!!
87 while ( *pWild
== CHAR_WILD
)
94 return ( *pWild
== 0 );
95 while ( *pStr
&& *pStr
!= *pWild
)
97 if ( *pWild
== CHAR_PLACE
) {
99 while ( *pWild
== CHAR_WILD
)
104 return ( *pWild
== 0 );
117 return ( *pStr
== 0 ) && ( *pWild
== 0 );
119 //------------------------------------------------------------------
120 rtl::OUString
toDateString(const ::com::sun::star::util::Date
& rDate
)
130 return rtl::OUString::createFromAscii(s
);
133 //------------------------------------------------------------------
134 rtl::OUString
toTimeString(const ::com::sun::star::util::Time
& rTime
)
144 return rtl::OUString::createFromAscii(s
);
147 //------------------------------------------------------------------
148 rtl::OUString
toDateTimeString(const ::com::sun::star::util::DateTime
& rDateTime
)
153 "%04d-%02d-%02d %02d:%02d:%02d",
155 (int)rDateTime
.Month
,
157 (int)rDateTime
.Hours
,
158 (int)rDateTime
.Minutes
,
159 (int)rDateTime
.Seconds
);
161 return rtl::OUString::createFromAscii(s
);
165 //--------------------------------------------------------------------------------------------------
166 rtl::OUString
toString(const Any
& rValue
)
169 TypeClass aDestinationClass
= rValue
.getValueType().getTypeClass();
171 switch (aDestinationClass
)
174 aRes
= ::rtl::OUString::valueOf(*(sal_Unicode
*)rValue
.getValue());
176 case TypeClass_FLOAT
:
177 aRes
= ::rtl::OUString::valueOf(*(float*)rValue
.getValue());
179 case TypeClass_DOUBLE
:
180 aRes
= ::rtl::OUString::valueOf(*(double*)rValue
.getValue());
182 case TypeClass_BOOLEAN
:
183 aRes
= ::rtl::OUString::valueOf((sal_Int32
)*(sal_Bool
*)rValue
.getValue());
186 case TypeClass_SHORT
:
188 aRes
= ::rtl::OUString::valueOf(*(sal_Int32
*)rValue
.getValue());
190 case TypeClass_HYPER
:
192 sal_Int64 nValue
= 0;
193 OSL_VERIFY( rValue
>>= nValue
);
194 aRes
= ::rtl::OUString::valueOf(nValue
);
196 case TypeClass_STRING
:
199 case TypeClass_STRUCT
:
200 if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::Date
*)0))
202 ::com::sun::star::util::Date aDate
;
204 aRes
= toDateString(aDate
);
206 else if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::DateTime
*)0))
208 ::com::sun::star::util::DateTime aDT
;
210 aRes
= toDateTimeString(aDT
);
212 else if (rValue
.getValueType() == ::getCppuType((const ::com::sun::star::util::Time
*)0))
214 ::com::sun::star::util::Time aTime
;
216 aRes
= toTimeString(aTime
);
226 // -----------------------------------------------------------------------------
227 ::rtl::Reference
< jvmaccess::VirtualMachine
> getJavaVM(const Reference
<XMultiServiceFactory
>& _rxFactory
)
229 ::rtl::Reference
< jvmaccess::VirtualMachine
> aRet
;
230 OSL_ENSURE(_rxFactory
.is(),"No XMultiServiceFactory a.v.!");
236 Reference
< starjava::XJavaVM
> xVM(_rxFactory
->createInstance(
237 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"))), UNO_QUERY
);
239 OSL_ENSURE(_rxFactory
.is(),"InitJava: I have no factory!");
240 if (!xVM
.is() || !_rxFactory
.is())
241 throw Exception(); // -2;
243 Sequence
<sal_Int8
> processID(16);
244 rtl_getGlobalProcessId( (sal_uInt8
*) processID
.getArray() );
245 processID
.realloc(17);
248 Any uaJVM
= xVM
->getJavaVM( processID
);
250 if (!uaJVM
.hasValue())
251 throw Exception(); // -5
254 sal_Int32 nValue
= 0;
255 jvmaccess::VirtualMachine
* pJVM
= NULL
;
256 if ( uaJVM
>>= nValue
)
257 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nValue
);
262 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nTemp
);
273 //------------------------------------------------------------------------------
274 sal_Bool
existsJavaClassByName( const ::rtl::Reference
< jvmaccess::VirtualMachine
>& _pJVM
,const ::rtl::OUString
& _sClassName
)
276 sal_Bool bRet
= sal_False
;
280 jvmaccess::VirtualMachine::AttachGuard
aGuard(_pJVM
);
281 JNIEnv
* pEnv
= aGuard
.getEnvironment();
284 ::rtl::OString sClassName
= ::rtl::OUStringToOString(_sClassName
, RTL_TEXTENCODING_ASCII_US
);
285 sClassName
= sClassName
.replace('.','/');
286 jobject out
= pEnv
->FindClass(sClassName
.getStr());
288 pEnv
->DeleteLocalRef( out
);
300 #include <ctype.h> //isdigit
303 //------------------------------------------------------------------
304 sal_Bool
isCharOk(sal_Unicode c
,const ::rtl::OUString
& _rSpecials
)
307 return ( ((c
>= 97) && (c
<= 122)) || ((c
>= 65) && (c
<= 90)) || ((c
>= 48) && (c
<= 57)) ||
308 c
== '_' || _rSpecials
.indexOf(c
) != -1);
311 //------------------------------------------------------------------------------
312 sal_Bool
isValidSQLName(const ::rtl::OUString
& rName
,const ::rtl::OUString
& _rSpecials
)
314 // Test for correct naming (in SQL sense)
315 // This is important for table names for example
316 const sal_Unicode
* pStr
= rName
.getStr();
317 if (*pStr
> 127 || isdigit(*pStr
))
320 for (; *pStr
; ++pStr
)
321 if(!isCharOk(*pStr
,_rSpecials
))
324 if ( !rName
.isEmpty()
325 && ( (rName
.toChar() == '_')
326 || ( (rName
.toChar() >= '0')
327 && (rName
.toChar() <= '9')
332 // the SQL-Standard requires the first character to be an alphabetic character, which
333 // isn't easy to decide in UniCode ...
334 // So we just prohibit the characters which already lead to problems ....
335 // 11.04.00 - 74902 - FS
339 //------------------------------------------------------------------
340 // Creates a new name if necessary
341 ::rtl::OUString
convertName2SQLName(const ::rtl::OUString
& rName
,const ::rtl::OUString
& _rSpecials
)
343 if(isValidSQLName(rName
,_rSpecials
))
345 ::rtl::OUString
aNewName(rName
);
346 const sal_Unicode
* pStr
= rName
.getStr();
347 sal_Int32 nLength
= rName
.getLength();
348 sal_Bool
bValid(*pStr
< 128 && !isdigit(*pStr
));
349 for (sal_Int32 i
=0; bValid
&& i
< nLength
; ++pStr
,++i
)
350 if(!isCharOk(*pStr
,_rSpecials
))
352 aNewName
= aNewName
.replace(*pStr
,'_');
353 pStr
= aNewName
.getStr() + i
;
357 aNewName
= ::rtl::OUString();
361 //------------------------------------------------------------------------------
362 ::rtl::OUString
quoteName(const ::rtl::OUString
& _rQuote
, const ::rtl::OUString
& _rName
)
364 ::rtl::OUString sName
= _rName
;
365 if( !_rQuote
.isEmpty() && _rQuote
.toChar() != ' ')
366 sName
= _rQuote
+ _rName
+ _rQuote
;
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */