2022-07-13 17:49:49 +00:00
/*
* db . c
*
* Created on : 13 и ю л . 2022 г .
* Author : alexander
*/
# include <stdbool.h>
2022-07-14 10:40:51 +00:00
# include "sqlite3.h"
2022-07-13 17:49:49 +00:00
# include <stdlib.h>
# include <stdio.h>
2022-07-14 10:40:51 +00:00
# include <string.h>
2022-07-13 17:49:49 +00:00
# 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 ) ;
}
2022-07-14 10:40:51 +00:00
if ( parameters [ 4 ] & & parameters [ 5 ] )
2022-07-13 17:49:49 +00:00
{
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 )
{
2022-07-14 10:40:51 +00:00
fprintf ( stderr , " Ошибка выполнения запроса: %s \n " , err_msg ) ;
2022-07-13 17:49:49 +00:00
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 ;
}
2022-07-14 10:40:51 +00:00
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 ;
}