bump product version to 4.1.6.2
[LibreOffice.git] / mysqlc / source / mysqlc_statement.cxx
bloba275689a2a4a45734f115efac10bb50f0970ca27
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 .
19 #include <stdio.h>
20 #include "mysqlc_connection.hxx"
21 #include "mysqlc_propertyids.hxx"
22 #include "mysqlc_resultset.hxx"
23 #include "mysqlc_statement.hxx"
24 #include "mysqlc_general.hxx"
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/sdbc/FetchDirection.hpp>
28 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
29 #include <com/sun/star/sdbc/ResultSetType.hpp>
31 #include <cppconn/connection.h>
32 #include <cppconn/exception.h>
33 #include <cppconn/statement.h>
34 #include <cppuhelper/typeprovider.hxx>
35 #include <osl/diagnose.h>
36 #include <osl/thread.h>
38 #define USE_CPP_CONN 1
40 using namespace connectivity::mysqlc;
41 //------------------------------------------------------------------------------
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::sdbc;
46 using namespace com::sun::star::sdbcx;
47 using namespace com::sun::star::container;
48 using namespace com::sun::star::io;
49 using namespace com::sun::star::util;
50 using ::osl::MutexGuard;
52 #include <stdio.h>
54 /* {{{ OConnection::OCommonStatement() -I- */
55 OCommonStatement::OCommonStatement(OConnection* _pConnection, sql::Statement *_cppStatement)
56 :OCommonStatement_IBase(m_aMutex)
57 ,OPropertySetHelper(OCommonStatement_IBase::rBHelper)
58 ,OStatement_CBase( (::cppu::OWeakObject*)_pConnection, this )
59 ,m_pConnection(_pConnection)
60 ,cppStatement(_cppStatement)
61 ,rBHelper(OCommonStatement_IBase::rBHelper)
63 OSL_TRACE("OCommonStatement::OCommonStatement");
64 m_pConnection->acquire();
66 /* }}} */
69 /* {{{ OConnection::~OCommonStatement() -I- */
70 OCommonStatement::~OCommonStatement()
72 OSL_TRACE("OCommonStatement::~OCommonStatement");
74 /* }}} */
77 /* {{{ OConnection::disposeResultSet() -I- */
78 void OCommonStatement::disposeResultSet()
80 OSL_TRACE("OCommonStatement::disposeResultSet");
81 // free the cursor if alive
82 delete cppStatement;
83 cppStatement = NULL;
85 /* }}} */
88 /* {{{ OConnection::disposing() -I- */
89 void OCommonStatement::disposing()
91 OSL_TRACE("OCommonStatement::disposing");
92 MutexGuard aGuard(m_aMutex);
94 disposeResultSet();
96 if (m_pConnection) {
97 m_pConnection->release();
98 m_pConnection = NULL;
100 delete cppStatement;
102 dispose_ChildImpl();
103 OCommonStatement_IBase::disposing();
105 /* }}} */
108 /* {{{ OCommonStatement::queryInterface() -I- */
109 Any SAL_CALL OCommonStatement::queryInterface(const Type & rType)
110 throw(RuntimeException)
112 OSL_TRACE("OCommonStatement::queryInterface");
113 Any aRet = OCommonStatement_IBase::queryInterface(rType);
114 if (!aRet.hasValue()) {
115 aRet = OPropertySetHelper::queryInterface(rType);
117 return aRet;
119 /* }}} */
122 /* {{{ OCommonStatement::getTypes() -I- */
123 Sequence< Type > SAL_CALL OCommonStatement::getTypes()
124 throw(RuntimeException)
126 OSL_TRACE("OCommonStatement::getTypes");
127 ::cppu::OTypeCollection aTypes( ::getCppuType( (const Reference< XMultiPropertySet > *)0 ),
128 ::getCppuType( (const Reference< XFastPropertySet > *)0 ),
129 ::getCppuType( (const Reference< XPropertySet > *)0 ));
131 return concatSequences(aTypes.getTypes(), OCommonStatement_IBase::getTypes());
133 /* }}} */
136 /* {{{ OCommonStatement::cancel() -I- */
137 void SAL_CALL OCommonStatement::cancel()
138 throw(RuntimeException)
140 OSL_TRACE("OCommonStatement::cancel");
141 MutexGuard aGuard(m_aMutex);
142 checkDisposed(rBHelper.bDisposed);
143 // cancel the current sql statement
145 /* }}} */
148 /* {{{ OCommonStatement::close() -I- */
149 void SAL_CALL OCommonStatement::close()
150 throw(SQLException, RuntimeException)
152 OSL_TRACE("OCommonStatement::close");
154 We need a block for the checkDisposed call.
155 After the check we can call dispose() as we are not under lock ??
158 MutexGuard aGuard(m_aMutex);
159 checkDisposed(rBHelper.bDisposed);
161 dispose();
163 /* }}} */
166 /* {{{ OStatement::clearBatch() -I- */
167 void SAL_CALL OStatement::clearBatch()
168 throw(SQLException, RuntimeException)
170 OSL_TRACE("OStatement::clearBatch");
171 // if you support batches clear it here
173 /* }}} */
176 /* {{{ OStatement::execute() -I- */
177 sal_Bool SAL_CALL OCommonStatement::execute(const OUString& sql)
178 throw(SQLException, RuntimeException)
180 OSL_TRACE("OCommonStatement::execute");
181 MutexGuard aGuard(m_aMutex);
182 checkDisposed(rBHelper.bDisposed);
183 const OUString sSqlStatement = m_pConnection->transFormPreparedStatement( sql );
185 sal_Bool success = false;
186 try {
187 success = cppStatement->execute(OUStringToOString(sSqlStatement, m_pConnection->getConnectionSettings().encoding).getStr())? sal_True:sal_False;
188 } catch (const sql::SQLException &e) {
189 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
191 return success;
193 /* }}} */
196 /* {{{ OStatement::executeQuery() -I- */
197 Reference< XResultSet > SAL_CALL OCommonStatement::executeQuery(const OUString& sql)
198 throw(SQLException, RuntimeException)
200 OSL_TRACE("OCommonStatement::executeQuery");
202 MutexGuard aGuard(m_aMutex);
203 checkDisposed(rBHelper.bDisposed);
204 const OUString sSqlStatement = m_pConnection->transFormPreparedStatement(sql);
206 Reference< XResultSet > xResultSet;
207 try {
208 std::auto_ptr< sql::ResultSet > rset(cppStatement->executeQuery(OUStringToOString(sSqlStatement, m_pConnection->getConnectionEncoding()).getStr()));
209 xResultSet = new OResultSet(this, rset.get(), m_pConnection->getConnectionEncoding());
210 rset.release();
211 } catch (const sql::SQLException &e) {
212 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
214 return xResultSet;
216 /* }}} */
219 /* {{{ OStatement::getConnection() -I- */
220 Reference< XConnection > SAL_CALL OCommonStatement::getConnection()
221 throw(SQLException, RuntimeException)
223 OSL_TRACE("OCommonStatement::getConnection");
224 MutexGuard aGuard(m_aMutex);
225 checkDisposed(rBHelper.bDisposed);
227 // just return(our connection here
228 return ((Reference< XConnection >)m_pConnection);
230 /* }}} */
233 /* {{{ OStatement::getUpdateCount() -I- */
234 sal_Int32 SAL_CALL OCommonStatement::getUpdateCount()
235 throw(SQLException, RuntimeException)
237 OSL_TRACE("OCommonStatement::getUpdateCount");
238 return 0;
240 /* }}} */
243 /* {{{ OStatement::queryInterface() -I- */
244 Any SAL_CALL OStatement::queryInterface(const Type & rType)
245 throw(RuntimeException)
247 OSL_TRACE("OStatement::queryInterface");
248 Any aRet = ::cppu::queryInterface(rType,static_cast< XBatchExecution*> (this));
249 if (!aRet.hasValue()) {
250 aRet = OCommonStatement::queryInterface(rType);
252 return (aRet);
254 /* }}} */
257 /* {{{ OStatement::addBatch() -I- */
258 void SAL_CALL OStatement::addBatch(const OUString& sql)
259 throw(SQLException, RuntimeException)
261 OSL_TRACE("OStatement::addBatch");
262 MutexGuard aGuard(m_aMutex);
263 checkDisposed(rBHelper.bDisposed);
265 m_aBatchList.push_back(sql);
267 /* }}} */
270 /* {{{ OStatement::executeBatch() -I- */
271 Sequence< sal_Int32 > SAL_CALL OStatement::executeBatch()
272 throw(SQLException, RuntimeException)
274 OSL_TRACE("OStatement::executeBatch");
275 MutexGuard aGuard(m_aMutex);
276 checkDisposed(rBHelper.bDisposed);
278 Sequence< sal_Int32 > aRet = Sequence< sal_Int32 >();
279 return aRet;
281 /* }}} */
284 /* {{{ OCommonStatement::executeUpdate() -I- */
285 sal_Int32 SAL_CALL OCommonStatement::executeUpdate(const OUString& sql)
286 throw(SQLException, RuntimeException)
288 OSL_TRACE("OCommonStatement::executeUpdate");
289 MutexGuard aGuard(m_aMutex);
290 checkDisposed(rBHelper.bDisposed);
291 const OUString sSqlStatement = m_pConnection->transFormPreparedStatement(sql);
293 sal_Int32 affectedRows = 0;
294 try {
295 affectedRows = cppStatement->executeUpdate(OUStringToOString(sSqlStatement, m_pConnection->getConnectionEncoding()).getStr());
296 } catch (const sql::SQLException &e) {
297 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
299 return affectedRows;
301 /* }}} */
304 /* {{{ OCommonStatement::getResultSet() -I- */
305 Reference< XResultSet > SAL_CALL OCommonStatement::getResultSet()
306 throw(SQLException, RuntimeException)
308 OSL_TRACE("OCommonStatement::getResultSet");
309 MutexGuard aGuard(m_aMutex);
310 checkDisposed(rBHelper.bDisposed);
312 Reference< XResultSet > xResultSet;
313 try {
314 std::auto_ptr< sql::ResultSet > rset(cppStatement->getResultSet());
315 xResultSet = new OResultSet(this, rset.get(), m_pConnection->getConnectionEncoding());
316 rset.release();
317 } catch (const sql::SQLException &e) {
318 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
320 return xResultSet;
322 /* }}} */
325 /* {{{ OCommonStatement::getMoreResults() -I- */
326 sal_Bool SAL_CALL OCommonStatement::getMoreResults()
327 throw(SQLException, RuntimeException)
329 OSL_TRACE("OCommonStatement::getMoreResults");
330 MutexGuard aGuard(m_aMutex);
331 checkDisposed(rBHelper.bDisposed);
333 // if your driver supports more than only one resultset
334 // and has one more at this moment return(true
335 return (sal_False);
337 /* }}} */
340 /* {{{ OCommonStatement::getWarnings() -I- */
341 Any SAL_CALL OCommonStatement::getWarnings()
342 throw(SQLException, RuntimeException)
344 OSL_TRACE("OCommonStatement::getWarnings");
345 MutexGuard aGuard(m_aMutex);
346 checkDisposed(rBHelper.bDisposed);
348 return makeAny(m_aLastWarning);
350 /* }}} */
353 /* {{{ OCommonStatement::clearWarnings() -I- */
354 void SAL_CALL OCommonStatement::clearWarnings()
355 throw(SQLException, RuntimeException)
357 OSL_TRACE("OCommonStatement::clearWarnings");
358 MutexGuard aGuard(m_aMutex);
359 checkDisposed(rBHelper.bDisposed);
361 m_aLastWarning = SQLWarning();
363 /* }}} */
366 /* {{{ OCommonStatement::createArrayHelper() -I- */
367 ::cppu::IPropertyArrayHelper* OCommonStatement::createArrayHelper( ) const
369 OSL_TRACE("OCommonStatement::createArrayHelper");
370 // this properties are define by the service statement
371 // they must in alphabetic order
372 Sequence< Property > aProps(10);
373 Property* pProperties = aProps.getArray();
374 sal_Int32 nPos = 0;
375 DECL_PROP0(CURSORNAME, OUString);
376 DECL_BOOL_PROP0(ESCAPEPROCESSING);
377 DECL_PROP0(FETCHDIRECTION,sal_Int32);
378 DECL_PROP0(FETCHSIZE, sal_Int32);
379 DECL_PROP0(MAXFIELDSIZE,sal_Int32);
380 DECL_PROP0(MAXROWS, sal_Int32);
381 DECL_PROP0(QUERYTIMEOUT,sal_Int32);
382 DECL_PROP0(RESULTSETCONCURRENCY,sal_Int32);
383 DECL_PROP0(RESULTSETTYPE,sal_Int32);
384 DECL_BOOL_PROP0(USEBOOKMARKS);
386 return new ::cppu::OPropertyArrayHelper(aProps);
388 /* }}} */
391 /* {{{ OCommonStatement::getInfoHelper() -I- */
392 ::cppu::IPropertyArrayHelper & OCommonStatement::getInfoHelper()
394 OSL_TRACE("OCommonStatement::getInfoHelper");
395 return(*const_cast<OCommonStatement*>(this)->getArrayHelper());
397 /* }}} */
400 /* {{{ OCommonStatement::convertFastPropertyValue() -I- */
401 sal_Bool OCommonStatement::convertFastPropertyValue(
402 Any & /* rConvertedValue */, Any & /* rOldValue */,
403 sal_Int32 /* nHandle */, const Any& /* rValue */)
404 throw (IllegalArgumentException)
406 OSL_TRACE("OCommonStatement::convertFastPropertyValue");
407 sal_Bool bConverted = sal_False;
408 // here we have to try to convert
409 return bConverted;
411 /* }}} */
414 /* {{{ OCommonStatement::setFastPropertyValue_NoBroadcast() -I- */
415 void OCommonStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& /* rValue */)
416 throw (Exception)
418 OSL_TRACE("OCommonStatement::setFastPropertyValue_NoBroadcast");
419 // set the value to what ever is necessary
420 switch (nHandle) {
421 case PROPERTY_ID_QUERYTIMEOUT:
422 case PROPERTY_ID_MAXFIELDSIZE:
423 case PROPERTY_ID_MAXROWS:
424 case PROPERTY_ID_CURSORNAME:
425 case PROPERTY_ID_RESULTSETCONCURRENCY:
426 case PROPERTY_ID_RESULTSETTYPE:
427 case PROPERTY_ID_FETCHDIRECTION:
428 case PROPERTY_ID_FETCHSIZE:
429 case PROPERTY_ID_ESCAPEPROCESSING:
430 case PROPERTY_ID_USEBOOKMARKS:
431 default:
435 /* }}} */
438 /* {{{ OCommonStatement::getFastPropertyValue() -I- */
439 void OCommonStatement::getFastPropertyValue(Any& _rValue, sal_Int32 nHandle) const
441 OSL_TRACE("OCommonStatement::getFastPropertyValue");
442 switch (nHandle) {
443 case PROPERTY_ID_QUERYTIMEOUT:
444 case PROPERTY_ID_MAXFIELDSIZE:
445 case PROPERTY_ID_MAXROWS:
446 case PROPERTY_ID_CURSORNAME:
447 case PROPERTY_ID_RESULTSETCONCURRENCY:
448 case PROPERTY_ID_RESULTSETTYPE:
449 case PROPERTY_ID_FETCHDIRECTION:
450 case PROPERTY_ID_FETCHSIZE:
451 case PROPERTY_ID_ESCAPEPROCESSING:
452 break;
453 case PROPERTY_ID_USEBOOKMARKS:
454 _rValue <<= sal_False;
455 break;
456 default:
460 /* }}} */
462 IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbcx.OStatement","com.sun.star.sdbc.Statement");
464 /* {{{ OCommonStatement::acquire() -I- */
465 void SAL_CALL OCommonStatement::acquire()
466 throw()
468 OSL_TRACE("OCommonStatement::acquire");
469 OCommonStatement_IBase::acquire();
471 /* }}} */
474 /* {{{ OCommonStatement::release() -I- */
475 void SAL_CALL OCommonStatement::release()
476 throw()
478 OSL_TRACE("OCommonStatement::release");
479 relase_ChildImpl();
481 /* }}} */
484 /* {{{ OStatement::acquire() -I- */
485 void SAL_CALL OStatement::acquire()
486 throw()
488 OSL_TRACE("OStatement::acquire");
489 OCommonStatement::acquire();
491 /* }}} */
494 /* {{{ OStatement::release() -I- */
495 void SAL_CALL OStatement::release()
496 throw()
498 OSL_TRACE("OStatement::release");
499 OCommonStatement::release();
501 /* }}} */
504 /* {{{ OCommonStatement::getPropertySetInfo() -I- */
505 Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OCommonStatement::getPropertySetInfo()
506 throw(RuntimeException)
508 OSL_TRACE("OCommonStatement::getPropertySetInfo");
509 return(::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper()));
511 /* }}} */
514 * Local variables:
515 * tab-width: 4
516 * c-basic-offset: 4
517 * End:
518 * vim600: noet sw=4 ts=4 fdm=marker
519 * vim<600: noet sw=4 ts=4
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */