2022-11-30 00:09:23 -08:00
|
|
|
from prompt_toolkit.completion import Completion, FuzzyCompleter
|
2022-11-25 15:40:24 -08:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
2022-12-21 15:17:13 -08:00
|
|
|
import groove.path
|
|
|
|
|
2022-11-25 15:40:24 -08:00
|
|
|
from . import metadata
|
|
|
|
|
|
|
|
|
2022-11-30 00:09:23 -08:00
|
|
|
class FuzzyTableCompleter(FuzzyCompleter):
|
|
|
|
|
|
|
|
def __init__(self, table, column, formatter, session):
|
|
|
|
self._table = table
|
|
|
|
self._column = column
|
|
|
|
self._formatter = formatter
|
|
|
|
self._session = session
|
|
|
|
|
|
|
|
def get_completions(self, document, complete_event):
|
|
|
|
line = document.current_line_before_cursor
|
|
|
|
query = self._session.query(self._table).filter(self._column.ilike(f"%{line}%"))
|
|
|
|
for row in query.all():
|
|
|
|
yield Completion(
|
|
|
|
self._formatter(row),
|
|
|
|
start_position=-len(line)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-25 15:40:24 -08:00
|
|
|
class DatabaseManager:
|
|
|
|
"""
|
|
|
|
A context manager for working with sqllite database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._engine = None
|
|
|
|
self._session = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def engine(self):
|
|
|
|
if not self._engine:
|
2022-12-21 15:17:13 -08:00
|
|
|
path = groove.path.database()
|
|
|
|
self._engine = create_engine(f"sqlite:///{path}?check_same_thread=False", future=True)
|
2022-11-25 15:40:24 -08:00
|
|
|
return self._engine
|
|
|
|
|
|
|
|
@property
|
|
|
|
def session(self):
|
|
|
|
if not self._session:
|
|
|
|
Session = sessionmaker(bind=self.engine, future=True)
|
|
|
|
self._session = Session()
|
|
|
|
return self._session
|
|
|
|
|
|
|
|
def import_from_filesystem(self):
|
|
|
|
pass
|
|
|
|
|
2022-11-30 00:09:23 -08:00
|
|
|
def fuzzy_table_completer(self, table, column, formatter):
|
|
|
|
return FuzzyTableCompleter(table, column, formatter, session=self.session)
|
|
|
|
|
2022-11-25 15:40:24 -08:00
|
|
|
def __enter__(self):
|
|
|
|
metadata.create_all(bind=self.engine)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
if self.session:
|
|
|
|
self.session.close()
|
|
|
|
|
|
|
|
|
|
|
|
database_manager = DatabaseManager
|