TaKl/src/db.c

196 lines
4.5 KiB
C
Raw Normal View History

2023-07-12 11:08:40 +02:00
/*
Fonctions d'interaction avec la base de données
*/
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include "include/colors.h"
#include "include/config.h"
#include "include/struct.h"
sqlite3* get_db() {
sqlite3* db;
if (access(DB_FILE, F_OK) != 0) {
// Create DB
sqlite3_open(DB_FILE, &db);
char* zErrMsg = NULL;
int ret = sqlite3_exec(
db,
"CREATE TABLE tasks ( \
id INTEGER PRIMARY KEY,\
text TEXT NOT NULL,\
done INTEGER, \
due_to INTEGER \
);",
NULL,
NULL,
&zErrMsg
);
if (ret != SQLITE_OK) {
fprintf(stderr, "(db creation) SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
printf(GREEN "OK" RESET " Base de données créée\n");
}
int ret = sqlite3_open(DB_FILE, &db);
if (ret != SQLITE_OK) {
fprintf(stderr, "(get_db) Unable to open db\n");
}
return db;
}
2023-07-12 21:28:54 +02:00
int add_task(task_t t) {
2023-07-12 11:08:40 +02:00
sqlite3* db = get_db();
2023-07-12 21:28:54 +02:00
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, done, due_to) VALUES (?1, ?2, ?3, ?4);", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
return 1;
}
char str_id[10];
sprintf(str_id, "%d", (int)t.id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC);
char str_done[2]; // Just a boolean
sprintf(str_done, "%d", t.done);
sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC);
char str_due_to[15];
sprintf(str_due_to, "%ld", t.due_to);
sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC);
ret = sqlite3_step(stmt);
sqlite3_finalize(stmt);
2023-07-12 11:08:40 +02:00
sqlite3_close(db);
2023-07-12 21:28:54 +02:00
if (ret != SQLITE_DONE) {
if (ret == 19) {
printf("Identifiant déjà utilisé\n");
} else {
printf("Return value: %d\n", ret);
}
return 1;
}
return 0;
2023-07-12 11:08:40 +02:00
}
int update_task(task_t t) { //TODO
(void)t;
return 1;
}
2023-07-12 21:28:54 +02:00
void delete_task(int id) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "DELETE FROM tasks WHERE id=?1;", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
exit(1);
}
char str_id[10];
sprintf(str_id, "%d", id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
}
2023-07-12 11:08:40 +02:00
void get_task(int id, task_t* t) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "SELECT id, text, done, due_to FROM tasks WHERE id=?1;", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
exit(1);
}
char str_id[10];
sprintf(str_id, "%d", id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW) {
t->id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10);
t->done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10);
t->due_to = strtol((char*)sqlite3_column_text(stmt, 3), NULL, 10);
char* text = (char*)sqlite3_column_text(stmt, 1);
t->text = malloc(sizeof(char)*(strlen(text)+1));
strcpy(t->text, text);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
// t.id will be (-1) if not found
}
int list_tasks() {
sqlite3* db = get_db();
sqlite3_stmt *stmt;
int ret = sqlite3_prepare_v2(db, "SELECT id, text, done, due_to FROM tasks;", -1, &stmt, NULL);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
return 1;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
task_t t;
t.id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10);
t.text = (char*)sqlite3_column_text(stmt, 1);
t.done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10);
t.due_to = strtol((char*)sqlite3_column_text(stmt, 3), NULL, 10);
//* Warning: t just lives here (particularly t.text)
if (t.done) {
printf(BOLD GREEN);
} else {
printf(BOLD YELLOW);
}
printf("[%d]" RESET " %s\n", t.id, t.text);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int get_new_task_id() {
return rand()%10000;
}