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 #include "java/sql/CallableStatement.hxx"
27 #include "java/tools.hxx"
28 #include "java/sql/Array.hxx"
29 #include "java/sql/Clob.hxx"
30 #include "java/sql/Blob.hxx"
31 #include "java/sql/Connection.hxx"
32 #include "java/sql/Ref.hxx"
33 #include "java/sql/Timestamp.hxx"
34 #include <cppuhelper/typeprovider.hxx>
35 #include <comphelper/sequence.hxx>
36 
37 #include <string.h>
38 
39 using namespace connectivity;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 //	using namespace ::com::sun::star::sdbcx;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
46 
47 
48 IMPLEMENT_SERVICE_INFO(java_sql_CallableStatement,"com.sun.star.sdbcx.ACallableStatement","com.sun.star.sdbc.CallableStatement");
49 
50 //**************************************************************
51 //************ Class: java.sql.CallableStatement
52 //**************************************************************
java_sql_CallableStatement(JNIEnv * pEnv,java_sql_Connection & _rCon,const::rtl::OUString & sql)53 java_sql_CallableStatement::java_sql_CallableStatement( JNIEnv * pEnv, java_sql_Connection& _rCon,const ::rtl::OUString& sql )
54 	: java_sql_PreparedStatement( pEnv, _rCon, sql )
55 {
56 }
57 // -----------------------------------------------------------------------------
~java_sql_CallableStatement()58 java_sql_CallableStatement::~java_sql_CallableStatement()
59 {
60 }
61 // -----------------------------------------------------------------------------
62 
queryInterface(const Type & rType)63 Any SAL_CALL java_sql_CallableStatement::queryInterface( const Type & rType ) throw(RuntimeException)
64 {
65 	Any aRet = java_sql_PreparedStatement::queryInterface(rType);
66 	return aRet.hasValue() ? aRet : ::cppu::queryInterface(rType,static_cast< starsdbc::XRow*>(this),static_cast< starsdbc::XOutParameters*>(this));
67 }
68 // -------------------------------------------------------------------------
getTypes()69 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL java_sql_CallableStatement::getTypes(  ) throw(::com::sun::star::uno::RuntimeException)
70 {
71 	::cppu::OTypeCollection aTypes(	::getCppuType( (const ::com::sun::star::uno::Reference< starsdbc::XRow > *)0 ),
72 									::getCppuType( (const ::com::sun::star::uno::Reference< starsdbc::XOutParameters > *)0 ));
73 
74 	return ::comphelper::concatSequences(aTypes.getTypes(),java_sql_PreparedStatement::getTypes());
75 }
76 // -------------------------------------------------------------------------
wasNull()77 sal_Bool SAL_CALL java_sql_CallableStatement::wasNull(  ) throw(starsdbc::SQLException, RuntimeException)
78 {
79     static jmethodID mID(NULL);
80     return callBooleanMethod( "wasNull", mID );
81 }
82 
getBoolean(sal_Int32 columnIndex)83 sal_Bool SAL_CALL java_sql_CallableStatement::getBoolean( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
84 {
85     static jmethodID mID(NULL);
86     return callBooleanMethodWithIntArg( "getBoolean", mID,columnIndex );
87 }
getByte(sal_Int32 columnIndex)88 sal_Int8 SAL_CALL java_sql_CallableStatement::getByte( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
89 {
90     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
91     createStatement(t.pEnv);
92     static jmethodID mID(NULL);
93     jbyte (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) = &JNIEnv::CallByteMethod;
94     return callMethodWithIntArg<jbyte>(pCallMethod,"getByte","(I)B",mID,columnIndex);
95 }
getBytes(sal_Int32 columnIndex)96 Sequence< sal_Int8 > SAL_CALL java_sql_CallableStatement::getBytes( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
97 {
98     ::osl::MutexGuard aGuard( m_aMutex );
99 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
100 	Sequence< sal_Int8 > aSeq;
101 
102     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
103     createStatement(t.pEnv);
104     static jmethodID mID(NULL);
105     jbyteArray out = (jbyteArray)callObjectMethodWithIntArg(t.pEnv,"getBytes","(I)[B", mID, columnIndex);
106 	if (out)
107 	{
108 		jboolean p = sal_False;
109 		aSeq.realloc(t.pEnv->GetArrayLength(out));
110 		memcpy(aSeq.getArray(),t.pEnv->GetByteArrayElements(out,&p),aSeq.getLength());
111 		t.pEnv->DeleteLocalRef(out);
112 	}
113 	return aSeq;
114 }
getDate(sal_Int32 columnIndex)115 ::com::sun::star::util::Date SAL_CALL java_sql_CallableStatement::getDate( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
116 {
117     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
118     createStatement(t.pEnv);
119     static jmethodID mID(NULL);
120     jobject out = callObjectMethodWithIntArg(t.pEnv,"getDate","(I)Ljava/sql/Date;", mID, columnIndex);
121 	return out ? static_cast <com::sun::star::util::Date>(java_sql_Date( t.pEnv, out )) : ::com::sun::star::util::Date();
122 }
getDouble(sal_Int32 columnIndex)123 double SAL_CALL java_sql_CallableStatement::getDouble( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
124 {
125     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
126     createStatement(t.pEnv);
127     static jmethodID mID(NULL);
128     double (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) = &JNIEnv::CallDoubleMethod;
129     return callMethodWithIntArg<double>(pCallMethod,"getDouble","(I)D",mID,columnIndex);
130 }
131 
getFloat(sal_Int32 columnIndex)132 float SAL_CALL java_sql_CallableStatement::getFloat( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
133 {
134     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
135     createStatement(t.pEnv);
136     static jmethodID mID(NULL);
137     jfloat (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) = &JNIEnv::CallFloatMethod;
138     return callMethodWithIntArg<jfloat>(pCallMethod,"getFloat","(I)F",mID,columnIndex);
139 }
140 
getInt(sal_Int32 columnIndex)141 sal_Int32 SAL_CALL java_sql_CallableStatement::getInt( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
142 {
143     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
144     createStatement(t.pEnv);
145     static jmethodID mID(NULL);
146     return callIntMethodWithIntArg("getInt",mID,columnIndex);
147 }
148 
getLong(sal_Int32 columnIndex)149 sal_Int64 SAL_CALL java_sql_CallableStatement::getLong( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
150 {
151     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
152     createStatement(t.pEnv);
153     static jmethodID mID(NULL);
154     jlong (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) = &JNIEnv::CallLongMethod;
155     return callMethodWithIntArg<jlong>(pCallMethod,"getLong","(I)J",mID,columnIndex);
156 }
157 
getObject(sal_Int32 columnIndex,const Reference<::com::sun::star::container::XNameAccess> &)158 Any SAL_CALL java_sql_CallableStatement::getObject( sal_Int32 columnIndex, const Reference< ::com::sun::star::container::XNameAccess >& /*typeMap*/ ) throw(starsdbc::SQLException, RuntimeException)
159 {
160     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
161     createStatement(t.pEnv);
162     static jmethodID mID(NULL);
163     /*jobject out = */callObjectMethodWithIntArg(t.pEnv,"getObject","(I)Ljava/lang/Object;", mID, columnIndex);
164 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
165 	return Any(); //out==0 ? 0 : new java_lang_Object( t.pEnv, out );
166 }
167 
getShort(sal_Int32 columnIndex)168 sal_Int16 SAL_CALL java_sql_CallableStatement::getShort( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
169 {
170     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
171     createStatement(t.pEnv);
172     static jmethodID mID(NULL);
173     jshort (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) = &JNIEnv::CallShortMethod;
174     return callMethodWithIntArg<jshort>(pCallMethod,"getShort","(I)S",mID,columnIndex);
175 }
176 
getString(sal_Int32 columnIndex)177 ::rtl::OUString SAL_CALL java_sql_CallableStatement::getString( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
178 {
179     ::osl::MutexGuard aGuard( m_aMutex );
180 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
181     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
182 	createStatement(t.pEnv);
183     static jmethodID mID(NULL);
184     return callStringMethodWithIntArg("getString",mID,columnIndex);
185 }
186 
getTime(sal_Int32 columnIndex)187  ::com::sun::star::util::Time SAL_CALL java_sql_CallableStatement::getTime( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
188 {
189     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
190     createStatement(t.pEnv);
191     static jmethodID mID(NULL);
192     jobject out = callObjectMethodWithIntArg(t.pEnv,"getTime","(I)Ljava/sql/Time;", mID, columnIndex);
193 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
194 	return out ? static_cast <com::sun::star::util::Time> (java_sql_Time( t.pEnv, out )) : ::com::sun::star::util::Time();
195 }
196 
getTimestamp(sal_Int32 columnIndex)197  ::com::sun::star::util::DateTime SAL_CALL java_sql_CallableStatement::getTimestamp( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
198 {
199     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
200     createStatement(t.pEnv);
201     static jmethodID mID(NULL);
202     jobject out = callObjectMethodWithIntArg(t.pEnv,"getTimestamp","(I)Ljava/sql/Timestamp;", mID, columnIndex);
203 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
204 	return out ? static_cast <com::sun::star::util::DateTime> (java_sql_Timestamp( t.pEnv, out )) : ::com::sun::star::util::DateTime();
205 }
206 
registerOutParameter(sal_Int32 parameterIndex,sal_Int32 sqlType,const::rtl::OUString & typeName)207 void SAL_CALL java_sql_CallableStatement::registerOutParameter( sal_Int32 parameterIndex, sal_Int32 sqlType, const ::rtl::OUString& typeName ) throw(starsdbc::SQLException, RuntimeException)
208 {
209     ::osl::MutexGuard aGuard( m_aMutex );
210 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
211     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
212 
213 	{
214 		createStatement(t.pEnv);
215 
216 		// temporaere Variable initialisieren
217 		static const char * cSignature = "(IILjava/lang/String;)V";
218 		static const char * cMethodName = "registerOutParameter";
219 		// Java-Call absetzen
220 		static jmethodID mID(NULL);
221         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
222 		// Parameter konvertieren
223 		jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,typeName));
224 		t.pEnv->CallVoidMethod( object, mID, parameterIndex,sqlType,str.get());
225 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
226 	}
227 }
registerNumericOutParameter(sal_Int32 parameterIndex,sal_Int32 sqlType,sal_Int32 scale)228 void SAL_CALL java_sql_CallableStatement::registerNumericOutParameter( sal_Int32 parameterIndex, sal_Int32 sqlType, sal_Int32 scale ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
229 {
230     ::osl::MutexGuard aGuard( m_aMutex );
231 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
232     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
233 
234 	{
235 		createStatement(t.pEnv);
236 		// temporaere Variable initialisieren
237 		static const char * cSignature = "(III)V";
238 		static const char * cMethodName = "registerOutParameter";
239 		// Java-Call absetzen
240 		static jmethodID mID(NULL);
241         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
242 		t.pEnv->CallVoidMethod( object, mID, parameterIndex,sqlType,scale);
243 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
244 	}
245 }
246 
247 jclass java_sql_CallableStatement::theClass = 0;
248 
getMyClass() const249 jclass java_sql_CallableStatement::getMyClass() const
250 {
251 	// die Klasse muss nur einmal geholt werden, daher statisch
252 	if( !theClass )
253         theClass = findMyClass("java/sql/CallableStatement");
254 	return theClass;
255 }
256 
getBinaryStream(sal_Int32 columnIndex)257 Reference< ::com::sun::star::io::XInputStream > SAL_CALL java_sql_CallableStatement::getBinaryStream( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
258 {
259 	Reference< starsdbc::XBlob > xBlob = getBlob(columnIndex);
260 	return xBlob.is() ? xBlob->getBinaryStream() : Reference< ::com::sun::star::io::XInputStream >();
261 }
getCharacterStream(sal_Int32 columnIndex)262 Reference< ::com::sun::star::io::XInputStream > SAL_CALL java_sql_CallableStatement::getCharacterStream( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
263 {
264 	Reference< starsdbc::XClob > xClob = getClob(columnIndex);
265 	return xClob.is() ? xClob->getCharacterStream() : Reference< ::com::sun::star::io::XInputStream >();
266 }
267 
getArray(sal_Int32 columnIndex)268 Reference< starsdbc::XArray > SAL_CALL java_sql_CallableStatement::getArray( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
269 {
270     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
271     createStatement(t.pEnv);
272     static jmethodID mID(NULL);
273     jobject out = callObjectMethodWithIntArg(t.pEnv,"getArray","(I)Ljava/sql/Array;", mID, columnIndex);
274 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
275 	return out==0 ? 0 : new java_sql_Array( t.pEnv, out );
276 }
277 
getClob(sal_Int32 columnIndex)278 Reference< starsdbc::XClob > SAL_CALL java_sql_CallableStatement::getClob( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
279 {
280     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
281     createStatement(t.pEnv);
282     static jmethodID mID(NULL);
283     jobject out = callObjectMethodWithIntArg(t.pEnv,"getClob","(I)Ljava/sql/Clob;", mID, columnIndex);
284 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
285 	return out==0 ? 0 : new java_sql_Clob( t.pEnv, out );
286 }
getBlob(sal_Int32 columnIndex)287 Reference< starsdbc::XBlob > SAL_CALL java_sql_CallableStatement::getBlob( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
288 {
289     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
290     createStatement(t.pEnv);
291     static jmethodID mID(NULL);
292     jobject out = callObjectMethodWithIntArg(t.pEnv,"getBlob","(I)Ljava/sql/Blob;", mID, columnIndex);
293 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
294 	return out==0 ? 0 : new java_sql_Blob( t.pEnv, out );
295 }
296 
getRef(sal_Int32 columnIndex)297 Reference< starsdbc::XRef > SAL_CALL java_sql_CallableStatement::getRef( sal_Int32 columnIndex ) throw(starsdbc::SQLException, RuntimeException)
298 {
299     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
300     createStatement(t.pEnv);
301     static jmethodID mID(NULL);
302     jobject out = callObjectMethodWithIntArg(t.pEnv,"getRef","(I)Ljava/sql/Ref;", mID, columnIndex);
303 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
304 	return out==0 ? 0 : new java_sql_Ref( t.pEnv, out );
305 }
306 // -----------------------------------------------------------------------------
acquire()307 void SAL_CALL java_sql_CallableStatement::acquire() throw()
308 {
309 	java_sql_PreparedStatement::acquire();
310 }
311 // -----------------------------------------------------------------------------
release()312 void SAL_CALL java_sql_CallableStatement::release() throw()
313 {
314 	java_sql_PreparedStatement::release();
315 }
316 // -----------------------------------------------------------------------------
createStatement(JNIEnv *)317 void java_sql_CallableStatement::createStatement(JNIEnv* /*_pEnv*/)
318 {
319 	::osl::MutexGuard aGuard( m_aMutex );
320 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
321 
322 
323     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
324 	if( t.pEnv && !object ){
325 		// temporaere Variable initialisieren
326 		static const char * cSignature = "(Ljava/lang/String;II)Ljava/sql/CallableStatement;";
327 		static const char * cMethodName = "prepareCall";
328 		// Java-Call absetzen
329 		jobject out = NULL;
330 		// Parameter konvertieren
331 		jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,m_sSqlStatement));
332 
333 		static jmethodID mID(NULL);
334 		if ( !mID  )
335 			mID  = t.pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature );
336 		if( mID ){
337 			out = t.pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID, str.get() ,m_nResultSetType,m_nResultSetConcurrency);
338 		} //mID
339 		else
340 		{
341 			static const char * cSignature2 = "(Ljava/lang/String;)Ljava/sql/CallableStatement;";
342 			static jmethodID mID2 = t.pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature2 );OSL_ENSURE(mID2,"Unknown method id!");
343 			if( mID2 ){
344 				out = t.pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID2, str.get() );
345 			} //mID
346 		}
347 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
348 
349 		if ( out )
350 			object = t.pEnv->NewGlobalRef( out );
351 	} //t.pEnv
352 }
353 // -----------------------------------------------------------------------------
354 
355 
356