-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSYS_Manager.cpp
More file actions
1759 lines (1529 loc) · 48.9 KB
/
SYS_Manager.cpp
File metadata and controls
1759 lines (1529 loc) · 48.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "EditArea.h"
#include "SYS_Manager.h"
#include "QU_Manager.h"
#include <iostream>
DB_INFO dbInfo;
RC execute(char* sql, CEditArea* editArea) {
RC rc;
sqlstr* sql_str = get_sqlstr();
rc = parse(sql, sql_str);//只有两种返回结果SUCCESS和SQL_SYNTAX
if (rc == SUCCESS) {
switch (sql_str->flag) {
case 1: {
//判断SQL语句为select语句
SelResult res;
Init_Result(&res);
//将查询结果处理一下,整理成下面这种形式
//调用editArea->ShowSelResult(col_num,row_num,fields,rows);
if ((rc = Query(sql, &res))) {
return rc;
}
int col_num = res.col_num;//列
int row_num = 0;//行
SelResult* cur_res = &res;
while (cur_res) {//所有节点的记录数之和
row_num += cur_res->row_num;
cur_res = cur_res->next_res;
}
char** fields = new char* [20];//各字段名称
for (int i = 0; i < col_num; i++) {
fields[i] = new char[20];
memset(fields[i], 0, 20);
memcpy(fields[i], res.fields[i], 20);
}
cur_res = &res;
char*** rows = new char** [row_num];
for (int i = 0; i < row_num; i++) {
rows[i] = new char* [col_num];//存放一条记录
for (int j = 0; j < col_num; j++) {
rows[i][j] = new char[20];//一条记录的一个字段
memset(rows[i][j], 0, 20);
memcpy(rows[i][j], cur_res->res[i][j], 20);
}
if (i == 99)
cur_res = cur_res->next_res;//每个链表节点最多记录100条记录
}
editArea->ShowSelResult(col_num, row_num, fields, rows);
for (int i = 0; i < col_num; i++) {
delete[] fields[i];
}
delete[] fields;
Destory_Result(&res);
return SELECT_SUCCESS;
}
case 2:
//判断SQL语句为insert语句
if((rc = Insert(sql_str->sstr.ins.relName, sql_str->sstr.ins.nValues, sql_str->sstr.ins.values))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 3:
//判断SQL语句为update语句
if ((rc = Update(sql_str->sstr.upd.relName, sql_str->sstr.upd.attrName, &sql_str->sstr.upd.value,
sql_str->sstr.upd.nConditions, sql_str->sstr.upd.conditions))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 4:
//判断SQL语句为delete语句
if ((rc = Delete(sql_str->sstr.del.relName, sql_str->sstr.del.nConditions, sql_str->sstr.del.conditions))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 5:
//判断SQL语句为createTable语句
if ((rc = CreateTable(sql_str->sstr.cret.relName, sql_str->sstr.cret.attrCount, sql_str->sstr.cret.attributes))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 6:
//判断SQL语句为dropTable语句
if ((rc = DropTable(sql_str->sstr.drt.relName))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 7:
//判断SQL语句为createIndex语句
if ((rc = CreateIndex(sql_str->sstr.crei.indexName, sql_str->sstr.crei.relName, sql_str->sstr.crei.attrName))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 8:
//判断SQL语句为dropIndex语句
if ((rc = DropIndex(sql_str->sstr.dri.indexName))) {
return SQL_SYNTAX;
}
return SUCCESS;
case 9:
//判断为help语句,可以给出帮助提示
return SUCCESS;
case 10:
//判断为exit语句,可以由此进行退出操作
AfxGetMainWnd()->SendMessage(WM_CLOSE);
return SUCCESS;
}
}
else {
AfxMessageBox(sql_str->sstr.errors);//弹出警告框,sql语句词法解析错误信息
return rc;
}
return SUCCESS;
}
void ExecuteAndMessage(char* sql, CEditArea* editArea) {//根据执行的语句类型在界面上显示执行结果。此函数需修改
RC rc = execute(sql, editArea);
int row_num = 0;
char** messages = NULL;
switch (rc) {
case SUCCESS:
row_num = 1;
messages = new char* [row_num];
messages[0] = "操作成功";
editArea->ShowMessage(row_num, messages);
delete[] messages;
break;
case SQL_SYNTAX:
row_num = 1;
messages = new char* [row_num];
messages[0] = "有语法错误";
editArea->ShowMessage(row_num, messages);
delete[] messages;
break;
case SELECT_SUCCESS:
break;
default:
row_num = 1;
messages = new char* [row_num];
messages[0] = "功能未实现";
editArea->ShowMessage(row_num, messages);
delete[] messages;
break;
}
}
//
//目的:在路径 dbPath 下创建一个名为 dbName 的空库,生成相应的系统文件。
//1. 检查dbpath和dbname的合法性。
//2. 判断dbpath以及dbname是否和dbInfo中保存相同。如果不同则调用CloseDb。如果相同则返回。
//3. 向OS申请在dbpath路径创建文件夹。
//4. 创建SYSTABLES文件和SYSCOLUMNS文件。
RC CreateDB(char* dbpath, char* dbname) {
RC rc;
//1. 检查dbpath和dbname的合法性。
if (dbpath == NULL || dbname == NULL)
return DB_NAME_ILLEGAL;
//2. 判断dbpath以及dbname是否和dbInfo中保存相同。如果不同则调用CloseDb。如果相同则返回。
std::string dbPath = dbpath;
dbPath = dbPath + "\\" + dbname;
if ( dbInfo.curDbName.size() && dbInfo.curDbName.compare(dbPath) != 0 ) {
if ((rc = CloseDB()))
return rc;
}
if (dbInfo.curDbName.size() && dbInfo.curDbName.compare(dbPath) == 0) {
return SUCCESS;
}
//3. 向OS申请在dbpath路径创建文件夹。
if (CreateDirectory(dbPath.c_str(), NULL)) {
if (SetCurrentDirectory((char*)dbPath.c_str())) {
//4. 创建SYSTABLES文件和SYSCOLUMNS文件
std::string sysTablePath = TABLE_META_NAME;
std::string sysColumnsPath = COLUMN_META_NAME;
if ((rc = RM_CreateFile((char*)(sysTablePath.c_str()), SIZE_SYS_TABLE)) ||
(rc = RM_CreateFile((char*)(sysColumnsPath.c_str()), SIZE_SYS_COLUMNS)))
return rc;
return SUCCESS;
}
return OS_FAIL;
}
return OS_FAIL;
}
//
//目的:删除数据库对应的目录以及目录下的所有文件
// 默认不存在子文件夹,所以不进行递归处理
//1. 判断dbname是否为空,或者dbInfo.path下是否存在dbname。如果不存在则报错。
//2. 判断删除的是否是当前DB。如果则要关闭当前目录.
//3. 搜索得到dbname目录下的所有文件名并删除,注意要跳过.和..目录
//4. 删除dbname目录
RC DropDB(char* dbname) {
//1. 判断dbname是否为空,或者是否存在dbname。如果不存在则报错。
std::string dbPath = dbname;
if (dbname == NULL || access(dbPath.c_str(), 0) == -1)
return DB_NOT_EXIST;
//2. 判断删除的是否是当前DB
RC rc;
if (dbInfo.curDbName.compare(dbname) == 0) {
//关闭当前目录
if ((rc = CloseDB()))
return rc;
}
HANDLE hFile;
WIN32_FIND_DATA pNextInfo;
dbPath += "\\*";
hFile = FindFirstFile(dbPath.c_str(), &pNextInfo);
if (hFile == INVALID_HANDLE_VALUE)
return OS_FAIL;
//3. 遍历删除文件夹下的所有文件
std::string fileName;
while (FindNextFile(hFile, &pNextInfo)) {
if (pNextInfo.cFileName[0] == '.')//跳过.和..目录
continue;
fileName = dbname;
fileName = fileName + "\\" + pNextInfo.cFileName;
if (!DeleteFile(fileName.c_str()))
return OS_FAIL;
}
//4. 删除dbname目录
std::string dbName =dbname;
if (!RemoveDirectory(dbName.c_str())) {
int i = GetLastError();
return OS_FAIL;
}
return SUCCESS;
}
//
//目的:改变系统的当前数据库为 dbName 对应的文件夹中的数据库
//1. 检查dbname是否为空。已经能否达到。
//2. 如果当前打开了db.
// 2.1 如果打开的目录和dbname相同,则返回。
// 2.2 否则关闭当前打开的目录。
//3. 打开SYSTABLES和SYSCOLUMNS,设置dbInfo
//4. 设置curDbName
RC OpenDB(char* dbname) {
RC rc;
//1. 检查dbname是否为空。
if (dbname == NULL)
return SQL_SYNTAX;
//2. 如果当前打开了db.
if (dbInfo.curDbName.size()) {
// 2.1 如果打开的目录和dbname相同,则返回。
if (dbInfo.curDbName.compare(dbname) == 0)
return SUCCESS;
// 2.2 否则关闭当前打开的目录。
if ((rc = CloseDB()))
return rc;
}
//2. 打开SYSTABLES和SYSCOLUMNS,设置dbInfo
std::string sysTablePath = TABLE_META_NAME;
//sysTablePath = sysTablePath + "\\" + TABLE_META_NAME;
std::string sysColumnsPath = COLUMN_META_NAME;
//sysColumnsPath = sysColumnsPath + "\\" + COLUMN_META_NAME;
if ((rc = RM_OpenFile((char*)sysTablePath.c_str(), &(dbInfo.sysTables))) ||
(rc = RM_OpenFile((char*)sysColumnsPath.c_str(), &(dbInfo.sysColumns))))
return rc;
//3. 设置curDbName
dbInfo.curDbName = dbname;
if (!SetCurrentDirectory(dbname)) {
return OS_FAIL;
}
return SUCCESS;
}
//
//目的:关闭当前数据库。关闭当前数据库中打开的所有文件
//1. 检查当前是否打开了DB。如果curDbName为空则报错,否则设置curDbName为空。
//2. 调用保存的SYS文件句柄close函数,关闭SYS文件。
RC CloseDB() {
//1. 检查当前是否打开了DB。如果curDbName为空则报错,否则设置curDbName为空。
if (!dbInfo.curDbName.size())
return SQL_SYNTAX;
dbInfo.curDbName.clear();
//2. 调用保存的SYS文件句柄close函数,关闭SYS文件。
RM_CloseFile(&(dbInfo.sysTables));
RM_CloseFile(&(dbInfo.sysColumns));
return SUCCESS;
}
bool CanButtonClick() {//需要重新实现
//如果当前有数据库已经打开
if (dbInfo.curDbName.size())
return true;
//如果当前没有数据库打开
return false;
}
//
//目的:检查传入的attributes是否存在属性名过长或者重名。
bool attrVaild(int attrCount, AttrInfo* attributes)
{
std::set<std::string> dupJudger;
std::set<std::string>::iterator iter;
for (int i = 0; i < attrCount; i++) {
if (strlen(attributes[i].attrName) >= SIZE_ATTR_NAME)
return false;
std::string attrName(attributes[i].attrName);
iter = dupJudger.find(attrName);
if (iter != dupJudger.end())
return false;
dupJudger.insert(attrName);
}
return true;
}
//
//目的:创建一个名为 relName 的表。
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 检查输入参数的合法性:
// 2.1. 检查atrrCount是否大于 MAXATTRS。
// 2.2. 检查relName是否大于20个字节。
// 2.3. 检查relName是否和SYSTABLES以及SYSCOLUMNS同名。
// 2.4. 检查attributes是否合法。
//3. 检查是否已经存在relName表。如果已经存在,则返回错误。
//4. 向SYSTABLES和SYSCOLUMNS表中传入元信息,并计算relName表记录的大小。
//5. 创建对应的RM文件。并将SYSTABLE文件和SYSCOLUMNS文件刷写到disk。
RC CreateTable(char* relName, int attrCount, AttrInfo* attributes) {
//参数 attrCount 表示关系中属性的数量(取值为1 到 MAXATTRS 之间) 。
//参数 attributes 是一个长度为 attrCount 的数组。 对于新关系中第 i 个属性,
//attributes 数组中的第 i 个元素包含名称、 类型和属性的长度(见 AttrInfo 结构定义)
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return DB_NOT_EXIST;
//2. 检查输入参数的合法性:
// 2.1. 检查atrrCount是否大于 MAXATTRS。
if (attrCount > dbInfo.MAXATTRS)
return TOO_MANY_ATTR;
// 2.2. 检查relName是否大于20个字节。
if (strlen(relName) >= SIZE_TABLE_NAME)
return TABLE_NAME_ILLEGAL;
// 2.3. 检查relName是否和SYSTABLES以及SYSCOLUMNS同名。
if (strcmp(relName, TABLE_META_NAME) == 0 ||
strcmp(relName, COLUMN_META_NAME) == 0)
return TABLE_NAME_ILLEGAL;
// 2.4. 检查attributes是否合法。
if (!attrVaild(attrCount, attributes))
return INVALID_ATTRS;
//3. 检查是否已经存在relName表。如果已经存在,则返回错误。
RM_Record rmRecord;
RC rc;
rmRecord.pData = new char[SIZE_SYS_TABLE];
memset(rmRecord.pData, 0, SIZE_SYS_TABLE);
if ((rc = TableMetaSearch(relName, &rmRecord)) != TABLE_NOT_EXIST) {
if (rc == SUCCESS) {
delete[] rmRecord.pData;
return TABLE_EXIST;
}
delete[] rmRecord.pData;
return rc;
}
//4. 向SYSTABLES和SYSCOLUMNS表中传入元信息,并计算relName表记录的大小。
//向SYSTABLES插入信息
if ((rc = TableMetaInsert(relName, attrCount))) {
//插入失败
delete[] rmRecord.pData;
return rc;
}
//向SYSCOLUMNS插入信息
char indexName[1];//indexName为空
int rmRecordSize = 0;
indexName[0] = 0;
for (int i = 0; i < attrCount; i++) {
if ((rc = ColumnMetaInsert(relName, attributes[i].attrName, attributes[i].attrType,
attributes[i].attrLength, rmRecordSize, 0, indexName))) {
delete[] rmRecord.pData;
return rc;
}
//计算记录的大小
rmRecordSize += attributes[i].attrLength;
}
//5. 创建对应的RM文件。并将SYSTABLE文件和SYSCOLUMNS文件刷写到disk。
//根据约定,文件名为了relName.rm。
std::string filePath = dbInfo.curDbName + "\\" + relName + RM_FILE_SUFFIX;
if ((rc = RM_CreateFile((char*)filePath.c_str(), rmRecordSize))||
(rc = ForceDataPages(&dbInfo.sysTables.pfFileHandle)) ||
(rc = ForceDataPages(&dbInfo.sysColumns.pfFileHandle)) ) {
delete[] rmRecord.pData;
return rc;
}
//
delete[] rmRecord.pData;
return SUCCESS;
}
//
//目的:销毁名为 relName 的表以及在该表上建立的所有索引。
//1. 检查当前是否已经打开了一个数据库,如果没有则报错。
//2. 检查relName是否和SYSTABLES以及SYSCOLUMNS同名。
//3. 从SYSTABLES中删除relName对应的记录以及relName对应的rm文件
//4. 删除SYSCOLUMNS中的记录项以及可能存在的ix文件
RC DropTable(char* relName) {
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
RC rc;
//2. 检查relName是否和SYSTABLES以及SYSCOLUMNS同名。
if (strcmp(relName, TABLE_META_NAME) == 0 ||
strcmp(relName, COLUMN_META_NAME) == 0) {
return TABLE_NAME_ILLEGAL;
}
//3. 从SYSTABLES中删除relName对应的记录以及relName对应的rm文件
if ((rc = TableMetaDelete(relName))) {
return rc;
}
//4. 删除SYSCOLUMNS中的记录项以及可能存在的ix文件
if ((rc = ColumnMetaDelete(relName))) {
return rc;
}
if((rc= ForceDataPages(&(dbInfo.sysTables.pfFileHandle))) ||
(rc= ForceDataPages(&(dbInfo.sysColumns.pfFileHandle))))
return rc;
return SUCCESS;
}
//该函数在关系 relName 的属性 attrName 上创建名为 indexName 的索引。
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 判断输入参数的indexName长度是否合法。
//3. 检查是否已经存在对应的索引。如果已经存在则报错。
//4. 创建对应的索引。
// 4.1. 创建IX文件
// 4.2. 更新SYS_COLUMNS
// 4.3. 扫描RM文件,将已有记录信息插入到创建的IX文件
RC CreateIndex(char* indexName, char* relName, char* attrName) {
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
//2. 判断输入参数的indexName长度是否合法。
if (strlen(indexName) >= SIZE_INDEX_NAME) {
return INDEX_NAME_ILLEGAL;
}
std::string ixFileName = dbInfo.curDbName + "\\" + indexName + IX_FILE_SUFFIX;
//3. 检查是否已经存在对应的索引。如果已经存在则报错。
RM_Record rmRecord;
RC rc;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
if ((rc = ColumnSearchAttr(relName, attrName, &rmRecord))) {
delete[] rmRecord.pData;
return rc;
}
char* ix_flag = rmRecord.pData + ATTR_IXFLAG_OFF;//无法在Scan的Conditions中加入ix_flag的判断。即使用chars也无法比较。
if ((*ix_flag) == (char)1) {//已经创建了对应的索引
delete[] rmRecord.pData;
return INDEX_EXIST;
}
//4. 创建对应的索引。
// 4.1. 创建IX文件
int attrType = *((int*)(rmRecord.pData + ATTR_TYPE_OFF));
int attrLength = *((int*)(rmRecord.pData + ATTR_LENGTH_OFF));
int attrOffset = *((int*)(rmRecord.pData + ATTR_OFFSET_OFF));
if ((rc = CreateIndex((char*)ixFileName.c_str(), (AttrType)attrType, attrLength))) {
delete[] rmRecord.pData;
return rc;
}
// 4.2. 更新SYS_COLUMNS
if ((rc = ColumnMetaUpdate(relName, attrName, 1, indexName))) {
delete[] rmRecord.pData;
return rc;
}
// 4.3. 扫描RM文件,将已有记录信息插入到创建的IX文件
if ((rc = CreateIxFromTable(relName, indexName, attrOffset))) {
delete[] rmRecord.pData;
return rc;
}
if((rc=ForceDataPages(&(dbInfo.sysTables.pfFileHandle))) ||
(rc= ForceDataPages(&(dbInfo.sysColumns.pfFileHandle)))){
delete[] rmRecord.pData;
return rc;
}
delete[] rmRecord.pData;
return SUCCESS;
}
//
//目的:该函数用来删除名为 indexName 的索引。
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 从SYSCOLUMNS中检查索引是否存在。(这里利用了我们约定的indexname具有唯一性来进行查找)
// 2.1 如果不存在则报错。
// 2.2. 删除ix文件
// 2.3.否则修改SYSCOLUMNS中对应记录项ix_flag为0.
RC DropIndex(char* indexName) {
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
//2. 从SYSCOLUMNS中检查索引是否存在。(这里利用了我们约定的indexname具有唯一性来进行查找)
RM_FileScan rmFileScan;
RM_Record rmRecord;
Con conditions[1];
RC rc;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
conditions[0].attrType = chars; conditions[0].bLhsIsAttr = 1; conditions[0].bRhsIsAttr = 0;
conditions[0].compOp = EQual; conditions[0].LattrLength = SIZE_INDEX_NAME;
conditions[0].LattrOffset = ATTR_INDEX_NAME_OFF;
conditions[0].Lvalue = NULL; conditions[0].RattrLength = SIZE_INDEX_NAME; conditions[0].RattrOffset = 0;
conditions[0].Rvalue = indexName;
if ((rc = OpenScan(&rmFileScan, &(dbInfo.sysColumns), 1, conditions))) {
delete[] rmRecord.pData;
return rc;
}
// 2.1 如果不存在则报错。
rc = GetNextRec(&rmFileScan, &rmRecord);
if (rc == RM_EOF) {
delete[] rmRecord.pData;
return INDEX_NOT_EXIST;
}
if (rc != SUCCESS) {
delete[] rmRecord.pData;
return rc;
}
char* ix_flag = rmRecord.pData + ATTR_IXFLAG_OFF;
if ((*ix_flag) == (char)0) {//没有创建索引。
delete[] rmRecord.pData;
return INDEX_NOT_EXIST;
}
if ((rc = CloseScan(&rmFileScan))) {
delete[] rmRecord.pData;
return rc;
}
// 2.2. 删除ix文件
std::string ixFilePath = dbInfo.curDbName + "\\" + indexName + IX_FILE_SUFFIX;
if (!DeleteFile((LPCSTR)ixFilePath.c_str())) {
delete[] rmRecord.pData;
return OS_FAIL;
}
// 2.3.否则修改SYSCOLUMNS中对应记录项ix_flag为0.
ix_flag = rmRecord.pData + ATTR_IXFLAG_OFF;
*ix_flag = (char)0;
if ((rc = UpdateRec(&(dbInfo.sysColumns), &rmRecord))) {
delete[] rmRecord.pData;
return rc;
}
if((rc= ForceDataPages(&(dbInfo.sysTables.pfFileHandle))) ||
(rc= ForceDataPages(&(dbInfo.sysColumns.pfFileHandle)))){
delete[] rmRecord.pData;
return rc;
}
delete[] rmRecord.pData;
return SUCCESS;
}
//
//目的:该函数用来在 relName 表中插入具有指定属性值的新元组,
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 检查传入参数的合法性
// 2.1. 扫描SYSTABLE
// 2.1.1 如果传入的relName表不存在就报错。
// 2.1.2 如果存在则保存attrcount。如果attrcount和传入的nValues不一致则报错。
// 2.2. 扫描SYSCOLUMNS文件。
// 2.2.1 检查传入value中属性值的类型和长度是否和SYSCOLUMNS记录的相同。
// 2.2.2 如果传入的value类型是chars,判断values.data长度是否和SYSCOLUMNS记录相同
//3. 将values的数据整合为一个数据。
//4. 利用relName打开RM文件.
//5. 对每一个保存建立了索引的属性打开对应的ix文件并保存索引。
//6. 向RM文件插入记录。同时向可能存在的索引文件插入记录。
//7. 关闭文件句柄
RC Insert(char* relName, int nValues, Value* values) {
//nValues 为属性值个数, values 为对应的属性值数组。
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
//2. 检查传入参数的合法性
// 2.1. 扫描SYSTABLE
RM_FileScan rmFileScan;
RM_Record rmRecord;
RC rc;
int attrCount = 0;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
// 2.1.1 如果传入的relName表不存在就报错。
if ((rc = TableMetaSearch(relName, &rmRecord))) {
delete[] rmRecord.pData;
return rc;
}
// 2.1.2 如果存在则保存attrcount。如果attrcount和传入的nValues不一致则报错。
attrCount = *((int*)(rmRecord.pData + ATTR_COUNT_OFF));
if (attrCount != nValues) {
delete[] rmRecord.pData;
return INVALID_VALUES;
}
delete[] rmRecord.pData;
// 2.2. 扫描SYSCOLUMNS文件。
std::vector<AttrEntry> attributes;
std::string str_indexName;
int recordSize = 0;
if ((rc = ColumnEntryGet(relName, &attrCount, attributes))) {
return rc;
}
for (int i = 0; i < attrCount; i++) {
// 2.2.1 检查传入values中属性值的类型是否和SYSCOLUMNS记录的相同。
if (values[i].type != attributes[i].attrType) {
return INVALID_VALUES;
}
// 2.2.2 如果传入的value类型是chars,判断values.data长度是否和SYSCOLUMNS记录相同
if ((values[i].type == chars) && strlen((char*)values[i].data) >= attributes[i].attrLength) {
return INVALID_VALUES;
}
recordSize += attributes[i].attrLength;
}
//3. 将values的数据整合为一个数据。
RID rid;
char* insertData = new char[recordSize];
recordSize = 0;
for (int i = 0; i < attrCount; i++) {
memcpy(insertData + recordSize, values[i].data, attributes[i].attrLength);
recordSize += attributes[i].attrLength;
}
//4. 利用relName打开RM文件.
std::string rmFileName = "";
RM_FileHandle rmFileHandle;
rmFileName = dbInfo.curDbName + "\\" + relName + RM_FILE_SUFFIX;
if ((rc = RM_OpenFile((char*)rmFileName.c_str(), &rmFileHandle))) {
delete[] insertData;
return rc;
}
//5. 对每一个保存建立了索引的属性打开对应的ix文件并保存索引。
std::vector<IxEntry> ixEntrys;
IxEntry curIxEntry;
for (int i = 0; i < attrCount; i++) {
if (attributes[i].ix_flag) {
str_indexName = dbInfo.curDbName + "\\" + attributes[i].indexName + IX_FILE_SUFFIX;
if ((rc = OpenIndex(str_indexName.c_str(), &curIxEntry.ixIndexHandle))) {
return rc;
}
curIxEntry.attrOffset = attributes[i].attrOffset;
ixEntrys.push_back(curIxEntry);
}
}
//6. 用保存ix文件句柄,将建立了索引的values插入到对应ix文件.
if ((rc = InsertRmAndIx(&rmFileHandle, ixEntrys, insertData))) {
delete[] insertData;
return rc;
}
delete[] insertData;
//7. 关闭文件句柄
if ((rc = RM_CloseFile(&rmFileHandle))) {
return rc;
}
int numIndexs = ixEntrys.size();
for (int i = 0; i < numIndexs; i++) {
if ((rc = CloseIndex(&(ixEntrys[i].ixIndexHandle)))) {
return rc;
}
}
return SUCCESS;
}
// 目的:检查属性对relName是否合法。如果合法将属性的类型写入attrType。
// 1.如果bLhsIsAttr为1:
// 1.1.检查lhsAttr的relName是否和传入的relName相同。不同返回false。
// 1.2.检查lhsAttr的attrName在relName中是否存在。不同返回false。
// 2.否则返回true。
bool checkAttr(char* relName, int hsIsAttr, RelAttr& hsAttr, AttrType* attrType) {
RM_Record rmRecord;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
// 1.如果bLhsIsAttr为1:
if (hsIsAttr) {
// 1.1.检查lhsAttr的relName是否和传入的relName相同。不同返回false。
if ( hsAttr.relName && strcmp(hsAttr.relName, relName)) {
delete[] rmRecord.pData;
return false;
}
// 1.2.检查lhsAttr的attrName在relName中是否存在。不同返回false。
if (hsAttr.attrName && ColumnSearchAttr(relName, hsAttr.attrName, &rmRecord)) {
delete[] rmRecord.pData;
return false;
}
}
// 2.否则返回true
*attrType = (AttrType)(*(int*)(rmRecord.pData + ATTR_TYPE_OFF));
delete[] rmRecord.pData;
return true;
}
//
// 目的:检查在relName表上,condition是否合法。
// 1. 如果bLhsIsAttr和bRhsIsAttr都为1,则报错。
// 2. 检查左右属性是否合法。
// 3. 检查比较运算左右类型是否一致。
bool CheckCondition(char* relName, Condition& condition) {
// 1. 如果bLhsIsAttr和bRhsIsAttr都为0,则报错。
if (!condition.bLhsIsAttr && !condition.bRhsIsAttr) {
return false;
}
// 2. 检查左右属性是否合法。
AttrType lType, rType;
if (!checkAttr(relName, condition.bLhsIsAttr, condition.lhsAttr, &lType) ||
!checkAttr(relName, condition.bRhsIsAttr, condition.rhsAttr, &rType)) {
return false;
}
// 3. 检查比较运算左右类型是否一致。
if (!condition.bLhsIsAttr) {
lType = condition.lhsValue.type;
}
if (!condition.bRhsIsAttr) {
rType = condition.rhsValue.type;
}
if (lType != rType) {
return false;
}
return true;
}
//
//目的:该函数用来删除 relName 表中所有满足指定条件的元组以及该元组对应的索
//引项。 如果没有指定条件, 则此方法删除 relName 关系中所有元组。 如果包
//含多个条件, 则这些条件之间为与关系。
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 检查传入参数的合法性
// 2.1. 扫描SYSTABLE.如果传入的relName表不存在就报错。
// 2.2. 检查传入的conditions是否正确。
//3. 利用relName打开RM文件。
//4. 扫描SYSCOLUMNS文件。保存每一个建立了索引的ixIndexHandle.同时计算relName表记录的大小
//5. 将输入的nConditions和conditions转换成RM_FileScan的参数。
//6. 用rm句柄、nConditions和conditions创建rmFileScan。对筛选出的每一项记录进行删除。同时删除可能存在的索引记录。
//7. 关闭文件句柄
RC Delete(char* relName, int nConditions, Condition* conditions) {
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
//2. 检查传入参数的合法性
RM_Record rmRecord;
RC rc;
int attrCount = 0;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
// 2.1. 扫描SYSTABLE.如果传入的relName表不存在就报错。
if ((rc = TableMetaSearch(relName, &rmRecord))) {
delete[] rmRecord.pData;
return rc;
}
delete[] rmRecord.pData;
// 2.2. 检查传入的conditions是否正确。
for (int i = 0; i < nConditions; i++) {
if (!CheckCondition(relName, conditions[i])) {
return INVALID_CONDITIONS;
}
}
//3. 利用relName打开RM文件。
std::string rmFileName = dbInfo.curDbName + "\\" + relName + RM_FILE_SUFFIX;//relName+".rm";
RM_FileHandle rmFileHandle;
if ((rc = RM_OpenFile((char*)rmFileName.c_str(), &rmFileHandle))) {
return rc;
}
//4. 扫描SYSCOLUMNS文件。保存每一个建立了索引的ixIndexHandle
std::string str_indexName;
std::vector<AttrEntry> attributes;
std::vector<IxEntry> ixEntrys;
IxEntry curIxEntry;
int recordSize = 0;
if ((rc = ColumnEntryGet(relName, &attrCount, attributes))) {
return rc;
}
for (int i = 0; i < attrCount; i++) {
if (attributes[i].ix_flag) {
str_indexName = dbInfo.curDbName + "\\" + attributes[i].indexName + IX_FILE_SUFFIX;
if ((rc = OpenIndex(str_indexName.c_str(), &curIxEntry.ixIndexHandle))) {
return rc;
}
curIxEntry.attrOffset = attributes[i].attrOffset;
ixEntrys.push_back(curIxEntry);
}
recordSize += attributes[i].attrLength;
}
//5. 将输入的nConditions和conditions转换成Con类型。
Con* cons = new Con[nConditions];
memset(cons, 0, sizeof(Con) * nConditions);
if ((rc = CreateConFromCondition(relName, nConditions, conditions, cons))) {
delete[] cons;
return rc;
}
//6. 用rm句柄、nConditions和conditions创建rmFileScan。对筛选出的每一项记录进行删除。同时删除可能存在的索引记录。
RM_FileScan rmFileScan;
RM_Record delRecord; //record to be deleted
delRecord.pData = new char[recordSize];
memset(delRecord.pData, 0, recordSize);
if ((rc = OpenScan(&rmFileScan, &rmFileHandle, nConditions, cons))) {
return rc;
}
while ((rc = GetNextRec(&rmFileScan, &delRecord)) == SUCCESS) {
if ((rc = DeleteRmAndIx(&rmFileHandle, ixEntrys, &delRecord))) {
delete[] cons;
delete[] delRecord.pData;
return rc;
}
}
delete[] cons;
delete[] delRecord.pData;
if (rc != RM_EOF || (rc = CloseScan(&rmFileScan))) {
return rc;
}
//6. 关闭文件句柄
if ((rc = RM_CloseFile(&rmFileHandle))) {
return rc;
}
int numIndexs = ixEntrys.size();
for (int i = 0; i < numIndexs; i++) {
if ((rc = CloseIndex(&(ixEntrys[i].ixIndexHandle)))) {
return rc;
}
}
return SUCCESS;
}
//目的:该函数用来在 relName 表中插入具有指定属性值的新元组,
//1. 检查当前是否打开了一个数据库。如果没有则报错。
//2. 检查传入参数的合法性
// 2.1. 查找relName。如果传入的relName表不存在就报错。
// 2.2. 扫描SYSCOLUMNS文件。
// 2.2.1 检查传入value中属性值的类型和长度是否和SYSCOLUMNS记录的相同。
// 2.2.2 如果传入的value类型是chars,判断values.data长度是否和SYSCOLUMNS记录相同
// 2.3. 检查传入的conditions是否合法。如果合法则将conditions转换成Con结构。
//3. 利用relName打开RM文件.
//4. 如果attrName上建立了索引。则打开对应的ix文件并保存索引。
//5. 扫描RM文件,对每一项符合要求的记录,删除记录后重新插入新的记录。对索引文件进行同步操作。
//6. 关闭文件句柄
RC Update(char* relName, char* attrName, Value* value, int nConditions, Condition* conditions) {
//1. 检查当前是否打开了一个数据库。如果没有则报错。
if (dbInfo.curDbName.size() == 0)
return SQL_SYNTAX;
//2. 检查传入参数的合法性
RM_Record rmRecord;
RC rc;
rmRecord.pData = new char[SIZE_SYS_COLUMNS];
memset(rmRecord.pData, 0, SIZE_SYS_COLUMNS);
// 2.1. 查找relName。如果传入的relName表不存在就报错。
if ((rc = TableMetaSearch(relName, &rmRecord))) {
delete[] rmRecord.pData;
return rc;
}
delete[] rmRecord.pData;
// 2.2. 扫描SYSCOLUMNS文件。
AttrEntry attrEntry;
if ((rc = ColumnMetaGet(relName, attrName, &attrEntry))) {
return rc;
}
// 2.2.1 检查传入values中属性值的类型是否和SYSCOLUMNS记录的相同。
if (value->type != attrEntry.attrType) {
return INVALID_VALUES;
}
// 2.2.2 如果传入的value类型是chars,判断values.data长度是否和SYSCOLUMNS记录相同
if ((value->type == chars) && strlen((char*)value->data) >= attrEntry.attrLength) {
return INVALID_VALUES;
}
// 2.3. 检查传入的conditions是否合法。如果合法则将conditions转换成Con结构。
for (int i = 0; i < nConditions; i++) {
if (!CheckCondition(relName, conditions[i]))
return INVALID_CONDITIONS;
}
Con* cons = new Con[nConditions];
memset(cons, 0, sizeof(Con) * nConditions);
if ((rc = CreateConFromCondition(relName, nConditions, conditions, cons))) {
return rc;
}
//3. 利用relName打开RM文件.
std::string rmFileName = "";
RM_FileHandle rmFileHandle;
rmFileName = dbInfo.curDbName + "\\" + relName + RM_FILE_SUFFIX;
if ((rc = RM_OpenFile((char*)rmFileName.c_str(), &rmFileHandle))) {
return rc;
}
//4. 对建立了索引的属性打开对应的ix文件并保存索引。
std::vector<IxEntry> ixEntrys;
IxEntry curIxEntry;
std::string str_indexName;
if (attrEntry.ix_flag) {
str_indexName = dbInfo.curDbName + "\\" + attrEntry.indexName + IX_FILE_SUFFIX;
if ((rc = OpenIndex(str_indexName.c_str(), &curIxEntry.ixIndexHandle))) {
return rc;
}
curIxEntry.attrOffset = attrEntry.attrOffset;
ixEntrys.push_back(curIxEntry);
}
//5. 扫描RM文件,对每一项符合要求的记录,删除记录后重新插入新的记录。对索引文件进行同步操作。
RM_FileScan rmFileScan;
RM_Record updateRecord;
int recordSize;
GetRecordSize(relName, &recordSize);
updateRecord.pData = new char[recordSize];
memset(updateRecord.pData, 0, recordSize);
if ((rc = OpenScan(&rmFileScan, &rmFileHandle, nConditions, cons))) {
return rc;
}
while ((rc = GetNextRec(&rmFileScan, &updateRecord)) == SUCCESS) {
if ((rc = DeleteRmAndIx(&rmFileHandle, ixEntrys, &updateRecord))) {
delete[] updateRecord.pData;
return rc;
}
memcpy(updateRecord.pData + attrEntry.attrOffset, value->data, attrEntry.attrLength);
if ((rc = InsertRmAndIx(&rmFileHandle, ixEntrys, updateRecord.pData))) {
delete[] updateRecord.pData;
return rc;
}
}
delete[] updateRecord.pData;
if (rc != RM_EOF || (rc = CloseScan(&rmFileScan))) {