1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cstdarg>
32*cdf0e10cSrcweir #include "java/tools.hxx"
33*cdf0e10cSrcweir #include "java/lang/String.hxx"
34*cdf0e10cSrcweir #include "java/lang/Class.hxx"
35*cdf0e10cSrcweir #include "java/util/Property.hxx"
36*cdf0e10cSrcweir #include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
38*cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace connectivity;
41*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
43*cdf0e10cSrcweir //	using namespace ::com::sun::star::sdbcx;
44*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
45*cdf0e10cSrcweir using namespace ::com::sun::star::container;
46*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir void java_util_Properties::setProperty(const ::rtl::OUString key, const ::rtl::OUString& value)
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
51*cdf0e10cSrcweir 	jobject out(0);
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	{
54*cdf0e10cSrcweir 		jvalue args[2];
55*cdf0e10cSrcweir 		// Parameter konvertieren
56*cdf0e10cSrcweir 		args[0].l = convertwchar_tToJavaString(t.pEnv,key);
57*cdf0e10cSrcweir 		args[1].l = convertwchar_tToJavaString(t.pEnv,value);
58*cdf0e10cSrcweir 		// temporaere Variable initialisieren
59*cdf0e10cSrcweir 		static const char * cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
60*cdf0e10cSrcweir 		static const char * cMethodName = "setProperty";
61*cdf0e10cSrcweir 		// Java-Call absetzen
62*cdf0e10cSrcweir 		static jmethodID mID(NULL);
63*cdf0e10cSrcweir         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
64*cdf0e10cSrcweir 		out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
65*cdf0e10cSrcweir 		ThrowSQLException(t.pEnv,NULL);
66*cdf0e10cSrcweir 		t.pEnv->DeleteLocalRef((jstring)args[1].l);
67*cdf0e10cSrcweir 		t.pEnv->DeleteLocalRef((jstring)args[0].l);
68*cdf0e10cSrcweir 		ThrowSQLException(t.pEnv,0);
69*cdf0e10cSrcweir 		if(out)
70*cdf0e10cSrcweir 			t.pEnv->DeleteLocalRef(out);
71*cdf0e10cSrcweir 	} //t.pEnv
72*cdf0e10cSrcweir 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir jclass java_util_Properties::theClass = 0;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir java_util_Properties::~java_util_Properties()
77*cdf0e10cSrcweir {}
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir jclass java_util_Properties::getMyClass() const
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	// die Klasse muss nur einmal geholt werden, daher statisch
82*cdf0e10cSrcweir 	if( !theClass )
83*cdf0e10cSrcweir         theClass = findMyClass("java/util/Properties");
84*cdf0e10cSrcweir 	return theClass;
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir //--------------------------------------------------------------------------
88*cdf0e10cSrcweir java_util_Properties::java_util_Properties( ): java_lang_Object( NULL, (jobject)NULL )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	SDBThreadAttach t;
91*cdf0e10cSrcweir 	if( !t.pEnv )
92*cdf0e10cSrcweir 		return;
93*cdf0e10cSrcweir 	// Java-Call fuer den Konstruktor absetzen
94*cdf0e10cSrcweir 	// temporaere Variable initialisieren
95*cdf0e10cSrcweir 	static const char * cSignature = "()V";
96*cdf0e10cSrcweir 	jobject tempObj;
97*cdf0e10cSrcweir 	static jmethodID mID(NULL);
98*cdf0e10cSrcweir     obtainMethodId(t.pEnv, "<init>",cSignature, mID);
99*cdf0e10cSrcweir 	tempObj = t.pEnv->NewObject( getMyClass(), mID);
100*cdf0e10cSrcweir 	saveRef( t.pEnv, tempObj );
101*cdf0e10cSrcweir 	t.pEnv->DeleteLocalRef( tempObj );
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir // --------------------------------------------------------------------------------
105*cdf0e10cSrcweir jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const ::rtl::OUString& _rTemp)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	OSL_ENSURE(pEnv,"Environment is NULL!");
108*cdf0e10cSrcweir 	jstring pStr = pEnv->NewString(_rTemp.getStr(), _rTemp.getLength());
109*cdf0e10cSrcweir 	pEnv->ExceptionClear();
110*cdf0e10cSrcweir 	OSL_ENSURE(pStr,"Could not create a jsstring object!");
111*cdf0e10cSrcweir 	return pStr;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // --------------------------------------------------------------------------------
115*cdf0e10cSrcweir java_util_Properties* connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info )  throw(SQLException, RuntimeException)
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir 	java_util_Properties* pProps = new java_util_Properties();
118*cdf0e10cSrcweir 	const PropertyValue* pBegin	= info.getConstArray();
119*cdf0e10cSrcweir 	const PropertyValue* pEnd	= pBegin + info.getLength();
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	for(;pBegin != pEnd;++pBegin)
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		// this is a special property to find the jdbc driver
124*cdf0e10cSrcweir 		if  (   pBegin->Name.compareToAscii( "JavaDriverClass" )
125*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "JavaDriverClassPath" )
126*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "SystemProperties" )
127*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "CharSet" )
128*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "AppendTableAliasName" )
129*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "AddIndexAppendix" )
130*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "FormsCheckRequiredFields" )
131*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "GenerateASBeforeCorrelationName" )
132*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "EscapeDateTime" )
133*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "ParameterNameSubstitution" )
134*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "IsPasswordRequired" )
135*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "IsAutoRetrievingEnabled" )
136*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "AutoRetrievingStatement" )
137*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "UseCatalogInSelect" )
138*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "UseSchemaInSelect" )
139*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "AutoIncrementCreation" )
140*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "Extension" )
141*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "NoNameLengthLimit" )
142*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "EnableSQL92Check" )
143*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "EnableOuterJoinEscape" )
144*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "BooleanComparisonMode" )
145*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "IgnoreCurrency" )
146*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "TypeInfoSettings" )
147*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "IgnoreDriverPrivileges" )
148*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "ImplicitCatalogRestriction" )
149*cdf0e10cSrcweir 			&&  pBegin->Name.compareToAscii( "ImplicitSchemaRestriction" )
150*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "SupportsTableCreation" )
151*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "UseJava" )
152*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "Authentication" )
153*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "PreferDosLikeLineEnds" )
154*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "PrimaryKeySupport" )
155*cdf0e10cSrcweir             &&  pBegin->Name.compareToAscii( "RespectDriverResultSetType" )
156*cdf0e10cSrcweir             )
157*cdf0e10cSrcweir 		{
158*cdf0e10cSrcweir 			::rtl::OUString aStr;
159*cdf0e10cSrcweir 			OSL_VERIFY( pBegin->Value >>= aStr );
160*cdf0e10cSrcweir 			pProps->setProperty(pBegin->Name,aStr);
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 	}
163*cdf0e10cSrcweir 	return pProps;
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir // --------------------------------------------------------------------------------
166*cdf0e10cSrcweir ::rtl::OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring _Str)
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	::rtl::OUString aStr;
169*cdf0e10cSrcweir 	if(_Str)
170*cdf0e10cSrcweir 	{
171*cdf0e10cSrcweir 		jboolean bCopy(sal_True);
172*cdf0e10cSrcweir 		const jchar* pChar = pEnv->GetStringChars(_Str,&bCopy);
173*cdf0e10cSrcweir 		jsize len = pEnv->GetStringLength(_Str);
174*cdf0e10cSrcweir 		aStr = ::rtl::OUString(pChar,len);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		if(bCopy)
177*cdf0e10cSrcweir 			pEnv->ReleaseStringChars(_Str,pChar);
178*cdf0e10cSrcweir 		pEnv->DeleteLocalRef(_Str);
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 	return aStr;
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir // --------------------------------------------------------------------------------
183*cdf0e10cSrcweir jobject connectivity::convertTypeMapToJavaMap(JNIEnv* /*pEnv*/,const Reference< ::com::sun::star::container::XNameAccess > & _rMap)
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     if ( _rMap.is() )
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir 	    ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames = _rMap->getElementNames();
188*cdf0e10cSrcweir         if ( aNames.getLength() > 0 )
189*cdf0e10cSrcweir             ::dbtools::throwFeatureNotImplementedException( "Type maps", NULL );
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 	return 0;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir // -----------------------------------------------------------------------------
194*cdf0e10cSrcweir sal_Bool connectivity::isExceptionOccured(JNIEnv *pEnv,sal_Bool _bClear)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir 	if ( !pEnv )
197*cdf0e10cSrcweir 		return sal_False;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	jthrowable pThrowable = pEnv->ExceptionOccurred();
200*cdf0e10cSrcweir 	sal_Bool bRet = pThrowable != NULL;
201*cdf0e10cSrcweir 	if ( pThrowable )
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir 		if ( _bClear )
204*cdf0e10cSrcweir 			pEnv->ExceptionClear();
205*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
206*cdf0e10cSrcweir 		if(pEnv->IsInstanceOf(pThrowable,java_sql_SQLException_BASE::st_getMyClass()))
207*cdf0e10cSrcweir 		{
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 			java_sql_SQLException_BASE* pException = new java_sql_SQLException_BASE(pEnv,pThrowable);
210*cdf0e10cSrcweir 			::rtl::OUString sError = pException->getMessage();
211*cdf0e10cSrcweir 			delete pException;
212*cdf0e10cSrcweir 		}
213*cdf0e10cSrcweir #else
214*cdf0e10cSrcweir 		pEnv->DeleteLocalRef(pThrowable);
215*cdf0e10cSrcweir #endif
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	return bRet;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir // -----------------------------------------------------------------------------
222*cdf0e10cSrcweir jobject connectivity::createByteInputStream(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     SDBThreadAttach t;
225*cdf0e10cSrcweir 	if( !t.pEnv || !x.is() )
226*cdf0e10cSrcweir 		return NULL;
227*cdf0e10cSrcweir 	// Java-Call fuer den Konstruktor absetzen
228*cdf0e10cSrcweir 	// temporaere Variable initialisieren
229*cdf0e10cSrcweir     jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
230*cdf0e10cSrcweir 	static jmethodID mID(NULL);
231*cdf0e10cSrcweir     if  ( !mID )
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir         static const char * cSignature = "([B)V";
234*cdf0e10cSrcweir         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
235*cdf0e10cSrcweir         OSL_ENSURE( mID, cSignature );
236*cdf0e10cSrcweir         if  ( !mID )
237*cdf0e10cSrcweir             throw SQLException();
238*cdf0e10cSrcweir     } // if  ( !_inout_MethodID )
239*cdf0e10cSrcweir     jbyteArray pByteArray = t.pEnv->NewByteArray(length);
240*cdf0e10cSrcweir     Sequence< sal_Int8 > aData;
241*cdf0e10cSrcweir     x->readBytes(aData,length);
242*cdf0e10cSrcweir     jboolean p = sal_False;
243*cdf0e10cSrcweir     rtl_copyMemory(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
244*cdf0e10cSrcweir 	jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
245*cdf0e10cSrcweir     t.pEnv->DeleteLocalRef((jbyteArray)pByteArray);
246*cdf0e10cSrcweir     return out;
247*cdf0e10cSrcweir }
248*cdf0e10cSrcweir // -----------------------------------------------------------------------------
249*cdf0e10cSrcweir jobject connectivity::createCharArrayReader(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     SDBThreadAttach t;
252*cdf0e10cSrcweir 	if( !t.pEnv || !x.is() )
253*cdf0e10cSrcweir 		return NULL;
254*cdf0e10cSrcweir 	// Java-Call fuer den Konstruktor absetzen
255*cdf0e10cSrcweir 	// temporaere Variable initialisieren
256*cdf0e10cSrcweir     jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
257*cdf0e10cSrcweir 	static jmethodID mID(NULL);
258*cdf0e10cSrcweir     if  ( !mID )
259*cdf0e10cSrcweir     {
260*cdf0e10cSrcweir         static const char * cSignature = "([C)V";
261*cdf0e10cSrcweir         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
262*cdf0e10cSrcweir         OSL_ENSURE( mID, cSignature );
263*cdf0e10cSrcweir         if  ( !mID )
264*cdf0e10cSrcweir             throw SQLException();
265*cdf0e10cSrcweir     } // if  ( !_inout_MethodID )
266*cdf0e10cSrcweir     jcharArray pCharArray = t.pEnv->NewCharArray(length);
267*cdf0e10cSrcweir     Sequence< sal_Int8 > aData;
268*cdf0e10cSrcweir     x->readBytes(aData,length);
269*cdf0e10cSrcweir     jboolean p = sal_False;
270*cdf0e10cSrcweir     rtl_copyMemory(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
271*cdf0e10cSrcweir 	jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
272*cdf0e10cSrcweir     t.pEnv->DeleteLocalRef((jcharArray)pCharArray);
273*cdf0e10cSrcweir     return out;
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir // -----------------------------------------------------------------------------
276