1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir #ifndef CONNECTIVITY_TSORTINDEX_HXX
24cdf0e10cSrcweir #define CONNECTIVITY_TSORTINDEX_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "connectivity/dbtoolsdllapi.hxx"
27cdf0e10cSrcweir #include "TKeyValue.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace connectivity
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 	typedef enum
32cdf0e10cSrcweir 	{
33cdf0e10cSrcweir 		SQL_ORDERBYKEY_NONE,		// do not sort
34cdf0e10cSrcweir 		SQL_ORDERBYKEY_DOUBLE,		// numeric key
35cdf0e10cSrcweir 		SQL_ORDERBYKEY_STRING		// String Key
36cdf0e10cSrcweir 	} OKeyType;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 	typedef enum
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 		SQL_ASC		= 1,			// ascending
41cdf0e10cSrcweir 		SQL_DESC	= -1			// otherwise
42cdf0e10cSrcweir 	} TAscendingOrder;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	class OKeySet;
45cdf0e10cSrcweir 	class OKeyValue;				// simple class which holds a sal_Int32 and a ::std::vector<ORowSetValueDecoratorRef>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	/**
48cdf0e10cSrcweir 		The class OSortIndex can be used to implement a sorted index.
49cdf0e10cSrcweir 		This can depend on the fields which should be sorted.
50cdf0e10cSrcweir 	*/
51cdf0e10cSrcweir 	class OOO_DLLPUBLIC_DBTOOLS OSortIndex
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 	public:
54cdf0e10cSrcweir 		typedef ::std::vector< ::std::pair<sal_Int32,OKeyValue*> >	TIntValuePairVector;
55cdf0e10cSrcweir 		typedef ::std::vector<OKeyType>								TKeyTypeVector;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	private:
58cdf0e10cSrcweir 		TIntValuePairVector			    m_aKeyValues;
59cdf0e10cSrcweir 		TKeyTypeVector				    m_aKeyType;
60cdf0e10cSrcweir         ::std::vector<TAscendingOrder>  m_aAscending;
61cdf0e10cSrcweir 		sal_Bool					    m_bFrozen;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	public:
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		OSortIndex(	const ::std::vector<OKeyType>& _aKeyType,
66cdf0e10cSrcweir                     const ::std::vector<TAscendingOrder>& _aAscending);
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 		~OSortIndex();
69cdf0e10cSrcweir 
operator new(size_t nSize)70cdf0e10cSrcweir 		inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
71cdf0e10cSrcweir 			{ return ::rtl_allocateMemory( nSize ); }
operator new(size_t,void * _pHint)72cdf0e10cSrcweir 		inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW( () )
73cdf0e10cSrcweir 			{ return _pHint; }
operator delete(void * pMem)74cdf0e10cSrcweir 		inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
75cdf0e10cSrcweir 			{ ::rtl_freeMemory( pMem ); }
operator delete(void *,void *)76cdf0e10cSrcweir 		inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW( () )
77cdf0e10cSrcweir 			{  }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		/**
81cdf0e10cSrcweir 			AddKeyValue appends a new value.
82cdf0e10cSrcweir 			@param
83cdf0e10cSrcweir 				pKeyValue	the keyvalue to be appended
84cdf0e10cSrcweir 			ATTENTION: when the sortindex is already frozen the parameter will be deleted
85cdf0e10cSrcweir 		*/
86cdf0e10cSrcweir 		void AddKeyValue(OKeyValue * pKeyValue);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		/**
89cdf0e10cSrcweir 			Freeze freezes the sortindex so that new values could only be appended by their value
90cdf0e10cSrcweir 		*/
91cdf0e10cSrcweir 		void Freeze();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 		/**
94cdf0e10cSrcweir 			CreateKeySet creates the keyset which vaalues could be used to travel in your table/result
95cdf0e10cSrcweir 			The returned keyset is frozen.
96cdf0e10cSrcweir 		*/
97cdf0e10cSrcweir 		::vos::ORef<OKeySet> CreateKeySet();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 		// look at the name
IsFrozen() const102cdf0e10cSrcweir 		sal_Bool IsFrozen() const { return m_bFrozen; }
103cdf0e10cSrcweir 		// returns the current size of the keyvalues
Count() const104cdf0e10cSrcweir 		sal_Int32 Count()	const { return m_aKeyValues.size(); }
105cdf0e10cSrcweir 		/** GetValue returns the value at position nPos (1..n) [sorted access].
106cdf0e10cSrcweir 			It only allowed to call this method after the sortindex has been frozen.
107cdf0e10cSrcweir 		*/
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		sal_Int32 GetValue(sal_Int32 nPos) const;
110cdf0e10cSrcweir 
getKeyType() const111cdf0e10cSrcweir 		inline const ::std::vector<OKeyType>& getKeyType() const { return m_aKeyType; }
getAscending(::std::vector<TAscendingOrder>::size_type _nPos) const112cdf0e10cSrcweir         inline TAscendingOrder getAscending(::std::vector<TAscendingOrder>::size_type _nPos) const { return m_aAscending[_nPos]; }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	};
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	/**
117cdf0e10cSrcweir 		The class OKeySet is a refcountable vector which also has a state.
118cdf0e10cSrcweir 		This state gives information about if the keyset is fixed.
119cdf0e10cSrcweir 	*/
120cdf0e10cSrcweir 	class OOO_DLLPUBLIC_DBTOOLS OKeySet : public ORefVector<sal_Int32>
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		sal_Bool m_bFrozen;
123cdf0e10cSrcweir 	public:
OKeySet()124cdf0e10cSrcweir 		OKeySet()
125cdf0e10cSrcweir 			: ORefVector<sal_Int32>()
126cdf0e10cSrcweir 			, m_bFrozen(sal_False){}
OKeySet(Vector::size_type _nSize)127cdf0e10cSrcweir 		OKeySet(Vector::size_type _nSize)
128cdf0e10cSrcweir 			: ORefVector<sal_Int32>(_nSize)
129cdf0e10cSrcweir 			, m_bFrozen(sal_False){}
130cdf0e10cSrcweir 
isFrozen() const131cdf0e10cSrcweir 		sal_Bool	isFrozen() const						{ return m_bFrozen; }
setFrozen(sal_Bool _bFrozen=sal_True)132cdf0e10cSrcweir 		void		setFrozen(sal_Bool _bFrozen=sal_True)	{ m_bFrozen = _bFrozen; }
133cdf0e10cSrcweir 	};
134cdf0e10cSrcweir }
135cdf0e10cSrcweir #endif // CONNECTIVITY_TSORTINDEX_HXX
136