Performance fixes.
[csql.git] / src / storage / FieldList.cxx
blobd0196f1659935b0fc8aa49165122a7b048910a51
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 void FieldList::fillFieldInfo(int fldpos, void *inp)
132 int pos=0;
133 FieldNode *iter = head;
134 while (pos <fldpos) { iter = iter->next; pos++; }
135 FieldInfoValue *info = (FieldInfoValue*) inp;
136 strcpy(info->fldName , iter->fldDef.fldName_);
137 info->length = iter->fldDef.length_;
138 info->type = iter->fldDef.type_;
139 info->offset = iter->fldDef.offset_;
140 info->isNullable = iter->fldDef.isNull_;
141 info->isPrimary = iter->fldDef.isPrimary_;
142 info->isUnique = iter->fldDef.isUnique_;
143 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
146 DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info)
149 FieldNode *iter = head;
150 if ('*' == fldName[0])
152 //the above is for count(*)
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 if (info->isDefault)
159 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
160 info->isNull = iter->fldDef.isNull_;
161 info->isPrimary = iter->fldDef.isPrimary_;
162 info->isUnique = iter->fldDef.isUnique_;
163 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
164 return OK;
167 while(iter != NULL)
169 if (0 == strcmp(iter->fldDef.fldName_, fldName))
171 strcpy(info->fldName , iter->fldDef.fldName_);
172 info->length = iter->fldDef.length_;
173 info->type = iter->fldDef.type_;
174 info->offset = iter->fldDef.offset_;
175 info->isDefault = iter->fldDef.isDefault_;
176 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
177 info->isNull = iter->fldDef.isNull_;
178 info->isPrimary = iter->fldDef.isPrimary_;
179 info->isUnique = iter->fldDef.isUnique_;
180 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
181 return OK;
183 iter = iter ->next;
185 return ErrNotFound;
188 int FieldList::getFieldOffset(const char *fldName)
190 FieldNode *iter = head;
191 int offset = 0;
192 while(iter != NULL)
194 if (0 == strcmp(iter->fldDef.fldName_, fldName))
196 return offset;
198 offset = offset + iter->fldDef.length_;
199 iter = iter ->next;
201 return -1;
203 int FieldList::getFieldOffset(int fldpos)
205 if (fldpos < 1) return -1;
206 FieldNode *iter = head;
207 int offset = 0;
208 int counter =0;
209 while(iter != NULL)
211 if (counter == fldpos -1)
213 return offset;
215 offset = offset + iter->fldDef.length_;
216 iter = iter ->next;
217 counter++;
219 return -1;
222 //Returns position of field in the list
223 //Count starting from 1
224 //-1 if field not found in the list
225 int FieldList::getFieldPosition(const char *fldName)
227 char onlyFldName[IDENTIFIER_LENGTH];
228 Table::getFieldNameAlone((char*)fldName, onlyFldName);
229 int position = 1;
230 FieldNode *iter = head;
231 while(iter != NULL)
233 if (0 == strcmp(iter->fldDef.fldName_, onlyFldName))
234 return position;
235 position++;
236 iter = iter->next;
239 return -1;
242 int FieldList::getTupleSize()
244 FieldNode *iter = head;
245 int offset = 0;
246 while(iter != NULL)
248 offset = offset + iter->fldDef.length_;
249 iter = iter ->next;
251 return offset;
256 DataType FieldList::getFieldType(const char *fldName)
258 FieldNode *iter = head;
259 int offset = 0;
260 while(iter != NULL)
262 if (0 == strcmp(iter->fldDef.fldName_, fldName))
264 return iter->fldDef.type_;
266 iter = iter ->next;
268 return typeUnknown;
271 //-1->if field not present in list
272 size_t FieldList::getFieldLength(const char *fldName)
274 FieldNode *iter = head;
275 int offset = 0;
276 while(iter != NULL)
278 if (0 == strcmp(iter->fldDef.fldName_, fldName))
280 return iter->fldDef.length_;
282 iter = iter ->next;
284 return -1;
288 //No check for duplicates
289 //TODO::User exposed so check for duplicates
290 DbRetVal FieldNameList::append(const char *name)
292 FieldNameNode *newNode = new FieldNameNode();
293 strcpy(newNode->fldName, name);
294 newNode->next = NULL;
295 //If this is the first node, set it as head
296 if (NULL == head) { head = newNode; return OK; }
298 FieldNameNode *it = head;
299 while (NULL != it->next) it = it->next;
300 it->next = newNode;
301 return OK;
303 //-1 -> if there is nothing in list
304 //-2 -> if it is not present in list
305 DbRetVal FieldNameList::remove(const char* name)
307 if (NULL == head)
309 printError(ErrNotExists, "List is empty");
310 return ErrNotExists;
312 FieldNameNode *ite = head, *prev = head;
313 while (ite->next != NULL)
315 if (0 == strcmp(ite->fldName, name))
317 prev->next = ite->next;
318 delete ite;
320 prev = ite;
321 ite = ite->next;
323 if( ite == head) // there is only one node in the list
325 if (0 == strcmp(ite->fldName, name))
327 delete head;
328 head = NULL;
329 return OK;
333 if( prev == head) // there are only two node in the list
335 if (0 == strcmp(ite->fldName, name))
337 head->next = NULL;
338 delete ite;
339 return OK;
342 printError(ErrNotFound, "Field name %s not present in the list", name);
343 return ErrNotFound;
346 DbRetVal FieldNameList::removeAll()
348 if (NULL == head) return OK;
349 FieldNameNode *iter = head, *next = head;
350 while (iter->next != NULL)
352 next = iter->next;
353 delete iter;
354 iter = next;
356 delete iter; //deleting the last element
357 head = NULL;
358 return OK;
361 char* FieldNameList::nextFieldName()
363 if (iter == NULL) return NULL;
364 FieldNameNode *node = iter;
365 iter = iter ->next;
366 return node->fldName;
369 int FieldNameList::size()
371 FieldNameNode *it = head;
372 if (NULL == it) return 0;
373 int count = 1;
374 while (NULL != it->next) {it = it->next; count++;}
375 return count;