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