Update in sync with enterprise version.
[csql.git] / src / storage / FieldList.cxx
blob6923106a6ef1f9d39d0626856ac23565e359d0bf
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 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 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
163 return OK;
166 while(iter != NULL)
168 if (0 == strcmp(iter->fldDef.fldName_, fldName))
170 strcpy(info->fldName , iter->fldDef.fldName_);
171 info->length = iter->fldDef.length_;
172 info->type = iter->fldDef.type_;
173 info->offset = iter->fldDef.offset_;
174 info->isDefault = iter->fldDef.isDefault_;
175 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
176 info->isNull = iter->fldDef.isNull_;
177 info->isPrimary = iter->fldDef.isPrimary_;
178 info->isUnique = iter->fldDef.isUnique_;
179 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
180 return OK;
182 iter = iter ->next;
184 return ErrNotFound;
187 int FieldList::getFieldOffset(const char *fldName)
189 FieldNode *iter = head;
190 int offset = 0;
191 while(iter != NULL)
193 if (0 == strcmp(iter->fldDef.fldName_, fldName))
195 return offset;
197 offset = offset + iter->fldDef.length_;
198 iter = iter ->next;
200 return -1;
202 int FieldList::getFieldOffset(int fldpos)
204 if (fldpos < 1) return -1;
205 FieldNode *iter = head;
206 int offset = 0;
207 int counter =0;
208 while(iter != NULL)
210 if (counter == fldpos -1)
212 return offset;
214 offset = offset + iter->fldDef.length_;
215 iter = iter ->next;
216 counter++;
218 return -1;
221 //Returns position of field in the list
222 //Count starting from 1
223 //-1 if field not found in the list
224 int FieldList::getFieldPosition(const char *fldName)
226 char onlyFldName[IDENTIFIER_LENGTH];
227 Table::getFieldNameAlone((char*)fldName, onlyFldName);
228 int position = 1;
229 FieldNode *iter = head;
230 while(iter != NULL)
232 if (0 == strcmp(iter->fldDef.fldName_, onlyFldName))
233 return position;
234 position++;
235 iter = iter->next;
238 return -1;
241 int FieldList::getTupleSize()
243 FieldNode *iter = head;
244 int offset = 0;
245 while(iter != NULL)
247 offset = offset + iter->fldDef.length_;
248 iter = iter ->next;
250 return offset;
255 DataType FieldList::getFieldType(const char *fldName)
257 FieldNode *iter = head;
258 int offset = 0;
259 while(iter != NULL)
261 if (0 == strcmp(iter->fldDef.fldName_, fldName))
263 return iter->fldDef.type_;
265 iter = iter ->next;
267 return typeUnknown;
270 //-1->if field not present in list
271 size_t FieldList::getFieldLength(const char *fldName)
273 FieldNode *iter = head;
274 int offset = 0;
275 while(iter != NULL)
277 if (0 == strcmp(iter->fldDef.fldName_, fldName))
279 return iter->fldDef.length_;
281 iter = iter ->next;
283 return -1;
287 //No check for duplicates
288 //TODO::User exposed so check for duplicates
289 DbRetVal FieldNameList::append(const char *name)
291 FieldNameNode *newNode = new FieldNameNode();
292 strcpy(newNode->fldName, name);
293 newNode->next = NULL;
294 //If this is the first node, set it as head
295 if (NULL == head) { head = newNode; return OK; }
297 FieldNameNode *it = head;
298 while (NULL != it->next) it = it->next;
299 it->next = newNode;
300 return OK;
302 //-1 -> if there is nothing in list
303 //-2 -> if it is not present in list
304 DbRetVal FieldNameList::remove(const char* name)
306 if (NULL == head)
308 printError(ErrNotExists, "List is empty");
309 return ErrNotExists;
311 FieldNameNode *ite = head, *prev = head;
312 while (ite->next != NULL)
314 if (0 == strcmp(ite->fldName, name))
316 prev->next = ite->next;
317 delete ite;
319 prev = ite;
320 ite = ite->next;
322 if( ite == head) // there is only one node in the list
324 if (0 == strcmp(ite->fldName, name))
326 delete head;
327 head = NULL;
328 return OK;
332 if( prev == head) // there are only two node in the list
334 if (0 == strcmp(ite->fldName, name))
336 head->next = NULL;
337 delete ite;
338 return OK;
341 printError(ErrNotFound, "Field name %s not present in the list", name);
342 return ErrNotFound;
345 DbRetVal FieldNameList::removeAll()
347 if (NULL == head) return OK;
348 FieldNameNode *iter = head, *next = head;
349 while (iter->next != NULL)
351 next = iter->next;
352 delete iter;
353 iter = next;
355 delete iter; //deleting the last element
356 head = NULL;
357 return OK;
360 char* FieldNameList::nextFieldName()
362 if (iter == NULL) return NULL;
363 FieldNameNode *node = iter;
364 iter = iter ->next;
365 return node->fldName;
368 int FieldNameList::size()
370 FieldNameNode *it = head;
371 if (NULL == it) return 0;
372 int count = 1;
373 while (NULL != it->next) {it = it->next; count++;}
374 return count;