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
= treeIndex
;
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");
152 if (fDef
->type_
== typeVarchar
|| fDef
->type_
== typeString
) {
154 //varchar and char require \0 to be stored at the end
156 /* To check char field and restrict it for the max length 8kb(8000) */
157 if( (fDef
->type_
== typeString
) && (fDef
->length_
> 8000) ){
158 printError(ErrBadRange
,"Char data type length should be less than 8kb(8000).");
161 //TODO : need a new addField function which can take FieldDef as parameter.
162 if (!fDef
->isDefault_
|| fDef
->isDefault_
&& fDef
->defaultValueBuf_
[0] == '\0') {
163 i
= tblDef
.addField(fDef
->fldName_
, fDef
->type_
, fDef
->length_
,
164 NULL
,fDef
->isNull_
,fDef
->isAutoIncrement_
);
166 i
= tblDef
.addField(fDef
->fldName_
, fDef
->type_
, fDef
->length_
,
167 fDef
->defaultValueBuf_
,fDef
->isNull_
,fDef
->isAutoIncrement_
);
171 printError(ErrUnknown
, "Error while adding field");
178 /////////////////////////////////////
179 DbRetVal
CacheTblStatement::resolve()
183 TableConf::config
.init();
184 cacheLoader
.setConnParam(I_USER
, I_PASS
);
185 cacheLoader
.setTable(parsedData
->getTableName());
186 TableConf::config
.setTable(parsedData
->getTableName());
187 if(parsedData
->getHCondFld()){
188 cacheLoader
.setCondition(parsedData
->getHCondition());// new one
189 TableConf::config
.setCondition(parsedData
->getHCondition());
191 if(parsedData
->getVCondFld()) {
192 cacheLoader
.setFieldListVal(parsedData
->getVCondition());
193 TableConf::config
.setFieldListVal(parsedData
->getVCondition());
195 if(parsedData
->getPkFld()){
196 cacheLoader
.setFieldName(parsedData
->getIndexName());
197 TableConf::config
.setFieldName(parsedData
->getIndexName());
199 if(parsedData
->getDSN()){
200 cacheLoader
.setDsnName(parsedData
->getPKTableName());
201 TableConf::config
.setDsnName(parsedData
->getPKTableName());
203 if( !(parsedData
->getUnCache()))
205 rv
= TableConf::config
.isTableCached(parsedData
->getTableName());
207 printError(ErrAlready
, "Table is already cached,unload table then try");
214 DbRetVal
CacheTblStatement::execute(int &rowsAffected
)
218 if( parsedData
->getUnCache())
221 TableConf::config
.getTableMode(parsedData
->getTableName());
222 bool isCached
= TableConf::config
.isTableCached(mode
);
224 printError(ErrNotCached
, "Table is not Cached");
227 TableConf::config
.removeFromCacheTableFile();
229 rv
= cacheLoader
.load(!(parsedData
->getNoSchema()));
231 TableConf::config
.addToCacheTableFile(parsedData
->getDirect());
237 ///////////////////////////////////////
238 CreateIdxStatement::CreateIdxStatement()
244 CreateIdxStatement::~CreateIdxStatement()
249 DbRetVal
CreateIdxStatement::execute(int &rowsAffected
)
252 if (parsedData
->getFieldNameList().size() > 0)
254 HashIndexInitInfo
*idxInfo
= new HashIndexInitInfo();
255 strcpy(idxInfo
->tableName
, parsedData
->getTableName());
256 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
257 FieldName
*name
= NULL
;
258 while (iter
.hasElement())
260 name
= (FieldName
*)iter
.nextElement();
261 idxInfo
->list
.append(name
->fldName
);
263 idxInfo
->indType
= parsedData
->getIndexType();
264 idxInfo
->isPrimary
= parsedData
->getPrimary();
265 idxInfo
->isUnique
= parsedData
->getUnique();
266 int bucket
= parsedData
->getBucketSize();
268 idxInfo
->bucketSize
= bucket
;
269 rv
= dbMgr
->createIndex(parsedData
->getIndexName(), idxInfo
);
272 if (rv
== OK
) dbMgr
->sendSignal(SIGCSQL1
);
276 // function for not to drop cached table
277 DbRetVal
isTableCached(char *tabName
) // function added by :Jitendra
282 if (!Conf::config
.useCache()) return OK
;
283 fp
= fopen(Conf::config
.getTableConfigFile(),"r");
284 if(fp
==NULL
) return OK
;
285 char tablename
[IDENTIFIER_LENGTH
]; tablename
[0] = '\0';
286 char condition
[IDENTIFIER_LENGTH
]; condition
[0]='\0';
287 char fieldname
[IDENTIFIER_LENGTH
]; fieldname
[0]='\0';
288 char field
[IDENTIFIER_LENGTH
]; field
[0]='\0';
289 char dsnName
[IDENTIFIER_LENGTH
]; dsnName
[0]='\0';
293 fscanf(fp
,"%d %s %s %s %s %s\n",&mode
,tablename
,fieldname
,condition
,field
,dsnName
);
294 if(strcmp(tablename
,tabName
) ==0){
296 return ErrNoPrivilege
;}
302 DbRetVal
DropTblStatement::execute(int &rowsAffected
)
304 DbRetVal rv
= OK
; // newly added
306 tab
= parsedData
->getTableName();
308 DatabaseManagerImpl
*dmgr
= (DatabaseManagerImpl
*)dbMgr
;
309 IsolationLevel iso
= dmgr
->txnMgr()->getIsoLevel();
310 rv
= dmgr
->txnMgr()->rollback(dmgr
->lockMgr());
311 rv
= dmgr
->txnMgr()->startTransaction(dmgr
->lockMgr(),iso
);
313 int mode
= TableConf::config
.getTableMode(tab
);
314 // rv = isTableCached(tab);
315 if (mode
!= 0 && mode
< 8) {
316 printf("Cached table '%s' cannot be dropped.\n", tab
);
317 printf("uncache the table by 'cachetable -t %s -u' and drop.\n", tab
);
318 printError(ErrNoPrivilege
, "Cached table '%s' cannot be dropped.", tab
);
319 return ErrNoPrivilege
;
321 else if (mode
== 8) {
322 printf("Replicated table '%s' cannot be dropped.\n", tab
);
323 printf("Unreplicate the table by 'repltable -t %s -u' and drop.\n", tab
);
324 printError(ErrNoPrivilege
, "Replicated table '%s' cannot be dropped.", tab
);
325 return ErrNoPrivilege
;
328 printf("Table %s is cached and replicated. Cannot be dropped.\n", tab
);
329 printf("Uncache the table by 'cachetable -t %s -u'.\n", tab
);
330 printf("Unreplicated the table by 'repltable -t %s -u' and drop.\n", tab
);
331 printError(ErrNoPrivilege
, "Table %s is cached and replicated. Cannot be dropped.", tab
);
332 return ErrNoPrivilege
;
335 rv
= dbMgr
->dropTable(parsedData
->getTableName());
336 if (rv
== OK
) dbMgr
->sendSignal(SIGCSQL1
);
340 DbRetVal
DropIdxStatement::execute(int &rowsAffected
)
343 DatabaseManagerImpl
*dmgr
= (DatabaseManagerImpl
*)dbMgr
;
344 IsolationLevel iso
= dmgr
->txnMgr()->getIsoLevel();
345 rv
= dmgr
->txnMgr()->rollback(dmgr
->lockMgr());
346 rv
= dmgr
->txnMgr()->startTransaction(dmgr
->lockMgr(),iso
);
347 rv
= dmgr
->dropIndex(parsedData
->getIndexName());
348 if (rv
== OK
) dbMgr
->sendSignal(SIGCSQL1
);
352 //================== Truncate Table Statement ===============
355 DbRetVal
TruncateTblStatement::execute(int &rowsAffected
)
358 table
->setCondition(NULL
);
359 IsolationLevel level
= ((DatabaseManagerImpl
*)dbMgr
)->txnMgr()->getIsoLevel();
360 rv
=((DatabaseManagerImpl
*)dbMgr
)->txnMgr()->commit(((DatabaseManagerImpl
*)dbMgr
)->lockMgr());
361 if (rv
!= OK
) return rv
;
362 rv
=((DatabaseManagerImpl
*)dbMgr
)->txnMgr()->startTransaction(((DatabaseManagerImpl
*)dbMgr
)->lockMgr(),level
);
363 if (rv
!= OK
) return rv
;
365 rv
= table
->execute();
366 if (rv
!= OK
) return rv
;
368 table
->setLoading(true);
372 tuple
= (char*)table
->fetchNoBind(rv
);
374 if (tuple
== NULL
) {break;}
375 rv
= table
->deleteTuple();
382 DbRetVal
TruncateTblStatement::resolve()
385 table
= dbMgr
->openTable(parsedData
->getTableName());
388 printError(ErrNotExists
, "Unable to open the table:Table not exists");
398 //================== Compact Table Statement===================
399 DbRetVal
CompactTblStatement::resolve()
402 table
= dbMgr
->openTable(parsedData
->getTableName());
405 printError(ErrNotExists
, "Unable to open the table:Table not exists");
410 DbRetVal
CompactTblStatement::execute(int &rowsAffected
)
413 rv
= table
->compact();
414 dbMgr
->closeTable(table
);
417 DbRetVal
UserTblStatement::resolve()
419 uType
= parsedData
->getUserType();
420 if( uType
== CREATEUSER
|| uType
== DROPUSER
|| (uType
== ALTERUSER
&& strcmp(userName
,parsedData
->getUserName())!=0))
422 if(strcmp(userName
,"root")!=0)
424 printError(ErrNoPrivilege
,"Permission Denied. Login as root");
425 return ErrNoPrivilege
;
426 }else if(uType
== DROPUSER
&& strcmp(parsedData
->getUserName(),"root")==0)
428 printError(ErrNoPrivilege
,"Permission Denied. root user cannot be deleted");
429 return ErrNoPrivilege
;
434 DbRetVal
UserTblStatement::execute(int &rowsAffected
)
437 if( uType
== CREATEUSER
){
438 rv
=(DbRetVal
) usrMgr
->createUser(parsedData
->getUserName(), parsedData
->getPassWord());
439 }else if(uType
== DROPUSER
){
440 rv
=(DbRetVal
) usrMgr
->deleteUser(parsedData
->getUserName());
442 rv
=(DbRetVal
) usrMgr
->changePassword(parsedData
->getUserName(), parsedData
->getPassWord());