1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <cppuhelper/queryinterface.hxx>
30 #include <comphelper/sequence.hxx>
34 using namespace connectivity
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::lang
;
41 IMPLEMENT_SERVICE_INFO(java_sql_CallableStatement
,u
"com.sun.star.sdbcx.ACallableStatement"_ustr
,u
"com.sun.star.sdbc.CallableStatement"_ustr
);
44 //************ Class: java.sql.CallableStatement
46 java_sql_CallableStatement::java_sql_CallableStatement( JNIEnv
* pEnv
, java_sql_Connection
& _rCon
,const OUString
& sql
)
47 : java_sql_PreparedStatement( pEnv
, _rCon
, sql
)
51 java_sql_CallableStatement::~java_sql_CallableStatement()
56 Any SAL_CALL
java_sql_CallableStatement::queryInterface( const Type
& rType
)
58 Any aRet
= java_sql_PreparedStatement::queryInterface(rType
);
59 return aRet
.hasValue() ? aRet
: ::cppu::queryInterface(rType
,static_cast< css::sdbc::XRow
*>(this),static_cast< css::sdbc::XOutParameters
*>(this));
62 css::uno::Sequence
< css::uno::Type
> SAL_CALL
java_sql_CallableStatement::getTypes( )
64 css::uno::Type aTypes
[] { cppu::UnoType
<css::sdbc::XRow
>::get(),
65 cppu::UnoType
<css::sdbc::XOutParameters
>::get() };
67 return ::comphelper::concatSequences(java_sql_PreparedStatement::getTypes(), aTypes
);
70 sal_Bool SAL_CALL
java_sql_CallableStatement::wasNull( )
72 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
73 createStatement(t
.pEnv
);
74 static jmethodID
mID(nullptr);
75 return callBooleanMethod( "wasNull", mID
);
78 sal_Bool SAL_CALL
java_sql_CallableStatement::getBoolean( sal_Int32 columnIndex
)
80 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
81 createStatement(t
.pEnv
);
82 static jmethodID
mID(nullptr);
83 return callBooleanMethodWithIntArg( "getBoolean", mID
,columnIndex
);
85 sal_Int8 SAL_CALL
java_sql_CallableStatement::getByte( sal_Int32 columnIndex
)
87 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
88 createStatement(t
.pEnv
);
89 static jmethodID
mID(nullptr);
90 jbyte (JNIEnv::* const pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallByteMethod
;
91 return callMethodWithIntArg
<jbyte
>(pCallMethod
,"getByte","(I)B",mID
,columnIndex
);
93 Sequence
< sal_Int8
> SAL_CALL
java_sql_CallableStatement::getBytes( sal_Int32 columnIndex
)
95 ::osl::MutexGuard
aGuard( m_aMutex
);
96 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
97 Sequence
< sal_Int8
> aSeq
;
99 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
100 createStatement(t
.pEnv
);
101 static jmethodID
mID(nullptr);
102 jbyteArray out
= static_cast<jbyteArray
>(callObjectMethodWithIntArg(t
.pEnv
,"getBytes","(I)[B", mID
, columnIndex
));
106 aSeq
.realloc(t
.pEnv
->GetArrayLength(out
));
107 memcpy(aSeq
.getArray(),t
.pEnv
->GetByteArrayElements(out
,&p
),aSeq
.getLength());
108 t
.pEnv
->DeleteLocalRef(out
);
112 css::util::Date SAL_CALL
java_sql_CallableStatement::getDate( sal_Int32 columnIndex
)
114 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
115 createStatement(t
.pEnv
);
116 static jmethodID
mID(nullptr);
117 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getDate","(I)Ljava/sql/Date;", mID
, columnIndex
);
118 return out
? static_cast <css::util::Date
>(java_sql_Date( t
.pEnv
, out
)) : css::util::Date();
120 double SAL_CALL
java_sql_CallableStatement::getDouble( sal_Int32 columnIndex
)
122 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
123 createStatement(t
.pEnv
);
124 static jmethodID
mID(nullptr);
125 double (JNIEnv::* const pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallDoubleMethod
;
126 return callMethodWithIntArg
<double>(pCallMethod
,"getDouble","(I)D",mID
,columnIndex
);
129 float SAL_CALL
java_sql_CallableStatement::getFloat( sal_Int32 columnIndex
)
131 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
132 createStatement(t
.pEnv
);
133 static jmethodID
mID(nullptr);
134 jfloat (JNIEnv::* const pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallFloatMethod
;
135 return callMethodWithIntArg
<jfloat
>(pCallMethod
,"getFloat","(I)F",mID
,columnIndex
);
138 sal_Int32 SAL_CALL
java_sql_CallableStatement::getInt( sal_Int32 columnIndex
)
140 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
141 createStatement(t
.pEnv
);
142 static jmethodID
mID(nullptr);
143 return callIntMethodWithIntArg_ThrowSQL("getInt",mID
,columnIndex
);
146 sal_Int64 SAL_CALL
java_sql_CallableStatement::getLong( sal_Int32 columnIndex
)
148 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
149 createStatement(t
.pEnv
);
150 static jmethodID
mID(nullptr);
151 jlong (JNIEnv::* const pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallLongMethod
;
152 return callMethodWithIntArg
<jlong
>(pCallMethod
,"getLong","(I)J",mID
,columnIndex
);
155 Any SAL_CALL
java_sql_CallableStatement::getObject( sal_Int32 columnIndex
, const Reference
< css::container::XNameAccess
>& /*typeMap*/ )
157 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
158 createStatement(t
.pEnv
);
159 static jmethodID
mID(nullptr);
160 callObjectMethodWithIntArg(t
.pEnv
,"getObject","(I)Ljava/lang/Object;", mID
, columnIndex
);
161 // WARNING: the caller becomes the owner of the returned pointer
165 sal_Int16 SAL_CALL
java_sql_CallableStatement::getShort( sal_Int32 columnIndex
)
167 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
168 createStatement(t
.pEnv
);
169 static jmethodID
mID(nullptr);
170 jshort (JNIEnv::* const pCallMethod
)( jobject obj
, jmethodID methodID
, ... ) = &JNIEnv::CallShortMethod
;
171 return callMethodWithIntArg
<jshort
>(pCallMethod
,"getShort","(I)S",mID
,columnIndex
);
174 OUString SAL_CALL
java_sql_CallableStatement::getString( sal_Int32 columnIndex
)
176 ::osl::MutexGuard
aGuard( m_aMutex
);
177 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
178 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
179 createStatement(t
.pEnv
);
180 static jmethodID
mID(nullptr);
181 return callStringMethodWithIntArg("getString",mID
,columnIndex
);
184 css::util::Time SAL_CALL
java_sql_CallableStatement::getTime( sal_Int32 columnIndex
)
186 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
187 createStatement(t
.pEnv
);
188 static jmethodID
mID(nullptr);
189 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTime","(I)Ljava/sql/Time;", mID
, columnIndex
);
190 // WARNING: the caller becomes the owner of the returned pointer
191 return out
? static_cast <css::util::Time
> (java_sql_Time( t
.pEnv
, out
)) : css::util::Time();
194 css::util::DateTime SAL_CALL
java_sql_CallableStatement::getTimestamp( sal_Int32 columnIndex
)
196 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
197 createStatement(t
.pEnv
);
198 static jmethodID
mID(nullptr);
199 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getTimestamp","(I)Ljava/sql/Timestamp;", mID
, columnIndex
);
200 // WARNING: the caller becomes the owner of the returned pointer
201 return out
? static_cast <css::util::DateTime
> (java_sql_Timestamp( t
.pEnv
, out
)) : css::util::DateTime();
204 void SAL_CALL
java_sql_CallableStatement::registerOutParameter( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const OUString
& typeName
)
206 ::osl::MutexGuard
aGuard( m_aMutex
);
207 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
208 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
211 createStatement(t
.pEnv
);
213 // initialize temporary variable
214 static const char * const cSignature
= "(IILjava/lang/String;)V";
215 static const char * const cMethodName
= "registerOutParameter";
217 static jmethodID
mID(nullptr);
218 obtainMethodId_throwSQL(t
.pEnv
, cMethodName
,cSignature
, mID
);
220 jdbc::LocalRef
< jstring
> str( t
.env(),convertwchar_tToJavaString(t
.pEnv
,typeName
));
221 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,str
.get());
222 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
225 void SAL_CALL
java_sql_CallableStatement::registerNumericOutParameter( sal_Int32 parameterIndex
, sal_Int32 sqlType
, sal_Int32 scale
)
227 ::osl::MutexGuard
aGuard( m_aMutex
);
228 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
229 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
232 createStatement(t
.pEnv
);
233 // initialize temporary variable
234 static const char * const cSignature
= "(III)V";
235 static const char * const cMethodName
= "registerOutParameter";
237 static jmethodID
mID(nullptr);
238 obtainMethodId_throwSQL(t
.pEnv
, cMethodName
,cSignature
, mID
);
239 t
.pEnv
->CallVoidMethod( object
, mID
, parameterIndex
,sqlType
,scale
);
240 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
244 jclass
java_sql_CallableStatement::theClass
= nullptr;
246 jclass
java_sql_CallableStatement::getMyClass() const
248 // the class must be fetched only once, therefore static
250 theClass
= findMyClass("java/sql/CallableStatement");
254 Reference
< css::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getBinaryStream( sal_Int32 columnIndex
)
256 Reference
< css::sdbc::XBlob
> xBlob
= getBlob(columnIndex
);
257 return xBlob
.is() ? xBlob
->getBinaryStream() : Reference
< css::io::XInputStream
>();
259 Reference
< css::io::XInputStream
> SAL_CALL
java_sql_CallableStatement::getCharacterStream( sal_Int32 columnIndex
)
261 Reference
< css::sdbc::XClob
> xClob
= getClob(columnIndex
);
262 return xClob
.is() ? xClob
->getCharacterStream() : Reference
< css::io::XInputStream
>();
265 Reference
< css::sdbc::XArray
> SAL_CALL
java_sql_CallableStatement::getArray( sal_Int32 columnIndex
)
267 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
268 createStatement(t
.pEnv
);
269 static jmethodID
mID(nullptr);
270 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getArray","(I)Ljava/sql/Array;", mID
, columnIndex
);
271 // WARNING: the caller becomes the owner of the returned pointer
272 return out
==nullptr ? nullptr : new java_sql_Array( t
.pEnv
, out
);
275 Reference
< css::sdbc::XClob
> SAL_CALL
java_sql_CallableStatement::getClob( sal_Int32 columnIndex
)
277 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
278 createStatement(t
.pEnv
);
279 static jmethodID
mID(nullptr);
280 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getClob","(I)Ljava/sql/Clob;", mID
, columnIndex
);
281 // WARNING: the caller becomes the owner of the returned pointer
282 return out
==nullptr ? nullptr : new java_sql_Clob( t
.pEnv
, out
);
284 Reference
< css::sdbc::XBlob
> SAL_CALL
java_sql_CallableStatement::getBlob( sal_Int32 columnIndex
)
286 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
287 createStatement(t
.pEnv
);
288 static jmethodID
mID(nullptr);
289 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getBlob","(I)Ljava/sql/Blob;", mID
, columnIndex
);
290 // WARNING: the caller becomes the owner of the returned pointer
291 return out
==nullptr ? nullptr : new java_sql_Blob( t
.pEnv
, out
);
294 Reference
< css::sdbc::XRef
> SAL_CALL
java_sql_CallableStatement::getRef( sal_Int32 columnIndex
)
296 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
297 createStatement(t
.pEnv
);
298 static jmethodID
mID(nullptr);
299 jobject out
= callObjectMethodWithIntArg(t
.pEnv
,"getRef","(I)Ljava/sql/Ref;", mID
, columnIndex
);
300 // WARNING: the caller becomes the owner of the returned pointer
301 return out
==nullptr ? nullptr : new java_sql_Ref( t
.pEnv
, out
);
304 void SAL_CALL
java_sql_CallableStatement::acquire() noexcept
306 java_sql_PreparedStatement::acquire();
309 void SAL_CALL
java_sql_CallableStatement::release() noexcept
311 java_sql_PreparedStatement::release();
314 void java_sql_CallableStatement::createStatement(JNIEnv
* /*_pEnv*/)
316 ::osl::MutexGuard
aGuard( m_aMutex
);
317 checkDisposed(java_sql_Statement_BASE::rBHelper
.bDisposed
);
319 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
320 if( !t
.pEnv
|| object
)
323 // initialize temporary variable
324 static const char * const cMethodName
= "prepareCall";
326 jobject out
= nullptr;
328 jdbc::LocalRef
< jstring
> str( t
.env(),convertwchar_tToJavaString(t
.pEnv
,m_sSqlStatement
));
330 static jmethodID mID
= [&]()
332 static const char * const cSignature
= "(Ljava/lang/String;II)Ljava/sql/CallableStatement;";
333 return t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature
);
336 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID
, str
.get() ,m_nResultSetType
,m_nResultSetConcurrency
);
340 static const char * const cSignature2
= "(Ljava/lang/String;)Ljava/sql/CallableStatement;";
341 static jmethodID mID2
= t
.pEnv
->GetMethodID( m_pConnection
->getMyClass(), cMethodName
, cSignature2
);OSL_ENSURE(mID2
,"Unknown method id!");
343 out
= t
.pEnv
->CallObjectMethod( m_pConnection
->getJavaObject(), mID2
, str
.get() );
346 ThrowLoggedSQLException( m_aLogger
, t
.pEnv
, *this );
349 object
= t
.pEnv
->NewGlobalRef( out
);
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */