Skip to content

Commit

Permalink
Adding changes to init function (it won't compile yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
zertmark committed Sep 29, 2024
1 parent 2ed321a commit d202941
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
45 changes: 40 additions & 5 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DEFAULT_PATH "./databases/d1.db"
#define MAX_STRING_LINES 100

static sqlite3 *dataBase = {0};
static char *errMessage = NULL;
Expand All @@ -14,6 +12,13 @@ static int bufferRowsCount = 0;
static int databaseRowsCount = 0;
static char *primaryKey = "product_id";


//A little hacky
const char* tables[] = {
"STACK",
"FINANCE"
};

static void free_outputList(char** outputList, int iter)
{
for (int i = 0; i < iter; i++)
Expand Down Expand Up @@ -210,23 +215,53 @@ int getDatabaseRowsCount(void)
size_t getBufferRowsCount(void)
{
return bufferRowsCount;
}
//Should be only called, after function executeReadCommand("SELECT * FROM ALL;");
static int checkHeaders(const char* d_table, const char* HEADERS)
{
if (buffer[0]==NULL || strstr(buffer[0], "NULL")!=NULL)
{
return 0;
}

return strcmp(buffer[0], HEADERS)==0;
}
static int checkRows()
{

}
int checkTable(const char* d_table, const char* HEADERS)
{
char formatted_command [128];
sprintf(formatted_command, "SELECT * FROM %s;", d_table);
if (!executeReadCommand(formatted_command))
{
return 0;
}

}
int checkDatabase()
{

}
void createDatabase(char* path_to_database)
{

}
void openDatabase(char *path_to_database)
{
sqlite3_initialize();
if (access(path_to_database, F_OK || W_OK || R_OK) == -1)
{
pritnf("[WARNING] Couldn't find existing database at databases/test.db\nUsing default database path...\n");
printf("[WARNING] Couldn't find existing database at databases/test.db\nUsing default database path...\n");
path_to_database = DEFAULT_PATH;
}
if (sqlite3_open(path_to_database, &dataBase) == SQLITE_OK)
{
return;
}

createDatabase(path_to_database);
}

static int countRowsResult(sqlite3_stmt *stmt)
{
sqlite3_reset(stmt);
Expand Down
17 changes: 15 additions & 2 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@
#include <stab.h>
#include <stddef.h>
#include <string.h>

#define TABLE_HEADERS_STRING_STACK "ID\tNAME\tREMAINING\tCOST\tREVENUE\tPROFIT\tPROFIT %\tCOST_1"
#define TABLE_HEADERS_STRING_STACK_LENGTH 61

#define NUMBER_OF_HEADERS_STACK 8
#define NUMBER_OF_HEADERS_FINANCE 4

#define TABLE_HEADERS_STRING_FINANCE "ID\tNAME\tPLAN\tPROFIT"
#define TABLE_HEADERS_STRING_FINANCE_LENGHT 28
#define NUMBER_OF_HEADERS_FINANCE 4

#define DEFAULT_PATH "./databases/d1.db"
#define MAX_STRING_LINES 100

/* Strange idea to implement mechanism to know,
what kind of table names existing database should have
*/
#define TABLE_NAME_1 "STACK"
#define TABLE_NAME_2 "FINANCE"

int fieldsAreNotSQLCommands(char **fieldsList, int sizeList);
int fieldAndRowExist(char *dataBaseTableName, char *field, char *field_data);
void openDatabase(char *path_to_database);
Expand All @@ -29,4 +42,4 @@ float getFieldsAverageSum(char *dataBaseTableName, char *field);
float getFieldsSum(char *dataBaseTableName, char *field);
int updateDatabaseRowsCount(char *dataBaseTableName);
void closeDatabase(void);
char** splitString(char *argsIn, int *listArgsSize, const char* determinator, int maxNumberOfStrings);
char** splitString(char *argsIn, int *listArgsSize, const char* determinator, int maxNumberOfStrings);
8 changes: 4 additions & 4 deletions src/pretty_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ static void append_spaces_to_table(char*** table, int* column_max, size_t rows_c
{
for(size_t i=0;i<columns_count;i++)
{
if (table[c][i]==NULL)
{
continue;
}
size_t size = strlen(table[c][i]);
table[c][i] = realloc(table[c][i], column_max[i]);
for(size_t j=size;j<column_max[i];j++)
Expand All @@ -70,6 +66,10 @@ static void calculate_columns_max(char*** table,int* column_max,
{
for(size_t j=0;j<columns_count;j++)
{
if (table[c][j]==NULL)
{
continue;
}
column_max[j] = max(column_max[j], strlen(table[c][j])+1);
}
}
Expand Down

0 comments on commit d202941

Please sign in to comment.