1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CallableStatement.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "java/sql/CallableStatement.hxx"
34 #include "java/tools.hxx"
35 #include "java/sql/Array.hxx"
36 #include "java/sql/Clob.hxx"
37 #include "java/sql/Blob.hxx"
38 #include "java/sql/Connection.hxx"
39 #include "java/sql/Ref.hxx"
40 #include "java/sql/Timestamp.hxx"
41 #include <cppuhelper/typeprovider.hxx>
42 #include <comphelper/sequence.hxx>
46 using namespace connectivity
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::beans
;
49 // using namespace ::com::sun::star::sdbcx;
50 using namespace ::com::sun::star::sdbc
;
51 using namespace ::com::sun::star::container
;
52 using namespace ::com::sun::star::lang
;
55 IMPLEMENT_SERVICE_INFO(java_sql_CallableStatement
,"com.sun.star.sdbcx.ACallableStatement","com.sun.star.sdbc.CallableStatement");
57 //**************************************************************
58 //************ Class: java.sql.CallableStatement
59 //**************************************************************
60 java_sql_CallableStatement::java_sql_CallableStatement( JNIEnv
* pEnv
, java_sql_Connection
& _rCon
,const ::rtl::OUString
& sql
)
61 : java_sql_PreparedStatement( pEnv
, _rCon
, sql
)
64 // -----------------------------------------------------------------------------
65 java_sql_CallableStatement::~java_sql_CallableStatement()
68 // -----------------------------------------------------------------------------
70 Any SAL_CALL
java_sql_CallableStatement::queryInterface( const Type
& rType
) throw(RuntimeException
)
72 Any aRet
= java_sql_PreparedStatement::queryInterface(rType
);
73 return aRet
.hasValue() ? aRet
: ::cppu::queryInterface(rType
,static_cast< starsdbc::XRow
*>(this),static_cast< starsdbc::XOutParameters
*>(this));
75 // -------------------------------------------------------------------------
76 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> SAL_CALL
java_sql_CallableStatement::getTypes( ) throw(::com::sun::star::uno::RuntimeException
)
78 ::cppu::OTypeCollection
aTypes( ::getCppuType( (const ::com::sun::star::uno::Reference
< starsdbc::XRow
> *)0 ),
79 ::getCppuType( (const ::com::sun::star::uno::Reference
< starsdbc::XOutParameters
> *)0 ));
81 return ::comphelper::concatSequences(aTypes
.getTypes(),java_sql_PreparedStatement::getTypes());
83 // -------------------------------------------------------------------------
84 sal_Bool SAL_CALL
java_sql_CallableStatement::wasNull( ) throw(starsdbc::SQLException
, RuntimeException
)
86 static jmethodID
mID(NULL
);
87 return callBooleanMethod( "wasNull", mID
);
90 sal_Bool SAL_CALL
java_sql_CallableStatement::getBoolean( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
92 static jmethodID
mID(NULL
);
93 return callBooleanMethodWithIntArg( "getBoolean", mID
,columnIndex
);
95 sal_Int8 SAL_CALL
java_sql_CallableStatement::getByte( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
97 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
98 createStatement(t
.pEnv
);
99 static jmethodID
mID(NULL
);
100 jbyte (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallByteMethod
;
101 return callMethodWithIntArg
<jbyte
>(pCallMethod
,"getByte","(I)B",mID
,columnIndex
);
103 Sequence
< sal_Int8
> SAL_CALL
java_sql_CallableStatement::getBytes( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
105 ::osl::MutexGuard
aGuard( m_aMutex
);
106 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
107 Sequence
< sal_Int8
> aSeq
;
109 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
110 createStatement(t
.pEnv
);
111 static jmethodID
mID(NULL
);
112 jbyteArray out
= (jbyteArray
)callObjectMethodWithIntArg(t
.pEnv
,"getBytes","(I)[B", mID
, columnIndex
);
115 jboolean p
= sal_False
;
116 aSeq
.realloc(t
.pEnv
->GetArrayLength(out
));
117 memcpy(aSeq
.getArray(),t
.pEnv
->GetByteArrayElements(out
,&p
),aSeq
.getLength());
118 t
.pEnv
->DeleteLocalRef(out
);
122 ::com::sun::star::util::Date SAL_CALL
java_sql_CallableStatement::getDate( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
124 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
125 createStatement(t
.pEnv
);
126 static jmethodID
mID(NULL
);
127 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getDate","(I)Ljava/sql/Date;", mID
, columnIndex
);
128 return out
? static_cast <com::sun::star::util::Date
>(java_sql_Date( t
.pEnv
, out
)) : ::com::sun::star::util::Date();
130 double SAL_CALL
java_sql_CallableStatement::getDouble( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
132 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
133 createStatement(t
.pEnv
);
134 static jmethodID
mID(NULL
);
135 double (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallDoubleMethod
;
136 return callMethodWithIntArg
<double>(pCallMethod
,"getDouble","(I)D",mID
,columnIndex
);
139 float SAL_CALL
java_sql_CallableStatement::getFloat( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
141 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
142 createStatement(t
.pEnv
);
143 static jmethodID
mID(NULL
);
144 jfloat (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallFloatMethod
;
145 return callMethodWithIntArg
<jfloat
>(pCallMethod
,"getFloat","(I)F",mID
,columnIndex
);
148 sal_Int32 SAL_CALL
java_sql_CallableStatement::getInt( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
150 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
151 createStatement(t
.pEnv
);
152 static jmethodID
mID(NULL
);
153 return callIntMethodWithIntArg("getInt",mID
,columnIndex
);
156 sal_Int64 SAL_CALL
java_sql_CallableStatement::getLong( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
158 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
159 createStatement(t
.pEnv
);
160 static jmethodID
mID(NULL
);
161 jlong (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallLongMethod
;
162 return callMethodWithIntArg
<jlong
>(pCallMethod
,"getLong","(I)J",mID
,columnIndex
);
165 Any SAL_CALL
java_sql_CallableStatement::getObject( sal_Int32 columnIndex
, const Reference
< ::com::sun::star::container::XNameAccess
>& /*typeMap*/ ) throw(starsdbc::SQLException
, RuntimeException
)
167 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
168 createStatement(t
.pEnv
);
169 static jmethodID
mID(NULL
);
170 /*jobject out = */callObjectMethodWithIntArg(t
.pEnv
,"getObject","(I)Ljava/lang/Object;", mID
, columnIndex
);
171 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
172 return Any(); //out==0 ? 0 : new java_lang_Object( t.pEnv, out );
175 sal_Int16 SAL_CALL
java_sql_CallableStatement::getShort( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
177 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
178 createStatement(t
.pEnv
);
179 static jmethodID
mID(NULL
);
180 jshort (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallShortMethod
;
181 return callMethodWithIntArg
<jshort
>(pCallMethod
,"getShort","(I)S",mID
,columnIndex
);
184 ::rtl::OUString SAL_CALL
java_sql_CallableStatement::getString( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
186 ::osl::MutexGuard
aGuard( m_aMutex
);
187 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
188 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
189 createStatement(t
.pEnv
);
190 static jmethodID
mID(NULL
);
191 return callStringMethodWithIntArg("getString",mID
,columnIndex
);
194 ::com::sun::star::util::Time SAL_CALL
java_sql_CallableStatement::getTime( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
196 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
197 createStatement(t
.pEnv
);
198 static jmethodID
mID(NULL
);
199 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTime","(I)Ljava/sql/Time;", mID
, columnIndex
);
200 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
201 return out
? static_cast <com::sun::star::util::Time
> (java_sql_Time( t
.pEnv
, out
)) : ::com::sun::star::util::Time();
204 ::com::sun::star::util::DateTime SAL_CALL
java_sql_CallableStatement::getTimestamp( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
206 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
207 createStatement(t
.pEnv
);
208 static jmethodID
mID(NULL
);
209 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTimestamp","(I)Ljava/sql/Timestamp;", mID
, columnIndex
);
210 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
211 return out
? static_cast <com::sun::star::util::DateTime
> (java_sql_Timestamp( t
.pEnv
, out
)) : ::com::sun::star::util::DateTime();
214 void SAL_CALL
java_sql_CallableStatement::registerOutParameter( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const ::rtl::OUString
& typeName
) throw(starsdbc::SQLException
, RuntimeException
)
216 ::osl::MutexGuard
aGuard( m_aMutex
);
217 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
218 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
221 createStatement(t
.pEnv
);
223 // temporaere Variable initialisieren
224 static const char * cSignature
= "(IILjava/lang/String;)V";
225 static const char * cMethodName
= "registerOutParameter";
226 // Java-Call absetzen
227 static jmethodID
mID(NULL
);
228 obtainMethodId(t
.pEnv
, cMethodName
,cSignature
, mID
);
229 // Parameter konvertieren
230 jstring str
= convertwchar_tToJavaString(t
.pEnv
,typeName
);
231 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,str
);
232 t
.pEnv
->DeleteLocalRef(str
);
233 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
236 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
)
238 ::osl::MutexGuard
aGuard( m_aMutex
);
239 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
240 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
243 createStatement(t
.pEnv
);
244 // temporaere Variable initialisieren
245 static const char * cSignature
= "(III)V";
246 static const char * cMethodName
= "registerOutParameter";
247 // Java-Call absetzen
248 static jmethodID
mID(NULL
);
249 obtainMethodId(t
.pEnv
, cMethodName
,cSignature
, mID
);
250 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,scale
);
251 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
255 jclass
java_sql_CallableStatement::theClass
= 0;
257 jclass
java_sql_CallableStatement::getMyClass() const
259 // die Klasse muss nur einmal geholt werden, daher statisch
261 theClass
= findMyClass("java/sql/CallableStatement");
265 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getBinaryStream( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
267 Reference
< starsdbc::XBlob
> xBlob
= getBlob(columnIndex
);
268 return xBlob
.is() ? xBlob
->getBinaryStream() : Reference
< ::com::sun::star::io::XInputStream
>();
270 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getCharacterStream( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
272 Reference
< starsdbc::XClob
> xClob
= getClob(columnIndex
);
273 return xClob
.is() ? xClob
->getCharacterStream() : Reference
< ::com::sun::star::io::XInputStream
>();
276 Reference
< starsdbc::XArray
> SAL_CALL
java_sql_CallableStatement::getArray( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
278 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
279 createStatement(t
.pEnv
);
280 static jmethodID
mID(NULL
);
281 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getArray","(I)Ljava/sql/Array;", mID
, columnIndex
);
282 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
283 return out
==0 ? 0 : new java_sql_Array( t
.pEnv
, out
);
286 Reference
< starsdbc::XClob
> SAL_CALL
java_sql_CallableStatement::getClob( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
288 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
289 createStatement(t
.pEnv
);
290 static jmethodID
mID(NULL
);
291 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getClob","(I)Ljava/sql/Clob;", mID
, columnIndex
);
292 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
293 return out
==0 ? 0 : new java_sql_Clob( t
.pEnv
, out
);
295 Reference
< starsdbc::XBlob
> SAL_CALL
java_sql_CallableStatement::getBlob( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
297 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
298 createStatement(t
.pEnv
);
299 static jmethodID
mID(NULL
);
300 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getBlob","(I)Ljava/sql/Blob;", mID
, columnIndex
);
301 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
302 return out
==0 ? 0 : new java_sql_Blob( t
.pEnv
, out
);
305 Reference
< starsdbc::XRef
> SAL_CALL
java_sql_CallableStatement::getRef( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
307 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
308 createStatement(t
.pEnv
);
309 static jmethodID
mID(NULL
);
310 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getRef","(I)Ljava/sql/Ref;", mID
, columnIndex
);
311 // ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
312 return out
==0 ? 0 : new java_sql_Ref( t
.pEnv
, out
);
314 // -----------------------------------------------------------------------------
315 void SAL_CALL
java_sql_CallableStatement::acquire() throw()
317 java_sql_PreparedStatement::acquire();
319 // -----------------------------------------------------------------------------
320 void SAL_CALL
java_sql_CallableStatement::release() throw()
322 java_sql_PreparedStatement::release();
324 // -----------------------------------------------------------------------------
325 void java_sql_CallableStatement::createStatement(JNIEnv
* /*_pEnv*/)
327 ::osl::MutexGuard
aGuard( m_aMutex
);
328 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
331 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
332 if( t
.pEnv
&& !object
){
333 // temporaere Variable initialisieren
334 static const char * cSignature
= "(Ljava/lang/String;II)Ljava/sql/CallableStatement;";
335 static const char * cMethodName
= "prepareCall";
336 // Java-Call absetzen
338 // Parameter konvertieren
339 jstring str
= convertwchar_tToJavaString(t
.pEnv
,m_sSqlStatement
);
341 static jmethodID
mID(NULL
);
343 mID
= t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature
);
345 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID
, str
,m_nResultSetType
,m_nResultSetConcurrency
);
349 static const char * cSignature2
= "(Ljava/lang/String;)Ljava/sql/CallableStatement;";
350 static jmethodID mID2
= t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature2
);OSL_ENSURE(mID2
,"Unknown method id!");
352 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID2
, str
);
355 t
.pEnv
->DeleteLocalRef(str
);
356 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
359 object
= t
.pEnv
->NewGlobalRef( out
);
362 // -----------------------------------------------------------------------------