aboutsummaryrefslogtreecommitdiffstats
path: root/src/dblayer.h
blob: 05ba5bd1cf97370f036df4181ac31641e9efc438 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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"

typedef enum SqlErrors SqlError;
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 struct DBstruct DB;
struct DBstruct {
  sqlite3 *ptr;
  SqlError error;
};

extern 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)*/
extern DB open_DB(const UserOpt *opt);

extern void close_DB(DB *db);
extern int program_exists(DB *db, const char *pName);

/********/
/* init */
/********/

/* Create the tables required for the ckdb */
extern void init_make_tables(DB *db);
extern DB init_make_DB(const UserOpt *opt);

/*******/
/* add */
/*******/

/* Returns 1 in error, 0 otherwise */
extern int add_transaction_try(DB *db, const AddOpt * const opt);

/********/
/* edit */
/********/

extern int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
extern int get_config_number(DB *db, char *pName);
extern int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec);
extern int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName);

/********/
/* list */
/********/

extern int list_get_paths(DB *db, cklist *ckl, int attr);
extern int list_get_programs(DB *db, cklist *ckl);
extern int list_get_path_program_tree(DB *db, cklist *ckl, int attr);

/*******/
/* del */
/*******/

extern int del_transaction_try(DB *db, char *arg, int conf);
#endif /* DBLAYER_H */