1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "java/sql/PreparedStatement.hxx"
27 #include "java/sql/ResultSet.hxx"
28 #include "java/sql/ResultSetMetaData.hxx"
29 #include "java/sql/Connection.hxx"
30 #include "java/sql/Timestamp.hxx"
31 #include "java/math/BigDecimal.hxx"
32 #include "java/tools.hxx"
33 #include <cppuhelper/typeprovider.hxx>
34 #include <comphelper/sequence.hxx>
35 #include "connectivity/dbtools.hxx"
36 #include "connectivity/FValue.hxx"
37 #include "connectivity/dbexception.hxx"
38 #include "resource/jdbc_log.hrc"
39 #include "resource/common_res.hrc"
40 #include "resource/sharedresources.hxx"
41 #include "java/LocalRef.hxx"
42 #include <string.h>
43 
44 using namespace connectivity;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::lang;
50 
51 //**************************************************************
52 //************ Class: java.sql.PreparedStatement
53 //**************************************************************
54 IMPLEMENT_SERVICE_INFO(java_sql_PreparedStatement,"com.sun.star.sdbcx.JPreparedStatement","com.sun.star.sdbc.PreparedStatement");
55 
java_sql_PreparedStatement(JNIEnv * pEnv,java_sql_Connection & _rCon,const::rtl::OUString & sql)56 java_sql_PreparedStatement::java_sql_PreparedStatement( JNIEnv * pEnv, java_sql_Connection& _rCon, const ::rtl::OUString& sql )
57 	: OStatement_BASE2( pEnv, _rCon )
58 {
59 	m_sSqlStatement = sql;
60 }
61 // -----------------------------------------------------------------------------
62 jclass java_sql_PreparedStatement::theClass = 0;
63 
~java_sql_PreparedStatement()64 java_sql_PreparedStatement::~java_sql_PreparedStatement()
65 {
66 }
67 // -----------------------------------------------------------------------------
68 
getMyClass() const69 jclass java_sql_PreparedStatement::getMyClass() const
70 {
71 	// die Klasse muss nur einmal geholt werden, daher statisch
72 	if( !theClass )
73         theClass = findMyClass("java/sql/PreparedStatement");
74 	return theClass;
75 }
76 // -------------------------------------------------------------------------
77 
queryInterface(const::com::sun::star::uno::Type & rType)78 ::com::sun::star::uno::Any SAL_CALL java_sql_PreparedStatement::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
79 {
80 	::com::sun::star::uno::Any aRet = OStatement_BASE2::queryInterface(rType);
81 	return aRet.hasValue() ? aRet : ::cppu::queryInterface(	rType,
82 										static_cast< XPreparedStatement*>(this),
83 										static_cast< XParameters*>(this),
84 										static_cast< XResultSetMetaDataSupplier*>(this),
85 										static_cast< XPreparedBatchExecution*>(this));
86 }
87 // -------------------------------------------------------------------------
getTypes()88 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL java_sql_PreparedStatement::getTypes(  ) throw(::com::sun::star::uno::RuntimeException)
89 {
90 	::cppu::OTypeCollection aTypes(	::getCppuType( (const ::com::sun::star::uno::Reference< XPreparedStatement > *)0 ),
91 									::getCppuType( (const ::com::sun::star::uno::Reference< XParameters > *)0 ),
92 									::getCppuType( (const ::com::sun::star::uno::Reference< XResultSetMetaDataSupplier > *)0 ),
93 									::getCppuType( (const ::com::sun::star::uno::Reference< XPreparedBatchExecution > *)0 ));
94 
95 	return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE2::getTypes());
96 }
97 // -------------------------------------------------------------------------
98 
execute()99 sal_Bool SAL_CALL java_sql_PreparedStatement::execute(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
100 {
101     m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED );
102     ::osl::MutexGuard aGuard( m_aMutex );
103 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
104 
105     static jmethodID mID(NULL);
106     return callBooleanMethod( "execute", mID );
107 }
108 // -------------------------------------------------------------------------
109 
executeUpdate()110 sal_Int32 SAL_CALL java_sql_PreparedStatement::executeUpdate(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
111 {
112     ::osl::MutexGuard aGuard( m_aMutex );
113 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
114     m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_UPDATE );
115     static jmethodID mID(NULL);
116     return callIntMethod("executeUpdate",mID);
117 }
118 // -------------------------------------------------------------------------
119 
setString(sal_Int32 parameterIndex,const::rtl::OUString & x)120 void SAL_CALL java_sql_PreparedStatement::setString( sal_Int32 parameterIndex, const ::rtl::OUString& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
121 {
122     ::osl::MutexGuard aGuard( m_aMutex );
123 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
124     m_aLogger.log( LogLevel::FINER, STR_LOG_STRING_PARAMETER, parameterIndex, x );
125 
126     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
127 	{		// temporaere Variable initialisieren
128 		createStatement(t.pEnv);
129 		static const char * cSignature = "(ILjava/lang/String;)V";
130 		static const char * cMethodName = "setString";
131 		// Java-Call absetzen
132 		static jmethodID mID(NULL);
133         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
134 		jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,x));
135 		t.pEnv->CallVoidMethod( object, mID, parameterIndex,str.get());
136 		// und aufraeumen
137 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
138 	} //t.pEnv
139 }
140 // -------------------------------------------------------------------------
141 
getConnection()142 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL java_sql_PreparedStatement::getConnection(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
143 {
144 	return (Reference< XConnection >)m_pConnection;
145 }
146 // -------------------------------------------------------------------------
147 
executeQuery()148 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > SAL_CALL java_sql_PreparedStatement::executeQuery(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
149 {
150     m_aLogger.log( LogLevel::FINE, STR_LOG_EXECUTING_PREPARED_QUERY );
151     ::osl::MutexGuard aGuard( m_aMutex );
152 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
153 
154     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
155 	createStatement(t.pEnv);
156     static jmethodID mID(NULL);
157     jobject out = callResultSetMethod(t.env(),"executeQuery",mID);
158 
159 	return out==0 ? 0 : new java_sql_ResultSet( t.pEnv, out, m_aLogger, *m_pConnection,this);
160 }
161 // -------------------------------------------------------------------------
162 
setBoolean(sal_Int32 parameterIndex,sal_Bool x)163 void SAL_CALL java_sql_PreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
164 {
165     m_aLogger.log( LogLevel::FINER, STR_LOG_BOOLEAN_PARAMETER, parameterIndex, x );
166     ::osl::MutexGuard aGuard( m_aMutex );
167 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
168 
169     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
170     createStatement(t.pEnv);
171     static jmethodID mID(NULL);
172     callVoidMethod("setBoolean", "(IZ)V", mID, parameterIndex, x);
173 }
174 // -------------------------------------------------------------------------
175 
setByte(sal_Int32 parameterIndex,sal_Int8 x)176 void SAL_CALL java_sql_PreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
177 {
178     m_aLogger.log( LogLevel::FINER, STR_LOG_BYTE_PARAMETER, parameterIndex, (sal_Int32)x );
179     ::osl::MutexGuard aGuard( m_aMutex );
180 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
181 
182     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
183     createStatement(t.pEnv);
184     static jmethodID mID(NULL);
185     callVoidMethod("setByte", "(IB)V", mID, parameterIndex, x);
186 }
187 // -------------------------------------------------------------------------
188 
setDate(sal_Int32 parameterIndex,const::com::sun::star::util::Date & x)189 void SAL_CALL java_sql_PreparedStatement::setDate( sal_Int32 parameterIndex, const ::com::sun::star::util::Date& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
190 {
191     m_aLogger.log( LogLevel::FINER, STR_LOG_DATE_PARAMETER, parameterIndex, x );
192     ::osl::MutexGuard aGuard( m_aMutex );
193 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
194 
195     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
196     createStatement(t.pEnv);
197     java_sql_Date aT(x);
198     static jmethodID mID(NULL);
199     callVoidMethod("setDate", "(ILjava/sql/Date;)V", mID, parameterIndex, aT.getJavaObject());
200 }
201 // -------------------------------------------------------------------------
202 
203 
setTime(sal_Int32 parameterIndex,const::com::sun::star::util::Time & x)204 void SAL_CALL java_sql_PreparedStatement::setTime( sal_Int32 parameterIndex, const ::com::sun::star::util::Time& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
205 {
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);
209 
210     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
211     createStatement(t.pEnv);
212     java_sql_Time aT(x);
213     static jmethodID mID(NULL);
214     callVoidMethod("setTime", "(ILjava/sql/Time;)V", mID, parameterIndex, aT.getJavaObject());
215 }
216 // -------------------------------------------------------------------------
217 
setTimestamp(sal_Int32 parameterIndex,const::com::sun::star::util::DateTime & x)218 void SAL_CALL java_sql_PreparedStatement::setTimestamp( sal_Int32 parameterIndex, const ::com::sun::star::util::DateTime& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
219 {
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);
223 
224     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
225     createStatement(t.pEnv);
226     static jmethodID mID(NULL);
227     java_sql_Timestamp aD(x);
228     callVoidMethod("setTimestamp", "(ILjava/sql/Timestamp;)V", mID, parameterIndex, aD.getJavaObject());
229 }
230 // -------------------------------------------------------------------------
setDouble(sal_Int32 parameterIndex,double x)231 void SAL_CALL java_sql_PreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
232 {
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);
236 
237     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
238     createStatement(t.pEnv);
239     static jmethodID mID(NULL);
240     callVoidMethod("setDouble", "(ID)V", mID, parameterIndex, x);
241 }
242 // -------------------------------------------------------------------------
243 
setFloat(sal_Int32 parameterIndex,float x)244 void SAL_CALL java_sql_PreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
245 {
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);
249 
250     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
251     createStatement(t.pEnv);
252     static jmethodID mID(NULL);
253     callVoidMethod("setFloat", "(IF)V", mID, parameterIndex, x);
254 }
255 // -------------------------------------------------------------------------
256 
setInt(sal_Int32 parameterIndex,sal_Int32 x)257 void SAL_CALL java_sql_PreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
258 {
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);
262 
263     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
264     createStatement(t.pEnv);
265     static jmethodID mID(NULL);
266     callVoidMethod("setInt", "(II)V", mID, parameterIndex, x);
267 }
268 // -------------------------------------------------------------------------
269 
setLong(sal_Int32 parameterIndex,sal_Int64 x)270 void SAL_CALL java_sql_PreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
271 {
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);
275 
276     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
277     createStatement(t.pEnv);
278     static jmethodID mID(NULL);
279     callVoidMethod("setLong", "(IJ)V", mID, parameterIndex, x);
280 }
281 // -------------------------------------------------------------------------
282 
setNull(sal_Int32 parameterIndex,sal_Int32 sqlType)283 void SAL_CALL java_sql_PreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
284 {
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);
288 
289     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
290     createStatement(t.pEnv);
291     static jmethodID mID(NULL);
292     callVoidMethod("setNull", "(II)V", mID, parameterIndex, sqlType);
293 }
294 // -------------------------------------------------------------------------
295 
setClob(sal_Int32,const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XClob> &)296 void SAL_CALL java_sql_PreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XClob >& /*x*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
297 {
298     ::dbtools::throwFeatureNotImplementedException( "XParameters::setClob", *this );
299 }
300 // -------------------------------------------------------------------------
301 
setBlob(sal_Int32,const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XBlob> &)302 void SAL_CALL java_sql_PreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob >& /*x*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
303 {
304     ::dbtools::throwFeatureNotImplementedException( "XParameters::setBlob", *this );
305 }
306 // -------------------------------------------------------------------------
307 
setArray(sal_Int32,const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XArray> &)308 void SAL_CALL java_sql_PreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XArray >& /*x*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
309 {
310     ::dbtools::throwFeatureNotImplementedException( "XParameters::setArray", *this );
311 }
312 // -------------------------------------------------------------------------
313 
setRef(sal_Int32,const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XRef> &)314 void SAL_CALL java_sql_PreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRef >& /*x*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
315 {
316     ::dbtools::throwFeatureNotImplementedException( "XParameters::setRef", *this );
317 }
318 // -------------------------------------------------------------------------
319 
setObjectWithInfo(sal_Int32 parameterIndex,const::com::sun::star::uno::Any & x,sal_Int32 targetSqlType,sal_Int32 scale)320 void SAL_CALL java_sql_PreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const ::com::sun::star::uno::Any& x, sal_Int32 targetSqlType, sal_Int32 scale ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
321 {
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);
325 
326     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
327 	{
328 		createStatement(t.pEnv);
329 
330 		// temporaere Variable initialisieren
331 		static const char * cSignature = "(ILjava/lang/Object;II)V";
332 		static const char * cMethodName = "setObject";
333 		// Java-Call absetzen
334 		static jmethodID mID(NULL);
335         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
336 		{
337             jobject obj = NULL;
338             double nTemp = 0.0;
339             switch(targetSqlType)
340             {
341                 case DataType::DECIMAL:
342 			    case DataType::NUMERIC:
343                     {
344                         ::std::auto_ptr<java_math_BigDecimal> pBigDecimal;
345                         if ( x >>= nTemp)
346                         {
347                             pBigDecimal.reset(new java_math_BigDecimal(nTemp));
348                             //setDouble(parameterIndex,nTemp);
349                             //return;
350                         }
351                         else
352                         {
353                             ORowSetValue aValue;
354                             aValue.fill(x);
355                             const ::rtl::OUString sValue = aValue;
356                             if ( sValue.getLength() )
357                                 pBigDecimal.reset(new java_math_BigDecimal(sValue));
358                             else
359                                 pBigDecimal.reset(new java_math_BigDecimal(0.0));
360                         }
361                             //obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
362                         t.pEnv->CallVoidMethod( object, mID, parameterIndex,pBigDecimal->getJavaObject(),targetSqlType,scale);
363                         ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
364                         return;
365                     }
366                 default:
367                     obj = convertwchar_tToJavaString(t.pEnv,::comphelper::getString(x));
368                     break;
369             }
370 			t.pEnv->CallVoidMethod( object, mID, parameterIndex,obj,targetSqlType,scale);
371             t.pEnv->DeleteLocalRef(obj);
372 			ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
373 			// und aufraeumen
374 		} //mID
375 	} //t.pEnv
376 }
377 // -------------------------------------------------------------------------
378 
setObjectNull(sal_Int32 parameterIndex,sal_Int32,const::rtl::OUString &)379 void SAL_CALL java_sql_PreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/, const ::rtl::OUString& /*typeName*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
380 {
381     m_aLogger.log( LogLevel::FINER, STR_LOG_OBJECT_NULL_PARAMETER, parameterIndex );
382     ::osl::MutexGuard aGuard( m_aMutex );
383 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
384 
385     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
386     createStatement(t.pEnv);
387     static jmethodID mID(NULL);
388     callVoidMethod<jobject>("setObject", "(ILjava/lang/Object;)V", mID, parameterIndex, NULL);
389 }
390 // -------------------------------------------------------------------------
391 
setObject(sal_Int32 parameterIndex,const::com::sun::star::uno::Any & x)392 void SAL_CALL java_sql_PreparedStatement::setObject( sal_Int32 parameterIndex, const ::com::sun::star::uno::Any& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
393 {
394 	if(!::dbtools::implSetObject(this,parameterIndex,x))
395 	{
396         const ::rtl::OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
397                 STR_UNKNOWN_PARA_TYPE,
398                 "$position$", ::rtl::OUString::valueOf(parameterIndex)
399              ) );
400 		::dbtools::throwGenericSQLException(sError,*this);
401 	}
402 }
403 // -------------------------------------------------------------------------
404 
setShort(sal_Int32 parameterIndex,sal_Int16 x)405 void SAL_CALL java_sql_PreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
406 {
407     m_aLogger.log( LogLevel::FINER, STR_LOG_SHORT_PARAMETER, parameterIndex, x );
408     ::osl::MutexGuard aGuard( m_aMutex );
409 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
410 
411     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
412     createStatement(t.pEnv);
413     static jmethodID mID(NULL);
414     callVoidMethod("setShort", "(IS)V", mID, parameterIndex, x);
415 }
416 // -------------------------------------------------------------------------
417 
setBytes(sal_Int32 parameterIndex,const::com::sun::star::uno::Sequence<sal_Int8> & x)418 void SAL_CALL java_sql_PreparedStatement::setBytes( sal_Int32 parameterIndex, const ::com::sun::star::uno::Sequence< sal_Int8 >& x ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
419 {
420     m_aLogger.log( LogLevel::FINER, STR_LOG_BYTES_PARAMETER, parameterIndex );
421     ::osl::MutexGuard aGuard( m_aMutex );
422 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
423 
424     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
425 	{
426 		createStatement(t.pEnv);
427 
428 		// temporaere Variable initialisieren
429 		static const char * cSignature = "(I[B)V";
430 		static const char * cMethodName = "setBytes";
431 		// Java-Call absetzen
432 		static jmethodID mID(NULL);
433         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
434 		jbyteArray pByteArray = t.pEnv->NewByteArray(x.getLength());
435 		t.pEnv->SetByteArrayRegion(pByteArray,0,x.getLength(),(jbyte*)x.getConstArray());
436 		t.pEnv->CallVoidMethod( object, mID, parameterIndex,pByteArray);
437 		t.pEnv->DeleteLocalRef(pByteArray);
438 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
439 	} //t.pEnv
440 }
441 // -------------------------------------------------------------------------
442 
setCharacterStream(sal_Int32 parameterIndex,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)443 void SAL_CALL java_sql_PreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
444 {
445     m_aLogger.log( LogLevel::FINER, STR_LOG_CHARSTREAM_PARAMETER, parameterIndex );
446     ::osl::MutexGuard aGuard( m_aMutex );
447 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
448 
449     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
450 	{
451 		createStatement(t.pEnv);
452 
453 		// temporaere Variable initialisieren
454 		static const char * cSignature = "(ILjava/io/InputStream;I)V";
455 		static const char * cMethodName = "setCharacterStream";
456 		// Java-Call absetzen
457 		static jmethodID mID(NULL);
458         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
459 		Sequence< sal_Int8 > aSeq;
460         if ( x.is() )
461             x->readBytes( aSeq, length );
462         sal_Int32 actualLength = aSeq.getLength();
463 
464 		jvalue args2[3];
465 		jbyteArray pByteArray = t.pEnv->NewByteArray( actualLength );
466 		t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,(jbyte*)aSeq.getConstArray());
467 		args2[0].l =  pByteArray;
468 		args2[1].i =  0;
469 		args2[2].i =  actualLength;
470 		// temporaere Variable initialisieren
471 		const char * cSignatureStream = "([BII)V";
472 		// Java-Call absetzen
473 		jclass aClass = t.pEnv->FindClass("java/io/CharArrayInputStream");
474 		static jmethodID mID2 = NULL;
475 		if ( !mID2  )
476 			mID2  = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
477 		jobject tempObj = NULL;
478 		if(mID2)
479 			tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
480 
481 		t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,actualLength);
482 		// und aufraeumen
483 		t.pEnv->DeleteLocalRef(pByteArray);
484 		t.pEnv->DeleteLocalRef(tempObj);
485 		t.pEnv->DeleteLocalRef(aClass);
486 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
487 	} //t.pEnv
488 }
489 // -------------------------------------------------------------------------
490 
setBinaryStream(sal_Int32 parameterIndex,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)491 void SAL_CALL java_sql_PreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
492 {
493     m_aLogger.log( LogLevel::FINER, STR_LOG_BINARYSTREAM_PARAMETER, parameterIndex );
494     ::osl::MutexGuard aGuard( m_aMutex );
495 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
496 
497     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
498 	{
499 		createStatement(t.pEnv);
500 		// temporaere Variable initialisieren
501 		static const char * cSignature = "(ILjava/io/InputStream;I)V";
502 		static const char * cMethodName = "setBinaryStream";
503 		// Java-Call absetzen
504 		static jmethodID mID(NULL);
505         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
506 		{
507 			Sequence< sal_Int8 > aSeq;
508             if ( x.is() )
509                 x->readBytes( aSeq, length );
510             sal_Int32 actualLength = aSeq.getLength();
511 
512 			jvalue args2[3];
513             jbyteArray pByteArray = t.pEnv->NewByteArray(actualLength);
514 			t.pEnv->SetByteArrayRegion(pByteArray,0,actualLength,(jbyte*)aSeq.getConstArray());
515 			args2[0].l =  pByteArray;
516 			args2[1].i =  0;
517 			args2[2].i =  (sal_Int32)actualLength;
518 
519 			// temporaere Variable initialisieren
520 			const char * cSignatureStream = "([BII)V";
521 			// Java-Call absetzen
522 			jclass aClass = t.pEnv->FindClass("java/io/ByteArrayInputStream");
523 			static jmethodID mID2 = NULL;
524 			if ( !mID2  )
525 				mID2  = t.pEnv->GetMethodID( aClass, "<init>", cSignatureStream );
526 			jobject tempObj = NULL;
527 			if(mID2)
528 				tempObj = t.pEnv->NewObjectA( aClass, mID2, args2 );
529 			t.pEnv->CallVoidMethod( object, mID, parameterIndex,tempObj,(sal_Int32)actualLength);
530 			// und aufraeumen
531 			t.pEnv->DeleteLocalRef(pByteArray);
532 			t.pEnv->DeleteLocalRef(tempObj);
533 			t.pEnv->DeleteLocalRef(aClass);
534 			ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
535 		}
536 	} //t.pEnv
537 }
538 // -------------------------------------------------------------------------
539 
clearParameters()540 void SAL_CALL java_sql_PreparedStatement::clearParameters(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
541 {
542     m_aLogger.log( LogLevel::FINER, STR_LOG_CLEAR_PARAMETERS );
543     ::osl::MutexGuard aGuard( m_aMutex );
544 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
545 
546     SDBThreadAttach t;
547 	{
548 		createStatement(t.pEnv);
549 
550         static jmethodID mID(NULL);
551         callVoidMethod("clearParameters",mID);
552 	} //t.pEnv
553 }
554 // -------------------------------------------------------------------------
clearBatch()555 void SAL_CALL java_sql_PreparedStatement::clearBatch(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
556 {
557     ::osl::MutexGuard aGuard( m_aMutex );
558 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
559     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
560 	{
561 		createStatement(t.pEnv);
562         static jmethodID mID(NULL);
563         callVoidMethod("clearBatch",mID);
564 	} //t.pEnv
565 }
566 // -------------------------------------------------------------------------
567 
addBatch()568 void SAL_CALL java_sql_PreparedStatement::addBatch( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
569 {
570     ::osl::MutexGuard aGuard( m_aMutex );
571 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
572     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
573 	{
574 		createStatement(t.pEnv);
575         static jmethodID mID(NULL);
576         callVoidMethod("addBatch",mID);
577 	} //t.pEnv
578 }
579 // -------------------------------------------------------------------------
580 
executeBatch()581 ::com::sun::star::uno::Sequence< sal_Int32 > SAL_CALL java_sql_PreparedStatement::executeBatch(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
582 {
583     ::osl::MutexGuard aGuard( m_aMutex );
584 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
585 	::com::sun::star::uno::Sequence< sal_Int32 > aSeq;
586     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
587     createStatement(t.pEnv);
588     static jmethodID mID(NULL);
589     jintArray out = (jintArray)callObjectMethod(t.pEnv,"executeBatch","()[I", mID);
590 	if(out)
591 	{
592 		jboolean p = sal_False;
593 		aSeq.realloc(t.pEnv->GetArrayLength(out));
594 		memcpy(aSeq.getArray(),t.pEnv->GetIntArrayElements(out,&p),aSeq.getLength());
595 		t.pEnv->DeleteLocalRef(out);
596 	}
597 	return aSeq;
598 }
599 // -------------------------------------------------------------------------
getMetaData()600 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData > SAL_CALL java_sql_PreparedStatement::getMetaData(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
601 {
602     ::osl::MutexGuard aGuard( m_aMutex );
603 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
604     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
605     createStatement(t.pEnv);
606     static jmethodID mID(NULL);
607     jobject out = callObjectMethod(t.pEnv,"getMetaData","()Ljava/sql/ResultSetMetaData;", mID);
608 
609 	return out==0 ? 0 : new java_sql_ResultSetMetaData( t.pEnv, out, m_aLogger,*m_pConnection );
610 }
611 // -----------------------------------------------------------------------------
acquire()612 void SAL_CALL java_sql_PreparedStatement::acquire() throw()
613 {
614 	OStatement_BASE2::acquire();
615 }
616 // -----------------------------------------------------------------------------
release()617 void SAL_CALL java_sql_PreparedStatement::release() throw()
618 {
619 	OStatement_BASE2::release();
620 }
621 // -----------------------------------------------------------------------------
createStatement(JNIEnv * _pEnv)622 void java_sql_PreparedStatement::createStatement(JNIEnv* _pEnv)
623 {
624 	::osl::MutexGuard aGuard( m_aMutex );
625 	checkDisposed(java_sql_Statement_BASE::rBHelper.bDisposed);
626 
627 	if( !object && _pEnv ){
628 		// temporaere Variable initialisieren
629 		static const char * cSignature = "(Ljava/lang/String;II)Ljava/sql/PreparedStatement;";
630 		static const char * cMethodName = "prepareStatement";
631 
632 		jvalue args[1];
633 		// Parameter konvertieren
634 		args[0].l = convertwchar_tToJavaString(_pEnv,m_sSqlStatement);
635 		// Java-Call absetzen
636 		jobject out = NULL;
637 		static jmethodID mID(NULL);
638 		if ( !mID  )
639 			mID  = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature );
640 		if( mID )
641 		{
642 			out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID, args[0].l ,m_nResultSetType,m_nResultSetConcurrency);
643 		}
644 		else
645 		{
646 			static const char * cSignature2 = "(Ljava/lang/String;)Ljava/sql/PreparedStatement;";
647 			static jmethodID mID2 = NULL;
648 			if ( !mID2)
649 				mID2  = _pEnv->GetMethodID( m_pConnection->getMyClass(), cMethodName, cSignature2 );OSL_ENSURE(mID,"Unknown method id!");
650 			if ( mID2 )
651 				out = _pEnv->CallObjectMethod( m_pConnection->getJavaObject(), mID2, args[0].l );
652 		}
653 		_pEnv->DeleteLocalRef((jstring)args[0].l);
654 		ThrowLoggedSQLException( m_aLogger, _pEnv, *this );
655 		if ( out )
656 			object = _pEnv->NewGlobalRef( out );
657 	} //t.pEnv
658 }
659 // -----------------------------------------------------------------------------
660 
661 
662 
663