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
97 FieldNode
*iter
= head
;
106 //-1->if val is passed NULL
107 //-2->if fld is not present
108 DbRetVal
FieldList::updateBindVal(const char *fldName
, void *val
,
111 if (NULL
== val
&& isNullExplicit
== false)
113 printError(ErrBadArg
, "Value passed is NULL");
116 FieldNode
*iter
= head
;
119 if (strcmp(iter
->fldDef
.fldName_
, fldName
) == 0)
121 if (NULL
== val
) iter
->fldDef
.isNullExplicit_
= true;
122 else iter
->fldDef
.bindVal_
= val
;
127 printError(ErrNotFound
, "Field not present in the list");
131 void *FieldList::getBindField(const char *fldName
)
133 FieldNode
*iter
= head
;
136 if (strcmp(iter
->fldDef
.fldName_
, fldName
) == 0)
138 return iter
->fldDef
.bindVal_
;
142 printError(ErrNotFound
, "Field not present in the list");
146 void FieldList::fillFieldInfo(int fldpos
, void *inp
)
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;
173 info
->length
= iter
->fldDef
.length_
;
174 info
->type
= iter
->fldDef
.type_
;
175 info
->offset
= iter
->fldDef
.offset_
;
176 info
->isDefault
= iter
->fldDef
.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_
;
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;
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_
;
210 int FieldList::getFieldOffset(const char *fldName
)
212 FieldNode
*iter
= head
;
216 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
220 if (iter
->fldDef
.type_
!= typeVarchar
)
221 offset
= offset
+ iter
->fldDef
.length_
;
223 offset
= offset
+ sizeof(void *);
229 int FieldList::getFieldOffset(int fldpos
)
231 if (fldpos
< 1) return -1;
232 FieldNode
*iter
= head
;
237 if (counter
== fldpos
-1)
241 if (iter
->fldDef
.type_
!= typeVarchar
)
242 offset
= offset
+ iter
->fldDef
.length_
;
243 else offset
= offset
+ sizeof(void *);
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
);
258 FieldNode
*iter
= head
;
261 if (0 == strcmp(iter
->fldDef
.fldName_
, onlyFldName
))
270 int FieldList::getTupleSize()
272 FieldNode
*iter
= head
;
276 if (iter
->fldDef
.type_
== typeVarchar
)
277 offset
+= sizeof(void *);
279 offset
= offset
+ iter
->fldDef
.length_
;
287 DataType
FieldList::getFieldType(const char *fldName
)
289 FieldNode
*iter
= head
;
293 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
295 return iter
->fldDef
.type_
;
302 //-1->if field not present in list
303 size_t FieldList::getFieldLength(const char *fldName
)
305 FieldNode
*iter
= head
;
309 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
311 return iter
->fldDef
.length_
;
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
;
335 //-1 -> if there is nothing in list
336 //-2 -> if it is not present in list
337 DbRetVal
FieldNameList::remove(const char* name
)
341 printError(ErrNotExists
, "List is empty");
344 FieldNameNode
*ite
= head
, *prev
= head
;
345 while (ite
->next
!= NULL
)
347 if (0 == strcmp(ite
->fldName
, name
))
349 prev
->next
= ite
->next
;
355 if( ite
== head
) // there is only one node in the list
357 if (0 == strcmp(ite
->fldName
, name
))
365 if( prev
== head
) // there are only two node in the list
367 if (0 == strcmp(ite
->fldName
, name
))
374 printError(ErrNotFound
, "Field name %s not present in the list", name
);
378 DbRetVal
FieldNameList::removeAll()
380 if (NULL
== head
) return OK
;
381 FieldNameNode
*iter
= head
, *next
= head
;
382 while (iter
->next
!= NULL
)
388 delete iter
; //deleting the last element
393 char* FieldNameList::nextFieldName()
395 if (iter
== NULL
) return NULL
;
396 FieldNameNode
*node
= iter
;
398 return node
->fldName
;
401 int FieldNameList::size()
403 FieldNameNode
*it
= head
;
404 if (NULL
== it
) return 0;
406 while (NULL
!= it
->next
) {it
= it
->next
; count
++;}