code reorg
[csql.git] / src / relational / table / TableDef.cxx
blobe6c12fa5abff18c3155e51e434cbd1cd4945a204
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();
26 void TableDef::reset()
28 fldList.removeAll();
29 fldCount = 0;
32 int TableDef::addField(const char *name, DataType type, size_t length,
33 const void *defaultValue, bool notNull, bool autoIn)
35 if (name == NULL) return (int)ErrBadArg;
36 if(strlen(name)>IDENTIFIER_LENGTH)
38 printError(ErrBadRange,"Field name should not exceed 64 character");
39 return (int)ErrBadRange;
41 // The following code checks for duplicates
42 FieldIterator iter = getFieldIterator();
43 while (iter.hasElement())
45 FieldDef *def = iter.nextElement();
46 if (! strcmp(def->fldName_, name)) {
47 printError(ErrAlready, "Field %s already Exists", name);
48 return (int) ErrAlready;
52 if (!Util::isIdentifier((char*)name)) {
53 printError(ErrBadArg, "fieldname contains invalid characters");
54 return (int) ErrBadArg;
56 FieldDef fldDef;
57 strcpy(fldDef.fldName_, name);
58 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
59 fldDef.type_ = type;
60 fldDef.length_ = AllDataType::size(type, length);
61 fldDef.bindVal_=NULL;
62 if (defaultValue != NULL && *(char *)defaultValue != '\0')
64 fldDef.isDefault_ = true;
65 if (typeBinary == type) {
66 const char *p = (const char *) defaultValue;
67 while (*p != '\0') {
68 if (! isxdigit((int)(*p++)) ) {
69 printError(ErrBadArg, "Invalid hexadecimal value");
70 return (int) ErrBadArg;
74 os::memcpy(fldDef.defaultValueBuf_, defaultValue, DEFAULT_VALUE_BUF_LENGTH);
76 else
78 fldDef.isDefault_ = false;
79 os::memset(fldDef.defaultValueBuf_,0, DEFAULT_VALUE_BUF_LENGTH);
81 fldDef.isNull_ = notNull;
82 fldDef.isAutoIncrement_ = autoIn;
83 //os::memset(fldDef.autoVal_,0, DEFAULT_VALUE_BUF_LENGTH);
85 /*PRABA:COMMENTED
86 switch(type)
88 case typeString :
89 case typeVarchar:
90 case typeBinary :
91 fldDef.length_ = os::align(length);
92 break;
93 default:
94 fldDef.length_ = os::align(AllDataType::size(type));
95 break;
98 if (type == typeBinary) fldDef.length_ = os::align(length);
99 fldDef.offset_ = fldList.getTupleSize();
100 int ret = fldList.append(fldDef);
101 if (0 == ret) fldCount++;
102 return ret;
105 int TableDef::dropField(const char *name)
107 int ret = fldList.remove(name);
108 if (0 == ret) fldCount--;
109 return ret;
112 int TableDef::getFieldCount()
114 return fldCount;
117 size_t TableDef::getTupleSize()
119 size_t length = 0;
120 FieldIterator iter = getFieldIterator();
121 while (iter.hasElement())
123 FieldDef *def = iter.nextElement();
124 if (def->type_ == typeVarchar)
125 length += sizeof (void *);
126 else if (def->type_ == typeString)
127 length += os::align(def->length_);
128 else
129 length = length + def->length_;
131 return length;
134 bool TableDef::isVarcharPresentInSchema(FieldIterator &iter)
136 while (iter.hasElement())
138 FieldDef *fDef = iter.nextElement();
139 if (fDef->type_ == typeVarchar) return true;
141 return false;