*** empty log message ***
[csql.git] / src / storage / TableDef.cxx
blobb2f3e5fc8e209a4d435ad927a7fadc59b67fb071
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, bool autoIn)
33 if (name == NULL) return (int)ErrBadArg;
34 if(strlen(name)>IDENTIFIER_LENGTH)
36 printError(ErrBadRange,"Field name should not exceed 64 character");
37 return (int)ErrBadRange;
39 // The following code checks for duplicates
40 FieldIterator iter = getFieldIterator();
41 while (iter.hasElement())
43 FieldDef *def = iter.nextElement();
44 if (! strcmp(def->fldName_, name)) {
45 printError(ErrAlready, "Field %s already Exists", name);
46 return (int) ErrAlready;
50 if (!Util::isIdentifier((char*)name)) {
51 printError(ErrBadArg, "fieldname contains invalid characters");
52 return (int) ErrBadArg;
54 FieldDef fldDef;
55 strcpy(fldDef.fldName_, name);
56 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
57 fldDef.type_ = type;
58 fldDef.length_ = AllDataType::size(type, length);
59 fldDef.bindVal_=NULL;
60 if (defaultValue != NULL && *(char *)defaultValue != '\0')
62 fldDef.isDefault_ = true;
63 if (typeBinary == type) {
64 const char *p = (const char *) defaultValue;
65 while (*p != '\0') {
66 if (! isxdigit((int)(*p++)) ) {
67 printError(ErrBadArg, "Invalid hexadecimal value");
68 return (int) ErrBadArg;
72 os::memcpy(fldDef.defaultValueBuf_, defaultValue, DEFAULT_VALUE_BUF_LENGTH);
74 else
76 fldDef.isDefault_ = false;
77 os::memset(fldDef.defaultValueBuf_,0, DEFAULT_VALUE_BUF_LENGTH);
79 fldDef.isNull_ = notNull;
80 fldDef.isAutoIncrement_ = autoIn;
81 //os::memset(fldDef.autoVal_,0, DEFAULT_VALUE_BUF_LENGTH);
82 switch(type)
84 case typeString :
85 case typeVarchar:
86 case typeBinary :
87 fldDef.length_ = os::align(length);
88 break;
89 default:
90 fldDef.length_ = os::align(AllDataType::size(type));
91 break;
93 fldDef.offset_ = fldList.getTupleSize();
94 int ret = fldList.append(fldDef);
95 if (0 == ret) fldCount++;
96 return ret;
99 int TableDef::dropField(const char *name)
101 int ret = fldList.remove(name);
102 if (0 == ret) fldCount--;
103 return ret;
106 int TableDef::getFieldCount()
108 return fldCount;
111 size_t TableDef::getTupleSize()
113 size_t length = 0;
114 FieldIterator iter = getFieldIterator();
115 while (iter.hasElement())
117 FieldDef *def = iter.nextElement();
118 if (def->type_ == typeVarchar) length += sizeof (void *);
119 else length = length + def->length_;
121 return length;
124 bool TableDef::isVarcharPresentInSchema(FieldIterator &iter)
126 while (iter.hasElement())
128 FieldDef *fDef = iter.nextElement();
129 if (fDef->type_ == typeVarchar) return true;
131 return false;