feature: establish sqlite and tables
This commit is contained in:
parent
ac75e1f4c6
commit
a954aafbcc
7 changed files with 113 additions and 6 deletions
12
src/cli.py
12
src/cli.py
|
@ -2,9 +2,9 @@ import argparse
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
from controllers.controller import Controller
|
from controllers.controller import Controller
|
||||||
|
from services.database_service import DatabaseService
|
||||||
|
|
||||||
importlib.invalidate_caches()
|
importlib.invalidate_caches()
|
||||||
|
|
||||||
file_path = "/home/thomas/repos/eolas-db/dev-data/Turing_completeness.md"
|
file_path = "/home/thomas/repos/eolas-db/dev-data/Turing_completeness.md"
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,15 +12,21 @@ def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="eolas-db", description="Eolas database manager."
|
prog="eolas-db", description="Eolas database manager."
|
||||||
)
|
)
|
||||||
parser.add_argument("command", choices=["parse"], help="Command to execute")
|
parser.add_argument(
|
||||||
|
"command", choices=["parse", "populate"], help="Command to execute"
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
controller = Controller()
|
database_service = DatabaseService("eolas")
|
||||||
|
controller = Controller(database_service)
|
||||||
|
|
||||||
if args.command == "parse":
|
if args.command == "parse":
|
||||||
parsed_entry = controller.parse_entry(file_path)
|
parsed_entry = controller.parse_entry(file_path)
|
||||||
print(parsed_entry)
|
print(parsed_entry)
|
||||||
|
|
||||||
|
if args.command == "populate":
|
||||||
|
controller.populate_database()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
from services.parse_file_service import ParseFileService
|
from services.parse_file_service import ParseFileService
|
||||||
|
from services.sqlite_service import SqliteService
|
||||||
|
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
def __init__(self):
|
def __init__(self, database_service):
|
||||||
pass
|
self.database_service = database_service
|
||||||
|
|
||||||
def parse_entry(self, file_path):
|
def parse_entry(self, file_path):
|
||||||
parse_file_service = ParseFileService(file_path)
|
parse_file_service = ParseFileService(file_path)
|
||||||
return parse_file_service.parse()
|
return parse_file_service.parse()
|
||||||
|
|
||||||
|
def populate_database(self):
|
||||||
|
connection = self.database_service.connect()
|
||||||
|
|
||||||
|
if connection is None:
|
||||||
|
raise Exception("Failed to establish database connection")
|
||||||
|
|
||||||
|
sqlite_service = SqliteService(connection)
|
||||||
|
sqlite_service.create_tables()
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
29
src/services/database_service.py
Normal file
29
src/services/database_service.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import sqlite3
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseService:
|
||||||
|
def __init__(self, db_name):
|
||||||
|
self.db_name = db_name
|
||||||
|
self.db_path = "/home/thomas/repos/eolas-db/db"
|
||||||
|
self.connection: Optional[sqlite3.Connection] = None
|
||||||
|
|
||||||
|
def connect(self) -> Optional[sqlite3.Connection]:
|
||||||
|
if self.connection is not None:
|
||||||
|
return self.connection
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.connection = sqlite3.connect(f"{self.db_path}/{self.db_name}.db")
|
||||||
|
print("INFO Database connection established")
|
||||||
|
return self.connection
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"Problem connecting to database: {e}")
|
||||||
|
|
||||||
|
def disconnect(self) -> None:
|
||||||
|
try:
|
||||||
|
if self.connection is not None:
|
||||||
|
self.connection.close()
|
||||||
|
self.connection = None
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"ERROR Problem disconnecting from database: {e}")
|
27
src/services/sqlite_service.py
Normal file
27
src/services/sqlite_service.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
from sql.create_tables import (
|
||||||
|
CREATE_BACKLINKS_TABLE,
|
||||||
|
CREATE_ENTRIES_TABLE,
|
||||||
|
CREATE_ENTRIES_TAGS_TABLE,
|
||||||
|
CREATE_TAGS_TABLE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SqliteService:
|
||||||
|
def __init__(self, connection):
|
||||||
|
self.connection = connection
|
||||||
|
self.cursor = connection.cursor()
|
||||||
|
|
||||||
|
def create_tables(self):
|
||||||
|
tables = [
|
||||||
|
CREATE_ENTRIES_TABLE,
|
||||||
|
CREATE_TAGS_TABLE,
|
||||||
|
CREATE_BACKLINKS_TABLE,
|
||||||
|
CREATE_ENTRIES_TAGS_TABLE,
|
||||||
|
]
|
||||||
|
for create_statement in tables:
|
||||||
|
self.cursor.execute(create_statement)
|
||||||
|
|
||||||
|
self.connection.commit()
|
||||||
|
|
||||||
|
self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
||||||
|
print(self.cursor.fetchall())
|
33
src/sql/create_tables.py
Normal file
33
src/sql/create_tables.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
CREATE_ENTRIES_TABLE = """
|
||||||
|
CREATE TABLE IF NOT EXISTS entries (
|
||||||
|
title TEXT PRIMARY KEY UNIQUE,
|
||||||
|
last_modified STRING,
|
||||||
|
size INTEGER,
|
||||||
|
body TEXT
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
CREATE_TAGS_TABLE = """
|
||||||
|
CREATE TABLE IF NOT EXISTS tags (
|
||||||
|
name TEXT PRIMARY KEY UNIQUE,
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
CREATE_BACKLINKS_TABLE = """
|
||||||
|
CREATE TABLE IF NOT EXISTS backlinks (
|
||||||
|
source_entry_title TEXT,
|
||||||
|
target_entry_title TEXT,
|
||||||
|
FOREIGN KEY (source_entry_title) REFERENCES entries(title),
|
||||||
|
FOREIGN KEY (target_entry_title) REFERENCES entries(title),
|
||||||
|
PRIMARY KEY (source_entry_title, target_entry_title)
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
CREATE_ENTRIES_TAGS_TABLE = """
|
||||||
|
CREATE TABLE IF NOT EXISTS entries_tags (
|
||||||
|
entry_title TEXT,
|
||||||
|
tag_name TEXT,
|
||||||
|
FOREIGN KEY (entry_title) REFERENCES entries(title),
|
||||||
|
FOREIGN KEY (tag_name) REFERENCES tags(name),
|
||||||
|
PRIMARY KEY (entry_title, tag_name)
|
||||||
|
)"""
|
3
src/sql/table_exists.py
Normal file
3
src/sql/table_exists.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
TABLE_EXISTS = (
|
||||||
|
"SELECT count(*) FROM sqlite_master WHERE type ='table' AND name=:table_name"
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue