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 info
->length
= iter
->fldDef
.length_
;
171 info
->type
= iter
->fldDef
.type_
;
172 info
->offset
= iter
->fldDef
.offset_
;
173 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_
;
185 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
187 strcpy(info
->fldName
, iter
->fldDef
.fldName_
);
188 info
->length
= iter
->fldDef
.length_
;
189 info
->type
= iter
->fldDef
.type_
;
190 info
->offset
= iter
->fldDef
.offset_
;
191 info
->isDefault
= iter
->fldDef
.isDefault_
;
192 strcpy(info
->defaultValueBuf
, iter
->fldDef
.defaultValueBuf_
);
193 info
->isNull
= iter
->fldDef
.isNull_
;
194 info
->isPrimary
= iter
->fldDef
.isPrimary_
;
195 info
->isUnique
= iter
->fldDef
.isUnique_
;
196 info
->isAutoIncrement
= iter
->fldDef
.isAutoIncrement_
;
204 int FieldList::getFieldOffset(const char *fldName
)
206 FieldNode
*iter
= head
;
210 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
214 if (iter
->fldDef
.type_
!= typeVarchar
)
215 offset
= offset
+ iter
->fldDef
.length_
;
216 else offset
= offset
+ sizeof(void *);
222 int FieldList::getFieldOffset(int fldpos
)
224 if (fldpos
< 1) return -1;
225 FieldNode
*iter
= head
;
230 if (counter
== fldpos
-1)
234 if (iter
->fldDef
.type_
!= typeVarchar
)
235 offset
= offset
+ iter
->fldDef
.length_
;
236 else offset
= offset
+ sizeof(void *);
243 //Returns position of field in the list
244 //Count starting from 1
245 //-1 if field not found in the list
246 int FieldList::getFieldPosition(const char *fldName
)
248 char onlyFldName
[IDENTIFIER_LENGTH
];
249 Table::getFieldNameAlone((char*)fldName
, onlyFldName
);
251 FieldNode
*iter
= head
;
254 if (0 == strcmp(iter
->fldDef
.fldName_
, onlyFldName
))
263 int FieldList::getTupleSize()
265 FieldNode
*iter
= head
;
269 if (iter
->fldDef
.type_
== typeVarchar
) offset
+= sizeof(void *);
270 else offset
= offset
+ iter
->fldDef
.length_
;
278 DataType
FieldList::getFieldType(const char *fldName
)
280 FieldNode
*iter
= head
;
284 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
286 return iter
->fldDef
.type_
;
293 //-1->if field not present in list
294 size_t FieldList::getFieldLength(const char *fldName
)
296 FieldNode
*iter
= head
;
300 if (0 == strcmp(iter
->fldDef
.fldName_
, fldName
))
302 return iter
->fldDef
.length_
;
310 //No check for duplicates
311 //TODO::User exposed so check for duplicates
312 DbRetVal
FieldNameList::append(const char *name
)
314 FieldNameNode
*newNode
= new FieldNameNode();
315 strcpy(newNode
->fldName
, name
);
316 newNode
->next
= NULL
;
317 //If this is the first node, set it as head
318 if (NULL
== head
) { head
= newNode
; return OK
; }
320 FieldNameNode
*it
= head
;
321 while (NULL
!= it
->next
) it
= it
->next
;
326 //-1 -> if there is nothing in list
327 //-2 -> if it is not present in list
328 DbRetVal
FieldNameList::remove(const char* name
)
332 printError(ErrNotExists
, "List is empty");
335 FieldNameNode
*ite
= head
, *prev
= head
;
336 while (ite
->next
!= NULL
)
338 if (0 == strcmp(ite
->fldName
, name
))
340 prev
->next
= ite
->next
;
346 if( ite
== head
) // there is only one node in the list
348 if (0 == strcmp(ite
->fldName
, name
))
356 if( prev
== head
) // there are only two node in the list
358 if (0 == strcmp(ite
->fldName
, name
))
365 printError(ErrNotFound
, "Field name %s not present in the list", name
);
369 DbRetVal
FieldNameList::removeAll()
371 if (NULL
== head
) return OK
;
372 FieldNameNode
*iter
= head
, *next
= head
;
373 while (iter
->next
!= NULL
)
379 delete iter
; //deleting the last element
384 char* FieldNameList::nextFieldName()
386 if (iter
== NULL
) return NULL
;
387 FieldNameNode
*node
= iter
;
389 return node
->fldName
;
392 int FieldNameList::size()
394 FieldNameNode
*it
= head
;
395 if (NULL
== it
) return 0;
397 while (NULL
!= it
->next
) {it
= it
->next
; count
++;}