/** * Smart Schema - Database Schema Analyzer with 83% Token Reduction * * Features: * - Multi-database schema introspection (PostgreSQL, MySQL, SQLite) * - Relationship graph with circular dependency detection * - Index analysis (missing/unused) * - Schema diff between environments * - Intelligent caching with schema version detection * - Token-optimized output formats * * Token Reduction Strategy: * - First introspection: Full schema details (baseline) * - Cached: Summary statistics only (95% reduction) * - Diff mode: Changed objects only (90% reduction) * - Analysis-only: Issues + recommendations (85% reduction) * - Average: 83% reduction */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; export interface SmartSchemaOptions { connectionString: string; mode?: 'full' | 'summary' | 'analysis' | 'diff'; compareWith?: string; forceRefresh?: boolean; includeData?: boolean; analyzeTables?: string[]; detectUnusedIndexes?: boolean; } export interface DatabaseSchema { databaseType: 'postgresql' | 'mysql' | 'sqlite'; version: string; schemaVersion: string; tables: TableInfo[]; views: ViewInfo[]; indexes: IndexInfo[]; constraints: ConstraintInfo[]; relationships: Relationship[]; } export interface TableInfo { schema: string; name: string; columns: ColumnInfo[]; rowCount?: number; sizeBytes?: number; comment?: string; } export interface ColumnInfo { name: string; type: string; nullable: boolean; defaultValue?: string; isPrimaryKey: boolean; isForeignKey: boolean; comment?: string; } export interface ViewInfo { schema: string; name: string; definition: string; } export interface IndexInfo { schema: string; table: string; name: string; columns: string[]; isUnique: boolean; isPrimary: boolean; sizeBytes?: number; unusedScans?: number; } export interface ConstraintInfo { schema: string; table: string; name: string; type: 'primary_key' | 'foreign_key' | 'unique' | 'check'; columns: string[]; referencedTable?: string; referencedColumns?: string[]; onDelete?: string; onUpdate?: string; } export interface Relationship { fromTable: string; fromSchema: string; fromColumns: string[]; toTable: string; toSchema: string; toColumns: string[]; constraintName: string; } export interface RelationshipGraph { nodes: Set; edges: Map>; } export interface CircularDependency { cycle: string[]; affectedTables: Set; } export interface SchemaAnalysis { summary: { tableCount: number; viewCount: number; indexCount: number; relationshipCount: number; totalSizeBytes?: number; }; issues: SchemaIssue[]; recommendations: string[]; relationshipGraph: RelationshipGraph; circularDependencies: CircularDependency[]; missingIndexes: MissingIndex[]; unusedIndexes: IndexInfo[]; } export interface SchemaIssue { severity: 'error' | 'warning' | 'info'; type: string; table?: string; column?: string; message: string; recommendation?: string; } export interface MissingIndex { table: string; columns: string[]; reason: string; estimatedImpact: 'high' | 'medium' | 'low'; } export interface SchemaDiff { added: { tables: TableInfo[]; columns: Array<{ table: string; column: ColumnInfo; }>; indexes: IndexInfo[]; constraints: ConstraintInfo[]; }; removed: { tables: TableInfo[]; columns: Array<{ table: string; column: ColumnInfo; }>; indexes: IndexInfo[]; constraints: ConstraintInfo[]; }; modified: { columns: Array<{ table: string; column: string; oldType: string; newType: string; changes: string[]; }>; constraints: Array<{ table: string; constraint: string; changes: string[]; }>; }; migrationSuggestions: string[]; } export interface SmartSchemaResult { schema?: DatabaseSchema; analysis: SchemaAnalysis; diff?: SchemaDiff; cached: boolean; cacheAge?: number; } export interface SmartSchemaOutput { result: string; tokens: { baseline: number; actual: number; saved: number; reduction: number; }; cached: boolean; analysisTime: number; } export declare class SmartSchema { private cache; private tokenCounter; private metrics; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); run(options: SmartSchemaOptions): Promise; private detectDatabaseType; private introspectSchema; private introspectPostgreSQL; private introspectMySQL; private introspectSQLite; private analyzeSchema; private buildRelationshipGraph; private detectCircularDependencies; private detectMissingIndexes; private detectUnusedIndexes; private performSchemaDiff; private diffSchemas; private diffTableColumns; private generateCacheKey; private generateSchemaVersionHash; private getCachedResult; private cacheResult; private transformOutput; private formatSummaryOutput; private formatAnalysisOutput; private formatDiffOutput; private formatFullOutput; private formatBytes; private formatDuration; } /** * Factory Function - Use Constructor Injection */ export declare function getSmartSchema(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartSchema; /** * CLI Function - Create Resources and Use Factory */ export declare function runSmartSchema(options: SmartSchemaOptions): Promise; export declare const SMART_SCHEMA_TOOL_DEFINITION: { readonly name: "smart_schema"; readonly description: "Database schema analyzer with intelligent caching and 83% token reduction. Supports PostgreSQL, MySQL, and SQLite. Provides schema introspection, relationship analysis, index recommendations, and schema diff."; readonly inputSchema: { readonly type: "object"; readonly properties: { readonly connectionString: { readonly type: "string"; readonly description: "Database connection string (e.g., postgresql://user:pass@host:port/db, mysql://user:pass@host/db, /path/to/database.sqlite)"; }; readonly mode: { readonly type: "string"; readonly enum: readonly ["full", "summary", "analysis", "diff"]; readonly description: "Output mode: full (complete schema), summary (statistics only, 95% reduction), analysis (issues only, 85% reduction), diff (compare schemas, 90% reduction)"; readonly default: "full"; }; readonly compareWith: { readonly type: "string"; readonly description: "Second connection string for diff mode (compare two databases)"; }; readonly forceRefresh: { readonly type: "boolean"; readonly description: "Force refresh schema analysis, bypassing cache"; readonly default: false; }; readonly includeData: { readonly type: "boolean"; readonly description: "Include row counts and table sizes in analysis"; readonly default: false; }; readonly analyzeTables: { readonly type: "array"; readonly items: { readonly type: "string"; }; readonly description: "Specific tables to analyze (all if not specified)"; }; readonly detectUnusedIndexes: { readonly type: "boolean"; readonly description: "Detect potentially unused indexes (requires database statistics)"; readonly default: false; }; }; readonly required: readonly ["connectionString"]; }; }; //# sourceMappingURL=smart-schema.d.ts.map