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 #ifndef CONNECTIVITY_TSORTINDEX_HXX
28*cdf0e10cSrcweir #define CONNECTIVITY_TSORTINDEX_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "connectivity/dbtoolsdllapi.hxx"
31*cdf0e10cSrcweir #include "TKeyValue.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir namespace connectivity
34*cdf0e10cSrcweir {
35*cdf0e10cSrcweir 	typedef enum
36*cdf0e10cSrcweir 	{
37*cdf0e10cSrcweir 		SQL_ORDERBYKEY_NONE,		// do not sort
38*cdf0e10cSrcweir 		SQL_ORDERBYKEY_DOUBLE,		// numeric key
39*cdf0e10cSrcweir 		SQL_ORDERBYKEY_STRING		// String Key
40*cdf0e10cSrcweir 	} OKeyType;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	typedef enum
43*cdf0e10cSrcweir 	{
44*cdf0e10cSrcweir 		SQL_ASC		= 1,			// ascending
45*cdf0e10cSrcweir 		SQL_DESC	= -1			// otherwise
46*cdf0e10cSrcweir 	} TAscendingOrder;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 	class OKeySet;
49*cdf0e10cSrcweir 	class OKeyValue;				// simple class which holds a sal_Int32 and a ::std::vector<ORowSetValueDecoratorRef>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	/**
52*cdf0e10cSrcweir 		The class OSortIndex can be used to implement a sorted index.
53*cdf0e10cSrcweir 		This can depend on the fields which should be sorted.
54*cdf0e10cSrcweir 	*/
55*cdf0e10cSrcweir 	class OOO_DLLPUBLIC_DBTOOLS OSortIndex
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 	public:
58*cdf0e10cSrcweir 		typedef ::std::vector< ::std::pair<sal_Int32,OKeyValue*> >	TIntValuePairVector;
59*cdf0e10cSrcweir 		typedef ::std::vector<OKeyType>								TKeyTypeVector;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	private:
62*cdf0e10cSrcweir 		TIntValuePairVector			    m_aKeyValues;
63*cdf0e10cSrcweir 		TKeyTypeVector				    m_aKeyType;
64*cdf0e10cSrcweir         ::std::vector<TAscendingOrder>  m_aAscending;
65*cdf0e10cSrcweir 		sal_Bool					    m_bFrozen;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	public:
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 		OSortIndex(	const ::std::vector<OKeyType>& _aKeyType,
70*cdf0e10cSrcweir                     const ::std::vector<TAscendingOrder>& _aAscending);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 		~OSortIndex();
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 		inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
75*cdf0e10cSrcweir 			{ return ::rtl_allocateMemory( nSize ); }
76*cdf0e10cSrcweir 		inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW( () )
77*cdf0e10cSrcweir 			{ return _pHint; }
78*cdf0e10cSrcweir 		inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
79*cdf0e10cSrcweir 			{ ::rtl_freeMemory( pMem ); }
80*cdf0e10cSrcweir 		inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW( () )
81*cdf0e10cSrcweir 			{  }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 		/**
85*cdf0e10cSrcweir 			AddKeyValue appends a new value.
86*cdf0e10cSrcweir 			@param
87*cdf0e10cSrcweir 				pKeyValue	the keyvalue to be appended
88*cdf0e10cSrcweir 			ATTENTION: when the sortindex is already frozen the parameter will be deleted
89*cdf0e10cSrcweir 		*/
90*cdf0e10cSrcweir 		void AddKeyValue(OKeyValue * pKeyValue);
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 		/**
93*cdf0e10cSrcweir 			Freeze freezes the sortindex so that new values could only be appended by their value
94*cdf0e10cSrcweir 		*/
95*cdf0e10cSrcweir 		void Freeze();
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 		/**
98*cdf0e10cSrcweir 			CreateKeySet creates the keyset which vaalues could be used to travel in your table/result
99*cdf0e10cSrcweir 			The returned keyset is frozen.
100*cdf0e10cSrcweir 		*/
101*cdf0e10cSrcweir 		::vos::ORef<OKeySet> CreateKeySet();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 		// look at the name
106*cdf0e10cSrcweir 		sal_Bool IsFrozen() const { return m_bFrozen; }
107*cdf0e10cSrcweir 		// returns the current size of the keyvalues
108*cdf0e10cSrcweir 		sal_Int32 Count()	const { return m_aKeyValues.size(); }
109*cdf0e10cSrcweir 		/** GetValue returns the value at position nPos (1..n) [sorted access].
110*cdf0e10cSrcweir 			It only allowed to call this method after the sortindex has been frozen.
111*cdf0e10cSrcweir 		*/
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		sal_Int32 GetValue(sal_Int32 nPos) const;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		inline const ::std::vector<OKeyType>& getKeyType() const { return m_aKeyType; }
116*cdf0e10cSrcweir         inline TAscendingOrder getAscending(::std::vector<TAscendingOrder>::size_type _nPos) const { return m_aAscending[_nPos]; }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	};
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	/**
121*cdf0e10cSrcweir 		The class OKeySet is a refcountable vector which also has a state.
122*cdf0e10cSrcweir 		This state gives information about if the keyset is fixed.
123*cdf0e10cSrcweir 	*/
124*cdf0e10cSrcweir 	class OOO_DLLPUBLIC_DBTOOLS OKeySet : public ORefVector<sal_Int32>
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir 		sal_Bool m_bFrozen;
127*cdf0e10cSrcweir 	public:
128*cdf0e10cSrcweir 		OKeySet()
129*cdf0e10cSrcweir 			: ORefVector<sal_Int32>()
130*cdf0e10cSrcweir 			, m_bFrozen(sal_False){}
131*cdf0e10cSrcweir 		OKeySet(Vector::size_type _nSize)
132*cdf0e10cSrcweir 			: ORefVector<sal_Int32>(_nSize)
133*cdf0e10cSrcweir 			, m_bFrozen(sal_False){}
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 		sal_Bool	isFrozen() const						{ return m_bFrozen; }
136*cdf0e10cSrcweir 		void		setFrozen(sal_Bool _bFrozen=sal_True)	{ m_bFrozen = _bFrozen; }
137*cdf0e10cSrcweir 	};
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir #endif // CONNECTIVITY_TSORTINDEX_HXX
140