Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / odbcbase / OStatement.cxx
blob396facf5079000f299333babd7d0f8f023c7476f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <osl/diagnose.h>
32 #include "odbc/OStatement.hxx"
33 #include "odbc/OConnection.hxx"
34 #include "odbc/OResultSet.hxx"
35 #include <comphelper/property.hxx>
36 #include "odbc/OTools.hxx"
37 #include <comphelper/uno3.hxx>
38 #include <osl/thread.h>
39 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
40 #include <com/sun/star/sdbc/ResultSetType.hpp>
41 #include <com/sun/star/sdbc/FetchDirection.hpp>
42 #include <com/sun/star/lang/DisposedException.hpp>
43 #include <comphelper/sequence.hxx>
44 #include <cppuhelper/typeprovider.hxx>
45 #include <comphelper/extract.hxx>
46 #include <comphelper/types.hxx>
47 #include "diagnose_ex.h"
48 #include <algorithm>
49 #include "resource/common_res.hrc"
50 #include "connectivity/dbexception.hxx"
52 using namespace ::comphelper;
54 #define THROW_SQL(x) \
55 OTools::ThrowException(m_pConnection,x,m_aStatementHandle,SQL_HANDLE_STMT,*this)
59 using namespace connectivity::odbc;
60 //------------------------------------------------------------------------------
61 using namespace com::sun::star::uno;
62 using namespace com::sun::star::lang;
63 using namespace com::sun::star::beans;
64 using namespace com::sun::star::sdbc;
65 using namespace com::sun::star::sdbcx;
66 using namespace com::sun::star::container;
67 using namespace com::sun::star::io;
68 using namespace com::sun::star::util;
69 //------------------------------------------------------------------------------
70 OStatement_Base::OStatement_Base(OConnection* _pConnection )
71 :OStatement_BASE(m_aMutex)
72 ,OPropertySetHelper(OStatement_BASE::rBHelper)
73 ,m_pConnection(_pConnection)
74 ,m_aStatementHandle(SQL_NULL_HANDLE)
75 ,m_pRowStatusArray(0)
76 ,rBHelper(OStatement_BASE::rBHelper)
78 osl_incrementInterlockedCount( &m_refCount );
79 m_pConnection->acquire();
80 m_aStatementHandle = m_pConnection->createStatementHandle();
82 //setMaxFieldSize(0);
83 // Don't do this. By ODBC spec, "0" is the default for the SQL_ATTR_MAX_LENGTH attribute. We once introduced
84 // this line since an PostgreSQL ODBC driver had a default other than 0. However, current drivers (at least 8.3
85 // and later) have a proper default of 0, so there should be no need anymore.
86 // On the other hand, the NotesSQL driver (IBM's ODBC driver for the Lotus Notes series) wrongly interprets
87 // "0" as "0", whereas the ODBC spec says it should in fact mean "unlimited".
88 // So, removing this line seems to be the best option for now.
89 // If we ever again encounter a ODBC driver which needs this option, then we should introduce a data source
90 // setting for it, instead of unconditionally doing it.
92 osl_decrementInterlockedCount( &m_refCount );
94 // -----------------------------------------------------------------------------
95 OStatement_Base::~OStatement_Base()
97 OSL_ENSURE(!m_aStatementHandle,"Sohould ne null here!");
99 //------------------------------------------------------------------------------
100 void OStatement_Base::disposeResultSet()
102 // free the cursor if alive
103 Reference< XComponent > xComp(m_xResultSet.get(), UNO_QUERY);
104 if (xComp.is())
105 xComp->dispose();
106 m_xResultSet = Reference< XResultSet>();
108 // -----------------------------------------------------------------------------
109 void SAL_CALL OStatement_Base::disposing(void)
111 ::osl::MutexGuard aGuard(m_aMutex);
113 disposeResultSet();
114 ::comphelper::disposeComponent(m_xGeneratedStatement);
116 OSL_ENSURE(m_aStatementHandle,"OStatement_BASE2::disposing: StatementHandle is null!");
117 if (m_pConnection)
119 m_pConnection->freeStatementHandle(m_aStatementHandle);
120 m_pConnection->release();
121 m_pConnection = NULL;
123 OSL_ENSURE(!m_aStatementHandle,"Sohould ne null here!");
125 OStatement_BASE::disposing();
127 //------------------------------------------------------------------------------
128 void OStatement_BASE2::disposing()
130 ::osl::MutexGuard aGuard(m_aMutex);
132 dispose_ChildImpl();
133 OStatement_Base::disposing();
135 //-----------------------------------------------------------------------------
136 void SAL_CALL OStatement_BASE2::release() throw()
138 relase_ChildImpl();
140 //-----------------------------------------------------------------------------
141 Any SAL_CALL OStatement_Base::queryInterface( const Type & rType ) throw(RuntimeException)
143 if ( m_pConnection && !m_pConnection->isAutoRetrievingEnabled() && rType == ::getCppuType( (const Reference< XGeneratedResultSet > *)0 ) )
144 return Any();
145 Any aRet = OStatement_BASE::queryInterface(rType);
146 return aRet.hasValue() ? aRet : OPropertySetHelper::queryInterface(rType);
148 // -------------------------------------------------------------------------
149 Sequence< Type > SAL_CALL OStatement_Base::getTypes( ) throw(RuntimeException)
151 ::cppu::OTypeCollection aTypes( ::getCppuType( (const Reference< XMultiPropertySet > *)0 ),
152 ::getCppuType( (const Reference< XFastPropertySet > *)0 ),
153 ::getCppuType( (const Reference< XPropertySet > *)0 ));
154 Sequence< Type > aOldTypes = OStatement_BASE::getTypes();
155 if ( m_pConnection && !m_pConnection->isAutoRetrievingEnabled() )
157 ::std::remove(aOldTypes.getArray(),aOldTypes.getArray() + aOldTypes.getLength(),
158 ::getCppuType( (const Reference< XGeneratedResultSet > *)0 ));
159 aOldTypes.realloc(aOldTypes.getLength() - 1);
162 return ::comphelper::concatSequences(aTypes.getTypes(),aOldTypes);
164 // -------------------------------------------------------------------------
165 Reference< XResultSet > SAL_CALL OStatement_Base::getGeneratedValues( ) throw (SQLException, RuntimeException)
167 OSL_ENSURE( m_pConnection && m_pConnection->isAutoRetrievingEnabled(),"Illegal call here. isAutoRetrievingEnabled is false!");
168 Reference< XResultSet > xRes;
169 if ( m_pConnection )
171 ::rtl::OUString sStmt = m_pConnection->getTransformedGeneratedStatement(m_sSqlStatement);
172 if ( !sStmt.isEmpty() )
174 ::comphelper::disposeComponent(m_xGeneratedStatement);
175 m_xGeneratedStatement = m_pConnection->createStatement();
176 xRes = m_xGeneratedStatement->executeQuery(sStmt);
179 return xRes;
181 // -----------------------------------------------------------------------------
182 void SAL_CALL OStatement_Base::cancel( ) throw(RuntimeException)
184 ::osl::MutexGuard aGuard( m_aMutex );
185 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
187 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
188 OTools::ThrowException(m_pConnection,N3SQLCancel(m_aStatementHandle),m_aStatementHandle,SQL_HANDLE_STMT,*this);
190 // -------------------------------------------------------------------------
192 void SAL_CALL OStatement_Base::close( ) throw(SQLException, RuntimeException)
195 ::osl::MutexGuard aGuard( m_aMutex );
196 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
199 dispose();
201 // -------------------------------------------------------------------------
203 void SAL_CALL OStatement::clearBatch( ) throw(SQLException, RuntimeException)
207 // -------------------------------------------------------------------------
209 void OStatement_Base::reset() throw (SQLException)
211 ::osl::MutexGuard aGuard( m_aMutex );
212 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
215 clearWarnings ();
217 if (m_xResultSet.get().is())
219 clearMyResultSet();
221 if(m_aStatementHandle)
223 THROW_SQL(N3SQLFreeStmt(m_aStatementHandle, SQL_CLOSE));
226 //--------------------------------------------------------------------
227 // clearMyResultSet
228 // If a ResultSet was created for this Statement, close it
229 //--------------------------------------------------------------------
231 void OStatement_Base::clearMyResultSet () throw (SQLException)
233 ::osl::MutexGuard aGuard( m_aMutex );
234 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
238 Reference<XCloseable> xCloseable;
239 if ( ::comphelper::query_interface( m_xResultSet.get(), xCloseable ) )
240 xCloseable->close();
242 catch( const DisposedException& ) { }
244 m_xResultSet = Reference< XResultSet >();
246 //--------------------------------------------------------------------
247 SQLLEN OStatement_Base::getRowCount () throw( SQLException)
249 ::osl::MutexGuard aGuard( m_aMutex );
250 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
253 SQLLEN numRows = 0;
255 try {
256 THROW_SQL(N3SQLRowCount(m_aStatementHandle,&numRows));
258 catch (const SQLException&)
261 return numRows;
263 //--------------------------------------------------------------------
264 // lockIfNecessary
265 // If the given SQL statement contains a 'FOR UPDATE' clause, change
266 // the concurrency to lock so that the row can then be updated. Returns
267 // true if the concurrency has been changed
268 //--------------------------------------------------------------------
270 sal_Bool OStatement_Base::lockIfNecessary (const ::rtl::OUString& sql) throw( SQLException)
272 sal_Bool rc = sal_False;
274 // First, convert the statement to upper case
276 ::rtl::OUString sqlStatement = sql.toAsciiUpperCase ();
278 // Now, look for the FOR UPDATE keywords. If there is any extra white
279 // space between the FOR and UPDATE, this will fail.
281 sal_Int32 index = sqlStatement.indexOf(::rtl::OUString(" FOR UPDATE"));
283 // We found it. Change our concurrency level to ensure that the
284 // row can be updated.
286 if (index > 0)
288 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
291 THROW_SQL((setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CONCURRENCY, SQL_CONCUR_LOCK)));
293 catch (const SQLWarning& warn)
295 // Catch any warnings and place on the warning stack
296 setWarning (warn);
298 rc = sal_True;
301 return rc;
303 //--------------------------------------------------------------------
304 // setWarning
305 // Sets the warning
306 //--------------------------------------------------------------------
308 void OStatement_Base::setWarning (const SQLWarning &ex) throw( SQLException)
310 ::osl::MutexGuard aGuard( m_aMutex );
311 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
314 m_aLastWarning = ex;
317 //--------------------------------------------------------------------
318 // getColumnCount
319 // Return the number of columns in the ResultSet
320 //--------------------------------------------------------------------
322 sal_Int32 OStatement_Base::getColumnCount () throw( SQLException)
324 ::osl::MutexGuard aGuard( m_aMutex );
325 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
328 sal_Int16 numCols = 0;
329 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
331 try {
332 THROW_SQL(N3SQLNumResultCols(m_aStatementHandle,&numCols));
334 catch (const SQLException&)
337 return numCols;
339 // -------------------------------------------------------------------------
341 sal_Bool SAL_CALL OStatement_Base::execute( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
343 ::osl::MutexGuard aGuard( m_aMutex );
344 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
345 m_sSqlStatement = sql;
348 ::rtl::OString aSql(::rtl::OUStringToOString(sql,getOwnConnection()->getTextEncoding()));
350 sal_Bool hasResultSet = sal_False;
351 SQLWarning aWarning;
353 // Reset the statement handle and warning
355 reset();
357 // Check for a 'FOR UPDATE' statement. If present, change
358 // the concurrency to lock
360 lockIfNecessary (sql);
362 // Call SQLExecDirect
363 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
365 try {
366 THROW_SQL(N3SQLExecDirect(m_aStatementHandle, (SDB_ODBC_CHAR*)aSql.getStr(),aSql.getLength()));
368 catch (const SQLWarning& ex) {
370 // Save pointer to warning and save with ResultSet
371 // object once it is created.
373 aWarning = ex;
376 // Now determine if there is a result set associated with
377 // the SQL statement that was executed. Get the column
378 // count, and if it is not zero, there is a result set.
380 if (getColumnCount () > 0)
382 hasResultSet = sal_True;
385 return hasResultSet;
387 //--------------------------------------------------------------------
388 // getResultSet
389 // getResultSet returns the current result as a ResultSet. It
390 // returns NULL if the current result is not a ResultSet.
391 //--------------------------------------------------------------------
392 Reference< XResultSet > OStatement_Base::getResultSet (sal_Bool checkCount) throw( SQLException)
394 ::osl::MutexGuard aGuard( m_aMutex );
395 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
398 if (m_xResultSet.get().is()) // if resultset already retrieved,
400 // throw exception to avoid sequence error
401 ::dbtools::throwFunctionSequenceException(*this,Any());
404 OResultSet* pRs = NULL;
405 sal_Int32 numCols = 1;
407 // If we already know we have result columns, checkCount
408 // is false. This is an optimization to prevent unneeded
409 // calls to getColumnCount
411 if (checkCount)
412 numCols = getColumnCount ();
414 // Only return a result set if there are result columns
416 if (numCols > 0)
418 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
419 pRs = createResulSet();
420 pRs->construct();
422 // Save a copy of our last result set
423 // Changed to save copy at getResultSet.
424 //m_xResultSet = rs;
426 else
427 clearMyResultSet ();
429 return pRs;
431 //--------------------------------------------------------------------
432 // getStmtOption
433 // Invoke SQLGetStmtOption with the given option.
434 //--------------------------------------------------------------------
436 template < typename T, SQLINTEGER BufferLength > T OStatement_Base::getStmtOption (SQLINTEGER fOption, T dflt) const
438 T result (dflt);
439 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
440 N3SQLGetStmtAttr(m_aStatementHandle, fOption, &result, BufferLength, NULL);
441 return result;
443 template < typename T, SQLINTEGER BufferLength > SQLRETURN OStatement_Base::setStmtOption (SQLINTEGER fOption, T value) const
445 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
446 SQLPOINTER sv = reinterpret_cast<SQLPOINTER>(value);
447 return N3SQLSetStmtAttr(m_aStatementHandle, fOption, sv, BufferLength);
449 // -------------------------------------------------------------------------
451 Reference< XResultSet > SAL_CALL OStatement_Base::executeQuery( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
453 ::osl::MutexGuard aGuard( m_aMutex );
454 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
457 Reference< XResultSet > xRS = NULL;
459 // Execute the statement. If execute returns true, a result
460 // set exists.
462 if (execute (sql))
464 xRS = getResultSet (sal_False);
465 m_xResultSet = xRS;
467 else
469 // No ResultSet was produced. Raise an exception
470 m_pConnection->throwGenericSQLException(STR_NO_RESULTSET,*this);
472 return xRS;
474 // -------------------------------------------------------------------------
476 Reference< XConnection > SAL_CALL OStatement_Base::getConnection( ) throw(SQLException, RuntimeException)
478 ::osl::MutexGuard aGuard( m_aMutex );
479 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
481 return (Reference< XConnection >)m_pConnection;
483 // -------------------------------------------------------------------------
485 Any SAL_CALL OStatement::queryInterface( const Type & rType ) throw(RuntimeException)
487 Any aRet = ::cppu::queryInterface(rType,static_cast< XBatchExecution*> (this));
488 return aRet.hasValue() ? aRet : OStatement_Base::queryInterface(rType);
490 // -------------------------------------------------------------------------
492 void SAL_CALL OStatement::addBatch( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
494 ::osl::MutexGuard aGuard( m_aMutex );
495 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
498 m_aBatchList.push_back(sql);
500 // -------------------------------------------------------------------------
501 Sequence< sal_Int32 > SAL_CALL OStatement::executeBatch( ) throw(SQLException, RuntimeException)
503 ::osl::MutexGuard aGuard( m_aMutex );
504 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
507 ::rtl::OString aBatchSql;
508 sal_Int32 nLen = 0;
509 for(::std::list< ::rtl::OUString>::const_iterator i=m_aBatchList.begin();i != m_aBatchList.end();++i,++nLen)
511 aBatchSql += ::rtl::OUStringToOString(*i,getOwnConnection()->getTextEncoding());
512 aBatchSql += ";";
515 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
516 THROW_SQL(N3SQLExecDirect(m_aStatementHandle, (SDB_ODBC_CHAR*)aBatchSql.getStr(),aBatchSql.getLength()));
518 Sequence< sal_Int32 > aRet(nLen);
519 sal_Int32* pArray = aRet.getArray();
520 for(sal_Int32 j=0;j<nLen;++j)
522 SQLRETURN nError = N3SQLMoreResults(m_aStatementHandle);
523 if(nError == SQL_SUCCESS)
525 SQLLEN nRowCount=0;
526 N3SQLRowCount(m_aStatementHandle,&nRowCount);
527 pArray[j] = nRowCount;
530 return aRet;
532 // -------------------------------------------------------------------------
535 sal_Int32 SAL_CALL OStatement_Base::executeUpdate( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
537 ::osl::MutexGuard aGuard( m_aMutex );
538 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
541 sal_Int32 numRows = -1;
543 // Execute the statement. If execute returns false, a
544 // row count exists.
546 if (!execute (sql)) {
547 numRows = getUpdateCount();
549 else {
551 // No update count was produced (a ResultSet was). Raise
552 // an exception
554 ::connectivity::SharedResources aResources;
555 const ::rtl::OUString sError( aResources.getResourceString(STR_NO_ROWCOUNT));
556 throw SQLException (sError, *this,::rtl::OUString(),0,Any());
558 return numRows;
561 // -------------------------------------------------------------------------
563 Reference< XResultSet > SAL_CALL OStatement_Base::getResultSet( ) throw(SQLException, RuntimeException)
565 ::osl::MutexGuard aGuard( m_aMutex );
566 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
569 m_xResultSet = getResultSet(sal_True);
570 return m_xResultSet;
572 // -------------------------------------------------------------------------
574 sal_Int32 SAL_CALL OStatement_Base::getUpdateCount( ) throw(SQLException, RuntimeException)
576 ::osl::MutexGuard aGuard( m_aMutex );
577 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
580 sal_Int32 rowCount = -1;
582 // Only return a row count for SQL statements that did not
583 // return a result set.
585 if (getColumnCount () == 0)
586 rowCount = getRowCount ();
588 return rowCount;
590 // -------------------------------------------------------------------------
592 sal_Bool SAL_CALL OStatement_Base::getMoreResults( ) throw(SQLException, RuntimeException)
594 ::osl::MutexGuard aGuard( m_aMutex );
595 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
598 SQLWarning warning;
599 sal_Bool hasResultSet = sal_False;
601 // clear previous warnings
603 clearWarnings ();
605 // Call SQLMoreResults
606 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
608 try {
609 hasResultSet = N3SQLMoreResults(m_aStatementHandle) == SQL_SUCCESS;
611 catch (const SQLWarning &ex) {
613 // Save pointer to warning and save with ResultSet
614 // object once it is created.
616 warning = ex;
619 // There are more results (it may not be a result set, though)
621 if (hasResultSet)
624 // Now determine if there is a result set associated
625 // with the SQL statement that was executed. Get the
626 // column count, and if it is zero, there is not a
627 // result set.
629 if (getColumnCount () == 0)
630 hasResultSet = sal_False;
633 // Set the warning for the statement, if one was generated
635 setWarning (warning);
637 // Return the result set indicator
639 return hasResultSet;
641 // -------------------------------------------------------------------------
643 // -------------------------------------------------------------------------
644 Any SAL_CALL OStatement_Base::getWarnings( ) throw(SQLException, RuntimeException)
646 ::osl::MutexGuard aGuard( m_aMutex );
647 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
650 return makeAny(m_aLastWarning);
652 // -------------------------------------------------------------------------
654 // -------------------------------------------------------------------------
655 void SAL_CALL OStatement_Base::clearWarnings( ) throw(SQLException, RuntimeException)
657 ::osl::MutexGuard aGuard( m_aMutex );
658 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
661 m_aLastWarning = SQLWarning();
663 // -------------------------------------------------------------------------
664 //------------------------------------------------------------------------------
665 sal_Int64 OStatement_Base::getQueryTimeOut() const
667 return getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_QUERY_TIMEOUT);
669 //------------------------------------------------------------------------------
670 sal_Int64 OStatement_Base::getMaxRows() const
672 return getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_MAX_ROWS);
674 //------------------------------------------------------------------------------
675 sal_Int32 OStatement_Base::getResultSetConcurrency() const
677 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
678 SQLULEN nValue (getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CONCURRENCY));
679 if(nValue == SQL_CONCUR_READ_ONLY)
680 nValue = ResultSetConcurrency::READ_ONLY;
681 else
682 nValue = ResultSetConcurrency::UPDATABLE;
683 return nValue;
685 //------------------------------------------------------------------------------
686 sal_Int32 OStatement_Base::getResultSetType() const
688 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
689 SQLULEN nValue (getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY));
690 switch(nValue)
692 case SQL_CURSOR_FORWARD_ONLY:
693 nValue = ResultSetType::FORWARD_ONLY;
694 break;
695 case SQL_CURSOR_KEYSET_DRIVEN:
696 case SQL_CURSOR_STATIC:
697 nValue = ResultSetType::SCROLL_INSENSITIVE;
698 break;
699 case SQL_CURSOR_DYNAMIC:
700 nValue = ResultSetType::SCROLL_SENSITIVE;
701 break;
702 default:
703 OSL_FAIL("Unknown ODBC Cursor Type");
706 return nValue;
708 //------------------------------------------------------------------------------
709 sal_Int32 OStatement_Base::getFetchDirection() const
711 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
712 SQLULEN nValue (getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_SCROLLABLE));
713 switch(nValue)
715 case SQL_SCROLLABLE:
716 nValue = FetchDirection::REVERSE;
717 break;
718 default:
719 nValue = FetchDirection::FORWARD;
720 break;
723 return nValue;
725 //------------------------------------------------------------------------------
726 sal_Int32 OStatement_Base::getFetchSize() const
728 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
729 return getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_ROW_ARRAY_SIZE);
731 //------------------------------------------------------------------------------
732 sal_Int64 OStatement_Base::getMaxFieldSize() const
734 return getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_MAX_LENGTH);
736 //------------------------------------------------------------------------------
737 ::rtl::OUString OStatement_Base::getCursorName() const
739 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
740 SQLCHAR pName[258];
741 SQLSMALLINT nRealLen = 0;
742 SQLRETURN nRetCode = N3SQLGetCursorName(m_aStatementHandle,(SQLCHAR*)pName,256,&nRealLen);
743 OSL_UNUSED( nRetCode );
744 return ::rtl::OUString::createFromAscii((const char*)pName);
746 //------------------------------------------------------------------------------
747 void OStatement_Base::setQueryTimeOut(sal_Int64 seconds)
749 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
750 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_QUERY_TIMEOUT,seconds);
752 //------------------------------------------------------------------------------
753 void OStatement_Base::setMaxRows(sal_Int64 _par0)
755 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
756 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_MAX_ROWS, _par0);
758 //------------------------------------------------------------------------------
759 void OStatement_Base::setResultSetConcurrency(sal_Int32 _par0)
761 SQLULEN nSet;
762 if(_par0 == ResultSetConcurrency::READ_ONLY)
763 nSet = SQL_CONCUR_READ_ONLY;
764 else
765 nSet = SQL_CONCUR_VALUES;
767 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
768 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CONCURRENCY, nSet);
770 //------------------------------------------------------------------------------
771 void OStatement_Base::setResultSetType(sal_Int32 _par0)
774 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
775 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN);
777 sal_Bool bUseBookmark = isUsingBookmarks();
778 SQLULEN nSet( SQL_UNSPECIFIED );
779 switch(_par0)
781 case ResultSetType::FORWARD_ONLY:
782 nSet = SQL_UNSPECIFIED;
783 break;
784 case ResultSetType::SCROLL_INSENSITIVE:
785 nSet = SQL_INSENSITIVE;
786 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_KEYSET_DRIVEN);
787 break;
788 case ResultSetType::SCROLL_SENSITIVE:
789 if(bUseBookmark)
791 SQLUINTEGER nCurProp = getCursorProperties(SQL_CURSOR_DYNAMIC,sal_True);
792 if((nCurProp & SQL_CA1_BOOKMARK) != SQL_CA1_BOOKMARK) // check if bookmark for this type isn't supported
793 { // we have to test the next one
794 nCurProp = getCursorProperties(SQL_CURSOR_KEYSET_DRIVEN,sal_True);
795 sal_Bool bNotBookmarks = ((nCurProp & SQL_CA1_BOOKMARK) != SQL_CA1_BOOKMARK);
796 nCurProp = getCursorProperties(SQL_CURSOR_KEYSET_DRIVEN,sal_False);
797 nSet = SQL_CURSOR_KEYSET_DRIVEN;
798 if( bNotBookmarks ||
799 ((nCurProp & SQL_CA2_SENSITIVITY_DELETIONS) != SQL_CA2_SENSITIVITY_DELETIONS) ||
800 ((nCurProp & SQL_CA2_SENSITIVITY_ADDITIONS) != SQL_CA2_SENSITIVITY_ADDITIONS))
802 // bookmarks for keyset isn't supported so reset bookmark setting
803 setUsingBookmarks(sal_False);
804 nSet = SQL_CURSOR_DYNAMIC;
807 else
808 nSet = SQL_CURSOR_DYNAMIC;
810 else
811 nSet = SQL_CURSOR_DYNAMIC;
812 if( setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_TYPE, nSet) != SQL_SUCCESS )
814 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_KEYSET_DRIVEN);
816 nSet = SQL_SENSITIVE;
817 break;
818 default:
819 OSL_FAIL( "OStatement_Base::setResultSetType: invalid result set type!" );
820 break;
824 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_SENSITIVITY, nSet);
826 //------------------------------------------------------------------------------
827 void OStatement_Base::setEscapeProcessing( const sal_Bool _bEscapeProc )
829 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
830 SQLULEN nEscapeProc( _bEscapeProc ? SQL_NOSCAN_OFF : SQL_NOSCAN_ON );
831 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_NOSCAN, nEscapeProc);
834 //------------------------------------------------------------------------------
835 void OStatement_Base::setFetchDirection(sal_Int32 _par0)
837 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
838 if(_par0 == FetchDirection::FORWARD)
840 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_SCROLLABLE, SQL_NONSCROLLABLE);
842 else if(_par0 == FetchDirection::REVERSE)
844 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_CURSOR_SCROLLABLE, SQL_SCROLLABLE);
847 //------------------------------------------------------------------------------
848 void OStatement_Base::setFetchSize(sal_Int32 _par0)
850 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
851 OSL_ENSURE(_par0>0,"Illegal fetch size!");
852 if ( _par0 > 0 )
854 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_ROW_ARRAY_SIZE, _par0);
856 if (m_pRowStatusArray)
857 delete[] m_pRowStatusArray;
858 m_pRowStatusArray = new SQLUSMALLINT[_par0];
859 setStmtOption<SQLUSMALLINT*, SQL_IS_POINTER>(SQL_ATTR_ROW_STATUS_PTR, m_pRowStatusArray);
862 //------------------------------------------------------------------------------
863 void OStatement_Base::setMaxFieldSize(sal_Int64 _par0)
865 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
866 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_MAX_LENGTH, _par0);
868 //------------------------------------------------------------------------------
869 void OStatement_Base::setCursorName(const ::rtl::OUString &_par0)
871 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
872 ::rtl::OString aName(::rtl::OUStringToOString(_par0,getOwnConnection()->getTextEncoding()));
873 N3SQLSetCursorName(m_aStatementHandle,(SDB_ODBC_CHAR*)aName.getStr(),(SQLSMALLINT)aName.getLength());
875 // -------------------------------------------------------------------------
876 sal_Bool OStatement_Base::isUsingBookmarks() const
878 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
879 return SQL_UB_OFF != getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_USE_BOOKMARKS, SQL_UB_OFF);
881 // -------------------------------------------------------------------------
882 sal_Bool OStatement_Base::getEscapeProcessing() const
884 OSL_ENSURE( m_aStatementHandle, "StatementHandle is null!" );
885 return SQL_NOSCAN_OFF == getStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_USE_BOOKMARKS, SQL_NOSCAN_OFF);;
887 // -------------------------------------------------------------------------
888 void OStatement_Base::setUsingBookmarks(sal_Bool _bUseBookmark)
890 OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
891 SQLULEN nValue = _bUseBookmark ? SQL_UB_VARIABLE : SQL_UB_OFF;
892 setStmtOption<SQLULEN, SQL_IS_UINTEGER>(SQL_ATTR_USE_BOOKMARKS, nValue);
894 // -------------------------------------------------------------------------
895 ::cppu::IPropertyArrayHelper* OStatement_Base::createArrayHelper( ) const
897 Sequence< Property > aProps(10);
898 Property* pProperties = aProps.getArray();
899 sal_Int32 nPos = 0;
900 DECL_PROP0(CURSORNAME, ::rtl::OUString);
901 DECL_BOOL_PROP0(ESCAPEPROCESSING);
902 DECL_PROP0(FETCHDIRECTION,sal_Int32);
903 DECL_PROP0(FETCHSIZE, sal_Int32);
904 DECL_PROP0(MAXFIELDSIZE,sal_Int64);
905 DECL_PROP0(MAXROWS, sal_Int64);
906 DECL_PROP0(QUERYTIMEOUT,sal_Int64);
907 DECL_PROP0(RESULTSETCONCURRENCY,sal_Int32);
908 DECL_PROP0(RESULTSETTYPE,sal_Int32);
909 DECL_BOOL_PROP0(USEBOOKMARKS);
911 return new ::cppu::OPropertyArrayHelper(aProps);
914 // -------------------------------------------------------------------------
915 ::cppu::IPropertyArrayHelper & OStatement_Base::getInfoHelper()
917 return *const_cast<OStatement_Base*>(this)->getArrayHelper();
919 // -------------------------------------------------------------------------
920 sal_Bool OStatement_Base::convertFastPropertyValue(
921 Any & rConvertedValue,
922 Any & rOldValue,
923 sal_Int32 nHandle,
924 const Any& rValue )
925 throw (::com::sun::star::lang::IllegalArgumentException)
927 sal_Bool bConverted = sal_False;
930 switch(nHandle)
932 case PROPERTY_ID_QUERYTIMEOUT:
933 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getQueryTimeOut());
934 break;
936 case PROPERTY_ID_MAXFIELDSIZE:
937 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getMaxFieldSize());
938 break;
940 case PROPERTY_ID_MAXROWS:
941 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getMaxRows());
942 break;
944 case PROPERTY_ID_CURSORNAME:
945 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getCursorName());
946 break;
948 case PROPERTY_ID_RESULTSETCONCURRENCY:
949 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getResultSetConcurrency());
950 break;
952 case PROPERTY_ID_RESULTSETTYPE:
953 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getResultSetType());
954 break;
956 case PROPERTY_ID_FETCHDIRECTION:
957 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getFetchDirection());
958 break;
960 case PROPERTY_ID_FETCHSIZE:
961 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getFetchSize());
962 break;
964 case PROPERTY_ID_USEBOOKMARKS:
965 bConverted = ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, isUsingBookmarks());
966 break;
968 case PROPERTY_ID_ESCAPEPROCESSING:
969 bConverted = ::comphelper::tryPropertyValue( rConvertedValue, rOldValue, rValue, getEscapeProcessing() );
970 break;
974 catch(const SQLException&)
976 // throw Exception(e.Message,*this);
978 return bConverted;
980 // -------------------------------------------------------------------------
981 void OStatement_Base::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue) throw (Exception)
985 switch(nHandle)
987 case PROPERTY_ID_QUERYTIMEOUT:
988 setQueryTimeOut(comphelper::getINT64(rValue));
989 break;
990 case PROPERTY_ID_MAXFIELDSIZE:
991 setMaxFieldSize(comphelper::getINT64(rValue));
992 break;
993 case PROPERTY_ID_MAXROWS:
994 setMaxRows(comphelper::getINT64(rValue));
995 break;
996 case PROPERTY_ID_CURSORNAME:
997 setCursorName(comphelper::getString(rValue));
998 break;
999 case PROPERTY_ID_RESULTSETCONCURRENCY:
1000 setResultSetConcurrency(comphelper::getINT32(rValue));
1001 break;
1002 case PROPERTY_ID_RESULTSETTYPE:
1003 setResultSetType(comphelper::getINT32(rValue));
1004 break;
1005 case PROPERTY_ID_FETCHDIRECTION:
1006 setFetchDirection(comphelper::getINT32(rValue));
1007 break;
1008 case PROPERTY_ID_FETCHSIZE:
1009 setFetchSize(comphelper::getINT32(rValue));
1010 break;
1011 case PROPERTY_ID_USEBOOKMARKS:
1012 setUsingBookmarks(comphelper::getBOOL(rValue));
1013 break;
1014 case PROPERTY_ID_ESCAPEPROCESSING:
1015 setEscapeProcessing( ::comphelper::getBOOL( rValue ) );
1016 break;
1017 default:
1018 OSL_FAIL( "OStatement_Base::setFastPropertyValue_NoBroadcast: what property?" );
1019 break;
1022 catch(const SQLException& )
1024 // throw Exception(e.Message,*this);
1027 // -------------------------------------------------------------------------
1028 void OStatement_Base::getFastPropertyValue(Any& rValue,sal_Int32 nHandle) const
1030 switch(nHandle)
1032 case PROPERTY_ID_QUERYTIMEOUT:
1033 rValue <<= getQueryTimeOut();
1034 break;
1035 case PROPERTY_ID_MAXFIELDSIZE:
1036 rValue <<= getMaxFieldSize();
1037 break;
1038 case PROPERTY_ID_MAXROWS:
1039 rValue <<= getMaxRows();
1040 break;
1041 case PROPERTY_ID_CURSORNAME:
1042 rValue <<= getCursorName();
1043 break;
1044 case PROPERTY_ID_RESULTSETCONCURRENCY:
1045 rValue <<= getResultSetConcurrency();
1046 break;
1047 case PROPERTY_ID_RESULTSETTYPE:
1048 rValue <<= getResultSetType();
1049 break;
1050 case PROPERTY_ID_FETCHDIRECTION:
1051 rValue <<= getFetchDirection();
1052 break;
1053 case PROPERTY_ID_FETCHSIZE:
1054 rValue <<= getFetchSize();
1055 break;
1056 case PROPERTY_ID_USEBOOKMARKS:
1057 rValue <<= isUsingBookmarks();
1058 break;
1059 case PROPERTY_ID_ESCAPEPROCESSING:
1060 rValue <<= getEscapeProcessing();
1061 break;
1062 default:
1063 OSL_FAIL( "OStatement_Base::getFastPropertyValue: what property?" );
1064 break;
1067 // -------------------------------------------------------------------------
1068 IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbcx.OStatement","com.sun.star.sdbc.Statement");
1069 // -----------------------------------------------------------------------------
1070 void SAL_CALL OStatement_Base::acquire() throw()
1072 OStatement_BASE::acquire();
1074 // -----------------------------------------------------------------------------
1075 void SAL_CALL OStatement_Base::release() throw()
1077 OStatement_BASE::release();
1079 // -----------------------------------------------------------------------------
1080 void SAL_CALL OStatement::acquire() throw()
1082 OStatement_BASE2::acquire();
1084 // -----------------------------------------------------------------------------
1085 void SAL_CALL OStatement::release() throw()
1087 OStatement_BASE2::release();
1089 // -----------------------------------------------------------------------------
1090 OResultSet* OStatement_Base::createResulSet()
1092 return new OResultSet(m_aStatementHandle,this);
1094 // -----------------------------------------------------------------------------
1095 Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OStatement_Base::getPropertySetInfo( ) throw(RuntimeException)
1097 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
1099 // -----------------------------------------------------------------------------
1100 SQLUINTEGER OStatement_Base::getCursorProperties(SQLINTEGER _nCursorType,sal_Bool bFirst)
1102 SQLUINTEGER nValueLen = 0;
1105 SQLUSMALLINT nAskFor = SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2;
1106 if(SQL_CURSOR_KEYSET_DRIVEN == _nCursorType)
1107 nAskFor = bFirst ? SQL_KEYSET_CURSOR_ATTRIBUTES1 : SQL_KEYSET_CURSOR_ATTRIBUTES2;
1108 else if(SQL_CURSOR_STATIC == _nCursorType)
1109 nAskFor = bFirst ? SQL_STATIC_CURSOR_ATTRIBUTES1 : SQL_STATIC_CURSOR_ATTRIBUTES2;
1110 else if(SQL_CURSOR_FORWARD_ONLY == _nCursorType)
1111 nAskFor = bFirst ? SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1 : SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2;
1112 else if(SQL_CURSOR_DYNAMIC == _nCursorType)
1113 nAskFor = bFirst ? SQL_DYNAMIC_CURSOR_ATTRIBUTES1 : SQL_DYNAMIC_CURSOR_ATTRIBUTES2;
1116 OTools::GetInfo(getOwnConnection(),getConnectionHandle(),nAskFor,nValueLen,NULL);
1118 catch(const Exception&)
1119 { // we don't want our result destroy here
1120 nValueLen = 0;
1122 return nValueLen;
1124 // -----------------------------------------------------------------------------
1126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */