1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
15 ***************************************************************************/
17 #include <Statement.h>
20 #include <TableConfig.h>
22 CreateTblStatement::CreateTblStatement()
28 CreateTblStatement::~CreateTblStatement()
32 DbRetVal
CreateTblStatement::execute(int &rowsAffected
)
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();
53 idxInfo
->bucketSize
= bucket
;
54 char indName
[IDENTIFIER_LENGTH
];
55 sprintf(indName
, "%s_idx1_Primary", tblName
);
56 rv
= dbMgr
->createIndex(indName
, idxInfo
);
59 dbMgr
->dropTable(tblName
);
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
);
84 dbMgr
->dropTable(tblName
);
90 if(parsedData
->getForeignKeyList().size() > 0)
92 ForeignKeyInfo
*fkInfo
=NULL
;
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
);
104 dbMgr
->dropTable(tblName
);
112 DbRetVal
CreateTblStatement::checkForDot(char *name
)
115 while ( name
[i
] != '\0')
117 if (name
[i
++] == '.') { return ErrSyntaxError
; }
122 DbRetVal
CreateTblStatement::resolve()
125 strcpy(tblName
, parsedData
->getTableName());
126 rv
= checkForDot(tblName
);
129 printf("Check SQL Syntax: .\n");
132 FieldIterator iter
= parsedData
->getCreFldList().getIterator();
135 FieldName
*name
= NULL
;
136 ListIterator nIter
= parsedData
->getFieldNameList().getIterator();
137 while (iter
.hasElement())
139 FieldDef
*fDef
= iter
.nextElement();
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_
);
149 printf("Check SQL Syntax: .\n");
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_
);
158 i
= tblDef
.addField(fDef
->fldName_
, fDef
->type_
, fDef
->length_
,
159 fDef
->defaultValueBuf_
,fDef
->isNull_
,fDef
->isAutoIncrement_
);
163 printError(ErrUnknown
, "Error while adding field");
170 /////////////////////////////////////
171 DbRetVal
CacheTblStatement::resolve()
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());
199 printError(ErrAlready
, "Table is already cached,unload table then try");
206 DbRetVal
CacheTblStatement::execute(int &rowsAffected
)
210 if( parsedData
->getUnCache())
213 TableConf::config
.getTableMode(parsedData
->getTableName());
214 bool isCached
= TableConf::config
.isTableCached(mode
);
216 printError(ErrNotCached
, "Table is not Cached");
219 TableConf::config
.removeFromCacheTableFile();
221 rv
= cacheLoader
.load(!(parsedData
->getNoSchema()));
223 TableConf::config
.addToCacheTableFile(parsedData
->getDirect());
229 ///////////////////////////////////////
230 CreateIdxStatement::CreateIdxStatement()
236 CreateIdxStatement::~CreateIdxStatement()
241 DbRetVal
CreateIdxStatement::execute(int &rowsAffected
)
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();
260 idxInfo
->bucketSize
= bucket
;
261 rv
= dbMgr
->createIndex(parsedData
->getIndexName(), idxInfo
);
267 // function for not to drop cached table
268 DbRetVal
isTableCached(char *tabName
) // function added by :Jitendra
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';
284 fscanf(fp
,"%d %s %s %s %s %s\n",&mode
,tablename
,fieldname
,condition
,field
,dsnName
);
285 if(strcmp(tablename
,tabName
) ==0){
287 return ErrNoPrivilege
;}
293 DbRetVal
DropTblStatement::execute(int &rowsAffected
)
295 DbRetVal rv
= OK
; // newly added
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
);
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
;
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
;
326 rv
= dbMgr
->dropTable(parsedData
->getTableName());
330 DbRetVal
DropIdxStatement::execute(int &rowsAffected
)
333 rv
= dbMgr
->dropIndex(parsedData
->getIndexName());
337 //================== Compact Table Statement===================
338 DbRetVal
CompactTblStatement::resolve()
341 table
= dbMgr
->openTable(parsedData
->getTableName());
344 printError(ErrNotExists
, "Unable to open the table:Table not exists");
349 DbRetVal
CompactTblStatement::execute(int &rowsAffected
)
352 rv
= table
->compact();
353 dbMgr
->closeTable(table
);
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
;
373 DbRetVal
UserTblStatement::execute(int &rowsAffected
)
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());
381 rv
=(DbRetVal
) usrMgr
->changePassword(parsedData
->getUserName(), parsedData
->getPassWord());