Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / jdbc / PreparedStatement.cxx
blob33aadcbca8401aeebfd981da521a495489d229f8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/PreparedStatement.hxx>
21 #include <java/sql/ResultSet.hxx>
22 #include <java/sql/ResultSetMetaData.hxx>
23 #include <java/sql/Connection.hxx>
24 #include <java/sql/Timestamp.hxx>
25 #include <java/math/BigDecimal.hxx>
26 #include <java/tools.hxx>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <comphelper/sequence.hxx>
30 #include <comphelper/types.hxx>
31 #include <connectivity/dbtools.hxx>
32 #include <connectivity/FValue.hxx>
33 #include <connectivity/dbexception.hxx>
34 #include <strings.hrc>
35 #include <resource/sharedresources.hxx>
36 #include <java/LocalRef.hxx>
37 #include <strings.hxx>
38 #include <string.h>
39 #include <memory>
41 using namespace connectivity;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
49 //************ Class: java.sql.PreparedStatement
51 IMPLEMENT_SERVICE_INFO(java_sql_PreparedStatement,"com.sun.star.sdbcx.JPreparedStatement","com.sun.star.sdbc.PreparedStatement");
53 java_sql_PreparedStatement::java_sql_PreparedStatement( JNIEnv * pEnv, java_sql_Connection& _rCon, const OUString& sql )
54 : OStatement_BASE2( pEnv, _rCon )
56 m_sSqlStatement = sql;
59 jclass java_sql_PreparedStatement::theClass = nullptr;
61 java_sql_PreparedStatement::~java_sql_PreparedStatement()
66 jclass java_sql_PreparedStatement::getMyClass() const
68 // the class must be fetched only once, therefore static
69 if( !theClass )
70 theClass = findMyClass("java/sql/PreparedStatement");
71 return theClass;
75 css::uno::Any SAL_CALL java_sql_PreparedStatement::queryInterface( const css::uno::Type & rType )
77 css::uno::Any aRet = OStatement_BASE2::queryInterface(rType);
78 return aRet.hasValue() ? aRet : ::cppu::queryInterface( rType,
79 static_cast< XPreparedStatement*>(this),
80 static_cast< XParameters*>(this),
81 static_cast< XResultSetMetaDataSupplier*>(this),
82 static_cast< XPreparedBatchExecution*>(this));
85 css::uno::Sequence< css::uno::Type > SAL_CALL java_sql_PreparedStatement::getTypes( )
87 ::cppu::OTypeCollection aTypes( cppu::UnoType<XPreparedStatement>::get(),
88 cppu::UnoType<XParameters>::get(),
89 cppu::UnoType<XResultSetMetaDataSupplier>::get(),
90 cppu::UnoType<XPreparedBatchExecution>::get());
92 return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE2::getTypes());
96 sal_Bool SAL_CALL java_sql_PreparedStatement::execute( )
98 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED );
99 ::osl::MutexGuard aGuard( m_aMutex );
100 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
102 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
103 createStatement(t.pEnv);
104 static jmethodID mID(nullptr);
105 return callBooleanMethod( "execute", mID );
109 sal_Int32 SAL_CALL java_sql_PreparedStatement::executeUpdate( )
111 ::osl::MutexGuard aGuard( m_aMutex );
112 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
113 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_UPDATE );
115 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
116 createStatement(t.pEnv);
117 static jmethodID mID(nullptr);
118 return callIntMethod_ThrowSQL("executeUpdate", mID);
122 void SAL_CALL java_sql_PreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x )
124 ::osl::MutexGuard aGuard( m_aMutex );
125 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
126 m_aLogger.log( LogLevel::FINER, STR_LOG_STRING_PARAMETER, parameterIndex, x );
128 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
129 { // initialize temporary Variable
130 createStatement(t.pEnv);
131 static const char * const cSignature = "(ILjava/lang/String;)V";
132 static const char * const cMethodName = "setString";
133 // Java-Call
134 static jmethodID mID(nullptr);
135 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
136 jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,x));
137 t.pEnv->CallVoidMethod( object, mID, parameterIndex,str.get());
138 // and clean up
139 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
140 } //t.pEnv
144 css::uno::Reference< css::sdbc::XConnection > SAL_CALL java_sql_PreparedStatement::getConnection( )
146 return m_pConnection;
150 css::uno::Reference< css::sdbc::XResultSet > SAL_CALL java_sql_PreparedStatement::executeQuery( )
152 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_QUERY );
153 ::osl::MutexGuard aGuard( m_aMutex );
154 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
156 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
157 createStatement(t.pEnv);
158 static jmethodID mID(nullptr);
159 jobject out = callResultSetMethod(t.env(),"executeQuery",mID);
161 return out==nullptr ? nullptr : new java_sql_ResultSet( t.pEnv, out, m_aLogger, *m_pConnection,this);
165 void SAL_CALL java_sql_PreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x )
167 m_aLogger.log( LogLevel::FINER, STR_LOG_BOOLEAN_PARAMETER, parameterIndex, bool(x) );
168 ::osl::MutexGuard aGuard( m_aMutex );
169 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
171 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
172 createStatement(t.pEnv);
173 static jmethodID mID(nullptr);
174 callVoidMethod_ThrowSQL("setBoolean", "(IZ)V", mID, parameterIndex, x);
178 void SAL_CALL java_sql_PreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x )
180 m_aLogger.log( LogLevel::FINER, STR_LOG_BYTE_PARAMETER, parameterIndex, static_cast<sal_Int32>(x) );
181 ::osl::MutexGuard aGuard( m_aMutex );
182 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
184 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
185 createStatement(t.pEnv);
186 static jmethodID mID(nullptr);
187 callVoidMethod_ThrowSQL("setByte", "(IB)V", mID, parameterIndex, x);
191 void SAL_CALL java_sql_PreparedStatement::setDate( sal_Int32 parameterIndex, const css::util::Date& x )
193 m_aLogger.log( LogLevel::FINER, STR_LOG_DATE_PARAMETER, parameterIndex, x );
194 ::osl::MutexGuard aGuard( m_aMutex );
195 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
197 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
198 createStatement(t.pEnv);
199 java_sql_Date aT(x);
200 static jmethodID mID(nullptr);
201 callVoidMethod_ThrowSQL("setDate", "(ILjava/sql/Date;)V", mID, parameterIndex, aT.getJavaObject());
205 void SAL_CALL java_sql_PreparedStatement::setTime( sal_Int32 parameterIndex, const css::util::Time& x )
207 m_aLogger.log( LogLevel::FINER, STR_LOG_TIME_PARAMETER, parameterIndex, x );
208 ::osl::MutexGuard aGuard( m_aMutex );
209 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
211 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
212 createStatement(t.pEnv);
213 java_sql_Time aT(x);
214 static jmethodID mID(nullptr);
215 callVoidMethod_ThrowSQL("setTime", "(ILjava/sql/Time;)V", mID, parameterIndex, aT.getJavaObject());
219 void SAL_CALL java_sql_PreparedStatement::setTimestamp( sal_Int32 parameterIndex, const css::util::DateTime& x )
221 m_aLogger.log( LogLevel::FINER, STR_LOG_TIMESTAMP_PARAMETER, parameterIndex, x );
222 ::osl::MutexGuard aGuard( m_aMutex );
223 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
225 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
226 createStatement(t.pEnv);
227 static jmethodID mID(nullptr);
228 java_sql_Timestamp aD(x);
229 callVoidMethod_ThrowSQL("setTimestamp", "(ILjava/sql/Timestamp;)V", mID, parameterIndex, aD.getJavaObject());
232 void SAL_CALL java_sql_PreparedStatement::setDouble( sal_Int32 parameterIndex, double x )
234 m_aLogger.log( LogLevel::FINER, STR_LOG_DOUBLE_PARAMETER, parameterIndex, x );
235 ::osl::MutexGuard aGuard( m_aMutex );
236 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
238 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
239 createStatement(t.pEnv);
240 static jmethodID mID(nullptr);
241 callVoidMethod_ThrowSQL("setDouble", "(ID)V", mID, parameterIndex, x);
245 void SAL_CALL java_sql_PreparedStatement::setFloat( sal_Int32 parameterIndex, float x )
247 m_aLogger.log( LogLevel::FINER, STR_LOG_FLOAT_PARAMETER, parameterIndex, x );
248 ::osl::MutexGuard aGuard( m_aMutex );
249 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
251 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
252 createStatement(t.pEnv);
253 static jmethodID mID(nullptr);
254 callVoidMethod_ThrowSQL("setFloat", "(IF)V", mID, parameterIndex, x);
258 void SAL_CALL java_sql_PreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x )
260 m_aLogger.log( LogLevel::FINER, STR_LOG_INT_PARAMETER, parameterIndex, x );
261 ::osl::MutexGuard aGuard( m_aMutex );
262 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
264 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
265 createStatement(t.pEnv);
266 static jmethodID mID(nullptr);
267 callVoidMethod_ThrowSQL("setInt", "(II)V", mID, parameterIndex, x);
271 void SAL_CALL java_sql_PreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x )
273 m_aLogger.log( LogLevel::FINER, STR_LOG_LONG_PARAMETER, parameterIndex, x );
274 ::osl::MutexGuard aGuard( m_aMutex );
275 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
277 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
278 createStatement(t.pEnv);
279 static jmethodID mID(nullptr);
280 callVoidMethod_ThrowSQL("setLong", "(IJ)V", mID, parameterIndex, x);
284 void SAL_CALL java_sql_PreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType )
286 m_aLogger.log( LogLevel::FINER, STR_LOG_NULL_PARAMETER, parameterIndex, sqlType );
287 ::osl::MutexGuard aGuard( m_aMutex );
288 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
290 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
291 createStatement(t.pEnv);
292 static jmethodID mID(nullptr);
293 callVoidMethod_ThrowSQL("setNull", "(II)V", mID, parameterIndex, sqlType);
297 void SAL_CALL java_sql_PreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XClob >& /*x*/ )
299 ::dbtools::throwFeatureNotImplementedSQLException( "XParameters::setClob", *this );
303 void SAL_CALL java_sql_PreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XBlob >& /*x*/ )
305 ::dbtools::throwFeatureNotImplementedSQLException( "XParameters::setBlob", *this );
309 void SAL_CALL java_sql_PreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XArray >& /*x*/ )
311 ::dbtools::throwFeatureNotImplementedSQLException( "XParameters::setArray", *this );
315 void SAL_CALL java_sql_PreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XRef >& /*x*/ )
317 ::dbtools::throwFeatureNotImplementedSQLException( "XParameters::setRef", *this );
321 void SAL_CALL java_sql_PreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const css::uno::Any& x, sal_Int32 targetSqlType, sal_Int32 scale )
323 m_aLogger.log( LogLevel::FINER, STR_LOG_OBJECT_NULL_PARAMETER, parameterIndex );
324 ::osl::MutexGuard aGuard( m_aMutex );
325 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
327 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
329 createStatement(t.pEnv);
331 // initialize temporary Variable
332 static const char * const cSignature = "(ILjava/lang/Object;II)V";
333 static const char * const cMethodName = "setObject";
334 // Java-Call
335 static jmethodID mID(nullptr);
336 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
338 jobject obj = nullptr;
339 switch(targetSqlType)
341 case DataType::DECIMAL:
342 case DataType::NUMERIC:
344 double nTemp = 0.0;
346 std::unique_ptr<java_math_BigDecimal> pBigDecimal;
347 if ( x >>= nTemp)
349 pBigDecimal.reset(new java_math_BigDecimal(nTemp));
350 //setDouble(parameterIndex,nTemp);
351 //return;
353 else
355 ORowSetValue aValue;
356 aValue.fill(x);
357 const OUString sValue = aValue.getString();
358 if ( !sValue.isEmpty() )
359 pBigDecimal.reset(new java_math_BigDecimal(sValue));
360 else
361 pBigDecimal.reset(new java_math_BigDecimal(0.0));
363 //obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
364 t.pEnv->CallVoidMethod( object, mID, parameterIndex,pBigDecimal->getJavaObject(),targetSqlType,scale);
365 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
366 return;
368 default:
369 obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
370 break;
372 t.pEnv->CallVoidMethod( object, mID, parameterIndex,obj,targetSqlType,scale);
373 t.pEnv->DeleteLocalRef(obj);
374 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
375 // and clean up
376 } //mID
377 } //t.pEnv
381 void SAL_CALL java_sql_PreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/, const OUString& /*typeName*/ )
383 m_aLogger.log( LogLevel::FINER, STR_LOG_OBJECT_NULL_PARAMETER, parameterIndex );
384 ::osl::MutexGuard aGuard( m_aMutex );
385 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
387 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
388 createStatement(t.pEnv);
389 static jmethodID mID(nullptr);
390 callVoidMethod_ThrowSQL<jobject>("setObject", "(ILjava/lang/Object;)V", mID, parameterIndex, nullptr);
394 void SAL_CALL java_sql_PreparedStatement::setObject( sal_Int32 parameterIndex, const css::uno::Any& x )
396 if(!::dbtools::implSetObject(this,parameterIndex,x))
398 const OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
399 STR_UNKNOWN_PARA_TYPE,
400 "$position$", OUString::number(parameterIndex)
401 ) );
402 ::dbtools::throwGenericSQLException(sError,*this);
407 void SAL_CALL java_sql_PreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x )
409 m_aLogger.log( LogLevel::FINER, STR_LOG_SHORT_PARAMETER, parameterIndex, x );
410 ::osl::MutexGuard aGuard( m_aMutex );
411 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
413 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
414 createStatement(t.pEnv);
415 static jmethodID mID(nullptr);
416 callVoidMethod_ThrowSQL("setShort", "(IS)V", mID, parameterIndex, x);
420 void SAL_CALL java_sql_PreparedStatement::setBytes( sal_Int32 parameterIndex, const css::uno::Sequence< sal_Int8 >& x )
422 m_aLogger.log( LogLevel::FINER, STR_LOG_BYTES_PARAMETER, parameterIndex );
423 ::osl::MutexGuard aGuard( m_aMutex );
424 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
426 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
428 createStatement(t.pEnv);
430 // initialize temporary Variable
431 static const char * const cSignature = "(I[B)V";
432 static const char * const cMethodName = "setBytes";
433 // Java-Call
434 static jmethodID mID(nullptr);
435 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
436 jbyteArray pByteArray = t.pEnv->NewByteArray(x.getLength());
437 jbyte * pData = reinterpret_cast<jbyte *>(
438 const_cast<sal_Int8 *>(x.getConstArray()));
439 // 4th param of Set*ArrayRegion changed from pointer to non-const to
440 // pointer to const between <http://docs.oracle.com/javase/6/docs/
441 // technotes/guides/jni/spec/functions.html#wp22933> and
442 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
443 // functions.html#wp22933>; work around that difference in a way
444 // that doesn't trigger loplugin:redundantcast
445 t.pEnv->SetByteArrayRegion(pByteArray,0,x.getLength(),pData);
446 t.pEnv->CallVoidMethod( object, mID, parameterIndex,pByteArray);
447 t.pEnv->DeleteLocalRef(pByteArray);
448 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
449 } //t.pEnv
453 void SAL_CALL java_sql_PreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream >& x, sal_Int32 length )
455 m_aLogger.log( LogLevel::FINER, STR_LOG_CHARSTREAM_PARAMETER, parameterIndex );
456 ::osl::MutexGuard aGuard( m_aMutex );
457 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
459 SDBThreadAttach t;
460 assert(t.pEnv && "Java environment has been deleted!");
462 createStatement(t.pEnv);
464 // initialize temporary variable
465 static const char * const cSignature = "(ILjava/io/InputStream;I)V";
466 static const char * const cMethodName = "setCharacterStream";
467 // Java-Call
468 static jmethodID mID(nullptr);
469 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
470 Sequence< sal_Int8 > aSeq;
471 if ( x.is() )
472 x->readBytes( aSeq, length );
473 sal_Int32 actualLength = aSeq.getLength();
475 jvalue args2[3];
476 jbyteArray pByteArray = t.pEnv->NewByteArray( actualLength );
477 jbyte * aSeqData = reinterpret_cast<jbyte *>(
478 const_cast<sal_Int8 *>(aSeq.getConstArray()));
479 // 4th param of Set*ArrayRegion changed from pointer to non-const to
480 // pointer to const between <http://docs.oracle.com/javase/6/docs/
481 // technotes/guides/jni/spec/functions.html#wp22933> and
482 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
483 // functions.html#wp22933>; work around that difference in a way
484 // that doesn't trigger loplugin:redundantcast
485 t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,aSeqData);
486 args2[0].l = pByteArray;
487 args2[1].i = 0;
488 args2[2].i = actualLength;
489 // Java-Call
490 jclass aClass = t.pEnv->FindClass("java/io/CharArrayInputStream");
491 static jmethodID mID2 = nullptr;
492 if ( !mID2 )
494 // initialize temporary variable
495 const char * const cSignatureStream = "([BII)V";
496 mID2 = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
498 jobject tempObj = nullptr;
499 if(mID2)
500 tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
502 t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,actualLength);
503 // and clean up
504 t.pEnv->DeleteLocalRef(pByteArray);
505 t.pEnv->DeleteLocalRef(tempObj);
506 t.pEnv->DeleteLocalRef(aClass);
507 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
508 } //t.pEnv
512 void SAL_CALL java_sql_PreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream >& x, sal_Int32 length )
514 m_aLogger.log( LogLevel::FINER, STR_LOG_BINARYSTREAM_PARAMETER, parameterIndex );
515 ::osl::MutexGuard aGuard( m_aMutex );
516 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
518 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
520 createStatement(t.pEnv);
521 // initialize temporary variable
522 static const char * const cSignature = "(ILjava/io/InputStream;I)V";
523 static const char * const cMethodName = "setBinaryStream";
524 // Java-Call
525 static jmethodID mID(nullptr);
526 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
528 Sequence< sal_Int8 > aSeq;
529 if ( x.is() )
530 x->readBytes( aSeq, length );
531 sal_Int32 actualLength = aSeq.getLength();
533 jvalue args2[3];
534 jbyteArray pByteArray = t.pEnv->NewByteArray(actualLength);
535 jbyte * aSeqData = reinterpret_cast<jbyte *>(
536 const_cast<sal_Int8 *>(aSeq.getConstArray()));
537 // 4th param of Set*ArrayRegion changed from pointer to non-const to
538 // pointer to const between <http://docs.oracle.com/javase/6/docs/
539 // technotes/guides/jni/spec/functions.html#wp22933> and
540 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
541 // functions.html#wp22933>; work around that difference in a way
542 // that doesn't trigger loplugin:redundantcast
543 t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,aSeqData);
544 args2[0].l = pByteArray;
545 args2[1].i = 0;
546 args2[2].i = actualLength;
548 // Java-Call
549 jclass aClass = t.pEnv->FindClass("java/io/ByteArrayInputStream");
550 static jmethodID mID2 = nullptr;
551 if ( !mID2 )
553 // initialize temporary variable
554 const char * const cSignatureStream = "([BII)V";
555 mID2 = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
557 jobject tempObj = nullptr;
558 if(mID2)
559 tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
560 t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,actualLength);
561 // and clean up
562 t.pEnv->DeleteLocalRef(pByteArray);
563 t.pEnv->DeleteLocalRef(tempObj);
564 t.pEnv->DeleteLocalRef(aClass);
565 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
567 } //t.pEnv
571 void SAL_CALL java_sql_PreparedStatement::clearParameters( )
573 m_aLogger.log( LogLevel::FINER, STR_LOG_CLEAR_PARAMETERS );
574 ::osl::MutexGuard aGuard( m_aMutex );
575 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
577 SDBThreadAttach t;
579 createStatement(t.pEnv);
581 static jmethodID mID(nullptr);
582 callVoidMethod_ThrowSQL("clearParameters",mID);
583 } //t.pEnv
586 void SAL_CALL java_sql_PreparedStatement::clearBatch( )
588 ::osl::MutexGuard aGuard( m_aMutex );
589 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
590 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
592 createStatement(t.pEnv);
593 static jmethodID mID(nullptr);
594 callVoidMethod_ThrowSQL("clearBatch",mID);
595 } //t.pEnv
599 void SAL_CALL java_sql_PreparedStatement::addBatch( )
601 ::osl::MutexGuard aGuard( m_aMutex );
602 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
603 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
605 createStatement(t.pEnv);
606 static jmethodID mID(nullptr);
607 callVoidMethod_ThrowSQL("addBatch", mID);
608 } //t.pEnv
612 css::uno::Sequence< sal_Int32 > SAL_CALL java_sql_PreparedStatement::executeBatch( )
614 ::osl::MutexGuard aGuard( m_aMutex );
615 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
616 css::uno::Sequence< sal_Int32 > aSeq;
617 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
618 createStatement(t.pEnv);
619 static jmethodID mID(nullptr);
620 jintArray out = static_cast<jintArray>(callObjectMethod(t.pEnv,"executeBatch","()[I", mID));
621 if(out)
623 jboolean p = false;
624 aSeq.realloc(t.pEnv->GetArrayLength(out));
625 memcpy(aSeq.getArray(),t.pEnv->GetIntArrayElements(out,&p),aSeq.getLength());
626 t.pEnv->DeleteLocalRef(out);
628 return aSeq;
631 css::uno::Reference< css::sdbc::XResultSetMetaData > SAL_CALL java_sql_PreparedStatement::getMetaData( )
633 ::osl::MutexGuard aGuard( m_aMutex );
634 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
635 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
636 createStatement(t.pEnv);
637 static jmethodID mID(nullptr);
638 jobject out = callObjectMethod(t.pEnv,"getMetaData","()Ljava/sql/ResultSetMetaData;", mID);
640 return out==nullptr ? nullptr : new java_sql_ResultSetMetaData( t.pEnv, out, *m_pConnection );
643 void SAL_CALL java_sql_PreparedStatement::acquire() noexcept
645 OStatement_BASE2::acquire();
648 void SAL_CALL java_sql_PreparedStatement::release() noexcept
650 OStatement_BASE2::release();
653 void java_sql_PreparedStatement::createStatement(JNIEnv* _pEnv)
655 ::osl::MutexGuard aGuard( m_aMutex );
656 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
658 if( object || !_pEnv )
659 return;
661 // initialize temporary variable
662 static const char * const cMethodName = "prepareStatement";
664 jvalue args[1];
665 // convert Parameter
666 args[0].l = convertwchar_tToJavaString(_pEnv,m_sSqlStatement);
667 // Java-Call
668 jobject out = nullptr;
669 static jmethodID mID(nullptr);
670 if ( !mID )
672 static const char * const cSignature = "(Ljava/lang/String;II)Ljava/sql/PreparedStatement;";
673 mID = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature );
675 if( mID )
677 out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID, args[0].l ,m_nResultSetType,m_nResultSetConcurrency);
679 else
681 static jmethodID mID2 = nullptr;
682 if ( !mID2 )
684 static const char * const cSignature2 = "(Ljava/lang/String;)Ljava/sql/PreparedStatement;";
685 mID2 = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature2 );
687 if ( mID2 )
688 out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID2, args[0].l );
690 _pEnv->DeleteLocalRef(static_cast<jstring>(args[0].l));
691 ThrowLoggedSQLException( m_aLogger, _pEnv, *this );
692 if ( out )
693 object = _pEnv->NewGlobalRef( out );
697 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */