Removing dependency for Cache module in MMDB build
[csql.git] / src / sql / DdlStatement.cxx
blobba8907627efc18831dde1ab44065aef7f3cc54aa
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 <os.h>
17 #include <Statement.h>
18 #include <Info.h>
19 #ifndef MMDB
20 #include <TableConfig.h>
21 #endif
22 CreateTblStatement::CreateTblStatement()
24 parsedData = NULL;
25 dbMgr = NULL;
28 CreateTblStatement::~CreateTblStatement()
30 tblDef.reset();
32 DbRetVal CreateTblStatement::execute(int &rowsAffected)
34 DbRetVal rv = OK;
35 rv = dbMgr->createTable(tblName, tblDef);
36 if (rv != OK) return rv;
37 if (parsedData->getFieldNameList().size() > 0)
39 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
40 strcpy(idxInfo->tableName, tblName);
41 ListIterator iter = parsedData->getFieldNameList().getIterator();
42 FieldName *name = NULL;
43 while (iter.hasElement())
45 name = (FieldName*)iter.nextElement();
46 idxInfo->list.append(name->fldName);
48 idxInfo->indType = hashIndex;
49 idxInfo->isPrimary = true;
50 idxInfo->isUnique = true;
51 int bucket = parsedData->getBucketSize();
52 if(bucket!=0)
53 idxInfo->bucketSize = bucket;
54 char indName[IDENTIFIER_LENGTH];
55 sprintf(indName, "%s_idx1_Primary", tblName);
56 rv = dbMgr->createIndex(indName, idxInfo);
57 if (rv != OK)
59 dbMgr->dropTable(tblName);
60 delete idxInfo;
61 return rv;
63 delete idxInfo;
65 if(parsedData->getSecondaryIndexFieldList().size() > 0)
67 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
68 strcpy(idxInfo->tableName, tblName);
69 ListIterator iter = parsedData->getSecondaryIndexFieldList().getIterator();
70 FieldInfo *name = NULL;
71 while (iter.hasElement())
73 name = (FieldInfo*)iter.nextElement();
74 idxInfo->list.append(name->fldName);
76 idxInfo->indType = hashIndex;
77 idxInfo->isPrimary = true;
78 idxInfo->isUnique = true;
79 char indName[IDENTIFIER_LENGTH];
80 sprintf(indName, "%s_idx_Auto_increment", tblName);
81 rv = dbMgr->createIndex(indName, idxInfo);
82 if (rv != OK)
84 dbMgr->dropTable(tblName);
85 delete idxInfo;
86 return rv;
88 delete idxInfo;
90 if(parsedData->getForeignKeyList().size() > 0)
92 ForeignKeyInfo *fkInfo=NULL;
93 int i=0;
94 ListIterator iter = parsedData->getForeignKeyList().getIterator();
95 while (iter.hasElement())
97 fkInfo = (ForeignKeyInfo *)iter.nextElement();
98 strcpy(fkInfo->fkTableName,tblName);
99 char fkName[IDENTIFIER_LENGTH];
100 sprintf(fkName, "%s_FKEY_%d", tblName,++i);
101 rv = dbMgr->createForeignKey(fkName,fkInfo);
102 if (rv != OK)
104 dbMgr->dropTable(tblName);
105 return rv;
110 return rv;
112 DbRetVal CreateTblStatement::checkForDot(char *name)
114 int i=0;
115 while ( name[i] != '\0')
117 if (name[i++] == '.') { return ErrSyntaxError; }
119 return OK;
122 DbRetVal CreateTblStatement::resolve()
124 DbRetVal rv = OK;
125 strcpy(tblName, parsedData->getTableName());
126 rv = checkForDot(tblName);
127 if ( rv!=OK )
129 printf("Check SQL Syntax: .\n");
130 return rv;
132 FieldIterator iter = parsedData->getCreFldList().getIterator();
134 int i = 0;
135 FieldName *name = NULL;
136 ListIterator nIter = parsedData->getFieldNameList().getIterator();
137 while (iter.hasElement())
139 FieldDef *fDef = iter.nextElement();
140 nIter.reset();
141 while (nIter.hasElement())
143 name = (FieldName*)nIter.nextElement();
144 if (strcmp(name->fldName, fDef->fldName_) == 0) fDef->isNull_ = true;
146 rv = checkForDot(fDef->fldName_);
147 if ( rv!=OK )
149 printf("Check SQL Syntax: .\n");
150 return rv;
153 //TODO : need a new addField function which can take FieldDef as parameter.
154 if (!fDef->isDefault_ || fDef->isDefault_ && fDef->defaultValueBuf_[0] == '\0') {
155 i = tblDef.addField(fDef->fldName_, fDef->type_, fDef->length_,
156 NULL,fDef->isNull_,fDef->isAutoIncrement_);
157 } else {
158 i = tblDef.addField(fDef->fldName_, fDef->type_, fDef->length_,
159 fDef->defaultValueBuf_,fDef->isNull_,fDef->isAutoIncrement_);
161 if( 0 != i )
163 printError(ErrUnknown, "Error while adding field");
164 rv = ErrUnknown;
165 break;
168 return rv;
170 /////////////////////////////////////
171 DbRetVal CacheTblStatement::resolve()
173 DbRetVal rv = OK;
174 #ifndef MMDB
175 TableConf::config.init();
176 cacheLoader.setConnParam(I_USER, I_PASS);
177 cacheLoader.setTable(parsedData->getTableName());
178 TableConf::config.setTable(parsedData->getTableName());
179 if(parsedData->getHCondFld()){
180 cacheLoader.setCondition(parsedData->getHCondition());// new one
181 TableConf::config.setCondition(parsedData->getHCondition());
183 if(parsedData->getVCondFld()) {
184 cacheLoader.setFieldListVal(parsedData->getVCondition());
185 TableConf::config.setFieldListVal(parsedData->getVCondition());
187 if(parsedData->getPkFld()){
188 cacheLoader.setFieldName(parsedData->getIndexName());
189 TableConf::config.setFieldName(parsedData->getIndexName());
191 if(parsedData->getDSN()){
192 cacheLoader.setDsnName(parsedData->getPKTableName());
193 TableConf::config.setDsnName(parsedData->getPKTableName());
195 if( !(parsedData->getUnCache()))
197 rv = TableConf::config.isTableCached(parsedData->getTableName());
198 if(rv == OK){
199 printError(ErrAlready, "Table is already cached,unload table then try");
200 return ErrAlready;
203 #endif
204 return OK;
206 DbRetVal CacheTblStatement::execute(int &rowsAffected)
208 DbRetVal rv = OK;
209 #ifndef MMDB
210 if( parsedData->getUnCache())
212 unsigned int mode =
213 TableConf::config.getTableMode(parsedData->getTableName());
214 bool isCached = TableConf::config.isTableCached(mode);
215 if (!isCached) {
216 printError(ErrNotCached, "Table is not Cached");
217 return ErrNotCached;
219 TableConf::config.removeFromCacheTableFile();
220 } else {
221 rv = cacheLoader.load(!(parsedData->getNoSchema()));
222 if(rv == OK){
223 TableConf::config.addToCacheTableFile(parsedData->getDirect());
224 } else return rv;
226 #endif
227 return OK;
229 ///////////////////////////////////////
230 CreateIdxStatement::CreateIdxStatement()
232 parsedData = NULL;
233 dbMgr = NULL;
236 CreateIdxStatement::~CreateIdxStatement()
241 DbRetVal CreateIdxStatement::execute(int &rowsAffected)
243 DbRetVal rv = OK;
244 if (parsedData->getFieldNameList().size() > 0)
246 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
247 strcpy(idxInfo->tableName, parsedData->getTableName());
248 ListIterator iter = parsedData->getFieldNameList().getIterator();
249 FieldName *name = NULL;
250 while (iter.hasElement())
252 name = (FieldName*)iter.nextElement();
253 idxInfo->list.append(name->fldName);
255 idxInfo->indType = parsedData->getIndexType();
256 idxInfo->isPrimary = parsedData->getPrimary();
257 idxInfo->isUnique = parsedData->getUnique();
258 int bucket = parsedData->getBucketSize();
259 if(bucket!=0)
260 idxInfo->bucketSize = bucket;
261 rv = dbMgr->createIndex(parsedData->getIndexName(), idxInfo);
262 delete idxInfo;
264 return rv;
267 // function for not to drop cached table
268 DbRetVal isTableCached(char *tabName) // function added by :Jitendra
271 DbRetVal rv =OK;
272 FILE *fp;
273 if (!Conf::config.useCache()) return OK;
274 fp = fopen(Conf::config.getTableConfigFile(),"r");
275 if(fp==NULL) return OK;
276 char tablename[IDENTIFIER_LENGTH]; tablename[0] = '\0';
277 char condition[IDENTIFIER_LENGTH]; condition[0]='\0';
278 char fieldname[IDENTIFIER_LENGTH]; fieldname[0]='\0';
279 char field[IDENTIFIER_LENGTH]; field[0]='\0';
280 char dsnName[IDENTIFIER_LENGTH]; dsnName[0]='\0';
281 int mode;
282 while(!feof(fp))
284 fscanf(fp,"%d %s %s %s %s %s\n",&mode,tablename,fieldname,condition,field,dsnName);
285 if(strcmp(tablename,tabName) ==0){
286 fclose(fp);
287 return ErrNoPrivilege;}
289 fclose(fp);
290 return rv;
293 DbRetVal DropTblStatement::execute(int &rowsAffected)
295 DbRetVal rv = OK; // newly added
296 char *tab;
297 tab = parsedData->getTableName();
299 DatabaseManagerImpl *dmgr = (DatabaseManagerImpl *)dbMgr;
300 IsolationLevel iso = dmgr->txnMgr()->getIsoLevel();
301 rv = dmgr->txnMgr()->rollback(dmgr->lockMgr());
302 rv = dmgr->txnMgr()->startTransaction(dmgr->lockMgr(),iso);
303 #ifndef MMDB
304 int mode = TableConf::config.getTableMode(tab);
305 // rv = isTableCached(tab);
306 if (mode != 0 && mode < 8) {
307 printf("Cached table '%s' cannot be dropped.\n", tab);
308 printf("uncache the table by 'cachetable -t %s -u' and drop.\n", tab);
309 printError(ErrNoPrivilege, "Cached table '%s' cannot be dropped.", tab);
310 return ErrNoPrivilege;
312 else if (mode == 8) {
313 printf("Replicated table '%s' cannot be dropped.\n", tab);
314 printf("Unreplicate the table by 'repltable -t %s -u' and drop.\n", tab);
315 printError(ErrNoPrivilege, "Replicated table '%s' cannot be dropped.", tab);
316 return ErrNoPrivilege;
318 else if (mode > 8) {
319 printf("Table %s is cached and replicated. Cannot be dropped.\n", tab);
320 printf("Uncache the table by 'cachetable -t %s -u'.\n", tab);
321 printf("Unreplicated the table by 'repltable -t %s -u' and drop.\n", tab);
322 printError(ErrNoPrivilege, "Table %s is cached and replicated. Cannot be dropped.", tab);
323 return ErrNoPrivilege;
325 #endif
326 rv = dbMgr->dropTable(parsedData->getTableName());
327 return rv;
330 DbRetVal DropIdxStatement::execute(int &rowsAffected)
332 DbRetVal rv = OK;
333 rv = dbMgr->dropIndex(parsedData->getIndexName());
334 return rv;
337 //================== Compact Table Statement===================
338 DbRetVal CompactTblStatement::resolve()
340 DbRetVal rv = OK;
341 table = dbMgr->openTable(parsedData->getTableName());
342 if (table == NULL)
344 printError(ErrNotExists, "Unable to open the table:Table not exists");
345 return ErrNotExists;
347 return OK;
349 DbRetVal CompactTblStatement::execute(int &rowsAffected)
351 DbRetVal rv = OK;
352 rv = table->compact();
353 dbMgr->closeTable(table);
354 return rv;
356 DbRetVal UserTblStatement::resolve()
358 uType = parsedData->getUserType();
359 if( uType == CREATEUSER || uType == DROPUSER || (uType == ALTERUSER && strcmp(userName,parsedData->getUserName())!=0))
361 if(strcmp(userName,"root")!=0)
363 printError(ErrNoPrivilege,"Permission Denied. Login as root");
364 return ErrNoPrivilege;
365 }else if(uType == DROPUSER && strcmp(parsedData->getUserName(),"root")==0)
367 printError(ErrNoPrivilege,"Permission Denied. root user cannot be deleted");
368 return ErrNoPrivilege;
371 return OK;
373 DbRetVal UserTblStatement::execute(int &rowsAffected)
375 DbRetVal rv = OK;
376 if( uType == CREATEUSER){
377 rv =(DbRetVal) usrMgr->createUser(parsedData->getUserName(), parsedData->getPassWord());
378 }else if(uType == DROPUSER){
379 rv =(DbRetVal) usrMgr->deleteUser(parsedData->getUserName());
380 }else{
381 rv =(DbRetVal) usrMgr->changePassword(parsedData->getUserName(), parsedData->getPassWord());
383 return rv;