/
main.c
1124 lines (990 loc) · 25.9 KB
/
main.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
/*
* main.c -- メインモジュール
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <tchar.h>
#include <string.h>
#include "microdb.h"
/*
* MAX_INPUT -- 入力行の最大文字数
*/
#define MAX_INPUT 256
/*
* TEST_SIZE -- 挿入するレコード数
*/
#define TEST_SIZE 1000
#define MY_MIN 1
#define MY_MAX 5
/*
* inputString -- 字句解析中の文字列を収める配列
*
* 大きさを「3 * MAX_INPUT」とするのは、入力行のすべての文字が区切り記号でも
* バッファオーバーフローを起こさないようにするため。
*/
static char inputString[3 * MAX_INPUT];
/*
* nextPosition -- 字句解析中の場所
*/
static char *nextPosition;
/*
* setInputString -- 字句解析する文字列の設定
*
* 引数:
* string: 解析する文字列
*
* 返り値:
* なし
*/
static void setInputString(char *string)
{
char *p = string;
char *q = inputString;
/* 入力行をコピーして保存する */
while (*p != '\0') {
/* 引用符の場合には、次の引用符まで読み飛ばす */
if (*p == '\'' || *p == '"') {
char *quote;
quote = p;
*q++ = *p++;
/* 閉じる引用符(または文字列の最後)までコピーする */
while (*p != *quote && *p != '\0') {
*q++ = *p++;
}
/* 閉じる引用符自体もコピーする */
if (*p == *quote) {
*q++ = *p++;
}
continue;
}
/* 区切り記号の場合には、その前後に空白文字を入れる */
if (*p == ',' || *p == '(' || *p == ')' || *p == '.') {
*q++ = ' ';
*q++ = *p++;
*q++ = ' ';
}
else {
*q++ = *p++;
}
}
*q = '\0';
/* getNextToken()の読み出し開始位置を文字列の先頭に設定する */
nextPosition = inputString;
}
/*
* getNextToken -- setInputStringで設定した文字列からの字句の取り出し
*
* 引数:
* なし
*
* 返り値:
* 次の字句の先頭番地を返す。最後までたどり着いたらNULLを返す。
*/
static char *getNextToken()
{
char *start;
char *end;
char *p;
/* 空白文字が複数続いていたら、その分nextPositionを移動させる */
while (*nextPosition == ' ') {
nextPosition++;
}
start = nextPosition;
/* nextPositionの位置が文字列の最後('\0')だったら、解析終了 */
if (*nextPosition == '\0') {
return NULL;
}
/* nextPositionの位置以降で、最初に見つかる空白文字の場所を探す */
p = nextPosition;
while (*p != ' ' && *p != '\0') {
/* 引用符の場合には、次の引用符まで読み飛ばす */
if (*p == '\'' || *p == '"') {
char *quote;
quote = p;
p++;
/* 閉じる引用符(または文字列の最後)まで読み飛ばす */
while (*p != *quote && *p != '\0') {
p++;
}
/* 閉じる引用符自体も読み飛ばす */
if (*p == *quote) {
p++;
}
}
else {
p++;
}
}
end = p;
/*
* 空白文字を終端文字で置き換えるとともに、
* 次回のgetNextToken()の呼び出しのために
* nextPositionをその次の文字に移動する
*/
if (*end != '\0') {
*end = '\0';
nextPosition = end + 1;
}
else {
/*
* (*end == '\0')の場合は文字列の最後まで解析が終わっているので、
* 次回のgetNextToken()の呼び出しのときにNULLが返るように、
* nextPositionを文字列の一番最後に移動させる
*/
nextPosition = end;
}
/* 字句の先頭番地を返す */
return start;
}
/*
*condtionSet -- select文とdelete文のconditionを決定する
*
* 引数:token
* :condition
*/
static void conditionSet(char *token, Condition *condition){
char *p; /*stringが''で囲まれているかを調べるのに用いる*/
char *q; /*''を取り除いた文字列をいれる*/
char *quote; /*シングルコーテーション*/
/*フィールドのデータ型が探索できないときは、文法エラー*/
if (condition->dataType == TYPE_UNKNOWN){
printf("指定したフィールドはありません%s\n", condition->name);
return;
}
/*次のトークンである比較演算子を読み込む*/
if ((token = getNextToken()) == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/*比較演算子をconditionに設定*/
if (strcmp(token, "=") == 0){
condition->operator=OPR_EQUAL;
}
else if (strcmp(token, "!=") == 0){
condition->operator=OPR_NOT_EQUAL;
}
else if (strcmp(token, ">") == 0){
condition->operator=OPR_GREATER_THAN;
}
else if (strcmp(token, "<") == 0){
condition->operator=OPR_LESS_THAN;
}
else if (strcmp(token, ">=") == 0){
condition->operator=OPR_GREATER_THAN_OR_EQUAL;
}
else if (strcmp(token, "<=") == 0){
condition->operator=OPR_LESS_THAN_OR_EQUAL;
}
else {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/*次のトークンである条件式を読み込む*/
if ((token = getNextToken()) == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/*条件式の値をconditionに設定*/
switch (condition->dataType){
case TYPE_INTEGER:
/*トークンの文字列を整数に変換して入れるよ!*/
condition->intValue = atoi(token);
break;
case TYPE_STRING:
/*'で囲まれているかを調べる*/
p = token;
/*頭が'かどうか調べる*/
if (*p == '\'') {
quote = p;
p++;
q = p; /*'をとばしてqに値を格納していく*/
/* 文字列の最後まで読み込む */
while (*p != *quote && *p != '\0') {
p++;
}
/* 終わりが'じゃなかったときは */
if (*p != '\'') {
printf("条件式の指定に間違いがあります%s\n", token);
return;
}
}
else{
printf("条件式の指定に間違いがあります%s\n", token);
return;
}
*p = '\0';
strcpy(condition->stringValue, q);
break;
default:
printf("データタイプが適切ではありませんでした\n");
break;
}
}
/*
* callCreateTable -- create文の構文解析とcreateTableの呼び出し
*
* 引数:
* なし
*
* 返り値:
* なし
*
* create tableの書式:
* create table テーブル名 ( フィールド名 データ型, ... )
*/
void callCreateTable()
{
char *token;
char *tableName;
int numField;
TableInfo tableInfo;
/* createの次のトークンを読み込み、それが"table"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "table") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* テーブル名を読み込む */
if ((tableName = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", tableName);
return;
}
/* 次のトークンを読み込み、それが"("かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "(") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/*
* ここから、フィールド名とデータ型の組を繰り返し読み込み、
* 配列fieldInfoに入れていく。
*/
numField = 0;
for (;;) {
/* フィールド名の読み込み */
if ((token = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* 読み込んだトークンが")"だったら、ループから抜ける */
if (strcmp(token, ")") == 0) {
break;
}
/* フィールド名を配列に設定 */
strcpy(tableInfo.fieldInfo[numField].name, token);
/* データ型の読み込み */
token = getNextToken();
if (token == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* データ型を配列に設定 (型によって場合分け)*/
if (strcmp(token, "integer") == 0){
tableInfo.fieldInfo[numField].dataType = TYPE_INTEGER;
}
else if (strcmp(token, "string") == 0){
tableInfo.fieldInfo[numField].dataType = TYPE_STRING;
}
else{
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* フィールド数をカウントする */
numField++;
/* フィールド数が上限を超えていたらエラー */
if (numField > MAX_FIELD) {
printf("フィールド数が上限を超えています。\n");
return;
}
/* 次のトークンの読み込み */
if ((token = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* 読み込んだトークンが")"だったら、ループから抜ける */
if (strcmp(token, ")") == 0) {
break;
}
else if (strcmp(token, ",") == 0) {
/* 次のフィールドを読み込むため、ループの先頭へ */
continue;
}
else {
/* 文法エラー */
printf("入力行に間違いがあります。\n");
return;
}
}
tableInfo.numField = numField;
/* createTableを呼び出し、テーブルを作成 */
if (createTable(tableName, &tableInfo) == OK) {
printf("テーブルを作成しました。\n");
}
else {
printf("テーブルの作成に失敗しました。\n");
}
}
/*
* callDropTable -- drop文の構文解析とdropTableの呼び出し
*
* 引数:
* なし
*
* 返り値:
* なし
*
* drop tableの書式:
* drop table テーブル名
*/
void callDropTable()
{
char *token;
char *tableName;
/*dropの次のトークンを読み込み、それが"table"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "table") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* テーブル名を読み込む */
if ((tableName = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", tableName);
return;
}
/* createTableを呼び出し、テーブルを削除 */
if (dropTable(tableName) == OK) {
printf("テーブルを削除しました。\n");
}
else {
printf("テーブルの削除に失敗しました。\n");
}
}
/*
* callInsertRecord -- insert文の構文解析とinsertRecordの呼び出し
*
* 引数:
* なし
*
* 返り値:
* なし
*
* insertの書式:
* insert into テーブル名 values ( フィールド値 , ... )
*/
void callInsertRecord()
{
char *token;
char *tableName;
int numField;
RecordData *recordData;
TableInfo *tableInfo;
int i;
/*insertの次のトークンを読み込み、それが"into"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "into") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* テーブル名を読み込む */
if ((tableName = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", tableName);
return;
}
/* 次のトークンを読み込み、それが"values"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "values") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* 次のトークンを読み込み、それが"("かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "(") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* テーブルの情報を取得する */
if ((tableInfo = getTableInfo(tableName)) == NULL) {
/* エラー処理 */
return;
}
/*RecordData構造体のメモリを確保*/
if ((recordData = (RecordData *)malloc(sizeof(RecordData))) == NULL){
fprintf(stderr, "Malloc error.\n");
return;
}
numField = 0;
for (i = 0; i < tableInfo->numField; i++) {
/* フィールド名の読み込み */
if ((token = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
freeTableInfo(tableInfo);
return;
}
switch (tableInfo->fieldInfo[i].dataType) {
case TYPE_INTEGER:
recordData->fieldData[i].intValue = atoi(token);
break;
case TYPE_STRING:
strcpy(recordData->fieldData[i].stringValue, token);
break;
default:
freeTableInfo(tableInfo);
return;
}
numField++;
/* 次のトークンの読み込み */
token = getNextToken();
if (token == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
freeTableInfo(tableInfo);
return;
}
if (strcmp(token, ")") == 0){
/*ループから出る*/
break;
}
else if (strcmp(token, ",") == 0) {
/* 次のフィールドを読み込むため、ループの先頭へ */
continue;
}
else {
/* 文法エラー */
printf("入力行に間違いがあります。\n");
freeTableInfo(tableInfo);
return;
}
}
if (numField != tableInfo->numField){
return;
}
freeTableInfo(tableInfo);
/* insertRecordを呼び出し、レコードを挿入 */
if (insertRecord(tableName, recordData) == OK) {
printf("レコードを挿入しました。\n");
}
else {
printf("レコードの挿入に失敗しました。\n");
}
free(recordData);
}
/*
* callSelectRecord -- select文の構文解析とselectRecordの呼び出し
*
* 引数:
* なし
*
* 返り値:
* なし
*
* selectの書式:
* select * from テーブル名 where 条件式
* select フィールド名 , ... from テーブル名 where 条件式 (発展課題)
*/
void callSelectRecord()
{
char *token;
char *tableName;
char *selectField;
char *p;
Condition condition;
TableInfo *tableInfo;
RecordSet *recordSet;
int i, j;
int numField;
int allSelectFlag; /* *かどうかの判断 */
char sendFieldName[MAX_FIELD][MAX_FIELD_NAME];
for (i = 0; i < MAX_FIELD; i++){
memset(&sendFieldName[i], '\0', MAX_FIELD_NAME);
}
/*selectの次のトークンを読み込み、distinctが存在するかをチェック */
token = getNextToken();
if (token == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
if (strcmp(token, "distinct") == 0){
condition.distinct = DISTINCT;
/*次のトークンを読み込み、それが"*"かどうかをチェック */
token = getNextToken();
if (token == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
}
else{
condition.distinct = NOT_DISTINCT;
}
selectField = (char *)malloc(sizeof(char)*MAX_FIELD_NAME*MAX_FIELD);
p = selectField;
numField = 0;
if (strcmp(token, "*") != 0){
allSelectFlag = 0;
for (;;) {
memcpy(p, token, strlen(token) + 1);
p += strlen(token);
/* フィールド数をカウントする */
numField++;
/* 次のトークンの読み込み */
if ((token = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
/* 読み込んだトークンが"from"だったら、ループから抜ける */
if (strcmp(token, "from") == 0) {
break;
}
else if (strcmp(token, ",") == 0) {
token = " ";
memcpy(p, token, strlen(token) + 1);
p += strlen(token);
/* 次のトークンの読み込み */
if ((token = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
continue;
}
else {
/* 文法エラー */
printf("入力行に間違いがあります。%s\n", token);
return;
}
}
}
/* *だったら */
else if (strcmp(token, "*") == 0){
allSelectFlag = 1;
token = getNextToken();
if (strcmp(token, "from") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
}
/* テーブル名を読み込む */
if ((tableName = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", tableName);
return;
}
/*次のトークンを読み込み、それが"where"かどうかをチェック */
token = getNextToken();
/*条件が入力されなかったらすべてのレコードを表示*/
if (token == NULL) {
/* テーブルの情報を取得する */
if ((tableInfo = getTableInfo(tableName)) == NULL) {
/* エラー処理 */
return;
}
numField = tableInfo->numField;
printTableData(tableName, numField);
freeTableInfo(tableInfo);
free(selectField);
return;
}
if (strcmp(token, "where") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* 条件式のフィールド名を読み込む */
if ((token = getNextToken()) == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
strcpy(condition.name, token);
/* テーブルの情報を取得する */
if ((tableInfo = getTableInfo(tableName)) == NULL) {
/* エラー処理 */
return;
}
/*条件式に指定されたフィールドのデータ型を調べる*/
condition.dataType = TYPE_UNKNOWN;
for (i = 0; i < tableInfo->numField; i++){
if (strcmp(tableInfo->fieldInfo[i].name, condition.name) == 0){
/* データ型をconditionにコピーしてループからぬける*/
condition.dataType = tableInfo->fieldInfo[i].dataType;
break;
}
}
/*conditionを決定する*/
conditionSet(token, &condition);
/* selectRecordを呼び出し、レコードを検索 */
if ((recordSet = selectRecord(tableName, &condition)) == NULL) {
printf("レコードの検索に失敗しました。\n");
}
/*selectFieldがフィールド名にあるか調べる*/
if (allSelectFlag == 0){
setInputString(selectField);
token = getNextToken();
for (i = 0; i < MAX_FIELD; i++){
if (token == NULL){
break;
}
for (j = 0; j < tableInfo->numField; j++){
/*tokenがフィールド名と一致したら*/
if (strcmp(tableInfo->fieldInfo[j].name, token) == 0){
strcpy(sendFieldName[i], token);
token = getNextToken();
break;
}
if (j == tableInfo->numField - 1){
printf("そのフィールド名は存在しません%s\n", token);
return;
}
}
}
}
/* *の場合は */
else{
for (i = 0; i < tableInfo->numField; i++){
strcpy(sendFieldName[i], tableInfo->fieldInfo[i].name);
}
numField = tableInfo->numField;
}
/*検索したレコード集合を表示*/
printRecordSet(recordSet, sendFieldName, numField);
/*解放*/
free(selectField);
freeTableInfo(tableInfo);
freeRecordSet(recordSet);
}
/*
* callDeleteRecord -- delete文の構文解析とdeleteRecordの呼び出し
*
* 引数:
* なし
*
* 返り値:
* なし
*
* deleteの書式:
* delete from テーブル名 where 条件式
*/
void callDeleteRecord()
{
char *token;
char *tableName;
Condition condition;
TableInfo *tableInfo;
int i;
/*deleteの次のトークンを読み込み、それが"from"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "from") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* テーブル名を読み込む */
if ((tableName = getNextToken()) == NULL) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", tableName);
return;
}
/*次のトークンを読み込み、それが"where"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "where") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
/* 条件式のフィールド名を読み込む */
if ((token = getNextToken()) == NULL){
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
strcpy(condition.name, token);
/* テーブルの情報を取得する */
if ((tableInfo = getTableInfo(tableName)) == NULL) {
/* エラー処理 */
return;
}
/*条件式に指定されたフィールドのデータ型を調べる*/
condition.dataType = TYPE_UNKNOWN;
for (i = 0; i < tableInfo->numField; i++){
if (strcmp(tableInfo->fieldInfo[i].name, condition.name) == 0){
/* データ型をconditionにコピーしてループからぬける*/
condition.dataType = tableInfo->fieldInfo[i].dataType;
break;
}
}
freeTableInfo(tableInfo);
/*conditionを決定する*/
conditionSet(token, &condition);
/* dereteRecordを呼び出し、レコードを削除 */
if (deleteRecord(tableName, &condition) == OK) {
printf("レコードを削除しました。\n");
}
else {
printf("レコードの削除に失敗しました。\n");
}
}
/*
* showTables -- show tablesが入力されたときに、データベース内のテーブルを表示
*/
void showTables(){
char *token;
char plus = '+';
char *hyphen = "----------------";
char vertical = '|';
char *tables = "tables";
wchar_t datName[260];
WIN32_FIND_DATA fd;
HANDLE h;
/*showの次のトークンを読み込み、それが"tables"かどうかをチェック */
token = getNextToken();
if (token == NULL || strcmp(token, "tables") != 0) {
/* 文法エラー */
printf("入力行に間違いがあります。:%s\n", token);
return;
}
printf("%c%s%c\n", plus, hyphen, plus); /* +-------------+ */
printf("%c %-14s %c\n", vertical, tables, vertical); /* | tables | */
printf("%c%s%c\n", plus, hyphen, plus); /* +-------------+ */
h = FindFirstFile(_T("C:\\Users\\shimpei\\Documents\\GitHub\\microdb\\microdatabase\\*.dat"), &fd);
if (h != INVALID_HANDLE_VALUE){
do{
_tcscpy(datName, fd.cFileName);
_tprintf(_T("%c %-14s %c\n"),vertical , datName ,vertical);
} while (FindNextFile(h, &fd));
FindClose(h);
}
printf("%c%s%c\n", plus, hyphen, plus); /* +-------------+ */
}
/*
* test1 -- rundomにinsertしまくる
* 1.先に乱数をつくる関数(initializeRandomGeneratorとgetRandomInteger)を用意
* 2.test1()を実行する
*/
int getRandomInteger(int min, int max){
int r;
r = min + (int)(max - min + 1)*(rand() / (RAND_MAX + 1.0));
return r;
}
void test1(){
RecordData record;
int i;
int j;
int r;
char id[MAX_STRING];
/*乱数を初期化して発生させる*/
srand((unsigned int)time(NULL));
/*100個のレコードを挿入する*/
for (i = 0; i < TEST_SIZE; i++){
j = 0;
r = getRandomInteger(MY_MIN, MY_MAX);
switch (r){
case 1:
sprintf(id, "i0000%c", '0' + r);
strcpy(record.fieldData[j].name, "id");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, id);
j++;
strcpy(record.fieldData[j].name, "name");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Mickey");
j++;
strcpy(record.fieldData[j].name, "age");
record.fieldData[j].dataType = TYPE_INTEGER;
record.fieldData[j].intValue = 85;
j++;
strcpy(record.fieldData[j].name, "address");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Urayasu");
j++;
record.numField = j;
break;
case 2:
sprintf(id, "i0000%c", '0' + r);
strcpy(record.fieldData[j].name, "id");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, id);
j++;
strcpy(record.fieldData[j].name, "name");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Minnie");
j++;
strcpy(record.fieldData[j].name, "age");
record.fieldData[j].dataType = TYPE_INTEGER;
record.fieldData[j].intValue = 85;
j++;
strcpy(record.fieldData[j].name, "address");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Urayasu");
j++;
record.numField = j;
break;
case 3:
sprintf(id, "i0000%c", '0' + r);
strcpy(record.fieldData[j].name, "id");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, id);
j++;
strcpy(record.fieldData[j].name, "name");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Pooh");
j++;
strcpy(record.fieldData[j].name, "age");
record.fieldData[j].dataType = TYPE_INTEGER;
record.fieldData[j].intValue = 4;
j++;
strcpy(record.fieldData[j].name, "address");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Canada");
j++;
record.numField = j;
break;
case 4:
sprintf(id, "i0000%c", '0' + r);
strcpy(record.fieldData[j].name, "id");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, id);
j++;
strcpy(record.fieldData[j].name, "name");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "Pocahontas");
j++;
strcpy(record.fieldData[j].name, "age");
record.fieldData[j].dataType = TYPE_INTEGER;
record.fieldData[j].intValue = 16;
j++;
strcpy(record.fieldData[j].name, "address");
record.fieldData[j].dataType = TYPE_STRING;
strcpy(record.fieldData[j].stringValue, "America");
j++;
record.numField = j;
break;
case 5:
sprintf(id, "i0000%c", '0' + r);
strcpy(record.fieldData[j].name, "id");