🔍 Code Extractor

class DatabaseSchema_v1

Maturity: 44

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

File:
/tf/active/vicechatdev/smartstat/sql_query_generator.py
Lines:
16 - 42
Complexity:
simple

Purpose

DatabaseSchema serves as a structured container for comprehensive database metadata. It stores information about database structure including table organization, relationships between tables, and system architecture. The primary use case is to load and maintain database schema information from JSON configuration files for use in database analysis, query generation, or documentation tools. It provides a type-safe way to access database metadata throughout an application.

Source Code

class DatabaseSchema:
    """Database schema information"""
    database_name: str
    description: str
    table_categories: Dict[str, Any]
    complete_table_list: List[str]
    key_relationships: Dict[str, Any]
    system_architecture: Dict[str, Any]
    
    @classmethod
    def from_json(cls, json_path: str) -> 'DatabaseSchema':
        """Load database schema from JSON file"""
        try:
            with open(json_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
            
            return cls(
                database_name=data.get('database_info', {}).get('name', 'Unknown'),
                description=data.get('database_info', {}).get('description', ''),
                table_categories=data.get('table_categories', {}),
                complete_table_list=data.get('complete_table_list', []),
                key_relationships=data.get('key_relationships', {}),
                system_architecture=data.get('system_architecture', {})
            )
        except Exception as e:
            logger.error(f"Error loading database schema from {json_path}: {str(e)}")
            raise

Parameters

Name Type Default Kind
bases - -

Parameter Details

database_name: The name identifier of the database. Expected to be a string representing the database name, defaults to 'Unknown' if not provided in JSON.

description: A textual description of the database's purpose and contents. Can be an empty string if not provided.

table_categories: A dictionary organizing tables into logical categories. Keys are category names (strings) and values can be any structure (typically lists of table names or nested dictionaries with table metadata).

complete_table_list: A flat list of all table names in the database as strings. Provides quick access to all available tables without navigating categories.

key_relationships: A dictionary describing relationships between tables, typically foreign key relationships. Structure depends on the schema format but generally maps table names to their relationship definitions.

system_architecture: A dictionary containing information about the overall system architecture, such as database type, version, connection details, or architectural patterns used.

Return Value

The class itself returns a DatabaseSchema instance when instantiated. The from_json class method returns a fully populated DatabaseSchema object loaded from a JSON file. If the JSON file cannot be read or parsed, it raises an exception after logging the error.

Class Interface

Methods

from_json(cls, json_path: str) -> 'DatabaseSchema'

Purpose: Class method that loads database schema information from a JSON file and returns a populated DatabaseSchema instance

Parameters:

  • cls: The class itself (automatically passed for class methods)
  • json_path: String path to the JSON file containing the database schema. Can be relative or absolute path.

Returns: A fully instantiated DatabaseSchema object with all attributes populated from the JSON file. Raises exceptions if file cannot be read or JSON is invalid.

Attributes

Name Type Description Scope
database_name str The name of the database instance
description str A textual description of the database's purpose and contents instance
table_categories Dict[str, Any] Dictionary organizing tables into logical categories with category names as keys instance
complete_table_list List[str] Flat list containing all table names in the database instance
key_relationships Dict[str, Any] Dictionary describing relationships between tables, typically foreign key relationships instance
system_architecture Dict[str, Any] Dictionary containing information about the overall system architecture and database configuration instance

Dependencies

  • json
  • logging
  • typing
  • dataclasses
  • pathlib
  • datetime
  • os
  • urllib.parse
  • importlib.util
  • re

Required Imports

import json
import logging
from typing import Dict, List, Optional, Any, Tuple
from dataclasses import dataclass
from pathlib import Path
from datetime import datetime
import os
from urllib.parse import quote_plus
import importlib.util
import re

Usage Example

# Ensure logger is configured
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Load schema from JSON file
schema = DatabaseSchema.from_json('database_schema.json')

# Access schema information
print(f"Database: {schema.database_name}")
print(f"Description: {schema.description}")
print(f"Total tables: {len(schema.complete_table_list)}")

# Access table categories
for category, tables in schema.table_categories.items():
    print(f"Category {category}: {tables}")

# Check relationships
if 'users' in schema.key_relationships:
    print(f"User table relationships: {schema.key_relationships['users']}")

# Manual instantiation (less common)
schema_manual = DatabaseSchema(
    database_name='MyDB',
    description='A sample database',
    table_categories={'core': ['users', 'products']},
    complete_table_list=['users', 'products', 'orders'],
    key_relationships={'orders': {'user_id': 'users.id'}},
    system_architecture={'type': 'PostgreSQL', 'version': '14'}
)

Best Practices

  • Always use the from_json class method to load schema from files rather than manual instantiation to ensure consistent data loading and error handling
  • Ensure the logger is properly configured before calling from_json, as it relies on a module-level logger instance
  • The JSON file structure must match the expected schema format with all required keys, though missing keys will use default values (empty dict/list or 'Unknown')
  • This is an immutable dataclass by default - once created, attributes should not be modified unless the dataclass is explicitly made mutable
  • Handle exceptions when calling from_json as it will raise exceptions for file I/O errors or JSON parsing errors
  • The class is designed for read-only access to schema information, not for schema modification or database operations
  • Store the DatabaseSchema instance for the lifetime of your application rather than repeatedly loading from JSON

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class DatabaseSchema 85.8% 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 DatabaseInfo 73.1% 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
  • function load_database_schema 69.0% similar

    Loads a database schema from a JSON file and returns it as a DatabaseSchema object.

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

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

    From: /tf/active/vicechatdev/full_smartstat/dynamic_schema_discovery.py
  • class DataSource 56.3% similar

    A dataclass that represents configuration for various data sources, supporting file-based, SQL database, and query-based data access patterns.

    From: /tf/active/vicechatdev/vice_ai/models.py
← Back to Browse