cleanup
[csql.git] / src / server / TableDef.cxx
blobf936140c2e35fa944d63160c94cda83008d10e04
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>
21 TableDef::~TableDef()
23 reset();
25 void TableDef::reset()
27 fldList.removeAll();
28 fldCount = 0;
30 int TableDef::addField(const char *name, DataType type, size_t length,
31 const void *defaultValue, bool notNull)
33 if (name == NULL) return (int)ErrBadArg;
34 // The following code checks for duplicates
35 FieldIterator iter = getFieldIterator();
36 while (iter.hasElement())
38 FieldDef def = iter.nextElement();
39 if (! strcmp(def.fldName_, name)) {
40 printError(ErrAlready, "Field %s already Exists", name);
41 return (int) ErrAlready;
44 FieldDef fldDef;
45 strcpy(fldDef.fldName_, name);
46 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
47 fldDef.type_ = type;
48 fldDef.length_ = length;
49 fldDef.bindVal_=NULL;
50 if (defaultValue != NULL)
52 fldDef.isDefault_ = true;
53 if (typeBinary == type) {
54 const char *p = (const char *) defaultValue;
55 while (*p != '\0') {
56 if (! isxdigit((int)(*p++)) ) {
57 printError(ErrBadArg, "Invalid hexadecimal value");
58 return (int) ErrBadArg;
62 os::memcpy(fldDef.defaultValueBuf_, defaultValue, DEFAULT_VALUE_BUF_LENGTH);
64 else
66 fldDef.isDefault_ = false;
67 os::memset(fldDef.defaultValueBuf_,0, DEFAULT_VALUE_BUF_LENGTH);
69 fldDef.isNull_ = notNull;
70 switch(type)
72 case typeString :
73 case typeBinary:
74 fldDef.length_ = length;
75 break;
76 default:
77 fldDef.length_ = AllDataType::size(type);
78 break;
80 int ret = fldList.append(fldDef);
81 if (0 == ret) fldCount++;
82 return ret;
85 int TableDef::dropField(const char *name)
87 int ret = fldList.remove(name);
88 if (0 == ret) fldCount--;
89 return ret;
92 int TableDef::getFieldCount()
94 return fldCount;
97 size_t TableDef::getTupleSize()
99 size_t length = 0;
100 FieldIterator iter = getFieldIterator();
101 while (iter.hasElement())
103 FieldDef def = iter.nextElement();
104 length = length + os::align(def.length_);
106 return length;