Catalog.cpp
上传用户:lkd6667
上传日期:2015-05-13
资源大小:1448k
文件大小:31k
源码类别:

其他数据库

开发平台:

C/C++

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "Catalog.h"
  4. extern char CurLocation[256];//<---存有当前正在使用的表的相对路径
  5. extern char CurRelationName[33];//<---存当前表的表名
  6. extern _M_Buffer Buffer;
  7. extern unsigned int BTreeNodeSize;
  8. using namespace std;
  9. //找出给定列的位置
  10. Column_Info* HCatalog::Find_Column(char* Name) const
  11. {
  12. char temp[289];
  13. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  14. _M_File CurrentTable = Buffer[temp];
  15. _F_FileAddr head;
  16. head = CurrentTable.GetCataPoint();     //指向文件的头指针
  17. Table_Info* TableHead;
  18. TableHead = (Table_Info*)head.MemAddr(); //指向内存的头指针
  19. Column_Info* column;
  20. column = (Column_Info*)TableHead->KeyPtr.FileKey.MemAddr(); //内存中指向列的信息
  21. while (column != NULL)
  22. {
  23. if(strcmp(column->ColumnName,Name) == 0)
  24. return column;
  25. else
  26. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  27. }
  28. return NULL;
  29. }
  30. //判断列是否存在
  31. bool HCatalog::Exist_Column(char* Name) const
  32. {
  33. if(Find_Column(Name) == NULL)
  34. return false;
  35. else
  36. return true;
  37. //判断列的类型是否正确
  38. bool HCatalog::Check_Type(Column_Info* column, Column_Type type) const
  39. {
  40. if(column->ColType == type)
  41. return true;
  42. else
  43. return false;
  44. }
  45. //判断此列是否为主键
  46. bool HCatalog::Check_Key(Column_Info* column) const
  47. {
  48. if(column->IsPrimary == 1)
  49. return true;
  50. else
  51. return false;
  52. }
  53. //判断约束条件是否成立
  54. bool HCatalog::Check_Value(Column_Info* column,Column_Value* val) const
  55. {
  56. Column_Type ColType = column->ColType; 
  57. int integer;
  58. float fl;
  59. char* ch;
  60.     switch(ColType){
  61.     case I:       //int
  62.     integer = val->IntValue;
  63. break;
  64. case F:       //float
  65. fl = val->FloatValue;
  66. break;
  67. case C:       //char
  68. ch = val->pCharValue;
  69. break;
  70. default:
  71. throw 1010; //Error code 1010----错误的类型;
  72. }
  73. //此列上的约束条件
  74.     Constraint_Info* constraint = (Constraint_Info*)column->ConstraintPtr.FileConstraint.MemAddr(); 
  75. //没有约束
  76.     if(constraint == NULL)
  77. return true;
  78.     if(ColType == I)    
  79. switch(constraint->OperType){
  80. case L:   // <
  81. if(integer >= constraint->max.IntValue)
  82. return false;
  83. break;
  84. case LE:   // <=
  85. if(integer > constraint->max.IntValue)
  86. return false;
  87. break;
  88. case B:    // >
  89. if(integer <= constraint->min.IntValue)
  90. return false;
  91. break;
  92. case BE:   // >=
  93. if(integer < constraint->min.IntValue)
  94. return false;
  95. break;
  96. case E:    // =
  97. if(integer != constraint->max.IntValue)
  98. return false;
  99. break;
  100. case NE:   // !=
  101. if(integer == constraint->max.IntValue)
  102. return false;
  103. break;
  104. case BETWEEN:   // between...and...
  105. if((integer < constraint->min.IntValue) || (integer > constraint->max.IntValue))
  106. return false;
  107. break;
  108. default:
  109. throw 1009; //error code 1009----错误的关系运算符
  110. }
  111.        else if (ColType == F)
  112. switch(constraint->OperType){
  113. case L:   // >
  114. if(fl >= constraint->max.FloatValue)
  115. return false;
  116. break;
  117. case LE:   // >=
  118. if(fl > constraint->max.FloatValue)
  119. return false;
  120. break;
  121. case B:     // <
  122. if(fl <= constraint->min.FloatValue)
  123. return false;
  124. break;
  125. case BE:    // <=
  126. if(fl < constraint->min.FloatValue)
  127. return false;
  128. break;
  129. case E:     // ==
  130. if(fl != constraint->max.FloatValue)
  131. return false;
  132. break;
  133. case NE:    // !=
  134. if(fl == constraint->max.FloatValue)
  135. return false;
  136. break;
  137. case BETWEEN:   // between...and...
  138. if((fl < constraint->min.FloatValue) || (fl > constraint->max.FloatValue))
  139. return false;
  140. break;
  141. default:
  142. throw 1009; //error code 1009----错误的关系运算符
  143. }
  144.       else{ 
  145. if(strlen(ch) > column->RequiredLength)
  146. throw 1013; //Error1013: 长度不符合
  147. switch(constraint->OperType){
  148. case L:   // >
  149. if(strcmp(ch,constraint->max.CharValue) >= 0)
  150. return false;
  151. break;
  152. case LE:  // >=
  153. if(strcmp(ch,constraint->max.CharValue) > 0)
  154. return false;
  155. break;
  156. case B:   // <
  157. if(strcmp(ch,constraint->min.CharValue) <= 0)
  158. return false;
  159. break;
  160. case BE:   // <=
  161. if(strcmp(ch,constraint->min.CharValue) < 0)
  162. return false;
  163. break;
  164. case E:   // ==
  165. if(strcmp(ch,constraint->max.CharValue) != 0)
  166. return false;
  167. break;
  168. case NE:  // !=
  169. if(strcmp(ch,constraint->max.CharValue) == 0)
  170. return false;
  171. break;
  172. case BETWEEN:   // between...and...
  173. if((strcmp(ch,constraint->min.CharValue) < 0) || (strcmp(ch,constraint->max.CharValue)) > 0)
  174. return false;
  175. break;
  176. default:
  177. throw 1009; //error code 1009-----错误的关系运算符
  178. }
  179. }
  180. return true;
  181. }
  182. //检查char的长度是否合法
  183. bool HCatalog::Check_Length(Column_Info* column, Column_Value* val) const
  184. {
  185. if((column->ColType==C) && (strlen(val->pCharValue) > column->RequiredLength))
  186. return false;
  187. else 
  188. return true;
  189. }
  190. //检查键是否合法
  191. bool HCatalog::Check_Key_Validation(Column_Type ColType, Column_Value* max, Column_Value* min) const
  192. {
  193.   switch(ColType){
  194.   case I:  
  195.     if(max->IntValue < min->IntValue)
  196.       return false;
  197.     else 
  198.       return true;
  199.   case F:   
  200.     if(max->FloatValue < min->FloatValue)
  201.       return false;
  202.     else 
  203.       return true;
  204.   case C:   
  205.     if(strcmp(max->pCharValue,min->pCharValue) < 0)
  206.       return false;
  207.     else
  208.       return true;
  209.   default:
  210.     throw 1010;   //Error1010: 错误的数据类型
  211.   }
  212. }
  213. //表的信息
  214. TTable_Info::TTable_Info()
  215. {
  216. KeyPtr.FileKey.Initialize();
  217. KeyPtr.Key = NULL;
  218. strcpy(TableName,"");
  219. TotalColumn = 0;
  220. RecordLength = 0;
  221. KeyAttrNum = 0;
  222. }
  223. //生成链表节点
  224. Column_Info* HCatalog::Form_ListNode(Create_Column* ptr,int id) const
  225. {
  226. Column_Info* pcolumn = new Column_Info;
  227. Constraint_Info* constraint;
  228. pcolumn->ID = id;
  229. strcpy(pcolumn->ColumnName,ptr->ColumnName);
  230. pcolumn->ColType = ptr->ColType;
  231. pcolumn->IsPrimary = ptr->IsPrimary;
  232. pcolumn->ColumnPtr.next = NULL;
  233. if(ptr->IsPrimary == true)
  234. pcolumn->IsNull = 0;
  235. else if (ptr->IsNull == true)
  236. pcolumn->IsNull = 1;
  237. else if (ptr->IsNull == false)
  238. pcolumn->IsNull = 0;
  239. switch(pcolumn->ColType){
  240. case I:   
  241. pcolumn->RequiredLength = sizeof(int);
  242. pcolumn->StoredLength = pcolumn->RequiredLength;
  243. break;
  244. case F:   
  245. pcolumn->RequiredLength = sizeof(float);
  246. pcolumn->StoredLength = pcolumn->RequiredLength;
  247. break;
  248. case C:   
  249. pcolumn->RequiredLength = ptr->length;
  250. pcolumn->StoredLength = pcolumn->RequiredLength+1;
  251. break;
  252. default:
  253. throw 1010; //error 1010: 错误的数据类型
  254. }
  255. //是否有约束
  256. bool flag=false;
  257. if((ptr->ColType==I) && (ptr->min.IntValue<=ptr->max.IntValue))
  258. flag=true;
  259. else if((ptr->ColType==F) && (ptr->min.FloatValue<=ptr->max.FloatValue))
  260. flag=true;
  261. else if((ptr->ColType==C) && (strcmp(ptr->min.pCharValue,ptr->max.pCharValue)<=0))
  262. flag=true;
  263. if(flag){ //有约束,连接入节点
  264. constraint = new Constraint_Info;
  265. constraint->OperType = ptr->OperType;
  266. switch(pcolumn->ColType){
  267. case I:
  268. constraint->min.IntValue = ptr->min.IntValue;
  269. constraint->max.IntValue = ptr->max.IntValue;
  270. break;
  271. case F:
  272. constraint->min.FloatValue = ptr->min.FloatValue;
  273. constraint->max.FloatValue = ptr->max.FloatValue;
  274. break;
  275. case C:
  276. strcpy(constraint->min.CharValue, ptr->min.pCharValue);
  277. strcpy(constraint->max.CharValue, ptr->max.pCharValue);
  278. break;
  279. }
  280. pcolumn->ConstraintPtr.constraint = constraint;
  281. }
  282. else //没约束,指针设为空
  283. pcolumn->ConstraintPtr.constraint = NULL;
  284. return pcolumn;
  285. }
  286. //创建
  287. void HCatalog::Create(TB_Create_Info& info, char* KeyInfo)
  288. {
  289. Create_Column* ptr;
  290. ptr = info.head;
  291. Table_Info TableNode;
  292. strcpy(TableNode.TableName,info.TableName);
  293. TableNode.TotalColumn = info.TotalColumn;
  294. Column_Info* column1;
  295. Column_Info* head;
  296. Column_Info* column2;
  297. int count=0; //主键数
  298. int length=0; //记录长度
  299. int j=0;
  300.     int keylen=0;
  301. for(int i=0 ;ptr!=NULL ;ptr=ptr->next,i++){
  302. column1 = this->Form_ListNode(ptr,i);   
  303. length += column1->StoredLength;        
  304. if(column1->IsPrimary == 1){
  305. count++;      
  306. keylen += column1->StoredLength;  
  307.             if(column1->ColType == C)
  308. j += sprintf(KeyInfo+j,"c%d",column1->RequiredLength);
  309. else if(column1->ColType == F)
  310. j += sprintf(KeyInfo+j,"f");
  311. else 
  312. j += sprintf(KeyInfo+j,"i");
  313. }
  314. if(i == 0)
  315. head = column1;
  316. else
  317. column2->ColumnPtr.next = column1;
  318. column2 = column1;
  319. }
  320.     if(( BTreeNodeSize - 2*sizeof(_F_FileAddr) - sizeof(bool) - sizeof(int) ) 
  321.                        / ( sizeof(_F_FileAddr) + keylen) < 4)
  322.                        throw 1034;      //Error 1034: Primary Key is too long!
  323. TableNode.KeyPtr.Key = head;
  324. TableNode.KeyAttrNum = count;
  325. TableNode.RecordLength = length;
  326.     //定位到当前表
  327. char temp[289];
  328. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  329. _M_File CurrentTable = Buffer[temp];
  330. //写入文件
  331. _F_FileAddr pFile,pFile1,pFile2;
  332. pFile = CurrentTable.GetCataPoint();
  333. pFile1 = MemWrite((void*)&TableNode,sizeof(Table_Info),&pFile);
  334. column1 = head;
  335. if(column1 != NULL){
  336. ((Table_Info*)pFile.MemAddr())->KeyPtr.FileKey = pFile1; //移动文件指针
  337. pFile = MemWrite((void*)column1,sizeof(Column_Info),&pFile1);
  338. ((Column_Info*)pFile1.MemAddr())->ColumnPtr.FileNext.Initialize();
  339. pFile2 = pFile1;
  340. if(column1->ConstraintPtr.constraint != NULL){
  341. ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint = pFile;
  342. pFile1 = MemWrite((void*)column1->ConstraintPtr.constraint,sizeof(Constraint_Info),&pFile);
  343. }
  344. else{
  345. ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint.Initialize();
  346. pFile1 = pFile;   //记录上次的写入点 
  347. }
  348. column1 = column1->ColumnPtr.next;
  349. }
  350. while(column1 != NULL){
  351. ((Column_Info*)pFile2.MemAddr())->ColumnPtr.FileNext = pFile1;
  352. pFile = MemWrite((void*)column1,sizeof(Column_Info),&pFile1);
  353. ((Column_Info*)pFile1.MemAddr())->ColumnPtr.FileNext.Initialize();
  354. pFile2 = pFile1;
  355. if(column1->ConstraintPtr.constraint != NULL){
  356. ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint = pFile;
  357. pFile1 = MemWrite((void*)column1->ConstraintPtr.constraint,sizeof(Constraint_Info),&pFile);
  358.     }
  359. else{
  360. ((Column_Info*)pFile1.MemAddr())->ConstraintPtr.FileConstraint.Initialize();
  361. pFile1 = pFile;   
  362. }
  363. column1 = column1->ColumnPtr.next;
  364. }
  365. //删除列表
  366. column1 = head;
  367. column2 = column1->ColumnPtr.next;
  368. while(column1 != NULL){
  369. if(column1->ConstraintPtr.constraint != NULL)
  370. delete column1->ConstraintPtr.constraint;
  371. delete column1;
  372. column1 = column2;
  373. if(column1 != NULL)
  374. column2 = column1->ColumnPtr.next;
  375. }
  376. return;
  377. }
  378. //选择
  379. void HCatalog::Select(TB_Select_Info& info,Condition_Info& index,Select_Rec_Info& record)
  380. {
  381.     char temp[289];
  382. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  383. _M_File CurrentTable = Buffer[temp];
  384. Select_Column* pcolumn = info.ColumnHead;
  385. Select_Condition* pcondition = info.ConditionHead;
  386. //检查约束条件列是否存在
  387.     Column_Info* column;
  388. int l=0;   
  389. if(strcmp(pcolumn->ColumnName,"*") != 0){
  390.         for(; pcolumn!=NULL; pcolumn=pcolumn->next){
  391. if(!this->Exist_Column(pcolumn->ColumnName))
  392. throw 1008; //Error1008: Column demanded has not existed yet!  
  393.             column = this->Find_Column(pcolumn->ColumnName);
  394.             l += column->StoredLength;
  395.         }
  396.     }
  397.     _F_FileAddr pFile = CurrentTable.GetCataPoint();
  398. Table_Info* TbInfo = (Table_Info*)pFile.MemAddr();
  399.     if((info.ConditionNum!=0) && (TbInfo->KeyAttrNum!=info.ConditionNum))
  400. throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
  401. for(; pcondition!=NULL; pcondition=pcondition->next){
  402. column = this->Find_Column(pcondition->PrimaryKey); //find the demanded column
  403. if(column == NULL)
  404. throw 1007; //Error1007: Primary Key demanded has not existed yet!
  405. if(!this->Check_Key(column))
  406. throw 1011; //Error1011: Not a select based on primary key!
  407. if(!this->Check_Type(column,pcondition->ColType))
  408. throw 1012; //Error1012: Type mismatch!
  409.         if(pcondition->OperType == BETWEEN){
  410.             if(!Check_Key_Validation(pcondition->ColType,&(pcondition->max),&(pcondition->min)))
  411.                 throw 1020;       //Error1020: Result is null!
  412.         }
  413.         if(pcondition->ColType == C){
  414. switch(pcondition->OperType){
  415. case L:
  416. case LE:
  417. if(!Check_Length(column,&(pcondition->max)))
  418. throw 1013; //Error1013: Length Invalid for type char!
  419. else 
  420. break;
  421. case B:
  422. case BE:
  423. if(!Check_Length(column,&(pcondition->min)))
  424. throw 1013; //Error1013: Length Invalid for type char!
  425. else
  426. break;
  427. case BETWEEN:
  428. case E:
  429. case NE:
  430. if(!Check_Length(column,&(pcondition->max)) || !Check_Length(column,&(pcondition->min)))
  431. throw 1013; //Error1013: Length Invalid for type char!
  432. else 
  433. break;
  434. default:
  435. throw 1009; //Error1009: Unknown relation operator!
  436. }
  437. }
  438. }
  439. Column_Info* head;
  440. head = (Column_Info*)TbInfo->KeyPtr.FileKey.MemAddr();
  441. column = head;
  442. pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
  443. pminkey = new Key_Attr;
  444. pkey1 = pminkey;  
  445. pkey3 = pkey1;    
  446. pmaxkey = new Key_Attr;
  447. pkey2 = pmaxkey; 
  448. pkey4 = pkey2;    
  449. int count = TbInfo->KeyAttrNum;
  450.     if(info.ConditionHead != NULL){
  451.       while((column!=NULL) && (count!=0)){
  452.         if(column->IsPrimary == 1){
  453. //找寻选择条件
  454.             for(pcondition = info.ConditionHead; pcondition!=NULL; pcondition=pcondition->next){
  455.                 if(strcmp(pcondition->PrimaryKey,column->ColumnName)==0){  
  456. switch (pcondition->ColType){
  457. case I:
  458. pkey1->value.IntValue = pcondition->min.IntValue;
  459. pkey2->value.IntValue = pcondition->max.IntValue;
  460. break;
  461. case F:
  462. pkey1->value.FloatValue = pcondition->min.FloatValue;
  463. pkey2->value.FloatValue = pcondition->max.FloatValue;
  464. break;
  465. case C:
  466. pkey1->value.pCharValue = pcondition->min.pCharValue;
  467. pkey2->value.pCharValue = pcondition->max.pCharValue;
  468. break;
  469. default:
  470. throw 1010; //Error1010: 错误的数据类型
  471. }
  472. break;
  473. }
  474. }
  475. pkey3 = pkey1;  
  476. pkey4 = pkey2;  
  477. pkey1->next = new Key_Attr; 
  478. pkey1 = pkey1->next;   
  479. pkey2->next = new Key_Attr; 
  480. pkey2 = pkey2->next;   
  481. count--;  
  482. }
  483. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  484. }
  485.   
  486. pkey3->next = NULL;
  487. pkey4->next = NULL;
  488. delete pkey1;
  489. delete pkey2;
  490. index.max = pmaxkey;
  491. index.min = pminkey;
  492.     index.OperType = info.ConditionHead->OperType;
  493.   }
  494.   
  495.   else{
  496.     index.max = NULL;
  497.     index.min = NULL;
  498.     index.OperType = ALL;
  499.   }
  500. //为record准备的数据
  501. Select_Cell* cell = new Select_Cell;
  502. Select_Cell* cell1 = cell;
  503. record.head = cell;
  504. pcolumn = info.ColumnHead;
  505. column = head;
  506. int len=0;    
  507.     if(strcmp(pcolumn->ColumnName,"*")!=0){
  508.         while(pcolumn != NULL){
  509. for(column = head; column!=NULL; len+=column->StoredLength,column=(Column_Info*)column->ColumnPtr.FileNext.MemAddr()){
  510. if(strcmp(pcolumn->ColumnName,column->ColumnName)==0){
  511. strcpy(cell->ColumnName,column->ColumnName);
  512. cell->ColType = column->ColType;
  513. cell->ColLength = column->StoredLength;
  514. cell->PriorLength = len;
  515. if(pcolumn->next != NULL){
  516. cell1 = cell;
  517. cell->next = new Select_Cell;
  518. cell = cell->next;
  519. }
  520. break;
  521. }
  522. }
  523. pcolumn = pcolumn->next;
  524. len = 0;
  525. }
  526. record.ColumnNum = info.ColumnNum;
  527.         record.RecordLength = l;
  528. }
  529.     else{
  530. while(column!=NULL){
  531. strcpy(cell->ColumnName,column->ColumnName);
  532. cell->ColType = column->ColType;
  533. cell->ColLength = column->StoredLength;
  534. cell->PriorLength = len;
  535. len += column->StoredLength;
  536. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  537. if(column!=NULL){
  538. cell->next = new Select_Cell;
  539. cell = cell->next;
  540. }
  541. }
  542. record.ColumnNum = TbInfo->TotalColumn;
  543.         record.RecordLength = TbInfo->RecordLength;
  544. }
  545. return;
  546. }
  547. //插入
  548. void HCatalog::Insert(TB_Insert_Info& info, Key_Attr& index, Rec_Info& record)
  549. {
  550.     char temp[289];
  551. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  552. _M_File CurrentTable = Buffer[temp];
  553. Insert_Column *raw;
  554. raw = info.head;
  555. Column_Info* column = NULL;
  556. Column_Info* head;
  557. Table_Info* table;
  558. table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
  559. head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
  560. int count=0;
  561. //检查插入的信息
  562.     while (raw != NULL){
  563.         if(strcmp(raw->ColumnName,"*") != 0){
  564. if(!Exist_Column(raw->ColumnName))
  565. throw 1008; //Error1008: Column demanded has not existed yet!
  566. column = this->Find_Column(raw->ColumnName);
  567. if(Check_Key(column))
  568. count++;
  569.         else{
  570. if(column == NULL)
  571. column = head;
  572. else
  573. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  574. }
  575. if(!Check_Type(column,raw->ColType))
  576. throw 1012; //Error1012: Type mismatch!
  577. if(!Check_Length(column,&(raw->value)))
  578. throw 1013; //Error1013: Length Invalid for type char!
  579. if(!Check_Value(column,&(raw->value)))
  580. throw 1014; //Error1014: Invalid Value!
  581. raw = raw->next;
  582. }
  583. raw = info.head;
  584. if(strcmp(raw->ColumnName,"*") != 0){
  585. if(count != table->KeyAttrNum)
  586. throw 1015; //Error1015: Value of Primary Key can not be null!
  587. }
  588. else{
  589. if(info.ColumnNum != table->TotalColumn)
  590. throw 1016; //Error1016: Inadequate value for every column! 
  591. count = table->KeyAttrNum;
  592. }
  593. column = head;
  594.     if(strcmp(info.head->ColumnName,"*") != 0){
  595. while(column != NULL){
  596. if(column->IsNull == 0){
  597. for(raw=info.head; raw!=NULL; raw=raw->next)
  598. if(strcmp(raw->ColumnName,column->ColumnName) == 0)
  599. break;
  600. if(raw == NULL)
  601. throw 1017; //Error1017: No value for some not null column!
  602. }
  603. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  604. }
  605. }
  606. //为index insertion 的信息
  607. Key_Attr* key = &index;
  608. column = head;
  609. raw = info.head;
  610. while(count!=0 && column!=NULL){
  611.         if(strcmp(info.head->ColumnName,"*") != 0){
  612.             if(column->IsPrimary == 0){   
  613. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  614. continue;
  615. }
  616. for(raw=info.head; raw!=NULL; raw=raw->next){
  617.                 if(strcmp(raw->ColumnName,column->ColumnName)==0){
  618. switch(column->ColType){
  619. case I:
  620. key->value.IntValue = raw->value.IntValue;
  621. break;
  622. case F:
  623. key->value.FloatValue = raw->value.FloatValue;
  624. break;
  625. case C:
  626. key->value.pCharValue = raw->value.pCharValue;
  627. break;
  628. default:
  629. throw 1010; //Error1010: 错误的数据类型
  630. }
  631. count--;
  632. break;
  633. }
  634. }
  635. }
  636. //i插入所有列
  637.         else{
  638.             if(column->IsPrimary == 0){
  639. raw = raw->next;
  640. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  641. continue;
  642. }
  643. switch(column->ColType){
  644. case I:
  645. key->value.IntValue = raw->value.IntValue;
  646. break;
  647. case F:
  648. key->value.FloatValue = raw->value.FloatValue;
  649. break;
  650. case C:
  651. key->value.pCharValue = raw->value.pCharValue;
  652. break;
  653. default:
  654. throw 1010; //Error1010: 错误的数据类型
  655. }
  656. count--;
  657. raw = raw->next;
  658. }
  659. if(count!=0){
  660. key->next = new Key_Attr;
  661. key = key->next;
  662. }
  663. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  664. }
  665. //为 record insert的信息
  666. Cell_Info* cell = new Cell_Info;
  667. record.ColNum = table->TotalColumn;
  668. record.head = cell;
  669.     record.RecordLength = table->RecordLength;
  670. column = head;
  671. raw = info.head;
  672. int len=0;  
  673. if(strcmp(info.head->ColumnName,"*") == 0){
  674. while(column != NULL){
  675. cell->ColLength = column->StoredLength;
  676. cell->ColType = column->ColType;
  677. cell->PriorLength = len;
  678. switch(column->ColType){
  679. case I:
  680. cell->value.IntValue = raw->value.IntValue;
  681. break;
  682. case F:
  683. cell->value.FloatValue = raw->value.FloatValue;
  684. break;
  685. case C:
  686. cell->value.pCharValue = raw->value.pCharValue;
  687. break;
  688. default:
  689. throw 1010; //Error1010: 错误的数据类型
  690. }
  691. len += column->StoredLength;
  692. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  693. raw = raw->next;
  694. if(column != NULL){
  695. cell->next = new Cell_Info;
  696. cell = cell->next;
  697. }
  698. }
  699. }
  700. else{
  701. column = head;
  702. len = 0;
  703. while(column!=NULL){
  704. raw = info.head;
  705. while(raw != NULL){
  706. if(strcmp(raw->ColumnName,column->ColumnName) == 0){
  707. cell->ColLength = column->StoredLength;
  708. cell->ColType = column->ColType;
  709. cell->PriorLength = len;
  710. switch(column->ColType){
  711. case I:
  712. cell->value.IntValue = raw->value.IntValue;
  713. break;
  714. case F:
  715. cell->value.FloatValue = raw->value.FloatValue;
  716. break;
  717. case C:
  718. cell->value.pCharValue = raw->value.pCharValue;
  719. break;
  720. default:
  721. throw 1010; //Error1010: 错误的数据类型
  722. }
  723. break;
  724. }
  725. raw = raw->next;
  726. }
  727. if(raw == NULL){
  728. cell->ColLength = column->StoredLength;
  729. cell->ColType = column->ColType;
  730. cell->PriorLength = len;
  731. switch(column->ColType){
  732. case I:
  733. cell->value.IntValue = 0;
  734. break;
  735. case F:
  736. cell->value.FloatValue = 0.0;
  737. break;
  738. case C:
  739. cell->value.pCharValue = NULL;
  740. break;
  741. default:
  742. throw 1010; //Error1010: 错误的数据类型
  743. }
  744. }
  745. len += column->StoredLength;
  746. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  747. if(column != NULL){
  748. cell->next = new Cell_Info;
  749. cell = cell->next;
  750. }
  751. }
  752. }
  753. return;
  754. }
  755. //更新
  756. void HCatalog::Update(TB_Update_Info& info, Condition_Info& index, Rec_Info& record)
  757. {
  758.     char temp[289];
  759. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  760. _M_File CurrentTable = Buffer[temp];
  761. Update_Column* col;
  762. Update_Condition* cond;
  763. col = info.ColumnHead;
  764. cond = info.ConditionHead;
  765. cout<<"text1";
  766. Column_Info* column;
  767. while(col != NULL){
  768. if(!Exist_Column(col->ColumnName))
  769. throw 1008; //Error1008: Column demanded has not existed yet!
  770. column = Find_Column(col->ColumnName);
  771. if(column->IsPrimary == 1)
  772. throw 1018; //Error1018: Update denied on primary key!
  773. if(!Check_Type(column,col->ColType))
  774. throw 1012; //Error1012: Type mismatch!
  775. if(col->ColType == C)
  776. if(!Check_Length(column,&col->value))
  777. throw 1013; //Error1013: Length Invalid for type char!
  778. if(!Check_Value(column,&col->value))
  779. throw 1014; //Error1014: Invalid Value!
  780. col = col->next;
  781. }
  782. cout<<"text1";
  783. Table_Info* table;
  784. table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
  785. if(info.ConditionNum != table->KeyAttrNum)
  786. throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
  787. for(; cond!=NULL; cond=cond->next){
  788. column = this->Find_Column(cond->PrimaryKey);
  789. if(column == NULL)
  790. throw 1007; //Error1007: Primary Key demanded has not existed yet!
  791. if(!this->Check_Key(column))
  792. throw 1011; //Error1011: Not a select based on primary key!
  793. if(!this->Check_Type(column,cond->ColType))
  794. throw 1012; //Error1012: Type mismatch!
  795.     if(cond->OperType == BETWEEN){
  796.       if(!Check_Key_Validation(cond->ColType,&(cond->max),&(cond->min)))
  797.         throw 1020;       //Error1020: Result is null!
  798.     }
  799. if(cond->ColType == C){ //check if the length is correct if the column is char type
  800. switch(cond->OperType){
  801. case L:
  802. case LE:
  803. if(!Check_Length(column,&(cond->max)))
  804. throw 1013; //Error1013: Length Invalid for type char!
  805. else 
  806. break;
  807. case B:
  808. case BE:
  809. if(!Check_Length(column,&(cond->min)))
  810. throw 1013; //Error1013: Length Invalid for type char!
  811. else
  812. break;
  813. case E:
  814. case NE:
  815. case BETWEEN:
  816. if(!Check_Length(column,&(cond->max)) || !Check_Length(column,&(cond->min)))
  817. throw 1013; //Error1013: Length Invalid for type char!
  818. else 
  819. break;
  820. default:
  821. throw 1009; //Error1009: Unknown relation operator!
  822. }
  823. }
  824. }
  825. cout<<"text1";
  826. //为 index select的信息
  827. Column_Info* head; 
  828. head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
  829. column = head;
  830. int count = table->KeyAttrNum;
  831. pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
  832. pminkey = new Key_Attr;
  833. pkey1 = pminkey;  
  834. pkey3 = pkey1;   
  835. pmaxkey = new Key_Attr;
  836. pkey2 = pmaxkey;  
  837. pkey4 = pkey2;    
  838.     while((column!=NULL) && (count!=0)){
  839.         if(column->IsPrimary == 1){
  840.             for(cond = info.ConditionHead; cond!=NULL; cond=cond->next){
  841. if(strcmp(cond->PrimaryKey,column->ColumnName)==0){  
  842. switch (cond->ColType){
  843. case I:
  844. pkey1->value.IntValue = cond->min.IntValue;
  845. pkey2->value.IntValue = cond->max.IntValue;
  846. break;
  847. case F:
  848. pkey1->value.FloatValue = cond->min.FloatValue;
  849. pkey2->value.FloatValue = cond->max.FloatValue;
  850. break;
  851. case C:
  852. pkey1->value.pCharValue = cond->min.pCharValue;
  853. pkey2->value.pCharValue = cond->max.pCharValue;
  854. break;
  855. default:
  856. throw 1010; //Error1010: 错误的数据类型
  857. }
  858. break;
  859. }
  860. }
  861. if(cond==NULL)
  862. switch(column->ColType){
  863. case I:
  864. pkey1->value.IntValue = 1;
  865. pkey2->value.IntValue = 0;
  866. break;
  867. case F:
  868. pkey1->value.FloatValue = 1.0;
  869. pkey2->value.FloatValue = 0.0;
  870. break;
  871. case C:
  872. strcpy(pkey1->value.pCharValue,"b");
  873. strcpy(pkey2->value.pCharValue,"a");
  874. break;
  875. default:
  876. throw 1010; //Error1010: 错误的数据类型
  877. }
  878. pkey3 = pkey1;
  879. pkey4 = pkey2;
  880. pkey1->next = new Key_Attr;
  881. pkey1 = pkey1->next;
  882. pkey2->next = new Key_Attr;
  883. pkey2 = pkey2->next;
  884. count--;
  885. }
  886. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  887. }
  888. pkey3->next = NULL;
  889. pkey4->next = NULL;
  890. delete pkey1;
  891. delete pkey2;
  892. index.max = pmaxkey;
  893. index.min = pminkey;
  894. if(info.ConditionHead == NULL)
  895.         index.OperType = ALL;
  896.     else
  897.         index.OperType = info.ConditionHead->OperType;
  898. cout<<"text1";
  899. //为 record update的信息
  900. Cell_Info* cell = new Cell_Info;
  901. Cell_Info* cell1 = cell;
  902. record.head = cell;
  903. record.ColNum = info.ColumnNum;
  904.     record.RecordLength = table->RecordLength;
  905. column = head;
  906. int len = 0;    
  907. while(column!=NULL){
  908. col = info.ColumnHead;
  909.         for(;col!=NULL;col=col->next){
  910. if(strcmp(col->ColumnName,column->ColumnName)==0){
  911. cell->ColLength = column->StoredLength;
  912. cell->ColType = column->ColType;
  913. cell->PriorLength = len;
  914. switch(column->ColType){
  915. case I:
  916. cell->value.IntValue = col->value.IntValue;
  917. break;
  918. case F:
  919. cell->value.FloatValue = col->value.FloatValue;
  920. break;
  921. case C:
  922. cell->value.pCharValue = col->value.pCharValue;
  923. break;
  924. default:
  925. throw 1010; //Error1010:错误的数据类型
  926. }
  927. cell->next = new Cell_Info;
  928. cell1 = cell;
  929. cell = cell->next;
  930. break;
  931. }
  932. }
  933. len += column->StoredLength;
  934. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  935. }
  936. cell1->next = NULL;
  937. delete cell;
  938. return;
  939. }
  940. //删除
  941. void HCatalog::Delete(TB_Delete_Info& info, Condition_Info& index)
  942. {
  943.     char temp[289];
  944. sprintf(temp,"%s%s.dbf",CurLocation,CurRelationName);
  945. _M_File CurrentTable = Buffer[temp];
  946. Delete_Condition* cond=info.head;
  947. Column_Info* column;
  948. Table_Info* table;
  949. table = (Table_Info*)CurrentTable.GetCataPoint().MemAddr();
  950.     if(info.ConditionNum != table->KeyAttrNum)
  951. throw 1019; //Error1019: Information Inadequate for selection(Every attribute should be included)!
  952. for(; cond!=NULL; cond=cond->next){
  953. column = this->Find_Column(cond->PrimaryKey);
  954. if(column == NULL)
  955. throw 1007; //Error1007: Primary Key demanded has not existed yet!
  956. if(!this->Check_Key(column))
  957. throw 1011; //Error1011: Not a select based on primary key!
  958. if(!this->Check_Type(column,cond->ColType))
  959. throw 1012; //Error1012: Type mismatch!
  960.         if(cond->OperType == BETWEEN){
  961.             if(!Check_Key_Validation(cond->ColType,&(cond->max),&(cond->min)))
  962.                 throw 1020;       //Error1020: Result is null!
  963.         }
  964. if(cond->ColType == C){
  965. switch(cond->OperType){
  966. case L:
  967. case LE:
  968. if(!Check_Length(column,&(cond->max)))
  969. throw 1013; //Error1013: Length Invalid for type char!
  970. else 
  971. break;
  972. case B:
  973. case BE:
  974. if(!Check_Length(column,&(cond->min)))
  975. throw 1013; //Error1013: Length Invalid for type char!
  976. else
  977. break;
  978. case E:
  979. case NE:
  980. case BETWEEN:
  981. if(!Check_Length(column,&(cond->max)) || !Check_Length(column,&(cond->min)))
  982. throw 1013; //Error1013: Length Invalid for type char!
  983. else 
  984. break;
  985. default:
  986. throw 1009; //Error1009: 错误的关系运算符
  987. }
  988. }
  989. }
  990. //给index的信息
  991.     Column_Info* head; 
  992. head = (Column_Info*)table->KeyPtr.FileKey.MemAddr();
  993. column = head;
  994. int count = table->KeyAttrNum;
  995. pKey_Attr pkey1,pkey2,pminkey,pmaxkey,pkey3,pkey4;
  996. pminkey = new Key_Attr;
  997. pkey1 = pminkey;   
  998. pkey3 = pkey1;     
  999. pmaxkey = new Key_Attr;
  1000. pkey2 = pmaxkey;    
  1001. pkey4 = pkey2;     
  1002.     while((column!=NULL) && (count!=0)){
  1003.         if(column->IsPrimary == 1){
  1004.             for(cond = info.head; cond!=NULL; cond=cond->next){
  1005. if(strcmp(cond->PrimaryKey,column->ColumnName)==0){  
  1006. switch (cond->ColType){
  1007. case I:
  1008. pkey1->value.IntValue = cond->min.IntValue;
  1009. pkey2->value.IntValue = cond->max.IntValue;
  1010. break;
  1011. case F:
  1012. pkey1->value.FloatValue = cond->min.FloatValue;
  1013. pkey2->value.FloatValue = cond->max.FloatValue;
  1014. break;
  1015. case C:
  1016. pkey1->value.pCharValue = cond->min.pCharValue;
  1017. pkey2->value.pCharValue = cond->max.pCharValue;
  1018. break;
  1019. default:
  1020. throw 1010; //Error1010: 错误的数据类型
  1021. }
  1022. break;
  1023. }
  1024. }
  1025.             if(cond==NULL)
  1026. switch(column->ColType){
  1027. case I:
  1028. pkey1->value.IntValue = 1;
  1029. pkey2->value.IntValue = 0;
  1030. break;
  1031. case F:
  1032. pkey1->value.FloatValue = 1.0;
  1033. pkey2->value.FloatValue = 0.0;
  1034. break;
  1035. case C:
  1036. strcpy(pkey1->value.pCharValue,"b");
  1037. strcpy(pkey2->value.pCharValue,"a");
  1038. break;
  1039. default:
  1040. throw 1010; //Error1010: 错误的数据类型
  1041. }
  1042. pkey3 = pkey1;
  1043. pkey4 = pkey2;
  1044. pkey1->next = new Key_Attr;
  1045. pkey1 = pkey1->next;
  1046. pkey2->next = new Key_Attr;
  1047. pkey2 = pkey2->next;
  1048. count--;
  1049. }
  1050. column = (Column_Info*)column->ColumnPtr.FileNext.MemAddr();
  1051. }
  1052. pkey3->next = NULL;
  1053. pkey4->next = NULL;
  1054. delete pkey1;
  1055. delete pkey2;
  1056. index.max = pmaxkey;
  1057. index.min = pminkey;
  1058. if(info.head == NULL)
  1059.         index.OperType = ALL;
  1060.     else
  1061.         index.OperType = info.head->OperType;
  1062. return;
  1063. }