code reorg
[csql.git] / src / relational / relop / OrderByTree.cxx
blobb443c84331bb7f20bf1663f4c6049d81cc14427e
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * ***************************************************************************/
17 #include<OrderByTree.h>
19 //Used for float type field and individual order by clause
20 void OrderByTree::insertSpecialCaseDataNode(void *data)
22 if(dataNode.size() == 0){ dataNode.append(data); return;}
23 ListIterator iter = dataNode.getIterator();
24 ListIterator fldIter = orderByList.getIterator();
25 iter.reset();
26 int ret=0;
27 void *insertedData = NULL;
28 void *prevIter = iter.getCurrentListNode();
29 void *prev = prevIter;
30 bool shouldInsertHappenAtMiddle=false;
31 OrderByFldDef *oFldDef = NULL;
32 int offset=0;
33 char *fldData=NULL, *fldInsertData=NULL;
34 bool result=false;
35 while (iter.hasElement())
37 bool shoudInsertHere=false;
38 bool isPrevEqual=false;
39 offset=0;
40 insertedData = iter.nextElement();
41 fldIter.reset();
42 while (fldIter.hasElement())
44 oFldDef = (OrderByFldDef*) fldIter.nextElement();
45 fldData = ((char*)data)+offset;
46 fldInsertData = ((char*)insertedData)+offset;
47 offset += oFldDef->length;
48 result=AllDataType::compareVal(fldData, fldInsertData, OpEquals,oFldDef->type, oFldDef->length);
49 if(result)
51 shoudInsertHere = true;
52 isPrevEqual = true;
53 continue;
55 else
57 result=AllDataType::compareVal(fldData, fldInsertData, OpGreaterThan,oFldDef->type, oFldDef->length);
58 if(result)
60 if(oFldDef->isDesc)
62 shoudInsertHere = true;
63 isPrevEqual = false;
64 continue;
66 else{
67 if(shoudInsertHere && !isPrevEqual) { isPrevEqual = false; continue;}
68 shoudInsertHere=false; break;
70 }else
72 //data<inserted
73 if(!oFldDef->isDesc)
75 shoudInsertHere = true;
76 isPrevEqual = false;
77 continue;
79 else{
80 if(shoudInsertHere && !isPrevEqual ) { isPrevEqual = false; continue;}
81 shoudInsertHere=false; break;
85 }//inner while ends here
86 if(shoudInsertHere){
87 if(shouldInsertHappenAtMiddle)
88 dataNode.addAtMiddle(data, prevIter);
89 else
90 dataNode.addAtBegin(data);
91 return;
93 prevIter = prev;
94 prev = iter.getCurrentListNode();
95 shouldInsertHappenAtMiddle=true;
97 dataNode.append(data);
100 DbRetVal OrderByTree::insertDataNode(void *data)
102 if(!fullOrderBy) {
103 insertSpecialCaseDataNode(data);
104 return OK;
106 dataNode.append(data);
107 projMap.insert(data);
108 return OK;
111 bool OrderByTree::find(void *data)
113 void *element = projMap.find(data);
114 if (element) return true;
115 return false;
118 int OrderByTree::compare(void *element1,void *element2,int size)
120 return os::memcmp(element1,element2,size);
123 ListIterator OrderByTree::getListIterator()
125 return dataNode.getIterator();
128 void OrderByTree::removeAll()
130 dataNode.reset();
131 if(fullOrderBy) projMap.removeAll();
132 return;