aboutsummaryrefslogtreecommitdiffstats
path: root/src/dbhelper.c
blob: 2be76edfdda37a9458cef54c30d55660b9ebc928 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "dbhelper.h"

void dbh_form_query_make_tables(char *query) {
  char tmp[STR_L] = "CREATE TABLE ";
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, "(");
  strcat(tmp, COL_PROGRAM_ID);
  strcat(tmp, " INT NOT NULL PRIMARY KEY, "); 
  strcat(tmp, COL_PROGRAM_NAME);
  strcat(tmp, " TEXT NOT NULL); ");

  strcat(tmp, "CREATE TABLE ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, "(");
  strcat(tmp, COL_CONFIG_ID);
  strcat(tmp, " INT NOT NULL PRIMARY KEY, "); 
  strcat(tmp, COL_CONFIG_PATH);
  strcat(tmp, " TEXT NOT NULL, ");
  strcat(tmp, COL_CONFIG_SECRET);
  strcat(tmp, " INT NOT NULL, ");
  strcat(tmp, COL_CONFIG_PRIME);
  strcat(tmp, " INT NOT NULL); ");

  strcat(tmp, "CREATE TABLE ");
  strcat(tmp, TBL_REL);
  strcat(tmp, "(");
  strcat(tmp, COL_REL_PROGRAM_ID);
  strcat(tmp, " INT NOT NULL, "); 
  strcat(tmp, COL_REL_CONFIG_ID);
  strcat(tmp, " INT NOT NULL);");

  strcpy(query, tmp);
}

void dbh_form_query_insert_program(char *query) {
  char tmp[STR_L] = "INSERT INTO ";
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, " VALUES(?, ?);");

  strcpy(query, tmp);
}

void dbh_form_query_insert_config(char *query) {
  char tmp[STR_L] = "INSERT INTO ";
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, " VALUES(?, ?, ?, ?);");

  strcpy(query, tmp);
}

void dbh_form_query_select_id_from(char *query, const char* tableName) {
  char tmp[STR_M] = "SELECT ID FROM ";
  strcat(tmp, tableName);
  strcat(tmp, " ORDER BY ID;");

  strcpy(query, tmp);
}

void dbh_form_query_select_all_tables(char *query) {
  strcpy(query, "SELECT * FROM SQLITE_MASTER WHERE type='table';");
}

void dhb_form_query_insert_relationship(char *query) {
  char tmp[STR_M] = "INSERT INTO ";
  strcat(tmp, TBL_REL);
  strcat(tmp, " VALUES(?, ?);");

  strcpy(query, tmp);
}

void dhb_form_query_find_program(char *query) {
  char tmp[STR_M] = "SELECT ID FROM ";
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, " WHERE ");
  strcat(tmp, COL_PROGRAM_NAME);
  strcat(tmp, " = ?;");

  strcpy(query, tmp);
}

void dhb_form_query_find_config(char *query) {
  char tmp[STR_M] = "SELECT ID FROM ";
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, " WHERE ");
  strcat(tmp, COL_CONFIG_PATH);
  strcat(tmp, " = ?;");

  strcpy(query, tmp);
}

void dhb_form_query_find_relationship(char *query) {
  char tmp[STR_M] = "SELECT ";
  strcat(tmp, COL_REL_PROGRAM_ID);
  strcat(tmp, ", ");
  strcat(tmp, COL_REL_CONFIG_ID);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_REL);
  strcat(tmp, " WHERE ");
  strcat(tmp, COL_REL_PROGRAM_ID);
  strcat(tmp, " = ? AND ");
  strcat(tmp, COL_REL_CONFIG_ID);
  strcat(tmp, " = ?;");

  strcpy(query, tmp);
}