xref: /AOO41X/main/connectivity/source/commontools/CommonTools.cxx (revision 24c56ab9f1bd1305754aa2f564704f38ff57627e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include <stdio.h>
28 #include "connectivity/CommonTools.hxx"
29 #include "connectivity/dbtools.hxx"
30 #include <com/sun/star/util/Date.hpp>
31 #include <com/sun/star/util/Time.hpp>
32 #include <com/sun/star/util/DateTime.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <comphelper/extract.hxx>
36 #include <cppuhelper/interfacecontainer.h>
37 #include "TConnection.hxx"
38 #include <comphelper/types.hxx>
39 #include <com/sun/star/java/XJavaVM.hpp>
40 #include <rtl/process.h>
41 
42 using namespace ::comphelper;
rtl_ascii_toUpperCase(sal_Unicode ch)43 inline sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
44 {
45     return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
46 }
47 
48 namespace connectivity
49 {
50     using namespace ::com::sun::star::uno;
51     using namespace ::com::sun::star::lang;
52     using namespace ::com::sun::star::beans;
53     using namespace dbtools;
54     namespace starjava  = com::sun::star::java;
55     //------------------------------------------------------------------------------
56     const sal_Unicode CHAR_PLACE = '_';
57     const sal_Unicode CHAR_WILD  = '%';
58     // -------------------------------------------------------------------------
match(const sal_Unicode * pWild,const sal_Unicode * pStr,const sal_Unicode cEscape)59     sal_Bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
60     {
61         int    pos=0;
62         int    flag=0;
63 
64         while ( *pWild || flag )
65         {
66             switch (*pWild)
67             {
68                 case CHAR_PLACE:
69                     if ( *pStr == 0 )
70                         return sal_False;
71                     break;
72                 default:
73                     if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
74                         pWild++;
75                     if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
76                         if ( !pos )
77                             return sal_False;
78                         else
79                             pWild += pos;
80                     else
81                         break;          // ACHTUNG laeuft unter bestimmten
82                                         // Umstaenden in den nachsten case rein!!
83                 case CHAR_WILD:
84                     while ( *pWild == CHAR_WILD )
85                         pWild++;
86                     if ( *pWild == 0 )
87                         return sal_True;
88                     flag = 1;
89                     pos  = 0;
90                     if ( *pStr == 0 )
91                         return ( *pWild == 0 );
92                     while ( *pStr && *pStr != *pWild )
93                     {
94                         if ( *pWild == CHAR_PLACE ) {
95                             pWild++;
96                             while ( *pWild == CHAR_WILD )
97                                 pWild++;
98                         }
99                         pStr++;
100                         if ( *pStr == 0 )
101                             return ( *pWild == 0 );
102                     }
103                     break;
104             }
105             if ( *pWild != 0 )
106                 pWild++;
107             if ( *pStr != 0 )
108                 pStr++;
109             else
110                 flag = 0;
111             if ( flag )
112                 pos--;
113         }
114         return ( *pStr == 0 ) && ( *pWild == 0 );
115     }
116     //------------------------------------------------------------------
toDateString(const::com::sun::star::util::Date & rDate)117     rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate)
118     {
119         sal_Char s[11];
120         snprintf(s,
121                 sizeof(s),
122                 "%04d-%02d-%02d",
123                 (int)rDate.Year,
124                 (int)rDate.Month,
125                 (int)rDate.Day);
126         s[10] = 0;
127         return rtl::OUString::createFromAscii(s);
128     }
129 
130     //------------------------------------------------------------------
toTimeString(const::com::sun::star::util::Time & rTime)131     rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime)
132     {
133         sal_Char s[9];
134         snprintf(s,
135                 sizeof(s),
136                 "%02d:%02d:%02d",
137                 (int)rTime.Hours,
138                 (int)rTime.Minutes,
139                 (int)rTime.Seconds);
140         s[8] = 0;
141         return rtl::OUString::createFromAscii(s);
142     }
143 
144     //------------------------------------------------------------------
toDateTimeString(const::com::sun::star::util::DateTime & rDateTime)145     rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& rDateTime)
146     {
147         sal_Char s[21];
148         snprintf(s,
149                 sizeof(s),
150                 "%04d-%02d-%02d %02d:%02d:%02d",
151                 (int)rDateTime.Year,
152                 (int)rDateTime.Month,
153                 (int)rDateTime.Day,
154                 (int)rDateTime.Hours,
155                 (int)rDateTime.Minutes,
156                 (int)rDateTime.Seconds);
157         s[20] = 0;
158         return rtl::OUString::createFromAscii(s);
159     }
160 
161 
162     //--------------------------------------------------------------------------------------------------
toString(const Any & rValue)163     rtl::OUString toString(const Any& rValue)
164     {
165         rtl::OUString aRes;
166         TypeClass aDestinationClass = rValue.getValueType().getTypeClass();
167 
168         switch (aDestinationClass)
169         {
170             case TypeClass_CHAR:
171                 aRes = ::rtl::OUString::valueOf(*(sal_Unicode*)rValue.getValue());
172                 break;
173             case TypeClass_FLOAT:
174                 aRes = ::rtl::OUString::valueOf(*(float*)rValue.getValue());
175                 break;
176             case TypeClass_DOUBLE:
177                 aRes = ::rtl::OUString::valueOf(*(double*)rValue.getValue());
178                 break;
179             case TypeClass_BOOLEAN:
180                 aRes = ::rtl::OUString::valueOf((sal_Int32)*(sal_Bool*)rValue.getValue());
181                 break;
182             case TypeClass_BYTE:
183             case TypeClass_SHORT:
184             case TypeClass_LONG:
185                 aRes = ::rtl::OUString::valueOf(*(sal_Int32*)rValue.getValue());
186                 break;
187             case TypeClass_HYPER:
188             {
189                 sal_Int64 nValue = 0;
190                 OSL_VERIFY( rValue >>= nValue );
191                 aRes = ::rtl::OUString::valueOf(nValue);
192             }
193             case TypeClass_STRING:
194                 rValue >>= aRes;
195                 break;
196             case TypeClass_STRUCT:
197                 if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Date*)0))
198                 {
199                     ::com::sun::star::util::Date aDate;
200                     rValue >>= aDate;
201                     aRes = toDateString(aDate);
202                 }
203                 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::DateTime*)0))
204                 {
205                     ::com::sun::star::util::DateTime aDT;
206                     rValue >>= aDT;
207                     aRes = toDateTimeString(aDT);
208                 }
209                 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Time*)0))
210                 {
211                     ::com::sun::star::util::Time aTime;
212                     rValue >>= aTime;
213                     aRes = toTimeString(aTime);
214                 }
215 
216                 break;
217             default:
218                 ;
219         }
220         return aRes;
221     }
222 
223     // -----------------------------------------------------------------------------
getJavaVM(const Reference<XMultiServiceFactory> & _rxFactory)224     ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XMultiServiceFactory >& _rxFactory)
225     {
226         ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
227         OSL_ENSURE(_rxFactory.is(),"No XMultiServiceFactory a.v.!");
228         if(!_rxFactory.is())
229             return aRet;
230 
231         try
232         {
233             Reference< starjava::XJavaVM > xVM(_rxFactory->createInstance(
234                 rtl::OUString::createFromAscii("com.sun.star.java.JavaVirtualMachine")), UNO_QUERY);
235 
236             OSL_ENSURE(_rxFactory.is(),"InitJava: I have no factory!");
237             if (!xVM.is() || !_rxFactory.is())
238                 throw Exception(); // -2;
239 
240             Sequence<sal_Int8> processID(16);
241             rtl_getGlobalProcessId( (sal_uInt8*) processID.getArray() );
242             processID.realloc(17);
243             processID[16] = 0;
244 
245             Any uaJVM = xVM->getJavaVM( processID );
246 
247             if (!uaJVM.hasValue())
248                 throw Exception(); // -5
249             else
250             {
251                 sal_Int32 nValue = 0;
252                 jvmaccess::VirtualMachine* pJVM = NULL;
253                 if ( uaJVM >>= nValue )
254                     pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nValue);
255                 else
256                 {
257                     sal_Int64 nTemp = 0;
258                     uaJVM >>= nTemp;
259                     pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nTemp);
260                 }
261                 aRet = pJVM;
262             }
263         }
264         catch (Exception&)
265         {
266         }
267 
268         return aRet;
269     }
270     //------------------------------------------------------------------------------
existsJavaClassByName(const::rtl::Reference<jvmaccess::VirtualMachine> & _pJVM,const::rtl::OUString & _sClassName)271     sal_Bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,const ::rtl::OUString& _sClassName )
272     {
273         sal_Bool bRet = sal_False;
274 #ifdef SOLAR_JAVA
275         if ( _pJVM.is() )
276         {
277             jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
278             JNIEnv* pEnv = aGuard.getEnvironment();
279             if( pEnv )
280             {
281                 ::rtl::OString sClassName = ::rtl::OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
282                 sClassName = sClassName.replace('.','/');
283                 jobject out = pEnv->FindClass( sClassName.getStr());
284                 bRet = out != NULL;
285                 pEnv->DeleteLocalRef( out );
286             }
287         }
288 #endif
289         return bRet;
290     }
291 
292 }
293 
294 #include <ctype.h>      //isdigit
295 namespace dbtools
296 {
297 //------------------------------------------------------------------
isCharOk(sal_Unicode c,const::rtl::OUString & _rSpecials)298 sal_Bool isCharOk(sal_Unicode c,const ::rtl::OUString& _rSpecials)
299 {
300 
301     return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <=  90)) || ((c >= 48) && (c <=  57)) ||
302           c == '_' || _rSpecials.indexOf(c) != -1);
303 }
304 
305 //------------------------------------------------------------------------------
isValidSQLName(const::rtl::OUString & rName,const::rtl::OUString & _rSpecials)306 sal_Bool isValidSQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
307 {
308     // Ueberpruefung auf korrekte Namensgebung im SQL Sinne
309     // Dieses ist wichtig fuer Tabellennamen beispielsweise
310     const sal_Unicode* pStr = rName.getStr();
311     if (*pStr > 127 || isdigit(*pStr))
312         return sal_False;
313 
314     for (; *pStr; ++pStr )
315         if(!isCharOk(*pStr,_rSpecials))
316             return sal_False;
317 
318     if  (   rName.getLength()
319         &&  (   (rName.toChar() == '_')
320             ||  (   (rName.toChar() >= '0')
321                 &&  (rName.toChar() <= '9')
322                 )
323             )
324         )
325         return sal_False;
326     // the SQL-Standard requires the first character to be an alphabetic character, which
327     // isn't easy to decide in UniCode ...
328     // So we just prohibit the characters which already lead to problems ....
329     // 11.04.00 - 74902 - FS
330 
331     return sal_True;
332 }
333 //------------------------------------------------------------------
334 // Erzeugt einen neuen Namen falls noetig
convertName2SQLName(const::rtl::OUString & rName,const::rtl::OUString & _rSpecials)335 ::rtl::OUString convertName2SQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
336 {
337     if(isValidSQLName(rName,_rSpecials))
338         return rName;
339     ::rtl::OUString aNewName(rName);
340     const sal_Unicode* pStr = rName.getStr();
341     sal_Int32 nLength = rName.getLength();
342     sal_Bool bValid(*pStr < 128 && !isdigit(*pStr));
343     for (sal_Int32 i=0; bValid && i < nLength; ++pStr,++i )
344         if(!isCharOk(*pStr,_rSpecials))
345         {
346             aNewName = aNewName.replace(*pStr,'_');
347             pStr = aNewName.getStr() + i;
348         }
349 
350     if ( !bValid )
351         aNewName = ::rtl::OUString();
352 
353     return aNewName;
354 }
355 //------------------------------------------------------------------------------
quoteName(const::rtl::OUString & _rQuote,const::rtl::OUString & _rName)356 ::rtl::OUString quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName)
357 {
358     ::rtl::OUString sName = _rName;
359     if(_rQuote.getLength() && _rQuote.toChar() != ' ')
360         sName = _rQuote + _rName + _rQuote;
361     return sName;
362 }
363 
364 
365 }
366