Reformat db too
This commit is contained in:
parent
7d8c37f75c
commit
23e90a4413
@ -1,5 +1,6 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
class DatabaseManager:
|
class DatabaseManager:
|
||||||
def __init__(self, database_name):
|
def __init__(self, database_name):
|
||||||
self.conn = sqlite3.connect(database_name)
|
self.conn = sqlite3.connect(database_name)
|
||||||
@ -14,6 +15,7 @@ class DatabaseManager:
|
|||||||
def close(self):
|
def close(self):
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
||||||
|
|
||||||
class BaseORM:
|
class BaseORM:
|
||||||
def __init__(self, db_manager, table_name, id, **kwargs):
|
def __init__(self, db_manager, table_name, id, **kwargs):
|
||||||
self.db_manager = db_manager
|
self.db_manager = db_manager
|
||||||
@ -31,8 +33,9 @@ class BaseORM:
|
|||||||
|
|
||||||
class Category(BaseORM):
|
class Category(BaseORM):
|
||||||
def __init__(self, db_manager, id, name, activity_count):
|
def __init__(self, db_manager, id, name, activity_count):
|
||||||
super().__init__(db_manager, "categories", id, name=name,
|
super().__init__(
|
||||||
activity_count=activity_count)
|
db_manager, "categories", id, name=name, activity_count=activity_count
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_categories(db_manager, filter_query=None):
|
def list_categories(db_manager, filter_query=None):
|
||||||
@ -65,7 +68,8 @@ class Category(BaseORM):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get_by_id(db_manager, category_id):
|
def get_by_id(db_manager, category_id):
|
||||||
cursor = db_manager.get_cursor()
|
cursor = db_manager.get_cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
categories.id,
|
categories.id,
|
||||||
categories.name,
|
categories.name,
|
||||||
@ -78,7 +82,9 @@ class Category(BaseORM):
|
|||||||
categories.id = activities.category_id
|
categories.id = activities.category_id
|
||||||
WHERE
|
WHERE
|
||||||
categories.id = ?
|
categories.id = ?
|
||||||
""", (category_id,))
|
""",
|
||||||
|
(category_id,),
|
||||||
|
)
|
||||||
|
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if row:
|
if row:
|
||||||
@ -88,7 +94,9 @@ class Category(BaseORM):
|
|||||||
|
|
||||||
class Activity(BaseORM):
|
class Activity(BaseORM):
|
||||||
def __init__(self, db_manager, id, name, category_id, category_name, facts_count):
|
def __init__(self, db_manager, id, name, category_id, category_name, facts_count):
|
||||||
super().__init__(db_manager, "activities", id, name=name, category_id=category_id)
|
super().__init__(
|
||||||
|
db_manager, "activities", id, name=name, category_id=category_id
|
||||||
|
)
|
||||||
self.category_name = category_name
|
self.category_name = category_name
|
||||||
self.facts_count = facts_count
|
self.facts_count = facts_count
|
||||||
|
|
||||||
@ -97,14 +105,17 @@ class Activity(BaseORM):
|
|||||||
|
|
||||||
print(f"moving from {self.id} to {to_activity.id}")
|
print(f"moving from {self.id} to {to_activity.id}")
|
||||||
|
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
UPDATE
|
UPDATE
|
||||||
facts
|
facts
|
||||||
SET
|
SET
|
||||||
activity_id = ?
|
activity_id = ?
|
||||||
WHERE
|
WHERE
|
||||||
activity_id = ?
|
activity_id = ?
|
||||||
""", (to_activity.id, self.id))
|
""",
|
||||||
|
(to_activity.id, self.id),
|
||||||
|
)
|
||||||
|
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
@ -142,12 +153,15 @@ class Activity(BaseORM):
|
|||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
|
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
return [Activity(db_manager, row[0], row[1], row[2], row[3], row[4]) for row in rows]
|
return [
|
||||||
|
Activity(db_manager, row[0], row[1], row[2], row[3], row[4]) for row in rows
|
||||||
|
]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_by_id(db_manager, activity_id):
|
def get_by_id(db_manager, activity_id):
|
||||||
cursor = db_manager.get_cursor()
|
cursor = db_manager.get_cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
activities.id,
|
activities.id,
|
||||||
activities.name,
|
activities.name,
|
||||||
@ -166,7 +180,9 @@ class Activity(BaseORM):
|
|||||||
activities.id = facts.activity_id
|
activities.id = facts.activity_id
|
||||||
WHERE
|
WHERE
|
||||||
activities.id = ?
|
activities.id = ?
|
||||||
""", (activity_id,))
|
""",
|
||||||
|
(activity_id,),
|
||||||
|
)
|
||||||
|
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if row:
|
if row:
|
||||||
|
Loading…
Reference in New Issue
Block a user