tree index releases Ist level mutex twice during unique contraint path
[csql.git] / src / storage / FieldList.cxx
blobbe48f7d0773690ffea6096d0a15565f22901a2c4
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;
105 //-1->if val is passed NULL
106 //-2->if fld is not present
107 DbRetVal FieldList::updateBindVal(const char *fldName, void *val,
108 bool isNullExplicit)
110 if (NULL == val && isNullExplicit == false)
112 printError(ErrBadArg, "Value passed is NULL");
113 return ErrBadArg;
115 FieldNode *iter = head;
116 while(NULL != iter)
118 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
120 if (NULL == val) iter->fldDef.isNullExplicit_ = true;
121 else iter->fldDef.bindVal_ = val;
122 return OK;
124 iter = iter ->next;
126 printError(ErrNotFound, "Field not present in the list");
127 return ErrNotFound;
129 void *FieldList::getBindField(const char *fldName)
131 FieldNode *iter = head;
132 while(NULL != iter)
134 if (strcmp(iter->fldDef.fldName_, fldName) == 0)
136 return iter->fldDef.bindVal_;
138 iter = iter ->next;
140 printError(ErrNotFound, "Field not present in the list");
141 return NULL;
143 void FieldList::fillFieldInfo(int fldpos, void *inp)
145 int pos=0;
146 FieldNode *iter = head;
147 while (pos <fldpos) { iter = iter->next; pos++; }
148 FieldInfoValue *info = (FieldInfoValue*) inp;
149 strcpy(info->fldName , iter->fldDef.fldName_);
150 info->length = iter->fldDef.length_;
151 info->type = iter->fldDef.type_;
152 info->offset = iter->fldDef.offset_;
153 info->isNullable = iter->fldDef.isNull_;
154 info->isPrimary = iter->fldDef.isPrimary_;
155 info->isUnique = iter->fldDef.isUnique_;
156 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
159 DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info)
162 FieldNode *iter = head;
163 if ('*' == fldName[0])
165 //the above is for count(*)
166 strcpy(info->fldName , iter->fldDef.fldName_);
167 info->length = iter->fldDef.length_;
168 info->type = iter->fldDef.type_;
169 info->offset = iter->fldDef.offset_;
170 info->isDefault = iter->fldDef.isDefault_;
171 if (info->isDefault)
172 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
173 info->isNull = iter->fldDef.isNull_;
174 info->isPrimary = iter->fldDef.isPrimary_;
175 info->isUnique = iter->fldDef.isUnique_;
176 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
177 return OK;
180 while(iter != NULL)
182 if (0 == strcmp(iter->fldDef.fldName_, fldName))
184 strcpy(info->fldName , iter->fldDef.fldName_);
185 info->length = iter->fldDef.length_;
186 info->type = iter->fldDef.type_;
187 info->offset = iter->fldDef.offset_;
188 info->isDefault = iter->fldDef.isDefault_;
189 strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_);
190 info->isNull = iter->fldDef.isNull_;
191 info->isPrimary = iter->fldDef.isPrimary_;
192 info->isUnique = iter->fldDef.isUnique_;
193 info->isAutoIncrement = iter->fldDef.isAutoIncrement_;
194 return OK;
196 iter = iter ->next;
198 return ErrNotFound;
201 int FieldList::getFieldOffset(const char *fldName)
203 FieldNode *iter = head;
204 int offset = 0;
205 while(iter != NULL)
207 if (0 == strcmp(iter->fldDef.fldName_, fldName))
209 return offset;
211 if (iter->fldDef.type_ != typeVarchar)
212 offset = offset + iter->fldDef.length_;
213 else offset = offset + sizeof(void *);
214 iter = iter ->next;
216 return -1;
218 int FieldList::getFieldOffset(int fldpos)
220 if (fldpos < 1) return -1;
221 FieldNode *iter = head;
222 int offset = 0;
223 int counter =0;
224 while(iter != NULL)
226 if (counter == fldpos -1)
228 return offset;
230 if (iter->fldDef.type_ != typeVarchar)
231 offset = offset + iter->fldDef.length_;
232 else offset = offset + sizeof(void *);
233 iter = iter ->next;
234 counter++;
236 return -1;
239 //Returns position of field in the list
240 //Count starting from 1
241 //-1 if field not found in the list
242 int FieldList::getFieldPosition(const char *fldName)
244 char onlyFldName[IDENTIFIER_LENGTH];
245 Table::getFieldNameAlone((char*)fldName, onlyFldName);
246 int position = 1;
247 FieldNode *iter = head;
248 while(iter != NULL)
250 if (0 == strcmp(iter->fldDef.fldName_, onlyFldName))
251 return position;
252 position++;
253 iter = iter->next;
256 return -1;
259 int FieldList::getTupleSize()
261 FieldNode *iter = head;
262 int offset = 0;
263 while(iter != NULL)
265 if (iter->fldDef.type_ == typeVarchar) offset += sizeof(void *);
266 else offset = offset + iter->fldDef.length_;
267 iter = iter->next;
269 return offset;
274 DataType FieldList::getFieldType(const char *fldName)
276 FieldNode *iter = head;
277 int offset = 0;
278 while(iter != NULL)
280 if (0 == strcmp(iter->fldDef.fldName_, fldName))
282 return iter->fldDef.type_;
284 iter = iter ->next;
286 return typeUnknown;
289 //-1->if field not present in list
290 size_t FieldList::getFieldLength(const char *fldName)
292 FieldNode *iter = head;
293 int offset = 0;
294 while(iter != NULL)
296 if (0 == strcmp(iter->fldDef.fldName_, fldName))
298 return iter->fldDef.length_;
300 iter = iter ->next;
302 return -1;
306 //No check for duplicates
307 //TODO::User exposed so check for duplicates
308 DbRetVal FieldNameList::append(const char *name)
310 FieldNameNode *newNode = new FieldNameNode();
311 strcpy(newNode->fldName, name);
312 newNode->next = NULL;
313 //If this is the first node, set it as head
314 if (NULL == head) { head = newNode; return OK; }
316 FieldNameNode *it = head;
317 while (NULL != it->next) it = it->next;
318 it->next = newNode;
319 return OK;
321 //-1 -> if there is nothing in list
322 //-2 -> if it is not present in list
323 DbRetVal FieldNameList::remove(const char* name)
325 if (NULL == head)
327 printError(ErrNotExists, "List is empty");
328 return ErrNotExists;
330 FieldNameNode *ite = head, *prev = head;
331 while (ite->next != NULL)
333 if (0 == strcmp(ite->fldName, name))
335 prev->next = ite->next;
336 delete ite;
338 prev = ite;
339 ite = ite->next;
341 if( ite == head) // there is only one node in the list
343 if (0 == strcmp(ite->fldName, name))
345 delete head;
346 head = NULL;
347 return OK;
351 if( prev == head) // there are only two node in the list
353 if (0 == strcmp(ite->fldName, name))
355 head->next = NULL;
356 delete ite;
357 return OK;
360 printError(ErrNotFound, "Field name %s not present in the list", name);
361 return ErrNotFound;
364 DbRetVal FieldNameList::removeAll()
366 if (NULL == head) return OK;
367 FieldNameNode *iter = head, *next = head;
368 while (iter->next != NULL)
370 next = iter->next;
371 delete iter;
372 iter = next;
374 delete iter; //deleting the last element
375 head = NULL;
376 return OK;
379 char* FieldNameList::nextFieldName()
381 if (iter == NULL) return NULL;
382 FieldNameNode *node = iter;
383 iter = iter ->next;
384 return node->fldName;
387 int FieldNameList::size()
389 FieldNameNode *it = head;
390 if (NULL == it) return 0;
391 int count = 1;
392 while (NULL != it->next) {it = it->next; count++;}
393 return count;