Update.
This commit is contained in:
parent
57c6619ce9
commit
611c27dc25
29
db_utils.h
29
db_utils.h
@ -22,22 +22,22 @@ json_object *db_set(const char *key, const char *value);
|
||||
json_object *db_get(const char *key);
|
||||
json_object *db_query(const char *query);
|
||||
|
||||
|
||||
|
||||
void db_initialize() {
|
||||
sqlite3 *db;
|
||||
char *err_msg = 0;
|
||||
int rc = sqlite3_open(db_file_expanded(), &db);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *sql =
|
||||
"CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
|
||||
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
db_execute("CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);");
|
||||
db_execute("CREATE TABLE IF NOT EXISTS file_version_history ( id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"path TEXT NOT NULL,"
|
||||
"content TEXT,"
|
||||
"date DATETIME DEFAULT CURRENT_TIMESTAMP"
|
||||
");");
|
||||
|
||||
sqlite3_close(db);
|
||||
}
|
||||
@ -180,6 +180,19 @@ json_object *db_execute(const char *query) {
|
||||
sqlite3_close(db);
|
||||
return result;
|
||||
}
|
||||
void db_store_file_version(const char * path) {
|
||||
char * expanded = expand_home_directory(path);
|
||||
|
||||
char * content = read_file(expanded);
|
||||
if(!content) {
|
||||
return;
|
||||
}
|
||||
fprintf(stderr, "Creating backup:: %s\n", expanded);
|
||||
char * formatted = sqlite3_mprintf("INSERT INTO file_version_history (path, content) VALUES (%Q, %Q)", expanded, content);
|
||||
db_execute(formatted);
|
||||
sqlite3_free(formatted);
|
||||
free(content);
|
||||
}
|
||||
char *db_get_schema() {
|
||||
json_object *tables =
|
||||
db_query("SELECT * FROM sqlite_master WHERE type='table'");
|
||||
|
4
tools.h
4
tools.h
@ -718,7 +718,9 @@ char *tool_function_read_file(char *path) {
|
||||
}
|
||||
|
||||
char *tool_function_write_file(char *path, char *content) {
|
||||
FILE *fp = fopen(path, "w");
|
||||
db_store_file_version(path);
|
||||
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
perror("fopen failed");
|
||||
return strdup("Failed to open file for writing!");
|
||||
|
Loading…
Reference in New Issue
Block a user