Skip to content

Commit

Permalink
Handle more Query, add a function to free a struct Table
Browse files Browse the repository at this point in the history
  • Loading branch information
PironP committed Dec 17, 2017
1 parent 4e39e1a commit 8590aa1
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 145 deletions.
1 change: 1 addition & 0 deletions sources/headers/manage_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ int createTableStructure(const Database database, const Table table);
int hasColumn(const Database database, const Table table, const Column column);
void removeDataTilde(const Database database, const Table table);
Column* getAllColumns(const Database database, const Table table, int *nbColumns);
void freeTable(Table *currentTable);

#endif
5 changes: 2 additions & 3 deletions sources/headers/read_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#include <stdio.h>
#include "config.h"

void findAllRecords(char* db_name, char* table_name);
void findSpecificRecords(char* db_name, char* table_name, char* column_name, char* record_value, char* conditionType);
void findColumnsName(char* db_name, char* table_name);
void showDatabases(char* managerPath);
Table findAllRecords(char* db_name, char* table_name);
StringArray readColumnsName(FILE* f);
StringArray readData(FILE* f);
int filesFound(char *db_name, char *table_name, char *table_file);
Expand Down
9 changes: 5 additions & 4 deletions sources/headers/read_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include "config.h"

void parseData(StringArray data, Table *currentTable);
void printResult(Table *currentTable);
void printAllResult(Table *currentTable);
int searchColumnPlace(Table *currentTable, char *columnName);
IntArray searchSpecificData(Table *currentTable, int columnPlace, char *dataSearched);
IntArray searchData(Table *currentTable, int columnPlace, char *dataSearched);

Table searchSpecificData(Table *currentTable, int columnPlace, char *dataSearched);
Table searchData(Table *currentTable, int columnPlace, char *dataSearched);
void searchSpecifiColumn(Table *currentTable, char *columnName);
void clearUnusedColumn(Table *currentTable, int columnPlace);
#endif

51 changes: 51 additions & 0 deletions sources/sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static void entryLoop(void);
static int isExitCommand(char *command);

int main(int argc, const char *argv[])
<<<<<<< HEAD
{
printf("Welcome to yaml_db project! \n");
printf("\n");
Expand Down Expand Up @@ -86,6 +87,56 @@ int isExitCommand(char *command)
if (strcmp(cleanCommand, "exit") == 0 || strcmp(cleanCommand, "quit") == 0) {
return 1;
}
}

void test-readtable() {
char *manager = DB_FILENAME;
char *managerPath = createFilePath(manager);

char *requete = "select";
char *table = "person";
StringArray where = {
malloc(ARRAY_CAPACITY * sizeof(int)),
0,
ARRAY_CAPACITY
};
where = appendValueToStringArray(where, "username");
where = appendValueToStringArray(where, "gender");
StringArray value = {
malloc(ARRAY_CAPACITY * sizeof(int)),
0,
ARRAY_CAPACITY
};
value = appendValueToStringArray(value, "%osie McQui%");
value = appendValueToStringArray(value, "%F%");
char *condition = "LIKE";
char *column = "";


Table currentTable = findAllRecords(managerPath, table);
if (where.size > 0 && strcmp(condition, "=") == 0)
{
for (int i = 0; i < where.size; i ++)
{
int columnPlace = searchColumnPlace(&currentTable, where.data[i]);
currentTable = searchSpecificData(&currentTable, columnPlace, value.data[i]);
}
}
else if (where.size > 0 && (strcmp(condition, "like") == 0 || strcmp(condition, "LIKE") == 0))
{
for (int i = 0; i < where.size; i ++)
{
int columnPlace = searchColumnPlace(&currentTable, where.data[i]);
currentTable = searchData(&currentTable, columnPlace, value.data[i]);
}
}
if (column != NULL && strcmp(column, "") != 0)
{
searchSpecifiColumn(&currentTable, column);
}
printAllResult(&currentTable);

//showDatabases(managerPath);

return 0;
}
2 changes: 2 additions & 0 deletions sources/sources/manage_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ void removeFileInDir(const char *filename, const char *dirname)
//
int isFile(const char *filename)
{
printf("%s", filename);

FILE *file = fopen(filename, "r");

if (file != NULL) {
Expand Down
22 changes: 22 additions & 0 deletions sources/sources/manage_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,25 @@ Column* getAllColumns(const Database database, const Table table, int *nbColumns

return columns;
}

void freeTable(Table *currentTable)
{
for (int i = 0; i < currentTable->nbRows; i++) {
for (int j = 0; j < currentTable->rows[i].nbCells; j++)
{
free(currentTable->rows[i].cells[j].data);
}
free(currentTable->rows[i].cells);
}
free(currentTable->rows);

for (int i = 0; i < currentTable->nbColumns; i++) {
free(currentTable->columns[i].name);
free(currentTable->columns[i].type);
free(currentTable->columns[i].defaultValue);
free(currentTable->columns[i].foreignKey);
}
free(currentTable->columns);

free(currentTable);
}
129 changes: 27 additions & 102 deletions sources/sources/read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,39 @@
#include "../headers/manage_array.h"
#include "../headers/read_file.h"

/**
// read the name of all databases
**/
void showDatabases(char* managerPath)
{
FILE* file = fopen(managerPath, "r");
if (file != NULL)
{
char databaseName[STRING_SIZE];
while (fgets(databaseName, STRING_SIZE, file) != NULL) {
if (strstr(databaseName, " ") != NULL) {
printf("%s", databaseName + 6);
}
}
}

}

/**
// Read the data for a given database name 'db_name'
// and a 'name'
// Return a Table
**/
void findAllRecords(char* db_name, char* table_name)
Table findAllRecords(char* db_name, char* table_name)
{
char *table_file = createFilePath(table_name);

Table currentTable;

if(!filesFound(db_name, table_name, table_file)) {
return;
return currentTable;
}

Table currentTable;


FILE* f = fopen(table_file, "r");

StringArray columnsName = readColumnsName(f);
Expand All @@ -48,107 +67,12 @@ void findAllRecords(char* db_name, char* table_name)

StringArray data = readData(f);
parseData(data, &currentTable);

printResult(&currentTable);
// func free table to clen memory


return;
}

/**
// Read the data for a given database name 'db_name',
// a given 'tbale_name',
// a condition (where / like),
// and print result where is a given 'record_value'
**/
void findSpecificRecords(char* db_name, char* table_name, char* column_name, char* record_value, char* conditionType)
{
/* char *table_file = createFilePath(table_name);
if (!filesFound(db_name, table_name, table_file)) {
return;
}
FILE* f = fopen(table_file, "r");
char temp[STRING_SIZE];
int columnNameSize = 0;
char **columnName = readColumnName(f);
while ((fgets(temp, STRING_SIZE, f) != NULL) && (strstr(temp, "-") != NULL)) {
if ((strstr(temp, " ")) != NULL && (strstr(temp, " ")) == NULL && (strstr(temp, record_value)) != NULL) {
char temp2[STRING_SIZE];
strcpy(temp2, temp + 11);
char *token;
token = strtok(temp2, ", ");
while (token != NULL) {
printf("%s |", token);
token = strtok(NULL, ",");
}
}
}
for (int i = 0; i < columnNameSize; i++) {
free(columnName[i]);
}
free(columnName);
*/
return currentTable;
}

void findColumnsName(char* db_name, char* table_name)
{
char *table_file = createFilePath(table_name);

if(!filesFound(db_name, table_name, table_file)) {
return;
}

Table currentTable;

FILE* f = fopen(table_file, "r");

StringArray columnsName = readColumnsName(f);

currentTable.columns = malloc(sizeof(Column) * columnsName.size);
currentTable.nbColumns = columnsName.size;

for (int i = 0; i < currentTable.nbColumns; i++) {
currentTable.columns[i].name = columnsName.data[i];
}

currentTable.nbRows = 0;

printResult(&currentTable);

return;
}

/**
// Read the data in a given FILE* 'f'
// and return an array of string with the column name
**/
char** readColumnNameOld(FILE* f, int *columnNameSize)
{

char **columnName = malloc(10 * sizeof(char *));

char temp[STRING_SIZE];

int i = 0;
while ((fgets(temp, STRING_SIZE, f) != NULL) && (strstr(temp, "data") == NULL)) {
if ((strstr(temp, " ")) != NULL && (strstr(temp, " ")) == NULL) {
temp[strlen(temp)-2] = '\0';
columnName[i] = malloc(STRING_SIZE * sizeof(char));
strcpy(columnName[i], temp + 8);
i++;
*columnNameSize += 1;
}
}

return columnName;
}


/**
// Read the data in a given FILE* 'f'
Expand Down Expand Up @@ -213,6 +137,7 @@ StringArray readData(FILE* f)
**/
int filesFound(char *db_name, char *table_name, char *table_file)
{

if (!isFile(db_name)) {
printf("the database doesn't exist\n");
return 0;
Expand Down
Loading

0 comments on commit 8590aa1

Please sign in to comment.