1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
31 #include "dbase/DResultSet.hxx"
32 #include <com/sun/star/lang/DisposedException.hpp>
33 #include <comphelper/sequence.hxx>
34 #include "dbase/DIndex.hxx"
35 #include "dbase/DIndexIter.hxx"
36 #include "dbase/DCode.hxx"
37 #include <comphelper/types.hxx>
38 #include <connectivity/dbexception.hxx>
39 #include "resource/dbase_res.hrc"
40 
41 using namespace ::comphelper;
42 
43 using namespace connectivity::dbase;
44 using namespace connectivity::file;
45 using namespace ::cppu;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::beans;
49 using namespace com::sun::star::sdbc;
50 using namespace com::sun::star::sdbcx;
51 //	using namespace com::sun::star::container;
52 //	using namespace com::sun::star::util;
53 //------------------------------------------------------------------------------
54 ODbaseResultSet::ODbaseResultSet( OStatement_Base* pStmt,connectivity::OSQLParseTreeIterator&	_aSQLIterator)
55 				: file::OResultSet(pStmt,_aSQLIterator)
56 				,m_bBookmarkable(sal_True)
57 {
58 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISBOOKMARKABLE),         PROPERTY_ID_ISBOOKMARKABLE,       PropertyAttribute::READONLY,&m_bBookmarkable,                ::getBooleanCppuType());
59 }
60 // -------------------------------------------------------------------------
61 ::rtl::OUString SAL_CALL ODbaseResultSet::getImplementationName(  ) throw ( RuntimeException)
62 {
63 	return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.dbase.ResultSet");
64 }
65 // -------------------------------------------------------------------------
66 Sequence< ::rtl::OUString > SAL_CALL ODbaseResultSet::getSupportedServiceNames(  ) throw( RuntimeException)
67 {
68 	 Sequence< ::rtl::OUString > aSupported(2);
69 	aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.ResultSet");
70 	aSupported[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.ResultSet");
71 	return aSupported;
72 }
73 // -------------------------------------------------------------------------
74 sal_Bool SAL_CALL ODbaseResultSet::supportsService( const ::rtl::OUString& _rServiceName ) throw( RuntimeException)
75 {
76 	Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
77 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
78 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
79 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
80 		;
81 
82 	return pSupported != pEnd;
83 }
84 // -------------------------------------------------------------------------
85 Any SAL_CALL ODbaseResultSet::queryInterface( const Type & rType ) throw(RuntimeException)
86 {
87 	Any aRet = ODbaseResultSet_BASE::queryInterface(rType);
88 	return aRet.hasValue() ? aRet : OResultSet::queryInterface(rType);
89 }
90 // -------------------------------------------------------------------------
91  Sequence<  Type > SAL_CALL ODbaseResultSet::getTypes(  ) throw( RuntimeException)
92 {
93 	return ::comphelper::concatSequences(OResultSet::getTypes(),ODbaseResultSet_BASE::getTypes());
94 }
95 
96 // -------------------------------------------------------------------------
97 // XRowLocate
98 Any SAL_CALL ODbaseResultSet::getBookmark(  ) throw( SQLException,  RuntimeException)
99 {
100 	 ::osl::MutexGuard aGuard( m_aMutex );
101 	checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
102 	OSL_ENSURE((m_bShowDeleted || !m_aRow->isDeleted()),"getBookmark called for deleted row");
103 
104 	return makeAny((sal_Int32)(m_aRow->get())[0]->getValue());
105 }
106 // -------------------------------------------------------------------------
107 sal_Bool SAL_CALL ODbaseResultSet::moveToBookmark( const  Any& bookmark ) throw( SQLException,  RuntimeException)
108 {
109 	::osl::MutexGuard aGuard( m_aMutex );
110 	checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
111 
112 
113 	m_bRowDeleted = m_bRowInserted = m_bRowUpdated = sal_False;
114 
115 	return m_pTable ? Move(IResultSetHelper::BOOKMARK,comphelper::getINT32(bookmark),sal_True) : sal_False;
116 }
117 // -------------------------------------------------------------------------
118 sal_Bool SAL_CALL ODbaseResultSet::moveRelativeToBookmark( const  Any& bookmark, sal_Int32 rows ) throw( SQLException,  RuntimeException)
119 {
120 	::osl::MutexGuard aGuard( m_aMutex );
121 	checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
122 	if(!m_pTable)
123 		return sal_False;
124 
125 
126 	Move(IResultSetHelper::BOOKMARK,comphelper::getINT32(bookmark),sal_False);
127 
128 	return relative(rows);
129 }
130 
131 // -------------------------------------------------------------------------
132 sal_Int32 SAL_CALL ODbaseResultSet::compareBookmarks( const Any& lhs, const Any& rhs ) throw( SQLException,  RuntimeException)
133 {
134 	sal_Int32 nFirst(0),nSecond(0),nResult(0);
135 	if ( !( lhs  >>= nFirst ) || !( rhs >>= nSecond ) )
136     {
137         ::connectivity::SharedResources aResources;
138         const ::rtl::OUString sMessage = aResources.getResourceString(STR_INVALID_BOOKMARK);
139 		::dbtools::throwGenericSQLException(sMessage ,*this);
140     } // if ( !( lhs  >>= nFirst ) || !( rhs >>= nSecond ) )
141 
142 	// have a look at CompareBookmark
143 	// we can't use the names there because we already have defines with the same name from the parser
144 	if(nFirst < nSecond)
145 		nResult = -1;
146 	else if(nFirst > nSecond)
147 		nResult = 1;
148 	else
149 		nResult = 0;
150 
151 	return  nResult;
152 }
153 // -------------------------------------------------------------------------
154 sal_Bool SAL_CALL ODbaseResultSet::hasOrderedBookmarks(  ) throw( SQLException,  RuntimeException)
155 {
156 	return sal_True;
157 }
158 // -------------------------------------------------------------------------
159 sal_Int32 SAL_CALL ODbaseResultSet::hashBookmark( const  Any& bookmark ) throw( SQLException,  RuntimeException)
160 {
161 	::osl::MutexGuard aGuard( m_aMutex );
162 	checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
163 
164 
165 	return comphelper::getINT32(bookmark);
166 }
167 // -------------------------------------------------------------------------
168 // XDeleteRows
169 Sequence< sal_Int32 > SAL_CALL ODbaseResultSet::deleteRows( const  Sequence<  Any >& /*rows*/ ) throw( SQLException,  RuntimeException)
170 {
171 	::osl::MutexGuard aGuard( m_aMutex );
172 	checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
173 
174     ::dbtools::throwFeatureNotImplementedException( "XDeleteRows::deleteRows", *this );
175 	return Sequence< sal_Int32 >();
176 }
177 // -------------------------------------------------------------------------
178 sal_Bool ODbaseResultSet::fillIndexValues(const Reference< XColumnsSupplier> &_xIndex)
179 {
180 	Reference<XUnoTunnel> xTunnel(_xIndex,UNO_QUERY);
181 	if(xTunnel.is())
182 	{
183 		dbase::ODbaseIndex* pIndex = reinterpret_cast< dbase::ODbaseIndex* >( xTunnel->getSomething(dbase::ODbaseIndex::getUnoTunnelImplementationId()) );
184 		if(pIndex)
185 		{
186 			dbase::OIndexIterator* pIter = pIndex->createIterator(NULL,NULL);
187 
188 			if (pIter)
189 			{
190 				sal_uInt32 nRec = pIter->First();
191 				while (nRec != SQL_COLUMN_NOTFOUND)
192 				{
193 					if (m_aOrderbyAscending[0])
194 						m_pFileSet->get().push_back(nRec);
195 					else
196 						m_pFileSet->get().insert(m_pFileSet->get().begin(),nRec);
197 					nRec = pIter->Next();
198 				}
199 				m_pFileSet->setFrozen();
200 				//	if(!bDistinct)
201 					//	SetRowCount(pFileSet->count());
202 				delete pIter;
203 				return sal_True;
204 			}
205 			delete pIter;
206 		}
207 	}
208 	return sal_False;
209 }
210 // -------------------------------------------------------------------------
211 ::cppu::IPropertyArrayHelper & ODbaseResultSet::getInfoHelper()
212 {
213 	return *ODbaseResultSet_BASE3::getArrayHelper();
214 }
215 // -----------------------------------------------------------------------------
216 ::cppu::IPropertyArrayHelper* ODbaseResultSet::createArrayHelper() const
217 {
218 	Sequence< Property > aProps;
219 	describeProperties(aProps);
220 	return new ::cppu::OPropertyArrayHelper(aProps);
221 }
222 // -----------------------------------------------------------------------------
223 void SAL_CALL ODbaseResultSet::acquire() throw()
224 {
225 	ODbaseResultSet_BASE2::acquire();
226 }
227 // -----------------------------------------------------------------------------
228 void SAL_CALL ODbaseResultSet::release() throw()
229 {
230 	ODbaseResultSet_BASE2::release();
231 }
232 // -----------------------------------------------------------------------------
233 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL ODbaseResultSet::getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException)
234 {
235 	return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
236 }
237 // -----------------------------------------------------------------------------
238 OSQLAnalyzer* ODbaseResultSet::createAnalyzer()
239 {
240 	return new OFILEAnalyzer(m_pTable->getConnection());
241 }
242 // -----------------------------------------------------------------------------
243 sal_Int32 ODbaseResultSet::getCurrentFilePos() const
244 {
245 	return m_pTable->getFilePos();
246 }
247 // -----------------------------------------------------------------------------
248 
249 
250