Implement methods for repo stat retrieval

This commit is contained in:
George Lacey 2021-05-05 05:44:44 +01:00
parent 93fa81777a
commit 693f7c828b
3 changed files with 40 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn
from .object import Archive
from .object.label import Label from .object.label import Label
from pathlib import Path from pathlib import Path
@ -46,4 +47,11 @@ class BorgDatabase(object):
def get_repos(self): def get_repos(self):
return self.repo_conn.get_all() return self.repo_conn.get_all()
def get_cache(self, repo):
archive = Archive.from_sql(self.get_latest_archive(repo))
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

View File

@ -7,6 +7,8 @@ class ArchiveConn(DatabaseConnection):
self.repo_table = repo_table self.repo_table = repo_table
super().__init__(db_path, table_name) super().__init__(db_path, table_name)
# region INIT
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}(" \
f"id INTEGER PRIMARY KEY," \ f"id INTEGER PRIMARY KEY," \
@ -23,6 +25,10 @@ class ArchiveConn(DatabaseConnection):
f" {self.repo_table} (id));" f" {self.repo_table} (id));"
self.sql_execute(create_statement) self.sql_execute(create_statement)
# endregion
# region INSERT
def _exists(self, record, repo_id=None, archive_id=None, label_id=None): def _exists(self, record, repo_id=None, archive_id=None, label_id=None):
return f"SELECT id FROM {self._sql_table}" \ return f"SELECT id FROM {self._sql_table}" \
f" WHERE fingerprint=?;", (record.fingerprint,) f" WHERE fingerprint=?;", (record.fingerprint,)
@ -42,3 +48,13 @@ class ArchiveConn(DatabaseConnection):
cursor.execute(statement, args) cursor.execute(statement, args)
self.sql_commit() self.sql_commit()
return cursor.lastrowid return cursor.lastrowid
# endregion
# 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,))
# endregion

View File

@ -6,6 +6,8 @@ class CacheConn(DatabaseConnection):
self.archive_table = archive_table self.archive_table = archive_table
super().__init__(db_path, table_name) super().__init__(db_path, table_name)
# region INIT
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}(" \
f"id INTEGER PRIMARY KEY," \ f"id INTEGER PRIMARY KEY," \
@ -20,6 +22,10 @@ class CacheConn(DatabaseConnection):
f" {self.archive_table} (id));" f" {self.archive_table} (id));"
self.sql_execute(create_statement) self.sql_execute(create_statement)
# endregion
# region INSERT
def _exists(self, record, repo_id=None, archive_id=None, label_id=None): def _exists(self, record, repo_id=None, archive_id=None, label_id=None):
return None, None return None, None
@ -38,3 +44,13 @@ class CacheConn(DatabaseConnection):
cursor.execute(statement, args) cursor.execute(statement, args)
self.sql_commit() self.sql_commit()
return cursor.lastrowid return cursor.lastrowid
# endregion
# 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,))
# endregion