Add table construction, adding commits, and commit checking queries. Modify Commit model properties & table.

This commit is contained in:
Xevion
2022-06-18 16:45:03 -05:00
parent 052fe84a7f
commit f8636c62b9
2 changed files with 37 additions and 7 deletions

View File

@@ -6,6 +6,8 @@ import logging
import sqlite3 import sqlite3
from typing import Optional from typing import Optional
from models import Commit
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -19,6 +21,19 @@ class Database:
self.__is_closed: bool = True # Becomes false after this statement. self.__is_closed: bool = True # Becomes false after this statement.
self.conn: Optional[sqlite3.Connection] = None self.conn: Optional[sqlite3.Connection] = None
self.open() self.open()
self.construct()
def construct(self) -> None:
"""Automatically initializes the database with the required table(s)."""
cur = self.conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS commits (
Id text primary key not null,
Source text not null,
CommitTimestamp text not null,
SeenTimestamp text not null,
Iteration integer not null,
ProjectId integer not null);""")
def open(self) -> None: def open(self) -> None:
"""Opens a connection to the database file.""" """Opens a connection to the database file."""
@@ -37,6 +52,22 @@ class Database:
self.conn = None self.conn = None
self.__is_closed = True self.__is_closed = True
def check_exists(self, id: str) -> bool: def add_commit(self, commit: Commit) -> None:
"""Inserts a commit into the database"""
cur = self.conn.cursor()
cur.execute("""INSERT INTO commits (Id, Source, ProjectId, Iteration, CommitTimestamp, SeenTimestamp) VALUES (?, ?, ?, ?, ?, ?)""",
(commit.id, commit.source, commit.project_id, commit.iteration,
commit.timestamp.isoformat(), commit.seen_timestamp.isoformat()))
def check_exists(self, id: str, source: Optional[str] = None) -> bool:
"""Returns true if the commit in question exists.""" """Returns true if the commit in question exists."""
cur = self.conn.cursor()
if source is None:
results = list(cur.execute("""SELECT Id FROM commits WHERE Id = ? LIMIT 1;""", (id,)))
else:
results = list(cur.execute("""SELECT Id FROM commits WHERE Id = ? AND Source = ?""", (id, source)))
if len(results) == 1: return True
return False return False

View File

@@ -5,13 +5,12 @@ from datetime import datetime
@dataclass @dataclass
class Commit: class Commit:
"""Describes a specific commit event in time.""" """Describes a specific commit event in time."""
id: str
# A unique identifier for this commit. Not necessarily the commit hash. project_id: int
id: int source: str
# A name, description etc. for this commit. Not required to be based on the original comit message. iteration: int
name: str
# A timestamp
timestamp: datetime timestamp: datetime
seen_timestamp: datetime
def __str__(self) -> str: def __str__(self) -> str:
return '{name}({params})'.format( return '{name}({params})'.format(