1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
18 #include<CatalogTables.h>
22 //does not check for duplicates
23 DbRetVal
FieldList::append(FieldDef fDef
)
25 FieldNode
*newNode
= new FieldNode();
26 newNode
->fldDef
= fDef
;
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
;
38 DbRetVal
FieldList::remove(const char* fldName
)
42 printError(ErrNotExists
, "There are no elements in the list. Empty list");
45 FieldNode
*iter
= head
, *prev
= head
;
46 while (iter
->next
!= NULL
)
48 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
50 prev
->next
= iter
->next
;
56 if( iter
== head
) // there is only one node in the list
58 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
66 if( prev
== head
) // there are only two node in the list
68 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
75 printError(ErrNotFound
, "There are no elements in the list");
79 DbRetVal
FieldList::removeAll()
81 if (NULL
== head
) return OK
;
82 FieldNode
*iter
= head
, *next
= head
;
83 while (iter
->next
!= NULL
)
89 delete iter
; //deleting the last element
94 //-1->if val is passed NULL
95 //-2->if fld is not present
96 DbRetVal
FieldList::updateBindVal(const char *fldName
, void *val
)
100 printError(ErrBadArg
, "Value passed is NULL");
103 FieldNode
*iter
= head
;
106 if (strcmp(iter
->fldDef
.fldName_
, fldName
) == 0)
108 iter
->fldDef
.bindVal_
= val
;
113 printError(ErrNotFound
, "Field not present in the list");
116 void *FieldList::getBindField(const char *fldName
)
118 FieldNode
*iter
= head
;
121 if (strcmp(iter
->fldDef
.fldName_
, fldName
) == 0)
123 return iter
->fldDef
.bindVal_
;
127 printError(ErrNotFound
, "Field not present in the list");
130 DbRetVal
FieldList::getFieldInfo(const char *fldName
, FieldInfo
*&info
)
133 FieldNode
*iter
= head
;
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_
;
153 int FieldList::getFieldOffset(const char *fldName
)
155 FieldNode
*iter
= head
;
159 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
163 offset
= offset
+ os::align(iter
->fldDef
.length_
);
168 int FieldList::getFieldOffset(int fldpos
)
170 if (fldpos
< 1) return -1;
171 FieldNode
*iter
= head
;
176 if (counter
== fldpos
-1)
180 offset
= offset
+ os::align(iter
->fldDef
.length_
);
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
)
193 FieldNode
*iter
= head
;
196 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
205 int FieldList::getTupleSize()
207 FieldNode
*iter
= head
;
211 offset
= offset
+ os::align(iter
->fldDef
.length_
);
219 DataType
FieldList::getFieldType(const char *fldName
)
221 FieldNode
*iter
= head
;
225 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
227 return iter
->fldDef
.type_
;
234 //-1->if field not present in list
235 size_t FieldList::getFieldLength(const char *fldName
)
237 FieldNode
*iter
= head
;
241 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
243 return iter
->fldDef
.length_
;
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
;
266 //-1 -> if there is nothing in list
267 //-2 -> if it is not present in list
268 DbRetVal
FieldNameList::remove(const char* name
)
272 printError(ErrNotExists
, "List is empty");
275 FieldNameNode
*ite
= head
, *prev
= head
;
276 while (ite
->next
!= NULL
)
278 if (0 == strcmp(ite
->fldName
, name
))
280 prev
->next
= ite
->next
;
286 if( ite
== head
) // there is only one node in the list
288 if (0 == strcmp(ite
->fldName
, name
))
296 if( prev
== head
) // there are only two node in the list
298 if (0 == strcmp(ite
->fldName
, name
))
305 printError(ErrNotFound
, "Field name %s not present in the list", name
);
309 DbRetVal
FieldNameList::removeAll()
311 if (NULL
== head
) return OK
;
312 FieldNameNode
*iter
= head
, *next
= head
;
313 while (iter
->next
!= NULL
)
319 delete iter
; //deleting the last element
324 char* FieldNameList::nextFieldName()
326 if (iter
== NULL
) return NULL
;
327 FieldNameNode
*node
= iter
;
329 return node
->fldName
;
332 int FieldNameList::size()
334 FieldNameNode
*it
= head
;
335 if (NULL
== it
) return 0;
337 while (NULL
!= it
->next
) {it
= it
->next
; count
++;}