-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskerdb.c
141 lines (119 loc) · 3.48 KB
/
taskerdb.c
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
#include "taskerdb.h"
/*
This library handles the database for tasker, it uses
sqlite 3 library,
*/
sqlite3 *db;
char *err_msg = 0;
// Call back function pointer used with sql executions
static int (*callback) (void *, int , char **, char **);
int intialize_tasks_table(){
char *sql;
sql = "CREATE TABLE IF NOT EXISTS TASKS (" \
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," \
"TODO TEXT NOT NULL," \
"CREATION_TIME DATETIME DEFAULT CURRENT_TIMESTAMP," \
"FINISHED_TIME DATETIME ," \
"REMINDER_TIME DATETIME ," \
"ARCHIVE INT default 0," \
"HAS_REMINDER INT NOT NULL,"\
"REMINDER_FREQ INT NOT NULL);";
return sqlite3_exec(db, sql, *callback, 0, &err_msg);
}
/*
Opens DB and set the callback function
name : The name of the db
callback_function: callback_function used with the executions
*/
int intialize_db(char *name, int (*callback_function) (void *, int , char **, char **)){
callback = callback_function;
int rc;
rc = sqlite3_open(name,&db);
if( rc )
return -1;
rc = intialize_tasks_table();
if( rc != SQLITE_OK )
sqlite3_free(err_msg);
else
return -1;
return 0;
}
/*
Returns all the data in the DB to the callback function
*/
int get_all_tasks(){
int rc;
char* sql;
sql = "SELECT * FROM TASKS;";
int flag = PRINT_TASKS;
rc = sqlite3_exec(db,sql,*callback,(void*)&flag,&err_msg);
if( rc != SQLITE_OK )
sqlite3_free(err_msg);
else
return 0;
return -1;
}
/*
Returns the equivelent task by id
*/
int get_task(long id){
int rc;
char* sql;
sql = (char*) malloc(sizeof(char)*40);
sprintf(sql,"SELECT * FROM TASKS WHERE ID=%ld;",id);
int flag = GET_TASK;
rc = sqlite3_exec(db,sql,*callback,(void*)&flag,&err_msg);
if( rc != SQLITE_OK )
sqlite3_free(err_msg);
else
return 0;
return -1;
}
int insert_task(task tsk){
char *sql;
sql = (char*) malloc(sizeof(char)*(300 + strlen(tsk.todo)));
int rc;
sprintf(sql ,"INSERT INTO TASKS (TODO,REMINDER_TIME,ARCHIVE,HAS_REMINDER,REMINDER_FREQ) " \
"VALUES (\"%s\",%s,\"%d\",\"%d\",\"%d\");",tsk.todo,get_formated_timestamp(&tsk.reminder_time),tsk.archive,tsk.has_reminder,tsk.remind_freq);
rc = sqlite3_exec(db,sql,*callback,0,&err_msg);
if( rc != SQLITE_OK )
{
printf("\n %s",err_msg);
sqlite3_free(err_msg);
printf("\n %s",err_msg);
}
else
return 0;
return -1;
}
int edit_task(task tsk){
char *sql;
int rc;
sql =(char*) malloc(sizeof(char)*100 + sizeof(char)*strlen(tsk.todo));
sprintf(sql , "UPDATE TASKS set TODO =\"%s\",CREATION_TIME=\"%s\",FINISHED_TIME=%s,REMINDER_TIME=%s,ARCHIVE=%d,HAS_REMINDER=%d,REMINDER_FREQ=%d where ID=%ld;", tsk.todo,get_formated_timestamp(&tsk.creation_time),get_formated_timestamp(&tsk.finished_time),get_formated_timestamp(&tsk.reminder_time),tsk.archive,tsk.has_reminder,tsk.remind_freq,tsk.id);
rc = sqlite3_exec(db,sql,*callback,0,&err_msg);
if( rc != SQLITE_OK )
{
printf("%s",err_msg);
sqlite3_free(err_msg);
}
else
return 0;
return -1;
}
int delete_task(long id){
char *sql;
int rc;
sql = malloc(sizeof(char) * 100);
sprintf(sql , "DELETE FROM TASKS WHERE ID = %ld ;",id);
rc = sqlite3_exec(db,sql,*callback,0,&err_msg);
if( rc != SQLITE_OK )
{
sqlite3_free(err_msg);}
else
return 0;
return -1;
}
void close_db(){
sqlite3_close(db);
}