🔍 Code Extractor

class TableInfo

Maturity: 48

A dataclass that encapsulates comprehensive metadata about a database table, including schema information, columns, keys, and data quality metrics.

File:
/tf/active/vicechatdev/full_smartstat/dynamic_schema_discovery.py
Lines:
19 - 29
Complexity:
simple

Purpose

TableInfo serves as a structured container for storing and passing around complete information about a database table. It captures essential metadata such as table name, schema, type, row count, column definitions, primary and foreign key relationships, optional descriptions, and data quality scores. This class is typically used in database introspection, schema analysis, metadata management, and data cataloging systems where comprehensive table information needs to be collected, stored, and shared across different components.

Source Code

class TableInfo:
    """Information about a database table"""
    name: str
    schema: str
    table_type: str
    row_count: int
    columns: List[Dict[str, Any]]
    primary_keys: List[str]
    foreign_keys: List[Dict[str, str]]
    description: Optional[str] = None
    data_quality_score: Optional[int] = None

Parameters

Name Type Default Kind
bases - -

Parameter Details

name: The name of the database table as a string. This is the identifier used to reference the table within its schema.

schema: The database schema name where this table resides. Used to fully qualify the table in multi-schema databases.

table_type: The type of table (e.g., 'TABLE', 'VIEW', 'MATERIALIZED VIEW', 'TEMPORARY'). Indicates the nature of the database object.

row_count: The number of rows currently in the table as an integer. Provides size information for the table.

columns: A list of dictionaries where each dictionary contains metadata about a column. Typical keys include 'name', 'type', 'nullable', 'default', etc.

primary_keys: A list of column names (strings) that constitute the primary key of the table. Empty list if no primary key is defined.

foreign_keys: A list of dictionaries describing foreign key relationships. Each dictionary typically contains keys like 'column', 'referenced_table', 'referenced_column', etc.

description: Optional human-readable description or documentation of the table's purpose. Defaults to None if not provided.

data_quality_score: Optional integer score representing the data quality assessment of the table. Defaults to None if not calculated. Typically a numeric value within a defined range.

Return Value

Instantiating TableInfo returns a dataclass instance containing all the provided table metadata. The dataclass decorator automatically generates __init__, __repr__, __eq__, and other methods. The instance can be used to access table information through its attributes and can be easily serialized using the asdict function from dataclasses module.

Class Interface

Methods

__init__(name: str, schema: str, table_type: str, row_count: int, columns: List[Dict[str, Any]], primary_keys: List[str], foreign_keys: List[Dict[str, str]], description: Optional[str] = None, data_quality_score: Optional[int] = None)

Purpose: Initializes a new TableInfo instance with the provided table metadata. Auto-generated by the dataclass decorator.

Parameters:

  • name: The table name
  • schema: The schema name
  • table_type: The type of table
  • row_count: Number of rows in the table
  • columns: List of column metadata dictionaries
  • primary_keys: List of primary key column names
  • foreign_keys: List of foreign key relationship dictionaries
  • description: Optional table description
  • data_quality_score: Optional data quality score

Returns: A new TableInfo instance

__repr__() -> str

Purpose: Returns a string representation of the TableInfo instance. Auto-generated by the dataclass decorator.

Returns: String representation showing all field values

__eq__(other: object) -> bool

Purpose: Compares two TableInfo instances for equality based on all fields. Auto-generated by the dataclass decorator.

Parameters:

  • other: Another object to compare with

Returns: True if all fields are equal, False otherwise

Attributes

Name Type Description Scope
name str The name of the database table instance
schema str The database schema containing this table instance
table_type str The type of table (TABLE, VIEW, etc.) instance
row_count int The number of rows in the table instance
columns List[Dict[str, Any]] List of dictionaries containing metadata for each column instance
primary_keys List[str] List of column names that form the primary key instance
foreign_keys List[Dict[str, str]] List of dictionaries describing foreign key relationships instance
description Optional[str] Optional human-readable description of the table instance
data_quality_score Optional[int] Optional numeric score representing data quality assessment instance

Dependencies

  • typing
  • dataclasses

Required Imports

from dataclasses import dataclass
from typing import List, Dict, Optional, Any

Usage Example

from dataclasses import dataclass, asdict
from typing import List, Dict, Optional, Any

@dataclass
class TableInfo:
    name: str
    schema: str
    table_type: str
    row_count: int
    columns: List[Dict[str, Any]]
    primary_keys: List[str]
    foreign_keys: List[Dict[str, str]]
    description: Optional[str] = None
    data_quality_score: Optional[int] = None

# Create a TableInfo instance
table_info = TableInfo(
    name='users',
    schema='public',
    table_type='TABLE',
    row_count=1500,
    columns=[
        {'name': 'id', 'type': 'INTEGER', 'nullable': False},
        {'name': 'username', 'type': 'VARCHAR(50)', 'nullable': False},
        {'name': 'email', 'type': 'VARCHAR(100)', 'nullable': True}
    ],
    primary_keys=['id'],
    foreign_keys=[],
    description='User account information table',
    data_quality_score=95
)

# Access attributes
print(f"Table: {table_info.schema}.{table_info.name}")
print(f"Rows: {table_info.row_count}")
print(f"Primary Keys: {table_info.primary_keys}")

# Convert to dictionary for serialization
table_dict = asdict(table_info)
print(table_dict)

Best Practices

  • Always provide all required fields (name, schema, table_type, row_count, columns, primary_keys, foreign_keys) when instantiating TableInfo.
  • Use consistent column dictionary structure across all columns in the columns list for easier processing.
  • Keep row_count updated if the table size changes significantly, as this affects query planning and analysis.
  • Use the asdict() function from dataclasses module to convert TableInfo instances to dictionaries for JSON serialization or API responses.
  • Foreign key dictionaries should include at minimum 'column', 'referenced_table', and 'referenced_column' keys for clarity.
  • Data quality scores should follow a consistent scale (e.g., 0-100) across all TableInfo instances in your system.
  • Since this is a dataclass, instances are mutable by default. Consider using frozen=True in the decorator if immutability is desired.
  • The description field is valuable for documentation and should be populated when available from database comments or metadata.
  • This class is designed for data transfer and storage, not for business logic. Keep methods minimal or use it as a pure data container.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class DatabaseInfo 83.0% similar

    A dataclass that encapsulates complete database schema information including tables, columns, relationships, and metadata for a specific database instance.

    From: /tf/active/vicechatdev/full_smartstat/dynamic_schema_discovery.py
  • class DatabaseSchema 71.0% similar

    A dataclass that represents comprehensive database schema information, including table structures, columns, relationships, and categorizations for SQL database introspection and query generation.

    From: /tf/active/vicechatdev/full_smartstat/sql_query_generator.py
  • class DatabaseSchema_v1 62.9% similar

    A dataclass that represents database schema information, including table categories, relationships, and system architecture. Provides functionality to load schema from JSON files.

    From: /tf/active/vicechatdev/smartstat/sql_query_generator.py
  • class TableSelectionResult 55.3% similar

    A dataclass that encapsulates the results of a table selection operation, including selected tables, reasoning, confidence score, and suggested joins.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
  • class SqlGenerationResult 48.6% similar

    A dataclass that encapsulates the results of an SQL query generation operation, including the generated query, explanation, confidence score, and metadata about database objects used.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
← Back to Browse