src/sql/DdlStatement.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include "Statement.h"
00017 #include <Info.h>
00018 CreateTblStatement::CreateTblStatement()
00019 {
00020     parsedData = NULL; 
00021     dbMgr = NULL; 
00022 }
00023 
00024 CreateTblStatement::~CreateTblStatement()
00025 {
00026     tblDef.reset();
00027 }
00028 DbRetVal CreateTblStatement::execute(int &rowsAffected)
00029 {
00030     DbRetVal rv = OK;
00031     rv = dbMgr->createTable(tblName, tblDef);
00032     if (rv != OK) return rv;
00033     if (parsedData->getFieldNameList().size() > 0)
00034     {
00035         HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
00036         strcpy(idxInfo->tableName, tblName);
00037         ListIterator iter = parsedData->getFieldNameList().getIterator();
00038         FieldName *name = NULL;
00039         while (iter.hasElement())
00040         {
00041             name = (FieldName*)iter.nextElement();
00042             idxInfo->list.append(name->fldName);
00043         }
00044         idxInfo->indType = hashIndex;
00045         idxInfo->isPrimary = true;
00046         idxInfo->isUnique = true;
00047  
00048         char indName[IDENTIFIER_LENGTH];
00049         sprintf(indName, "%s_idx1_Primary", tblName);
00050         rv = dbMgr->createIndex(indName, idxInfo);
00051         delete idxInfo;
00052     }
00053     return rv;
00054 }
00055 
00056 DbRetVal CreateTblStatement::resolve()
00057 {
00058     DbRetVal rv = OK;
00059     strcpy(tblName, parsedData->getTableName());
00060     FieldIterator iter = parsedData->getCreFldList().getIterator();
00061     FieldDef fDef; 
00062 
00063     int i = 0;
00064     FieldName *name = NULL;
00065     ListIterator nIter = parsedData->getFieldNameList().getIterator();
00066     while (iter.hasElement())
00067     {
00068         fDef = iter.nextElement();
00069         nIter.reset();
00070         while (nIter.hasElement())
00071         {
00072             name = (FieldName*)nIter.nextElement();
00073             if (strcmp(name->fldName, fDef.fldName_) == 0) fDef.isNull_ = true;
00074         }
00075 
00076 
00077        //TODO : need a new addField function which can take FieldDef as parameter.
00078        if (!fDef.isDefault_)  {
00079            i = tblDef.addField(fDef.fldName_, fDef.type_, fDef.length_, 
00080                         NULL,fDef.isNull_);
00081        } else {
00082            i = tblDef.addField(fDef.fldName_, fDef.type_, fDef.length_, 
00083                         fDef.defaultValueBuf_,fDef.isNull_);
00084        }
00085        if( 0 != i )
00086        {
00087           printError(ErrUnknown, "Error while adding field");
00088           rv = ErrUnknown;
00089           break;
00090        }
00091     }
00092     return rv;
00093 }
00094 
00096 CreateIdxStatement::CreateIdxStatement()
00097 {
00098     parsedData = NULL; 
00099     dbMgr = NULL; 
00100 }
00101 
00102 CreateIdxStatement::~CreateIdxStatement()
00103 {
00104 
00105 }
00106 
00107 DbRetVal CreateIdxStatement::execute(int &rowsAffected)
00108 {
00109     DbRetVal rv = OK;
00110     if (parsedData->getFieldNameList().size() > 0)
00111     {
00112         HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
00113         strcpy(idxInfo->tableName, parsedData->getTableName());
00114         ListIterator iter = parsedData->getFieldNameList().getIterator();
00115         FieldName *name = NULL;
00116         while (iter.hasElement())
00117         {
00118             name = (FieldName*)iter.nextElement();
00119             idxInfo->list.append(name->fldName);
00120         }
00121         idxInfo->indType = parsedData->getIndexType();
00122         idxInfo->isPrimary = parsedData->getPrimary();
00123         idxInfo->isUnique = parsedData->getUnique();
00124         rv = dbMgr->createIndex(parsedData->getIndexName(), idxInfo);
00125         delete idxInfo;
00126     }
00127     return rv;
00128 }
00129 
00130 DbRetVal DropTblStatement::execute(int &rowsAffected)
00131 {
00132     DbRetVal rv = OK;
00133     rv = dbMgr->dropTable(parsedData->getTableName());
00134     return rv;
00135 }
00136 
00137 DbRetVal DropIdxStatement::execute(int &rowsAffected)
00138 {
00139     DbRetVal rv = OK;
00140     rv = dbMgr->dropIndex(parsedData->getIndexName());
00141     return rv;
00142 }
00143 

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7