104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
import logging
|
|
import sqlite3 as sq
|
|
|
|
from classes import Activity, ActivityType, Tag, VetroProject
|
|
from enums import ActivityTypes
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Database:
|
|
def __init__(self, db_name: str, sql_path: str):
|
|
self.con = sq.connect(db_name)
|
|
cursor = self.con.cursor()
|
|
|
|
try:
|
|
with open(sql_path, 'r', encoding='utf-8') as f:
|
|
sql_script = f.read()
|
|
|
|
cursor.executescript(sql_script)
|
|
self.con.commit()
|
|
logger.info("База данных успешно инициализирована!")
|
|
except Exception as e:
|
|
logger.error("Ошибка при инициализации БД:", e)
|
|
self.con.rollback()
|
|
|
|
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.description,
|
|
act.time_spent,
|
|
act.date_start,
|
|
act.date_end,
|
|
act.is_tracked,
|
|
act.vetro_project_id,
|
|
act.tag_id,
|
|
act.project_id,
|
|
)
|
|
)
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logger.debug(e)
|
|
|
|
# Доделать
|
|
def insert_time_entries(self, activities: list[Activity]):
|
|
cur = self.con.cursor()
|
|
for act in activities:
|
|
try:
|
|
cur.execute(
|
|
"""INSERT INTO time_entry
|
|
(date, description, activity_id, time_spent, task_id, activity_type_id)
|
|
VALUES(?,?,?,?,?,?)""",
|
|
(
|
|
act.date_start.date(),
|
|
act.description,
|
|
act.id,
|
|
act.time_spent,
|
|
act.task_id,
|
|
ActivityTypes.DEVELOPMENT
|
|
)
|
|
)
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logger.debug(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:
|
|
logger.debug(e)
|
|
|
|
|
|
def insert_vetro_projects(self, vetro_projects: list[VetroProject]):
|
|
cur = self.con.cursor()
|
|
for vetro_project in vetro_projects:
|
|
try:
|
|
cur.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title))
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logger.debug(e)
|
|
|
|
def insert_activity_types(self, activity_types: list[ActivityType]):
|
|
cur = self.con.cursor()
|
|
for activity_type in activity_types:
|
|
try:
|
|
cur.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title))
|
|
self.con.commit()
|
|
except Exception as e:
|
|
logger.debug(e)
|