示例#1
0
/*
 * main -- データ操作モジュールのテスト
 */
int main(int argc, char **argv)
{
    char tableName[20];
    TableInfo tableInfo;
    int i;

    /* ファイルモジュールを初期化する */
    if (initializeFileModule() != OK) {
        fprintf(stderr, "Cannot initialize file module.\n");
        exit(1);
    }

    /* データ定義ジュールを初期化する */
    if (initializeDataDefModule() != OK) {
        fprintf(stderr, "Cannot initialize data definition module.\n");
        exit(1);
    }

    /* データ操作ジュールを初期化する */
    if (initializeDataManipModule() != OK) {
        fprintf(stderr, "Cannot initialize data manipulation module.\n");
        exit(1);
    }

    /*
     * このプログラムの前回の実行の時のデータ定義残っている可能性があるので、
     * とりあえず削除する
     */
    dropTable(TABLE_NAME);

    /*
     * 以下のテーブルを作成
     * create table student (
     *   id string,
     *   name string,
     *   age int,
     *   address string
     * )
     */
    strcpy(tableName, TABLE_NAME);
    i = 0;

    strcpy(tableInfo.fieldInfo[i].name, "id");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "name");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "age");
    tableInfo.fieldInfo[i].dataType = TYPE_INT;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "weight");
    tableInfo.fieldInfo[i].dataType = TYPE_DOUBLE;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "address");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    tableInfo.numField = i;

    /* テーブルの作成 */
    if (createTable(tableName, &tableInfo) != OK) {
        /* テーブルの作成に失敗 */
        fprintf(stderr, "Cannot create table.\n");
        exit(1);
    }

    /* 挿入テスト */
    fprintf(stderr, "test1: Start\n\n");
    if (test1() == OK) {
        fprintf(stderr, "test1: OK\n\n");
    } else {
        fprintf(stderr, "test1: NG\n\n");
    }

    /* 検索テスト */
    fprintf(stderr, "test2: Start\n\n");
    if (test2() == OK) {
        fprintf(stderr, "test2: OK\n\n");
    } else {
        fprintf(stderr, "test2: NG\n\n");
    }

    /* 削除テスト */
    fprintf(stderr, "test3: Start\n\n");
    if (test3() == OK) {
        fprintf(stderr, "test3: OK\n\n");
    } else {
        fprintf(stderr, "test3: NG\n\n");
    }

    /* 後始末 */
    dropTable(TABLE_NAME);
    finalizeDataManipModule();
    finalizeDataDefModule();
    finalizeFileModule();
}
示例#2
0
文件: main.c 项目: sympe/microDB
/*
* main -- マイクロDBシステムのエントリポイント
*/
int main()
{
	char input[MAX_INPUT];
	char *token;

	/* ファイルモジュールの初期化 */
	if (initializeFileModule() != OK) {
		fprintf(stderr, "Cannot initialize file module.\n");
		exit(1);
	}

	/* データ定義ジュールの初期化 */
	if (initializeDataDefModule() != OK) {
		fprintf(stderr, "Cannot initialize data definition module.\n");
		exit(1);
	}

	/* データ操作ジュールの初期化 */
	if (initializeDataManipModule() != OK) {
		fprintf(stderr, "Cannot initialize data manipulation module.\n");
		exit(1);
	}

	/* ウェルカムメッセージを出力 */
	printf("マイクロDBMSを起動しました。\n");

	/* 1行ずつ入力を読み込みながら、処理を行う */
	for (;;) {
		/* プロンプトの出力 */
		printf("\nDDLまたはDMLを入力してください。\n");
		printf("> ");

		/* キーボード入力を1行読み込む */
		fgets(input, MAX_INPUT, stdin);

		/* 入力の最後の改行を取り除く */
		if (strchr(input, '\n') != NULL) {
			*(strchr(input, '\n')) = '\0';
		}

		/* 字句解析するために入力文字列を設定する */
		setInputString(input);

		/* 最初のトークンを取り出す */
		token = getNextToken();

		/* 入力が空行だったら、ループの先頭に戻ってやり直し */
		if (token == NULL) {
			continue;
		}

		/* 入力が"quit"だったら、ループを抜けてプログラムを終了させる */
		if (strcmp(token, "quit") == 0) {
			printf("マイクロDBMSを終了します。\n\n");
			break;
		}

		/* 最初のトークンが何かによって、呼び出す関数を決める */
		if (strcmp(token, "create") == 0) {
			callCreateTable();
		}
		else if (strcmp(token, "drop") == 0) {
			callDropTable();
		}
		else if (strcmp(token, "insert") == 0) {
			callInsertRecord();
		}
		else if (strcmp(token, "select") == 0) {
			callSelectRecord();
		}
		else if (strcmp(token, "delete") == 0) {
			callDeleteRecord();
		}
		else if (strcmp(token, "show") == 0){
			showTables();
		}
		else if (strcmp(token, "test1") == 0){
			test1();
		}
		else {
			/* 入力に間違いがあった */
			printf("入力に間違いがあります。:%s\n", token);
			printf("もう一度入力し直してください。\n\n");
		}
	}

	/* 各モジュールの終了処理 */
	finalizeDataManipModule();
	finalizeDataDefModule();
	finalizeFileModule();
}