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

6
include/choicemenu.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef CHOICEMENU_H
#define CHOICEMENU_H
void choice_menu(void);
#endif

18
include/database.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef DATABASE_H
#define DATABASE_H
extern char _server[56];
extern char _user[56];
extern char _password[56];
extern char _database[56];
void init_mysql();
void kill(int code);
char *get_tables(void);
char *print_tables(void);
char *print_field(char *table, char *field);
int insert_row(char table[56], char fieldData[2048], char req[2048]);
int delete_row(char table[56], char identifier[2048]);
int modify_row(char table[56], char identifier[2048], char data[2048]);
#endif

6
include/utils.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef UTILS_H
#define UTILS_H
const unsigned long hash(const char *str);
#endif

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;
}

31
makefile Normal file
View File

@@ -0,0 +1,31 @@
CC := gcc
SRC_DIR := src
LIB_DIR := lib
OBJ_DIR := obj
OUT_DIR := out
INC_DIR := include
SRC_FILES := $(wildcard $(SRC_DIR)/*.c) $(wildcard $(LIB_DIR)/*.c)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
LIBS := $(mysql_config --libs) $(mysql_config --cflags) -lmysqlclient
CFLAGS := -I$(INC_DIR)
dev: $(OBJ_FILES)
$(CC) -o $(OUT_DIR)/$@ $^ $(CFLAGS) $(LIBS) && strace ./out/dev
run: $(OBJ_FILES)
$(CC) -o $(OUT_DIR)/$@ $^ $(CFLAGS) $(LIBS) && ./out/run
prod: $(OBJ_FILES)
$(CC) -o $(OUT_DIR)/$@ $^ $(CFLAGS) $(LIBS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) -c -o $@ $<
.PHONY: clean
clean:
# rm -f $(OBJ_DIR)/*.o *~ core $(INC_DIR)/*~
rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/*

BIN
obj/main.o Normal file

Binary file not shown.

BIN
out/run Executable file

Binary file not shown.

27
src/main.c Normal file
View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../include/database.h"
#include "../include/choicemenu.h"
#define __MAIN__
#ifdef __MAIN__
char _server[56] = "localhost";
char _user[56] = "pipo";
char _password[56] = "theclown";
char _database[56] = "f1";
#else
extern char _server[56];
extern char _user[56];
extern char _password[56];
extern char _database[56];
#endif
void main() {
init_mysql();
choice_menu();
}

45
tmp/database.c Normal file
View File

@@ -0,0 +1,45 @@
#include <mysql/mysql.h>
#include <stdio.h>
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *_server = "localhost";
char *_user = "pipo";
char *_password = "theclown"; /* set me first */
char *_database = "mysql";
void init(char *server, char *user, char *password, char *database) {
_server = server;
_user = user;
_password = password; /* set me first */
_database = database;
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
}
void kill(int code) {
mysql_free_result(res);
mysql_close(conn);
}

45
tmp/main.c Normal file
View File

@@ -0,0 +1,45 @@
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
void main() {
// test();
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "pipo";
char *password = "theclown"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password,
database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}

BIN
tmp/main.o Normal file

Binary file not shown.

37
tmp/makefile.bak Normal file
View File

@@ -0,0 +1,37 @@
CC = gcc
OBJ = *.o database.o
LIBS = $(mysql_config --libs) $(mysql_config --cflags) -lmysqlclient
OUTDIR = out
IDIR = include
ODIR = obj
LDIR = lib
SDIR = src
CFLAGS = -I$(IDIR)
_DEPS = *.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
# SRC = $(wildcard $(SDIR)/*.0)
_OBJ = main.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
# OBJ = $(patsubst $(SDIR)/%.c,$(ODIR)/%.o,$(SRC))
$(ODIR)/%.o: $(SDIR)/%.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
# $(OBJ): $(ODIR)/%.o: src/%.c $(DEPS)
# $(CC) -c -o $@ $< $(CFLAGS)
dev: $(OBJ)
$(CC) -o $(OUTDIR)/$@ $^ $(CFLAGS) $(LIBS)
prod: $(OBJ)
$(CC) -o $(OUTDIR)/$@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~

52
tmp/pgp.txt Normal file
View File

@@ -0,0 +1,52 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBGM8PhQBEADpxu2OQZK3QYJWvvS0OWlpJb+hsjB+bDslHiuurjeFH2+LYCk9
Q0rOiIJAZDxaTFgFuBCasdWeCgRVyZVwrCyXjhr/mahJL9yO0+/v9XQmRL8HD6DZ
rsl8a4weI83sFQJCl0zn1toZN5YWIWSo3PM4wwaaAFhDEWK22mNR2b/xwzZb96G2
EvIh0xU62/jJJkvPAhqrzqj0ECKT8xWAKpc6/NC9sQLM18i3OSGbcARxnxD4XzHf
8wd2bFmk8rB7tW3sEcH2cXrj1Zv3e4g2JFZzyhdlq8liPTzTlOrb4ec9fmZhuD8n
EmHSr2L61FrY/7FiotWslxBmn5f7n3wM7zQmflBvcgDFYh0RWnc2+GqWQ60+V7Qf
T8uLXZ7UZRaNmk+LdSeZZC5IuM+kL9FP82YZvNum3I2/pTiP007Uy6s4drWr2RU9
SG8qRPId7BWXXAdZp/6yvQI1d9XTCXu13iJ0Krbb8LGIqWY2s1djFnrmgCGyWUiG
8+LU76bMw5jraLrPqblT1fAeLzfF1KO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