xref: /trunk/main/connectivity/source/drivers/jdbc/tools.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
19b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59b5730f6SAndrew Rist  * distributed with this work for additional information
69b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
99b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
119b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
139b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149b5730f6SAndrew Rist  * software distributed under the License is distributed on an
159b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
179b5730f6SAndrew Rist  * specific language governing permissions and limitations
189b5730f6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
209b5730f6SAndrew Rist  *************************************************************/
219b5730f6SAndrew Rist 
229b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cstdarg>
28cdf0e10cSrcweir #include "java/tools.hxx"
29cdf0e10cSrcweir #include "java/lang/String.hxx"
30cdf0e10cSrcweir #include "java/lang/Class.hxx"
31cdf0e10cSrcweir #include "java/util/Property.hxx"
32cdf0e10cSrcweir #include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
33cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
34cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace connectivity;
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir using namespace ::com::sun::star::beans;
39cdf0e10cSrcweir //  using namespace ::com::sun::star::sdbcx;
40cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
41cdf0e10cSrcweir using namespace ::com::sun::star::container;
42cdf0e10cSrcweir using namespace ::com::sun::star::lang;
43cdf0e10cSrcweir 
setProperty(const::rtl::OUString key,const::rtl::OUString & value)44cdf0e10cSrcweir void java_util_Properties::setProperty(const ::rtl::OUString key, const ::rtl::OUString& value)
45cdf0e10cSrcweir {
46*a3cdc23eSJohn Bampton     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Environment been deleted!");
47cdf0e10cSrcweir     jobject out(0);
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         jvalue args[2];
51cdf0e10cSrcweir         // Parameter konvertieren
52cdf0e10cSrcweir         args[0].l = convertwchar_tToJavaString(t.pEnv,key);
53cdf0e10cSrcweir         args[1].l = convertwchar_tToJavaString(t.pEnv,value);
54cdf0e10cSrcweir         // temporaere Variable initialisieren
55cdf0e10cSrcweir         static const char * cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
56cdf0e10cSrcweir         static const char * cMethodName = "setProperty";
57cdf0e10cSrcweir         // Java-Call absetzen
58cdf0e10cSrcweir         static jmethodID mID(NULL);
59cdf0e10cSrcweir         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
60cdf0e10cSrcweir         out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
61cdf0e10cSrcweir         ThrowSQLException(t.pEnv,NULL);
62cdf0e10cSrcweir         t.pEnv->DeleteLocalRef((jstring)args[1].l);
63cdf0e10cSrcweir         t.pEnv->DeleteLocalRef((jstring)args[0].l);
64cdf0e10cSrcweir         ThrowSQLException(t.pEnv,0);
65cdf0e10cSrcweir         if(out)
66cdf0e10cSrcweir             t.pEnv->DeleteLocalRef(out);
67cdf0e10cSrcweir     } //t.pEnv
68cdf0e10cSrcweir     // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
69cdf0e10cSrcweir }
70cdf0e10cSrcweir jclass java_util_Properties::theClass = 0;
71cdf0e10cSrcweir 
~java_util_Properties()72cdf0e10cSrcweir java_util_Properties::~java_util_Properties()
73cdf0e10cSrcweir {}
74cdf0e10cSrcweir 
getMyClass() const75cdf0e10cSrcweir jclass java_util_Properties::getMyClass() const
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     // die Klasse muss nur einmal geholt werden, daher statisch
78cdf0e10cSrcweir     if( !theClass )
79cdf0e10cSrcweir         theClass = findMyClass("java/util/Properties");
80cdf0e10cSrcweir     return theClass;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //--------------------------------------------------------------------------
java_util_Properties()84cdf0e10cSrcweir java_util_Properties::java_util_Properties( ): java_lang_Object( NULL, (jobject)NULL )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     SDBThreadAttach t;
87cdf0e10cSrcweir     if( !t.pEnv )
88cdf0e10cSrcweir         return;
89cdf0e10cSrcweir     // Java-Call fuer den Konstruktor absetzen
90cdf0e10cSrcweir     // temporaere Variable initialisieren
91cdf0e10cSrcweir     static const char * cSignature = "()V";
92cdf0e10cSrcweir     jobject tempObj;
93cdf0e10cSrcweir     static jmethodID mID(NULL);
94cdf0e10cSrcweir     obtainMethodId(t.pEnv, "<init>",cSignature, mID);
95cdf0e10cSrcweir     tempObj = t.pEnv->NewObject( getMyClass(), mID);
96cdf0e10cSrcweir     saveRef( t.pEnv, tempObj );
97cdf0e10cSrcweir     t.pEnv->DeleteLocalRef( tempObj );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // --------------------------------------------------------------------------------
convertwchar_tToJavaString(JNIEnv * pEnv,const::rtl::OUString & _rTemp)101cdf0e10cSrcweir jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const ::rtl::OUString& _rTemp)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     OSL_ENSURE(pEnv,"Environment is NULL!");
104cdf0e10cSrcweir     jstring pStr = pEnv->NewString(_rTemp.getStr(), _rTemp.getLength());
105cdf0e10cSrcweir     pEnv->ExceptionClear();
106cdf0e10cSrcweir     OSL_ENSURE(pStr,"Could not create a jsstring object!");
107cdf0e10cSrcweir     return pStr;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir // --------------------------------------------------------------------------------
createStringPropertyArray(const Sequence<PropertyValue> & info)111cdf0e10cSrcweir java_util_Properties* connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info )  throw(SQLException, RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     java_util_Properties* pProps = new java_util_Properties();
114cdf0e10cSrcweir     const PropertyValue* pBegin = info.getConstArray();
115cdf0e10cSrcweir     const PropertyValue* pEnd   = pBegin + info.getLength();
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     for(;pBegin != pEnd;++pBegin)
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         // this is a special property to find the jdbc driver
120cdf0e10cSrcweir         if  (   pBegin->Name.compareToAscii( "JavaDriverClass" )
121cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "JavaDriverClassPath" )
122cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "SystemProperties" )
123cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "CharSet" )
124cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "AppendTableAliasName" )
125cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "AddIndexAppendix" )
126cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "FormsCheckRequiredFields" )
127cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "GenerateASBeforeCorrelationName" )
128cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "EscapeDateTime" )
129cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "ParameterNameSubstitution" )
130cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "IsPasswordRequired" )
131cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "IsAutoRetrievingEnabled" )
132cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "AutoRetrievingStatement" )
133cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "UseCatalogInSelect" )
134cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "UseSchemaInSelect" )
135cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "AutoIncrementCreation" )
136cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "Extension" )
137cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "NoNameLengthLimit" )
138cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "EnableSQL92Check" )
139cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "EnableOuterJoinEscape" )
140cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "BooleanComparisonMode" )
141cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "IgnoreCurrency" )
142cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "TypeInfoSettings" )
143cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "IgnoreDriverPrivileges" )
144cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "ImplicitCatalogRestriction" )
145cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "ImplicitSchemaRestriction" )
146cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "SupportsTableCreation" )
147cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "UseJava" )
148cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "Authentication" )
149cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "PreferDosLikeLineEnds" )
150cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "PrimaryKeySupport" )
151cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "RespectDriverResultSetType" )
152cdf0e10cSrcweir             )
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             ::rtl::OUString aStr;
155cdf0e10cSrcweir             OSL_VERIFY( pBegin->Value >>= aStr );
156cdf0e10cSrcweir             pProps->setProperty(pBegin->Name,aStr);
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir     return pProps;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir // --------------------------------------------------------------------------------
JavaString2String(JNIEnv * pEnv,jstring _Str)162cdf0e10cSrcweir ::rtl::OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring _Str)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     ::rtl::OUString aStr;
165cdf0e10cSrcweir     if(_Str)
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         jboolean bCopy(sal_True);
168cdf0e10cSrcweir         const jchar* pChar = pEnv->GetStringChars(_Str,&bCopy);
169cdf0e10cSrcweir         jsize len = pEnv->GetStringLength(_Str);
170cdf0e10cSrcweir         aStr = ::rtl::OUString(pChar,len);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         if(bCopy)
173cdf0e10cSrcweir             pEnv->ReleaseStringChars(_Str,pChar);
174cdf0e10cSrcweir         pEnv->DeleteLocalRef(_Str);
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir     return aStr;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir // --------------------------------------------------------------------------------
convertTypeMapToJavaMap(JNIEnv *,const Reference<::com::sun::star::container::XNameAccess> & _rMap)179cdf0e10cSrcweir jobject connectivity::convertTypeMapToJavaMap(JNIEnv* /*pEnv*/,const Reference< ::com::sun::star::container::XNameAccess > & _rMap)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     if ( _rMap.is() )
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames = _rMap->getElementNames();
184cdf0e10cSrcweir         if ( aNames.getLength() > 0 )
185cdf0e10cSrcweir             ::dbtools::throwFeatureNotImplementedException( "Type maps", NULL );
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir     return 0;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir // -----------------------------------------------------------------------------
isExceptionOccured(JNIEnv * pEnv,sal_Bool _bClear)190cdf0e10cSrcweir sal_Bool connectivity::isExceptionOccured(JNIEnv *pEnv,sal_Bool _bClear)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     if ( !pEnv )
193cdf0e10cSrcweir         return sal_False;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     jthrowable pThrowable = pEnv->ExceptionOccurred();
196cdf0e10cSrcweir     sal_Bool bRet = pThrowable != NULL;
197cdf0e10cSrcweir     if ( pThrowable )
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         if ( _bClear )
200cdf0e10cSrcweir             pEnv->ExceptionClear();
201cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
202cdf0e10cSrcweir         if(pEnv->IsInstanceOf(pThrowable,java_sql_SQLException_BASE::st_getMyClass()))
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir 
205cdf0e10cSrcweir             java_sql_SQLException_BASE* pException = new java_sql_SQLException_BASE(pEnv,pThrowable);
206cdf0e10cSrcweir             ::rtl::OUString sError = pException->getMessage();
207cdf0e10cSrcweir             delete pException;
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir #else
210cdf0e10cSrcweir         pEnv->DeleteLocalRef(pThrowable);
211cdf0e10cSrcweir #endif
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     return bRet;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir // -----------------------------------------------------------------------------
createByteInputStream(const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)218cdf0e10cSrcweir jobject connectivity::createByteInputStream(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     SDBThreadAttach t;
221cdf0e10cSrcweir     if( !t.pEnv || !x.is() )
222cdf0e10cSrcweir         return NULL;
223cdf0e10cSrcweir     // Java-Call fuer den Konstruktor absetzen
224cdf0e10cSrcweir     // temporaere Variable initialisieren
225cdf0e10cSrcweir     jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
226cdf0e10cSrcweir     static jmethodID mID(NULL);
227cdf0e10cSrcweir     if  ( !mID )
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         static const char * cSignature = "([B)V";
230cdf0e10cSrcweir         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
231cdf0e10cSrcweir         OSL_ENSURE( mID, cSignature );
232cdf0e10cSrcweir         if  ( !mID )
233cdf0e10cSrcweir             throw SQLException();
234cdf0e10cSrcweir     } // if  ( !_inout_MethodID )
235cdf0e10cSrcweir     jbyteArray pByteArray = t.pEnv->NewByteArray(length);
236cdf0e10cSrcweir     Sequence< sal_Int8 > aData;
237cdf0e10cSrcweir     x->readBytes(aData,length);
238cdf0e10cSrcweir     jboolean p = sal_False;
239cdf0e10cSrcweir     rtl_copyMemory(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
240cdf0e10cSrcweir     jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
241cdf0e10cSrcweir     t.pEnv->DeleteLocalRef((jbyteArray)pByteArray);
242cdf0e10cSrcweir     return out;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir // -----------------------------------------------------------------------------
createCharArrayReader(const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)245cdf0e10cSrcweir jobject connectivity::createCharArrayReader(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir     SDBThreadAttach t;
248cdf0e10cSrcweir     if( !t.pEnv || !x.is() )
249cdf0e10cSrcweir         return NULL;
250cdf0e10cSrcweir     // Java-Call fuer den Konstruktor absetzen
251cdf0e10cSrcweir     // temporaere Variable initialisieren
252cdf0e10cSrcweir     jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
253cdf0e10cSrcweir     static jmethodID mID(NULL);
254cdf0e10cSrcweir     if  ( !mID )
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         static const char * cSignature = "([C)V";
257cdf0e10cSrcweir         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
258cdf0e10cSrcweir         OSL_ENSURE( mID, cSignature );
259cdf0e10cSrcweir         if  ( !mID )
260cdf0e10cSrcweir             throw SQLException();
261cdf0e10cSrcweir     } // if  ( !_inout_MethodID )
262cdf0e10cSrcweir     jcharArray pCharArray = t.pEnv->NewCharArray(length);
263cdf0e10cSrcweir     Sequence< sal_Int8 > aData;
264cdf0e10cSrcweir     x->readBytes(aData,length);
265cdf0e10cSrcweir     jboolean p = sal_False;
266cdf0e10cSrcweir     rtl_copyMemory(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
267cdf0e10cSrcweir     jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
268cdf0e10cSrcweir     t.pEnv->DeleteLocalRef((jcharArray)pCharArray);
269cdf0e10cSrcweir     return out;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir // -----------------------------------------------------------------------------
272