diff --git a/corrlib/find.py b/corrlib/find.py index 1e6b4bf..7b07321 100644 --- a/corrlib/find.py +++ b/corrlib/find.py @@ -7,7 +7,6 @@ from .input.implementations import codes from .tools import k2m, get_db_file from .tracker import get from .integrity import has_valid_times -from .sql import thin_sql_wrapper from typing import Any, Optional from pathlib import Path import datetime as dt @@ -15,7 +14,7 @@ from collections.abc import Callable import warnings -def _project_lookup_by_alias(path: Path, alias: str) -> str: +def _project_lookup_by_alias(db: Path, alias: str) -> str: """ Lookup a projects UUID by its (human-readable) alias. @@ -31,8 +30,11 @@ def _project_lookup_by_alias(path: Path, alias: str) -> str: uuid: str The UUID of the project with the given alias. """ - stmt = f"SELECT * FROM 'projects' WHERE aliases = '{alias}'" - results = thin_sql_wrapper(path, stmt) + conn = sqlite3.connect(db) + c = conn.cursor() + c.execute(f"SELECT * FROM 'projects' WHERE aliases = '{alias}'") + results = c.fetchall() + conn.close() if len(results)>1: print("Error: multiple projects found with alias " + alias) elif len(results) == 0: @@ -40,7 +42,7 @@ def _project_lookup_by_alias(path: Path, alias: str) -> str: return str(results[0][0]) -def _project_lookup_by_id(path: Path, uuid: str) -> list[tuple[str, ...]]: +def _project_lookup_by_id(db: Path, uuid: str) -> list[tuple[str, ...]]: """ Return the project information available in the database by UUID. @@ -56,8 +58,11 @@ def _project_lookup_by_id(path: Path, uuid: str) -> list[tuple[str, ...]]: results: list The row of the project in the database. """ - stmt = f"SELECT * FROM 'projects' WHERE id = '{uuid}'" - results = thin_sql_wrapper(path, stmt) + conn = sqlite3.connect(db) + c = conn.cursor() + c.execute(f"SELECT * FROM 'projects' WHERE id = '{uuid}'") + results = c.fetchall() + conn.close() return results @@ -355,7 +360,7 @@ def find_project(path: Path, name: str) -> str: """ db_file = get_db_file(path) get(path, db_file) - return _project_lookup_by_alias(path, name) + return _project_lookup_by_alias(path / db_file, name) def list_projects(path: Path) -> list[tuple[str, str]]: diff --git a/corrlib/sql.py b/corrlib/sql.py deleted file mode 100644 index f45ce31..0000000 --- a/corrlib/sql.py +++ /dev/null @@ -1,17 +0,0 @@ -import sqlite3 -from .tools import get_db_file -from pathlib import Path -from typing import Any - - -def thin_sql_wrapper(path: Path, stmt: str) -> list[Any]: - db_file = get_db_file(path) - db = path / db_file - conn = sqlite3.connect(db) - c = conn.cursor() - - c.execute(stmt) - results = c.fetchall() - conn.commit() - conn.close() - return results diff --git a/tests/find_test.py b/tests/find_test.py index 2144001..cc455f9 100644 --- a/tests/find_test.py +++ b/tests/find_test.py @@ -9,17 +9,12 @@ import datetime as dt def make_sql(path: Path) -> Path: - db = path / "backlogger.db" + db = path / "test.db" cinit._create_db(db) return db -def make_config(path: Path) -> None: - cinit._write_config(path, cinit._create_config(path, "datalad", False)) - - def test_find_lookup_by_one_alias(tmp_path: Path) -> None: - make_config(tmp_path) db = make_sql(tmp_path) conn = sqlite3.connect(db) c = conn.cursor() @@ -31,7 +26,7 @@ def test_find_lookup_by_one_alias(tmp_path: Path) -> None: c.execute("INSERT INTO projects (id, aliases, customTags, owner, code, created_at, updated_at) VALUES (?, ?, ?, ?, ?, datetime('now'), datetime('now'))", (uuid, alias_str, tag_str, owner, code)) conn.commit() - assert uuid == find._project_lookup_by_alias(tmp_path, "fun_project") + assert uuid == find._project_lookup_by_alias(db, "fun_project") uuid = "test_uuid2" alias_str = "fun_project" c.execute("INSERT INTO projects (id, aliases, customTags, owner, code, created_at, updated_at) VALUES (?, ?, ?, ?, ?, datetime('now'), datetime('now'))", @@ -41,9 +36,7 @@ def test_find_lookup_by_one_alias(tmp_path: Path) -> None: assert uuid == find._project_lookup_by_alias(db, "fun_project") conn.close() - def test_find_lookup_by_id(tmp_path: Path) -> None: - make_config(tmp_path) db = make_sql(tmp_path) conn = sqlite3.connect(db) c = conn.cursor() @@ -56,7 +49,7 @@ def test_find_lookup_by_id(tmp_path: Path) -> None: (uuid, alias_str, tag_str, owner, code)) conn.commit() conn.close() - result = find._project_lookup_by_id(tmp_path, uuid)[0] + result = find._project_lookup_by_id(db, uuid)[0] assert uuid == result[0] assert alias_str == result[1] assert tag_str == result[2]