1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <connectivity/CommonTools.hxx>
23 #include <connectivity/dbtools.hxx>
24 #include <com/sun/star/util/Date.hpp>
25 #include <com/sun/star/util/Time.hpp>
26 #include <com/sun/star/util/DateTime.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <comphelper/extract.hxx>
30 #include <cppuhelper/interfacecontainer.h>
31 #include "TConnection.hxx"
32 #include <comphelper/types.hxx>
33 #include <com/sun/star/java/JavaVirtualMachine.hpp>
35 #include <jvmaccess/virtualmachine.hxx>
37 #include <rtl/process.h>
39 using namespace ::comphelper
;
40 inline sal_Unicode
rtl_ascii_toUpperCase( sal_Unicode ch
)
42 return ch
>= 0x0061 && ch
<= 0x007a ? ch
+ 0x20 : ch
;
45 namespace connectivity
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::lang
;
49 using namespace ::com::sun::star::beans
;
50 using namespace ::com::sun::star::java
;
51 using namespace dbtools
;
53 const sal_Unicode CHAR_PLACE
= '_';
54 const sal_Unicode CHAR_WILD
= '%';
56 bool match(const sal_Unicode
* pWild
, const sal_Unicode
* pStr
, const sal_Unicode cEscape
)
61 while ( *pWild
|| flag
)
70 if (*pWild
&& (*pWild
== cEscape
) && ((*(pWild
+1)== CHAR_PLACE
) || (*(pWild
+1) == CHAR_WILD
)) )
72 if ( rtl_ascii_toUpperCase(*pWild
) != rtl_ascii_toUpperCase(*pStr
) )
78 break; // WARNING in certain circumstances
79 // it will run into the next 'case'!!
81 while ( *pWild
== CHAR_WILD
)
88 return ( *pWild
== 0 );
89 while ( *pStr
&& *pStr
!= *pWild
)
91 if ( *pWild
== CHAR_PLACE
) {
93 while ( *pWild
== CHAR_WILD
)
98 return ( *pWild
== 0 );
111 return ( *pStr
== 0 ) && ( *pWild
== 0 );
114 #if HAVE_FEATURE_JAVA
115 ::rtl::Reference
< jvmaccess::VirtualMachine
> getJavaVM(const Reference
<XComponentContext
>& _rxContext
)
117 ::rtl::Reference
< jvmaccess::VirtualMachine
> aRet
;
118 OSL_ENSURE(_rxContext
.is(),"No XMultiServiceFactory a.v.!");
124 Reference
< XJavaVM
> xVM
= JavaVirtualMachine::create(_rxContext
);
126 Sequence
<sal_Int8
> processID(16);
127 rtl_getGlobalProcessId( reinterpret_cast<sal_uInt8
*>(processID
.getArray()) );
128 processID
.realloc(17);
131 Any uaJVM
= xVM
->getJavaVM( processID
);
133 if (!uaJVM
.hasValue())
134 throw Exception(); // -5
137 sal_Int32 nValue
= 0;
138 jvmaccess::VirtualMachine
* pJVM
= NULL
;
139 if ( uaJVM
>>= nValue
)
140 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nValue
);
145 pJVM
= reinterpret_cast< jvmaccess::VirtualMachine
* > (nTemp
);
157 bool existsJavaClassByName( const ::rtl::Reference
< jvmaccess::VirtualMachine
>& _pJVM
,const OUString
& _sClassName
)
162 jvmaccess::VirtualMachine::AttachGuard
aGuard(_pJVM
);
163 JNIEnv
* pEnv
= aGuard
.getEnvironment();
166 OString sClassName
= OUStringToOString(_sClassName
, RTL_TEXTENCODING_ASCII_US
);
167 sClassName
= sClassName
.replace('.','/');
168 jobject out
= pEnv
->FindClass(sClassName
.getStr());
170 pEnv
->DeleteLocalRef( out
);
182 bool isCharOk(sal_Unicode c
,const OUString
& _rSpecials
)
185 return ( ((c
>= 97) && (c
<= 122)) || ((c
>= 65) && (c
<= 90)) || ((c
>= 48) && (c
<= 57)) ||
186 c
== '_' || _rSpecials
.indexOf(c
) != -1);
190 bool isValidSQLName(const OUString
& rName
,const OUString
& _rSpecials
)
192 // Test for correct naming (in SQL sense)
193 // This is important for table names for example
194 const sal_Unicode
* pStr
= rName
.getStr();
195 if (*pStr
> 127 || isdigit(*pStr
))
198 for (; *pStr
; ++pStr
)
199 if(!isCharOk(*pStr
,_rSpecials
))
202 if ( !rName
.isEmpty()
203 && ( (rName
.toChar() == '_')
204 || ( (rName
.toChar() >= '0')
205 && (rName
.toChar() <= '9')
210 // the SQL-Standard requires the first character to be an alphabetic character, which
211 // isn't easy to decide in UniCode ...
212 // So we just prohibit the characters which already lead to problems ....
213 // 11.04.00 - 74902 - FS
218 // Creates a new name if necessary
219 OUString
convertName2SQLName(const OUString
& rName
,const OUString
& _rSpecials
)
221 if(isValidSQLName(rName
,_rSpecials
))
223 OUString
aNewName(rName
);
224 const sal_Unicode
* pStr
= rName
.getStr();
225 sal_Int32 nLength
= rName
.getLength();
226 bool bValid(*pStr
< 128 && !isdigit(*pStr
));
227 for (sal_Int32 i
=0; bValid
&& i
< nLength
; ++pStr
,++i
)
228 if(!isCharOk(*pStr
,_rSpecials
))
230 aNewName
= aNewName
.replace(*pStr
,'_');
231 pStr
= aNewName
.getStr() + i
;
240 OUString
quoteName(const OUString
& _rQuote
, const OUString
& _rName
)
242 OUString sName
= _rName
;
243 if( !_rQuote
.isEmpty() && _rQuote
.toChar() != ' ')
244 sName
= _rQuote
+ _rName
+ _rQuote
;
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */