-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.function.c
2342 lines (2206 loc) · 47 KB
/
sql.function.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
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
/*
*函数实现与C例程
*by axknightroad
*2013/05/27
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include "sql.struct.h"
#define PAGE_SIZE 2048 /* 页大小 */
char now_db[50]; //当前数据库
char *xjgl="XJGL"; //缺省数据库
char null[5]; //空字符串
/*
栈函数
static struct DataStack *DStack;
static struct LineStack *LStack;
int dsisempty()
{
return DStack==NULL;
}
int lsisempty()
{
return LStack==NULL;
}
int isfull()
{
return 0;
}
void dpush(struct DataStack *ds,struct t_data *td)
{
struct DataStack *temp=malloc(sizeof(struct DataStack));
//assert(!isfull());
//assert(temp!=NULL);
temp->num=1+ds->num;
temp->next=ds;
ds=temp;
ds->td=td;
}
struct t_data *dpop(struct DataStack *ds)
{
struct t_data *td;
struct DataStack *temp;
//assert(!dsisempty());
temp=ds;
td=ds->td;
ds=temp->next;
free(temp);
return td;
}
void lpush(struct LineStack *ls,struct t_line *tl)
{
struct DataStack *temp=malloc(sizeof(struct DataStack));
//assert(!isfull());
//assert(temp!=NULL);
//temp->num=1+ls->num;
temp->next=ls;
ls=temp;
ls->tl=tl;
}
struct t_line *lpop(struct LineStack *ls)
{
struct t_line *tl;
struct DataStack *temp;
//assert(!lsisempty());
temp=ls;
tl=ls->tl;
ls=temp->next;
free(temp);
return tl;
}
*/
/*
*构造节点的函数定义
*/
/*构造create语句中字段的语法树*/
struct Create_field *new_create_field(char *field,int type,int length)
{
struct Create_field *a =malloc(sizeof(struct Create_field));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->field=field;
a->type=type;
a->length=length;
return a;
}
/*构造create语句中字段组的语法树*/
struct Create_fields *new_create_fields(struct Create_field *cf,struct Create_fields *next_cf)
{
struct Create_fields *a =malloc(sizeof(struct Create_fields));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->cf=cf;
a->next_cf=next_cf;
return a;
}
/*构造create语法树的根节点*/
struct Create_struct *new_create_struct(char *table,struct Create_fields *cf)
{
struct Create_struct *a =malloc(sizeof(struct Create_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->cf=cf;
return a;
}
/*构造select语法树的根节点的语法树*/
struct Select_struct *new_select_struct(int select_flag,struct Select_fields *sf,struct Select_tables *st,struct Conditions *cons,struct Groupby *gb,struct Conditions *ha,struct Groupby *ob)
{
struct Select_struct *a =malloc(sizeof(struct Select_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->select_flag=select_flag;
a->sf=sf;
a->st=st;
a->cons=cons;
a->gb=gb;
a->ha=ha;
a->ob=ob;
return a;
}
/*构造select语句条件字段的语法树*/
struct Conditions *new_conditions(struct Conditions *l,struct Conditions *r,int com_opt,int type,char *value,int num,float flo,int boolnum,char *table,int not_flag,int any_flag,int null_flag,int like_flag,int exists_flag,int fun_flag,int all_flag,int in_num,struct Select_struct *ss)
{
struct Conditions *a =malloc(sizeof(struct Conditions));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->l=l;
a->r=r;
a->com_opt=com_opt;
a->type=type;
a->value=value;
a->num=num;
a->flo=flo;
a->boolnum=boolnum;
a->table=table;
a->not_flag=not_flag;
a->any_flag=any_flag;
a->null_flag=null_flag;
a->like_flag=like_flag;
a->exists_flag=exists_flag;
a->fun_flag=fun_flag;
a->all_flag=all_flag;
a->in_num=in_num;
a->ss=ss;
return a;
}
/*构造select 语句中选中的字段的语法树*/
struct Select_fields *new_select_fields(char *table,char *field,struct Select_fields *next_sf,int sall_flag)
{
struct Select_fields *a =malloc(sizeof(struct Select_fields));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->field=field;
a->next_sf=next_sf;
a->sall_flag=sall_flag;
return a;
}
/*构造select语句中选中的表的语法树*/
struct Select_tables *new_select_tables(char *table,struct Select_tables *next_st)
{
struct Select_tables *a =malloc(sizeof(struct Select_tables));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->next_st=next_st;
return a;
}
/*构造select语句中Groupby语句的语法树*/
struct Groupby *new_groupby(struct Conditions *gr_con,struct Groupby *next_gr,int sc_flag)
{
struct Groupby *a =malloc(sizeof(struct Groupby));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->gr_con=gr_con;
a->next_gr=next_gr;
a->sc_flag=sc_flag;
return a;
}
/*构造val_list结构的语法树*/
struct Val_list_struct *new_val_list_struct(struct Conditions *val_con,int val_num)
{
struct Val_list_struct *a =malloc(sizeof(struct Val_list_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->val_con=val_con;
a->val_num=val_num;
return a;
}
/*构造delete语法根节点树*/
struct Delete_struct *new_delete_struct(char *table,struct Conditions *cons)
{
struct Delete_struct *a =malloc(sizeof(struct Delete_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->cons=cons;
return a;
}
/*构造字段名列表语法树*/
struct Column_list_struct *new_column_list_struct(char *name,struct Column_list_struct *next_cl)
{
struct Column_list_struct *a =malloc(sizeof(struct Column_list_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->name=name;
a->next_cl=next_cl;
return a;
}
/*构造Insert语句中值列表语法树*/
struct Insert_val_list_struct *new_insert_val_list_struct(struct Conditions *cons,struct Insert_val_list_struct *next_ivl)
{
struct Insert_val_list_struct *a =malloc(sizeof(struct Insert_val_list_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->cons=cons;
a->next_ivl=next_ivl;
return a;
}
/*构造Insert语法树根节点*/
struct Insert_struct *new_insert_struct(char *table,struct Column_list_struct *cl,struct Insert_val_list_struct *ivl)
{
struct Insert_struct *a =malloc(sizeof(struct Insert_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->cl=cl;
a->ivl=ivl;
return a;
}
/*构造update语句中列名及表达式语法树*/
struct Update_asgn *new_update_asgn(char *name,struct Conditions *con,struct Update_asgn *next)
{
struct Update_asgn *a =malloc(sizeof(struct Update_asgn));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->name=name;
a->con=con;
a->next=next;
return a;
}
/*构造update语法树的根节点*/
struct Update_struct *new_update_struct(char *table,struct Update_asgn *ua,struct Conditions *con)
{
struct Update_struct *a =malloc(sizeof(struct Update_struct));
if(!a)
{
yyerror("out of space");
exit(0);
}
a->table=table;
a->ua=ua;
a->con=con;
return a;
}
/*
* 函数定义
*/
void create_database(char *name) //创建数据库函数
{
FILE *fp1,*fp2,*fp3;
char db_name[50],dat_name[50];
strcpy(db_name,name);
strcpy(dat_name,name);
strcat(db_name,".db");
strcat(dat_name,".dat");
fp1=fopen(db_name,"ab+");
fp2=fopen(dat_name,"ab+");
fclose(fp1);
fclose(fp2);
int db_num=0;
char db_found[50];
fp3=fopen("db.info","rb+");
if(fp3==NULL)
fp3=fopen("db.info","wb+");
fseek(fp3,0,SEEK_SET);
fread(&db_num,sizeof(int),1,fp3);
int i;
for(i=0;i<db_num;i++)
{
fseek(fp3,sizeof(int)+i*50,SEEK_SET);
fread(db_found,50,1,fp3);
if(strcmp(db_found,name)==0)
{
printf("This database have been created\n");
exit(0);
}
}
fseek(fp3,sizeof(int)+50*db_num,SEEK_SET);
fwrite(name,50,1,fp3);
db_num++;
fseek(fp3,0,SEEK_SET);
fwrite(&db_num,sizeof(int),1,fp3);
fclose(fp3);
printf("Create database successed\n");
}
void drop_database(char *name) //删除数据库函数
{
char db_name[50],dat_name[50];
strcpy(db_name,name);
strcpy(dat_name,name);
strcat(db_name,".db");
strcat(dat_name,".dat");
unlink(db_name);
unlink(dat_name);
FILE *fp3;
char s[50];
int db_num,this_db_num,i;
char db_found[50];
fp3=fopen("db.info","rb+");
fseek(fp3,0,SEEK_SET);
fread(&db_num,sizeof(int),1,fp3);
for(i=0;i<db_num;i++)
{
fseek(fp3,sizeof(int)+i*50,SEEK_SET);
fread(db_found,50,1,fp3);
if(strcmp(db_found,name)==0)
{
this_db_num=i;
break;
}
if(i==db_num-1)
{
printf("no this database\n");
exit(0);
}
}
if(this_db_num==db_num-1)
{
fseek(fp3,sizeof(int)+this_db_num*50,SEEK_SET);
fwrite(null,5,10,fp3);
}
else
{
fseek(fp3,sizeof(int)+db_num-1*50,SEEK_SET);
fread(db_found,50,1,fp3);
fseek(fp3,sizeof(int)+this_db_num*50,SEEK_SET);
fwrite(db_found,50,1,fp3);
fseek(fp3,sizeof(int)+db_num-1*50,SEEK_SET);
fwrite(null,5,10,fp3);
}
db_num--;
fseek(fp3,0,SEEK_SET);
fwrite(&db_num,sizeof(int),1,fp3);
fclose(fp3);
printf("Drop database successed\n");
}
void show_databases()
{
FILE *fp3;
int db_num,this_db_num,i;
char db_found[50];
fp3=fopen("db.info","rb+");
fseek(fp3,0,SEEK_SET);
fread(&db_num,sizeof(int),1,fp3);
for(i=0;i<db_num;i++)
{
fseek(fp3,sizeof(int)+i*50,SEEK_SET);
fread(db_found,50,1,fp3);
printf("database:%s\n",db_found);
}
}
void use(char *name)
{
strcpy(now_db,name);
FILE *fp1;
char db_name[50];
strcpy(db_name,now_db);
strcat(db_name,".db");
fp1=fopen(db_name,"rb+");
fseek(fp1,PAGE_SIZE-50,SEEK_SET);
fread(null,5,1,fp1);
printf("use database %s now\n",name);
}
void create_table(struct Create_struct *t_name) //创建表
{
//打开元数据文件和基本数据文件
FILE *fp1,*fp2;
char db_name[50],dat_name[50];
strcpy(db_name,now_db);
strcpy(dat_name,now_db);
strcat(db_name,".db");
strcat(dat_name,".dat");
fp1=fopen(db_name,"rb+");
fp2=fopen(dat_name,"rb+");
int db_page,dat_page,used_page;
db_page=0;
dat_page=0;
used_page=-1;
//确定空白页
fseek(fp1,PAGE_SIZE*(db_page+1)-sizeof(int),SEEK_SET);
//int t=ftell(fp1);
int t2=fread(&used_page,sizeof(int),1,fp1);
while(db_page==used_page)
{
db_page++;
fseek(fp1,PAGE_SIZE*(db_page+1)-sizeof(int),SEEK_SET);
if(fread(&used_page,sizeof(int),1,fp1)!=1)
break;
}
//确认是否重复
char table_name[20],now_table[20];
int i=0;
for(i=0;i<20;i++)
{
table_name[i]='\0';
table_name[i]='\0';
}
strcpy(table_name,t_name->table);
rewind(fp1);
if(fread(now_table,20,1,fp1)==1)
{
int p;
for(p=0;p<db_page;p++)
{
fseek(fp1,PAGE_SIZE*p,SEEK_SET);
if(fread(now_table,20,1,fp1)!=1)
{
printf("Error in reading data\n");
exit(0);
}
if(strcmp(table_name,now_table)==0)
{
printf("This table has been created\n");
exit(0);
}
}
}
//写表字典
fseek(fp1,PAGE_SIZE*db_page,SEEK_SET);
fwrite(table_name,20,1,fp1); //向表字典写表名
if(db_page>1)
{
int last_table_db_page=db_page-2;
fseek(fp1,PAGE_SIZE*last_table_db_page+20,SEEK_SET);
if(fread(&dat_page,sizeof(int),1,fp1)!=1)
{
printf("Error in reading data\n");
exit(0);
}
dat_page++;
}
fseek(fp2,PAGE_SIZE*(dat_page+1)-sizeof(int),SEEK_SET);
if(fread(&used_page,sizeof(int),1,fp2)==1)
{
while(dat_page==used_page)
{
dat_page++;
fseek(fp2,PAGE_SIZE*(dat_page+1)-sizeof(int),SEEK_SET);
if(fread(&used_page,sizeof(int),1,fp2)!=1)
break;
}
}
fseek(fp2,PAGE_SIZE*(dat_page+1)-sizeof(int),SEEK_SET);
fwrite(&dat_page,sizeof(int),1,fp2);
fseek(fp1,PAGE_SIZE*db_page+20,SEEK_SET);
fwrite(&dat_page,sizeof(int),1,fp1); //向表字典写起始页
fseek(fp1,PAGE_SIZE*(db_page+1)-sizeof(int),SEEK_SET);
fwrite(&db_page,sizeof(int),1,fp1);
//写列字典
db_page++;
fseek(fp1,PAGE_SIZE*db_page,SEEK_SET);
struct Create_fields *a=malloc(sizeof(struct Create_fields));
struct Create_field *b=malloc(sizeof(struct Create_field));
a=t_name->cf;
char row_name[20];
for(i=0;i<20;i++)
{
row_name[i]='\0';
}
int row_offset=0;
int row_length=0;
int row=0;
while(a!=NULL)
{
b=a->cf;
if(fwrite(table_name,20,1,fp1)!=1)//向列字典写表名
{
printf("Error in writeing data\n");
exit(0);
}
if(fwrite(&row,sizeof(int),1,fp1)!=1) //向列字典写列号
{
printf("Error in writeing data\n");
exit(0);
}
row++;
strcpy(row_name,b->field);
if(fwrite(row_name,20,1,fp1)!=1)//写列名
{
printf("Error in writeing data\n");
exit(0);
}
row_offset=row_offset+row_length;
if(fwrite(&row_offset,sizeof(int),1,fp1)!=1)//写偏移量
{
printf("Error in writeing data\n");
exit(0);
}
if(b->type==0)
{
row_length=sizeof(int);
if(fwrite(&row_length,sizeof(int),1,fp1)!=1)//写宽度
{
printf("Error in writeing data\n");
exit(0);
}
int type=0;
fwrite(&type,sizeof(int),1,fp1);//写数据类型
}
else
{
row_length=b->length;
if(fwrite(&row_length,sizeof(int),1,fp1)!=1)//写宽度
{
printf("Error in writeing data\n");
exit(0);
}
int type=1;
fwrite(&type,sizeof(int),1,fp1); //写数据类型
}
a=a->next_cf;
}
fseek(fp1,PAGE_SIZE*(db_page+1)-sizeof(int),SEEK_SET);
if(fwrite(&db_page,sizeof(int),1,fp1)!=1)
{
printf("Error in writeing data\n");
exit(0);
}
fclose(fp1);
fclose(fp2);
printf("Create table successed\n");
}
void insert_eval(struct Insert_struct *name){ //计算insert函数
//打开基本数据文件和元数据文件
FILE *fp1,*fp2;
char db_name[50],dat_name[50];
strcpy(db_name,now_db);
strcpy(dat_name,now_db);
strcat(db_name,".db");
strcat(dat_name,".dat");
fp1=fopen(db_name,"rb");
fp2=fopen(dat_name,"rb+");
int db_page,dat_page,used_page; //元数据文件和基本数据文件页数
db_page=0;
dat_page=0;
used_page=-1;
//确定该表在元数据文件所在页
char table_name[20],now_table[20];
int i=0;
for(i=0;i<20;i++)
{
table_name[i]='\0';
table_name[i]='\0';
}
strcpy(table_name,name->table);
rewind(fp1);
if(fread(now_table,20,1,fp1)==1)
{
while(strcmp(table_name,now_table)!=0)
{
db_page++;
fseek(fp1,PAGE_SIZE*db_page,SEEK_SET);
if(fread(now_table,20,1,fp1)!=1)
{
printf("There is no this table\n");
exit(0);
}
}
}
else
{
printf("There is no this table\n");
exit(0);
}
if(fread(&dat_page,sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
fseek(fp2,PAGE_SIZE*dat_page,SEEK_SET);//将基本数据文件读写头移到该表存储页
//获取表信息
db_page++;
fseek(fp1,PAGE_SIZE*db_page,SEEK_SET);
int row_num=0; //该表共有列数
char eof_flag[20];
i=0;
for(i=0;i<20;i++)
{
eof_flag[i]='\0';
}
int rowsize=sizeof(int)*4+40; //列字典每行长度
if(fread(eof_flag,20,1,fp1)!=1)
{
printf("There is no this table\n");
exit(0);
}
while(strcmp(eof_flag,table_name)==0)
{
row_num++;
fseek(fp1,rowsize*row_num+PAGE_SIZE*db_page,SEEK_SET);
if(fread(eof_flag,20,1,fp1)!=1)
break;
}
fseek(fp1,PAGE_SIZE*db_page+rowsize*row_num-3*sizeof(int),SEEK_SET);
int line_offset,tmp0; //该表每行的偏移量
if(fread(&line_offset,sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
if(fread(&tmp0,sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
line_offset=tmp0+line_offset;
int line_num=0; //该表现有行数
char tmp2[5];
i=0;
for(i=0;i<5;i++)
{
tmp2[i]='\0';
}
fseek(fp2,PAGE_SIZE*dat_page,SEEK_SET);
fread(tmp2,5,1,fp2);
while(strcmp(null,tmp2)!=0)
{
line_num++;
fseek(fp2,line_offset*line_num+PAGE_SIZE*dat_page,SEEK_SET);
fread(tmp2,2,1,fp2);
}
fseek(fp2,PAGE_SIZE*dat_page+line_num*line_offset,SEEK_SET); //读写头移到空白处准备写入数据
fseek(fp1,PAGE_SIZE*db_page,SEEK_SET);
int row_offset[row_num],row_length[row_num],row_type[row_num];//该表每列偏移量、长度、数据类型
char row_name[row_num*20]; //每列名称
for(i=0;i<20*row_num;i++)
{
row_name[i]='\0';
}
for(i=0;i<row_num;i++)
{
fseek(fp1,20+sizeof(int)+PAGE_SIZE*db_page+i*rowsize,SEEK_SET);
if(fread(&row_name[i*20],1,20,fp1)!=20)
{
printf("Erroe in reading data\n");
exit(0);
}
if(fread(&row_offset[i],sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
if(fread(&row_length[i],sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
if(fread(&row_type[i],sizeof(int),1,fp1)!=1)
{
printf("Erroe in reading data\n");
exit(0);
}
}
struct Insert_struct *is=malloc(sizeof(struct Insert_struct));
is=name;
//写入数据
fseek(fp2,dat_page*PAGE_SIZE+line_num*line_offset,SEEK_SET);
int p=ftell(fp2);
struct Column_list_struct *cl;
struct Insert_val_list_struct *ivl;
cl=is->cl;
ivl=is->ivl;
for(i=0;i<row_num;i++)
{
p=ftell(fp2);
if(is->cl==NULL) //所有属性列都有数据
{
switch(ivl->cons->type)
{
case 2: //添加字符串
{
char s[22],s1[20];
strcpy(s,ivl->cons->value);
int j=0;
while(s[j+1]!='\'')
{
s1[j]=s[j+1];
j++;
}
int k;
for(k=j;k<20;k++)
{
s1[k]='\0';
}
if(fwrite(s1,row_length[i],1,fp2)!=1)
{
printf("Erroe in writeing data\n");
exit(0);
}
}
break;
case 3: //添加整数
{
int rv2=ivl->cons->num;
if(fwrite(&rv2,row_length[i],1,fp2)!=1)
{
printf("Erroe in writeing data\n");
exit(0);
}
}
break;
}
ivl=ivl->next_ivl;
}
else //添加个别列
{
if(cl==NULL)
{
fwrite("null",4,1,fp2);
if(row_length[i]-4>0)
fwrite('\0',1,row_length[i]-4,fp2);
goto over;
}
char t1[20],t2[20];
int k;
for(k=0;k<20;k++)
t1[k]=row_name[i*20+k];
strcpy(t2,cl->name);
if(strcmp(t1,t2)==0)
{
switch(ivl->cons->type)
{
case 2:
{
char s[22],s1[20];
strcpy(s,ivl->cons->value);
int j=0;
while(s[j+1]!='\'')
{
s1[j]=s[j+1];
j++;
}
int k;
for(k=j;k<20;k++)
{
s1[k]='\0';
}
if(fwrite(s1,row_length[i],1,fp2)!=1)
{
printf("Erroe in writeing data\n");
exit(0);
}
}
break;
case 3:
{
int rv2=ivl->cons->num;
if(fwrite(&rv2,row_length[i],1,fp2)!=1)
{
printf("Erroe in writeing data\n");
exit(0);
}
}
break;
}
}
next:ivl=ivl->next_ivl;
cl=cl->next_cl;
}
}
over:fclose(fp1);
fclose(fp2);
printf("Insert successed\n");
}
struct t_data *loaddata(FILE *fp2,int row_num,char row_name[],int row_type[],int row_length[],int row_offset[],int lnum,int rnum,int dat_page,int line_offset,int line_num)
{ //读每项数据
struct t_data *a=malloc(sizeof(struct t_data));
a->lnum=lnum;
a->loffset=line_offset;
a->rnum=rnum;
int i;
char name0[20];
char value[row_length[rnum]];
for(i=0;i<20;i++)
name0[i]=row_name[rnum*20+i];
//strncpy(name0,&row_name[num*20],20);
a->name=name0;
a->roffset=row_offset[rnum];
a->type=row_type[rnum];
// fseek(fp2,line_offset*line_num+PAGE_SIZE*dat_page+row_offset[num],SEEK_SET);
char s[4];
if(fread(s,4,1,fp2)!=1)
{
printf("Error in reading data\n");
exit(0);
}
if(strcmp(s,"null")==0) //判断是否为空数据
{
a->nflag=1;
a->value=NULL;
a->num=-1;
fseek(fp2,-4,SEEK_CUR);
fseek(fp2,row_length[rnum],SEEK_CUR);
}
else //读具体数据
{
fseek(fp2,-4,SEEK_CUR);
//fseek(fp2,PAGE_SIZE*dat_page+row_offset[rnum]+line_offset*lnum,SEEK_SET);
a->nflag=0;
if(a->type==0)
{
int anum;
if(fread(&anum,sizeof(int),1,fp2)!=1)
{
printf("Error in reading data\n");
exit(0);
}
a->num=anum;
a->length=0;
}
else
{
if(fread(value,row_length[rnum],1,fp2)!=1)
{
printf("Error in reading data\n");
exit(0);
}
//strcpy(va,value);
// value2=value0;
// *value1=*value2;
a->value=value;
a->length=row_length[rnum];
}
}
a->rsflag=0;
if(rnum<row_num-1)
a->next_data=loaddata(fp2,row_num,row_name,row_type,row_length,row_offset,lnum,rnum+1,dat_page,line_offset,line_num);
else if(lnum<line_num-1)
a->next_data=loaddata(fp2,row_num,row_name,row_type,row_length,row_offset,lnum+1,0,dat_page,line_offset,line_num);
else
a->next_data=NULL;
return a;
}
/*
struct t_line *loadline(FILE *fp2,int line_num,int row_num,char row_name[],int row_type[],int row_length[],int line_offset,int row_offset[],int num,int dat_page) //读行数据
{
struct t_line *a=malloc(sizeof(struct t_line));
a->lnum=num;
a->loffset=line_offset;
a->sflag=0;
fseek(fp2,dat_page*PAGE_SIZE+num*line_offset,SEEK_SET);
int i;
/*struct t_data *td;
for(i=0;i<row_num-1;i++)
{
td=loaddata(fp2,row_num,row_name,row_type,row_length,row_offset,i,dat_page,line_offset,num); //读此行数据
dpush(DStack,td);
}
a->ld=loaddata(fp2,row_num,row_name,row_type,row_length,row_offset,0,dat_page,line_offset,num);
// td->next_data=NULL;
/* for(i=0;i<row_num-1;i++)
{
struct t_data *temp;
temp=td;
td=dpop(DStack);
td->next_data=temp;
}
//a->ld=td;
if(num<line_num-1)
a->next_line=loadline(fp2,line_num,row_num,row_name,row_type,row_length,line_offset,row_offset,num+1,dat_page); //递归下一行
else
a->next_line=NULL;
return a;
}
*/
struct table_s *loadtable(FILE *fp1,FILE *fp2,int table_num) //读表数据
{
struct table_s *a=malloc(sizeof(struct table_s));
int dat_page,db_page;
char table_name[20];