Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / api / preparedstatement.cxx
blobfa624f183cb6672ac687aec5b24150abd753e3b7
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/typeprovider.hxx>
30 #include <preparedstatement.hxx>
31 #include <resultcolumn.hxx>
32 #include <resultset.hxx>
33 #include <tools/debug.hxx>
34 #include <tools/diagnose_ex.h>
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::sdbcx;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::cppu;
42 using namespace ::osl;
43 using namespace dbaccess;
45 DBG_NAME(OPreparedStatement)
47 OPreparedStatement::OPreparedStatement(const Reference< XConnection > & _xConn,
48 const Reference< XInterface > & _xStatement)
49 :OStatementBase(_xConn, _xStatement)
51 DBG_CTOR(OPreparedStatement, NULL);
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< ::rtl::OUString>(), NULL,NULL);
58 OPreparedStatement::~OPreparedStatement()
60 m_pColumns->acquire();
61 m_pColumns->disposing();
62 delete m_pColumns;
64 DBG_DTOR(OPreparedStatement, NULL);
67 // com::sun::star::lang::XTypeProvider
68 Sequence< Type > OPreparedStatement::getTypes() throw (RuntimeException)
70 OTypeCollection aTypes(::getCppuType( (const Reference< XServiceInfo > *)0 ),
71 ::getCppuType( (const Reference< XPreparedStatement > *)0 ),
72 ::getCppuType( (const Reference< XParameters > *)0 ),
73 ::getCppuType( (const Reference< XResultSetMetaDataSupplier > *)0 ),
74 ::getCppuType( (const Reference< XColumnsSupplier > *)0 ),
75 OStatementBase::getTypes() );
77 return aTypes.getTypes();
80 Sequence< sal_Int8 > OPreparedStatement::getImplementationId() throw (RuntimeException)
82 static OImplementationId * pId = 0;
83 if (! pId)
85 MutexGuard aGuard( Mutex::getGlobalMutex() );
86 if (! pId)
88 static OImplementationId aId;
89 pId = &aId;
92 return pId->getImplementationId();
95 // com::sun::star::uno::XInterface
96 Any OPreparedStatement::queryInterface( const Type & rType ) throw (RuntimeException)
98 Any aIface = OStatementBase::queryInterface( rType );
99 if (!aIface.hasValue())
100 aIface = ::cppu::queryInterface(
101 rType,
102 static_cast< XServiceInfo * >( this ),
103 static_cast< XParameters * >( this ),
104 static_cast< XColumnsSupplier * >( this ),
105 static_cast< XResultSetMetaDataSupplier * >( this ),
106 static_cast< XPreparedBatchExecution * >( this ),
107 static_cast< XMultipleResults * >( this ),
108 static_cast< XPreparedStatement * >( this ));
109 return aIface;
112 void OPreparedStatement::acquire() throw ()
114 OStatementBase::acquire();
117 void OPreparedStatement::release() throw ()
119 OStatementBase::release();
122 // XServiceInfo
123 rtl::OUString OPreparedStatement::getImplementationName( ) throw(RuntimeException)
125 return rtl::OUString("com.sun.star.sdb.OPreparedStatement");
128 sal_Bool OPreparedStatement::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
130 return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
133 Sequence< ::rtl::OUString > OPreparedStatement::getSupportedServiceNames( ) throw (RuntimeException)
135 Sequence< ::rtl::OUString > aSNS( 2 );
136 aSNS.getArray()[0] = SERVICE_SDBC_PREPAREDSTATEMENT;
137 aSNS.getArray()[1] = SERVICE_SDB_PREPAREDSTATMENT;
138 return aSNS;
141 // OComponentHelper
142 void OPreparedStatement::disposing()
145 MutexGuard aGuard(m_aMutex);
146 m_pColumns->disposing();
147 m_xAggregateAsParameters = NULL;
149 OStatementBase::disposing();
152 // ::com::sun::star::sdbcx::XColumnsSupplier
153 Reference< ::com::sun::star::container::XNameAccess > OPreparedStatement::getColumns(void) throw( RuntimeException )
155 MutexGuard aGuard(m_aMutex);
156 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
158 // do we have to populate the columns
159 if (!m_pColumns->isInitialized())
163 Reference< XResultSetMetaDataSupplier > xSuppMeta( m_xAggregateAsSet, UNO_QUERY_THROW );
164 Reference< XResultSetMetaData > xMetaData( xSuppMeta->getMetaData(), UNO_SET_THROW );
166 Reference< XConnection > xConn( getConnection(), UNO_SET_THROW );
167 Reference< XDatabaseMetaData > xDBMeta( xConn->getMetaData(), UNO_SET_THROW );
169 for (sal_Int32 i = 0, nCount = xMetaData->getColumnCount(); i < nCount; ++i)
171 // retrieve the name of the column
172 rtl::OUString aName = xMetaData->getColumnName(i + 1);
173 OResultColumn* pColumn = new OResultColumn(xMetaData, i + 1, xDBMeta);
174 m_pColumns->append(aName, pColumn);
177 catch (const SQLException& )
179 DBG_UNHANDLED_EXCEPTION();
181 m_pColumns->setInitialized();
183 return m_pColumns;
186 // XResultSetMetaDataSupplier
187 Reference< XResultSetMetaData > OPreparedStatement::getMetaData(void) throw( SQLException, RuntimeException )
189 MutexGuard aGuard(m_aMutex);
190 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
191 return Reference< XResultSetMetaDataSupplier >( m_xAggregateAsSet, UNO_QUERY_THROW )->getMetaData();
194 // XPreparedStatement
195 Reference< XResultSet > OPreparedStatement::executeQuery() throw( SQLException, RuntimeException )
197 MutexGuard aGuard(m_aMutex);
198 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
200 disposeResultSet();
202 Reference< XResultSet > xResultSet;
203 Reference< XResultSet > xDrvResultSet = Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeQuery();
204 if (xDrvResultSet.is())
206 xResultSet = new OResultSet(xDrvResultSet, *this, m_pColumns->isCaseSensitive());
208 // keep the resultset weak
209 m_aResultSet = xResultSet;
211 return xResultSet;
214 sal_Int32 OPreparedStatement::executeUpdate() throw( SQLException, RuntimeException )
216 MutexGuard aGuard(m_aMutex);
217 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
219 disposeResultSet();
221 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeUpdate();
224 sal_Bool OPreparedStatement::execute() throw( SQLException, RuntimeException )
226 MutexGuard aGuard(m_aMutex);
227 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
229 disposeResultSet();
231 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->execute();
234 Reference< XConnection > OPreparedStatement::getConnection(void) throw( SQLException, RuntimeException )
236 return Reference< XConnection > (m_xParent, UNO_QUERY);
239 // XParameters
240 void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType ) throw(SQLException, RuntimeException)
242 MutexGuard aGuard(m_aMutex);
243 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
245 m_xAggregateAsParameters->setNull(parameterIndex, sqlType);
248 void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const ::rtl::OUString& typeName ) throw(SQLException, RuntimeException)
250 MutexGuard aGuard(m_aMutex);
251 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
253 m_xAggregateAsParameters->setObjectNull(parameterIndex, sqlType, typeName);
256 void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException)
258 MutexGuard aGuard(m_aMutex);
259 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
261 m_xAggregateAsParameters->setBoolean(parameterIndex, x);
264 void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException)
266 MutexGuard aGuard(m_aMutex);
267 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
269 m_xAggregateAsParameters->setByte(parameterIndex, x);
272 void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException)
274 MutexGuard aGuard(m_aMutex);
275 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
277 m_xAggregateAsParameters->setShort(parameterIndex, x);
280 void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException)
282 MutexGuard aGuard(m_aMutex);
283 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
285 m_xAggregateAsParameters->setInt(parameterIndex, x);
288 void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x ) throw(SQLException, RuntimeException)
290 MutexGuard aGuard(m_aMutex);
291 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
293 m_xAggregateAsParameters->setLong(parameterIndex, x);
296 void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException)
298 MutexGuard aGuard(m_aMutex);
299 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
301 m_xAggregateAsParameters->setFloat(parameterIndex, x);
304 void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException)
306 MutexGuard aGuard(m_aMutex);
307 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
309 m_xAggregateAsParameters->setDouble(parameterIndex, x);
312 void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const ::rtl::OUString& x ) throw(SQLException, RuntimeException)
314 MutexGuard aGuard(m_aMutex);
315 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
317 m_xAggregateAsParameters->setString(parameterIndex, x);
320 void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException)
322 MutexGuard aGuard(m_aMutex);
323 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
325 m_xAggregateAsParameters->setBytes(parameterIndex, x);
328 void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const ::com::sun::star::util::Date& x ) throw(SQLException, RuntimeException)
330 MutexGuard aGuard(m_aMutex);
331 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
333 m_xAggregateAsParameters->setDate(parameterIndex, x);
336 void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const ::com::sun::star::util::Time& x ) throw(SQLException, RuntimeException)
338 MutexGuard aGuard(m_aMutex);
339 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
341 m_xAggregateAsParameters->setTime(parameterIndex, x);
344 void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const ::com::sun::star::util::DateTime& x ) throw(SQLException, RuntimeException)
346 MutexGuard aGuard(m_aMutex);
347 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
349 m_xAggregateAsParameters->setTimestamp(parameterIndex, x);
352 void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
354 MutexGuard aGuard(m_aMutex);
355 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
357 m_xAggregateAsParameters->setBinaryStream(parameterIndex, x, length);
360 void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
362 MutexGuard aGuard(m_aMutex);
363 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
365 m_xAggregateAsParameters->setCharacterStream(parameterIndex, x, length);
368 void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException)
370 MutexGuard aGuard(m_aMutex);
371 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
373 m_xAggregateAsParameters->setObject(parameterIndex, x);
376 void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 targetSqlType, sal_Int32 scale ) throw(SQLException, RuntimeException)
378 MutexGuard aGuard(m_aMutex);
379 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
381 m_xAggregateAsParameters->setObjectWithInfo(parameterIndex, x, targetSqlType, scale);
384 void SAL_CALL OPreparedStatement::setRef( sal_Int32 parameterIndex, const Reference< XRef >& x ) throw(SQLException, RuntimeException)
386 MutexGuard aGuard(m_aMutex);
387 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
389 m_xAggregateAsParameters->setRef(parameterIndex, x);
392 void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x ) throw(SQLException, RuntimeException)
394 MutexGuard aGuard(m_aMutex);
395 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
397 m_xAggregateAsParameters->setBlob(parameterIndex, x);
400 void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x ) throw(SQLException, RuntimeException)
402 MutexGuard aGuard(m_aMutex);
403 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
405 m_xAggregateAsParameters->setClob(parameterIndex, x);
408 void SAL_CALL OPreparedStatement::setArray( sal_Int32 parameterIndex, const Reference< XArray >& x ) throw(SQLException, RuntimeException)
410 MutexGuard aGuard(m_aMutex);
411 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
413 m_xAggregateAsParameters->setArray(parameterIndex, x);
416 void SAL_CALL OPreparedStatement::clearParameters( ) throw(SQLException, RuntimeException)
418 MutexGuard aGuard(m_aMutex);
419 ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
421 m_xAggregateAsParameters->clearParameters();
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */