Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / preparedstatement.cxx
blob4197c78a4f5dd68930bdebdc3645fa79efb2eff2
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 .
21 #include "dbastrings.hrc"
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
27 #include <comphelper/property.hxx>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <preparedstatement.hxx>
33 #include <resultcolumn.hxx>
34 #include <resultset.hxx>
35 #include <tools/debug.hxx>
36 #include <tools/diagnose_ex.h>
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::sdbcx;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::cppu;
44 using namespace ::osl;
45 using namespace dbaccess;
48 OPreparedStatement::OPreparedStatement(const Reference< XConnection > & _xConn,
49 const Reference< XInterface > & _xStatement)
50 :OStatementBase(_xConn, _xStatement)
52 m_xAggregateAsParameters = Reference< XParameters >( m_xAggregateAsSet, UNO_QUERY_THROW );
54 Reference<XDatabaseMetaData> xMeta = _xConn->getMetaData();
55 m_pColumns = new OColumns(*this, m_aMutex, xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers(),::std::vector< OUString>(), NULL,NULL);
58 OPreparedStatement::~OPreparedStatement()
60 m_pColumns->acquire();
61 m_pColumns->disposing();
62 delete m_pColumns;
66 // com::sun::star::lang::XTypeProvider
67 Sequence< Type > OPreparedStatement::getTypes() throw (RuntimeException, std::exception)
69 OTypeCollection aTypes(cppu::UnoType<XServiceInfo>::get(),
70 cppu::UnoType<XPreparedStatement>::get(),
71 cppu::UnoType<XParameters>::get(),
72 cppu::UnoType<XResultSetMetaDataSupplier>::get(),
73 cppu::UnoType<XColumnsSupplier>::get(),
74 OStatementBase::getTypes() );
76 return aTypes.getTypes();
79 Sequence< sal_Int8 > OPreparedStatement::getImplementationId() throw (RuntimeException, std::exception)
81 return css::uno::Sequence<sal_Int8>();
84 // com::sun::star::uno::XInterface
85 Any OPreparedStatement::queryInterface( const Type & rType ) throw (RuntimeException, std::exception)
87 Any aIface = OStatementBase::queryInterface( rType );
88 if (!aIface.hasValue())
89 aIface = ::cppu::queryInterface(
90 rType,
91 static_cast< XServiceInfo * >( this ),
92 static_cast< XParameters * >( this ),
93 static_cast< XColumnsSupplier * >( this ),
94 static_cast< XResultSetMetaDataSupplier * >( this ),
95 static_cast< XPreparedBatchExecution * >( this ),
96 static_cast< XMultipleResults * >( this ),
97 static_cast< XPreparedStatement * >( this ));
98 return aIface;
101 void OPreparedStatement::acquire() throw ()
103 OStatementBase::acquire();
106 void OPreparedStatement::release() throw ()
108 OStatementBase::release();
111 // XServiceInfo
112 OUString OPreparedStatement::getImplementationName( ) throw(RuntimeException, std::exception)
114 return OUString("com.sun.star.sdb.OPreparedStatement");
117 sal_Bool OPreparedStatement::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
119 return cppu::supportsService(this, _rServiceName);
122 Sequence< OUString > OPreparedStatement::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
124 Sequence< OUString > aSNS( 2 );
125 aSNS.getArray()[0] = SERVICE_SDBC_PREPAREDSTATEMENT;
126 aSNS.getArray()[1] = SERVICE_SDB_PREPAREDSTATMENT;
127 return aSNS;
130 // OComponentHelper
131 void OPreparedStatement::disposing()
134 MutexGuard aGuard(m_aMutex);
135 m_pColumns->disposing();
136 m_xAggregateAsParameters = NULL;
138 OStatementBase::disposing();
141 // ::com::sun::star::sdbcx::XColumnsSupplier
142 Reference< ::com::sun::star::container::XNameAccess > OPreparedStatement::getColumns() throw( RuntimeException, std::exception )
144 MutexGuard aGuard(m_aMutex);
145 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
147 // do we have to populate the columns
148 if (!m_pColumns->isInitialized())
152 Reference< XResultSetMetaDataSupplier > xSuppMeta( m_xAggregateAsSet, UNO_QUERY_THROW );
153 Reference< XResultSetMetaData > xMetaData( xSuppMeta->getMetaData(), UNO_SET_THROW );
155 Reference< XConnection > xConn( getConnection(), UNO_SET_THROW );
156 Reference< XDatabaseMetaData > xDBMeta( xConn->getMetaData(), UNO_SET_THROW );
158 for (sal_Int32 i = 0, nCount = xMetaData->getColumnCount(); i < nCount; ++i)
160 // retrieve the name of the column
161 OUString aName = xMetaData->getColumnName(i + 1);
162 OResultColumn* pColumn = new OResultColumn(xMetaData, i + 1, xDBMeta);
163 m_pColumns->append(aName, pColumn);
166 catch (const SQLException& )
168 DBG_UNHANDLED_EXCEPTION();
170 m_pColumns->setInitialized();
172 return m_pColumns;
175 // XResultSetMetaDataSupplier
176 Reference< XResultSetMetaData > OPreparedStatement::getMetaData() throw( SQLException, RuntimeException, std::exception )
178 MutexGuard aGuard(m_aMutex);
179 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
180 return Reference< XResultSetMetaDataSupplier >( m_xAggregateAsSet, UNO_QUERY_THROW )->getMetaData();
183 // XPreparedStatement
184 Reference< XResultSet > OPreparedStatement::executeQuery() throw( SQLException, RuntimeException, std::exception )
186 MutexGuard aGuard(m_aMutex);
187 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
189 disposeResultSet();
191 Reference< XResultSet > xResultSet;
192 Reference< XResultSet > xDrvResultSet = Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeQuery();
193 if (xDrvResultSet.is())
195 xResultSet = new OResultSet(xDrvResultSet, *this, m_pColumns->isCaseSensitive());
197 // keep the resultset weak
198 m_aResultSet = xResultSet;
200 return xResultSet;
203 sal_Int32 OPreparedStatement::executeUpdate() throw( SQLException, RuntimeException, std::exception )
205 MutexGuard aGuard(m_aMutex);
206 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
208 disposeResultSet();
210 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeUpdate();
213 sal_Bool OPreparedStatement::execute() throw( SQLException, RuntimeException, std::exception )
215 MutexGuard aGuard(m_aMutex);
216 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
218 disposeResultSet();
220 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->execute();
223 Reference< XConnection > OPreparedStatement::getConnection() throw( SQLException, RuntimeException, std::exception )
225 return Reference< XConnection > (m_xParent, UNO_QUERY);
228 // XParameters
229 void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType ) throw(SQLException, RuntimeException, std::exception)
231 MutexGuard aGuard(m_aMutex);
232 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
234 m_xAggregateAsParameters->setNull(parameterIndex, sqlType);
237 void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& typeName ) throw(SQLException, RuntimeException, std::exception)
239 MutexGuard aGuard(m_aMutex);
240 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
242 m_xAggregateAsParameters->setObjectNull(parameterIndex, sqlType, typeName);
245 void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException, std::exception)
247 MutexGuard aGuard(m_aMutex);
248 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
250 m_xAggregateAsParameters->setBoolean(parameterIndex, x);
253 void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException, std::exception)
255 MutexGuard aGuard(m_aMutex);
256 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
258 m_xAggregateAsParameters->setByte(parameterIndex, x);
261 void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException, std::exception)
263 MutexGuard aGuard(m_aMutex);
264 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
266 m_xAggregateAsParameters->setShort(parameterIndex, x);
269 void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException, std::exception)
271 MutexGuard aGuard(m_aMutex);
272 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
274 m_xAggregateAsParameters->setInt(parameterIndex, x);
277 void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x ) throw(SQLException, RuntimeException, std::exception)
279 MutexGuard aGuard(m_aMutex);
280 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
282 m_xAggregateAsParameters->setLong(parameterIndex, x);
285 void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException, std::exception)
287 MutexGuard aGuard(m_aMutex);
288 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
290 m_xAggregateAsParameters->setFloat(parameterIndex, x);
293 void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException, std::exception)
295 MutexGuard aGuard(m_aMutex);
296 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
298 m_xAggregateAsParameters->setDouble(parameterIndex, x);
301 void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x ) throw(SQLException, RuntimeException, std::exception)
303 MutexGuard aGuard(m_aMutex);
304 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
306 m_xAggregateAsParameters->setString(parameterIndex, x);
309 void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException, std::exception)
311 MutexGuard aGuard(m_aMutex);
312 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
314 m_xAggregateAsParameters->setBytes(parameterIndex, x);
317 void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const ::com::sun::star::util::Date& x ) throw(SQLException, RuntimeException, std::exception)
319 MutexGuard aGuard(m_aMutex);
320 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
322 m_xAggregateAsParameters->setDate(parameterIndex, x);
325 void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const ::com::sun::star::util::Time& x ) throw(SQLException, RuntimeException, std::exception)
327 MutexGuard aGuard(m_aMutex);
328 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
330 m_xAggregateAsParameters->setTime(parameterIndex, x);
333 void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const ::com::sun::star::util::DateTime& x ) throw(SQLException, RuntimeException, std::exception)
335 MutexGuard aGuard(m_aMutex);
336 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
338 m_xAggregateAsParameters->setTimestamp(parameterIndex, x);
341 void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
343 MutexGuard aGuard(m_aMutex);
344 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
346 m_xAggregateAsParameters->setBinaryStream(parameterIndex, x, length);
349 void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
351 MutexGuard aGuard(m_aMutex);
352 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
354 m_xAggregateAsParameters->setCharacterStream(parameterIndex, x, length);
357 void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException, std::exception)
359 MutexGuard aGuard(m_aMutex);
360 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
362 m_xAggregateAsParameters->setObject(parameterIndex, x);
365 void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 targetSqlType, sal_Int32 scale ) throw(SQLException, RuntimeException, std::exception)
367 MutexGuard aGuard(m_aMutex);
368 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
370 m_xAggregateAsParameters->setObjectWithInfo(parameterIndex, x, targetSqlType, scale);
373 void SAL_CALL OPreparedStatement::setRef( sal_Int32 parameterIndex, const Reference< XRef >& x ) throw(SQLException, RuntimeException, std::exception)
375 MutexGuard aGuard(m_aMutex);
376 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
378 m_xAggregateAsParameters->setRef(parameterIndex, x);
381 void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x ) throw(SQLException, RuntimeException, std::exception)
383 MutexGuard aGuard(m_aMutex);
384 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
386 m_xAggregateAsParameters->setBlob(parameterIndex, x);
389 void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x ) throw(SQLException, RuntimeException, std::exception)
391 MutexGuard aGuard(m_aMutex);
392 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
394 m_xAggregateAsParameters->setClob(parameterIndex, x);
397 void SAL_CALL OPreparedStatement::setArray( sal_Int32 parameterIndex, const Reference< XArray >& x ) throw(SQLException, RuntimeException, std::exception)
399 MutexGuard aGuard(m_aMutex);
400 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
402 m_xAggregateAsParameters->setArray(parameterIndex, x);
405 void SAL_CALL OPreparedStatement::clearParameters( ) throw(SQLException, RuntimeException, std::exception)
407 MutexGuard aGuard(m_aMutex);
408 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
410 m_xAggregateAsParameters->clearParameters();
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */