Get detailed information about a table including columns, constraints, and indexes. Uses pgAdmin's SQL templates for version-aware queries. Args: sid: Server ID did: Database ID schema_name: Schema name table_name: Table name Returns: D
(
sid: int,
did: int,
schema_name: str,
table_name: str
)
| 627 | |
| 628 | |
| 629 | def get_table_info( |
| 630 | sid: int, |
| 631 | did: int, |
| 632 | schema_name: str, |
| 633 | table_name: str |
| 634 | ) -> dict: |
| 635 | """ |
| 636 | Get detailed information about a table including columns, |
| 637 | constraints, and indexes. |
| 638 | |
| 639 | Uses pgAdmin's SQL templates for version-aware queries. |
| 640 | |
| 641 | Args: |
| 642 | sid: Server ID |
| 643 | did: Database ID |
| 644 | schema_name: Schema name |
| 645 | table_name: Table name |
| 646 | |
| 647 | Returns: |
| 648 | Dictionary containing comprehensive table information |
| 649 | """ |
| 650 | conn_id = f"llm_{secrets.choice(range(1, 9999999))}" |
| 651 | manager = None |
| 652 | |
| 653 | try: |
| 654 | manager, conn = _get_connection(sid, did, conn_id) |
| 655 | status, error = _connect_readonly(manager, conn, conn_id) |
| 656 | if not status: |
| 657 | raise DatabaseToolError(f"Connection failed: {error}", |
| 658 | code="CONNECTION_ERROR") |
| 659 | |
| 660 | sversion = manager.sversion or 0 |
| 661 | driver = get_driver(config.PG_DEFAULT_DRIVER) |
| 662 | |
| 663 | # Use qtLiteral for safe SQL escaping |
| 664 | schema_lit = driver.qtLiteral(schema_name, conn) |
| 665 | table_lit = driver.qtLiteral(table_name, conn) |
| 666 | |
| 667 | # Get table OID first |
| 668 | oid_sql = f""" |
| 669 | SELECT c.oid, n.oid as schema_oid |
| 670 | FROM pg_catalog.pg_class c |
| 671 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
| 672 | WHERE c.relname = {table_lit} |
| 673 | AND n.nspname = {schema_lit} |
| 674 | """ |
| 675 | |
| 676 | status, _ = conn.execute_void("BEGIN TRANSACTION READ ONLY") |
| 677 | if not status: |
| 678 | raise DatabaseToolError("Failed to start transaction", |
| 679 | code="TRANSACTION_ERROR") |
| 680 | |
| 681 | try: |
| 682 | status, oid_res = conn.execute_dict(oid_sql) |
| 683 | if not status or not oid_res.get('rows'): |
| 684 | raise DatabaseToolError( |
| 685 | f"Table {schema_name}.{table_name} not found", |
| 686 | code="NOT_FOUND" |
no test coverage detected