commiting changes from enterprise version for V2.4
[csql.git] / src / storage / FieldList.cxx
blob756072b6e0ec8d9a6abf89054f8d458869b60e5c
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 if ('*' == fldName[0])
136 //the above is for count(*)
137 strcpy(info->fldName , iter->fldDef.fldName_);
138 info->length = iter->fldDef.length_;
139 info->type = iter->fldDef.type_;
140 info->offset = iter->fldDef.offset_;
141 info->isDefault = iter->fldDef.isDefault_;
142 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
143 info->isNull = iter->fldDef.isNull_;
144 info->isPrimary = iter->fldDef.isPrimary_;
145 info->isUnique = iter->fldDef.isUnique_;
146 return OK;
149 while(iter != NULL)
151 if (0 == strcmp(iter->fldDef.fldName_, fldName))
153 strcpy(info->fldName , iter->fldDef.fldName_);
154 info->length = iter->fldDef.length_;
155 info->type = iter->fldDef.type_;
156 info->offset = iter->fldDef.offset_;
157 info->isDefault = iter->fldDef.isDefault_;
158 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
159 info->isNull = iter->fldDef.isNull_;
160 info->isPrimary = iter->fldDef.isPrimary_;
161 info->isUnique = iter->fldDef.isUnique_;
162 return OK;
164 iter = iter ->next;
166 return ErrNotFound;
169 int FieldList::getFieldOffset(const char *fldName)
171 FieldNode *iter = head;
172 int offset = 0;
173 while(iter != NULL)
175 if (0 == strcmp(iter->fldDef.fldName_, fldName))
177 return offset;
179 offset = offset + os::align(iter->fldDef.length_);
180 iter = iter ->next;
182 return -1;
184 int FieldList::getFieldOffset(int fldpos)
186 if (fldpos < 1) return -1;
187 FieldNode *iter = head;
188 int offset = 0;
189 int counter =0;
190 while(iter != NULL)
192 if (counter == fldpos -1)
194 return offset;
196 offset = offset + os::align(iter->fldDef.length_);
197 iter = iter ->next;
198 counter++;
200 return -1;
203 //Returns position of field in the list
204 //Count starting from 1
205 //-1 if field not found in the list
206 int FieldList::getFieldPosition(const char *fldName)
208 int position = 1;
209 FieldNode *iter = head;
210 while(iter != NULL)
212 if (0 == strcmp(iter->fldDef.fldName_, fldName))
213 return position;
214 position++;
215 iter = iter->next;
218 return -1;
221 int FieldList::getTupleSize()
223 FieldNode *iter = head;
224 int offset = 0;
225 while(iter != NULL)
227 offset = offset + os::align(iter->fldDef.length_);
228 iter = iter ->next;
230 return offset;
235 DataType FieldList::getFieldType(const char *fldName)
237 FieldNode *iter = head;
238 int offset = 0;
239 while(iter != NULL)
241 if (0 == strcmp(iter->fldDef.fldName_, fldName))
243 return iter->fldDef.type_;
245 iter = iter ->next;
247 return typeUnknown;
250 //-1->if field not present in list
251 size_t FieldList::getFieldLength(const char *fldName)
253 FieldNode *iter = head;
254 int offset = 0;
255 while(iter != NULL)
257 if (0 == strcmp(iter->fldDef.fldName_, fldName))
259 return iter->fldDef.length_;
261 iter = iter ->next;
263 return -1;
267 //No check for duplicates
268 //TODO::User exposed so check for duplicates
269 DbRetVal FieldNameList::append(const char *name)
271 FieldNameNode *newNode = new FieldNameNode();
272 strcpy(newNode->fldName, name);
273 newNode->next = NULL;
274 //If this is the first node, set it as head
275 if (NULL == head) { head = newNode; return OK; }
277 FieldNameNode *it = head;
278 while (NULL != it->next) it = it->next;
279 it->next = newNode;
280 return OK;
282 //-1 -> if there is nothing in list
283 //-2 -> if it is not present in list
284 DbRetVal FieldNameList::remove(const char* name)
286 if (NULL == head)
288 printError(ErrNotExists, "List is empty");
289 return ErrNotExists;
291 FieldNameNode *ite = head, *prev = head;
292 while (ite->next != NULL)
294 if (0 == strcmp(ite->fldName, name))
296 prev->next = ite->next;
297 delete ite;
299 prev = ite;
300 ite = ite->next;
302 if( ite == head) // there is only one node in the list
304 if (0 == strcmp(ite->fldName, name))
306 delete head;
307 head = NULL;
308 return OK;
312 if( prev == head) // there are only two node in the list
314 if (0 == strcmp(ite->fldName, name))
316 head->next = NULL;
317 delete ite;
318 return OK;
321 printError(ErrNotFound, "Field name %s not present in the list", name);
322 return ErrNotFound;
325 DbRetVal FieldNameList::removeAll()
327 if (NULL == head) return OK;
328 FieldNameNode *iter = head, *next = head;
329 while (iter->next != NULL)
331 next = iter->next;
332 delete iter;
333 iter = next;
335 delete iter; //deleting the last element
336 head = NULL;
337 return OK;
340 char* FieldNameList::nextFieldName()
342 if (iter == NULL) return NULL;
343 FieldNameNode *node = iter;
344 iter = iter ->next;
345 return node->fldName;
348 int FieldNameList::size()
350 FieldNameNode *it = head;
351 if (NULL == it) return 0;
352 int count = 1;
353 while (NULL != it->next) {it = it->next; count++;}
354 return count;