Skip to content

Commit cd94aa5

Browse files
committed
Implement dump database feature
1 parent 9b9353e commit cd94aa5

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

single-HTTP/config/server.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
port=8888
22
document_root=/home/elliott/Github/C-Server-Collection/single-HTTP/
33
log_root=/home/elliott/Github/C-Server-Collection/single-HTTP/logs/
4-
database_path=/home/elliott/Github/C-Server-Collection/single-HTTP/database/db.sqlite3/bob
4+
database_path=/home/elliott/Github/C-Server-Collection/single-HTTP/database/db.sqlite3

single-HTTP/lib/sqlite3/sqlite3.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "../types/types.h"
1414
#include "../colors/colors.h"
1515

16+
#include "../../debug.h"
17+
18+
#define ALL_TABLES -1
19+
1620
typedef struct query_s {
1721
String stmt, specifiers;
1822
bool is_parameterized;
@@ -191,11 +195,44 @@ void sqlite_load_exec(const String restrict filepath) {
191195
sqlite_exec(sql_buf);
192196
}
193197

194-
void sqlite_dumpdata(const String restrict table) {
195-
if (!table)
196-
puts("Database");
197-
else
198-
puts("Specific table");
198+
void sqlite_dumpdb(void) {
199+
sqlite3 *d_db, *s_db;
200+
sqlite3_backup *backup;
201+
int result_code = sqlite3_open(_db_path, &s_db);
202+
203+
if (result_code != SQLITE_OK) {
204+
fprintf(stderr, RED "Database Error: Cannot open database: %s\n" RESET, sqlite3_errmsg(s_db));
205+
exit(EXIT_FAILURE);
206+
}
207+
result_code = sqlite3_open("database/copy.sqlite3", &d_db);
208+
209+
if (result_code != SQLITE_OK) {
210+
fprintf(stderr, RED "Database Error: Cannot open database: %s\n" RESET, sqlite3_errmsg(d_db));
211+
exit(EXIT_FAILURE);
212+
}
213+
backup = sqlite3_backup_init(d_db, "main", s_db, "main");
214+
215+
if (!backup) {
216+
fprintf(stderr, RED "Database Error: Cannot initalize database copy: %s\n" RESET, sqlite3_errmsg(d_db));
217+
exit(EXIT_FAILURE);
218+
}
219+
result_code = sqlite3_backup_step(backup, ALL_TABLES);
220+
221+
while (result_code == SQLITE_OK)
222+
result_code = sqlite3_backup_step(backup, ALL_TABLES);
223+
224+
if (result_code != SQLITE_DONE) {
225+
fprintf(stderr, RED "Database Error: Cannot copy database: %s\n" RESET, sqlite3_errmsg(s_db));
226+
exit(EXIT_FAILURE);
227+
}
228+
sqlite3_backup_finish(backup);
229+
sqlite3_close(s_db);
230+
231+
return;
232+
}
233+
234+
void sqlite_dumptable(const String restrict table) {
235+
puts("Dump table");
199236
return;
200237
}
201238

single-HTTP/lib/sqlite3/sqlite3.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
extern int sqlite_exec(const String restrict, ...);
77
extern void sqlite_load_fixture(const String restrict);
8-
extern void sqlite_dumpdata(const String restrict);
8+
extern void sqlite_dumpdb(void);
9+
extern void sqlite_dumptable(const String restrict);
910
extern void sqlite_load_exec(const String restrict);
1011
extern String sqlite_get_version(void);
1112

single-HTTP/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define STR_MAX 2048
4646
#define PORT_MIN 0
4747
#define PORT_MAX 65536
48-
#define MAX_ARGS 4
48+
#define MAX_ARGS 10
4949
#define PACKET_MAX 1024
5050
#define HTTP_VER_AMT 3
5151
#define HTTP_REQ_AMT 8
@@ -179,9 +179,9 @@ void compute_flags(const int argc, String *const argv, bool *v_flag) { // Done
179179
exit(EXIT_SUCCESS);
180180
case 'd':
181181
if (optarg)
182-
sqlite_dumpdata(optarg);
182+
sqlite_dumptable(optarg);
183183
else
184-
sqlite_dumpdata(NULL);
184+
sqlite_dumpdb();
185185
exit(EXIT_SUCCESS);
186186
case 'l':
187187
sqlite_load_exec(optarg);
@@ -607,6 +607,8 @@ int main(const int argc, String *const argv) {
607607
"Using: %s\n" RESET,
608608
_port, _doc_root, _log_root, sqlite_get_version());
609609

610+
sqlite_exec("SELECT * FROM test;");
611+
610612
const int default_root_len = strnlen(_doc_root, PATH_MAX);
611613

612614
while (sigint_flag) {

0 commit comments

Comments
 (0)