GUI-FreeRDP/db.c

390 lines
10 KiB
C
Raw Permalink 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]));
}
free(parameters);
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;
}
static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName)
{
Hosts *hosts = *(Hosts **)answer;
Host *host = (Host*) malloc(sizeof(Host));
host->data = (char **) malloc(sizeof(char*) * argc);
host->size = argc;
for (int i = 0; i < argc; i++)
{
host->data[i] = (char *) malloc(sizeof(char) * strlen(argv[i]));
strcpy(host->data[i], argv[i]);
}
Host **tmp = hosts->host;
hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size);
for (size_t i = 0; i < hosts->size - 1; ++i)
{
hosts->host[i] = tmp[i];
}
if (tmp)
{
free(tmp);
}
hosts->host[hosts->size - 1] = host;
return 0;
}
Hosts *dbGetHostsList()
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
char *err_msg = 0;
char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`";
Hosts *hosts = (Hosts *) malloc(sizeof(Hosts));
hosts->size = 0;
hosts->host = NULL;
if (sqlite3_exec(db, sql, dbCreateHostsList, &hosts, &err_msg) != SQLITE_OK)
{
fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return hosts;
}
void dbFreeHosts(Hosts *hosts)
{
for (size_t i = 0; i < hosts->size; ++i)
{
for (size_t j = 0; j < hosts->host[i]->size; ++j)
{
free(hosts->host[i]->data[j]);
}
free(hosts->host[i]);
}
free(hosts);
}
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;
}
int dbAddServer(char *ip, char *dns)
{
int result = 0;
if (!(ip && strlen(ip) && dns && strlen(dns)))
return -1;
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return -2;
}
sqlite3_stmt *res;
char *sql = "SELECT * FROM `hosts` WHERE `ip` = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, ip, -1, 0);
}
else
{
sqlite3_finalize(res);
sqlite3_close(db);
return -3;
}
if (sqlite3_step(res) == SQLITE_ROW)
{
int id = atoi((const char *)sqlite3_column_text(res, 0));
sqlite3_finalize(res);
sql = "UPDATE `hosts` set `set` = 0";
if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK)
{
sql = "UPDATE `hosts` set `dns` = ?, `set` = 1 where id = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, dns, -1, 0);
sqlite3_bind_int(res, 2, id);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 5;
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 4;
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 3;
}
}
else
{
result = -5;
}
}
else if(sqlite3_step(res) == SQLITE_DONE)
{
sqlite3_finalize(res);
sql = "UPDATE `hosts` set `set` = 0";
if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK)
{
sql = "INSERT INTO `hosts` (`ip`, `dns`, `set`) VALUES (?, ?, 1)";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, ip, -1, 0);
sqlite3_bind_text(res, 2, dns, -1, 0);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 2;
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 1;
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 0;
}
}
else
{
result = -6;
}
}
else
{
result = -4;
}
sqlite3_finalize(res);
sqlite3_close(db);
return result;
}
bool dbSetUserNameCurrent(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, VALUE_USERNAME);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
}
sqlite3_finalize(res);
sqlite3_close(db);
return true;
}