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

📄 ctrl_center.cpp

📁 实现一个精简型单用户SQL引擎(DBMS)MiniSQL
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        min = tree.MoveToNextKey(min);
      IncludeMin = 1; IncludeMax =1;  // include the max&min key when updating
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);              
      break;
    }
    case BE :  // '>='
    {
      max = tree.GetEnd();
      ZBTree_Node node;
      node.ReadNodeFromFile(max.ptr);
      // compare with the biggest key
      if( node.Compare(node.k[node.ItemOnNode-1],ConditionInfo.min) < 0)
      throw 1027;  // Error 1027: No Reocrd
      tree.Search(ConditionInfo.min,&min);
      if( min.offset == node.MaxItem )  // can't find and reach the end of a node
        min = tree.MoveToNextKey(min);
      IncludeMin = 1; IncludeMax =1;    // include the max&min key when updating
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);              
      break;
    }
    case L  :  // '<'
    {
      min = tree.GetStart();
      ZBTree_Node node;
      node.ReadNodeFromFile(min.ptr);
      // compare with the smallest key
      if( node.Compare(node.k[0],ConditionInfo.max) >= 0)
        throw 1027;  // Error 1027: No Reocrd
      tree.Search(ConditionInfo.max,&max);
         
     /* Key_Location end;
      end = tree.GetEnd();*/
      ZBTree_Node EndNode;
      EndNode.ReadNodeFromFile(max.ptr);
      // exceed the biggest key in tree
      if( max.offset == EndNode.MaxItem ) 
      {
        IncludeMax = 1;
        max.offset = EndNode.ItemOnNode - 1;
      }
      else 
        IncludeMax = 0;

      IncludeMin = 1;  // include the min key when updating
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);      
      break;
    }
    case LE :  // '<='
    {
      min = tree.GetStart();
      ZBTree_Node node;
      node.ReadNodeFromFile(min.ptr);
      // max < k[0]
      if( node.Compare(node.k[0],ConditionInfo.max) > 0)
        throw 1027;// no record

      if( tree.Search(ConditionInfo.max,&max) )  // the max key is existed
        IncludeMax = 1;
      else 
      {
        ZBTree_Node EndNode;
        EndNode.ReadNodeFromFile(max.ptr);
        // exceed the biggest key in tree
        if( max.offset == EndNode.MaxItem ) 
        {
          IncludeMax = 1;
          max.offset = EndNode.ItemOnNode - 1;
        }
       
        else 
          IncludeMax = 0;
      }
      IncludeMin = 1;  // include the min key when updating
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
      break;
    }
    case E  :  // '='
    {
      if( !tree.Search(ConditionInfo.min,&min) )
        throw 1026;  // Error 1026: the Key name isn't existed    
      max = min;
      IncludeMin = 1;  IncludeMax = 1;
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
      break;
    }
    case NE :  // '!='
    {
      Key_Location temp;
      if( !tree.Search(ConditionInfo.min,&temp) ) // the key isn't existed
      {
        min = tree.GetStart();
        max = tree.GetEnd();
        IncludeMin = 1;
        IncludeMax = 1;
        // update all record;
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
        break;
      }
      else  // the key is existed
      {
        min = tree.GetStart();
        max = tree.GetEnd();
        IncludeMin = 1;
        IncludeMax = 1;
                       
        // the key is in the middle of the tree
        IncludeMin = 1;
        IncludeMax = 0;
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,temp);
        IncludeMin = 0;
        IncludeMax = 1;
        UpdateRec(tree,RecInfo,IncludeMin,temp,IncludeMax,max);
        break;
      }
    }
    case BETWEEN:  // between ... and ...
    {                                        //  **************************************    
      Key_Location temp;                     //  6 conditions
      temp = tree.GetStart();                //    * * | |
      ZBTree_Node StartNode;                 //    | | * *    | -- the boundary of tree
      StartNode.ReadNodeFromFile(temp.ptr);  //    * | | *    * -- the value in SQL
                                             //    * | * |
      temp = tree.GetEnd();                  //    | * | * 
      ZBTree_Node EndNode;                   //    | * * |
      EndNode.ReadNodeFromFile(temp.ptr);    //  **************************************

      // compare with the start key  * * | |
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.max) > 0)
        throw 1027;  // Error 1027: No Reocrd            
              
      // compare with the end key    | | * *
      if( EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.min) < 0)
        throw 1027;  // Error 1027: No Reocrd

      // * | | *
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) >= 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) <= 0)
      {
        // print all record
        min = tree.GetStart();
        max = tree.GetEnd();
        IncludeMin = 1;
        IncludeMax = 1;
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
        break;
      }
      // * | * |  similiar as LE
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) >= 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) > 0)
      {
        min = tree.GetStart();
        ZBTree_Node node;
        node.ReadNodeFromFile(min.ptr);
        if( tree.Search(ConditionInfo.max,&max) )  // the max key is existed
          IncludeMax = 1;
        else
        {
          Key_Location end;
          end = tree.GetEnd();
          // exceed the biggest key in tree
          if( max.offset == node.MaxItem && max.ptr == end.ptr )  
          {
            IncludeMax = 1;
            max.offset = node.ItemOnNode - 1;
          }
          else if( max.offset == node.MaxItem )  // exceed the biggest key in node
          {
            max = tree.MoveToNextKey(max);       // move to the next key
            IncludeMax = 0;
          }
          else 
            IncludeMax = 0;
        }
        IncludeMin = 1;  // include the max key when updating
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
        break;
      }
      // | * | *  similiar as BE
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) < 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) <= 0)
      {
        max = tree.GetEnd();
        ZBTree_Node node;
        node.ReadNodeFromFile(max.ptr);
        tree.Search(ConditionInfo.min,&min);
        if( min.offset == node.MaxItem )  // can't find and reach the end of a node
          min = tree.MoveToNextKey(min);  // move to the next Key
        IncludeMin = 1; IncludeMax =1;    // include the max&min key when updating
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
          break;
       }

      // | * * | 
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) < 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) > 0)
      {
        Key_Location temp;
        temp = tree.GetEnd();
        ZBTree_Node node;
        node.ReadNodeFromFile(temp.ptr);
        tree.Search(ConditionInfo.min,&min);
        if( min.offset == node.MaxItem )  // can't find and reach the end of a node
          min = tree.MoveToNextKey(min);  // move to the next key
        IncludeMin = 1;   // include the min key when printing

        if( tree.Search(ConditionInfo.max,&max) )  // the max key is existed
          IncludeMax = 1;
        else 
        {
          if( max.offset == node.MaxItem )  // exceed the biggest key in node
          {
            max = tree.MoveToNextKey(max);
            IncludeMax = 0;
          }
          else 
            IncludeMax = 0;
        }
        // update the record
        UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
        break;     
      }
    }
  }     

}
void Delete(TB_Delete_Info & DeleteInfo)
{
  Condition_Info ConditionInfo;
  Key_Location KeyLoca;
  Rec_Info RecInfo;
  HCatalog catalog;
  Record   rec;
  ZBTree tree;
  // check the validity of DeleteInfo
  catalog.Delete(DeleteInfo,ConditionInfo);
  if( !tree.Search(ConditionInfo.min,&KeyLoca) )
    throw 1026;  // Error 1026: the Key name isn't existed
  _F_FileAddr ptr;
  tree.Delete(ConditionInfo.min,ptr);  // Delete the key and return the record address
  rec.Delete(ptr);  // Delete the Record
}

// IncludeMin == 0 --------  exclude min
// IncludeMin == 1 --------  include min
// IncludeMax == 0 --------  exclude max
// IncludeMax == 1 --------  include max
void Print(ZBTree & tree,Select_Rec_Info & SelectRecInfo,
           bool IncludeMin,Key_Location min,bool IncludeMax,Key_Location max)
{
  Record rec;
  Key_Location temp;
  // ****************
  if( (min == max) && ((0==IncludeMin) || (0==IncludeMax)) )
    return;
  
  if( IncludeMin == 0 ) // doesn't include min
    min = tree.MoveToNextKey(min);
  // print the the record
  if(IncludeMax == 0 )  // doesn't include max
  {
    temp = min;
    _F_FileAddr ptr;
    if(temp == max )
      throw 1027;  // no record
    while( temp != max )
    {
      ptr = tree.GetCurRecAddr(temp);
      rec.Print(ptr,SelectRecInfo);     // print the record
      temp = tree.MoveToNextKey(temp);  // move to next
    }
  }
  else  // include max
  {
    temp = min;
    _F_FileAddr ptr;
    do
    {
      ptr = tree.GetCurRecAddr(temp);
      rec.Print(ptr,SelectRecInfo);     // print the record
      if(temp == max)
        break;
      temp = tree.MoveToNextKey(temp);  // move to next
    }while(1);
  }
}

void Ctrl_DropTB()
{
  char address[256];
  strcpy(address,CurLocation);
  strcat(address,CurRelationName);
  strcat(address,".dbf");
  _M_File table1 = Buffer[address];
  table1.Close();  // close the file whose path is 'address'

  strcpy(address,CurLocation);
  strcat(address,CurRelationName);
  DelTB(address);  // delete the file
}

void Ctrl_DropDB()
{
  Buffer.End();   // close the buffer,write all page from memory to file
  char address[256];
  strcpy(address,CurLocation);
  DelDB(address); // delete datebase 
  Buffer.Start(); // initialize a new buffer
}


// IncludeMin == 0 --------  exclude min
// IncludeMin == 1 --------  include min
// IncludeMax == 0 --------  exclude max
// IncludeMax == 1 --------  include max
void UpdateRec(ZBTree & tree,Rec_Info & RecInfo,
           bool IncludeMin,Key_Location min,bool IncludeMax,Key_Location max)
{
  Record rec;
  Key_Location temp;
  // ****************
  if( (min == max) && ((0==IncludeMin) || (0==IncludeMax)) )
    return;
  
  if( IncludeMin == 0 ) // doesn't include min
    min = tree.MoveToNextKey(min);
  // print the the record
  if(IncludeMax == 0 )  // doesn't include max
  {
    temp = min;
    _F_FileAddr ptr;
    if(temp == max )
      throw 1027;  // no record
    while( temp != max )
    {
      ptr = tree.GetCurRecAddr(temp);
      rec.Update(ptr,RecInfo);          // update the record
      temp = tree.MoveToNextKey(temp);  // move to next
    }
  }
  else  // include max
  {
    temp = min;
    _F_FileAddr ptr;
    do
    {
      ptr = tree.GetCurRecAddr(temp);
      rec.Update(ptr,RecInfo);          // update the record
      if(temp == max)
        break;
      temp = tree.MoveToNextKey(temp);  // move to next
    }while(1);
  }
}

⌨️ 快捷键说明

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