#include "catalog.h"
#include "stdio.h"
//
// Destroys a relation. It performs the following steps:
//
// 	removes the catalog entry for the relation
// 	destroys the heap file containing the tuples in the relation
//
// Returns:
// 	OK on success
// 	error code otherwise
//

const Status RelCatalog::destroyRel(const string & relation)
{
  Status status;
 
  if (relation.empty() || 
      relation == string(RELCATNAME) || 
      relation == string(ATTRCATNAME))
  return BADCATPARM;
 
  RelDesc rd;
  cout<<"Step0"<<endl;
  status = relCat->getInfo(relation, rd);
  cout<<"Status is: "<<status;
  if(status != OK) return status;
  cout<<"Step1"<<endl;
  status = attrCat->dropRelation(relation); 
  if(status != OK) return status;  
  cout<<"Step2"<<endl;
  status = relCat->removeInfo(relation);
  if(status != OK) return status;
  cout<<"Step3"<<endl;
  status = destroyHeapFile(relation);
  if(status != OK) return status;
  return OK;
}


//
// Drops a relation. It performs the following steps:
//
// 	removes the catalog entries for the relation
//
// Returns:
// 	OK on success
// 	error code otherwise
//

const Status AttrCatalog::dropRelation(const string & relation)
{
  Status status;
  AttrDesc *attrs;
  int attrCnt, i;

  if (relation.empty()) return BADCATPARM;
  status = attrCat->getRelInfo(relation, attrCnt, attrs);
  if(status != OK) return status;
  for ( i = 0;i<attrCnt; i++){
	status = attrCat->removeInfo(relation, attrs[i].attrName);
	if(status != OK) return status;
  }  
  delete attrs;
  return OK;
}


