1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/sdbc/XConnection.hpp>
23 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
24 #include <com/sun/star/sdbc/SQLException.hpp>
26 #include <connectivity/dbtools.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <preparedstatement.hxx>
31 #include <strings.hxx>
32 #include "resultcolumn.hxx"
33 #include "resultset.hxx"
34 #include <connection.hxx>
35 #include <comphelper/diagnose_ex.hxx>
37 using namespace ::com::sun::star::sdbc
;
38 using namespace ::com::sun::star::sdbcx
;
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
;
46 OPreparedStatement::OPreparedStatement(const rtl::Reference
< OConnection
> & _xConn
,
47 const Reference
< XInterface
> & _xStatement
)
48 :OStatementBase(_xConn
, _xStatement
)
50 m_xAggregateAsParameters
.set( m_xAggregateAsSet
, UNO_QUERY_THROW
);
52 Reference
<XDatabaseMetaData
> xMeta
= _xConn
->getMetaData();
53 m_pColumns
.reset( new OColumns(*this, m_aMutex
, xMeta
.is() && xMeta
->supportsMixedCaseQuotedIdentifiers(),std::vector
< OUString
>(), nullptr,nullptr) );
56 OPreparedStatement::~OPreparedStatement()
58 m_pColumns
->acquire();
59 m_pColumns
->disposing();
62 // css::lang::XTypeProvider
63 Sequence
< Type
> OPreparedStatement::getTypes()
65 OTypeCollection
aTypes(cppu::UnoType
<XServiceInfo
>::get(),
66 cppu::UnoType
<XPreparedStatement
>::get(),
67 cppu::UnoType
<XParameters
>::get(),
68 cppu::UnoType
<XResultSetMetaDataSupplier
>::get(),
69 cppu::UnoType
<XColumnsSupplier
>::get(),
70 OStatementBase::getTypes() );
72 return aTypes
.getTypes();
75 Sequence
< sal_Int8
> OPreparedStatement::getImplementationId()
77 return css::uno::Sequence
<sal_Int8
>();
80 // css::uno::XInterface
81 Any
OPreparedStatement::queryInterface( const Type
& rType
)
83 Any aIface
= OStatementBase::queryInterface( rType
);
84 if (!aIface
.hasValue())
85 aIface
= ::cppu::queryInterface(
87 static_cast< XServiceInfo
* >( this ),
88 static_cast< XParameters
* >( this ),
89 static_cast< XColumnsSupplier
* >( this ),
90 static_cast< XResultSetMetaDataSupplier
* >( this ),
91 static_cast< XPreparedBatchExecution
* >( this ),
92 static_cast< XMultipleResults
* >( this ),
93 static_cast< XPreparedStatement
* >( this ));
97 void OPreparedStatement::acquire() noexcept
99 OStatementBase::acquire();
102 void OPreparedStatement::release() noexcept
104 OStatementBase::release();
108 OUString
OPreparedStatement::getImplementationName( )
110 return u
"com.sun.star.sdb.OPreparedStatement"_ustr
;
113 sal_Bool
OPreparedStatement::supportsService( const OUString
& _rServiceName
)
115 return cppu::supportsService(this, _rServiceName
);
118 Sequence
< OUString
> OPreparedStatement::getSupportedServiceNames( )
120 return { SERVICE_SDBC_PREPAREDSTATEMENT
, SERVICE_SDB_PREPAREDSTATEMENT
};
124 void OPreparedStatement::disposing()
127 MutexGuard
aGuard(m_aMutex
);
128 m_pColumns
->disposing();
129 m_xAggregateAsParameters
= nullptr;
131 OStatementBase::disposing();
134 // css::sdbcx::XColumnsSupplier
135 Reference
< css::container::XNameAccess
> OPreparedStatement::getColumns()
137 MutexGuard
aGuard(m_aMutex
);
138 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
140 // do we have to populate the columns
141 if (!m_pColumns
->isInitialized())
145 Reference
< XResultSetMetaDataSupplier
> xSuppMeta( m_xAggregateAsSet
, UNO_QUERY_THROW
);
146 Reference
< XResultSetMetaData
> xMetaData( xSuppMeta
->getMetaData(), UNO_SET_THROW
);
148 Reference
< XConnection
> xConn( getConnection(), UNO_SET_THROW
);
149 Reference
< XDatabaseMetaData
> xDBMeta( xConn
->getMetaData(), UNO_SET_THROW
);
151 for (sal_Int32 i
= 0, nCount
= xMetaData
->getColumnCount(); i
< nCount
; ++i
)
153 // retrieve the name of the column
154 OUString aName
= xMetaData
->getColumnName(i
+ 1);
155 rtl::Reference
<OResultColumn
> pColumn
= new OResultColumn(xMetaData
, i
+ 1, xDBMeta
);
156 // don't silently assume that the name is unique - preparedStatement implementations
157 // are allowed to return duplicate names, but we are required to have
158 // unique column names
159 if ( m_pColumns
->hasByName( aName
) )
160 aName
= ::dbtools::createUniqueName( m_pColumns
.get(), aName
);
162 m_pColumns
->append(aName
, pColumn
.get());
165 catch (const SQLException
& )
167 DBG_UNHANDLED_EXCEPTION("dbaccess");
169 m_pColumns
->setInitialized();
171 return m_pColumns
.get();
174 // XResultSetMetaDataSupplier
175 Reference
< XResultSetMetaData
> OPreparedStatement::getMetaData()
177 MutexGuard
aGuard(m_aMutex
);
178 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
179 return Reference
< XResultSetMetaDataSupplier
>( m_xAggregateAsSet
, UNO_QUERY_THROW
)->getMetaData();
182 // XPreparedStatement
183 Reference
< XResultSet
> OPreparedStatement::executeQuery()
185 MutexGuard
aGuard(m_aMutex
);
186 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
190 Reference
< XResultSet
> xResultSet
;
191 Reference
< XResultSet
> xDrvResultSet
= Reference
< XPreparedStatement
>( m_xAggregateAsSet
, UNO_QUERY_THROW
)->executeQuery();
192 if (xDrvResultSet
.is())
194 xResultSet
= new OResultSet(xDrvResultSet
, *this, m_pColumns
->isCaseSensitive());
196 // keep the resultset weak
197 m_aResultSet
= xResultSet
;
202 sal_Int32
OPreparedStatement::executeUpdate()
204 MutexGuard
aGuard(m_aMutex
);
205 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
209 return Reference
< XPreparedStatement
>( m_xAggregateAsSet
, UNO_QUERY_THROW
)->executeUpdate();
212 sal_Bool
OPreparedStatement::execute()
214 MutexGuard
aGuard(m_aMutex
);
215 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
219 return Reference
< XPreparedStatement
>( m_xAggregateAsSet
, UNO_QUERY_THROW
)->execute();
222 Reference
< XConnection
> OPreparedStatement::getConnection()
224 return m_xParent
.get();
228 void SAL_CALL
OPreparedStatement::setNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
)
230 MutexGuard
aGuard(m_aMutex
);
231 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
233 m_xAggregateAsParameters
->setNull(parameterIndex
, sqlType
);
236 void SAL_CALL
OPreparedStatement::setObjectNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const OUString
& typeName
)
238 MutexGuard
aGuard(m_aMutex
);
239 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
241 m_xAggregateAsParameters
->setObjectNull(parameterIndex
, sqlType
, typeName
);
244 void SAL_CALL
OPreparedStatement::setBoolean( sal_Int32 parameterIndex
, sal_Bool x
)
246 MutexGuard
aGuard(m_aMutex
);
247 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
249 m_xAggregateAsParameters
->setBoolean(parameterIndex
, x
);
252 void SAL_CALL
OPreparedStatement::setByte( sal_Int32 parameterIndex
, sal_Int8 x
)
254 MutexGuard
aGuard(m_aMutex
);
255 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
257 m_xAggregateAsParameters
->setByte(parameterIndex
, x
);
260 void SAL_CALL
OPreparedStatement::setShort( sal_Int32 parameterIndex
, sal_Int16 x
)
262 MutexGuard
aGuard(m_aMutex
);
263 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
265 m_xAggregateAsParameters
->setShort(parameterIndex
, x
);
268 void SAL_CALL
OPreparedStatement::setInt( sal_Int32 parameterIndex
, sal_Int32 x
)
270 MutexGuard
aGuard(m_aMutex
);
271 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
273 m_xAggregateAsParameters
->setInt(parameterIndex
, x
);
276 void SAL_CALL
OPreparedStatement::setLong( sal_Int32 parameterIndex
, sal_Int64 x
)
278 MutexGuard
aGuard(m_aMutex
);
279 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
281 m_xAggregateAsParameters
->setLong(parameterIndex
, x
);
284 void SAL_CALL
OPreparedStatement::setFloat( sal_Int32 parameterIndex
, float x
)
286 MutexGuard
aGuard(m_aMutex
);
287 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
289 m_xAggregateAsParameters
->setFloat(parameterIndex
, x
);
292 void SAL_CALL
OPreparedStatement::setDouble( sal_Int32 parameterIndex
, double x
)
294 MutexGuard
aGuard(m_aMutex
);
295 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
297 m_xAggregateAsParameters
->setDouble(parameterIndex
, x
);
300 void SAL_CALL
OPreparedStatement::setString( sal_Int32 parameterIndex
, const OUString
& x
)
302 MutexGuard
aGuard(m_aMutex
);
303 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
305 m_xAggregateAsParameters
->setString(parameterIndex
, x
);
308 void SAL_CALL
OPreparedStatement::setBytes( sal_Int32 parameterIndex
, const Sequence
< sal_Int8
>& x
)
310 MutexGuard
aGuard(m_aMutex
);
311 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
313 m_xAggregateAsParameters
->setBytes(parameterIndex
, x
);
316 void SAL_CALL
OPreparedStatement::setDate( sal_Int32 parameterIndex
, const css::util::Date
& x
)
318 MutexGuard
aGuard(m_aMutex
);
319 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
321 m_xAggregateAsParameters
->setDate(parameterIndex
, x
);
324 void SAL_CALL
OPreparedStatement::setTime( sal_Int32 parameterIndex
, const css::util::Time
& x
)
326 MutexGuard
aGuard(m_aMutex
);
327 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
329 m_xAggregateAsParameters
->setTime(parameterIndex
, x
);
332 void SAL_CALL
OPreparedStatement::setTimestamp( sal_Int32 parameterIndex
, const css::util::DateTime
& x
)
334 MutexGuard
aGuard(m_aMutex
);
335 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
337 m_xAggregateAsParameters
->setTimestamp(parameterIndex
, x
);
340 void SAL_CALL
OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex
, const Reference
< css::io::XInputStream
>& x
, sal_Int32 length
)
342 MutexGuard
aGuard(m_aMutex
);
343 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
345 m_xAggregateAsParameters
->setBinaryStream(parameterIndex
, x
, length
);
348 void SAL_CALL
OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex
, const Reference
< css::io::XInputStream
>& x
, sal_Int32 length
)
350 MutexGuard
aGuard(m_aMutex
);
351 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
353 m_xAggregateAsParameters
->setCharacterStream(parameterIndex
, x
, length
);
356 void SAL_CALL
OPreparedStatement::setObject( sal_Int32 parameterIndex
, const Any
& x
)
358 MutexGuard
aGuard(m_aMutex
);
359 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
361 m_xAggregateAsParameters
->setObject(parameterIndex
, x
);
364 void SAL_CALL
OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex
, const Any
& x
, sal_Int32 targetSqlType
, sal_Int32 scale
)
366 MutexGuard
aGuard(m_aMutex
);
367 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
369 m_xAggregateAsParameters
->setObjectWithInfo(parameterIndex
, x
, targetSqlType
, scale
);
372 void SAL_CALL
OPreparedStatement::setRef( sal_Int32 parameterIndex
, const Reference
< XRef
>& x
)
374 MutexGuard
aGuard(m_aMutex
);
375 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
377 m_xAggregateAsParameters
->setRef(parameterIndex
, x
);
380 void SAL_CALL
OPreparedStatement::setBlob( sal_Int32 parameterIndex
, const Reference
< XBlob
>& x
)
382 MutexGuard
aGuard(m_aMutex
);
383 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
385 m_xAggregateAsParameters
->setBlob(parameterIndex
, x
);
388 void SAL_CALL
OPreparedStatement::setClob( sal_Int32 parameterIndex
, const Reference
< XClob
>& x
)
390 MutexGuard
aGuard(m_aMutex
);
391 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
393 m_xAggregateAsParameters
->setClob(parameterIndex
, x
);
396 void SAL_CALL
OPreparedStatement::setArray( sal_Int32 parameterIndex
, const Reference
< XArray
>& x
)
398 MutexGuard
aGuard(m_aMutex
);
399 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
401 m_xAggregateAsParameters
->setArray(parameterIndex
, x
);
404 void SAL_CALL
OPreparedStatement::clearParameters( )
406 MutexGuard
aGuard(m_aMutex
);
407 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper
.bDisposed
);
409 m_xAggregateAsParameters
->clearParameters();
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */