reorg files
[csql.git] / src / storage / FieldList.cxx
blobcc87307b764f6821450c9f15ba52ae56b115d23a
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 int FieldList::size()
96 int size = 0;
97 FieldNode *iter = head;
98 while (iter!= NULL)
100 size++;
101 iter = iter->next;
103 return size;
106 //-1->if val is passed NULL
107 //-2->if fld is not present
108 DbRetVal FieldList::updateBindVal(const char *fldName, void *val,
109 bool isNullExplicit)
111 if (NULL == val && isNullExplicit == false)
113 printError(ErrBadArg, "Value passed is NULL");
114 return ErrBadArg;
116 FieldNode *iter = head;
117 while(NULL != iter)
119 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
121 if (NULL == val) iter->fldDef.isNullExplicit_ = true;
122 else iter->fldDef.bindVal_ = val;
123 return OK;
125 iter = iter ->next;
127 printError(ErrNotFound, "Field not present in the list");
128 return ErrNotFound;
131 void *FieldList::getBindField(const char *fldName)
133 FieldNode *iter = head;
134 while(NULL != iter)
136 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
138 return iter->fldDef.bindVal_;
140 iter = iter ->next;
142 printError(ErrNotFound, "Field not present in the list");
143 return NULL;
146 void FieldList::fillFieldInfo(int fldpos, void *inp)
148 int pos=0;
149 FieldNode *iter = head;
150 while (pos <fldpos) { iter = iter->next; pos++; }
151 FieldInfoValue *info = (FieldInfoValue*) inp;
152 strcpy(info->fldName , iter->fldDef.fldName_);
153 info->length = iter->fldDef.length_;
154 info->type = iter->fldDef.type_;
155 info->offset = iter->fldDef.offset_;
156 info->isNullable = iter->fldDef.isNull_;
157 info->isPrimary = iter->fldDef.isPrimary_;
158 info->isUnique = iter->fldDef.isUnique_;
159 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
162 DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info)
165 FieldNode *iter = head;
166 if ('*' == fldName[0])
168 //the above is for count(*)
169 strcpy(info->fldName , iter->fldDef.fldName_);
170 if (info->type == typeString || info->type == typeVarchar)
171 info->length = iter->fldDef.length_ -1;
172 else
173 info->length = iter->fldDef.length_;
174 info->type = iter->fldDef.type_;
175 info->offset = iter->fldDef.offset_;
176 info->isDefault = iter->fldDef.isDefault_;
177 if (info->isDefault)
178 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
179 info->isNull = iter->fldDef.isNull_;
180 info->isPrimary = iter->fldDef.isPrimary_;
181 info->isUnique = iter->fldDef.isUnique_;
182 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
183 return OK;
186 while(iter != NULL)
188 if (0 == strcmp(iter->fldDef.fldName_, fldName))
190 strcpy(info->fldName , iter->fldDef.fldName_);
191 if (info->type == typeString || info->type == typeVarchar)
192 info->length = iter->fldDef.length_ -1;
193 else
194 info->length = iter->fldDef.length_;
195 info->type = iter->fldDef.type_;
196 info->offset = iter->fldDef.offset_;
197 info->isDefault = iter->fldDef.isDefault_;
198 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
199 info->isNull = iter->fldDef.isNull_;
200 info->isPrimary = iter->fldDef.isPrimary_;
201 info->isUnique = iter->fldDef.isUnique_;
202 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
203 return OK;
205 iter = iter ->next;
207 return ErrNotFound;
210 int FieldList::getFieldOffset(const char *fldName)
212 FieldNode *iter = head;
213 int offset = 0;
214 while(iter != NULL)
216 if (0 == strcmp(iter->fldDef.fldName_, fldName))
218 return offset;
220 if (iter->fldDef.type_ != typeVarchar)
221 offset = offset + iter->fldDef.length_;
222 else
223 offset = offset + sizeof(void *);
224 iter = iter ->next;
226 return -1;
229 int FieldList::getFieldOffset(int fldpos)
231 if (fldpos < 1) return -1;
232 FieldNode *iter = head;
233 int offset = 0;
234 int counter =0;
235 while(iter != NULL)
237 if (counter == fldpos -1)
239 return offset;
241 if (iter->fldDef.type_ != typeVarchar)
242 offset = offset + iter->fldDef.length_;
243 else offset = offset + sizeof(void *);
244 iter = iter ->next;
245 counter++;
247 return -1;
250 //Returns position of field in the list
251 //Count starting from 1
252 //-1 if field not found in the list
253 int FieldList::getFieldPosition(const char *fldName)
255 char onlyFldName[IDENTIFIER_LENGTH];
256 Table::getFieldNameAlone((char*)fldName, onlyFldName);
257 int position = 1;
258 FieldNode *iter = head;
259 while(iter != NULL)
261 if (0 == strcmp(iter->fldDef.fldName_, onlyFldName))
262 return position;
263 position++;
264 iter = iter->next;
267 return -1;
270 int FieldList::getTupleSize()
272 FieldNode *iter = head;
273 int offset = 0;
274 while(iter != NULL)
276 if (iter->fldDef.type_ == typeVarchar)
277 offset += sizeof(void *);
278 else
279 offset = offset + iter->fldDef.length_;
280 iter = iter->next;
282 return offset;
287 DataType FieldList::getFieldType(const char *fldName)
289 FieldNode *iter = head;
290 int offset = 0;
291 while(iter != NULL)
293 if (0 == strcmp(iter->fldDef.fldName_, fldName))
295 return iter->fldDef.type_;
297 iter = iter ->next;
299 return typeUnknown;
302 //-1->if field not present in list
303 size_t FieldList::getFieldLength(const char *fldName)
305 FieldNode *iter = head;
306 int offset = 0;
307 while(iter != NULL)
309 if (0 == strcmp(iter->fldDef.fldName_, fldName))
311 return iter->fldDef.length_;
313 iter = iter ->next;
315 return -1;
319 //No check for duplicates
320 //TODO::User exposed so check for duplicates
321 DbRetVal FieldNameList::append(const char *name)
323 FieldNameNode *newNode = new FieldNameNode();
324 strcpy(newNode->fldName, name);
325 newNode->next = NULL;
326 //If this is the first node, set it as head
327 if (NULL == head) { head = newNode; return OK; }
329 FieldNameNode *it = head;
330 while (NULL != it->next) it = it->next;
331 it->next = newNode;
332 return OK;
335 //-1 -> if there is nothing in list
336 //-2 -> if it is not present in list
337 DbRetVal FieldNameList::remove(const char* name)
339 if (NULL == head)
341 printError(ErrNotExists, "List is empty");
342 return ErrNotExists;
344 FieldNameNode *ite = head, *prev = head;
345 while (ite->next != NULL)
347 if (0 == strcmp(ite->fldName, name))
349 prev->next = ite->next;
350 delete ite;
352 prev = ite;
353 ite = ite->next;
355 if( ite == head) // there is only one node in the list
357 if (0 == strcmp(ite->fldName, name))
359 delete head;
360 head = NULL;
361 return OK;
365 if( prev == head) // there are only two node in the list
367 if (0 == strcmp(ite->fldName, name))
369 head->next = NULL;
370 delete ite;
371 return OK;
374 printError(ErrNotFound, "Field name %s not present in the list", name);
375 return ErrNotFound;
378 DbRetVal FieldNameList::removeAll()
380 if (NULL == head) return OK;
381 FieldNameNode *iter = head, *next = head;
382 while (iter->next != NULL)
384 next = iter->next;
385 delete iter;
386 iter = next;
388 delete iter; //deleting the last element
389 head = NULL;
390 return OK;
393 char* FieldNameList::nextFieldName()
395 if (iter == NULL) return NULL;
396 FieldNameNode *node = iter;
397 iter = iter ->next;
398 return node->fldName;
401 int FieldNameList::size()
403 FieldNameNode *it = head;
404 if (NULL == it) return 0;
405 int count = 1;
406 while (NULL != it->next) {it = it->next; count++;}
407 return count;