1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "java/sql/CallableStatement.hxx"
30 #include "java/tools.hxx"
31 #include "java/sql/Array.hxx"
32 #include "java/sql/Clob.hxx"
33 #include "java/sql/Blob.hxx"
34 #include "java/sql/Connection.hxx"
35 #include "java/sql/Ref.hxx"
36 #include "java/sql/Timestamp.hxx"
37 #include <cppuhelper/typeprovider.hxx>
38 #include <comphelper/sequence.hxx>
42 using namespace connectivity
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::sdbc
;
46 using namespace ::com::sun::star::container
;
47 using namespace ::com::sun::star::lang
;
50 IMPLEMENT_SERVICE_INFO(java_sql_CallableStatement
,"com.sun.star.sdbcx.ACallableStatement","com.sun.star.sdbc.CallableStatement");
52 //**************************************************************
53 //************ Class: java.sql.CallableStatement
54 //**************************************************************
55 java_sql_CallableStatement::java_sql_CallableStatement( JNIEnv
* pEnv
, java_sql_Connection
& _rCon
,const ::rtl::OUString
& sql
)
56 : java_sql_PreparedStatement( pEnv
, _rCon
, sql
)
59 // -----------------------------------------------------------------------------
60 java_sql_CallableStatement::~java_sql_CallableStatement()
63 // -----------------------------------------------------------------------------
65 Any SAL_CALL
java_sql_CallableStatement::queryInterface( const Type
& rType
) throw(RuntimeException
)
67 Any aRet
= java_sql_PreparedStatement::queryInterface(rType
);
68 return aRet
.hasValue() ? aRet
: ::cppu::queryInterface(rType
,static_cast< starsdbc::XRow
*>(this),static_cast< starsdbc::XOutParameters
*>(this));
70 // -------------------------------------------------------------------------
71 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> SAL_CALL
java_sql_CallableStatement::getTypes( ) throw(::com::sun::star::uno::RuntimeException
)
73 ::cppu::OTypeCollection
aTypes( ::getCppuType( (const ::com::sun::star::uno::Reference
< starsdbc::XRow
> *)0 ),
74 ::getCppuType( (const ::com::sun::star::uno::Reference
< starsdbc::XOutParameters
> *)0 ));
76 return ::comphelper::concatSequences(aTypes
.getTypes(),java_sql_PreparedStatement::getTypes());
78 // -------------------------------------------------------------------------
79 sal_Bool SAL_CALL
java_sql_CallableStatement::wasNull( ) throw(starsdbc::SQLException
, RuntimeException
)
81 static jmethodID
mID(NULL
);
82 return callBooleanMethod( "wasNull", mID
);
85 sal_Bool SAL_CALL
java_sql_CallableStatement::getBoolean( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
87 static jmethodID
mID(NULL
);
88 return callBooleanMethodWithIntArg( "getBoolean", mID
,columnIndex
);
90 sal_Int8 SAL_CALL
java_sql_CallableStatement::getByte( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
92 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
93 createStatement(t
.pEnv
);
94 static jmethodID
mID(NULL
);
95 jbyte (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallByteMethod
;
96 return callMethodWithIntArg
<jbyte
>(pCallMethod
,"getByte","(I)B",mID
,columnIndex
);
98 Sequence
< sal_Int8
> SAL_CALL
java_sql_CallableStatement::getBytes( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
100 ::osl::MutexGuard
aGuard( m_aMutex
);
101 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
102 Sequence
< sal_Int8
> aSeq
;
104 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
105 createStatement(t
.pEnv
);
106 static jmethodID
mID(NULL
);
107 jbyteArray out
= (jbyteArray
)callObjectMethodWithIntArg(t
.pEnv
,"getBytes","(I)[B", mID
, columnIndex
);
110 jboolean p
= sal_False
;
111 aSeq
.realloc(t
.pEnv
->GetArrayLength(out
));
112 memcpy(aSeq
.getArray(),t
.pEnv
->GetByteArrayElements(out
,&p
),aSeq
.getLength());
113 t
.pEnv
->DeleteLocalRef(out
);
117 ::com::sun::star::util::Date SAL_CALL
java_sql_CallableStatement::getDate( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
119 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
120 createStatement(t
.pEnv
);
121 static jmethodID
mID(NULL
);
122 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getDate","(I)Ljava/sql/Date;", mID
, columnIndex
);
123 return out
? static_cast <com::sun::star::util::Date
>(java_sql_Date( t
.pEnv
, out
)) : ::com::sun::star::util::Date();
125 double SAL_CALL
java_sql_CallableStatement::getDouble( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
127 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
128 createStatement(t
.pEnv
);
129 static jmethodID
mID(NULL
);
130 double (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallDoubleMethod
;
131 return callMethodWithIntArg
<double>(pCallMethod
,"getDouble","(I)D",mID
,columnIndex
);
134 float SAL_CALL
java_sql_CallableStatement::getFloat( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
136 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
137 createStatement(t
.pEnv
);
138 static jmethodID
mID(NULL
);
139 jfloat (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallFloatMethod
;
140 return callMethodWithIntArg
<jfloat
>(pCallMethod
,"getFloat","(I)F",mID
,columnIndex
);
143 sal_Int32 SAL_CALL
java_sql_CallableStatement::getInt( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
145 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
146 createStatement(t
.pEnv
);
147 static jmethodID
mID(NULL
);
148 return callIntMethodWithIntArg("getInt",mID
,columnIndex
);
151 sal_Int64 SAL_CALL
java_sql_CallableStatement::getLong( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
153 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
154 createStatement(t
.pEnv
);
155 static jmethodID
mID(NULL
);
156 jlong (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallLongMethod
;
157 return callMethodWithIntArg
<jlong
>(pCallMethod
,"getLong","(I)J",mID
,columnIndex
);
160 Any SAL_CALL
java_sql_CallableStatement::getObject( sal_Int32 columnIndex
, const Reference
< ::com::sun::star::container::XNameAccess
>& /*typeMap*/ ) throw(starsdbc::SQLException
, RuntimeException
)
162 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
163 createStatement(t
.pEnv
);
164 static jmethodID
mID(NULL
);
165 callObjectMethodWithIntArg(t
.pEnv
,"getObject","(I)Ljava/lang/Object;", mID
, columnIndex
);
166 // WARNING: the caller becomes the owner of the returned pointer
170 sal_Int16 SAL_CALL
java_sql_CallableStatement::getShort( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
172 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
173 createStatement(t
.pEnv
);
174 static jmethodID
mID(NULL
);
175 jshort (JNIEnv::*pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallShortMethod
;
176 return callMethodWithIntArg
<jshort
>(pCallMethod
,"getShort","(I)S",mID
,columnIndex
);
179 ::rtl::OUString SAL_CALL
java_sql_CallableStatement::getString( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
181 ::osl::MutexGuard
aGuard( m_aMutex
);
182 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
183 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
184 createStatement(t
.pEnv
);
185 static jmethodID
mID(NULL
);
186 return callStringMethodWithIntArg("getString",mID
,columnIndex
);
189 ::com::sun::star::util::Time SAL_CALL
java_sql_CallableStatement::getTime( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
191 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
192 createStatement(t
.pEnv
);
193 static jmethodID
mID(NULL
);
194 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTime","(I)Ljava/sql/Time;", mID
, columnIndex
);
195 // WARNING: the caller becomes the owner of the returned pointer
196 return out
? static_cast <com::sun::star::util::Time
> (java_sql_Time( t
.pEnv
, out
)) : ::com::sun::star::util::Time();
199 ::com::sun::star::util::DateTime SAL_CALL
java_sql_CallableStatement::getTimestamp( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
201 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
202 createStatement(t
.pEnv
);
203 static jmethodID
mID(NULL
);
204 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTimestamp","(I)Ljava/sql/Timestamp;", mID
, columnIndex
);
205 // WARNING: the caller becomes the owner of the returned pointer
206 return out
? static_cast <com::sun::star::util::DateTime
> (java_sql_Timestamp( t
.pEnv
, out
)) : ::com::sun::star::util::DateTime();
209 void SAL_CALL
java_sql_CallableStatement::registerOutParameter( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const ::rtl::OUString
& typeName
) throw(starsdbc::SQLException
, RuntimeException
)
211 ::osl::MutexGuard
aGuard( m_aMutex
);
212 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
213 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
216 createStatement(t
.pEnv
);
218 // initialize temporary variable
219 static const char * cSignature
= "(IILjava/lang/String;)V";
220 static const char * cMethodName
= "registerOutParameter";
222 static jmethodID
mID(NULL
);
223 obtainMethodId(t
.pEnv
, cMethodName
,cSignature
, mID
);
225 jdbc::LocalRef
< jstring
> str( t
.env(),convertwchar_tToJavaString(t
.pEnv
,typeName
));
226 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,str
.get());
227 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
230 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
)
232 ::osl::MutexGuard
aGuard( m_aMutex
);
233 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
234 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
237 createStatement(t
.pEnv
);
238 // initialize temporary variable
239 static const char * cSignature
= "(III)V";
240 static const char * cMethodName
= "registerOutParameter";
242 static jmethodID
mID(NULL
);
243 obtainMethodId(t
.pEnv
, cMethodName
,cSignature
, mID
);
244 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,scale
);
245 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
249 jclass
java_sql_CallableStatement::theClass
= 0;
251 jclass
java_sql_CallableStatement::getMyClass() const
253 // the class must be fetched only once, therefore static
255 theClass
= findMyClass("java/sql/CallableStatement");
259 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getBinaryStream( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
261 Reference
< starsdbc::XBlob
> xBlob
= getBlob(columnIndex
);
262 return xBlob
.is() ? xBlob
->getBinaryStream() : Reference
< ::com::sun::star::io::XInputStream
>();
264 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getCharacterStream( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
266 Reference
< starsdbc::XClob
> xClob
= getClob(columnIndex
);
267 return xClob
.is() ? xClob
->getCharacterStream() : Reference
< ::com::sun::star::io::XInputStream
>();
270 Reference
< starsdbc::XArray
> SAL_CALL
java_sql_CallableStatement::getArray( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
272 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
273 createStatement(t
.pEnv
);
274 static jmethodID
mID(NULL
);
275 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getArray","(I)Ljava/sql/Array;", mID
, columnIndex
);
276 // WARNING: the caller becomes the owner of the returned pointer
277 return out
==0 ? 0 : new java_sql_Array( t
.pEnv
, out
);
280 Reference
< starsdbc::XClob
> SAL_CALL
java_sql_CallableStatement::getClob( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
282 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
283 createStatement(t
.pEnv
);
284 static jmethodID
mID(NULL
);
285 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getClob","(I)Ljava/sql/Clob;", mID
, columnIndex
);
286 // WARNING: the caller becomes the owner of the returned pointer
287 return out
==0 ? 0 : new java_sql_Clob( t
.pEnv
, out
);
289 Reference
< starsdbc::XBlob
> SAL_CALL
java_sql_CallableStatement::getBlob( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
291 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
292 createStatement(t
.pEnv
);
293 static jmethodID
mID(NULL
);
294 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getBlob","(I)Ljava/sql/Blob;", mID
, columnIndex
);
295 // WARNING: the caller becomes the owner of the returned pointer
296 return out
==0 ? 0 : new java_sql_Blob( t
.pEnv
, out
);
299 Reference
< starsdbc::XRef
> SAL_CALL
java_sql_CallableStatement::getRef( sal_Int32 columnIndex
) throw(starsdbc::SQLException
, RuntimeException
)
301 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
302 createStatement(t
.pEnv
);
303 static jmethodID
mID(NULL
);
304 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getRef","(I)Ljava/sql/Ref;", mID
, columnIndex
);
305 // WARNING: the caller becomes the owner of the returned pointer
306 return out
==0 ? 0 : new java_sql_Ref( t
.pEnv
, out
);
308 // -----------------------------------------------------------------------------
309 void SAL_CALL
java_sql_CallableStatement::acquire() throw()
311 java_sql_PreparedStatement::acquire();
313 // -----------------------------------------------------------------------------
314 void SAL_CALL
java_sql_CallableStatement::release() throw()
316 java_sql_PreparedStatement::release();
318 // -----------------------------------------------------------------------------
319 void java_sql_CallableStatement::createStatement(JNIEnv
* /*_pEnv*/)
321 ::osl::MutexGuard
aGuard( m_aMutex
);
322 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
325 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java Enviroment geloescht worden!");
326 if( t
.pEnv
&& !object
){
327 // initialize temporary variable
328 static const char * cSignature
= "(Ljava/lang/String;II)Ljava/sql/CallableStatement;";
329 static const char * cMethodName
= "prepareCall";
333 jdbc::LocalRef
< jstring
> str( t
.env(),convertwchar_tToJavaString(t
.pEnv
,m_sSqlStatement
));
335 static jmethodID
mID(NULL
);
337 mID
= t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature
);
339 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID
, str
.get() ,m_nResultSetType
,m_nResultSetConcurrency
);
343 static const char * cSignature2
= "(Ljava/lang/String;)Ljava/sql/CallableStatement;";
344 static jmethodID mID2
= t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature2
);OSL_ENSURE(mID2
,"Unknown method id!");
346 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID2
, str
.get() );
349 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
352 object
= t
.pEnv
->NewGlobalRef( out
);
355 // -----------------------------------------------------------------------------
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */