127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
import logging
|
|
import sqlite3 as sq
|
|
|
|
from classes import Activity, Tag
|
|
|
|
|
|
class Database:
|
|
def __init__(self, db_name: str):
|
|
try:
|
|
self.con = sq.connect(db_name)
|
|
self.con.cursor().executescript('''CREATE TABLE IF NOT EXISTS "activities" (
|
|
"id" VARCHAR NOT NULL UNIQUE,
|
|
"vetro_project_id" VARCHAR NOT NULL,
|
|
"vetro_project_title" VARCHAR NOT NULL,
|
|
"task_id" VARCHAR NOT NULL,
|
|
"description" VARCHAR NOT NULL,
|
|
"task_title" VARCHAR NOT NULL,
|
|
"tag_id" VARCHAR NOT NULL,
|
|
"project_id" VARCHAR NOT NULL,
|
|
"time_spent" NUMERIC NOT NULL,
|
|
"date_start" DATETIME NOT NULL,
|
|
"date_end" DATETIME,
|
|
"is_tracked" BOOLEAN NOT NULL DEFAULT false,
|
|
PRIMARY KEY("id"),
|
|
FOREIGN KEY ("tag_id") REFERENCES "tags"("id")
|
|
ON UPDATE NO ACTION ON DELETE NO ACTION
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "tags" (
|
|
"id" VARCHAR NOT NULL UNIQUE,
|
|
"title" VARCHAR NOT NULL,
|
|
PRIMARY KEY("id")
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "vetro_projects" (
|
|
"id" VARCHAR NOT NULL UNIQUE,
|
|
"title" VARCHAR NOT NULL,
|
|
PRIMARY KEY("id"),
|
|
FOREIGN KEY ("id") REFERENCES "activities"("vetro_project_id")
|
|
ON UPDATE NO ACTION ON DELETE NO ACTION
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "time_entry" (
|
|
"id" INTEGER NOT NULL UNIQUE,
|
|
"activity_id" VARCHAR NOT NULL,
|
|
"task_id" INTEGER NOT NULL,
|
|
"date" DATETIME NOT NULL,
|
|
"time_spent" NUMERIC NOT NULL,
|
|
"description" VARCHAR NOT NULL,
|
|
"activity_type" INTEGER NOT NULL CHECK(activity_type in (9, 8, 57, 58, 60)),
|
|
PRIMARY KEY("id"),
|
|
FOREIGN KEY ("activity_id") REFERENCES "activities"("id")
|
|
ON UPDATE NO ACTION ON DELETE NO ACTION,
|
|
FOREIGN KEY ("task_id") REFERENCES "activities"("task_id")
|
|
ON UPDATE NO ACTION ON DELETE NO ACTION
|
|
);
|
|
''')
|
|
except Exception as e:
|
|
raise(e)
|
|
|
|
def insert_activities(self, activities: list[Activity]):
|
|
cur = self.con.cursor()
|
|
for act in activities:
|
|
try:
|
|
cur.execute(
|
|
"INSERT INTO activities VALUES(?, ?, ? ,? ,? ,? ,? ,? ,? ,? ,? ,?)",
|
|
(
|
|
act.id,act.vetro_project_id,
|
|
act.vetro_project_title,
|
|
act.task_id,
|
|
act.description,
|
|
act.task_title,
|
|
act.tag_id,
|
|
act.project_id,
|
|
act.time_spent,
|
|
act.date_start,
|
|
act.date_end,
|
|
act.is_tracked
|
|
)
|
|
)
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logging.error(e)
|
|
|
|
# Доделать
|
|
def insert_time_entries(self, activities: list[Activity]):
|
|
cur = self.con.cursor()
|
|
for act in activities:
|
|
try:
|
|
cur.execute(
|
|
"INSERT INTO time_entry VALUES(?, ?, ? ,? ,? ,? ,? ,? ,? ,? ,? ,?)",
|
|
(
|
|
act.id,act.vetro_project_id,
|
|
act.vetro_project_title,
|
|
act.task_id,
|
|
act.description,
|
|
act.task_title,
|
|
act.tag_id,
|
|
act.project_id,
|
|
act.time_spent,
|
|
act.date_start,
|
|
act.date_end,
|
|
act.is_tracked
|
|
)
|
|
)
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logging.error(e)
|
|
|
|
|
|
def insert_tags(self, tags: list[Tag]):
|
|
cur = self.con.cursor()
|
|
for tag in tags:
|
|
try:
|
|
cur.execute(
|
|
"INSERT INTO tags VALUES(?, ?)",
|
|
(
|
|
tag.id,
|
|
tag.title
|
|
)
|
|
)
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logging.error(e)
|
|
|
|
|