⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select.c

📁 linux 下用c++ 开发的一个小型数据库系统
💻 C
字号:
#include "catalog.h"#include "query.h"// forward declarationconst Status ScanSelect(const string & result, 			const int projCnt, 			const AttrDesc projNames[],			const AttrDesc *attrDesc, 			const Operator op, 			const char *filter,			const int reclen);/* * Selects records from the specified relation. * * Returns: * 	OK on success * 	an error code otherwise */const Status QU_Select(const string & result, 		       const int projCnt, 		       const attrInfo projNames[],		       const attrInfo *attr, 		       const Operator op, 		       const char *attrValue){   // Qu_Select sets up things and then calls ScanSelect to do the actual work   // cout << "Doing QU_Select " << endl;   InsertFileScan	*ifs = NULL ;   HeapFileScan		*hfs = NULL ;   AttrDesc		ads ;   RelDesc		rds ;   AttrDesc		*attrs ;   Record		rec ;   RID			rid, rid1 ;   string 		astr, bstr ;   int			intval ;   float		flval ;   char			*mydata ;   int			i, n, t, len, attrCnt ;   Status		status ;      if ((status = relCat->getInfo( result.c_str(), rds)) != OK) return status ;   bstr.append(projNames[0].relName) ;   if ((status = relCat->getInfo( bstr.c_str(), rds)) != OK) return status ;      len = 0 ;      astr.append("projName") ;   for( i = 0; i < projCnt; i++){     astr.erase(1) ;     n = strlen(projNames[i].attrName) ;     astr.replace( 0, n, projNames[i].attrName) ;     if((status = attrCat->getInfo( bstr, astr, ads)) != OK)       return status ;     len = len + ads.attrLen ;   }      if (attr == NULL){     ifs =new InsertFileScan( result, status) ;       if(status != OK) return status ;     hfs =new HeapFileScan( bstr, status) ;       if(status != OK) return status ;     status =attrCat->getRelInfo( bstr, attrCnt, attrs) ;       if (status != OK) return status ;     hfs->startScan( 0, 0, (Datatype)attrs[0].attrType, NULL, EQ ) ;     mydata = new char[len] ;     char *tmpattr = NULL ;     int tempi, tempf ;     while(hfs->scanNext(rid) != FILEEOF){       hfs->getRecord(rec) ;       t = 0 ;       for( i = 0; i < len; i++) mydata[i] = 0 ;         for (i = 0; i < attrCnt; i++){           tmpattr = (char *)rec.data + attrs[i].attrOffset;           switch(attrs[i].attrType) {             case INTEGER:               memcpy(&tempi, tmpattr, sizeof(int));               break;             case FLOAT:               memcpy(&tempf, tmpattr, sizeof(float));               break;           }           for (n = 0; n < projCnt; n++){             if (strcmp(attrs[i].attrName, projNames[n].attrName) == 0){               if (attrs[i].attrType == INTEGER){                 memcpy( mydata + t, &tempi, sizeof(int)) ;                 t = t + sizeof(int) ;               }               if (attrs[i].attrType == FLOAT){                 memcpy( mydata + t, &tempf, sizeof(float)) ;		 t = t + sizeof(float) ;               }               if (attrs[i].attrType == STRING){                 memcpy( mydata + t, tmpattr, attrs[i].attrLen) ;                 t = t + attrs[i].attrLen ;               }             }           }         }      rec.data = mydata ;      rec.length = len ;      ifs->insertRecord( rec, rid1) ;    }     delete [] attrs ;     delete [] mydata ;     delete hfs ;     delete ifs ;     return OK ;  }      cout<<"where"<<endl ;   astr.append("attr") ;   astr.erase(1) ;   n = strlen(attr[0].attrName) ;   astr.replace( 0, n, attr[0].attrName) ;      if ((status = attrCat->getInfo( bstr, astr, ads)) != OK) return status ;      ifs =new InsertFileScan( result, status) ;   if(status != OK) return status ;      hfs =new HeapFileScan( bstr, status) ;   if(status != OK) return status ;      status =attrCat->getRelInfo( bstr, attrCnt, attrs) ;   if (status != OK) return status ;    if (ads.attrType == INTEGER){      intval = atoi(attrValue) ;      hfs->startScan( ads.attrOffset, ads.attrLen,                      (Datatype)ads.attrType, (char *)&intval, op) ;    }    if (ads.attrType == FLOAT){      flval =(float)atof(attrValue) ;      hfs->startScan( ads.attrOffset, ads.attrLen,                      (Datatype)ads.attrType, (char *)&flval, op) ;    }    if (ads.attrType == STRING){       hfs->startScan( ads.attrOffset, ads.attrLen,                       (Datatype)ads.attrType, attrValue, op) ;    }      mydata = new char[len] ;   while(hfs->scanNext(rid) != FILEEOF){     hfs->getRecord(rec) ;     t = 0 ;     for( i = 0; i < len; i++) mydata[i] = 0 ;      for (i = 0; i < attrCnt; i++){         for (n = 0; n < projCnt; n++){          if (strcmp(attrs[i].attrName, projNames[n].attrName) == 0){            memcpy( mydata + t,(char *)rec.data + attrs[i].attrOffset,                    attrs[i].attrLen) ;            t = t + attrs[i].attrLen ;           }        }      }           rec.data = mydata ;      rec.length = len ;      ifs->insertRecord( rec, rid1) ;   }            delete [] attrs ;   delete [] mydata ;   delete hfs ;   delete ifs ;   return OK ; }const Status ScanSelect(const string & result, 			const int projCnt, 			const AttrDesc projNames[],			const AttrDesc *attrDesc, 			const Operator op, 			const char *filter,			const int reclen){   // cout << "Doing HeapFileScan Selection using ScanSelect()" << endl;     return OK ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -