Skip to content

Ability to get view names #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ydb_dbapi as dbapi


def main() -> None:
connection = dbapi.connect(
host="localhost",
port=2136,
database="/local",
)

print(f"Existing tables: {connection.get_table_names()}")
print(f"Existing views: {connection.get_view_names()}")
# TODO: fill example

connection.close()


if __name__ == "__main__":
main()
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/ydb-platform/ydb-python-dbapi/"

[tool.poetry.dependencies]
python = "^3.8"
ydb = "^3.18.13"
ydb = "^3.18.16"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.5.0"
Expand Down Expand Up @@ -78,6 +78,7 @@ force-single-line = true

[tool.ruff.lint.per-file-ignores]
"**/test_*.py" = ["S", "SLF", "ANN201", "ARG", "PLR2004", "PT012"]
"examples/*.py" = ["T201", "INP001"]
"conftest.py" = ["S", "ARG001"]
"__init__.py" = ["F401", "F403"]

Expand Down
46 changes: 46 additions & 0 deletions tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,41 @@ def _test_error_with_interactive_tx(
maybe_await(cur.close())
maybe_await(connection.rollback())

def _test_get_view_names(
self,
connection: dbapi.Connection,
) -> None:
cur = connection.cursor()

maybe_await(
cur.execute_scheme(
"""
DROP VIEW if exists test_view;
"""
)
)

res = maybe_await(connection.get_view_names())

assert len(res) == 0

maybe_await(
cur.execute_scheme(
"""
CREATE VIEW test_view WITH (security_invoker = TRUE) as (
select 1 as res
);
"""
)
)

res = maybe_await(connection.get_view_names())

assert len(res) == 1
assert res[0] == "test_view"

maybe_await(cur.close())


class TestConnection(BaseDBApiTestSuit):
@pytest.fixture
Expand Down Expand Up @@ -289,6 +324,11 @@ def test_errors_with_interactive_tx(
) -> None:
self._test_error_with_interactive_tx(connection)

def test_get_view_names(
self, connection: dbapi.Connection
) -> None:
self._test_get_view_names(connection)


class TestAsyncConnection(BaseDBApiTestSuit):
@pytest_asyncio.fixture
Expand Down Expand Up @@ -358,3 +398,9 @@ async def test_errors_with_interactive_tx(
self, connection: dbapi.AsyncConnection
) -> None:
await greenlet_spawn(self._test_error_with_interactive_tx, connection)

@pytest.mark.asyncio
async def test_get_view_names(
self, connection: dbapi.AsyncConnection
) -> None:
await greenlet_spawn(self._test_get_view_names, connection)
40 changes: 32 additions & 8 deletions ydb_dbapi/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ def check_exists(self, table_path: str) -> bool:
@handle_ydb_errors
def get_table_names(self) -> list[str]:
abs_dir_path = posixpath.join(self.database, self.table_path_prefix)
names = self._get_table_names(abs_dir_path)
names = self._get_entity_names(abs_dir_path, ydb.SchemeEntryType.TABLE)
return [posixpath.relpath(path, abs_dir_path) for path in names]

@handle_ydb_errors
def get_view_names(self) -> list[str]:
abs_dir_path = posixpath.join(self.database, self.table_path_prefix)
names = self._get_entity_names(abs_dir_path, ydb.SchemeEntryType.VIEW)
return [posixpath.relpath(path, abs_dir_path) for path in names]

def _check_path_exists(self, table_path: str) -> bool:
Expand All @@ -295,7 +301,9 @@ def callee() -> None:
else:
return True

def _get_table_names(self, abs_dir_path: str) -> list[str]:
def _get_entity_names(
self, abs_dir_path: str, etype: ydb.SchemeEntryType
) -> list[str]:
settings = self._get_request_settings()

def callee() -> ydb.Directory:
Expand All @@ -308,10 +316,10 @@ def callee() -> ydb.Directory:
result = []
for child in directory.children:
child_abs_path = posixpath.join(abs_dir_path, child.name)
if child.is_table():
if child.type == etype:
result.append(child_abs_path)
elif child.is_directory() and not child.name.startswith("."):
result.extend(self._get_table_names(child_abs_path))
result.extend(self._get_entity_names(child_abs_path, etype))
return result

@handle_ydb_errors
Expand Down Expand Up @@ -452,7 +460,19 @@ async def check_exists(self, table_path: str) -> bool:
@handle_ydb_errors
async def get_table_names(self) -> list[str]:
abs_dir_path = posixpath.join(self.database, self.table_path_prefix)
names = await self._get_table_names(abs_dir_path)
names = await self._get_entity_names(
abs_dir_path,
ydb.SchemeEntryType.TABLE,
)
return [posixpath.relpath(path, abs_dir_path) for path in names]

@handle_ydb_errors
async def get_view_names(self) -> list[str]:
abs_dir_path = posixpath.join(self.database, self.table_path_prefix)
names = await self._get_entity_names(
abs_dir_path,
ydb.SchemeEntryType.VIEW,
)
return [posixpath.relpath(path, abs_dir_path) for path in names]

async def _check_path_exists(self, table_path: str) -> bool:
Expand All @@ -471,7 +491,9 @@ async def callee() -> None:
else:
return True

async def _get_table_names(self, abs_dir_path: str) -> list[str]:
async def _get_entity_names(
self, abs_dir_path: str, etype: ydb.SchemeEntryType
) -> list[str]:
settings = self._get_request_settings()

async def callee() -> ydb.Directory:
Expand All @@ -484,10 +506,12 @@ async def callee() -> ydb.Directory:
result = []
for child in directory.children:
child_abs_path = posixpath.join(abs_dir_path, child.name)
if child.is_table():
if child.type == etype:
result.append(child_abs_path)
elif child.is_directory() and not child.name.startswith("."):
result.extend(await self._get_table_names(child_abs_path))
result.extend(
await self._get_entity_names(child_abs_path, etype)
)
return result

@handle_ydb_errors
Expand Down