From f8636c62b9526cce35c5bc566921de5078523544 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 18 Jun 2022 16:45:03 -0500 Subject: [PATCH] Add table construction, adding commits, and commit checking queries. Modify Commit model properties & table. --- database.py | 33 ++++++++++++++++++++++++++++++++- models.py | 11 +++++------ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/database.py b/database.py index 09fbc88..de6818b 100644 --- a/database.py +++ b/database.py @@ -6,6 +6,8 @@ import logging import sqlite3 from typing import Optional +from models import Commit + logger = logging.getLogger(__name__) @@ -19,6 +21,19 @@ class Database: self.__is_closed: bool = True # Becomes false after this statement. self.conn: Optional[sqlite3.Connection] = None 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: """Opens a connection to the database file.""" @@ -37,6 +52,22 @@ class Database: self.conn = None 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.""" + + 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 diff --git a/models.py b/models.py index d68da5f..7bd4814 100644 --- a/models.py +++ b/models.py @@ -5,13 +5,12 @@ from datetime import datetime @dataclass class Commit: """Describes a specific commit event in time.""" - - # A unique identifier for this commit. Not necessarily the commit hash. - id: int - # A name, description etc. for this commit. Not required to be based on the original comit message. - name: str - # A timestamp + id: str + project_id: int + source: str + iteration: int timestamp: datetime + seen_timestamp: datetime def __str__(self) -> str: return '{name}({params})'.format(