Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <stdio.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/XJavaVM.hpp>
34 : #include <rtl/process.h>
35 :
36 : using namespace ::comphelper;
37 10000 : inline sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
38 : {
39 10000 : return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
40 : }
41 :
42 : namespace connectivity
43 : {
44 : using namespace ::com::sun::star::uno;
45 : using namespace ::com::sun::star::lang;
46 : using namespace ::com::sun::star::beans;
47 : using namespace dbtools;
48 : namespace starjava = com::sun::star::java;
49 : //------------------------------------------------------------------------------
50 : const sal_Unicode CHAR_PLACE = '_';
51 : const sal_Unicode CHAR_WILD = '%';
52 : // -------------------------------------------------------------------------
53 3055 : sal_Bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
54 : {
55 3055 : int pos=0;
56 3055 : int flag=0;
57 :
58 8378 : while ( *pWild || flag )
59 : {
60 5161 : switch (*pWild)
61 : {
62 : case CHAR_PLACE:
63 78 : if ( *pStr == 0 )
64 0 : return sal_False;
65 78 : break;
66 : default:
67 5000 : if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
68 0 : pWild++;
69 5000 : if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
70 2810 : if ( !pos )
71 2810 : return sal_False;
72 : else
73 0 : pWild += pos;
74 : else
75 2190 : break; // WARNING in certain circumstances
76 : // it will run into the next 'case'!!
77 : case CHAR_WILD:
78 249 : while ( *pWild == CHAR_WILD )
79 83 : pWild++;
80 83 : if ( *pWild == 0 )
81 83 : return sal_True;
82 0 : flag = 1;
83 0 : pos = 0;
84 0 : if ( *pStr == 0 )
85 0 : return ( *pWild == 0 );
86 0 : while ( *pStr && *pStr != *pWild )
87 : {
88 0 : if ( *pWild == CHAR_PLACE ) {
89 0 : pWild++;
90 0 : while ( *pWild == CHAR_WILD )
91 0 : pWild++;
92 : }
93 0 : pStr++;
94 0 : if ( *pStr == 0 )
95 0 : return ( *pWild == 0 );
96 : }
97 0 : break;
98 : }
99 2268 : if ( *pWild != 0 )
100 2268 : pWild++;
101 2268 : if ( *pStr != 0 )
102 2268 : pStr++;
103 : else
104 0 : flag = 0;
105 2268 : if ( flag )
106 0 : pos--;
107 : }
108 162 : return ( *pStr == 0 ) && ( *pWild == 0 );
109 : }
110 : //------------------------------------------------------------------
111 0 : rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate)
112 : {
113 : sal_Char s[11];
114 : snprintf(s,
115 : sizeof(s),
116 : "%04d-%02d-%02d",
117 : (int)rDate.Year,
118 : (int)rDate.Month,
119 0 : (int)rDate.Day);
120 0 : s[10] = 0;
121 0 : return rtl::OUString::createFromAscii(s);
122 : }
123 :
124 : //------------------------------------------------------------------
125 0 : rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime)
126 : {
127 : sal_Char s[9];
128 : snprintf(s,
129 : sizeof(s),
130 : "%02d:%02d:%02d",
131 : (int)rTime.Hours,
132 : (int)rTime.Minutes,
133 0 : (int)rTime.Seconds);
134 0 : s[8] = 0;
135 0 : return rtl::OUString::createFromAscii(s);
136 : }
137 :
138 : //------------------------------------------------------------------
139 0 : rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& rDateTime)
140 : {
141 : sal_Char s[20];
142 : snprintf(s,
143 : sizeof(s),
144 : "%04d-%02d-%02d %02d:%02d:%02d",
145 : (int)rDateTime.Year,
146 : (int)rDateTime.Month,
147 : (int)rDateTime.Day,
148 : (int)rDateTime.Hours,
149 : (int)rDateTime.Minutes,
150 0 : (int)rDateTime.Seconds);
151 0 : s[19] = 0;
152 0 : return rtl::OUString::createFromAscii(s);
153 : }
154 :
155 : // -----------------------------------------------------------------------------
156 0 : ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XMultiServiceFactory >& _rxFactory)
157 : {
158 0 : ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
159 : OSL_ENSURE(_rxFactory.is(),"No XMultiServiceFactory a.v.!");
160 0 : if(!_rxFactory.is())
161 0 : return aRet;
162 :
163 : try
164 : {
165 0 : Reference< starjava::XJavaVM > xVM(_rxFactory->createInstance(
166 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"))), UNO_QUERY);
167 :
168 : OSL_ENSURE(_rxFactory.is(),"InitJava: I have no factory!");
169 0 : if (!xVM.is() || !_rxFactory.is())
170 0 : throw Exception(); // -2;
171 :
172 0 : Sequence<sal_Int8> processID(16);
173 0 : rtl_getGlobalProcessId( (sal_uInt8*) processID.getArray() );
174 0 : processID.realloc(17);
175 0 : processID[16] = 0;
176 :
177 0 : Any uaJVM = xVM->getJavaVM( processID );
178 :
179 0 : if (!uaJVM.hasValue())
180 0 : throw Exception(); // -5
181 : else
182 : {
183 0 : sal_Int32 nValue = 0;
184 0 : jvmaccess::VirtualMachine* pJVM = NULL;
185 0 : if ( uaJVM >>= nValue )
186 0 : pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nValue);
187 : else
188 : {
189 0 : sal_Int64 nTemp = 0;
190 0 : uaJVM >>= nTemp;
191 0 : pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nTemp);
192 : }
193 0 : aRet = pJVM;
194 0 : }
195 : }
196 0 : catch (Exception&)
197 : {
198 : }
199 :
200 0 : return aRet;
201 : }
202 : //------------------------------------------------------------------------------
203 0 : sal_Bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,const ::rtl::OUString& _sClassName )
204 : {
205 0 : sal_Bool bRet = sal_False;
206 : #ifdef SOLAR_JAVA
207 0 : if ( _pJVM.is() )
208 : {
209 0 : jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
210 0 : JNIEnv* pEnv = aGuard.getEnvironment();
211 0 : if( pEnv )
212 : {
213 0 : ::rtl::OString sClassName = ::rtl::OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
214 0 : sClassName = sClassName.replace('.','/');
215 0 : jobject out = pEnv->FindClass(sClassName.getStr());
216 0 : bRet = out != NULL;
217 0 : pEnv->DeleteLocalRef( out );
218 0 : }
219 : }
220 : #else
221 : (void)_pJVM;
222 : (void)_sClassName;
223 : #endif
224 0 : return bRet;
225 : }
226 :
227 : }
228 :
229 : #include <ctype.h> //isdigit
230 : namespace dbtools
231 : {
232 : //------------------------------------------------------------------
233 0 : sal_Bool isCharOk(sal_Unicode c,const ::rtl::OUString& _rSpecials)
234 : {
235 :
236 : return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) ||
237 0 : c == '_' || _rSpecials.indexOf(c) != -1);
238 : }
239 :
240 : //------------------------------------------------------------------------------
241 0 : sal_Bool isValidSQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
242 : {
243 : // Test for correct naming (in SQL sense)
244 : // This is important for table names for example
245 0 : const sal_Unicode* pStr = rName.getStr();
246 0 : if (*pStr > 127 || isdigit(*pStr))
247 0 : return sal_False;
248 :
249 0 : for (; *pStr; ++pStr )
250 0 : if(!isCharOk(*pStr,_rSpecials))
251 0 : return sal_False;
252 :
253 0 : if ( !rName.isEmpty()
254 0 : && ( (rName.toChar() == '_')
255 0 : || ( (rName.toChar() >= '0')
256 0 : && (rName.toChar() <= '9')
257 : )
258 : )
259 : )
260 0 : return sal_False;
261 : // the SQL-Standard requires the first character to be an alphabetic character, which
262 : // isn't easy to decide in UniCode ...
263 : // So we just prohibit the characters which already lead to problems ....
264 : // 11.04.00 - 74902 - FS
265 :
266 0 : return sal_True;
267 : }
268 : //------------------------------------------------------------------
269 : // Creates a new name if necessary
270 0 : ::rtl::OUString convertName2SQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
271 : {
272 0 : if(isValidSQLName(rName,_rSpecials))
273 0 : return rName;
274 0 : ::rtl::OUString aNewName(rName);
275 0 : const sal_Unicode* pStr = rName.getStr();
276 0 : sal_Int32 nLength = rName.getLength();
277 0 : sal_Bool bValid(*pStr < 128 && !isdigit(*pStr));
278 0 : for (sal_Int32 i=0; bValid && i < nLength; ++pStr,++i )
279 0 : if(!isCharOk(*pStr,_rSpecials))
280 : {
281 0 : aNewName = aNewName.replace(*pStr,'_');
282 0 : pStr = aNewName.getStr() + i;
283 : }
284 :
285 0 : if ( !bValid )
286 0 : aNewName = ::rtl::OUString();
287 :
288 0 : return aNewName;
289 : }
290 : //------------------------------------------------------------------------------
291 0 : ::rtl::OUString quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName)
292 : {
293 0 : ::rtl::OUString sName = _rName;
294 0 : if( !_rQuote.isEmpty() && _rQuote.toChar() != ' ')
295 0 : sName = _rQuote + _rName + _rQuote;
296 0 : return sName;
297 : }
298 :
299 :
300 : }
301 :
302 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|