feat: initial commit

This commit is contained in:
2022-10-04 16:14:18 +02:00
commit 833cc93681
16 changed files with 646 additions and 0 deletions

183
lib/choicemenu.c Normal file
View File

@@ -0,0 +1,183 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* Local imports */
#include "./utils.h"
#include "./database.h"
/* Hashed constants for choice menu */
#define SA 5863801
#define SF 5863806
#define AR 5863224
#define DR 5863323
#define MR 5863620
#define KL 5863548
/* variables */
unsigned long choiceHash;
char confirmation[3],
table[56], field[56],
identifier[2048], data[2048];
/**
* @brief Shows `field` from `table`
*
* Used to list all rows that have a value
* assigned to `field` in `table`. Determined
* by user input.
*
* @returns Void.
*/
void table_show() {
printf("\n\t[SHOW TABLE FIELD]\n");
printf("Insert table to show:\t");
scanf("%s", table);
printf("Insert field to show:\t");
scanf("%s", field);
printf("Selected:\t\t%s[%s]\n", table, field);
printf("Correct? (y/n):\t");
scanf("%s", confirmation);
if (strcmp(confirmation, "n") == 0 || strcmp(confirmation, "no") == 0)
table_show();
print_field(table, field);
}
/** @brief Puts `req` into `table`
*
* Used to let the end user add rows to
* the desired table, asks for `req` and
* `fieldData` to assign `req` to.
*
* @returns Void.
*/
void row_input() {
char fieldData[2048];
char req[2048];
printf("\n\t[INSERT ROW INTO TABLE]\n");
printf("Insert table to use:\t");
scanf("%s", table);
printf("Insert fields to use:\t");
scanf("%s", fieldData);
printf("Insert data for fields:\t");
scanf("%s", req);
printf("Selected:\t\t%s(%s): %s\n", table, fieldData, req);
printf("Correct? (y/n):\t");
scanf("%s", confirmation);
if (strcmp(confirmation, "n") == 0 || strcmp(confirmation, "no") == 0)
row_input();
insert_row(table, fieldData, req);
}
/** @brief Removes `identifier` from `table`
*
* Deletes rows that match `identifier`
* determined by the user from `table`.
*
* @returns Void.
*/
void row_delete() {
printf("\n\t[DELETE ROW FROM TABLE]\n");
printf("Insert table to use:\t");
scanf("%s", table);
printf("Insert identifier(s) to use:\t");
scanf("%s", identifier);
printf("Selected:\t\t%s: %s\n", table, identifier);
printf("Correct? (y/n):\t");
scanf("%s", confirmation);
if (strcmp(confirmation, "n") == 0 || strcmp(confirmation, "no") == 0)
row_delete();
delete_row(table, identifier);
}
/** @brief Modifies `identifier` to `data` from `table`
*
* Searches for `identifier` in `table` and modifies it
* to `data` all determined by the user.
*
* @returns Void.
*/
void row_modify() {
printf("\n\t[CREATE TABLE]\n");
printf("Insert table to use:\t");
scanf("%s", table);
printf("Insert identifier(s) to use:\t");
scanf("%s", identifier);
printf("Insert data to override:\t");
scanf("%s", data);
printf("Selected:\t\t%s(%s): %s\n", table, identifier, data);
printf("Correct? (y/n):\t");
scanf("%s", confirmation);
if (strcmp(confirmation, "n") == 0 || strcmp(confirmation, "no") == 0)
row_modify();
modify_row(table, identifier, data);
}
/** @brief Activates the choice menu loop
*
* Used to give the user an interfcae to
* interact with all the commands defined above.
*
* @returns Void.
*/
void choice_menu() {
printf("Choose one of the following:\n");
printf("\t[sa] Show all tables\n");
printf("\t[sf] Show `field` from `table`\n");
printf("\t[ar] Add `row` from `table`\n");
printf("\t[dr] Delete `row` from `table`\n");
printf("\t[dr] Modify `row` from `table`\n");
printf("\t[kl] End database connection`\n");
char input[2];
printf("Choice:\t");
scanf("%s", input);
choiceHash = hash(input);
switch (choiceHash)
{
case KL:
printf("\t[Kill database connection]\n");
kill(0);
break;
case SA:
printf("\n\t[SHOW ALL TABLES]\n");
print_tables();
choice_menu();
break;
case SF:
table_show();
choice_menu();
break;
case AR:
row_input();
choice_menu();
break;
case DR:
row_delete();
choice_menu();
break;
case MR:
row_modify();
choice_menu();
break;
default:
printf("\t[INVALID CHOOCE, PLEASE PICK AGAIN]\n");
choice_menu();
}
}

176
lib/database.c Normal file
View File

@@ -0,0 +1,176 @@
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
/* Sets global variables if not set */
#ifdef __MAIN__
char _server[56] = "localhost";
char _user[56] = "pipo";
char _password[56] = "theclown";
char _database[56] = "mysql";
#else
extern char _server[56];
extern char _user[56];
extern char _password[56];
extern char _database[56];
#endif
/* Variables */
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_RES *query(char *command) {
if (mysql_query(conn, "SHOW TABLES")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
return mysql_use_result(conn);
}
/** @brief Starts the connection to the database
*
* Initializes a conncection to `_user@_serve r`
* That will be used by the rest of this lib.
*
* @returns Void.
*/
void init_mysql() {
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, _server, _user, _password, _database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
}
/** @brief Ends the connection to `_server`
*
* This function is used to cleanly exit the
* database when operations are finished.
*
* @returns Void.
*/
void kill() {
mysql_free_result(res);
mysql_close(conn);
}
/* Functions that return to stdout */
/** @brief Prints all tables available in `_database`
*
* Loops through all tables in `_database` accsessible
* by `_user` and prints them to stdout.
*
* @returns Void.
*/
void print_tables() {
res = query("SHOW TABLES");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
}
/** @brief Prints all values of `field` from `table`
*
* Loops through `field` in `table` and prints them out
* to stdout.
*
* @param table The table to view
* @param field The field to view from `table`
*
* @returns Void.
*/
void print_field(char* table, char* field) {
char command[2048];
sprintf(command, "SELECT %s FROM %s", field, table);
res = query(command);
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
}
/* Functions that return a usable variable */
/** @brief Returns all tables in `_database`
*
* Returns all tables in `_database` to a string,
* seperated with new lines (\n).
*
* @returns str The string of tables in `_database`
*/
char *get_tables() {
res = query("SHOW TABLES");
char *str = malloc(sizeof(char) * 2048);
sprintf(str, "");
while ((row = mysql_fetch_row(res)) != NULL)
sprintf(str, "%s %s\n", str, row[0]);
return str;
}
/** @brief Inserts `req` into `table` using `fieldData`
*
* Used to create an entry into `table` with `req`
* as data that is put into `fieldData`.
*
* @param table The table to insert the row into
* @param fieldData The fields to assign `req` to
* @param req The data to assign to the new row
*
* @returns Void
*/
void insert_row(char table[56], char fieldData[2048], char req[2048]) {
char command[2048];
sprintf(command, "INSERT INTO %s (%s) VALUES (%s)", table, fieldData, req);
res = query(command);
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
}
/** @brief Deletes row matching `identifier` from `table`
*
* This function is used to delete unwanted rows
* from `table` that match the conditions mentioned
* in `identifier`.
*
* @param table The table to match the identifier to
* @param identifier The conditions to use when searching
*
* @returns Void.
*/
void delete_row(char table[56], char identifier[2048]) {
char command[2048];
sprintf(command, "DELETE FROM %s WHERE %s", table, identifier);
res = query(command);
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
}
/** @brief Modify a row in `table` that match `identifier` using `data`
*
* Changes the values of rows found in `table` matching the
* `identifier` to the desired values passed into `data`.
*
* @param table The table to modify the row from
* @param identifier The identifier for the row to modify
* @param data The data to overwrite the row
* found with `identifier` with
*
* @returns Void.
*/
void modify_row(char table[56], char identifier[2048], char data[2048]) {
char command[2048];
sprintf(command, "UPDATE %s SET %s WHERE %s", table, data, identifier);
res = query(command);
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
}

20
lib/utils.c Normal file
View File

@@ -0,0 +1,20 @@
/** @brief Hash `str` to `hash` for use in a switch case
*
* A string needs to be hashed for it to be used
* inside of a switch statement. We want to use a switch
* statement instead of an if-else chain for it's
* simplicity and lower amount of low-level operations.
*
* @param str The string to hash
*
* @returns hash The hashed version of `str`
*/
const unsigned long hash(const char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c;
return hash;
}