1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include "dbase/DIndexIter.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/sdb/SQLFilterOperator.hpp>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
34*cdf0e10cSrcweir using namespace connectivity;
35*cdf0e10cSrcweir using namespace connectivity::dbase;
36*cdf0e10cSrcweir using namespace connectivity::file;
37*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
38*cdf0e10cSrcweir //==================================================================
39*cdf0e10cSrcweir // OIndexIterator
40*cdf0e10cSrcweir //==================================================================
41*cdf0e10cSrcweir //------------------------------------------------------------------
42*cdf0e10cSrcweir OIndexIterator::~OIndexIterator()
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	//	m_pIndex->UnLock();
45*cdf0e10cSrcweir 	m_pIndex->release();
46*cdf0e10cSrcweir }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir //------------------------------------------------------------------
49*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::First()
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	return Find(sal_True);
52*cdf0e10cSrcweir }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //------------------------------------------------------------------
55*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::Next()
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	return Find(sal_False);
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir //------------------------------------------------------------------
60*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::Find(sal_Bool bFirst)
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	sal_uIntPtr nRes = STRING_NOTFOUND;
65*cdf0e10cSrcweir //	if (!m_pIndex->IsOpen())
66*cdf0e10cSrcweir //		return nRes;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	if (bFirst)
69*cdf0e10cSrcweir 	{
70*cdf0e10cSrcweir 		m_aRoot = m_pIndex->getRoot();
71*cdf0e10cSrcweir 		m_aCurLeaf = NULL;
72*cdf0e10cSrcweir 	}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	if (!m_pOperator)
75*cdf0e10cSrcweir 	{
76*cdf0e10cSrcweir 		// Vorbereitung , auf kleinstes Element positionieren
77*cdf0e10cSrcweir 		if (bFirst)
78*cdf0e10cSrcweir 		{
79*cdf0e10cSrcweir 			ONDXPage* pPage = m_aRoot;
80*cdf0e10cSrcweir 			while (pPage && !pPage->IsLeaf())
81*cdf0e10cSrcweir 				pPage = pPage->GetChild(m_pIndex);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 			m_aCurLeaf = pPage;
84*cdf0e10cSrcweir 			m_nCurNode = NODE_NOTFOUND;
85*cdf0e10cSrcweir 		}
86*cdf0e10cSrcweir 		ONDXKey* pKey = GetNextKey();
87*cdf0e10cSrcweir 		nRes = pKey ? pKey->GetRecord() : STRING_NOTFOUND;
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 	else if (m_pOperator->IsA(TYPE(OOp_ISNOTNULL)))
90*cdf0e10cSrcweir 		nRes = GetNotNull(bFirst);
91*cdf0e10cSrcweir 	else if (m_pOperator->IsA(TYPE(OOp_ISNULL)))
92*cdf0e10cSrcweir 		nRes = GetNull(bFirst);
93*cdf0e10cSrcweir 	else if (m_pOperator->IsA(TYPE(OOp_LIKE)))
94*cdf0e10cSrcweir 		nRes = GetLike(bFirst);
95*cdf0e10cSrcweir 	else if (m_pOperator->IsA(TYPE(OOp_COMPARE)))
96*cdf0e10cSrcweir 		nRes = GetCompare(bFirst);
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	return nRes;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir //------------------------------------------------------------------
102*cdf0e10cSrcweir ONDXKey* OIndexIterator::GetFirstKey(ONDXPage* pPage, const OOperand& rKey)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	// sucht den vorgegeben key
105*cdf0e10cSrcweir 	// Besonderheit: gelangt der Algorithmus ans Ende
106*cdf0e10cSrcweir 	// wird immer die aktuelle Seite und die Knotenposition vermerkt
107*cdf0e10cSrcweir 	// auf die die Bedingung <= zutrifft
108*cdf0e10cSrcweir 	// dieses findet beim Insert besondere Beachtung
109*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
110*cdf0e10cSrcweir 	OOp_COMPARE aTempOp(SQLFilterOperator::GREATER);
111*cdf0e10cSrcweir 	sal_uInt16 i = 0;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	if (pPage->IsLeaf())
114*cdf0e10cSrcweir 	{
115*cdf0e10cSrcweir 		// im blatt wird die eigentliche Operation ausgefuehrt, sonst die temp. (>)
116*cdf0e10cSrcweir 		while (i < pPage->Count() && !m_pOperator->operate(&((*pPage)[i]).GetKey(),&rKey))
117*cdf0e10cSrcweir 			   i++;
118*cdf0e10cSrcweir 	}
119*cdf0e10cSrcweir 	else
120*cdf0e10cSrcweir 		while (i < pPage->Count() && !aTempOp.operate(&((*pPage)[i]).GetKey(),&rKey))
121*cdf0e10cSrcweir 			   i++;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	ONDXKey* pFoundKey = NULL;
125*cdf0e10cSrcweir 	if (!pPage->IsLeaf())
126*cdf0e10cSrcweir 	{
127*cdf0e10cSrcweir 		// weiter absteigen
128*cdf0e10cSrcweir 		ONDXPagePtr aPage = (i==0) ? pPage->GetChild(m_pIndex)
129*cdf0e10cSrcweir 									 : ((*pPage)[i-1]).GetChild(m_pIndex, pPage);
130*cdf0e10cSrcweir 		pFoundKey = aPage.Is() ? GetFirstKey(aPage, rKey) : NULL;
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir 	else if (i == pPage->Count())
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		pFoundKey = NULL;
135*cdf0e10cSrcweir 	}
136*cdf0e10cSrcweir 	else
137*cdf0e10cSrcweir 	{
138*cdf0e10cSrcweir 		pFoundKey = &(*pPage)[i].GetKey();
139*cdf0e10cSrcweir 		if (!m_pOperator->operate(pFoundKey,&rKey))
140*cdf0e10cSrcweir 			pFoundKey = NULL;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 		m_aCurLeaf = pPage;
143*cdf0e10cSrcweir 		m_nCurNode = pFoundKey ? i : i - 1;
144*cdf0e10cSrcweir 	}
145*cdf0e10cSrcweir 	return pFoundKey;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //------------------------------------------------------------------
149*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::GetCompare(sal_Bool bFirst)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	ONDXKey* pKey = NULL;
152*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
153*cdf0e10cSrcweir 	sal_Int32 ePredicateType = PTR_CAST(file::OOp_COMPARE,m_pOperator)->getPredicateType();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	if (bFirst)
156*cdf0e10cSrcweir 	{
157*cdf0e10cSrcweir 		// Vorbereitung , auf kleinstes Element positionieren
158*cdf0e10cSrcweir 		ONDXPage* pPage = m_aRoot;
159*cdf0e10cSrcweir 		switch (ePredicateType)
160*cdf0e10cSrcweir 		{
161*cdf0e10cSrcweir 			case SQLFilterOperator::NOT_EQUAL:
162*cdf0e10cSrcweir 			case SQLFilterOperator::LESS:
163*cdf0e10cSrcweir 			case SQLFilterOperator::LESS_EQUAL:
164*cdf0e10cSrcweir 				while (pPage && !pPage->IsLeaf())
165*cdf0e10cSrcweir 					pPage = pPage->GetChild(m_pIndex);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 				m_aCurLeaf = pPage;
168*cdf0e10cSrcweir 				m_nCurNode = NODE_NOTFOUND;
169*cdf0e10cSrcweir 		}
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		switch (ePredicateType)
173*cdf0e10cSrcweir 		{
174*cdf0e10cSrcweir 			case SQLFilterOperator::NOT_EQUAL:
175*cdf0e10cSrcweir 				while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand)) ;
176*cdf0e10cSrcweir 				break;
177*cdf0e10cSrcweir 			case SQLFilterOperator::LESS:
178*cdf0e10cSrcweir                 while ( ( ( pKey = GetNextKey() ) != NULL ) && pKey->getValue().isNull()) ;
179*cdf0e10cSrcweir 				break;
180*cdf0e10cSrcweir 			case SQLFilterOperator::LESS_EQUAL:
181*cdf0e10cSrcweir                 while ( ( pKey = GetNextKey() ) != NULL ) ;
182*cdf0e10cSrcweir 				break;
183*cdf0e10cSrcweir 			case SQLFilterOperator::GREATER_EQUAL:
184*cdf0e10cSrcweir 			case SQLFilterOperator::EQUAL:
185*cdf0e10cSrcweir 				pKey = GetFirstKey(m_aRoot,*m_pOperand);
186*cdf0e10cSrcweir 				break;
187*cdf0e10cSrcweir 			case SQLFilterOperator::GREATER:
188*cdf0e10cSrcweir                 pKey = GetFirstKey(m_aRoot,*m_pOperand);
189*cdf0e10cSrcweir                 if ( !pKey )
190*cdf0e10cSrcweir                     while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand)) ;
191*cdf0e10cSrcweir 		}
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 	else
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir 		switch (ePredicateType)
196*cdf0e10cSrcweir 		{
197*cdf0e10cSrcweir 			case SQLFilterOperator::NOT_EQUAL:
198*cdf0e10cSrcweir 				while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand))
199*cdf0e10cSrcweir 					;
200*cdf0e10cSrcweir 				break;
201*cdf0e10cSrcweir 			case SQLFilterOperator::LESS:
202*cdf0e10cSrcweir 			case SQLFilterOperator::LESS_EQUAL:
203*cdf0e10cSrcweir 			case SQLFilterOperator::EQUAL:
204*cdf0e10cSrcweir 				if ( ( ( pKey = GetNextKey() ) == NULL )  || !m_pOperator->operate(pKey,m_pOperand))
205*cdf0e10cSrcweir 				{
206*cdf0e10cSrcweir 					pKey = NULL;
207*cdf0e10cSrcweir 					m_aCurLeaf = NULL;
208*cdf0e10cSrcweir 				}
209*cdf0e10cSrcweir 				break;
210*cdf0e10cSrcweir 			case SQLFilterOperator::GREATER_EQUAL:
211*cdf0e10cSrcweir 			case SQLFilterOperator::GREATER:
212*cdf0e10cSrcweir 				pKey = GetNextKey();
213*cdf0e10cSrcweir 		}
214*cdf0e10cSrcweir 	}
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	return pKey ? pKey->GetRecord() : STRING_NOTFOUND;
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir //------------------------------------------------------------------
220*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::GetLike(sal_Bool bFirst)
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
223*cdf0e10cSrcweir 	if (bFirst)
224*cdf0e10cSrcweir 	{
225*cdf0e10cSrcweir 		ONDXPage* pPage = m_aRoot;
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 		while (pPage && !pPage->IsLeaf())
228*cdf0e10cSrcweir 			pPage = pPage->GetChild(m_pIndex);
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 		m_aCurLeaf = pPage;
231*cdf0e10cSrcweir 		m_nCurNode = NODE_NOTFOUND;
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	ONDXKey* pKey;
235*cdf0e10cSrcweir 	while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand))
236*cdf0e10cSrcweir 		;
237*cdf0e10cSrcweir 	return pKey ? pKey->GetRecord() : STRING_NOTFOUND;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir //------------------------------------------------------------------
241*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::GetNull(sal_Bool bFirst)
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
244*cdf0e10cSrcweir 	if (bFirst)
245*cdf0e10cSrcweir 	{
246*cdf0e10cSrcweir 		ONDXPage* pPage = m_aRoot;
247*cdf0e10cSrcweir 		while (pPage && !pPage->IsLeaf())
248*cdf0e10cSrcweir 			pPage = pPage->GetChild(m_pIndex);
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 		m_aCurLeaf = pPage;
251*cdf0e10cSrcweir 		m_nCurNode = NODE_NOTFOUND;
252*cdf0e10cSrcweir 	}
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 	ONDXKey* pKey;
255*cdf0e10cSrcweir 	if ( ( ( pKey = GetNextKey() ) == NULL ) || !pKey->getValue().isNull())
256*cdf0e10cSrcweir 	{
257*cdf0e10cSrcweir 		pKey = NULL;
258*cdf0e10cSrcweir 		m_aCurLeaf = NULL;
259*cdf0e10cSrcweir 	}
260*cdf0e10cSrcweir 	return pKey ? pKey->GetRecord() : STRING_NOTFOUND;
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir //------------------------------------------------------------------
264*cdf0e10cSrcweir sal_uIntPtr OIndexIterator::GetNotNull(sal_Bool bFirst)
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir 	ONDXKey* pKey;
267*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
268*cdf0e10cSrcweir 	if (bFirst)
269*cdf0e10cSrcweir 	{
270*cdf0e10cSrcweir 		// erst alle NULL werte abklappern
271*cdf0e10cSrcweir 		for (sal_uIntPtr nRec = GetNull(bFirst);
272*cdf0e10cSrcweir 			 nRec != STRING_NOTFOUND;
273*cdf0e10cSrcweir 			 nRec = GetNull(sal_False))
274*cdf0e10cSrcweir 				 ;
275*cdf0e10cSrcweir 		pKey = m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : NULL;
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir 	else
278*cdf0e10cSrcweir 		pKey = GetNextKey();
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 	return pKey ? pKey->GetRecord() : STRING_NOTFOUND;
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir //------------------------------------------------------------------
284*cdf0e10cSrcweir ONDXKey* OIndexIterator::GetNextKey()
285*cdf0e10cSrcweir {
286*cdf0e10cSrcweir 	//	ONDXIndex* m_pIndex = GetNDXIndex();
287*cdf0e10cSrcweir 	if (m_aCurLeaf.Is() && ((++m_nCurNode) >= m_aCurLeaf->Count()))
288*cdf0e10cSrcweir 	{
289*cdf0e10cSrcweir 		ONDXPage* pPage = m_aCurLeaf;
290*cdf0e10cSrcweir 		// naechste Seite suchen
291*cdf0e10cSrcweir 		while (pPage)
292*cdf0e10cSrcweir 		{
293*cdf0e10cSrcweir 			ONDXPage* pParentPage = pPage->GetParent();
294*cdf0e10cSrcweir 			if (pParentPage)
295*cdf0e10cSrcweir 			{
296*cdf0e10cSrcweir 				sal_uInt16 nPos = pParentPage->Search(pPage);
297*cdf0e10cSrcweir 				if (nPos != pParentPage->Count() - 1)
298*cdf0e10cSrcweir 				{	// Seite gefunden
299*cdf0e10cSrcweir 					pPage = (*pParentPage)[nPos+1].GetChild(m_pIndex,pParentPage);
300*cdf0e10cSrcweir 					break;
301*cdf0e10cSrcweir 				}
302*cdf0e10cSrcweir 			}
303*cdf0e10cSrcweir 			pPage = pParentPage;
304*cdf0e10cSrcweir 		}
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 		// jetzt wieder zum Blatt
307*cdf0e10cSrcweir 		while (pPage && !pPage->IsLeaf())
308*cdf0e10cSrcweir 			pPage = pPage->GetChild(m_pIndex);
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 		m_aCurLeaf = pPage;
311*cdf0e10cSrcweir 		m_nCurNode = 0;
312*cdf0e10cSrcweir 	}
313*cdf0e10cSrcweir 	return m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : NULL;
314*cdf0e10cSrcweir }
315*cdf0e10cSrcweir 
316