Catalog.cpp
资源名称:minisqlc.rar [点击查看]
上传用户:lkd6667
上传日期:2015-05-13
资源大小:1448k
文件大小:31k
源码类别:
其他数据库
开发平台:
C/C++
- #include <iostream>
- #include <stdio.h>
- #include "Catalog.h"
- extern char CurLocation[256];//<---存有当前正在使用的表的相对路径
- extern char CurRelationName[33];//<---存当前表的表名
- extern _M_Buffer Buffer;
- extern unsigned int BTreeNodeSize;
- using namespace std;
- //找出给定列的位置
- Column_Info* HCatalog::Find_Column(char* Name) const
- {
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- _F_FileAddr head;
- head = CurrentTable.GetCataPoint(); //指向文件的头指针
- Table_Info* TableHead;
- TableHead = (Table_Info*)head.MemAddr(); //指向内存的头指针
- Column_Info* column;
- column = (Column_Info*)TableHead->KeyPtr.FileKey.MemAddr(); //内存中指向列的信息
- while (column != NULL)
- {
- if(strcmp(column->ColumnName,Name) == 0)
- return column;
- else
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- return NULL;
- }
- //判断列是否存在
- bool HCatalog::Exist_Column(char* Name) const
- {
- if(Find_Column(Name) == NULL)
- return false;
- else
- return true;
- }
- //判断列的类型是否正确
- bool HCatalog::Check_Type(Column_Info* column, Column_Type type) const
- {
- if(column->ColType == type)
- return true;
- else
- return false;
- }
- //判断此列是否为主键
- bool HCatalog::Check_Key(Column_Info* column) const
- {
- if(column->IsPrimary == 1)
- return true;
- else
- return false;
- }
- //判断约束条件是否成立
- bool HCatalog::Check_Value(Column_Info* column,Column_Value* val) const
- {
- Column_Type ColType = column->ColType;
- int integer;
- float fl;
- char* ch;
- switch(ColType){
- case I: //int
- integer = val->IntValue;
- break;
- case F: //float
- fl = val->FloatValue;
- break;
- case C: //char
- ch = val->pCharValue;
- break;
- default:
- throw 1010; //Error code 1010----错误的类型;
- }
- //此列上的约束条件
- Constraint_Info* constraint = (Constraint_Info*)column->ConstraintPtr.FileConstraint.MemAddr();
- //没有约束
- if(constraint == NULL)
- return true;
- if(ColType == I)
- switch(constraint->OperType){
- case L: // <
- if(integer >= constraint->max.IntValue)
- return false;
- break;
- case LE: // <=
- if(integer > constraint->max.IntValue)
- return false;
- break;
- case B: // >
- if(integer <= constraint->min.IntValue)
- return false;
- break;
- case BE: // >=
- if(integer < constraint->min.IntValue)
- return false;
- break;
- case E: // =
- if(integer != constraint->max.IntValue)
- return false;
- break;
- case NE: // !=
- if(integer == constraint->max.IntValue)
- return false;
- break;
- case BETWEEN: // between...and...
- if((integer < constraint->min.IntValue) || (integer > constraint->max.IntValue))
- return false;
- break;
- default:
- throw 1009; //error code 1009----错误的关系运算符
- }
- else if (ColType == F)
- switch(constraint->OperType){
- case L: // >
- if(fl >= constraint->max.FloatValue)
- return false;
- break;
- case LE: // >=
- if(fl > constraint->max.FloatValue)
- return false;
- break;
- case B: // <
- if(fl <= constraint->min.FloatValue)
- return false;
- break;
- case BE: // <=
- if(fl < constraint->min.FloatValue)
- return false;
- break;
- case E: // ==
- if(fl != constraint->max.FloatValue)
- return false;
- break;
- case NE: // !=
- if(fl == constraint->max.FloatValue)
- return false;
- break;
- case BETWEEN: // between...and...
- if((fl < constraint->min.FloatValue) || (fl > constraint->max.FloatValue))
- return false;
- break;
- default:
- throw 1009; //error code 1009----错误的关系运算符
- }
- else{
- if(strlen(ch) > column->RequiredLength)
- throw 1013; //Error1013: 长度不符合
- switch(constraint->OperType){
- case L: // >
- if(strcmp(ch,constraint->max.CharValue) >= 0)
- return false;
- break;
- case LE: // >=
- if(strcmp(ch,constraint->max.CharValue) > 0)
- return false;
- break;
- case B: // <
- if(strcmp(ch,constraint->min.CharValue) <= 0)
- return false;
- break;
- case BE: // <=
- if(strcmp(ch,constraint->min.CharValue) < 0)
- return false;
- break;
- case E: // ==
- if(strcmp(ch,constraint->max.CharValue) != 0)
- return false;
- break;
- case NE: // !=
- if(strcmp(ch,constraint->max.CharValue) == 0)
- return false;
- break;
- case BETWEEN: // between...and...
- if((strcmp(ch,constraint->min.CharValue) < 0) || (strcmp(ch,constraint->max.CharValue)) > 0)
- return false;
- break;
- default:
- throw 1009; //error code 1009-----错误的关系运算符
- }
- }
- return true;
- }
- //检查char的长度是否合法
- bool HCatalog::Check_Length(Column_Info* column, Column_Value* val) const
- {
- if((column->ColType==C) && (strlen(val->pCharValue) > column->RequiredLength))
- return false;
- else
- return true;
- }
- //检查键是否合法
- bool HCatalog::Check_Key_Validation(Column_Type ColType, Column_Value* max, Column_Value* min) const
- {
- switch(ColType){
- case I:
- if(max->IntValue < min->IntValue)
- return false;
- else
- return true;
- case F:
- if(max->FloatValue < min->FloatValue)
- return false;
- else
- return true;
- case C:
- if(strcmp(max->pCharValue,min->pCharValue) < 0)
- return false;
- else
- return true;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- }
- //表的信息
- TTable_Info::TTable_Info()
- {
- KeyPtr.FileKey.Initialize();
- KeyPtr.Key = NULL;
- strcpy(TableName,"");
- TotalColumn = 0;
- RecordLength = 0;
- KeyAttrNum = 0;
- }
- //生成链表节点
- Column_Info* HCatalog::Form_ListNode(Create_Column* ptr,int id) const
- {
- Column_Info* pcolumn = new Column_Info;
- Constraint_Info* constraint;
- pcolumn->ID = id;
- strcpy(pcolumn->ColumnName,ptr->ColumnName);
- pcolumn->ColType = ptr->ColType;
- pcolumn->IsPrimary = ptr->IsPrimary;
- pcolumn->ColumnPtr.next = NULL;
- if(ptr->IsPrimary == true)
- pcolumn->IsNull = 0;
- else if (ptr->IsNull == true)
- pcolumn->IsNull = 1;
- else if (ptr->IsNull == false)
- pcolumn->IsNull = 0;
- switch(pcolumn->ColType){
- case I:
- pcolumn->RequiredLength = sizeof(int);
- pcolumn->StoredLength = pcolumn->RequiredLength;
- break;
- case F:
- pcolumn->RequiredLength = sizeof(float);
- pcolumn->StoredLength = pcolumn->RequiredLength;
- break;
- case C:
- pcolumn->RequiredLength = ptr->length;
- pcolumn->StoredLength = pcolumn->RequiredLength+1;
- break;
- default:
- throw 1010; //error 1010: 错误的数据类型
- }
- //是否有约束
- bool flag=false;
- if((ptr->ColType==I) && (ptr->min.IntValue<=ptr->max.IntValue))
- flag=true;
- else if((ptr->ColType==F) && (ptr->min.FloatValue<=ptr->max.FloatValue))
- flag=true;
- else if((ptr->ColType==C) && (strcmp(ptr->min.pCharValue,ptr->max.pCharValue)<=0))
- flag=true;
- if(flag){ //有约束,连接入节点
- constraint = new Constraint_Info;
- constraint->OperType = ptr->OperType;
- switch(pcolumn->ColType){
- case I:
- constraint->min.IntValue = ptr->min.IntValue;
- constraint->max.IntValue = ptr->max.IntValue;
- break;
- case F:
- constraint->min.FloatValue = ptr->min.FloatValue;
- constraint->max.FloatValue = ptr->max.FloatValue;
- break;
- case C:
- strcpy(constraint->min.CharValue, ptr->min.pCharValue);
- strcpy(constraint->max.CharValue, ptr->max.pCharValue);
- break;
- }
- pcolumn->ConstraintPtr.constraint = constraint;
- }
- else //没约束,指针设为空
- pcolumn->ConstraintPtr.constraint = NULL;
- return pcolumn;
- }
- //创建
- void HCatalog::Create(TB_Create_Info& info, char* KeyInfo)
- {
- Create_Column* ptr;
- ptr = info.head;
- Table_Info TableNode;
- strcpy(TableNode.TableName,info.TableName);
- TableNode.TotalColumn = info.TotalColumn;
- Column_Info* column1;
- Column_Info* head;
- Column_Info* column2;
- int count=0; //主键数
- int length=0; //记录长度
- int j=0;
- int keylen=0;
- for(int i=0 ;ptr!=NULL ;ptr=ptr->next,i++){
- column1 = this->Form_ListNode(ptr,i);
- length += column1->StoredLength;
- if(column1->IsPrimary == 1){
- count++;
- keylen += column1->StoredLength;
- if(column1->ColType == C)
- j += sprintf(KeyInfo+j,"c%d",column1->RequiredLength);
- else if(column1->ColType == F)
- j += sprintf(KeyInfo+j,"f");
- else
- j += sprintf(KeyInfo+j,"i");
- }
- if(i == 0)
- head = column1;
- else
- column2->ColumnPtr.next = column1;
- column2 = column1;
- }
- if(( BTreeNodeSize - 2*sizeof(_F_FileAddr) - sizeof(bool) - sizeof(int) )
- / ( sizeof(_F_FileAddr) + keylen) < 4)
- throw 1034; //Error 1034: Primary Key is too long!
- TableNode.KeyPtr.Key = head;
- TableNode.KeyAttrNum = count;
- TableNode.RecordLength = length;
- //定位到当前表
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- //写入文件
- _F_FileAddr pFile,pFile1,pFile2;
- pFile = CurrentTable.GetCataPoint();
- pFile1 = MemWrite((void*)&TableNode,sizeof(Table_Info),&pFile);
- column1 = head;
- if(column1 != NULL){
- ((Table_Info*)pFile.MemAddr())->KeyPtr.FileKey = pFile1; //移动文件指针
- pFile = MemWrite((void*)column1,sizeof(Column_Info),&pFile1);
- ((Column_Info*)pFile1.MemAddr())->ColumnPtr.FileNext.Initialize();
- pFile2 = pFile1;
- if(column1->ConstraintPtr.constraint != NULL){
- ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint = pFile;
- pFile1 = MemWrite((void*)column1->ConstraintPtr.constraint,sizeof(Constraint_Info),&pFile);
- }
- else{
- ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint.Initialize();
- pFile1 = pFile; //记录上次的写入点
- }
- column1 = column1->ColumnPtr.next;
- }
- while(column1 != NULL){
- ((Column_Info*)pFile2.MemAddr())->ColumnPtr.FileNext = pFile1;
- pFile = MemWrite((void*)column1,sizeof(Column_Info),&pFile1);
- ((Column_Info*)pFile1.MemAddr())->ColumnPtr.FileNext.Initialize();
- pFile2 = pFile1;
- if(column1->ConstraintPtr.constraint != NULL){
- ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint = pFile;
- pFile1 = MemWrite((void*)column1->ConstraintPtr.constraint,sizeof(Constraint_Info),&pFile);
- }
- else{
- ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint.Initialize();
- pFile1 = pFile;
- }
- column1 = column1->ColumnPtr.next;
- }
- //删除列表
- column1 = head;
- column2 = column1->ColumnPtr.next;
- while(column1 != NULL){
- if(column1->ConstraintPtr.constraint != NULL)
- delete column1->ConstraintPtr.constraint;
- delete column1;
- column1 = column2;
- if(column1 != NULL)
- column2 = column1->ColumnPtr.next;
- }
- return;
- }
- //选择
- void HCatalog::Select(TB_Select_Info& info,Condition_Info& index,Select_Rec_Info& record)
- {
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- Select_Column* pcolumn = info.ColumnHead;
- Select_Condition* pcondition = info.ConditionHead;
- //检查约束条件列是否存在
- Column_Info* column;
- int l=0;
- if(strcmp(pcolumn->ColumnName,"*") != 0){
- for(; pcolumn!=NULL; pcolumn=pcolumn->next){
- if(!this->Exist_Column(pcolumn->ColumnName))
- throw 1008; //Error1008: Column demanded has not existed yet!
- column = this->Find_Column(pcolumn->ColumnName);
- l += column->StoredLength;
- }
- }
- _F_FileAddr pFile = CurrentTable.GetCataPoint();
- Table_Info* TbInfo = (Table_Info*)pFile.MemAddr();
- if((info.ConditionNum!=0) && (TbInfo->KeyAttrNum!=info.ConditionNum))
- throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
- for(; pcondition!=NULL; pcondition=pcondition->next){
- column = this->Find_Column(pcondition->PrimaryKey); //find the demanded column
- if(column == NULL)
- throw 1007; //Error1007: Primary Key demanded has not existed yet!
- if(!this->Check_Key(column))
- throw 1011; //Error1011: Not a select based on primary key!
- if(!this->Check_Type(column,pcondition->ColType))
- throw 1012; //Error1012: Type mismatch!
- if(pcondition->OperType == BETWEEN){
- if(!Check_Key_Validation(pcondition->ColType,&(pcondition->max),&(pcondition->min)))
- throw 1020; //Error1020: Result is null!
- }
- if(pcondition->ColType == C){
- switch(pcondition->OperType){
- case L:
- case LE:
- if(!Check_Length(column,&(pcondition->max)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case B:
- case BE:
- if(!Check_Length(column,&(pcondition->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case BETWEEN:
- case E:
- case NE:
- if(!Check_Length(column,&(pcondition->max)) || !Check_Length(column,&(pcondition->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- default:
- throw 1009; //Error1009: Unknown relation operator!
- }
- }
- }
- Column_Info* head;
- head = (Column_Info*)TbInfo->KeyPtr.FileKey.MemAddr();
- column = head;
- pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
- pminkey = new Key_Attr;
- pkey1 = pminkey;
- pkey3 = pkey1;
- pmaxkey = new Key_Attr;
- pkey2 = pmaxkey;
- pkey4 = pkey2;
- int count = TbInfo->KeyAttrNum;
- if(info.ConditionHead != NULL){
- while((column!=NULL) && (count!=0)){
- if(column->IsPrimary == 1){
- //找寻选择条件
- for(pcondition = info.ConditionHead; pcondition!=NULL; pcondition=pcondition->next){
- if(strcmp(pcondition->PrimaryKey,column->ColumnName)==0){
- switch (pcondition->ColType){
- case I:
- pkey1->value.IntValue = pcondition->min.IntValue;
- pkey2->value.IntValue = pcondition->max.IntValue;
- break;
- case F:
- pkey1->value.FloatValue = pcondition->min.FloatValue;
- pkey2->value.FloatValue = pcondition->max.FloatValue;
- break;
- case C:
- pkey1->value.pCharValue = pcondition->min.pCharValue;
- pkey2->value.pCharValue = pcondition->max.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- break;
- }
- }
- pkey3 = pkey1;
- pkey4 = pkey2;
- pkey1->next = new Key_Attr;
- pkey1 = pkey1->next;
- pkey2->next = new Key_Attr;
- pkey2 = pkey2->next;
- count--;
- }
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- pkey3->next = NULL;
- pkey4->next = NULL;
- delete pkey1;
- delete pkey2;
- index.max = pmaxkey;
- index.min = pminkey;
- index.OperType = info.ConditionHead->OperType;
- }
- else{
- index.max = NULL;
- index.min = NULL;
- index.OperType = ALL;
- }
- //为record准备的数据
- Select_Cell* cell = new Select_Cell;
- Select_Cell* cell1 = cell;
- record.head = cell;
- pcolumn = info.ColumnHead;
- column = head;
- int len=0;
- if(strcmp(pcolumn->ColumnName,"*")!=0){
- while(pcolumn != NULL){
- for(column = head; column!=NULL; len+=column->StoredLength,column=(Column_Info*)column->ColumnPtr.FileNext.MemAddr()){
- if(strcmp(pcolumn->ColumnName,column->ColumnName)==0){
- strcpy(cell->ColumnName,column->ColumnName);
- cell->ColType = column->ColType;
- cell->ColLength = column->StoredLength;
- cell->PriorLength = len;
- if(pcolumn->next != NULL){
- cell1 = cell;
- cell->next = new Select_Cell;
- cell = cell->next;
- }
- break;
- }
- }
- pcolumn = pcolumn->next;
- len = 0;
- }
- record.ColumnNum = info.ColumnNum;
- record.RecordLength = l;
- }
- else{
- while(column!=NULL){
- strcpy(cell->ColumnName,column->ColumnName);
- cell->ColType = column->ColType;
- cell->ColLength = column->StoredLength;
- cell->PriorLength = len;
- len += column->StoredLength;
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- if(column!=NULL){
- cell->next = new Select_Cell;
- cell = cell->next;
- }
- }
- record.ColumnNum = TbInfo->TotalColumn;
- record.RecordLength = TbInfo->RecordLength;
- }
- return;
- }
- //插入
- void HCatalog::Insert(TB_Insert_Info& info, Key_Attr& index, Rec_Info& record)
- {
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- Insert_Column *raw;
- raw = info.head;
- Column_Info* column = NULL;
- Column_Info* head;
- Table_Info* table;
- table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
- head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
- int count=0;
- //检查插入的信息
- while (raw != NULL){
- if(strcmp(raw->ColumnName,"*") != 0){
- if(!Exist_Column(raw->ColumnName))
- throw 1008; //Error1008: Column demanded has not existed yet!
- column = this->Find_Column(raw->ColumnName);
- if(Check_Key(column))
- count++;
- }
- else{
- if(column == NULL)
- column = head;
- else
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- if(!Check_Type(column,raw->ColType))
- throw 1012; //Error1012: Type mismatch!
- if(!Check_Length(column,&(raw->value)))
- throw 1013; //Error1013: Length Invalid for type char!
- if(!Check_Value(column,&(raw->value)))
- throw 1014; //Error1014: Invalid Value!
- raw = raw->next;
- }
- raw = info.head;
- if(strcmp(raw->ColumnName,"*") != 0){
- if(count != table->KeyAttrNum)
- throw 1015; //Error1015: Value of Primary Key can not be null!
- }
- else{
- if(info.ColumnNum != table->TotalColumn)
- throw 1016; //Error1016: Inadequate value for every column!
- count = table->KeyAttrNum;
- }
- column = head;
- if(strcmp(info.head->ColumnName,"*") != 0){
- while(column != NULL){
- if(column->IsNull == 0){
- for(raw=info.head; raw!=NULL; raw=raw->next)
- if(strcmp(raw->ColumnName,column->ColumnName) == 0)
- break;
- if(raw == NULL)
- throw 1017; //Error1017: No value for some not null column!
- }
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- }
- //为index insertion 的信息
- Key_Attr* key = &index;
- column = head;
- raw = info.head;
- while(count!=0 && column!=NULL){
- if(strcmp(info.head->ColumnName,"*") != 0){
- if(column->IsPrimary == 0){
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- continue;
- }
- for(raw=info.head; raw!=NULL; raw=raw->next){
- if(strcmp(raw->ColumnName,column->ColumnName)==0){
- switch(column->ColType){
- case I:
- key->value.IntValue = raw->value.IntValue;
- break;
- case F:
- key->value.FloatValue = raw->value.FloatValue;
- break;
- case C:
- key->value.pCharValue = raw->value.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- count--;
- break;
- }
- }
- }
- //i插入所有列
- else{
- if(column->IsPrimary == 0){
- raw = raw->next;
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- continue;
- }
- switch(column->ColType){
- case I:
- key->value.IntValue = raw->value.IntValue;
- break;
- case F:
- key->value.FloatValue = raw->value.FloatValue;
- break;
- case C:
- key->value.pCharValue = raw->value.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- count--;
- raw = raw->next;
- }
- if(count!=0){
- key->next = new Key_Attr;
- key = key->next;
- }
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- //为 record insert的信息
- Cell_Info* cell = new Cell_Info;
- record.ColNum = table->TotalColumn;
- record.head = cell;
- record.RecordLength = table->RecordLength;
- column = head;
- raw = info.head;
- int len=0;
- if(strcmp(info.head->ColumnName,"*") == 0){
- while(column != NULL){
- cell->ColLength = column->StoredLength;
- cell->ColType = column->ColType;
- cell->PriorLength = len;
- switch(column->ColType){
- case I:
- cell->value.IntValue = raw->value.IntValue;
- break;
- case F:
- cell->value.FloatValue = raw->value.FloatValue;
- break;
- case C:
- cell->value.pCharValue = raw->value.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- len += column->StoredLength;
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- raw = raw->next;
- if(column != NULL){
- cell->next = new Cell_Info;
- cell = cell->next;
- }
- }
- }
- else{
- column = head;
- len = 0;
- while(column!=NULL){
- raw = info.head;
- while(raw != NULL){
- if(strcmp(raw->ColumnName,column->ColumnName) == 0){
- cell->ColLength = column->StoredLength;
- cell->ColType = column->ColType;
- cell->PriorLength = len;
- switch(column->ColType){
- case I:
- cell->value.IntValue = raw->value.IntValue;
- break;
- case F:
- cell->value.FloatValue = raw->value.FloatValue;
- break;
- case C:
- cell->value.pCharValue = raw->value.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- break;
- }
- raw = raw->next;
- }
- if(raw == NULL){
- cell->ColLength = column->StoredLength;
- cell->ColType = column->ColType;
- cell->PriorLength = len;
- switch(column->ColType){
- case I:
- cell->value.IntValue = 0;
- break;
- case F:
- cell->value.FloatValue = 0.0;
- break;
- case C:
- cell->value.pCharValue = NULL;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- }
- len += column->StoredLength;
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- if(column != NULL){
- cell->next = new Cell_Info;
- cell = cell->next;
- }
- }
- }
- return;
- }
- //更新
- void HCatalog::Update(TB_Update_Info& info, Condition_Info& index, Rec_Info& record)
- {
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- Update_Column* col;
- Update_Condition* cond;
- col = info.ColumnHead;
- cond = info.ConditionHead;
- cout<<"text1";
- Column_Info* column;
- while(col != NULL){
- if(!Exist_Column(col->ColumnName))
- throw 1008; //Error1008: Column demanded has not existed yet!
- column = Find_Column(col->ColumnName);
- if(column->IsPrimary == 1)
- throw 1018; //Error1018: Update denied on primary key!
- if(!Check_Type(column,col->ColType))
- throw 1012; //Error1012: Type mismatch!
- if(col->ColType == C)
- if(!Check_Length(column,&col->value))
- throw 1013; //Error1013: Length Invalid for type char!
- if(!Check_Value(column,&col->value))
- throw 1014; //Error1014: Invalid Value!
- col = col->next;
- }
- cout<<"text1";
- Table_Info* table;
- table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
- if(info.ConditionNum != table->KeyAttrNum)
- throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
- for(; cond!=NULL; cond=cond->next){
- column = this->Find_Column(cond->PrimaryKey);
- if(column == NULL)
- throw 1007; //Error1007: Primary Key demanded has not existed yet!
- if(!this->Check_Key(column))
- throw 1011; //Error1011: Not a select based on primary key!
- if(!this->Check_Type(column,cond->ColType))
- throw 1012; //Error1012: Type mismatch!
- if(cond->OperType == BETWEEN){
- if(!Check_Key_Validation(cond->ColType,&(cond->max),&(cond->min)))
- throw 1020; //Error1020: Result is null!
- }
- if(cond->ColType == C){ //check if the length is correct if the column is char type
- switch(cond->OperType){
- case L:
- case LE:
- if(!Check_Length(column,&(cond->max)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case B:
- case BE:
- if(!Check_Length(column,&(cond->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case E:
- case NE:
- case BETWEEN:
- if(!Check_Length(column,&(cond->max)) || !Check_Length(column,&(cond->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- default:
- throw 1009; //Error1009: Unknown relation operator!
- }
- }
- }
- cout<<"text1";
- //为 index select的信息
- Column_Info* head;
- head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
- column = head;
- int count = table->KeyAttrNum;
- pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
- pminkey = new Key_Attr;
- pkey1 = pminkey;
- pkey3 = pkey1;
- pmaxkey = new Key_Attr;
- pkey2 = pmaxkey;
- pkey4 = pkey2;
- while((column!=NULL) && (count!=0)){
- if(column->IsPrimary == 1){
- for(cond = info.ConditionHead; cond!=NULL; cond=cond->next){
- if(strcmp(cond->PrimaryKey,column->ColumnName)==0){
- switch (cond->ColType){
- case I:
- pkey1->value.IntValue = cond->min.IntValue;
- pkey2->value.IntValue = cond->max.IntValue;
- break;
- case F:
- pkey1->value.FloatValue = cond->min.FloatValue;
- pkey2->value.FloatValue = cond->max.FloatValue;
- break;
- case C:
- pkey1->value.pCharValue = cond->min.pCharValue;
- pkey2->value.pCharValue = cond->max.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- break;
- }
- }
- if(cond==NULL)
- switch(column->ColType){
- case I:
- pkey1->value.IntValue = 1;
- pkey2->value.IntValue = 0;
- break;
- case F:
- pkey1->value.FloatValue = 1.0;
- pkey2->value.FloatValue = 0.0;
- break;
- case C:
- strcpy(pkey1->value.pCharValue,"b");
- strcpy(pkey2->value.pCharValue,"a");
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- pkey3 = pkey1;
- pkey4 = pkey2;
- pkey1->next = new Key_Attr;
- pkey1 = pkey1->next;
- pkey2->next = new Key_Attr;
- pkey2 = pkey2->next;
- count--;
- }
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- pkey3->next = NULL;
- pkey4->next = NULL;
- delete pkey1;
- delete pkey2;
- index.max = pmaxkey;
- index.min = pminkey;
- if(info.ConditionHead == NULL)
- index.OperType = ALL;
- else
- index.OperType = info.ConditionHead->OperType;
- cout<<"text1";
- //为 record update的信息
- Cell_Info* cell = new Cell_Info;
- Cell_Info* cell1 = cell;
- record.head = cell;
- record.ColNum = info.ColumnNum;
- record.RecordLength = table->RecordLength;
- column = head;
- int len = 0;
- while(column!=NULL){
- col = info.ColumnHead;
- for(;col!=NULL;col=col->next){
- if(strcmp(col->ColumnName,column->ColumnName)==0){
- cell->ColLength = column->StoredLength;
- cell->ColType = column->ColType;
- cell->PriorLength = len;
- switch(column->ColType){
- case I:
- cell->value.IntValue = col->value.IntValue;
- break;
- case F:
- cell->value.FloatValue = col->value.FloatValue;
- break;
- case C:
- cell->value.pCharValue = col->value.pCharValue;
- break;
- default:
- throw 1010; //Error1010:错误的数据类型
- }
- cell->next = new Cell_Info;
- cell1 = cell;
- cell = cell->next;
- break;
- }
- }
- len += column->StoredLength;
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- cell1->next = NULL;
- delete cell;
- return;
- }
- //删除
- void HCatalog::Delete(TB_Delete_Info& info, Condition_Info& index)
- {
- char temp[289];
- sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
- _M_File CurrentTable = Buffer[temp];
- Delete_Condition* cond=info.head;
- Column_Info* column;
- Table_Info* table;
- table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
- if(info.ConditionNum != table->KeyAttrNum)
- throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
- for(; cond!=NULL; cond=cond->next){
- column = this->Find_Column(cond->PrimaryKey);
- if(column == NULL)
- throw 1007; //Error1007: Primary Key demanded has not existed yet!
- if(!this->Check_Key(column))
- throw 1011; //Error1011: Not a select based on primary key!
- if(!this->Check_Type(column,cond->ColType))
- throw 1012; //Error1012: Type mismatch!
- if(cond->OperType == BETWEEN){
- if(!Check_Key_Validation(cond->ColType,&(cond->max),&(cond->min)))
- throw 1020; //Error1020: Result is null!
- }
- if(cond->ColType == C){
- switch(cond->OperType){
- case L:
- case LE:
- if(!Check_Length(column,&(cond->max)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case B:
- case BE:
- if(!Check_Length(column,&(cond->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- case E:
- case NE:
- case BETWEEN:
- if(!Check_Length(column,&(cond->max)) || !Check_Length(column,&(cond->min)))
- throw 1013; //Error1013: Length Invalid for type char!
- else
- break;
- default:
- throw 1009; //Error1009: 错误的关系运算符
- }
- }
- }
- //给index的信息
- Column_Info* head;
- head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
- column = head;
- int count = table->KeyAttrNum;
- pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
- pminkey = new Key_Attr;
- pkey1 = pminkey;
- pkey3 = pkey1;
- pmaxkey = new Key_Attr;
- pkey2 = pmaxkey;
- pkey4 = pkey2;
- while((column!=NULL) && (count!=0)){
- if(column->IsPrimary == 1){
- for(cond = info.head; cond!=NULL; cond=cond->next){
- if(strcmp(cond->PrimaryKey,column->ColumnName)==0){
- switch (cond->ColType){
- case I:
- pkey1->value.IntValue = cond->min.IntValue;
- pkey2->value.IntValue = cond->max.IntValue;
- break;
- case F:
- pkey1->value.FloatValue = cond->min.FloatValue;
- pkey2->value.FloatValue = cond->max.FloatValue;
- break;
- case C:
- pkey1->value.pCharValue = cond->min.pCharValue;
- pkey2->value.pCharValue = cond->max.pCharValue;
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- break;
- }
- }
- if(cond==NULL)
- switch(column->ColType){
- case I:
- pkey1->value.IntValue = 1;
- pkey2->value.IntValue = 0;
- break;
- case F:
- pkey1->value.FloatValue = 1.0;
- pkey2->value.FloatValue = 0.0;
- break;
- case C:
- strcpy(pkey1->value.pCharValue,"b");
- strcpy(pkey2->value.pCharValue,"a");
- break;
- default:
- throw 1010; //Error1010: 错误的数据类型
- }
- pkey3 = pkey1;
- pkey4 = pkey2;
- pkey1->next = new Key_Attr;
- pkey1 = pkey1->next;
- pkey2->next = new Key_Attr;
- pkey2 = pkey2->next;
- count--;
- }
- column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
- }
- pkey3->next = NULL;
- pkey4->next = NULL;
- delete pkey1;
- delete pkey2;
- index.max = pmaxkey;
- index.min = pminkey;
- if(info.head == NULL)
- index.OperType = ALL;
- else
- index.OperType = info.head->OperType;
- return;
- }