Line data Source code
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 "dbastrings.hrc"
22 :
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 : #include <com/sun/star/sdbc/XConnection.hpp>
25 : #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
26 :
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>
37 :
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;
46 :
47 :
48 113 : OPreparedStatement::OPreparedStatement(const Reference< XConnection > & _xConn,
49 : const Reference< XInterface > & _xStatement)
50 113 : :OStatementBase(_xConn, _xStatement)
51 : {
52 113 : m_xAggregateAsParameters = Reference< XParameters >( m_xAggregateAsSet, UNO_QUERY_THROW );
53 :
54 113 : Reference<XDatabaseMetaData> xMeta = _xConn->getMetaData();
55 113 : m_pColumns = new OColumns(*this, m_aMutex, xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers(),::std::vector< OUString>(), NULL,NULL);
56 113 : }
57 :
58 339 : OPreparedStatement::~OPreparedStatement()
59 : {
60 113 : m_pColumns->acquire();
61 113 : m_pColumns->disposing();
62 113 : delete m_pColumns;
63 :
64 226 : }
65 :
66 : // com::sun::star::lang::XTypeProvider
67 0 : Sequence< Type > OPreparedStatement::getTypes() throw (RuntimeException, std::exception)
68 : {
69 0 : OTypeCollection aTypes(cppu::UnoType<XServiceInfo>::get(),
70 0 : cppu::UnoType<XPreparedStatement>::get(),
71 0 : cppu::UnoType<XParameters>::get(),
72 0 : cppu::UnoType<XResultSetMetaDataSupplier>::get(),
73 0 : cppu::UnoType<XColumnsSupplier>::get(),
74 0 : OStatementBase::getTypes() );
75 :
76 0 : return aTypes.getTypes();
77 : }
78 :
79 0 : Sequence< sal_Int8 > OPreparedStatement::getImplementationId() throw (RuntimeException, std::exception)
80 : {
81 0 : return css::uno::Sequence<sal_Int8>();
82 : }
83 :
84 : // com::sun::star::uno::XInterface
85 14430 : Any OPreparedStatement::queryInterface( const Type & rType ) throw (RuntimeException, std::exception)
86 : {
87 14430 : Any aIface = OStatementBase::queryInterface( rType );
88 14430 : if (!aIface.hasValue())
89 27478 : 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 13739 : static_cast< XPreparedStatement * >( this ));
98 14430 : return aIface;
99 : }
100 :
101 71400 : void OPreparedStatement::acquire() throw ()
102 : {
103 71400 : OStatementBase::acquire();
104 71400 : }
105 :
106 71287 : void OPreparedStatement::release() throw ()
107 : {
108 71287 : OStatementBase::release();
109 71287 : }
110 :
111 : // XServiceInfo
112 0 : OUString OPreparedStatement::getImplementationName( ) throw(RuntimeException, std::exception)
113 : {
114 0 : return OUString("com.sun.star.sdb.OPreparedStatement");
115 : }
116 :
117 0 : sal_Bool OPreparedStatement::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
118 : {
119 0 : return cppu::supportsService(this, _rServiceName);
120 : }
121 :
122 0 : Sequence< OUString > OPreparedStatement::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
123 : {
124 0 : Sequence< OUString > aSNS( 2 );
125 0 : aSNS.getArray()[0] = SERVICE_SDBC_PREPAREDSTATEMENT;
126 0 : aSNS.getArray()[1] = SERVICE_SDB_PREPAREDSTATMENT;
127 0 : return aSNS;
128 : }
129 :
130 : // OComponentHelper
131 113 : void OPreparedStatement::disposing()
132 : {
133 : {
134 113 : MutexGuard aGuard(m_aMutex);
135 113 : m_pColumns->disposing();
136 113 : m_xAggregateAsParameters = NULL;
137 : }
138 113 : OStatementBase::disposing();
139 113 : }
140 :
141 : // ::com::sun::star::sdbcx::XColumnsSupplier
142 0 : Reference< ::com::sun::star::container::XNameAccess > OPreparedStatement::getColumns() throw( RuntimeException, std::exception )
143 : {
144 0 : MutexGuard aGuard(m_aMutex);
145 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
146 :
147 : // do we have to populate the columns
148 0 : if (!m_pColumns->isInitialized())
149 : {
150 : try
151 : {
152 0 : Reference< XResultSetMetaDataSupplier > xSuppMeta( m_xAggregateAsSet, UNO_QUERY_THROW );
153 0 : Reference< XResultSetMetaData > xMetaData( xSuppMeta->getMetaData(), UNO_SET_THROW );
154 :
155 0 : Reference< XConnection > xConn( getConnection(), UNO_SET_THROW );
156 0 : Reference< XDatabaseMetaData > xDBMeta( xConn->getMetaData(), UNO_SET_THROW );
157 :
158 0 : for (sal_Int32 i = 0, nCount = xMetaData->getColumnCount(); i < nCount; ++i)
159 : {
160 : // retrieve the name of the column
161 0 : OUString aName = xMetaData->getColumnName(i + 1);
162 0 : OResultColumn* pColumn = new OResultColumn(xMetaData, i + 1, xDBMeta);
163 0 : m_pColumns->append(aName, pColumn);
164 0 : }
165 : }
166 0 : catch (const SQLException& )
167 : {
168 : DBG_UNHANDLED_EXCEPTION();
169 : }
170 0 : m_pColumns->setInitialized();
171 : }
172 0 : return m_pColumns;
173 : }
174 :
175 : // XResultSetMetaDataSupplier
176 43 : Reference< XResultSetMetaData > OPreparedStatement::getMetaData() throw( SQLException, RuntimeException, std::exception )
177 : {
178 43 : MutexGuard aGuard(m_aMutex);
179 43 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
180 43 : return Reference< XResultSetMetaDataSupplier >( m_xAggregateAsSet, UNO_QUERY_THROW )->getMetaData();
181 : }
182 :
183 : // XPreparedStatement
184 13516 : Reference< XResultSet > OPreparedStatement::executeQuery() throw( SQLException, RuntimeException, std::exception )
185 : {
186 13516 : MutexGuard aGuard(m_aMutex);
187 13516 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
188 :
189 13516 : disposeResultSet();
190 :
191 13516 : Reference< XResultSet > xResultSet;
192 27032 : Reference< XResultSet > xDrvResultSet = Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeQuery();
193 13516 : if (xDrvResultSet.is())
194 : {
195 13516 : xResultSet = new OResultSet(xDrvResultSet, *this, m_pColumns->isCaseSensitive());
196 :
197 : // keep the resultset weak
198 13516 : m_aResultSet = xResultSet;
199 : }
200 27032 : return xResultSet;
201 : }
202 :
203 622 : sal_Int32 OPreparedStatement::executeUpdate() throw( SQLException, RuntimeException, std::exception )
204 : {
205 622 : MutexGuard aGuard(m_aMutex);
206 622 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
207 :
208 622 : disposeResultSet();
209 :
210 622 : return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->executeUpdate();
211 : }
212 :
213 0 : sal_Bool OPreparedStatement::execute() throw( SQLException, RuntimeException, std::exception )
214 : {
215 0 : MutexGuard aGuard(m_aMutex);
216 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
217 :
218 0 : disposeResultSet();
219 :
220 0 : return Reference< XPreparedStatement >( m_xAggregateAsSet, UNO_QUERY_THROW )->execute();
221 : }
222 :
223 55 : Reference< XConnection > OPreparedStatement::getConnection() throw( SQLException, RuntimeException, std::exception )
224 : {
225 55 : return Reference< XConnection > (m_xParent, UNO_QUERY);
226 : }
227 :
228 : // XParameters
229 0 : void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType ) throw(SQLException, RuntimeException, std::exception)
230 : {
231 0 : MutexGuard aGuard(m_aMutex);
232 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
233 :
234 0 : m_xAggregateAsParameters->setNull(parameterIndex, sqlType);
235 0 : }
236 :
237 0 : void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& typeName ) throw(SQLException, RuntimeException, std::exception)
238 : {
239 0 : MutexGuard aGuard(m_aMutex);
240 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
241 :
242 0 : m_xAggregateAsParameters->setObjectNull(parameterIndex, sqlType, typeName);
243 0 : }
244 :
245 0 : void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException, std::exception)
246 : {
247 0 : MutexGuard aGuard(m_aMutex);
248 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
249 :
250 0 : m_xAggregateAsParameters->setBoolean(parameterIndex, x);
251 0 : }
252 :
253 0 : void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException, std::exception)
254 : {
255 0 : MutexGuard aGuard(m_aMutex);
256 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
257 :
258 0 : m_xAggregateAsParameters->setByte(parameterIndex, x);
259 0 : }
260 :
261 0 : void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException, std::exception)
262 : {
263 0 : MutexGuard aGuard(m_aMutex);
264 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
265 :
266 0 : m_xAggregateAsParameters->setShort(parameterIndex, x);
267 0 : }
268 :
269 14101 : void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException, std::exception)
270 : {
271 14101 : MutexGuard aGuard(m_aMutex);
272 14101 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
273 :
274 14101 : m_xAggregateAsParameters->setInt(parameterIndex, x);
275 14101 : }
276 :
277 0 : void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x ) throw(SQLException, RuntimeException, std::exception)
278 : {
279 0 : MutexGuard aGuard(m_aMutex);
280 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
281 :
282 0 : m_xAggregateAsParameters->setLong(parameterIndex, x);
283 0 : }
284 :
285 0 : void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException, std::exception)
286 : {
287 0 : MutexGuard aGuard(m_aMutex);
288 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
289 :
290 0 : m_xAggregateAsParameters->setFloat(parameterIndex, x);
291 0 : }
292 :
293 0 : void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException, std::exception)
294 : {
295 0 : MutexGuard aGuard(m_aMutex);
296 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
297 :
298 0 : m_xAggregateAsParameters->setDouble(parameterIndex, x);
299 0 : }
300 :
301 609 : void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x ) throw(SQLException, RuntimeException, std::exception)
302 : {
303 609 : MutexGuard aGuard(m_aMutex);
304 609 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
305 :
306 609 : m_xAggregateAsParameters->setString(parameterIndex, x);
307 609 : }
308 :
309 0 : void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException, std::exception)
310 : {
311 0 : MutexGuard aGuard(m_aMutex);
312 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
313 :
314 0 : m_xAggregateAsParameters->setBytes(parameterIndex, x);
315 0 : }
316 :
317 0 : void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const ::com::sun::star::util::Date& x ) throw(SQLException, RuntimeException, std::exception)
318 : {
319 0 : MutexGuard aGuard(m_aMutex);
320 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
321 :
322 0 : m_xAggregateAsParameters->setDate(parameterIndex, x);
323 0 : }
324 :
325 0 : void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const ::com::sun::star::util::Time& x ) throw(SQLException, RuntimeException, std::exception)
326 : {
327 0 : MutexGuard aGuard(m_aMutex);
328 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
329 :
330 0 : m_xAggregateAsParameters->setTime(parameterIndex, x);
331 0 : }
332 :
333 0 : void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const ::com::sun::star::util::DateTime& x ) throw(SQLException, RuntimeException, std::exception)
334 : {
335 0 : MutexGuard aGuard(m_aMutex);
336 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
337 :
338 0 : m_xAggregateAsParameters->setTimestamp(parameterIndex, x);
339 0 : }
340 :
341 0 : void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
342 : {
343 0 : MutexGuard aGuard(m_aMutex);
344 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
345 :
346 0 : m_xAggregateAsParameters->setBinaryStream(parameterIndex, x, length);
347 0 : }
348 :
349 0 : void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
350 : {
351 0 : MutexGuard aGuard(m_aMutex);
352 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
353 :
354 0 : m_xAggregateAsParameters->setCharacterStream(parameterIndex, x, length);
355 0 : }
356 :
357 0 : void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException, std::exception)
358 : {
359 0 : MutexGuard aGuard(m_aMutex);
360 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
361 :
362 0 : m_xAggregateAsParameters->setObject(parameterIndex, x);
363 0 : }
364 :
365 0 : void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 targetSqlType, sal_Int32 scale ) throw(SQLException, RuntimeException, std::exception)
366 : {
367 0 : MutexGuard aGuard(m_aMutex);
368 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
369 :
370 0 : m_xAggregateAsParameters->setObjectWithInfo(parameterIndex, x, targetSqlType, scale);
371 0 : }
372 :
373 0 : void SAL_CALL OPreparedStatement::setRef( sal_Int32 parameterIndex, const Reference< XRef >& x ) throw(SQLException, RuntimeException, std::exception)
374 : {
375 0 : MutexGuard aGuard(m_aMutex);
376 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
377 :
378 0 : m_xAggregateAsParameters->setRef(parameterIndex, x);
379 0 : }
380 :
381 0 : void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x ) throw(SQLException, RuntimeException, std::exception)
382 : {
383 0 : MutexGuard aGuard(m_aMutex);
384 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
385 :
386 0 : m_xAggregateAsParameters->setBlob(parameterIndex, x);
387 0 : }
388 :
389 0 : void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x ) throw(SQLException, RuntimeException, std::exception)
390 : {
391 0 : MutexGuard aGuard(m_aMutex);
392 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
393 :
394 0 : m_xAggregateAsParameters->setClob(parameterIndex, x);
395 0 : }
396 :
397 0 : void SAL_CALL OPreparedStatement::setArray( sal_Int32 parameterIndex, const Reference< XArray >& x ) throw(SQLException, RuntimeException, std::exception)
398 : {
399 0 : MutexGuard aGuard(m_aMutex);
400 0 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
401 :
402 0 : m_xAggregateAsParameters->setArray(parameterIndex, x);
403 0 : }
404 :
405 13478 : void SAL_CALL OPreparedStatement::clearParameters( ) throw(SQLException, RuntimeException, std::exception)
406 : {
407 13478 : MutexGuard aGuard(m_aMutex);
408 13478 : ::connectivity::checkDisposed(OComponentHelper::rBHelper.bDisposed);
409 :
410 13478 : m_xAggregateAsParameters->clearParameters();
411 13478 : }
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|