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