forked from zerofang/myDBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.c
888 lines (865 loc) · 27 KB
/
sql.c
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
#include "sql.h"
#define TRUE 1
#define FALSE 0
extern char database[64];
struct mydb *dbroot = NULL;
/*item链表就地逆置*/
struct item_def *converse_items(struct item_def *ppHead){
struct item_def *pCurNode = ppHead;
struct item_def *pPrevNode = NULL;
struct item_def *pTmpNode = NULL;
while(pCurNode)
{
pPrevNode = pCurNode;
pCurNode = pCurNode->next;
pPrevNode->next = pTmpNode;
pTmpNode = pPrevNode;
}
return pTmpNode;
}
/*各种结构链表的内存释放*/
void free_hyper_items(struct hyper_items_def *Hitemroot){
struct hyper_items_def *Hitemtemp;
while(Hitemroot != NULL){
Hitemtemp = Hitemroot->next;
free(Hitemroot);
Hitemroot = Hitemtemp;
}
}
void free_conditions(struct conditions_def *conroot){
if(conroot == NULL)
return;
if (conroot->left != NULL)
free_conditions(conroot->left);
else if (conroot->right != NULL)
free_conditions(conroot->right);
else
free(conroot);
}
void free_items(struct item_def *itemroot){
struct item_def *itemtemp;
while(itemroot != NULL){
itemtemp = itemroot->next;
free(itemroot);
itemroot = itemtemp;
}
}
void free_tables(struct table_def *tableroot){
struct table_def *tableptr;
while(tableroot != NULL){
tableptr = tableroot->next;
free(tableroot);
tableroot = tableptr;
}
}
void free_upcons(struct upcon_def *uproot){
struct upcon_def *uptemp;
while(uproot != NULL){
uptemp = uproot->next;
free(uptemp);
uproot = uptemp;
}
}
void free_value(struct value_def *valroot){
struct value_def *valtemp;
while(valroot != NULL){
valtemp = valroot->next;
free(valroot);
valroot = valtemp;
}
}
/*创建数据库。若数据库根节点为空,则在根结点处创建数据库,若不为空则遍历数据库链表,
若找到同名数据库,提示已存在,返回,若没找到,在链表尾部插入新的数据库*/
void create_database(){
if(dbroot == NULL){
dbroot = (struct mydb *)malloc(sizeof(struct mydb));
strcpy(dbroot->name,database);
dbroot->tbroot = NULL;
dbroot->next = NULL;
if(dbroot != NULL);
printf("Database %s created successfully!\n", database);
return;
}
struct mydb *newdb;
newdb = dbroot;
while(newdb->next != NULL){
if(strcmp(newdb->name,database) == 0){
printf("error: The database already exists!\n");
return;
}
newdb = newdb->next;
}
if(strcmp(newdb->name,database) == 0){
printf("error: The database already exists!\n");
return;
}
newdb->next = (struct mydb *)malloc(sizeof(struct mydb));
strcpy(newdb->next->name,database);
newdb->next->tbroot = NULL;
newdb->next->next = NULL;
if(dbroot->next != NULL);
printf("Database %s created successfully!\n", database);
}
/*展示所有数据库。若根节点为空,提示数据库列表为空请先创建一个数据库,否则遍历数据库链表,打印数据库名*/
void show_database(){
struct mydb *dbtemp = NULL;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("Database list is NULL, Please create a database first!\n");
return;
}
while(dbtemp != NULL){
printf("%s ", dbtemp->name);
dbtemp = dbtemp->next;
}
printf("\n");
}
/*指定使用的数据库,若数据库根节点为空,提示请先创建一个数据库,遍历数据库链表,找到对应数据库,
将全局变量database更新为当前数据库名,否则提示数据库不存在,返回*/
void use_database(char *dbname){
struct mydb *dbtemp = NULL;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name, dbname) == 0){
strcpy(database, dbname);
printf("Using database %s\n", dbname);
return;
}
dbtemp = dbtemp->next;
}
printf("error: Database %s doesn't exist!\n", dbname);
}
/*创建表。若数据库根节点为空,提示先创建数据库,否则遍历数据库列表,查找当前使用的数据库,若未找到,提示数据库不存在,
若找到,再看当前数据库下的表根节点是否为空,若为空,在根节点处创建新表,否则遍历表链表,若找到名字相同的表,提示
该表已存在,返回,否则在链表尾部建立新表*/
void create_table(char *tableval, struct hyper_items_def *Hitemroot){
int i;
struct mydb *dbtemp = NULL;
struct table *newtable = NULL;
struct table *tabletemp = NULL;
struct hyper_items_def *itemstemp = Hitemroot;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
free_hyper_items(Hitemroot);
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
if(dbtemp->tbroot == NULL){
dbtemp->tbroot = (struct table *)malloc(sizeof(struct table));
strcpy(dbtemp->tbroot->name, tableval);
dbtemp->tbroot->ffield = (struct field *)malloc(10*sizeof(struct field));
i = 0;
while(itemstemp != NULL && i<10){
strcpy(dbtemp->tbroot->ffield[i].name, itemstemp->field);
if(itemstemp->type == 0)
dbtemp->tbroot->ffield[i].type = 0;
else
dbtemp->tbroot->ffield[i].type = 1;
itemstemp = itemstemp->next;
i++;
}
dbtemp->tbroot->flen = i;
dbtemp->tbroot->ilen = 0;
dbtemp->tbroot->next = NULL;
if(dbtemp->tbroot != NULL)
printf("Table %s created successfully!\n", tableval);
free_hyper_items(Hitemroot);
return;
}
newtable = dbtemp->tbroot;
while(newtable->next != NULL){
if(strcmp(newtable->name,tableval) == 0){
printf("error: The table already exists!\n");
free_hyper_items(Hitemroot);
return;
}
newtable = newtable->next;
}
if(strcmp(newtable->name,tableval) == 0){
printf("error: The table already exists!\n");
free_hyper_items(Hitemroot);
return;
}
newtable->next = (struct table *)malloc(sizeof(struct table));
strcpy(newtable->next->name, tableval);
newtable->next->ffield = (struct field *)malloc(10*sizeof(struct field));
i = 0;
while(itemstemp != NULL && i<10){
strcpy(newtable->next->ffield[i].name, itemstemp->field);
if(itemstemp->type == 0)
newtable->next->ffield[i].type = 0;
else
newtable->next->ffield[i].type = 1;
itemstemp = itemstemp->next;
i++;
}
newtable->next->flen = i;
newtable->next->next = NULL;
printf("Table %s created successfully!\n", tableval);
free_hyper_items(Hitemroot);
return;
}
dbtemp = dbtemp->next;
}
free_hyper_items(Hitemroot);
printf("error: Database %s doesn't exist!\n", database);
}
/*展示表。遍历当前选用的数据库下的表链表,打印表名*/
void show_table(){
struct mydb *dbtemp = NULL;
struct table *tabletemp = NULL;
int i,j,temp;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
printf("%s ", tabletemp->name);
tabletemp = tabletemp->next;
}
printf("\n");
return;
}
dbtemp = dbtemp->next;
}
printf("error: Database %s doesn't exist!\n", database);
}
/*插入记录。找到插入的表,若未找到,提示不存在,找到以后,若未指定字段及其顺序,将value list中的值依次插入到每个字段的最后,
若指定了字段和顺序,则按顺序查找字段,并依次追加记录,若未查找到对应字段,则提示字段与表不匹配,返回。完成插值以后,
判断itemlist和valuelist是否同时为空,若不是,则提示两者不匹配,返回,若是,则提示成功,并将该表的条数值加一*/
void multi_insert(char *tableval, struct item_def *itemroot, struct value_def *valroot){
struct mydb *dbtemp = NULL;
struct table *tabletemp = NULL;
struct item_def *itemtemp = NULL;
struct value_def *valtemp = NULL;
int i,j,temp;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
free_items(itemroot);
free_value(valroot);
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
if(strcmp(tabletemp->name, tableval) == 0){
i = 0;
temp = tabletemp->ilen;
valtemp = valroot;
if(itemroot == NULL){
while(valtemp != NULL){
if(valtemp->type == 0){
tabletemp->ffield[i].type = 0;
tabletemp->ffield[i].key[temp].intkey = valtemp->value.intkey;
}
else{
tabletemp->ffield[i].type = 1;
strcpy(tabletemp->ffield[i].key[temp].skey, valtemp->value.skey);
}
i++;
valtemp = valtemp->next;
}
}
else{
itemtemp = itemroot;
while(itemtemp != NULL && valtemp != NULL){
for(j = 0; j < tabletemp->flen; j++){
if(strcmp(tabletemp->ffield[j].name, itemtemp->field) == 0){
if(valtemp->type == 0){
tabletemp->ffield[j].type = 0;
tabletemp->ffield[j].key[temp].intkey = valtemp->value.intkey;
}
else{
tabletemp->ffield[j].type = 1;
strcpy(tabletemp->ffield[j].key[temp].skey, valtemp->value.skey);
}
i++;
break;
}
}
if(j == tabletemp->flen){
printf("error: Column name does not match the table definition!\n");
free_items(itemroot);
free_value(valroot);
return;
}
itemtemp = itemtemp->next;
valtemp = valtemp->next;
}
}
if(i <= tabletemp->flen && itemtemp == NULL && valtemp == NULL){
temp++;
tabletemp->ilen = temp;
printf("Insert successfully!\n");
}
else{
printf("error: The number of supplied values does not match the table definition!\n");
}
free_items(itemroot);
free_value(valroot);
return;
}
tabletemp = tabletemp->next;
}
printf("error: The table doesn't exist!\n");
free_items(itemroot);
free_value(valroot);
return;
}
dbtemp = dbtemp->next;
}
free_items(itemroot);
free_value(valroot);
printf("error: Database %s doesn't exist!\n", database);
}
/*二叉树递归判断条件正误,i是当前判断的一条记录在表中的位置索引,tabletemp是用于判断的临时表,单表查询时它指向单表,
多表查询时它是多表的笛卡尔乘积,conroot是条件二叉树根节点,当conroot为空时直接判断为TRUE,这个主要用于无条件的查询和更新,
删除等操作,有条件时,即最初传入的conroot不为空时,不会触发这条判断执行*/
_Bool whereTF(int i, struct table *tabletemp, struct conditions_def *conroot){
if(conroot == NULL){
return TRUE;
}
if(conroot->cmp_op == 7){
return whereTF(i, tabletemp, conroot->left) && whereTF(i, tabletemp, conroot->right);
}
else if(conroot->cmp_op == 8){
return whereTF(i, tabletemp, conroot->left) || whereTF(i, tabletemp, conroot->right);
}
else{
if(conroot->litem->pos == NULL){
int k;
for(k = 0; k < tabletemp->flen; k++){
if(strcmp(tabletemp->ffield[k].name, conroot->litem->field) == 0){
conroot->litem->pos = &tabletemp->ffield[k];
break;
}
}
if(conroot->litem->pos == NULL){
printf("error: Field %s doesn't exist!\n", conroot->litem->field);
return FALSE;
}
}
if(conroot->cmp_op == 1){
if(conroot->type == 0)
return conroot->litem->pos->key[i].intkey == conroot->intv;
else
return strcmp(conroot->litem->pos->key[i].skey, conroot->strv) == 0;
}
else if(conroot->cmp_op == 2){
if(conroot->type == 0){
return conroot->litem->pos->key[i].intkey > conroot->intv;
}
else{
printf("error: String can not compare!\n");
return FALSE;
}
}
else if(conroot->cmp_op == 3){
if(conroot->type == 0)
return conroot->litem->pos->key[i].intkey < conroot->intv;
else{
printf("error: String can not compare!\n");
return FALSE;
}
}
else if(conroot->cmp_op == 4){
if(conroot->type == 0)
return conroot->litem->pos->key[i].intkey >= conroot->intv;
else{
printf("error: String can not compare!\n");
return FALSE;
}
}
else if(conroot->cmp_op == 5){
if(conroot->type == 0)
return conroot->litem->pos->key[i].intkey <= conroot->intv;
else{
printf("error: String can not compare!\n");
return FALSE;
}
}
else {
if(conroot->type == 0)
return conroot->litem->pos->key[i].intkey != conroot->intv;
else
return strcmp(conroot->litem->pos->key[i].skey, conroot->strv) != 0;
}
}
}
/*查询操作,目前仅支持最多2个表查询。itemroot是选择的字段链表根节点,若为*即全选则传入NULL,tableroot是选择的表根节点,
conroot是条件二叉树根节点,若无条件则传入NULL,首先找到查询的表,找到以后更新每个table_def结构的pos变量,指向对应的表,
然后判断是否是单表,若为单表,tabletemp指向该表,再判断itemroot是否为空,若为空表示全选,依次将整个表打印出来,
提示成功,返回。若不为单表,即为2个表联合查询,那么先遍历两个表,找到第一组名称相同且数据类型相同的字段,作为主码,
将字段值相同的记录进行合并,构成新表,存入tabletemp中,若没有字段相同的,将笛卡尔乘积存入tabletemp中,然后判断itemroot
是否为空,若为空,判断并打印所有字段记录,若不为空,判断并打印对应的字段记录*/
void select_where(struct item_def *itemroot, struct table_def *tableroot, struct conditions_def *conroot){
struct mydb *dbtemp = NULL;
struct table_def *tableptr = NULL;
struct table *tabletemp = NULL;
struct item_def *itemtemp = NULL;
int i, j, k, m, n, len1, len2, pk1 = -1, pk2 = -1;
itemroot = converse_items(itemroot);
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
free_conditions(conroot);
free_items(itemroot);
free_tables(tableroot);
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tableptr = tableroot;
while(tableptr != NULL){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
if(strcmp(tabletemp->name, tableptr->table) == 0){
tableptr->pos = tabletemp;
break;
}
tabletemp = tabletemp->next;
}
if(tabletemp == NULL){
printf("error: Table %s doesn't exist!\n", tableptr->table);
free_conditions(conroot);
free_items(itemroot);
free_tables(tableroot);
return;
}
tableptr = tableptr->next;
}
if(tableroot->next == NULL){
tabletemp = tableroot->pos;
if(itemroot == NULL){
for(j = tabletemp->flen - 1; j >= 0; j--){
printf("%-20s ", tabletemp->ffield[j].name);
}
printf("\n");
for(i = 0; i < tabletemp->ilen; i++){
if(whereTF(i, tabletemp, conroot) == TRUE){
for(j = tabletemp->flen - 1; j >= 0 ; j--){
if(tabletemp->ffield[j].type == 0)
printf("%-20d ", tabletemp->ffield[j].key[i].intkey);
else
printf("%-20s ", tabletemp->ffield[j].key[i].skey);
}
printf("\n");
}
}
free_conditions(conroot);
free_tables(tableroot);
return;
}
}
else{
tableroot->next->next = tableroot;
tableroot = tableroot->next;
tableroot->next->next = NULL;
len1 = tableroot->pos->flen;
len2 = tableroot->next->pos->flen;
tabletemp = (struct table *)malloc(sizeof(struct table));
for(i = len1 - 1; i >= 0; i--){
for(j = len2 - 1; j >= 0; j--){
if(strcmp(tableroot->pos->ffield[i].name, tableroot->next->pos->ffield[j].name) == 0 && tableroot->pos->ffield[i].type == tableroot->next->pos->ffield[j].type){
pk1 = i;
pk2 = j;
break;
}
}
if(strcmp(tableroot->pos->ffield[i].name, tableroot->next->pos->ffield[j].name) == 0)
break;
}
if(pk2 == -1){
tabletemp->ffield = (struct field *)malloc((len1 + len2)*sizeof(struct field));
tabletemp->flen = len1 + len2;
k = 0;
for(i = len1 - 1; i >= 0; i--){
strcpy(tabletemp->ffield[k].name, tableroot->pos->ffield[i].name);
k++;
}
for(i = len2 - 1; i >= 0; i--){
strcpy(tabletemp->ffield[k].name, tableroot->next->pos->ffield[i].name);
k++;
}
n = 0;
for(i = 0; i < tableroot->pos->ilen; i++){
for(j = 0; j < tableroot->next->pos->ilen; j++){
k = 0;
for(m = len1 - 1; m >= 0; m--){
tabletemp->ffield[k].type = tableroot->pos->ffield[m].type;
if(tableroot->pos->ffield[m].type == 0)
tabletemp->ffield[k].key[n].intkey = tableroot->pos->ffield[m].key[i].intkey;
else
strcpy(tabletemp->ffield[k].key[n].skey, tableroot->pos->ffield[m].key[i].skey);
k++;
}
for(m = len2 - 1; m >= 0; m--){
tabletemp->ffield[k].type = tableroot->next->pos->ffield[m].type;
if(tableroot->next->pos->ffield[m].type == 0)
tabletemp->ffield[k].key[n].intkey = tableroot->next->pos->ffield[m].key[j].intkey;
else
strcpy(tabletemp->ffield[k].key[n].skey, tableroot->next->pos->ffield[m].key[j].skey);
k++;
}
n++;
}
}
tabletemp->ilen = n;
}
else{
tabletemp->ffield = (struct field *)malloc((len1 + len2 - 1)*sizeof(struct field));
tabletemp->flen = len1 + len2 - 1;
k = 0;
for(i = len1 - 1; i >= 0; i--){
strcpy(tabletemp->ffield[k].name, tableroot->pos->ffield[i].name);
k++;
}
for(i = len2 - 1; i >= 0; i--){
if(i == pk2)
continue;
strcpy(tabletemp->ffield[k].name, tableroot->next->pos->ffield[i].name);
k++;
}
n = 0;
for(i = 0; i < tableroot->pos->ilen; i++){
for(j = 0; j < tableroot->next->pos->ilen; j++){
if(tableroot->pos->ffield[pk1].type == 0 && tableroot->pos->ffield[pk1].key[i].intkey == tableroot->next->pos->ffield[pk2].key[j].intkey || tableroot->pos->ffield[pk1].type == 1 && strcmp(tableroot->pos->ffield[pk1].key[i].skey, tableroot->next->pos->ffield[pk2].key[j].skey) == 0){
k = 0;
for(m = len1 - 1; m >= 0; m--){
tabletemp->ffield[k].type = tableroot->pos->ffield[m].type;
if(tableroot->pos->ffield[m].type == 0)
tabletemp->ffield[k].key[n].intkey = tableroot->pos->ffield[m].key[i].intkey;
else
strcpy(tabletemp->ffield[k].key[n].skey, tableroot->pos->ffield[m].key[i].skey);
k++;
}
for(m = len2 - 1; m >= 0; m--){
if(m == pk2)
continue;
tabletemp->ffield[k].type = tableroot->next->pos->ffield[m].type;
if(tableroot->next->pos->ffield[m].type == 0)
tabletemp->ffield[k].key[n].intkey = tableroot->next->pos->ffield[m].key[j].intkey;
else
strcpy(tabletemp->ffield[k].key[n].skey, tableroot->next->pos->ffield[m].key[j].skey);
k++;
}
n++;
}
}
}
tabletemp->ilen = n;
}
}
itemtemp = itemroot;
while(itemtemp != NULL){
for(i = 0; i < tabletemp->flen; i++){
if(strcmp(itemtemp->field, tabletemp->ffield[i].name) == 0){
itemtemp->pos = &tabletemp->ffield[i];
break;
}
}
if(i == tabletemp->flen){
printf("error: Column name does not match the table definition!\n");
free_conditions(conroot);
free_items(itemroot);
free_tables(tableroot);
return;
}
itemtemp = itemtemp->next;
}
if(itemroot == NULL){
for(j = 0; j < tabletemp->flen; j++){
printf("%-20s ", tabletemp->ffield[j].name);
}
printf("\n");
}
else{
itemtemp = itemroot;
while(itemtemp != NULL){
printf("%-20s ", itemtemp->field);
itemtemp = itemtemp->next;
}
printf("\n");
}
for(i = 0; i < tabletemp->ilen; i++){
if(whereTF(i, tabletemp, conroot) == TRUE){
if(itemroot == NULL){
for(j = 0; j < tabletemp->flen; j++){
if(tabletemp->ffield[j].type == 0)
printf("%-20d ", tabletemp->ffield[j].key[i].intkey);
else
printf("%-20s ", tabletemp->ffield[j].key[i].skey);
}
printf("\n");
}
else{
itemtemp = itemroot;
while(itemtemp != NULL){
if(itemtemp->pos->type == 0)
printf("%-20d ", itemtemp->pos->key[i].intkey);
else
printf("%-20s ", itemtemp->pos->key[i].skey);
itemtemp = itemtemp->next;
}
printf("\n");
}
}
}
free_conditions(conroot);
free_items(itemroot);
free_tables(tableroot);
return;
}
dbtemp = dbtemp->next;
}
free_conditions(conroot);
free_items(itemroot);
free_tables(tableroot);
printf("error: Database %s doesn't exist!\n", database);
}
/*删除操作。tableval为做删除的表的名称,conroot为条件二叉树的根节点,若为NULL,表示删除全部记录,
首先找到表,然后遍历表的每一条记录,若符合条件,则将后面的记录统一往前移动一条,将原纪录覆盖*/
void deletes(char *tableval, struct conditions_def *conroot){
struct mydb *dbtemp = NULL;
struct table *tabletemp = NULL;
int i,j,k;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
free_conditions(conroot);
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
if(strcmp(tabletemp->name, tableval) == 0){
for(i = 0; i < tabletemp->ilen; i++){
if(whereTF(i, tabletemp, conroot) == TRUE){
for(k = i; k < tabletemp->ilen-1; k++){
for(j = 0; j < tabletemp->flen; j++){
if(tabletemp->ffield[j].type == 0)
tabletemp->ffield[j].key[k].intkey = tabletemp->ffield[j].key[k+1].intkey;
else
strcpy(tabletemp->ffield[j].key[k].skey, tabletemp->ffield[j].key[k+1].skey);
}
}
tabletemp->ilen--;
i--;
}
}
printf("Delete successfully!\n");
free_conditions(conroot);
return;
}
tabletemp = tabletemp->next;
}
printf("error: The table doesn't exist!\n");
free_conditions(conroot);
return;
}
dbtemp = dbtemp->next;
}
free_conditions(conroot);
printf("error: Database %s doesn not exist!\n", database);
}
/*更新操作。tableval是要更新的表名称,uproot为赋值结构链表,conroot为条件二叉树的根节点。
先找到要更新的表,然后遍历赋值结构链表,找到对应的字段,并将其地址赋给每个赋值结构的pos变量,
便于后续操作,然后遍历表的每一条记录,判断符合条件则将对应的字段更新为对应的值*/
void updates(char *tableval, struct upcon_def *uproot, struct conditions_def *conroot){
struct mydb *dbtemp = NULL;
struct table *tabletemp = NULL;
struct upcon_def *uptemp = NULL;
int i,j;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
free_conditions(conroot);
free_upcons(uproot);
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
if(strcmp(tabletemp->name, tableval) == 0){
uptemp = uproot;
while(uptemp != NULL){
for(i = 0; i < tabletemp->flen; i++){
if(strcmp(tabletemp->ffield[i].name, uptemp->field) == 0){
uptemp->pos = &tabletemp->ffield[i];
break;
}
}
if(i == tabletemp->flen){
printf("error: Field %s does not exist!\n", uptemp->field);
free_conditions(conroot);
free_upcons(uproot);
return;
}
uptemp = uptemp->next;
}
for(i = 0; i < tabletemp->ilen; i++){
if(whereTF(i, tabletemp, conroot) == TRUE){
uptemp = uproot;
while(uptemp != NULL){
if(uptemp->pos->type == 0 && uptemp->type == 0)
uptemp->pos->key[i].intkey = uptemp->value.intkey;
else if(uptemp->pos->type == 1 && uptemp->type == 1)
strcpy(uptemp->pos->key[i].skey, uptemp->value.skey);
else{
printf("error: Data type mismatch!\n");
free_conditions(conroot);
free_upcons(uproot);
return;
}
uptemp = uptemp->next;
}
}
}
printf("Update successfully!\n");
free_conditions(conroot);
free_upcons(uproot);
return;
}
tabletemp = tabletemp->next;
}
printf("error: The table doesn't exist!\n");
free_conditions(conroot);
free_upcons(uproot);
return;
}
dbtemp = dbtemp->next;
}
free_conditions(conroot);
printf("error: Database %s doesn not exist!\n", database);
}
/*丢弃表,即链表的删除节点操作,删除时释放内存*/
void drop_table(char *tableval){
struct mydb *dbtemp = NULL;
struct table *tabletemp = NULL;
struct table *tabletod = NULL;
int i,j;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
return;
}
while(dbtemp != NULL){
if(strcmp(dbtemp->name,database) == 0){
tabletemp = dbtemp->tbroot;
if(strcmp(dbtemp->tbroot->name, tableval) == 0){
tabletod = dbtemp->tbroot;
dbtemp->tbroot = dbtemp->tbroot->next;
free(tabletod->ffield);
free(tabletod);
printf("Drop table successfully!\n");
return;
}
if(dbtemp->tbroot->next == NULL)
dbtemp->tbroot = NULL;
while(tabletemp->next != NULL){
if(strcmp(tabletemp->next->name, tableval) == 0){
tabletod = tabletemp->next;
tabletemp->next = tabletemp->next->next;
free(tabletod->ffield);
free(tabletod);
printf("Drop table successfully!\n");
return;
}
tabletemp = tabletemp->next;
}
if(strcmp(tabletemp->name, tableval) == 0){
free(tabletemp->ffield);
free(tabletemp);
printf("Drop table successfully!\n");
return;
}
printf("error: The table doesn't exist!\n");
return;
}
dbtemp = dbtemp->next;
}
printf("error: Database %s doesn not exist!\n", database);
}
/*丢弃数据库,即链表的删除节点操作,删除时释放内存*/
void drop_database(char *dbname){
struct mydb *dbtemp = NULL;
struct mydb *dbtod = NULL;
struct table *tabletemp = NULL;
struct table *tabletod = NULL;
int i,j;
if(dbroot != NULL)
dbtemp = dbroot;
else{
printf("error: Please create a database first!\n");
return;
}
if(strcmp(dbroot->name, dbname) == 0){
tabletemp = dbroot->tbroot;
while(tabletemp != NULL){
tabletod = tabletemp->next;
free(tabletemp->ffield);
free(tabletemp);
tabletemp = tabletod;
}
dbtod = dbroot;
dbroot = dbroot->next;
free(dbtod);
printf("Drop database successfully!\n");
return;
}
if(dbroot->next == NULL)
dbroot = NULL;
while(dbtemp->next != NULL){
if(strcmp(dbtemp->next->name, dbname) == 0){
tabletemp = dbtemp->next->tbroot;
while(tabletemp != NULL){
tabletod = tabletemp->next;
free(tabletemp->ffield);
free(tabletemp);
tabletemp = tabletod;
}
dbtod = dbtemp->next;
dbtemp->next = dbtemp->next->next;
free(dbtod);
printf("Drop database successfully!\n");
return;
}
dbtemp = dbtemp->next;
}
if(strcmp(dbtemp->name, dbname) == 0){
tabletemp = dbtemp->tbroot;
while(tabletemp != NULL){
tabletod = tabletemp->next;
free(tabletemp->ffield);
free(tabletemp);
tabletemp = tabletod;
}
free(dbtemp);
printf("Drop database successfully!\n");
return;
}
printf("error: Database %s doesn not exist!\n", database);
}