140 lines
3.2 KiB
C
140 lines
3.2 KiB
C
|
/*
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
|
||
|
int add_task(task_t t) { //TODO
|
||
|
sqlite3* db = get_db();
|
||
|
|
||
|
sqlite3_close(db);
|
||
|
(void)t;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
int update_task(task_t t) { //TODO
|
||
|
(void)t;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
|
||
|
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;
|
||
|
}
|