code reorg
[csql.git] / src / server / FieldList.cxx
blobcde181891f537dd7c502eb9482a3cc03d509848a
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Table.h>
17 #include<Index.h>
18 #include<CatalogTables.h>
19 #include<Lock.h>
20 #include<Debug.h>
22 //does not check for duplicates
23 DbRetVal FieldList::append(FieldDef fDef)
25 FieldNode *newNode = new FieldNode();
26 newNode->fldDef = fDef;
27 newNode->next = NULL;
28 //If this is the first node, set it as head
29 if (NULL == head) { head = newNode; return OK; }
31 FieldNode *iter = head;
32 while (NULL != iter->next) iter = iter->next;
33 iter->next = newNode;
34 return OK;
38 DbRetVal FieldList::remove(const char* fldName)
40 if (NULL == head)
42 printError(ErrNotExists, "There are no elements in the list. Empty list");
43 return ErrNotExists;
45 FieldNode *iter = head, *prev = head;
46 while (iter->next != NULL)
48 if (0 == strcmp(iter->fldDef.fldName_, fldName))
50 prev->next = iter->next;
51 delete iter;
53 prev = iter;
54 iter = iter->next;
56 if( iter == head) // there is only one node in the list
58 if (0 == strcmp(iter->fldDef.fldName_, fldName))
60 delete head;
61 head = NULL;
62 return OK;
66 if( prev == head) // there are only two node in the list
68 if (0 == strcmp(iter->fldDef.fldName_, fldName))
70 head->next = NULL;
71 delete iter;
72 return OK;
75 printError(ErrNotFound, "There are no elements in the list");
76 return ErrNotFound;
79 DbRetVal FieldList::removeAll()
81 if (NULL == head) return OK;
82 FieldNode *iter = head, *next = head;
83 while (iter->next != NULL)
85 next = iter->next;
86 delete iter;
87 iter = next;
89 delete iter; //deleting the last element
90 head = NULL;
91 return OK;
94 //-1->if val is passed NULL
95 //-2->if fld is not present
96 DbRetVal FieldList::updateBindVal(const char *fldName, void *val )
98 if (NULL == val)
100 printError(ErrBadArg, "Value passed is NULL");
101 return ErrBadArg;
103 FieldNode *iter = head;
104 while(NULL != iter)
106 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
108 iter->fldDef.bindVal_ = val;
109 return OK;
111 iter = iter ->next;
113 printError(ErrNotFound, "Field not present in the list");
114 return ErrNotFound;
117 DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info)
119 FieldNode *iter = head;
120 while(iter != NULL)
122 if (0 == strcmp(iter->fldDef.fldName_, fldName))
124 strcpy(info->fldName , iter->fldDef.fldName_);
125 info->length = iter->fldDef.length_;
126 info->type = iter->fldDef.type_;
127 info->offset = getFieldOffset(fldName);
128 info->isDefault = iter->fldDef.isDefault_;
129 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
130 info->isNull = iter->fldDef.isNull_;
131 info->isPrimary = iter->fldDef.isPrimary_;
132 return OK;
135 iter = iter ->next;
137 return ErrNotFound;
140 int FieldList::getFieldOffset(const char *fldName)
142 FieldNode *iter = head;
143 int offset = 0;
144 while(iter != NULL)
146 if (0 == strcmp(iter->fldDef.fldName_, fldName))
148 return offset;
150 offset = offset + os::align(iter->fldDef.length_);
151 iter = iter ->next;
153 return -1;
155 int FieldList::getFieldOffset(int fldpos)
157 if (fldpos < 1) return -1;
158 FieldNode *iter = head;
159 int offset = 0;
160 int counter =0;
161 while(iter != NULL)
163 if (counter == fldpos -1)
165 return offset;
167 offset = offset + os::align(iter->fldDef.length_);
168 iter = iter ->next;
169 counter++;
171 return -1;
174 //Returns position of field in the list
175 //Count starting from 1
176 //-1 if field not found in the list
177 int FieldList::getFieldPosition(const char *fldName)
179 int position = 1;
180 FieldNode *iter = head;
181 while(iter != NULL)
183 if (0 == strcmp(iter->fldDef.fldName_, fldName))
184 return position;
185 position++;
186 iter = iter->next;
189 return -1;
192 int FieldList::getTupleSize()
194 FieldNode *iter = head;
195 int offset = 0;
196 while(iter != NULL)
198 offset = offset + os::align(iter->fldDef.length_);
199 iter = iter ->next;
201 return offset;
206 DataType FieldList::getFieldType(const char *fldName)
208 FieldNode *iter = head;
209 int offset = 0;
210 while(iter != NULL)
212 if (0 == strcmp(iter->fldDef.fldName_, fldName))
214 return iter->fldDef.type_;
216 iter = iter ->next;
218 return typeUnknown;
221 //-1->if field not present in list
222 size_t FieldList::getFieldLength(const char *fldName)
224 FieldNode *iter = head;
225 int offset = 0;
226 while(iter != NULL)
228 if (0 == strcmp(iter->fldDef.fldName_, fldName))
230 return iter->fldDef.length_;
232 iter = iter ->next;
234 return -1;
238 //No check for duplicates
239 //TODO::User exposed so check for duplicates
240 DbRetVal FieldNameList::append(const char *name)
242 FieldNameNode *newNode = new FieldNameNode();
243 strcpy(newNode->fldName, name);
244 newNode->next = NULL;
245 //If this is the first node, set it as head
246 if (NULL == head) { head = newNode; return OK; }
248 FieldNameNode *it = head;
249 while (NULL != it->next) it = it->next;
250 it->next = newNode;
251 return OK;
253 //-1 -> if there is nothing in list
254 //-2 -> if it is not present in list
255 DbRetVal FieldNameList::remove(const char* name)
257 if (NULL == head)
259 printError(ErrNotExists, "List is empty");
260 return ErrNotExists;
262 FieldNameNode *ite = head, *prev = head;
263 while (ite->next != NULL)
265 if (0 == strcmp(ite->fldName, name))
267 prev->next = ite->next;
268 delete ite;
270 prev = ite;
271 ite = ite->next;
273 if( ite == head) // there is only one node in the list
275 if (0 == strcmp(ite->fldName, name))
277 delete head;
278 head = NULL;
279 return OK;
283 if( prev == head) // there are only two node in the list
285 if (0 == strcmp(ite->fldName, name))
287 head->next = NULL;
288 delete ite;
289 return OK;
292 printError(ErrNotFound, "Field name %s not present in the list", name);
293 return ErrNotFound;
296 DbRetVal FieldNameList::removeAll()
298 if (NULL == head) return OK;
299 FieldNameNode *iter = head, *next = head;
300 while (iter->next != NULL)
302 next = iter->next;
303 delete iter;
304 iter = next;
306 delete iter; //deleting the last element
307 head = NULL;
308 return OK;
311 char* FieldNameList::nextFieldName()
313 if (iter == NULL) return NULL;
314 FieldNameNode *node = iter;
315 iter = iter ->next;
316 return node->fldName;
319 int FieldNameList::size()
321 FieldNameNode *it = head;
322 if (NULL == it) return 0;
323 int count = 1;
324 while (NULL != it->next) {it = it->next; count++;}
325 return count;