self.cursor
This commit is contained in:
31
db/db.py
31
db/db.py
@@ -10,13 +10,13 @@ logger = logging.getLogger(__name__)
|
|||||||
class Database:
|
class Database:
|
||||||
def __init__(self, db_name: str, sql_path: str):
|
def __init__(self, db_name: str, sql_path: str):
|
||||||
self.con = sq.connect(db_name)
|
self.con = sq.connect(db_name)
|
||||||
cursor = self.con.cursor()
|
self.cursor = self.con.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(sql_path, 'r', encoding='utf-8') as f:
|
with open(sql_path, 'r', encoding='utf-8') as f:
|
||||||
sql_script = f.read()
|
sql_script = f.read()
|
||||||
|
|
||||||
cursor.executescript(sql_script)
|
self.cursor.executescript(sql_script)
|
||||||
self.con.commit()
|
self.con.commit()
|
||||||
logger.info("База данных успешно инициализирована!")
|
logger.info("База данных успешно инициализирована!")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -25,16 +25,15 @@ class Database:
|
|||||||
|
|
||||||
# Сделать изменение всех полей при IntegrityError
|
# Сделать изменение всех полей при IntegrityError
|
||||||
def insert_activities(self, activities: list[Activity]):
|
def insert_activities(self, activities: list[Activity]):
|
||||||
cur = self.con.cursor()
|
|
||||||
is_updated = True
|
is_updated = True
|
||||||
for act in activities:
|
for act in activities:
|
||||||
if is_updated:
|
if is_updated:
|
||||||
tags = cur.execute("SELECT * FROM tags WHERE id = ?", (act.tag_id, )).fetchall()
|
tags = self.cursor.execute("SELECT * FROM tags WHERE id = ?", (act.tag_id, )).fetchall()
|
||||||
vetro_projects = cur.execute("SELECT * FROM vetro_projects WHERE id = ?", (act.vetro_project_id, )).fetchall()
|
vetro_projects = self.cursor.execute("SELECT * FROM vetro_projects WHERE id = ?", (act.vetro_project_id, )).fetchall()
|
||||||
if not tags or not vetro_projects:
|
if not tags or not vetro_projects:
|
||||||
is_updated = False
|
is_updated = False
|
||||||
try:
|
try:
|
||||||
cur.execute(
|
self.cursor.execute(
|
||||||
"INSERT INTO activities VALUES(?,?,?,?,?,?,?,?,?)",
|
"INSERT INTO activities VALUES(?,?,?,?,?,?,?,?,?)",
|
||||||
(
|
(
|
||||||
act.id,
|
act.id,
|
||||||
@@ -49,7 +48,7 @@ class Database:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
except sq.IntegrityError:
|
except sq.IntegrityError:
|
||||||
cur.execute(
|
self.cursor.execute(
|
||||||
"""UPDATE activities
|
"""UPDATE activities
|
||||||
SET description = ?, time_spent = ?, date_start = ?, date_end = ?, is_tracked = ?, vetro_project_id = ?, tag_id = ?, project_id = ?
|
SET description = ?, time_spent = ?, date_start = ?, date_end = ?, is_tracked = ?, vetro_project_id = ?, tag_id = ?, project_id = ?
|
||||||
WHERE id = ?""",
|
WHERE id = ?""",
|
||||||
@@ -71,10 +70,9 @@ class Database:
|
|||||||
logging.info("База данных устарела. Рекомендуем обновить с помощью флага -i [--init-db]")
|
logging.info("База данных устарела. Рекомендуем обновить с помощью флага -i [--init-db]")
|
||||||
|
|
||||||
def insert_time_entries(self, activities: list[Activity]):
|
def insert_time_entries(self, activities: list[Activity]):
|
||||||
cur = self.con.cursor()
|
|
||||||
for act in activities:
|
for act in activities:
|
||||||
try:
|
try:
|
||||||
cur.execute(
|
self.cursor.execute(
|
||||||
"""INSERT INTO time_entry
|
"""INSERT INTO time_entry
|
||||||
(date, description, activity_id, time_spent, task_id, activity_type_id)
|
(date, description, activity_id, time_spent, task_id, activity_type_id)
|
||||||
VALUES(?,?,?,?,?,?)""",
|
VALUES(?,?,?,?,?,?)""",
|
||||||
@@ -93,12 +91,11 @@ class Database:
|
|||||||
|
|
||||||
|
|
||||||
def insert_tags(self, tags: list[Tag]):
|
def insert_tags(self, tags: list[Tag]):
|
||||||
cur = self.con.cursor()
|
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
try:
|
try:
|
||||||
cur.execute("INSERT INTO tags VALUES(?, ?)",(tag.id,tag.title))
|
self.cursor.execute("INSERT INTO tags VALUES(?, ?)",(tag.id,tag.title))
|
||||||
except sq.IntegrityError:
|
except sq.IntegrityError:
|
||||||
cur.execute(
|
self.cursor.execute(
|
||||||
"""UPDATE tags
|
"""UPDATE tags
|
||||||
SET title = ?
|
SET title = ?
|
||||||
WHERE id = ?""",
|
WHERE id = ?""",
|
||||||
@@ -109,12 +106,11 @@ class Database:
|
|||||||
|
|
||||||
|
|
||||||
def insert_vetro_projects(self, vetro_projects: list[VetroProject]):
|
def insert_vetro_projects(self, vetro_projects: list[VetroProject]):
|
||||||
cur = self.con.cursor()
|
|
||||||
for vetro_project in vetro_projects:
|
for vetro_project in vetro_projects:
|
||||||
try:
|
try:
|
||||||
cur.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title))
|
self.cursor.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title))
|
||||||
except sq.IntegrityError:
|
except sq.IntegrityError:
|
||||||
cur.execute("""
|
self.cursor.execute("""
|
||||||
UPDATE vetro_projects
|
UPDATE vetro_projects
|
||||||
SET title = ?
|
SET title = ?
|
||||||
WHERE id = ?""",
|
WHERE id = ?""",
|
||||||
@@ -124,12 +120,11 @@ class Database:
|
|||||||
self.con.commit()
|
self.con.commit()
|
||||||
|
|
||||||
def insert_activity_types(self, activity_types: list[ActivityType]):
|
def insert_activity_types(self, activity_types: list[ActivityType]):
|
||||||
cur = self.con.cursor()
|
|
||||||
for activity_type in activity_types:
|
for activity_type in activity_types:
|
||||||
try:
|
try:
|
||||||
cur.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title))
|
self.cursor.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title))
|
||||||
except sq.IntegrityError:
|
except sq.IntegrityError:
|
||||||
cur.execute("""
|
self.cursor.execute("""
|
||||||
UPDATE activity_types
|
UPDATE activity_types
|
||||||
SET title = ?
|
SET title = ?
|
||||||
WHERE id = ?""",
|
WHERE id = ?""",
|
||||||
|
|||||||
Reference in New Issue
Block a user