Merge branch 'master' into 2.3beta-2.5beta
[csql/przemoc.git] / src / storage / TableDef.cxx
blob918a6895cafb1cebafed282c30ea7e375a29be9d
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)>64)
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;
49 FieldDef fldDef;
50 strcpy(fldDef.fldName_, name);
51 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
52 fldDef.type_ = type;
53 fldDef.length_ = os::align(length);
54 fldDef.bindVal_=NULL;
55 if (defaultValue != NULL)
57 fldDef.isDefault_ = true;
58 if (typeBinary == type) {
59 const char *p = (const char *) defaultValue;
60 while (*p != '\0') {
61 if (! isxdigit((int)(*p++)) ) {
62 printError(ErrBadArg, "Invalid hexadecimal value");
63 return (int) ErrBadArg;
67 os::memcpy(fldDef.defaultValueBuf_, defaultValue, DEFAULT_VALUE_BUF_LENGTH);
69 else
71 fldDef.isDefault_ = false;
72 os::memset(fldDef.defaultValueBuf_,0, DEFAULT_VALUE_BUF_LENGTH);
74 fldDef.isNull_ = notNull;
75 fldDef.isAutoIncrement_= autoIn;
76 switch(type)
78 case typeString :
79 case typeBinary:
80 fldDef.length_ = os::align(length);
81 break;
82 default:
83 fldDef.length_ = os::align(AllDataType::size(type));
84 break;
86 fldDef.offset_ = fldList.getTupleSize();
87 int ret = fldList.append(fldDef);
88 if (0 == ret) fldCount++;
89 return ret;
92 int TableDef::dropField(const char *name)
94 int ret = fldList.remove(name);
95 if (0 == ret) fldCount--;
96 return ret;
99 int TableDef::getFieldCount()
101 return fldCount;
104 size_t TableDef::getTupleSize()
106 size_t length = 0;
107 FieldIterator iter = getFieldIterator();
108 while (iter.hasElement())
110 FieldDef *def = iter.nextElement();
111 length = length + os::align(def->length_);
113 return length;