Initial commit
This commit is contained in:
commit
6d425d0acd
10 changed files with 1637 additions and 0 deletions
161
.gitignore
vendored
Normal file
161
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
Pipfile
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Ted Conbeer
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
4
README.md
Normal file
4
README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# harlequin-adapter-template
|
||||
This repo provides a template you can use to accelerate development of a new [Harlequin](https://harlequin.sh) database adapter.
|
||||
|
||||
For an in-depth guide on writing your own adapter, see the [Harlequin Docs](https://harlequin.sh/docs/contributing/adapter-guide).
|
||||
1141
poetry.lock
generated
Normal file
1141
poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
64
pyproject.toml
Normal file
64
pyproject.toml
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
[tool.poetry]
|
||||
name = "harlequin_myadapter"
|
||||
version = "0.1.0"
|
||||
description = "A Harlequin adapter for <my favorite database>."
|
||||
authors = ["Ted Conbeer <tconbeer@users.noreply.github.com>"]
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
packages = [
|
||||
{ include = "harlequin_myadapter", from = "src" },
|
||||
]
|
||||
|
||||
[tool.poetry.plugins."harlequin.adapter"]
|
||||
my-adapter = "harlequin_myadapter:MyAdapter"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.1,<4.0"
|
||||
harlequin = "^1.7"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
ruff = "^0.1.6"
|
||||
pytest = "^7.4.3"
|
||||
mypy = "^1.7.0"
|
||||
pre-commit = "^3.5.0"
|
||||
importlib_metadata = { version = ">=4.6.0", python = "<3.10.0" }
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
|
||||
[tool.ruff]
|
||||
select = ["A", "B", "E", "F", "I"]
|
||||
target-version = "py38"
|
||||
|
||||
[tool.mypy]
|
||||
python_version = "3.8"
|
||||
files = [
|
||||
"src/**/*.py",
|
||||
"tests/**/*.py",
|
||||
]
|
||||
mypy_path = "src:stubs"
|
||||
|
||||
show_column_numbers = true
|
||||
|
||||
# show error messages from unrelated files
|
||||
follow_imports = "normal"
|
||||
|
||||
# be strict
|
||||
disallow_untyped_calls = true
|
||||
disallow_untyped_defs = true
|
||||
check_untyped_defs = true
|
||||
disallow_untyped_decorators = true
|
||||
disallow_incomplete_defs = true
|
||||
disallow_subclassing_any = true
|
||||
strict_optional = true
|
||||
|
||||
warn_return_any = true
|
||||
warn_no_return = true
|
||||
warn_redundant_casts = true
|
||||
warn_unused_ignores = true
|
||||
warn_unused_configs = true
|
||||
|
||||
no_implicit_reexport = true
|
||||
strict_equality = true
|
||||
3
src/harlequin_myadapter/__init__.py
Normal file
3
src/harlequin_myadapter/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from harlequin_myadapter.adapter import MyAdapter
|
||||
|
||||
__all__ = ["MyAdapter"]
|
||||
139
src/harlequin_myadapter/adapter.py
Normal file
139
src/harlequin_myadapter/adapter.py
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Sequence
|
||||
|
||||
from harlequin import (
|
||||
HarlequinAdapter,
|
||||
HarlequinConnection,
|
||||
HarlequinCursor,
|
||||
)
|
||||
from harlequin.autocomplete.completion import HarlequinCompletion
|
||||
from harlequin.catalog import Catalog, CatalogItem
|
||||
from harlequin.exception import HarlequinConnectionError, HarlequinQueryError
|
||||
from textual_fastdatatable.backend import AutoBackendType
|
||||
|
||||
from harlequin_myadapter.cli_options import MYADAPTER_OPTIONS
|
||||
|
||||
|
||||
class MyCursor(HarlequinCursor):
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
self.cur = args[0]
|
||||
self._limit: int | None = None
|
||||
|
||||
def columns(self) -> list[tuple[str, str]]:
|
||||
names = self.cur.column_names
|
||||
types = self.cur.column_types
|
||||
return list(zip(names, types))
|
||||
|
||||
def set_limit(self, limit: int) -> MyCursor:
|
||||
self._limit = limit
|
||||
return self
|
||||
|
||||
def fetchall(self) -> AutoBackendType:
|
||||
try:
|
||||
if self._limit is None:
|
||||
return self.cur.fetchall()
|
||||
else:
|
||||
return self.cur.fetchmany(self._limit)
|
||||
except Exception as e:
|
||||
raise HarlequinQueryError(
|
||||
msg=str(e),
|
||||
title="Harlequin encountered an error while executing your query.",
|
||||
) from e
|
||||
|
||||
|
||||
class MyConnection(HarlequinConnection):
|
||||
def __init__(
|
||||
self, conn_str: Sequence[str], *args: Any, init_message: str = "", **kwargs: Any
|
||||
) -> None:
|
||||
self.init_message = init_message
|
||||
try:
|
||||
self.conn = "your database library's connect method goes here"
|
||||
except Exception as e:
|
||||
raise HarlequinConnectionError(
|
||||
msg=str(e), title="Harlequin could not connect to your database."
|
||||
) from e
|
||||
|
||||
def execute(self, query: str) -> HarlequinCursor | None:
|
||||
try:
|
||||
cur = self.conn.execute(query) # type: ignore
|
||||
except Exception as e:
|
||||
raise HarlequinQueryError(
|
||||
msg=str(e),
|
||||
title="Harlequin encountered an error while executing your query.",
|
||||
) from e
|
||||
else:
|
||||
if cur is not None:
|
||||
return MyCursor(cur)
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_catalog(self) -> Catalog:
|
||||
databases = self.conn.list_databases()
|
||||
db_items: list[CatalogItem] = []
|
||||
for db in databases:
|
||||
schemas = self.conn.list_schemas_in_db(db)
|
||||
schema_items: list[CatalogItem] = []
|
||||
for schema in schemas:
|
||||
relations = self.conn.list_relations_in_schema(schema)
|
||||
rel_items: list[CatalogItem] = []
|
||||
for rel, rel_type in relations:
|
||||
cols = self.conn.list_columns_in_relation(rel)
|
||||
col_items = [
|
||||
CatalogItem(
|
||||
qualified_identifier=f'"{db}"."{schema}"."{rel}"."{col}"',
|
||||
query_name=f'"{col}"',
|
||||
label=col,
|
||||
type_label=col_type,
|
||||
)
|
||||
for col, col_type in cols
|
||||
]
|
||||
rel_items.append(
|
||||
CatalogItem(
|
||||
qualified_identifier=f'"{db}"."{schema}"."{rel}"',
|
||||
query_name=f'"{db}"."{schema}"."{rel}"',
|
||||
label=rel,
|
||||
type_label=rel_type,
|
||||
children=col_items,
|
||||
)
|
||||
)
|
||||
schema_items.append(
|
||||
CatalogItem(
|
||||
qualified_identifier=f'"{db}"."{schema}"',
|
||||
query_name=f'"{db}"."{schema}"',
|
||||
label=schema,
|
||||
type_label="s",
|
||||
children=rel_items,
|
||||
)
|
||||
)
|
||||
db_items.append(
|
||||
CatalogItem(
|
||||
qualified_identifier=f'"{db}"',
|
||||
query_name=f'"{db}"',
|
||||
label=db,
|
||||
type_label="db",
|
||||
children=schema_items,
|
||||
)
|
||||
)
|
||||
return Catalog(items=db_items)
|
||||
|
||||
def get_completions(self) -> list[HarlequinCompletion]:
|
||||
extra_keywords = ["foo", "bar", "baz"]
|
||||
return [
|
||||
HarlequinCompletion(
|
||||
label=item, type_label="kw", value=item, priority=1000, context=None
|
||||
)
|
||||
for item in extra_keywords
|
||||
]
|
||||
|
||||
|
||||
class MyAdapter(HarlequinAdapter):
|
||||
ADAPTER_OPTIONS = MYADAPTER_OPTIONS
|
||||
|
||||
def __init__(self, conn_str: Sequence[str], **options: Any) -> None:
|
||||
self.conn_str = conn_str
|
||||
self.options = options
|
||||
|
||||
def connect(self) -> MyConnection:
|
||||
conn = MyConnection(self.conn_str, self.options)
|
||||
return conn
|
||||
15
src/harlequin_myadapter/cli_options.py
Normal file
15
src/harlequin_myadapter/cli_options.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from harlequin.options import (
|
||||
FlagOption, # noqa
|
||||
ListOption, # noqa
|
||||
PathOption, # noqa
|
||||
SelectOption, # noqa
|
||||
TextOption,
|
||||
)
|
||||
|
||||
foo = TextOption(
|
||||
name="foo",
|
||||
description="Help text goes here",
|
||||
short_decls=["-f"],
|
||||
)
|
||||
|
||||
MYADAPTER_OPTIONS = [foo]
|
||||
0
src/harlequin_myadapter/py.typed
Normal file
0
src/harlequin_myadapter/py.typed
Normal file
89
tests/test_adapter.py
Normal file
89
tests/test_adapter.py
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import sys
|
||||
|
||||
import pytest
|
||||
from harlequin.adapter import HarlequinAdapter, HarlequinConnection, HarlequinCursor
|
||||
from harlequin.catalog import Catalog, CatalogItem
|
||||
from harlequin.exception import HarlequinConnectionError, HarlequinQueryError
|
||||
from harlequin_myadapter.adapter import MyAdapter, MyConnection
|
||||
from textual_fastdatatable.backend import create_backend
|
||||
|
||||
if sys.version_info < (3, 10):
|
||||
from importlib_metadata import entry_points
|
||||
else:
|
||||
from importlib.metadata import entry_points
|
||||
|
||||
|
||||
def test_plugin_discovery() -> None:
|
||||
PLUGIN_NAME = "my-adapter"
|
||||
eps = entry_points(group="harlequin.adapter")
|
||||
assert eps[PLUGIN_NAME]
|
||||
adapter_cls = eps[PLUGIN_NAME].load()
|
||||
assert issubclass(adapter_cls, HarlequinAdapter)
|
||||
assert adapter_cls == MyAdapter
|
||||
|
||||
|
||||
def test_connect() -> None:
|
||||
conn = MyAdapter(conn_str=tuple()).connect()
|
||||
assert isinstance(conn, HarlequinConnection)
|
||||
|
||||
|
||||
def test_init_extra_kwargs() -> None:
|
||||
assert MyAdapter(conn_str=tuple(), foo=1, bar="baz").connect()
|
||||
|
||||
|
||||
def test_connect_raises_connection_error() -> None:
|
||||
with pytest.raises(HarlequinConnectionError):
|
||||
_ = MyAdapter(conn_str=("foo",)).connect()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def connection() -> MyConnection:
|
||||
return MyAdapter(conn_str=tuple()).connect()
|
||||
|
||||
|
||||
def test_get_catalog(connection: MyConnection) -> None:
|
||||
catalog = connection.get_catalog()
|
||||
assert isinstance(catalog, Catalog)
|
||||
assert catalog.items
|
||||
assert isinstance(catalog.items[0], CatalogItem)
|
||||
|
||||
|
||||
def test_execute_ddl(connection: MyConnection) -> None:
|
||||
cur = connection.execute("create table foo (a int)")
|
||||
assert cur is None
|
||||
|
||||
|
||||
def test_execute_select(connection: MyConnection) -> None:
|
||||
cur = connection.execute("select 1 as a")
|
||||
assert isinstance(cur, HarlequinCursor)
|
||||
assert cur.columns() == [("a", "##")]
|
||||
data = cur.fetchall()
|
||||
backend = create_backend(data)
|
||||
assert backend.column_count == 1
|
||||
assert backend.row_count == 1
|
||||
|
||||
|
||||
def test_execute_select_dupe_cols(connection: MyConnection) -> None:
|
||||
cur = connection.execute("select 1 as a, 2 as a, 3 as a")
|
||||
assert isinstance(cur, HarlequinCursor)
|
||||
assert len(cur.columns()) == 3
|
||||
data = cur.fetchall()
|
||||
backend = create_backend(data)
|
||||
assert backend.column_count == 3
|
||||
assert backend.row_count == 1
|
||||
|
||||
|
||||
def test_set_limit(connection: MyConnection) -> None:
|
||||
cur = connection.execute("select 1 as a union all select 2 union all select 3")
|
||||
assert isinstance(cur, HarlequinCursor)
|
||||
cur = cur.set_limit(2)
|
||||
assert isinstance(cur, HarlequinCursor)
|
||||
data = cur.fetchall()
|
||||
backend = create_backend(data)
|
||||
assert backend.column_count == 1
|
||||
assert backend.row_count == 2
|
||||
|
||||
|
||||
def test_execute_raises_query_error(connection: MyConnection) -> None:
|
||||
with pytest.raises(HarlequinQueryError):
|
||||
_ = connection.execute("selec;")
|
||||
Loading…
Add table
Add a link
Reference in a new issue