aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c59
1 files changed, 53 insertions, 6 deletions
diff --git a/src/main.c b/src/main.c
index d1a2ee8..f2f0c55 100644
--- a/src/main.c
+++ b/src/main.c
@@ -74,9 +74,10 @@ static void assert_no_single_quote(const char *str)
static int table_next_id_callback(void *_id, int, char **cols, char **)
{
int *id = _id;
- assert(cols);
- assert(cols[0]);
- *id = atoi(cols[0]);
+ if (cols[0])
+ *id = atoi(cols[0]);
+ else
+ *id = 0;
return 0;
}
@@ -122,10 +123,8 @@ static void ftag_list_table(const char *table, const char *col)
char sql[64];
sqlite3 *db = NULL;
int rc;
-
rc = sqlite3_open(DATABASE_PATH, &db);
sqlite3_check(rc, db);
-
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1, "SELECT %s FROM %s;", col, table);
rc = sqlite3_exec(db, sql, ftag_print, NULL, NULL);
@@ -225,6 +224,8 @@ static void ftag_add_one_file(sqlite3 *db,
}
if (date[0] == '\n')
strftime(date, line_len-1, "%Y-%m-%d", now_tm);
+ else
+ date[read_len-1] = '\0';
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1,
@@ -290,6 +291,34 @@ static void ftag_file_list(int argc, char **argv)
ftag_list_table("files", "full_name");
}
+static int get_id_by_col_callback(void *_id, int, char **cols, char **)
+{
+ int *id = _id;
+ if (cols[0]) {
+ *id = atoi(cols[0]);
+ return 0;
+ }
+ else {
+ return 1;
+ }
+}
+
+static int get_id_by_col(sqlite3 *db,
+ const char *table,
+ const char *col,
+ const char *val)
+{
+ int id = -1;
+ char sql[128];
+ memset(sql, 0, sizeof(sql));
+ snprintf(sql, sizeof(sql)-1,
+ "SELECT id FROM %s WHERE %s = '%s';", table, col, val);
+ int rc = sqlite3_exec(db, sql, get_id_by_col_callback, &id, NULL);
+ sqlite3_check(rc, db);
+ assert(id >= 0);
+ return id;
+}
+
static void ftag_file_tag(int argc, char **argv)
{
if (argc < 2) {
@@ -297,6 +326,24 @@ static void ftag_file_tag(int argc, char **argv)
"ftag file tag: must supply a file name and tags\n");
exit(EXIT_FAILURE);
}
+ const char *file = argv[0];
+ char sql[64];
+ sqlite3 *db;
+ int rc = sqlite3_open(DATABASE_PATH, &db);
+ sqlite3_check(rc, db);
+ int file_id = get_id_by_col(db, "files", "canonical_name", file);
+ printf("%s: debug: file id is %d\n", __func__, file_id);
+ for (int i = 1; i < argc; i++) {
+ int tag_id = get_id_by_col(db, "tags", "name", argv[i]);
+ printf("%s: debug: tag id is %d\n", __func__, tag_id);
+ memset(sql, 0, sizeof(sql));
+ snprintf(sql, sizeof(sql)-1,
+ "INSERT INTO file_tags values(%d, %d);",
+ file_id, tag_id);
+ rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
+ sqlite3_check(rc, db);
+ }
+ sqlite3_close(db);
}
static void ftag_file(int argc, char **argv)
@@ -307,7 +354,7 @@ static void ftag_file(int argc, char **argv)
{.name = "list", .func = ftag_file_list},
{.name = "tag", .func = ftag_file_tag}
};
- const int file_command_count = sizeof(file_commands) / sizeof(struct ftag_command);
+ const int file_command_count = sizeof(file_commands)/ sizeof(struct ftag_command);
parse_args(argc, argv, file_commands, file_command_count);
}