unique support for tree index
[csql.git] / src / sql / DdlStatement.cxx
blob246bfc65ff1661b8ad90d73948435005dfca6353
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 "Statement.h"
17 #include <Info.h>
18 CreateTblStatement::CreateTblStatement()
20 parsedData = NULL;
21 dbMgr = NULL;
24 CreateTblStatement::~CreateTblStatement()
26 tblDef.reset();
28 DbRetVal CreateTblStatement::execute(int &rowsAffected)
30 DbRetVal rv = OK;
31 rv = dbMgr->createTable(tblName, tblDef);
32 if (rv != OK) return rv;
33 if (parsedData->getFieldNameList().size() > 0)
35 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
36 strcpy(idxInfo->tableName, tblName);
37 ListIterator iter = parsedData->getFieldNameList().getIterator();
38 FieldName *name = NULL;
39 while (iter.hasElement())
41 name = (FieldName*)iter.nextElement();
42 idxInfo->list.append(name->fldName);
44 idxInfo->indType = hashIndex;
45 idxInfo->isPrimary = true;
46 idxInfo->isUnique = true;
48 char indName[IDENTIFIER_LENGTH];
49 sprintf(indName, "%s_idx1_Primary", tblName);
50 rv = dbMgr->createIndex(indName, idxInfo);
51 delete idxInfo;
53 return rv;
56 DbRetVal CreateTblStatement::resolve()
58 DbRetVal rv = OK;
59 strcpy(tblName, parsedData->getTableName());
60 FieldIterator iter = parsedData->getCreFldList().getIterator();
61 FieldDef fDef;
63 int i = 0;
64 FieldName *name = NULL;
65 ListIterator nIter = parsedData->getFieldNameList().getIterator();
66 while (iter.hasElement())
68 fDef = iter.nextElement();
69 nIter.reset();
70 while (nIter.hasElement())
72 name = (FieldName*)nIter.nextElement();
73 if (strcmp(name->fldName, fDef.fldName_) == 0) fDef.isNull_ = true;
77 //TODO : need a new addField function which can take FieldDef as parameter.
78 if (!fDef.isDefault_) {
79 i = tblDef.addField(fDef.fldName_, fDef.type_, fDef.length_,
80 NULL,fDef.isNull_);
81 } else {
82 i = tblDef.addField(fDef.fldName_, fDef.type_, fDef.length_,
83 fDef.defaultValueBuf_,fDef.isNull_);
85 if( 0 != i )
87 printError(ErrUnknown, "Error while adding field");
88 rv = ErrUnknown;
89 break;
92 return rv;
95 ///////////////////////////////////////
96 CreateIdxStatement::CreateIdxStatement()
98 parsedData = NULL;
99 dbMgr = NULL;
102 CreateIdxStatement::~CreateIdxStatement()
107 DbRetVal CreateIdxStatement::execute(int &rowsAffected)
109 DbRetVal rv = OK;
110 if (parsedData->getFieldNameList().size() > 0)
112 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
113 strcpy(idxInfo->tableName, parsedData->getTableName());
114 ListIterator iter = parsedData->getFieldNameList().getIterator();
115 FieldName *name = NULL;
116 while (iter.hasElement())
118 name = (FieldName*)iter.nextElement();
119 idxInfo->list.append(name->fldName);
121 idxInfo->indType = parsedData->getIndexType();
122 idxInfo->isPrimary = parsedData->getPrimary();
123 idxInfo->isUnique = parsedData->getUnique();
124 rv = dbMgr->createIndex(parsedData->getIndexName(), idxInfo);
125 delete idxInfo;
127 return rv;
130 // function for not to drop cached table
131 DbRetVal isTableCached(char *tabName) // function added by :Jitendra
134 DbRetVal rv =OK;
135 FILE *fp;
136 if (!Conf::config.useCache()) return OK;
137 fp = fopen(Conf::config.getTableConfigFile(),"r");
138 if(fp==NULL) return OK;
139 char tablename[IDENTIFIER_LENGTH];
140 tablename[0] = '\0';
141 char condition[IDENTIFIER_LENGTH];
142 condition[0]='\0';
143 char fieldname[IDENTIFIER_LENGTH];
144 fieldname[0]='\0';
145 char field[IDENTIFIER_LENGTH]; field[0]='\0';
146 int mode;
147 while(!feof(fp))
149 fscanf(fp,"%d:%s %s %s %s\n",&mode,tablename,fieldname,condition,field);
150 if(strcmp(tablename,tabName) ==0){
151 fclose(fp);
152 return ErrNoPrivilege;}
154 fclose(fp);
155 return rv;
158 DbRetVal DropTblStatement::execute(int &rowsAffected)
160 DbRetVal rv = OK; // newly added
161 char *tab;
162 tab = parsedData->getTableName();
164 rv = isTableCached(tab);
165 if(rv !=OK)
167 printf("cached table can't be dropped,Table can be unloaded by \"cachetable -t <tableName> -u\"\n");
168 return ErrNoPrivilege;
169 } // upto this
170 rv = dbMgr->dropTable(parsedData->getTableName());
171 return rv;
174 DbRetVal DropIdxStatement::execute(int &rowsAffected)
176 DbRetVal rv = OK;
177 rv = dbMgr->dropIndex(parsedData->getIndexName());
178 return rv;