Return database objects instead of queries

Require object class when creating connection classes
This commit is contained in:
George Lacey 2021-05-05 14:23:45 +01:00
parent 86daf3ea7f
commit 8559df502d
8 changed files with 27 additions and 29 deletions

View File

@ -1,5 +1,5 @@
from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn
from .object import Archive
from .object import Repo, Archive, Label
from .object.label import Label
from pathlib import Path
@ -44,17 +44,11 @@ class BorgDatabase(object):
# region GET
def get_repos(self):
return self.repo_conn.get_all()
def get_repo_name(self, repo):
return self.label_conn.get_repo_name(repo.primary_key)
def get_cache(self, repo):
archive = Archive.from_sql(self.get_latest_archive(repo))
archive = self.archive_conn.get_latest(repo.primary_key)
return self.cache_conn.get(archive.primary_key)
def get_latest_archive(self, repo):
return self.archive_conn.get_latest(repo.primary_key)
# endregion

View File

@ -7,7 +7,7 @@ class ArchiveConn(DatabaseConnection):
def __init__(self, db_path, repo_table: str,
table_name: str = "archive"):
self.repo_table = repo_table
super().__init__(db_path, table_name)
super().__init__(db_path, Archive, table_name)
# region INIT
@ -56,8 +56,8 @@ class ArchiveConn(DatabaseConnection):
# region QUERIES
def get_latest(self, repo_id: int):
return self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE repo_id = ?"
f" ORDER BY id DESC LIMIT 1;", (repo_id,))
return Archive.from_sql(self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE repo_id = ?"
f" ORDER BY id DESC LIMIT 1;", (repo_id,)))
def archive_on_hour(self, repo_id, date: datetime):
date_string = str(date.date())

View File

@ -1,10 +1,11 @@
from .databaseconnection import DatabaseConnection
from borgmanager.database.object import Cache
class CacheConn(DatabaseConnection):
def __init__(self, db_path, archive_table: str, table_name: str = "cache"):
self.archive_table = archive_table
super().__init__(db_path, table_name)
super().__init__(db_path, Cache, table_name)
# region INIT
@ -50,7 +51,7 @@ class CacheConn(DatabaseConnection):
# region QUERIES
def get(self, archive_id: int):
return self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE archive_id = ?"
f" ORDER BY id DESC LIMIT 1;", (archive_id,))
return Cache.from_sql(self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE archive_id = ?"
f" ORDER BY id DESC LIMIT 1;", (archive_id,)))
# endregion

View File

@ -4,7 +4,9 @@ import sqlite3
class DatabaseConnection(ABC):
def __init__(self, db_path, table_name: str):
def __init__(self, db_path, object_class, table_name: str):
self.object_class = object_class
self.__sql_lock = Lock()
self.__sql_database = sqlite3.connect(db_path,
@ -120,7 +122,7 @@ class DatabaseConnection(ABC):
# region QUERIES
def get_all(self):
return self.sql_execute_all(f"SELECT * FROM {self._sql_table};")
return [self.object_class.from_sql(row) for row in self.sql_execute_all(f"SELECT * FROM {self._sql_table};")]
# endregion

View File

@ -1,11 +1,12 @@
from .databaseconnection import DatabaseConnection
from borgmanager.database.object import Error
from datetime import datetime
class ErrorConn(DatabaseConnection):
def __init__(self, db_path, label_table: str, table_name: str = "errors"):
self.label_table = label_table
super().__init__(db_path, table_name)
super().__init__(db_path, Error, table_name)
def _create_table(self):
create_statement = f"create table if not exists {self._sql_table}(" \

View File

@ -1,4 +1,5 @@
from .databaseconnection import DatabaseConnection
from borgmanager.database.object import Label
class LabelConn(DatabaseConnection):
@ -6,7 +7,7 @@ class LabelConn(DatabaseConnection):
table_name: str = "label"):
self.repo_table = repo_table
super().__init__(db_path, table_name)
super().__init__(db_path, Label, table_name)
# region INIT

View File

@ -1,9 +1,10 @@
from .databaseconnection import DatabaseConnection
from borgmanager.database.object import Repo
class RepoConn(DatabaseConnection):
def __init__(self, db_path, table_name: str = 'repo'):
super(RepoConn, self).__init__(db_path, table_name)
super(RepoConn, self).__init__(db_path, Repo, table_name)
def _create_table(self):
create_statement = f"create table if not exists {self._sql_table}(" \

View File

@ -11,13 +11,12 @@ class Summary(object):
print(self.print_repo_stats())
def print_repo_stats(self):
repo_sql = self.db.get_repos()
repos = self.db.repo_conn.get_all()
return_string = ""
for line in repo_sql:
repo = Repo.from_sql(line)
latest_archive = Archive.from_sql(self.db.get_latest_archive(repo))
cache = Cache.from_sql(self.db.get_cache(repo))
for repo in repos:
latest_archive = self.db.archive_conn.get_latest(repo.primary_key)
cache = self.db.get_cache(repo)
repo_name = self.db.get_repo_name(repo)
if repo_name is not None:
return_string += f"{repo_name} ({repo.location}):\n"
@ -32,11 +31,10 @@ class Summary(object):
return return_string.strip()
def get_backup_line(self, repo_id):
units = []
units.append(['H' if h else '_' for h in self.get_archive_hours(repo_id, 24)])
units.append(['D' if d else '_' for d in self.get_archive_days(repo_id, 7)])
units.append(['W' if w else '_' for w in self.get_archive_units(repo_id, 5, 7)])
units.append(['M' if m else '_' for m in self.get_archive_units(repo_id, 12, 30)])
units = [['H' if h else '_' for h in self.get_archive_hours(repo_id, 24)],
['D' if d else '_' for d in self.get_archive_days(repo_id, 7)],
['W' if w else '_' for w in self.get_archive_units(repo_id, 5, 7)],
['M' if m else '_' for m in self.get_archive_units(repo_id, 12, 30)]]
return f"[{']['.join([''.join(u) for u in units])}]"