1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: preparedstatement.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef _DBA_COREAPI_PREPAREDSTATEMENT_HXX_
34 #include <preparedstatement.hxx>
36 #ifndef _DBA_COREAPI_RESULTSET_HXX_
37 #include <resultset.hxx>
39 #ifndef _DBA_COREAPI_RESULTCOLUMN_HXX_
40 #include <resultcolumn.hxx>
42 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
43 #include "dbastrings.hrc"
45 #ifndef _COM_SUN_STAR_LANG_DISPOSEDEXCEPTION_HPP_
46 #include <com/sun/star/lang/DisposedException.hpp>
48 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
49 #include <com/sun/star/sdbc/XConnection.hpp>
51 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
52 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
54 #ifndef _COMPHELPER_SEQUENCE_HXX_
55 #include <comphelper/sequence.hxx>
57 #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
58 #include <cppuhelper/typeprovider.hxx>
60 #ifndef _COMPHELPER_PROPERTY_HXX_
61 #include <comphelper/property.hxx>
63 #ifndef _TOOLS_DEBUG_HXX //autogen
64 #include <tools/debug.hxx>
67 using namespace ::com::sun::star::sdbc
;
68 using namespace ::com::sun::star::sdbcx
;
69 using namespace ::com::sun::star::beans
;
70 using namespace ::com::sun::star::uno
;
71 using namespace ::com::sun::star::lang
;
72 using namespace ::cppu
;
73 using namespace ::osl
;
74 using namespace dbaccess
;
76 DBG_NAME(OPreparedStatement
)
78 //--------------------------------------------------------------------------
79 OPreparedStatement::OPreparedStatement(const Reference
< XConnection
> & _xConn
,
80 const Reference
< XInterface
> & _xStatement
)
81 :OStatementBase(_xConn
, _xStatement
)
83 DBG_CTOR(OPreparedStatement
, NULL
);
84 m_xAggregateAsParameters
= Reference
< XParameters
>( m_xAggregateAsSet
, UNO_QUERY_THROW
);
86 Reference
<XDatabaseMetaData
> xMeta
= _xConn
->getMetaData();
87 m_pColumns
= new OColumns(*this, m_aMutex
, xMeta
.is() && xMeta
->supportsMixedCaseQuotedIdentifiers(),::std::vector
< ::rtl::OUString
>(), NULL
,NULL
);
90 //--------------------------------------------------------------------------
91 OPreparedStatement::~OPreparedStatement()
93 m_pColumns
->acquire();
94 m_pColumns
->disposing();
97 DBG_DTOR(OPreparedStatement
, NULL
);
100 // com::sun::star::lang::XTypeProvider
101 //--------------------------------------------------------------------------
102 Sequence
< Type
> OPreparedStatement::getTypes() throw (RuntimeException
)
104 OTypeCollection
aTypes(::getCppuType( (const Reference
< XServiceInfo
> *)0 ),
105 ::getCppuType( (const Reference
< XPreparedStatement
> *)0 ),
106 ::getCppuType( (const Reference
< XParameters
> *)0 ),
107 ::getCppuType( (const Reference
< XResultSetMetaDataSupplier
> *)0 ),
108 ::getCppuType( (const Reference
< XColumnsSupplier
> *)0 ),
109 OStatementBase::getTypes() );
111 return aTypes
.getTypes();
114 //--------------------------------------------------------------------------
115 Sequence
< sal_Int8
> OPreparedStatement::getImplementationId() throw (RuntimeException
)
117 static OImplementationId
* pId
= 0;
120 MutexGuard
aGuard( Mutex::getGlobalMutex() );
123 static OImplementationId aId
;
127 return pId
->getImplementationId();
130 // com::sun::star::uno::XInterface
131 //--------------------------------------------------------------------------
132 Any
OPreparedStatement::queryInterface( const Type
& rType
) throw (RuntimeException
)
134 Any aIface
= OStatementBase::queryInterface( rType
);
135 if (!aIface
.hasValue())
136 aIface
= ::cppu::queryInterface(
138 static_cast< XServiceInfo
* >( this ),
139 static_cast< XParameters
* >( this ),
140 static_cast< XColumnsSupplier
* >( this ),
141 static_cast< XResultSetMetaDataSupplier
* >( this ),
142 static_cast< XPreparedBatchExecution
* >( this ),
143 static_cast< XMultipleResults
* >( this ),
144 static_cast< XPreparedStatement
* >( this ));
148 //--------------------------------------------------------------------------
149 void OPreparedStatement::acquire() throw ()
151 OStatementBase::acquire();
154 //--------------------------------------------------------------------------
155 void OPreparedStatement::release() throw ()
157 OStatementBase::release();
161 //------------------------------------------------------------------------------
162 rtl::OUString
OPreparedStatement::getImplementationName( ) throw(RuntimeException
)
164 return rtl::OUString::createFromAscii("com.sun.star.sdb.OPreparedStatement");
167 //------------------------------------------------------------------------------
168 sal_Bool
OPreparedStatement::supportsService( const ::rtl::OUString
& _rServiceName
) throw (RuntimeException
)
170 return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName
, sal_True
).getLength() != 0;
173 //------------------------------------------------------------------------------
174 Sequence
< ::rtl::OUString
> OPreparedStatement::getSupportedServiceNames( ) throw (RuntimeException
)
176 Sequence
< ::rtl::OUString
> aSNS( 2 );
177 aSNS
.getArray()[0] = SERVICE_SDBC_PREPAREDSTATEMENT
;
178 aSNS
.getArray()[1] = SERVICE_SDB_PREPAREDSTATMENT
;
183 //------------------------------------------------------------------------------
184 void OPreparedStatement::disposing()
187 MutexGuard
aGuard(m_aMutex
);
188 m_pColumns
->disposing();
189 m_xAggregateAsParameters
= NULL
;
191 OStatementBase::disposing();
194 // ::com::sun::star::sdbcx::XColumnsSupplier
195 //------------------------------------------------------------------------------
196 Reference
< ::com::sun::star::container::XNameAccess
> OPreparedStatement::getColumns(void) throw( RuntimeException
)
198 MutexGuard
aGuard(m_aMutex
);
199 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
201 // do we have to populate the columns
202 if (!m_pColumns
->isInitialized())
207 Reference
< XResultSetMetaData
> xMetaData
= Reference
< XResultSetMetaDataSupplier
>(m_xAggregateAsSet
, UNO_QUERY
)->getMetaData();
208 // do we have columns
209 if ( xMetaData
.is() )
211 Reference
< XDatabaseMetaData
> xDBMeta
;
212 Reference
< XConnection
> xConn( getConnection() );
214 xDBMeta
= xConn
->getMetaData();
216 for (sal_Int32 i
= 0, nCount
= xMetaData
->getColumnCount(); i
< nCount
; ++i
)
218 // retrieve the name of the column
219 rtl::OUString aName
= xMetaData
->getColumnName(i
+ 1);
220 OResultColumn
* pColumn
= new OResultColumn(xMetaData
, i
+ 1, xDBMeta
);
221 m_pColumns
->append(aName
, pColumn
);
228 m_pColumns
->setInitialized();
233 // XResultSetMetaDataSupplier
234 //------------------------------------------------------------------------------
235 Reference
< XResultSetMetaData
> OPreparedStatement::getMetaData(void) throw( SQLException
, RuntimeException
)
237 MutexGuard
aGuard(m_aMutex
);
238 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
239 return Reference
< XResultSetMetaDataSupplier
>(m_xAggregateAsSet
, UNO_QUERY
)->getMetaData();
242 // XPreparedStatement
243 //------------------------------------------------------------------------------
244 Reference
< XResultSet
> OPreparedStatement::executeQuery() throw( SQLException
, RuntimeException
)
246 MutexGuard
aGuard(m_aMutex
);
247 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
251 Reference
< XResultSet
> xResultSet
;
252 Reference
< XResultSet
> xDrvResultSet
= Reference
< XPreparedStatement
>(m_xAggregateAsSet
, UNO_QUERY
)->executeQuery();
253 if (xDrvResultSet
.is())
255 xResultSet
= new OResultSet(xDrvResultSet
, *this, m_pColumns
->isCaseSensitive());
257 // keep the resultset weak
258 m_aResultSet
= xResultSet
;
263 //------------------------------------------------------------------------------
264 sal_Int32
OPreparedStatement::executeUpdate() throw( SQLException
, RuntimeException
)
266 MutexGuard
aGuard(m_aMutex
);
267 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
271 return Reference
< XPreparedStatement
>(m_xAggregateAsSet
, UNO_QUERY
)->executeUpdate();
274 //------------------------------------------------------------------------------
275 sal_Bool
OPreparedStatement::execute() throw( SQLException
, RuntimeException
)
277 MutexGuard
aGuard(m_aMutex
);
278 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
281 return Reference
< XPreparedStatement
>(m_xAggregateAsSet
, UNO_QUERY
)->execute();
284 //------------------------------------------------------------------------------
285 Reference
< XConnection
> OPreparedStatement::getConnection(void) throw( SQLException
, RuntimeException
)
287 return Reference
< XConnection
> (m_xParent
, UNO_QUERY
);
291 //------------------------------------------------------------------------------
292 void SAL_CALL
OPreparedStatement::setNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
) throw(SQLException
, RuntimeException
)
294 MutexGuard
aGuard(m_aMutex
);
295 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
297 m_xAggregateAsParameters
->setNull(parameterIndex
, sqlType
);
300 //------------------------------------------------------------------------------
301 void SAL_CALL
OPreparedStatement::setObjectNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const ::rtl::OUString
& typeName
) throw(SQLException
, RuntimeException
)
303 MutexGuard
aGuard(m_aMutex
);
304 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
306 m_xAggregateAsParameters
->setObjectNull(parameterIndex
, sqlType
, typeName
);
309 //------------------------------------------------------------------------------
310 void SAL_CALL
OPreparedStatement::setBoolean( sal_Int32 parameterIndex
, sal_Bool x
) throw(SQLException
, RuntimeException
)
312 MutexGuard
aGuard(m_aMutex
);
313 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
315 m_xAggregateAsParameters
->setBoolean(parameterIndex
, x
);
318 //------------------------------------------------------------------------------
319 void SAL_CALL
OPreparedStatement::setByte( sal_Int32 parameterIndex
, sal_Int8 x
) throw(SQLException
, RuntimeException
)
321 MutexGuard
aGuard(m_aMutex
);
322 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
324 m_xAggregateAsParameters
->setByte(parameterIndex
, x
);
327 //------------------------------------------------------------------------------
328 void SAL_CALL
OPreparedStatement::setShort( sal_Int32 parameterIndex
, sal_Int16 x
) throw(SQLException
, RuntimeException
)
330 MutexGuard
aGuard(m_aMutex
);
331 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
333 m_xAggregateAsParameters
->setShort(parameterIndex
, x
);
336 //------------------------------------------------------------------------------
337 void SAL_CALL
OPreparedStatement::setInt( sal_Int32 parameterIndex
, sal_Int32 x
) throw(SQLException
, RuntimeException
)
339 MutexGuard
aGuard(m_aMutex
);
340 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
342 m_xAggregateAsParameters
->setInt(parameterIndex
, x
);
345 //------------------------------------------------------------------------------
346 void SAL_CALL
OPreparedStatement::setLong( sal_Int32 parameterIndex
, sal_Int64 x
) throw(SQLException
, RuntimeException
)
348 MutexGuard
aGuard(m_aMutex
);
349 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
351 m_xAggregateAsParameters
->setLong(parameterIndex
, x
);
354 //------------------------------------------------------------------------------
355 void SAL_CALL
OPreparedStatement::setFloat( sal_Int32 parameterIndex
, float x
) throw(SQLException
, RuntimeException
)
357 MutexGuard
aGuard(m_aMutex
);
358 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
360 m_xAggregateAsParameters
->setFloat(parameterIndex
, x
);
363 //------------------------------------------------------------------------------
364 void SAL_CALL
OPreparedStatement::setDouble( sal_Int32 parameterIndex
, double x
) throw(SQLException
, RuntimeException
)
366 MutexGuard
aGuard(m_aMutex
);
367 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
369 m_xAggregateAsParameters
->setDouble(parameterIndex
, x
);
372 //------------------------------------------------------------------------------
373 void SAL_CALL
OPreparedStatement::setString( sal_Int32 parameterIndex
, const ::rtl::OUString
& x
) throw(SQLException
, RuntimeException
)
375 MutexGuard
aGuard(m_aMutex
);
376 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
378 m_xAggregateAsParameters
->setString(parameterIndex
, x
);
381 //------------------------------------------------------------------------------
382 void SAL_CALL
OPreparedStatement::setBytes( sal_Int32 parameterIndex
, const Sequence
< sal_Int8
>& x
) throw(SQLException
, RuntimeException
)
384 MutexGuard
aGuard(m_aMutex
);
385 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
387 m_xAggregateAsParameters
->setBytes(parameterIndex
, x
);
390 //------------------------------------------------------------------------------
391 void SAL_CALL
OPreparedStatement::setDate( sal_Int32 parameterIndex
, const ::com::sun::star::util::Date
& x
) throw(SQLException
, RuntimeException
)
393 MutexGuard
aGuard(m_aMutex
);
394 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
396 m_xAggregateAsParameters
->setDate(parameterIndex
, x
);
399 //------------------------------------------------------------------------------
400 void SAL_CALL
OPreparedStatement::setTime( sal_Int32 parameterIndex
, const ::com::sun::star::util::Time
& x
) throw(SQLException
, RuntimeException
)
402 MutexGuard
aGuard(m_aMutex
);
403 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
405 m_xAggregateAsParameters
->setTime(parameterIndex
, x
);
408 //------------------------------------------------------------------------------
409 void SAL_CALL
OPreparedStatement::setTimestamp( sal_Int32 parameterIndex
, const ::com::sun::star::util::DateTime
& x
) throw(SQLException
, RuntimeException
)
411 MutexGuard
aGuard(m_aMutex
);
412 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
414 m_xAggregateAsParameters
->setTimestamp(parameterIndex
, x
);
417 //------------------------------------------------------------------------------
418 void SAL_CALL
OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
)
420 MutexGuard
aGuard(m_aMutex
);
421 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
423 m_xAggregateAsParameters
->setBinaryStream(parameterIndex
, x
, length
);
426 //------------------------------------------------------------------------------
427 void SAL_CALL
OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
)
429 MutexGuard
aGuard(m_aMutex
);
430 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
432 m_xAggregateAsParameters
->setCharacterStream(parameterIndex
, x
, length
);
435 //------------------------------------------------------------------------------
436 void SAL_CALL
OPreparedStatement::setObject( sal_Int32 parameterIndex
, const Any
& x
) throw(SQLException
, RuntimeException
)
438 MutexGuard
aGuard(m_aMutex
);
439 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
441 m_xAggregateAsParameters
->setObject(parameterIndex
, x
);
444 //------------------------------------------------------------------------------
445 void SAL_CALL
OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex
, const Any
& x
, sal_Int32 targetSqlType
, sal_Int32 scale
) throw(SQLException
, RuntimeException
)
447 MutexGuard
aGuard(m_aMutex
);
448 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
450 m_xAggregateAsParameters
->setObjectWithInfo(parameterIndex
, x
, targetSqlType
, scale
);
453 //------------------------------------------------------------------------------
454 void SAL_CALL
OPreparedStatement::setRef( sal_Int32 parameterIndex
, const Reference
< XRef
>& x
) throw(SQLException
, RuntimeException
)
456 MutexGuard
aGuard(m_aMutex
);
457 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
459 m_xAggregateAsParameters
->setRef(parameterIndex
, x
);
462 //------------------------------------------------------------------------------
463 void SAL_CALL
OPreparedStatement::setBlob( sal_Int32 parameterIndex
, const Reference
< XBlob
>& x
) throw(SQLException
, RuntimeException
)
465 MutexGuard
aGuard(m_aMutex
);
466 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
468 m_xAggregateAsParameters
->setBlob(parameterIndex
, x
);
471 //------------------------------------------------------------------------------
472 void SAL_CALL
OPreparedStatement::setClob( sal_Int32 parameterIndex
, const Reference
< XClob
>& x
) throw(SQLException
, RuntimeException
)
474 MutexGuard
aGuard(m_aMutex
);
475 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
477 m_xAggregateAsParameters
->setClob(parameterIndex
, x
);
480 //------------------------------------------------------------------------------
481 void SAL_CALL
OPreparedStatement::setArray( sal_Int32 parameterIndex
, const Reference
< XArray
>& x
) throw(SQLException
, RuntimeException
)
483 MutexGuard
aGuard(m_aMutex
);
484 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
486 m_xAggregateAsParameters
->setArray(parameterIndex
, x
);
489 //------------------------------------------------------------------------------
490 void SAL_CALL
OPreparedStatement::clearParameters( ) throw(SQLException
, RuntimeException
)
492 MutexGuard
aGuard(m_aMutex
);
493 ::connectivity::checkDisposed(OComponentHelper::rBHelper
.bDisposed
);
495 m_xAggregateAsParameters
->clearParameters();