LibreOffice Module dbaccess (master) 1
preparedstatement.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
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>
25
30#include <preparedstatement.hxx>
31#include <strings.hxx>
32#include "resultcolumn.hxx"
33#include "resultset.hxx"
35
36using namespace ::com::sun::star::sdbc;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::beans;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::lang;
41using namespace ::cppu;
42using namespace ::osl;
43using namespace dbaccess;
44
45
46OPreparedStatement::OPreparedStatement(const Reference< XConnection > & _xConn,
47 const Reference< XInterface > & _xStatement)
48 :OStatementBase(_xConn, _xStatement)
49{
50 m_xAggregateAsParameters.set( m_xAggregateAsSet, UNO_QUERY_THROW );
51
52 Reference<XDatabaseMetaData> xMeta = _xConn->getMetaData();
53 m_pColumns.reset( new OColumns(*this, m_aMutex, xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers(),std::vector< OUString>(), nullptr,nullptr) );
54}
55
56OPreparedStatement::~OPreparedStatement()
57{
58 m_pColumns->acquire();
59 m_pColumns->disposing();
60}
61
62// css::lang::XTypeProvider
64{
71
72 return aTypes.getTypes();
73}
74
76{
77 return css::uno::Sequence<sal_Int8>();
78}
79
80// css::uno::XInterface
82{
83 Any aIface = OStatementBase::queryInterface( rType );
84 if (!aIface.hasValue())
85 aIface = ::cppu::queryInterface(
86 rType,
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 ));
94 return aIface;
95}
96
98{
100}
101
103{
105}
106
107// XServiceInfo
109{
110 return "com.sun.star.sdb.OPreparedStatement";
111}
112
113sal_Bool OPreparedStatement::supportsService( const OUString& _rServiceName )
114{
115 return cppu::supportsService(this, _rServiceName);
116}
117
119{
121}
122
123// OComponentHelper
125{
126 {
127 MutexGuard aGuard(m_aMutex);
128 m_pColumns->disposing();
129 m_xAggregateAsParameters = nullptr;
130 }
132}
133
134// css::sdbcx::XColumnsSupplier
135Reference< css::container::XNameAccess > OPreparedStatement::getColumns()
136{
137 MutexGuard aGuard(m_aMutex);
138 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
139
140 // do we have to populate the columns
141 if (!m_pColumns->isInitialized())
142 {
143 try
144 {
145 Reference< XResultSetMetaDataSupplier > xSuppMeta( m_xAggregateAsSet, UNO_QUERY_THROW );
146 Reference< XResultSetMetaData > xMetaData( xSuppMeta->getMetaData(), UNO_SET_THROW );
147
148 Reference< XConnection > xConn( getConnection(), UNO_SET_THROW );
149 Reference< XDatabaseMetaData > xDBMeta( xConn->getMetaData(), UNO_SET_THROW );
150
151 for (sal_Int32 i = 0, nCount = xMetaData->getColumnCount(); i < nCount; ++i)
152 {
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 );
161
162 m_pColumns->append(aName, pColumn.get());
163 }
164 }
165 catch (const SQLException& )
166 {
167 DBG_UNHANDLED_EXCEPTION("dbaccess");
168 }
169 m_pColumns->setInitialized();
170 }
171 return m_pColumns.get();
172}
173
174// XResultSetMetaDataSupplier
175Reference< XResultSetMetaData > OPreparedStatement::getMetaData()
176{
177 MutexGuard aGuard(m_aMutex);
178 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
179 return Reference< XResultSetMetaDataSupplier >( m_xAggregateAsSet, UNO_QUERY_THROW )->getMetaData();
180}
181
182// XPreparedStatement
183Reference< XResultSet > OPreparedStatement::executeQuery()
184{
185 MutexGuard aGuard(m_aMutex);
186 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
187
189
190 Reference< XResultSet > xResultSet;
191 Reference< XResultSet > xDrvResultSet = Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeQuery();
192 if (xDrvResultSet.is())
193 {
194 xResultSet = new OResultSet(xDrvResultSet, *this, m_pColumns->isCaseSensitive());
195
196 // keep the resultset weak
197 m_aResultSet = xResultSet;
198 }
199 return xResultSet;
200}
201
203{
204 MutexGuard aGuard(m_aMutex);
205 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
206
208
209 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeUpdate();
210}
211
213{
214 MutexGuard aGuard(m_aMutex);
215 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
216
218
219 return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->execute();
220}
221
222Reference< XConnection > OPreparedStatement::getConnection()
223{
224 return Reference< XConnection > (m_xParent, UNO_QUERY);
225}
226
227// XParameters
228void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType )
229{
230 MutexGuard aGuard(m_aMutex);
231 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
232
233 m_xAggregateAsParameters->setNull(parameterIndex, sqlType);
234}
235
236void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& typeName )
237{
238 MutexGuard aGuard(m_aMutex);
239 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
240
241 m_xAggregateAsParameters->setObjectNull(parameterIndex, sqlType, typeName);
242}
243
244void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x )
245{
246 MutexGuard aGuard(m_aMutex);
247 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
248
249 m_xAggregateAsParameters->setBoolean(parameterIndex, x);
250}
251
252void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x )
253{
254 MutexGuard aGuard(m_aMutex);
255 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
256
257 m_xAggregateAsParameters->setByte(parameterIndex, x);
258}
259
260void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x )
261{
262 MutexGuard aGuard(m_aMutex);
263 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
264
265 m_xAggregateAsParameters->setShort(parameterIndex, x);
266}
267
268void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x )
269{
270 MutexGuard aGuard(m_aMutex);
271 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
272
273 m_xAggregateAsParameters->setInt(parameterIndex, x);
274}
275
276void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x )
277{
278 MutexGuard aGuard(m_aMutex);
279 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
280
281 m_xAggregateAsParameters->setLong(parameterIndex, x);
282}
283
284void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x )
285{
286 MutexGuard aGuard(m_aMutex);
287 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
288
289 m_xAggregateAsParameters->setFloat(parameterIndex, x);
290}
291
292void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x )
293{
294 MutexGuard aGuard(m_aMutex);
295 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
296
297 m_xAggregateAsParameters->setDouble(parameterIndex, x);
298}
299
300void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x )
301{
302 MutexGuard aGuard(m_aMutex);
303 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
304
305 m_xAggregateAsParameters->setString(parameterIndex, x);
306}
307
308void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x )
309{
310 MutexGuard aGuard(m_aMutex);
311 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
312
313 m_xAggregateAsParameters->setBytes(parameterIndex, x);
314}
315
316void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const css::util::Date& x )
317{
318 MutexGuard aGuard(m_aMutex);
319 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
320
321 m_xAggregateAsParameters->setDate(parameterIndex, x);
322}
323
324void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const css::util::Time& x )
325{
326 MutexGuard aGuard(m_aMutex);
327 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
328
329 m_xAggregateAsParameters->setTime(parameterIndex, x);
330}
331
332void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const css::util::DateTime& x )
333{
334 MutexGuard aGuard(m_aMutex);
335 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
336
337 m_xAggregateAsParameters->setTimestamp(parameterIndex, x);
338}
339
340void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length )
341{
342 MutexGuard aGuard(m_aMutex);
343 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
344
345 m_xAggregateAsParameters->setBinaryStream(parameterIndex, x, length);
346}
347
348void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length )
349{
350 MutexGuard aGuard(m_aMutex);
351 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
352
353 m_xAggregateAsParameters->setCharacterStream(parameterIndex, x, length);
354}
355
356void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x )
357{
358 MutexGuard aGuard(m_aMutex);
359 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
360
361 m_xAggregateAsParameters->setObject(parameterIndex, x);
362}
363
364void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 targetSqlType, sal_Int32 scale )
365{
366 MutexGuard aGuard(m_aMutex);
367 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
368
369 m_xAggregateAsParameters->setObjectWithInfo(parameterIndex, x, targetSqlType, scale);
370}
371
372void SAL_CALL OPreparedStatement::setRef( sal_Int32 parameterIndex, const Reference< XRef >& x )
373{
374 MutexGuard aGuard(m_aMutex);
375 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
376
377 m_xAggregateAsParameters->setRef(parameterIndex, x);
378}
379
380void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x )
381{
382 MutexGuard aGuard(m_aMutex);
383 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
384
385 m_xAggregateAsParameters->setBlob(parameterIndex, x);
386}
387
388void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x )
389{
390 MutexGuard aGuard(m_aMutex);
391 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
392
393 m_xAggregateAsParameters->setClob(parameterIndex, x);
394}
395
396void SAL_CALL OPreparedStatement::setArray( sal_Int32 parameterIndex, const Reference< XArray >& x )
397{
398 MutexGuard aGuard(m_aMutex);
399 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
400
401 m_xAggregateAsParameters->setArray(parameterIndex, x);
402}
403
405{
406 MutexGuard aGuard(m_aMutex);
407 ::connectivity::checkDisposed(WeakComponentImplHelper::rBHelper.bDisposed);
408
409 m_xAggregateAsParameters->clearParameters();
410}
411
412/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL acquire() noexcept override
Definition: statement.cxx:111
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: statement.cxx:64
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: statement.cxx:83
css::uno::WeakReferenceHelper m_aResultSet
Definition: statement.hxx:55
virtual void SAL_CALL release() noexcept override
Definition: statement.cxx:116
virtual void SAL_CALL disposing() override
Definition: statement.cxx:131
void disposeResultSet()
Definition: statement.cxx:121
css::uno::Reference< css::beans::XPropertySet > m_xAggregateAsSet
Definition: statement.hxx:56
css::uno::Reference< css::uno::XInterface > m_xParent
Definition: apitools.hxx:33
mutable::osl::Mutex m_aMutex
virtual void SAL_CALL setBytes(sal_Int32 parameterIndex, const css::uno::Sequence< sal_Int8 > &x) override
virtual sal_Bool SAL_CALL execute() override
virtual css::uno::Reference< css::sdbc::XResultSetMetaData > SAL_CALL getMetaData() override
virtual void SAL_CALL setTimestamp(sal_Int32 parameterIndex, const css::util::DateTime &x) override
virtual void SAL_CALL setObject(sal_Int32 parameterIndex, const css::uno::Any &x) override
virtual void SAL_CALL setDate(sal_Int32 parameterIndex, const css::util::Date &x) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
virtual void SAL_CALL setNull(sal_Int32 parameterIndex, sal_Int32 sqlType) override
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnection() override
virtual void SAL_CALL setObjectNull(sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString &typeName) override
virtual void SAL_CALL setByte(sal_Int32 parameterIndex, sal_Int8 x) override
virtual void SAL_CALL setTime(sal_Int32 parameterIndex, const css::util::Time &x) override
virtual void SAL_CALL setArray(sal_Int32 parameterIndex, const css::uno::Reference< css::sdbc::XArray > &x) override
virtual void SAL_CALL setRef(sal_Int32 parameterIndex, const css::uno::Reference< css::sdbc::XRef > &x) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
virtual void SAL_CALL setCharacterStream(sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream > &x, sal_Int32 length) override
virtual void SAL_CALL acquire() noexcept override
virtual void SAL_CALL setBoolean(sal_Int32 parameterIndex, sal_Bool x) override
virtual void SAL_CALL setObjectWithInfo(sal_Int32 parameterIndex, const css::uno::Any &x, sal_Int32 targetSqlType, sal_Int32 scale) override
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL executeQuery() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getColumns() override
virtual void SAL_CALL setBinaryStream(sal_Int32 parameterIndex, const css::uno::Reference< css::io::XInputStream > &x, sal_Int32 length) override
std::unique_ptr< OColumns > m_pColumns
virtual void SAL_CALL setDouble(sal_Int32 parameterIndex, double x) override
virtual void SAL_CALL setShort(sal_Int32 parameterIndex, sal_Int16 x) override
virtual void SAL_CALL setString(sal_Int32 parameterIndex, const OUString &x) override
virtual void SAL_CALL release() noexcept override
virtual void SAL_CALL setInt(sal_Int32 parameterIndex, sal_Int32 x) override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual sal_Int32 SAL_CALL executeUpdate() override
virtual void SAL_CALL setClob(sal_Int32 parameterIndex, const css::uno::Reference< css::sdbc::XClob > &x) override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL setBlob(sal_Int32 parameterIndex, const css::uno::Reference< css::sdbc::XBlob > &x) override
virtual void SAL_CALL clearParameters() override
virtual void SAL_CALL disposing() override
css::uno::Reference< css::sdbc::XParameters > m_xAggregateAsParameters
virtual void SAL_CALL setFloat(sal_Int32 parameterIndex, float x) override
virtual void SAL_CALL setLong(sal_Int32 parameterIndex, sal_Int64 x) override
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
float x
OUString aName
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
OUString typeName
constexpr OUStringLiteral SERVICE_SDBC_PREPAREDSTATEMENT
Definition: strings.hxx:168
constexpr OUStringLiteral SERVICE_SDB_PREPAREDSTATEMENT
Definition: strings.hxx:185
unsigned char sal_Bool
signed char sal_Int8
const SvXMLTokenMapEntry aTypes[]