aboutsummaryrefslogtreecommitdiffstats
path: root/src/queries.c
blob: 8b0a6e87b6642b3597509c70194fe03361ee2476 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* queries.c - Database stuff for ck -----------------------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 * 
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2019  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include "queries.h"
#include "ckutil.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_PRIMARY);
  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);");

  strcat(tmp, "CREATE TABLE ");
  strcat(tmp, TBL_CTX);
  strcat(tmp, "(");
  strcat(tmp, COL_CTX_KEY);
  strcat(tmp, " TEXT NOT NULL, ");
  strcat(tmp, COL_CTX_VAL);
  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);
}

void dbh_form_query_select_from_joined_eq(char *query, const char *selection, const char* condition) {
  char tmp[STR_L] = "SELECT ";
  strcat(tmp, selection);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_REL);
  strcat(tmp, " JOIN ");
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, " ON ");
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, ".");
  strcat(tmp, COL_PROGRAM_ID);
  strcat(tmp, " = ");
  strcat(tmp, COL_REL_PROGRAM_ID);
  strcat(tmp, " JOIN ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, " ON ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, ".");
  strcat(tmp, COL_CONFIG_ID);
  strcat(tmp, " = ");
  strcat(tmp, COL_REL_CONFIG_ID);
  strcat(tmp, " WHERE ");
  strcat(tmp, condition);
  strcat(tmp, " = ?;");

  strcpy(query, tmp);
}

void dbh_format_query_select_from_joined_like(char *query, const char *selection, const char* condition) {
  char tmp[STR_L] = "SELECT ";
  strcat(tmp, selection);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_REL);
  strcat(tmp, " JOIN ");
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, " ON ");
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, ".");
  strcat(tmp, COL_PROGRAM_ID);
  strcat(tmp, " = ");
  strcat(tmp, COL_REL_PROGRAM_ID);
  strcat(tmp, " JOIN ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, " ON ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, ".");
  strcat(tmp, COL_CONFIG_ID);
  strcat(tmp, " = ");
  strcat(tmp, COL_REL_CONFIG_ID);
  strcat(tmp, " WHERE ");
  strcat(tmp, condition);
  strcat(tmp, " LIKE '%' || ? || '%';");

  strcpy(query, tmp);
}

void dbh_form_query_select_paths_with_attributes(char *query) {
  char tmp[STR_L] = "SELECT ";
  strcat(tmp, COL_CONFIG_PATH);
  strcat(tmp, ",");
  strcat(tmp, COL_CONFIG_SECRET);
  strcat(tmp, ",");
  strcat(tmp, COL_CONFIG_PRIMARY);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_CONFIG);
  strcat(tmp, ";");

  strcpy(query, tmp);
}

void dbh_form_query_select_programs(char *query) {
  char tmp[STR_L] = "SELECT ";
  strcat(tmp, COL_PROGRAM_NAME);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_PROGRAM);
  strcat(tmp, ";");

  strcpy(query, tmp);
}

void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y) {
  char tmp[STR_M] = "DELETE FROM ";
  strcat(tmp, y);
  strcat(tmp, " WHERE ");
  strcat(tmp, x);
  strcat(tmp, " = ?;");

  strcpy(query, tmp);
}

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

  strcpy(query, tmp);
}

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

  strcpy(query, tmp);
}

void dbh_form_query_secret_enabled(char *query) {
  char tmp[STR_M] = "SELECT ";
  strcat(tmp, COL_CTX_VAL);
  strcat(tmp, " FROM ");
  strcat(tmp, TBL_CTX);
  strcat(tmp, " WHERE ");
  strcat(tmp, COL_CTX_KEY);
  strcat(tmp, " = \"");
  strcat(tmp, KEY_CTX_SECRET);
  strcat(tmp, "\";");

  strcpy(query, tmp);
}

void dbh_form_query_set_secret_enabled(char *query) {
  char tmp[STR_L] = "INSERT INTO ";
  strcat(tmp, TBL_CTX);
  strcat(tmp, " VALUES(\"");
  strcat(tmp, KEY_CTX_SECRET);
  strcat(tmp, "\", ?);");

  strcpy(query, tmp);
}