Fix for Bug # 2483638
[csql.git] / src / storage / FieldList.cxx
blobc2021244018ad0272157e6376712c9c9f5f5f3c1
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;
116 void *FieldList::getBindField(const char *fldName)
118 FieldNode *iter = head;
119 while(NULL != iter)
121 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
123 return iter->fldDef.bindVal_;
125 iter = iter ->next;
127 printError(ErrNotFound, "Field not present in the list");
128 return NULL;
130 DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info)
133 FieldNode *iter = head;
134 while(iter != NULL)
136 if (0 == strcmp(iter->fldDef.fldName_, fldName))
138 strcpy(info->fldName , iter->fldDef.fldName_);
139 info->length = iter->fldDef.length_;
140 info->type = iter->fldDef.type_;
141 info->offset = iter->fldDef.offset_;
142 info->isDefault = iter->fldDef.isDefault_;
143 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
144 info->isNull = iter->fldDef.isNull_;
145 info->isPrimary = iter->fldDef.isPrimary_;
146 info->isUnique = iter->fldDef.isUnique_;
147 return OK;
149 iter = iter ->next;
151 return ErrNotFound;
154 int FieldList::getFieldOffset(const char *fldName)
156 FieldNode *iter = head;
157 int offset = 0;
158 while(iter != NULL)
160 if (0 == strcmp(iter->fldDef.fldName_, fldName))
162 return offset;
164 offset = offset + os::align(iter->fldDef.length_);
165 iter = iter ->next;
167 return -1;
169 int FieldList::getFieldOffset(int fldpos)
171 if (fldpos < 1) return -1;
172 FieldNode *iter = head;
173 int offset = 0;
174 int counter =0;
175 while(iter != NULL)
177 if (counter == fldpos -1)
179 return offset;
181 offset = offset + os::align(iter->fldDef.length_);
182 iter = iter ->next;
183 counter++;
185 return -1;
188 //Returns position of field in the list
189 //Count starting from 1
190 //-1 if field not found in the list
191 int FieldList::getFieldPosition(const char *fldName)
193 int position = 1;
194 FieldNode *iter = head;
195 while(iter != NULL)
197 if (0 == strcmp(iter->fldDef.fldName_, fldName))
198 return position;
199 position++;
200 iter = iter->next;
203 return -1;
206 int FieldList::getTupleSize()
208 FieldNode *iter = head;
209 int offset = 0;
210 while(iter != NULL)
212 offset = offset + os::align(iter->fldDef.length_);
213 iter = iter ->next;
215 return offset;
220 DataType FieldList::getFieldType(const char *fldName)
222 FieldNode *iter = head;
223 int offset = 0;
224 while(iter != NULL)
226 if (0 == strcmp(iter->fldDef.fldName_, fldName))
228 return iter->fldDef.type_;
230 iter = iter ->next;
232 return typeUnknown;
235 //-1->if field not present in list
236 size_t FieldList::getFieldLength(const char *fldName)
238 FieldNode *iter = head;
239 int offset = 0;
240 while(iter != NULL)
242 if (0 == strcmp(iter->fldDef.fldName_, fldName))
244 return iter->fldDef.length_;
246 iter = iter ->next;
248 return -1;
252 //No check for duplicates
253 //TODO::User exposed so check for duplicates
254 DbRetVal FieldNameList::append(const char *name)
256 FieldNameNode *newNode = new FieldNameNode();
257 strcpy(newNode->fldName, name);
258 newNode->next = NULL;
259 //If this is the first node, set it as head
260 if (NULL == head) { head = newNode; return OK; }
262 FieldNameNode *it = head;
263 while (NULL != it->next) it = it->next;
264 it->next = newNode;
265 return OK;
267 //-1 -> if there is nothing in list
268 //-2 -> if it is not present in list
269 DbRetVal FieldNameList::remove(const char* name)
271 if (NULL == head)
273 printError(ErrNotExists, "List is empty");
274 return ErrNotExists;
276 FieldNameNode *ite = head, *prev = head;
277 while (ite->next != NULL)
279 if (0 == strcmp(ite->fldName, name))
281 prev->next = ite->next;
282 delete ite;
284 prev = ite;
285 ite = ite->next;
287 if( ite == head) // there is only one node in the list
289 if (0 == strcmp(ite->fldName, name))
291 delete head;
292 head = NULL;
293 return OK;
297 if( prev == head) // there are only two node in the list
299 if (0 == strcmp(ite->fldName, name))
301 head->next = NULL;
302 delete ite;
303 return OK;
306 printError(ErrNotFound, "Field name %s not present in the list", name);
307 return ErrNotFound;
310 DbRetVal FieldNameList::removeAll()
312 if (NULL == head) return OK;
313 FieldNameNode *iter = head, *next = head;
314 while (iter->next != NULL)
316 next = iter->next;
317 delete iter;
318 iter = next;
320 delete iter; //deleting the last element
321 head = NULL;
322 return OK;
325 char* FieldNameList::nextFieldName()
327 if (iter == NULL) return NULL;
328 FieldNameNode *node = iter;
329 iter = iter ->next;
330 return node->fldName;
333 int FieldNameList::size()
335 FieldNameNode *it = head;
336 if (NULL == it) return 0;
337 int count = 1;
338 while (NULL != it->next) {it = it->next; count++;}
339 return count;