GUI-FreeRDP/db.c

210 lines
5.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* db.c
*
* Created on: 13 июл. 2022 г.
* Author: alexander
*/
#include <stdbool.h>
#include "sqlite3.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "db.h"
#include "node_settings.h"
static sqlite3 *dbGetBase(char *path)
{
sqlite3 *db;
if (sqlite3_open(path, &db) != SQLITE_OK)
{
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return NULL;
}
return db;
}
/*
* 0 - parameter
* 1 - key
* 2 - set_key
* 3 - single
* 4 - value
* 5 - current (data)
* 6 - set_val
* 7 - dependence
* 8 - conflict
*/
static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName)
{
NotUsed = 0;
char **parameters = (char **) malloc(sizeof(char*) * argc);
for (int i = 0; i < argc; i++)
{
parameters[i] = argv[i];
}
if (!getParameter(atoi(parameters[0])))
{
addParameterKey(atoi(parameters[0]), parameters[1], atoi(parameters[2]), atoi(parameters[3]),
parameters[7] ? getParameter(atoi(parameters[7])) : NULL,
parameters[8] ? getParameter(atoi(parameters[8])) : NULL);
}
if (parameters[4] && parameters[5])
{
addParameterValue(atoi(parameters[0]), atoi(parameters[4]), parameters[5], atoi(parameters[6]));
}
return 0;
}
bool dbLoadData()
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
char *err_msg = 0;
char *sql =
"SELECT `parameters`.`id` as `parameter`, `parameters`.`data` as `key`, `parameters`.`set` as `set_key`, `parameters`.`single` as `single`, `values`.`id` as `value`, `values`.`data` as `current`, `values`.`set` as `set_val`,`dependencies`.`dependence` as `dependence`, `conflicts`.`conflict` as `conflict` \
FROM `parameters` as `parameters` \
LEFT JOIN `arguments` as `arguments` ON `parameters`.`id` = `arguments`.`parameter` \
LEFT JOIN `values` as `values` ON `arguments`.`value` = `values`.`id` \
LEFT JOIN `dependencies` as `dependencies` ON `dependencies`.`parameter` = `parameters`.`id` \
LEFT JOIN `conflicts` as `conflicts` ON `conflicts`.`parameter` = `parameters`.`id`";
if (sqlite3_exec(db, sql, dbLoad, 0, &err_msg) != SQLITE_OK)
{
fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return true;
}
bool dbWriteParameter(Parameter name, bool set)
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
sqlite3_stmt *res;
char *sql = "UPDATE `parameters` set `set` = ? where id = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_int(res, 1, set);
sqlite3_bind_int(res, 2, name);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key);
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key);
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key);
}
sqlite3_finalize(res);
sqlite3_close(db);
return true;
}
bool dbWriteValue(Value name, bool set)
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
sqlite3_stmt *res;
char *sql = "UPDATE `values` set `set` = ? where id = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_int(res, 1, set);
sqlite3_bind_int(res, 2, name);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current);
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current);
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current);
}
sqlite3_finalize(res);
sqlite3_close(db);
return true;
}
bool dbSetValueCurrent(Value name, char *current)
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
char *text = strlen(current) ? current : NULL;
sqlite3_stmt *res;
char *sql = "UPDATE `values` set `data` = ?, `set` = ? where id = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, text, -1, 0);
sqlite3_bind_int(res, 2, text ? true : false);
sqlite3_bind_int(res, 3, name);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key);
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key);
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key);
}
sqlite3_finalize(res);
sqlite3_close(db);
return true;
}