tdf#150789 - FILEOPEN PPTX: fix text in SmartArt vertically off
[LibreOffice.git] / connectivity / source / drivers / jdbc / PreparedStatement.cxx
blob65ff309c019c9e4f9399c5a76897d3ad49847577
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::sdbc;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
48 //************ Class: java.sql.PreparedStatement
50 IMPLEMENT_SERVICE_INFO(java_sql_PreparedStatement,u"com.sun.star.sdbcx.JPreparedStatement"_ustr,u"com.sun.star.sdbc.PreparedStatement"_ustr);
52 java_sql_PreparedStatement::java_sql_PreparedStatement( JNIEnv * pEnv, java_sql_Connection& _rCon, const OUString& sql )
53 : OStatement_BASE2( pEnv, _rCon )
55 m_sSqlStatement = sql;
58 jclass java_sql_PreparedStatement::theClass = nullptr;
60 java_sql_PreparedStatement::~java_sql_PreparedStatement()
65 jclass java_sql_PreparedStatement::getMyClass() const
67 // the class must be fetched only once, therefore static
68 if( !theClass )
69 theClass = findMyClass("java/sql/PreparedStatement");
70 return theClass;
74 css::uno::Any SAL_CALL java_sql_PreparedStatement::queryInterface( const css::uno::Type & rType )
76 css::uno::Any aRet = OStatement_BASE2::queryInterface(rType);
77 return aRet.hasValue() ? aRet : ::cppu::queryInterface( rType,
78 static_cast< XPreparedStatement*>(this),
79 static_cast< XParameters*>(this),
80 static_cast< XResultSetMetaDataSupplier*>(this),
81 static_cast< XPreparedBatchExecution*>(this));
84 css::uno::Sequence< css::uno::Type > SAL_CALL java_sql_PreparedStatement::getTypes( )
86 ::cppu::OTypeCollection aTypes( cppu::UnoType<XPreparedStatement>::get(),
87 cppu::UnoType<XParameters>::get(),
88 cppu::UnoType<XResultSetMetaDataSupplier>::get(),
89 cppu::UnoType<XPreparedBatchExecution>::get());
91 return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE2::getTypes());
95 sal_Bool SAL_CALL java_sql_PreparedStatement::execute( )
97 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED );
98 ::osl::MutexGuard aGuard( m_aMutex );
99 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
101 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
102 createStatement(t.pEnv);
103 static jmethodID mID(nullptr);
104 return callBooleanMethod( "execute", mID );
108 sal_Int32 SAL_CALL java_sql_PreparedStatement::executeUpdate( )
110 ::osl::MutexGuard aGuard( m_aMutex );
111 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
112 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_UPDATE );
114 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
115 createStatement(t.pEnv);
116 static jmethodID mID(nullptr);
117 return callIntMethod_ThrowSQL("executeUpdate", mID);
121 void SAL_CALL java_sql_PreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x )
123 ::osl::MutexGuard aGuard( m_aMutex );
124 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
125 m_aLogger.log( LogLevel::FINER, STR_LOG_STRING_PARAMETER, parameterIndex, x );
127 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
128 { // initialize temporary Variable
129 createStatement(t.pEnv);
130 static const char * const cSignature = "(ILjava/lang/String;)V";
131 static const char * const cMethodName = "setString";
132 // Java-Call
133 static jmethodID mID(nullptr);
134 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
135 jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,x));
136 t.pEnv->CallVoidMethod( object, mID, parameterIndex,str.get());
137 // and clean up
138 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
139 } //t.pEnv
143 css::uno::Reference< css::sdbc::XConnection > SAL_CALL java_sql_PreparedStatement::getConnection( )
145 return m_pConnection;
149 css::uno::Reference< css::sdbc::XResultSet > SAL_CALL java_sql_PreparedStatement::executeQuery( )
151 m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_QUERY );
152 ::osl::MutexGuard aGuard( m_aMutex );
153 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
155 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
156 createStatement(t.pEnv);
157 static jmethodID mID(nullptr);
158 jobject out = callResultSetMethod(t.env(),"executeQuery",mID);
160 return out==nullptr ? nullptr : new java_sql_ResultSet( t.pEnv, out, m_aLogger, *m_pConnection,this);
164 void SAL_CALL java_sql_PreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x )
166 m_aLogger.log( LogLevel::FINER, STR_LOG_BOOLEAN_PARAMETER, parameterIndex, bool(x) );
167 ::osl::MutexGuard aGuard( m_aMutex );
168 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
170 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
171 createStatement(t.pEnv);
172 static jmethodID mID(nullptr);
173 callVoidMethod_ThrowSQL("setBoolean", "(IZ)V", mID, parameterIndex, x);
177 void SAL_CALL java_sql_PreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x )
179 m_aLogger.log( LogLevel::FINER, STR_LOG_BYTE_PARAMETER, parameterIndex, static_cast<sal_Int32>(x) );
180 ::osl::MutexGuard aGuard( m_aMutex );
181 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
183 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
184 createStatement(t.pEnv);
185 static jmethodID mID(nullptr);
186 callVoidMethod_ThrowSQL("setByte", "(IB)V", mID, parameterIndex, x);
190 void SAL_CALL java_sql_PreparedStatement::setDate( sal_Int32 parameterIndex, const css::util::Date& x )
192 m_aLogger.log( LogLevel::FINER, STR_LOG_DATE_PARAMETER, parameterIndex, x );
193 ::osl::MutexGuard aGuard( m_aMutex );
194 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
196 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
197 createStatement(t.pEnv);
198 java_sql_Date aT(x);
199 static jmethodID mID(nullptr);
200 callVoidMethod_ThrowSQL("setDate", "(ILjava/sql/Date;)V", mID, parameterIndex, aT.getJavaObject());
204 void SAL_CALL java_sql_PreparedStatement::setTime( sal_Int32 parameterIndex, const css::util::Time& x )
206 m_aLogger.log( LogLevel::FINER, STR_LOG_TIME_PARAMETER, parameterIndex, x );
207 ::osl::MutexGuard aGuard( m_aMutex );
208 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
210 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
211 createStatement(t.pEnv);
212 java_sql_Time aT(x);
213 static jmethodID mID(nullptr);
214 callVoidMethod_ThrowSQL("setTime", "(ILjava/sql/Time;)V", mID, parameterIndex, aT.getJavaObject());
218 void SAL_CALL java_sql_PreparedStatement::setTimestamp( sal_Int32 parameterIndex, const css::util::DateTime& x )
220 m_aLogger.log( LogLevel::FINER, STR_LOG_TIMESTAMP_PARAMETER, parameterIndex, x );
221 ::osl::MutexGuard aGuard( m_aMutex );
222 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
224 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
225 createStatement(t.pEnv);
226 static jmethodID mID(nullptr);
227 java_sql_Timestamp aD(x);
228 callVoidMethod_ThrowSQL("setTimestamp", "(ILjava/sql/Timestamp;)V", mID, parameterIndex, aD.getJavaObject());
231 void SAL_CALL java_sql_PreparedStatement::setDouble( sal_Int32 parameterIndex, double x )
233 m_aLogger.log( LogLevel::FINER, STR_LOG_DOUBLE_PARAMETER, parameterIndex, x );
234 ::osl::MutexGuard aGuard( m_aMutex );
235 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
237 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
238 createStatement(t.pEnv);
239 static jmethodID mID(nullptr);
240 callVoidMethod_ThrowSQL("setDouble", "(ID)V", mID, parameterIndex, x);
244 void SAL_CALL java_sql_PreparedStatement::setFloat( sal_Int32 parameterIndex, float x )
246 m_aLogger.log( LogLevel::FINER, STR_LOG_FLOAT_PARAMETER, parameterIndex, x );
247 ::osl::MutexGuard aGuard( m_aMutex );
248 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
250 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
251 createStatement(t.pEnv);
252 static jmethodID mID(nullptr);
253 callVoidMethod_ThrowSQL("setFloat", "(IF)V", mID, parameterIndex, x);
257 void SAL_CALL java_sql_PreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x )
259 m_aLogger.log( LogLevel::FINER, STR_LOG_INT_PARAMETER, parameterIndex, x );
260 ::osl::MutexGuard aGuard( m_aMutex );
261 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
263 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
264 createStatement(t.pEnv);
265 static jmethodID mID(nullptr);
266 callVoidMethod_ThrowSQL("setInt", "(II)V", mID, parameterIndex, x);
270 void SAL_CALL java_sql_PreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x )
272 m_aLogger.log( LogLevel::FINER, STR_LOG_LONG_PARAMETER, parameterIndex, x );
273 ::osl::MutexGuard aGuard( m_aMutex );
274 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
276 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
277 createStatement(t.pEnv);
278 static jmethodID mID(nullptr);
279 callVoidMethod_ThrowSQL("setLong", "(IJ)V", mID, parameterIndex, x);
283 void SAL_CALL java_sql_PreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType )
285 m_aLogger.log( LogLevel::FINER, STR_LOG_NULL_PARAMETER, parameterIndex, sqlType );
286 ::osl::MutexGuard aGuard( m_aMutex );
287 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
289 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
290 createStatement(t.pEnv);
291 static jmethodID mID(nullptr);
292 callVoidMethod_ThrowSQL("setNull", "(II)V", mID, parameterIndex, sqlType);
296 void SAL_CALL java_sql_PreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XClob >& /*x*/ )
298 ::dbtools::throwFeatureNotImplementedSQLException( u"XParameters::setClob"_ustr, *this );
302 void SAL_CALL java_sql_PreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XBlob >& /*x*/ )
304 ::dbtools::throwFeatureNotImplementedSQLException( u"XParameters::setBlob"_ustr, *this );
308 void SAL_CALL java_sql_PreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XArray >& /*x*/ )
310 ::dbtools::throwFeatureNotImplementedSQLException( u"XParameters::setArray"_ustr, *this );
314 void SAL_CALL java_sql_PreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const css::uno::Reference< css::sdbc::XRef >& /*x*/ )
316 ::dbtools::throwFeatureNotImplementedSQLException( u"XParameters::setRef"_ustr, *this );
320 void SAL_CALL java_sql_PreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const css::uno::Any& x, sal_Int32 targetSqlType, sal_Int32 scale )
322 m_aLogger.log( LogLevel::FINER, STR_LOG_OBJECT_NULL_PARAMETER, parameterIndex );
323 ::osl::MutexGuard aGuard( m_aMutex );
324 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
326 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
328 createStatement(t.pEnv);
330 // initialize temporary Variable
331 static const char * const cSignature = "(ILjava/lang/Object;II)V";
332 static const char * const cMethodName = "setObject";
333 // Java-Call
334 static jmethodID mID(nullptr);
335 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
337 jobject obj = nullptr;
338 switch(targetSqlType)
340 case DataType::DECIMAL:
341 case DataType::NUMERIC:
343 double nTemp = 0.0;
345 std::unique_ptr<java_math_BigDecimal> pBigDecimal;
346 if ( x >>= nTemp)
348 pBigDecimal.reset(new java_math_BigDecimal(nTemp));
349 //setDouble(parameterIndex,nTemp);
350 //return;
352 else
354 ORowSetValue aValue;
355 aValue.fill(x);
356 const OUString sValue = aValue.getString();
357 if ( !sValue.isEmpty() )
358 pBigDecimal.reset(new java_math_BigDecimal(sValue));
359 else
360 pBigDecimal.reset(new java_math_BigDecimal(0.0));
362 //obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
363 t.pEnv->CallVoidMethod( object, mID, parameterIndex,pBigDecimal->getJavaObject(),targetSqlType,scale);
364 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
365 return;
367 default:
368 obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
369 break;
371 t.pEnv->CallVoidMethod( object, mID, parameterIndex,obj,targetSqlType,scale);
372 t.pEnv->DeleteLocalRef(obj);
373 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
374 // and clean up
375 } //mID
376 } //t.pEnv
380 void SAL_CALL java_sql_PreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/, const OUString& /*typeName*/ )
382 m_aLogger.log( LogLevel::FINER, STR_LOG_OBJECT_NULL_PARAMETER, parameterIndex );
383 ::osl::MutexGuard aGuard( m_aMutex );
384 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
386 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
387 createStatement(t.pEnv);
388 static jmethodID mID(nullptr);
389 callVoidMethod_ThrowSQL<jobject>("setObject", "(ILjava/lang/Object;)V", mID, parameterIndex, nullptr);
393 void SAL_CALL java_sql_PreparedStatement::setObject( sal_Int32 parameterIndex, const css::uno::Any& x )
395 if(!::dbtools::implSetObject(this,parameterIndex,x))
397 const OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
398 STR_UNKNOWN_PARA_TYPE,
399 "$position$", OUString::number(parameterIndex)
400 ) );
401 ::dbtools::throwGenericSQLException(sError,*this);
406 void SAL_CALL java_sql_PreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x )
408 m_aLogger.log( LogLevel::FINER, STR_LOG_SHORT_PARAMETER, parameterIndex, x );
409 ::osl::MutexGuard aGuard( m_aMutex );
410 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
412 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
413 createStatement(t.pEnv);
414 static jmethodID mID(nullptr);
415 callVoidMethod_ThrowSQL("setShort", "(IS)V", mID, parameterIndex, x);
419 void SAL_CALL java_sql_PreparedStatement::setBytes( sal_Int32 parameterIndex, const css::uno::Sequence< sal_Int8 >& x )
421 m_aLogger.log( LogLevel::FINER, STR_LOG_BYTES_PARAMETER, parameterIndex );
422 ::osl::MutexGuard aGuard( m_aMutex );
423 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
425 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
427 createStatement(t.pEnv);
429 // initialize temporary Variable
430 static const char * const cSignature = "(I[B)V";
431 static const char * const cMethodName = "setBytes";
432 // Java-Call
433 static jmethodID mID(nullptr);
434 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
435 jbyteArray pByteArray = t.pEnv->NewByteArray(x.getLength());
436 jbyte * pData = reinterpret_cast<jbyte *>(
437 const_cast<sal_Int8 *>(x.getConstArray()));
438 // 4th param of Set*ArrayRegion changed from pointer to non-const to
439 // pointer to const between <http://docs.oracle.com/javase/6/docs/
440 // technotes/guides/jni/spec/functions.html#wp22933> and
441 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
442 // functions.html#wp22933>; work around that difference in a way
443 // that doesn't trigger loplugin:redundantcast
444 t.pEnv->SetByteArrayRegion(pByteArray,0,x.getLength(),pData);
445 t.pEnv->CallVoidMethod( object, mID, parameterIndex,pByteArray);
446 t.pEnv->DeleteLocalRef(pByteArray);
447 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
448 } //t.pEnv
452 void SAL_CALL java_sql_PreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream >& x, sal_Int32 length )
454 m_aLogger.log( LogLevel::FINER, STR_LOG_CHARSTREAM_PARAMETER, parameterIndex );
455 ::osl::MutexGuard aGuard( m_aMutex );
456 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
458 SDBThreadAttach t;
459 assert(t.pEnv && "Java environment has been deleted!");
461 createStatement(t.pEnv);
463 // initialize temporary variable
464 static const char * const cSignature = "(ILjava/io/InputStream;I)V";
465 static const char * const cMethodName = "setCharacterStream";
466 // Java-Call
467 static jmethodID mID(nullptr);
468 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
469 Sequence< sal_Int8 > aSeq;
470 if ( x.is() )
471 x->readBytes( aSeq, length );
472 sal_Int32 actualLength = aSeq.getLength();
474 jvalue args2[3];
475 jbyteArray pByteArray = t.pEnv->NewByteArray( actualLength );
476 jbyte * aSeqData = reinterpret_cast<jbyte *>(
477 const_cast<sal_Int8 *>(aSeq.getConstArray()));
478 // 4th param of Set*ArrayRegion changed from pointer to non-const to
479 // pointer to const between <http://docs.oracle.com/javase/6/docs/
480 // technotes/guides/jni/spec/functions.html#wp22933> and
481 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
482 // functions.html#wp22933>; work around that difference in a way
483 // that doesn't trigger loplugin:redundantcast
484 t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,aSeqData);
485 args2[0].l = pByteArray;
486 args2[1].i = 0;
487 args2[2].i = actualLength;
488 // Java-Call
489 jclass aClass = t.pEnv->FindClass("java/io/CharArrayInputStream");
490 static jmethodID mID2 = nullptr;
491 if ( !mID2 )
493 // initialize temporary variable
494 const char * const cSignatureStream = "([BII)V";
495 mID2 = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
497 jobject tempObj = nullptr;
498 if(mID2)
499 tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
501 t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,actualLength);
502 // and clean up
503 t.pEnv->DeleteLocalRef(pByteArray);
504 t.pEnv->DeleteLocalRef(tempObj);
505 t.pEnv->DeleteLocalRef(aClass);
506 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
507 } //t.pEnv
511 void SAL_CALL java_sql_PreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream >& x, sal_Int32 length )
513 m_aLogger.log( LogLevel::FINER, STR_LOG_BINARYSTREAM_PARAMETER, parameterIndex );
514 ::osl::MutexGuard aGuard( m_aMutex );
515 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
517 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
519 createStatement(t.pEnv);
520 // initialize temporary variable
521 static const char * const cSignature = "(ILjava/io/InputStream;I)V";
522 static const char * const cMethodName = "setBinaryStream";
523 // Java-Call
524 static jmethodID mID(nullptr);
525 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
527 Sequence< sal_Int8 > aSeq;
528 if ( x.is() )
529 x->readBytes( aSeq, length );
530 sal_Int32 actualLength = aSeq.getLength();
532 jvalue args2[3];
533 jbyteArray pByteArray = t.pEnv->NewByteArray(actualLength);
534 jbyte * aSeqData = reinterpret_cast<jbyte *>(
535 const_cast<sal_Int8 *>(aSeq.getConstArray()));
536 // 4th param of Set*ArrayRegion changed from pointer to non-const to
537 // pointer to const between <http://docs.oracle.com/javase/6/docs/
538 // technotes/guides/jni/spec/functions.html#wp22933> and
539 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
540 // functions.html#wp22933>; work around that difference in a way
541 // that doesn't trigger loplugin:redundantcast
542 t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,aSeqData);
543 args2[0].l = pByteArray;
544 args2[1].i = 0;
545 args2[2].i = actualLength;
547 // Java-Call
548 jclass aClass = t.pEnv->FindClass("java/io/ByteArrayInputStream");
549 static jmethodID mID2 = nullptr;
550 if ( !mID2 )
552 // initialize temporary variable
553 const char * const cSignatureStream = "([BII)V";
554 mID2 = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
556 jobject tempObj = nullptr;
557 if(mID2)
558 tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
559 t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,actualLength);
560 // and clean up
561 t.pEnv->DeleteLocalRef(pByteArray);
562 t.pEnv->DeleteLocalRef(tempObj);
563 t.pEnv->DeleteLocalRef(aClass);
564 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
566 } //t.pEnv
570 void SAL_CALL java_sql_PreparedStatement::clearParameters( )
572 m_aLogger.log( LogLevel::FINER, STR_LOG_CLEAR_PARAMETERS );
573 ::osl::MutexGuard aGuard( m_aMutex );
574 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
576 SDBThreadAttach t;
578 createStatement(t.pEnv);
580 static jmethodID mID(nullptr);
581 callVoidMethod_ThrowSQL("clearParameters",mID);
582 } //t.pEnv
585 void SAL_CALL java_sql_PreparedStatement::clearBatch( )
587 ::osl::MutexGuard aGuard( m_aMutex );
588 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
589 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
591 createStatement(t.pEnv);
592 static jmethodID mID(nullptr);
593 callVoidMethod_ThrowSQL("clearBatch",mID);
594 } //t.pEnv
598 void SAL_CALL java_sql_PreparedStatement::addBatch( )
600 ::osl::MutexGuard aGuard( m_aMutex );
601 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
602 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
604 createStatement(t.pEnv);
605 static jmethodID mID(nullptr);
606 callVoidMethod_ThrowSQL("addBatch", mID);
607 } //t.pEnv
611 css::uno::Sequence< sal_Int32 > SAL_CALL java_sql_PreparedStatement::executeBatch( )
613 ::osl::MutexGuard aGuard( m_aMutex );
614 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
615 css::uno::Sequence< sal_Int32 > aSeq;
616 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
617 createStatement(t.pEnv);
618 static jmethodID mID(nullptr);
619 jintArray out = static_cast<jintArray>(callObjectMethod(t.pEnv,"executeBatch","()[I", mID));
620 if(out)
622 jboolean p = false;
623 aSeq.realloc(t.pEnv->GetArrayLength(out));
624 memcpy(aSeq.getArray(),t.pEnv->GetIntArrayElements(out,&p),aSeq.getLength());
625 t.pEnv->DeleteLocalRef(out);
627 return aSeq;
630 css::uno::Reference< css::sdbc::XResultSetMetaData > SAL_CALL java_sql_PreparedStatement::getMetaData( )
632 ::osl::MutexGuard aGuard( m_aMutex );
633 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
634 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
635 createStatement(t.pEnv);
636 static jmethodID mID(nullptr);
637 jobject out = callObjectMethod(t.pEnv,"getMetaData","()Ljava/sql/ResultSetMetaData;", mID);
639 return out==nullptr ? nullptr : new java_sql_ResultSetMetaData( t.pEnv, out, *m_pConnection );
642 void SAL_CALL java_sql_PreparedStatement::acquire() noexcept
644 OStatement_BASE2::acquire();
647 void SAL_CALL java_sql_PreparedStatement::release() noexcept
649 OStatement_BASE2::release();
652 void java_sql_PreparedStatement::createStatement(JNIEnv* _pEnv)
654 ::osl::MutexGuard aGuard( m_aMutex );
655 checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
657 if( object || !_pEnv )
658 return;
660 // initialize temporary variable
661 static const char * const cMethodName = "prepareStatement";
663 jvalue args[1];
664 // convert Parameter
665 args[0].l = convertwchar_tToJavaString(_pEnv,m_sSqlStatement);
666 // Java-Call
667 jobject out = nullptr;
668 static jmethodID mID(nullptr);
669 if ( !mID )
671 static const char * const cSignature = "(Ljava/lang/String;II)Ljava/sql/PreparedStatement;";
672 mID = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature );
674 if( mID )
676 out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID, args[0].l ,m_nResultSetType,m_nResultSetConcurrency);
678 else
680 static jmethodID mID2 = nullptr;
681 if ( !mID2 )
683 static const char * const cSignature2 = "(Ljava/lang/String;)Ljava/sql/PreparedStatement;";
684 mID2 = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature2 );
686 if ( mID2 )
687 out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID2, args[0].l );
689 _pEnv->DeleteLocalRef(static_cast<jstring>(args[0].l));
690 ThrowLoggedSQLException( m_aLogger, _pEnv, *this );
691 if ( out )
692 object = _pEnv->NewGlobalRef( out );
696 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */