Return database objects instead of queries
Require object class when creating connection classes
This commit is contained in:
parent
86daf3ea7f
commit
8559df502d
|
@ -1,5 +1,5 @@
|
||||||
from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn
|
from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn
|
||||||
from .object import Archive
|
from .object import Repo, Archive, Label
|
||||||
from .object.label import Label
|
from .object.label import Label
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -44,17 +44,11 @@ class BorgDatabase(object):
|
||||||
|
|
||||||
# region GET
|
# region GET
|
||||||
|
|
||||||
def get_repos(self):
|
|
||||||
return self.repo_conn.get_all()
|
|
||||||
|
|
||||||
def get_repo_name(self, repo):
|
def get_repo_name(self, repo):
|
||||||
return self.label_conn.get_repo_name(repo.primary_key)
|
return self.label_conn.get_repo_name(repo.primary_key)
|
||||||
|
|
||||||
def get_cache(self, repo):
|
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)
|
return self.cache_conn.get(archive.primary_key)
|
||||||
|
|
||||||
def get_latest_archive(self, repo):
|
|
||||||
return self.archive_conn.get_latest(repo.primary_key)
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
|
@ -7,7 +7,7 @@ class ArchiveConn(DatabaseConnection):
|
||||||
def __init__(self, db_path, repo_table: str,
|
def __init__(self, db_path, repo_table: str,
|
||||||
table_name: str = "archive"):
|
table_name: str = "archive"):
|
||||||
self.repo_table = repo_table
|
self.repo_table = repo_table
|
||||||
super().__init__(db_path, table_name)
|
super().__init__(db_path, Archive, table_name)
|
||||||
|
|
||||||
# region INIT
|
# region INIT
|
||||||
|
|
||||||
|
@ -56,8 +56,8 @@ class ArchiveConn(DatabaseConnection):
|
||||||
# region QUERIES
|
# region QUERIES
|
||||||
|
|
||||||
def get_latest(self, repo_id: int):
|
def get_latest(self, repo_id: int):
|
||||||
return self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE 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,))
|
f" ORDER BY id DESC LIMIT 1;", (repo_id,)))
|
||||||
|
|
||||||
def archive_on_hour(self, repo_id, date: datetime):
|
def archive_on_hour(self, repo_id, date: datetime):
|
||||||
date_string = str(date.date())
|
date_string = str(date.date())
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from .databaseconnection import DatabaseConnection
|
from .databaseconnection import DatabaseConnection
|
||||||
|
from borgmanager.database.object import Cache
|
||||||
|
|
||||||
|
|
||||||
class CacheConn(DatabaseConnection):
|
class CacheConn(DatabaseConnection):
|
||||||
def __init__(self, db_path, archive_table: str, table_name: str = "cache"):
|
def __init__(self, db_path, archive_table: str, table_name: str = "cache"):
|
||||||
self.archive_table = archive_table
|
self.archive_table = archive_table
|
||||||
super().__init__(db_path, table_name)
|
super().__init__(db_path, Cache, table_name)
|
||||||
|
|
||||||
# region INIT
|
# region INIT
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ class CacheConn(DatabaseConnection):
|
||||||
# region QUERIES
|
# region QUERIES
|
||||||
|
|
||||||
def get(self, archive_id: int):
|
def get(self, archive_id: int):
|
||||||
return self.sql_execute_one(f"SELECT * FROM {self._sql_table} WHERE 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,))
|
f" ORDER BY id DESC LIMIT 1;", (archive_id,)))
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
|
@ -4,7 +4,9 @@ import sqlite3
|
||||||
|
|
||||||
|
|
||||||
class DatabaseConnection(ABC):
|
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_lock = Lock()
|
||||||
|
|
||||||
self.__sql_database = sqlite3.connect(db_path,
|
self.__sql_database = sqlite3.connect(db_path,
|
||||||
|
@ -120,7 +122,7 @@ class DatabaseConnection(ABC):
|
||||||
# region QUERIES
|
# region QUERIES
|
||||||
|
|
||||||
def get_all(self):
|
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
|
# endregion
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
from .databaseconnection import DatabaseConnection
|
from .databaseconnection import DatabaseConnection
|
||||||
|
from borgmanager.database.object import Error
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class ErrorConn(DatabaseConnection):
|
class ErrorConn(DatabaseConnection):
|
||||||
def __init__(self, db_path, label_table: str, table_name: str = "errors"):
|
def __init__(self, db_path, label_table: str, table_name: str = "errors"):
|
||||||
self.label_table = label_table
|
self.label_table = label_table
|
||||||
super().__init__(db_path, table_name)
|
super().__init__(db_path, Error, table_name)
|
||||||
|
|
||||||
def _create_table(self):
|
def _create_table(self):
|
||||||
create_statement = f"create table if not exists {self._sql_table}(" \
|
create_statement = f"create table if not exists {self._sql_table}(" \
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from .databaseconnection import DatabaseConnection
|
from .databaseconnection import DatabaseConnection
|
||||||
|
from borgmanager.database.object import Label
|
||||||
|
|
||||||
|
|
||||||
class LabelConn(DatabaseConnection):
|
class LabelConn(DatabaseConnection):
|
||||||
|
@ -6,7 +7,7 @@ class LabelConn(DatabaseConnection):
|
||||||
table_name: str = "label"):
|
table_name: str = "label"):
|
||||||
self.repo_table = repo_table
|
self.repo_table = repo_table
|
||||||
|
|
||||||
super().__init__(db_path, table_name)
|
super().__init__(db_path, Label, table_name)
|
||||||
|
|
||||||
# region INIT
|
# region INIT
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from .databaseconnection import DatabaseConnection
|
from .databaseconnection import DatabaseConnection
|
||||||
|
from borgmanager.database.object import Repo
|
||||||
|
|
||||||
|
|
||||||
class RepoConn(DatabaseConnection):
|
class RepoConn(DatabaseConnection):
|
||||||
def __init__(self, db_path, table_name: str = 'repo'):
|
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):
|
def _create_table(self):
|
||||||
create_statement = f"create table if not exists {self._sql_table}(" \
|
create_statement = f"create table if not exists {self._sql_table}(" \
|
||||||
|
|
|
@ -11,13 +11,12 @@ class Summary(object):
|
||||||
print(self.print_repo_stats())
|
print(self.print_repo_stats())
|
||||||
|
|
||||||
def print_repo_stats(self):
|
def print_repo_stats(self):
|
||||||
repo_sql = self.db.get_repos()
|
repos = self.db.repo_conn.get_all()
|
||||||
|
|
||||||
return_string = ""
|
return_string = ""
|
||||||
for line in repo_sql:
|
for repo in repos:
|
||||||
repo = Repo.from_sql(line)
|
latest_archive = self.db.archive_conn.get_latest(repo.primary_key)
|
||||||
latest_archive = Archive.from_sql(self.db.get_latest_archive(repo))
|
cache = self.db.get_cache(repo)
|
||||||
cache = Cache.from_sql(self.db.get_cache(repo))
|
|
||||||
repo_name = self.db.get_repo_name(repo)
|
repo_name = self.db.get_repo_name(repo)
|
||||||
if repo_name is not None:
|
if repo_name is not None:
|
||||||
return_string += f"{repo_name} ({repo.location}):\n"
|
return_string += f"{repo_name} ({repo.location}):\n"
|
||||||
|
@ -32,11 +31,10 @@ class Summary(object):
|
||||||
return return_string.strip()
|
return return_string.strip()
|
||||||
|
|
||||||
def get_backup_line(self, repo_id):
|
def get_backup_line(self, repo_id):
|
||||||
units = []
|
units = [['H' if h else '_' for h in self.get_archive_hours(repo_id, 24)],
|
||||||
units.append(['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)],
|
||||||
units.append(['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)],
|
||||||
units.append(['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)]]
|
||||||
units.append(['M' if m else '_' for m in self.get_archive_units(repo_id, 12, 30)])
|
|
||||||
|
|
||||||
return f"[{']['.join([''.join(u) for u in units])}]"
|
return f"[{']['.join([''.join(u) for u in units])}]"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user