/** * Database Service * Core business logic for database operations * Shared between MCP and HTTP modes */ import type { DbAdapter, DbConfig, QueryResult, SchemaInfo, TableInfo, EnumValuesResult, SampleDataResult } from '../types/adapter.js'; import { SchemaEnhancerConfig } from '../utils/schema-enhancer.js'; /** * Schema 缓存配置 */ export interface SchemaCacheConfig { /** 缓存过期时间(毫秒),默认 5 分钟 */ ttl: number; /** 是否启用缓存,默认 true */ enabled: boolean; } /** * Schema 增强配置(导出供外部使用) */ export type { SchemaEnhancerConfig }; /** * Schema 缓存统计信息 */ export interface SchemaCacheStats { /** 缓存是否有效 */ isCached: boolean; /** 缓存时间 */ cachedAt: Date | null; /** 缓存过期时间 */ expiresAt: Date | null; /** 缓存命中次数 */ hitCount: number; /** 缓存未命中次数 */ missCount: number; } /** * Database Service Class * Encapsulates all database operations with validation and error handling */ export declare class DatabaseService { private adapter; private config; private schemaCache; private schemaCacheTime; private cacheConfig; private cacheHitCount; private cacheMissCount; private schemaEnhancer; private dataMasker; constructor(adapter: DbAdapter, config: DbConfig, cacheConfig?: Partial, enhancerConfig?: Partial); /** * Execute a query with validation */ executeQuery(query: string, params?: unknown[]): Promise; /** * Get complete database schema * @param forceRefresh - 是否强制刷新缓存,忽略现有缓存 */ getSchema(forceRefresh?: boolean): Promise; /** * 增强 Schema 信息 * - 为现有外键关系添加 source 标记 * - 推断隐式关系 * - 细化关系类型 */ private enhanceSchema; /** * Get information about a specific table * @param tableName - 表名(支持 schema.table_name 格式) * @param forceRefresh - 是否强制刷新缓存 */ getTableInfo(tableName: string, forceRefresh?: boolean): Promise; /** * List all tables in the database * @param forceRefresh - 是否强制刷新缓存 */ listTables(forceRefresh?: boolean): Promise; /** * Test database connection */ testConnection(): Promise; /** * 清除 Schema 缓存 */ clearSchemaCache(): void; /** * 获取缓存统计信息 */ getCacheStats(): SchemaCacheStats; /** * 获取缓存命中率 */ getCacheHitRate(): string; /** * 更新缓存配置 */ updateCacheConfig(config: Partial): void; /** * 更新 Schema 增强配置 */ updateEnhancerConfig(config: Partial): void; /** * 获取 Schema 增强配置 */ getEnhancerConfig(): SchemaEnhancerConfig; /** * Validate query against write permissions */ private validateQuery; /** * Get the underlying adapter */ getAdapter(): DbAdapter; /** * Get the configuration */ getConfig(): DbConfig; /** * 获取指定列的枚举值 * 用于帮助 LLM 了解 status、type 等枚举列的所有可能值 * * @param tableName - 表名 * @param columnName - 列名 * @param limit - 最大返回数量(默认 50,最大 100) * @param includeCount - 是否包含每个值的出现次数(默认 false) * @returns 枚举值查询结果 */ getEnumValues(tableName: string, columnName: string, limit?: number, includeCount?: boolean): Promise; /** * 获取表的示例数据(已脱敏) * 用于帮助 LLM 理解数据格式(日期格式、ID 格式等) * * @param tableName - 表名 * @param columns - 要查看的列(可选,默认全部) * @param limit - 返回行数(默认 3,最大 10) * @returns 示例数据查询结果 */ getSampleData(tableName: string, columns?: string[], limit?: number): Promise; /** * 构建枚举值查询 SQL(不含计数) */ private buildEnumValuesQuery; /** * 构建枚举值查询 SQL(含计数) */ private buildEnumValuesQueryWithCount; /** * 构建示例数据查询 SQL */ private buildSampleDataQuery; /** * 引用标识符(表名、列名) * 根据数据库类型使用不同的引号 * 支持 schema.table 格式:自动拆分并分别引用 */ private quoteIdentifier; /** * 引用单个标识符(不含 schema 前缀) */ private quoteSimpleIdentifier; /** * 添加 LIMIT 子句 * 根据数据库类型使用不同的语法 */ private appendLimit; } //# sourceMappingURL=database-service.d.ts.map