📄 update.c
字号:
#include "catalog.h"#include "query.h"/* * Deletes records from a specified relation. * * Returns: * OK on success * an error code otherwise */const Status QU_Update(const string & relation, const int attrCnt, const attrInfo attrList[], const string & attrName, const Operator op, const Datatype type, const char *attrValue){ Record rec ; int intval ; float flval ; HeapFileScan *hfs ; AttrDesc ads ; RelDesc rds ; string astr ; int n, i, len, recNum ; char *mydata ; AttrDesc *attrs ; int myattrCnt ; Status status ; RID rid ; if (relation.empty()) return BADCATPARM; status = relCat->getInfo( relation.c_str(), rds) ; if (status != OK) return status ; astr.append("begin") ; for( i = 0; i < attrCnt; i++){ astr.erase(1) ; n = strlen(attrList[i].attrName) ; astr.replace( 0, n, attrList[i].attrName) ; if((status = attrCat->getInfo( relation, astr, ads)) != OK) return status ; } status = attrCat->getRelInfo( relation, myattrCnt, attrs) ; if (status != OK) return status ; if ((status = attrCat->getInfo( relation, attrName, ads)) != OK){ delete [] attrs ; return status ; } len = 0 ; for (i = 0; i < myattrCnt; i++) len = len + attrs[i].attrLen ; mydata = new char[len] ; hfs = new HeapFileScan( relation, status) ; if (status != OK){ delete [] attrs ; delete [] mydata ; return status ; } if (type == INTEGER){ intval = atoi(attrValue) ; hfs->startScan( ads.attrOffset, ads.attrLen, (Datatype)ads.attrType, (char *)&intval, op) ; } if (type == FLOAT){ flval =(float)atof(attrValue) ; hfs->startScan( ads.attrOffset, ads.attrLen, (Datatype)ads.attrType, (char *)&flval, op) ; } if (type == STRING){ hfs->startScan( ads.attrOffset, ads.attrLen, (Datatype)ads.attrType, attrValue, op) ; } int intval1, flval1 ; recNum = 0 ; while(hfs->scanNext(rid) != FILEEOF){ for (i = 0; i < len; i++) mydata[i] = 0 ; hfs->getRecord(rec) ; memcpy( mydata, rec.data, rec.length) ; for (i = 0; i < myattrCnt; i++){ for (n = 0; n < attrCnt; n++){ if (strcmp(attrs[i].attrName, attrList[n].attrName) == 0){ if (attrs[i].attrType == INTEGER){ intval1 = atoi((char *)attrList[n].attrValue) ; memcpy( mydata + attrs[i].attrOffset, &intval1, attrs[i].attrLen) ; } if (attrs[i].attrType == FLOAT){ flval1 = (float)atof((char *)attrList[n].attrValue) ; memcpy( mydata + attrs[i].attrOffset, &flval1, attrs[i].attrLen) ; } if (attrs[i].attrType == STRING) memcpy( mydata + attrs[i].attrOffset, attrList[n].attrValue, attrs[i].attrLen) ; } } } rec.data = mydata ; rec.length = len ; hfs->updateRecord(rec) ; recNum++ ; } delete hfs ; delete [] mydata ; delete [] attrs ; printf("\n Table (%s) %d Record (s) Updateed!\n", relation.c_str(), recNum) ; return OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -