00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include<Table.h>
00017 #include<Index.h>
00018 #include<CatalogTables.h>
00019 #include<Lock.h>
00020
00021 TableDef::~TableDef()
00022 {
00023 reset();
00024 }
00025 void TableDef::reset()
00026 {
00027 fldList.removeAll();
00028 fldCount = 0;
00029 }
00030 int TableDef::addField(const char *name, DataType type, size_t length,
00031 const void *defaultValue, bool notNull)
00032 {
00033 if (name == NULL) return (int)ErrBadArg;
00034
00035 FieldIterator iter = getFieldIterator();
00036 while (iter.hasElement())
00037 {
00038 FieldDef def = iter.nextElement();
00039 if (! strcmp(def.fldName_, name)) {
00040 printError(ErrAlready, "Field %s already Exists", name);
00041 return (int) ErrAlready;
00042 }
00043 }
00044 FieldDef fldDef;
00045 strcpy(fldDef.fldName_, name);
00046 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
00047 fldDef.type_ = type;
00048 fldDef.length_ = length;
00049 if (defaultValue != NULL)
00050 {
00051 fldDef.isDefault_ = true;
00052 os::memcpy(fldDef.defaultValueBuf_, defaultValue,
00053 DEFAULT_VALUE_BUF_LENGTH);
00054 }
00055 else
00056 {
00057 fldDef.isDefault_ = false;
00058 os::memset(fldDef.defaultValueBuf_,0, DEFAULT_VALUE_BUF_LENGTH);
00059 }
00060 fldDef.isNull_ = notNull;
00061 switch(type)
00062 {
00063 case typeString :
00064 case typeBinary:
00065 fldDef.length_ = length;
00066 break;
00067 default:
00068 fldDef.length_ = AllDataType::size(type);
00069 break;
00070 }
00071 int ret = fldList.append(fldDef);
00072 if (0 == ret) fldCount++;
00073 return ret;
00074 }
00075
00076 int TableDef::dropField(const char *name)
00077 {
00078 int ret = fldList.remove(name);
00079 if (0 == ret) fldCount--;
00080 return ret;
00081 }
00082
00083 int TableDef::getFieldCount()
00084 {
00085 return fldCount;
00086 }
00087
00088 size_t TableDef::getTupleSize()
00089 {
00090 size_t length = 0;
00091 FieldIterator iter = getFieldIterator();
00092 while (iter.hasElement())
00093 {
00094 FieldDef def = iter.nextElement();
00095 length = length + os::align(def.length_);
00096 }
00097 return length;
00098 }