diff options
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/init.py | 18 | ||||
-rw-r--r-- | src/db/tasks.py | 50 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/db/init.py b/src/db/init.py new file mode 100644 index 0000000..9517714 --- /dev/null +++ b/src/db/init.py @@ -0,0 +1,18 @@ +import sqlite3 +import os.path +import time + +def init(path): + is_db_new = not os.path.isfile(path) + database = sqlite3.connect('database') + if is_db_new: + database.cursor().execute( + " CREATE TABLE IF NOT EXISTS tasks(" + " id INTEGER PRIMARY KEY," + " created_at INTEGER NOT NULL," + " modified_at INTEGER NOT NULL," + " name TEXT NOT NULL," + " tag TEXT" + " )") + database.commit() + return database diff --git a/src/db/tasks.py b/src/db/tasks.py new file mode 100644 index 0000000..26a430a --- /dev/null +++ b/src/db/tasks.py @@ -0,0 +1,50 @@ +from sqlite3 import Cursor +import time + +from model.task import Task, TaskForm + +def get(cursor: Cursor) -> Task: + cursor.execute('SELECT id, created_at, modified_at, name, tag FROM tasks') + + res = [] + + for task in cursor.fetchall(): + res.append(Task( + id = task[0], + created_at = task[1], + modified_at = task[2], + name = task[3], + tag = task[4] + )) + + return res + +def insert(cursor: Cursor, taskForm): + now = int(time.time()) + cursor.execute('INSERT INTO tasks(created_at, modified_at, name, tag) VALUES (?, ?, ?, ?)', (now, now, taskForm.name, taskForm.tag)) + return Task( + id = cursor.lastrowid, + created_at = now, + modified_at = now, + name = taskForm.name, + tag = taskForm.tag + ) + +def update(cursor: Cursor, task: Task, taskForm: TaskForm): + now = int(time.time()) + + cursor.execute( + 'UPDATE tasks SET modified_at = ?, name = ?, tag = ? WHERE id = ?', + (now, taskForm.name, taskForm.tag, task.id)) + + return Task( + id = task.id, + created_at = task.created_at, + modified_at = now, + name = taskForm.name, + tag = taskForm.tag + ) + +def delete(cursor: Cursor, ids): + if len(ids) >= 1: + cursor.execute('DELETE FROM tasks WHERE id IN (%s)' % ','.join('?'*len(ids)), ids) |