/* * test2 -- 検索 */ Result test2() { RecordSet *recordSet; Condition condition; FieldList fieldList; /* * 以下の検索を実行 * select * from TABLE_NAME where age > 17 */ strcpy(condition.name, "age"); condition.dataType = TYPE_INT; condition.operator = OPR_GREATER_THAN; condition.val.intVal = 17; condition.distinct = NOT_DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select * from TABLE_NAME where age > 17\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select distinct * from TABLE_NAME where age > 17 */ strcpy(condition.name, "age"); condition.dataType = TYPE_INT; condition.operator = OPR_GREATER_THAN; condition.val.intVal = 17; condition.distinct = DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select distinct * from TABLE_NAME where age > 17\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select * from TABLE_NAME where address != 'Florida' */ strcpy(condition.name, "address"); condition.dataType = TYPE_VARCHAR; condition.operator = OPR_NOT_EQUAL; strcpy(condition.val.stringVal, "Florida"); condition.distinct = NOT_DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select * from TABLE_NAME where address != 'Florida'\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select name from TABLE_NAME' */ strcpy(condition.name, ""); condition.dataType = TYPE_UNKNOWN; condition.operator = OPR_UNKNOWN; strcpy(condition.val.stringVal, ""); condition.distinct = NOT_DISTINCT; strcpy(fieldList.name[0], "name"); fieldList.numField = 1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select name from TABLE_NAME\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* * 以下の検索を実行 * select name address from TABLE_NAME where address != 'Florida' */ strcpy(condition.name, "address"); condition.dataType = TYPE_VARCHAR; condition.operator = OPR_NOT_EQUAL; strcpy(condition.val.stringVal, "Florida"); condition.distinct = NOT_DISTINCT; strcpy(fieldList.name[0], "name"); strcpy(fieldList.name[1], "address"); fieldList.numField = 2; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select name, address from TABLE_NAME where address != 'Florida'\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); return OK; }
/* * 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); }