corrected to accept hostname and connect to network
[csql.git] / src / storage / FieldList.cxx
blob8c4b3ab07e91d31d852768e5f94bf0384cfcb209
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 = getFieldOffset(fldName);
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 return OK;
148 iter = iter ->next;
150 return ErrNotFound;
153 int FieldList::getFieldOffset(const char *fldName)
155 FieldNode *iter = head;
156 int offset = 0;
157 while(iter != NULL)
159 if (0 == strcmp(iter->fldDef.fldName_, fldName))
161 return offset;
163 offset = offset + os::align(iter->fldDef.length_);
164 iter = iter ->next;
166 return -1;
168 int FieldList::getFieldOffset(int fldpos)
170 if (fldpos < 1) return -1;
171 FieldNode *iter = head;
172 int offset = 0;
173 int counter =0;
174 while(iter != NULL)
176 if (counter == fldpos -1)
178 return offset;
180 offset = offset + os::align(iter->fldDef.length_);
181 iter = iter ->next;
182 counter++;
184 return -1;
187 //Returns position of field in the list
188 //Count starting from 1
189 //-1 if field not found in the list
190 int FieldList::getFieldPosition(const char *fldName)
192 int position = 1;
193 FieldNode *iter = head;
194 while(iter != NULL)
196 if (0 == strcmp(iter->fldDef.fldName_, fldName))
197 return position;
198 position++;
199 iter = iter->next;
202 return -1;
205 int FieldList::getTupleSize()
207 FieldNode *iter = head;
208 int offset = 0;
209 while(iter != NULL)
211 offset = offset + os::align(iter->fldDef.length_);
212 iter = iter ->next;
214 return offset;
219 DataType FieldList::getFieldType(const char *fldName)
221 FieldNode *iter = head;
222 int offset = 0;
223 while(iter != NULL)
225 if (0 == strcmp(iter->fldDef.fldName_, fldName))
227 return iter->fldDef.type_;
229 iter = iter ->next;
231 return typeUnknown;
234 //-1->if field not present in list
235 size_t FieldList::getFieldLength(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.length_;
245 iter = iter ->next;
247 return -1;
251 //No check for duplicates
252 //TODO::User exposed so check for duplicates
253 DbRetVal FieldNameList::append(const char *name)
255 FieldNameNode *newNode = new FieldNameNode();
256 strcpy(newNode->fldName, name);
257 newNode->next = NULL;
258 //If this is the first node, set it as head
259 if (NULL == head) { head = newNode; return OK; }
261 FieldNameNode *it = head;
262 while (NULL != it->next) it = it->next;
263 it->next = newNode;
264 return OK;
266 //-1 -> if there is nothing in list
267 //-2 -> if it is not present in list
268 DbRetVal FieldNameList::remove(const char* name)
270 if (NULL == head)
272 printError(ErrNotExists, "List is empty");
273 return ErrNotExists;
275 FieldNameNode *ite = head, *prev = head;
276 while (ite->next != NULL)
278 if (0 == strcmp(ite->fldName, name))
280 prev->next = ite->next;
281 delete ite;
283 prev = ite;
284 ite = ite->next;
286 if( ite == head) // there is only one node in the list
288 if (0 == strcmp(ite->fldName, name))
290 delete head;
291 head = NULL;
292 return OK;
296 if( prev == head) // there are only two node in the list
298 if (0 == strcmp(ite->fldName, name))
300 head->next = NULL;
301 delete ite;
302 return OK;
305 printError(ErrNotFound, "Field name %s not present in the list", name);
306 return ErrNotFound;
309 DbRetVal FieldNameList::removeAll()
311 if (NULL == head) return OK;
312 FieldNameNode *iter = head, *next = head;
313 while (iter->next != NULL)
315 next = iter->next;
316 delete iter;
317 iter = next;
319 delete iter; //deleting the last element
320 head = NULL;
321 return OK;
324 char* FieldNameList::nextFieldName()
326 if (iter == NULL) return NULL;
327 FieldNameNode *node = iter;
328 iter = iter ->next;
329 return node->fldName;
332 int FieldNameList::size()
334 FieldNameNode *it = head;
335 if (NULL == it) return 0;
336 int count = 1;
337 while (NULL != it->next) {it = it->next; count++;}
338 return count;