From 3a504a241989aae827b698c95dfe498e8e830ce5 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 24 Sep 2022 09:14:36 -0700 Subject: [PATCH] ... --- src/data/attr.c | 134 ++++++++++++++++++++++----------------------- src/data/recent.c | 110 ++++++++++++++++++------------------- src/data/setup.c | 27 ++++----- src/data/workout.c | 124 ++++++++++++++++++++--------------------- 4 files changed, 198 insertions(+), 197 deletions(-) diff --git a/src/data/attr.c b/src/data/attr.c index d4b7ba9..441bc48 100644 --- a/src/data/attr.c +++ b/src/data/attr.c @@ -3,32 +3,32 @@ int attribute_count() { int count = 0; - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,ATTRIBUTE_COUNT_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,ATTRIBUTE_COUNT_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_ROW) { goto cleanup; } + if(sqlite3_step(stmt)!=SQLITE_ROW) { goto cleanup; } - count = sqlite3_column_int(stmt_p,0); + count = sqlite3_column_int(stmt,0); - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return count; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int attribute_delete(const char *name) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; int i, index, count, mask; index = attribute_index(name); @@ -45,67 +45,67 @@ int attribute_delete(const char *name) { i--; } - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_exec(db_p,"BEGIN;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_exec(db,"BEGIN;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } // delete attribute - if(sqlite3_prepare_v2(db_p,ATTRIBUTE_DELETE_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,ATTRIBUTE_DELETE_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; // shift attributes flags in workouts table to account for now missing attribute - if(sqlite3_prepare_v2(db_p,WORKOUT_SHIFT_ATTRIBUTE_FLAGS_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,1,index)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,2,mask)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,3,count)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_SHIFT_ATTRIBUTE_FLAGS_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,1,index)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,2,mask)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,3,count)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; // commit changes to database - if(sqlite3_exec(db_p,"COMMIT;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_exec(db,"COMMIT;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - sqlite3_exec(db_p,"ROLLBACK;",NULL,NULL,NULL); - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + sqlite3_exec(db,"ROLLBACK;",NULL,NULL,NULL); + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int attribute_get(void (*print)(const unsigned char*)) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,ATTRIBUTE_GET_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,ATTRIBUTE_GET_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } int ret; - while((ret = sqlite3_step(stmt_p))==SQLITE_ROW) { - const unsigned char *name = sqlite3_column_text(stmt_p,0); + while((ret = sqlite3_step(stmt))==SQLITE_ROW) { + const unsigned char *name = sqlite3_column_text(stmt,0); print(name); } if(ret!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } @@ -139,24 +139,24 @@ void attribute_index_helper(const unsigned char *name) { } int attribute_insert(const char *name) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,ATTRIBUTE_INSERT_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,ATTRIBUTE_INSERT_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } @@ -200,26 +200,26 @@ unsigned int attribute_template(const char *p, int len) { } int attribute_update(const char *from, const char *to) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,ATTRIBUTE_UPDATE_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,to,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,from,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,ATTRIBUTE_UPDATE_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,to,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,from,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(0==sqlite3_changes(db_p)) { goto cleanup; } + if(0==sqlite3_changes(db)) { goto cleanup; } - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } diff --git a/src/data/recent.c b/src/data/recent.c index f665eb9..2638f46 100644 --- a/src/data/recent.c +++ b/src/data/recent.c @@ -1,110 +1,110 @@ #include int recent_delete(const char *workout, const char *date) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,RECENT_DELETE_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,RECENT_DELETE_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int recent_get(const char *term, int limit, void (*f)(const unsigned char*,const unsigned char*)) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } if(NULL==term) { - if(sqlite3_prepare_v2(db_p,RECENT_GET_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,1,limit)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,RECENT_GET_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,1,limit)!=SQLITE_OK) { goto cleanup; } } else { - if(sqlite3_prepare_v2(db_p,RECENT_GET_SEARCH_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,term,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,2,limit)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,RECENT_GET_SEARCH_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,term,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,2,limit)!=SQLITE_OK) { goto cleanup; } } int ret; - while((ret = sqlite3_step(stmt_p))==SQLITE_ROW) { - const unsigned char *name_p = sqlite3_column_text(stmt_p,0); - const unsigned char *date_p = sqlite3_column_text(stmt_p,1); + while((ret = sqlite3_step(stmt))==SQLITE_ROW) { + const unsigned char *name_p = sqlite3_column_text(stmt,0); + const unsigned char *date_p = sqlite3_column_text(stmt,1); f(name_p,date_p); } if(ret!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int recent_insert(const char *workout, const char *date) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_exec(db_p,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_exec(db,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,RECENT_INSERT_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,RECENT_INSERT_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int recent_update(const char *workout, const char *from_date, const char *to_date) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,RECENT_UPDATE_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,to_date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,from_date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,3,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,RECENT_UPDATE_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,to_date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,from_date,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,3,workout,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(0==sqlite3_changes(db_p)) { goto cleanup; } + if(0==sqlite3_changes(db)) { goto cleanup; } - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } diff --git a/src/data/setup.c b/src/data/setup.c index 0046307..688ab11 100644 --- a/src/data/setup.c +++ b/src/data/setup.c @@ -2,34 +2,35 @@ #define SETUP_FILE_MISSING_PROMPT "%s doesn't exist... create? [Y/n] " -static int create_prompt(struct sqlite3**db); +static int create_prompt(); +static int exists(); -static int create_prompt(struct sqlite3 **db) { +static int create_prompt() { log_err(SETUP_FILE_MISSING_PROMPT,DB_FILENAME); if(fgetc(stdin)!='Y') { return -1; } - - if(sqlite3_open_v2(DB_FILENAME,db,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL)!=SQLITE_OK) { return -1; } + return 1; +} +static int exists() { + if(access(DB_FILENAME,F_OK)!=0) { return -1; } return 1; } int setup() { struct sqlite3 *db = NULL; - int open_flags = SQLITE_OPEN_READWRITE; - if(global_opts.auto_create!=0) { - open_flags |= SQLITE_OPEN_CREATE; + if(exists()<0) { + if(global_opts.auto_create==0) { + if(create_prompt()<0) { return -1; } + } } if(sqlite3_open_v2( DB_FILENAME, /* const char * filename */ &db, /* sqlite3 **ppDb */ - open_flags, /* int flags */ + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* int flags */ NULL /* const char *zVfs */ - )!=SQLITE_OK) { - if(db!=NULL) { sqlite3_close_v2(db); } - if(create_prompt(&db)<0) { goto cleanup; } - } + )!=SQLITE_OK) { goto cleanup; } if(sqlite3_exec(db,CREATE_SCHEMA_SQL,NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } @@ -37,6 +38,6 @@ int setup() { return 1; cleanup: - if(db!=NULL) { sqlite3_close_v2(db); } + sqlite3_close_v2(db); return -1; } diff --git a/src/data/workout.c b/src/data/workout.c index d6328a2..69f7771 100644 --- a/src/data/workout.c +++ b/src/data/workout.c @@ -1,32 +1,32 @@ #include int workout_delete(const char *name) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_exec(db_p,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_exec(db,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,WORKOUT_DELETE_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_DELETE_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int workout_get(const char *term, const char *filter, int limit, void (*print_row)(const unsigned char*,int,const unsigned char*)) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; int required = 0; int exclude = 0; @@ -34,62 +34,62 @@ int workout_get(const char *term, const char *filter, int limit, void (*print_ro if(attribute_parse(filter,&required,&exclude)<0) { return -1; } } - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } if(NULL==term) { - if(sqlite3_prepare_v2(db_p,WORKOUT_GET_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,1,required)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,2,exclude)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,3,limit)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_GET_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,1,required)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,2,exclude)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,3,limit)!=SQLITE_OK) { goto cleanup; } } else { - if(sqlite3_prepare_v2(db_p,WORKOUT_GET_SEARCH_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,term,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,2,required)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,3,exclude)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,4,limit)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_GET_SEARCH_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,term,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,2,required)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,3,exclude)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,4,limit)!=SQLITE_OK) { goto cleanup; } } int ret; - while((ret = sqlite3_step(stmt_p))==SQLITE_ROW) { - const unsigned char *name_p = sqlite3_column_text(stmt_p,0); - const unsigned char *last_p = sqlite3_column_text(stmt_p,2); - print_row(name_p,sqlite3_column_int(stmt_p,1),last_p); + while((ret = sqlite3_step(stmt))==SQLITE_ROW) { + const unsigned char *name_p = sqlite3_column_text(stmt,0); + const unsigned char *last_p = sqlite3_column_text(stmt,2); + print_row(name_p,sqlite3_column_int(stmt,1),last_p); } if(ret!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } int workout_insert(const char *name, unsigned int flags) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_prepare_v2(db_p,WORKOUT_INSERT_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,2,flags)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_INSERT_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,2,flags)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } @@ -135,34 +135,34 @@ void workout_toggle_helper(const unsigned char *name, int flags, const unsigned } int workout_update(const char *old, const char *name, int flags) { - sqlite3 *db_p = NULL; - sqlite3_stmt *stmt_p = NULL; + struct sqlite3 *db = NULL; + struct sqlite3_stmt *stmt = NULL; - if(sqlite3_open_v2(DB_FILENAME,&db_p,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_open_v2(DB_FILENAME,&db,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_exec(db_p,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_exec(db,"PRAGMA foreign_keys = ON;",NULL,NULL,NULL)!=SQLITE_OK) { goto cleanup; } if(name!=NULL) { - if(sqlite3_prepare_v2(db_p,WORKOUT_UPDATE_NAME_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,old,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_UPDATE_NAME_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,1,name,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,old,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } } else { - if(sqlite3_prepare_v2(db_p,WORKOUT_UPDATE_ATTRIBUTES_SQL,-1,&stmt_p,NULL)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_int(stmt_p,1,flags)!=SQLITE_OK) { goto cleanup; } - if(sqlite3_bind_text(stmt_p,2,old,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_prepare_v2(db,WORKOUT_UPDATE_ATTRIBUTES_SQL,-1,&stmt,NULL)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_int(stmt,1,flags)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_bind_text(stmt,2,old,-1,SQLITE_STATIC)!=SQLITE_OK) { goto cleanup; } } - if(sqlite3_step(stmt_p)!=SQLITE_DONE) { goto cleanup; } - if(sqlite3_finalize(stmt_p)!=SQLITE_OK) { goto cleanup; } - stmt_p = NULL; + if(sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } + if(sqlite3_finalize(stmt)!=SQLITE_OK) { goto cleanup; } + stmt = NULL; - if(0==sqlite3_changes(db_p)) { goto cleanup; } + if(0==sqlite3_changes(db)) { goto cleanup; } - if(sqlite3_close_v2(db_p)!=SQLITE_OK) { goto cleanup; } + if(sqlite3_close_v2(db)!=SQLITE_OK) { goto cleanup; } return 1; cleanup: - if(stmt_p!=NULL) { sqlite3_finalize(stmt_p); } - if(db_p!=NULL) { sqlite3_close_v2(db_p); } + if(stmt!=NULL) { sqlite3_finalize(stmt); } + if(db!=NULL) { sqlite3_close_v2(db); } return -1; } -- 2.39.5