/* dblayer.h - Database layer for ck -----------------------------------*- C -*-
*
* This file is part of ck, the config keeper
*
* -----------------------------------------------------------------------------
*
* Copyright (C) 2018 Anastasis Grammenos
* GPLv3 (see LICENCE for the full notice)
*
* -----------------------------------------------------------------------------
*
* Give access to the database.
*
* -------------------------------------------------------------------------- */
#ifndef DBLAYER_H
#define DBLAYER_H
#include <sqlite3.h>
#include "actions.h"
enum SqlErrors {
SQL_NO_ERR = 0,
SQL_ERR_NO_DB_FILE,
SQL_ERR_NO_TABLES,
SQL_ERR_SQLITE,
SQL_CONFIG_PATH_EXISTS,
SQL_ERR_PRIMARY_REDEFINITION
};
typedef enum SqlErrors SqlError;
typedef struct DBstruct DB;
struct DBstruct {
sqlite3 *ptr;
SqlError error;
};
int db_exists(const UserOpt *opt);
/* Open the db file. On fail return null pointer to db
* and the corresponding SQL error (NO_DB_FILE | NO_TABLES)*/
DB open_DB(const UserOpt *opt);
void close_DB(DB *db);
int program_exists(DB *db, const char *pName);
int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int attr, const char *home);
/********/
/* init */
/********/
/* Create the tables required for the ckdb */
void init_make_tables(DB *db);
DB init_make_DB(const UserOpt *opt);
/*******/
/* add */
/*******/
/* Returns 1 in error, 0 otherwise */
int add_transaction_try(DB *db, const AddOpt * const opt, const char *home);
/********/
/* edit */
/********/
int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
int get_config_number(DB *db, char *pName);
int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec);
/********/
/* list */
/********/
int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home);
int list_get_programs(DB *db, cklist *ckl);
int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home);
/*******/
/* del */
/*******/
int del_transaction_try(DB *db, const char *pName, const char *cBaseName);
/***********/
/* restore */
/***********/
int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to);
int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to);
#endif /* DBLAYER_H */