/** * 查询模板系统 * 提供 10 个预设模板,适配 MySQL、PostgreSQL、Redis、MongoDB 四种数据库类型 */ export type DBType = 'mysql' | 'postgresql' | 'redis' | 'mongodb'; export type QueryTemplate = { id: string; name: string; description: string; category: string; sql: Partial>; supportedTypes: DBType[]; }; export const queryTemplates: QueryTemplate[] = [ { id: 'table-structure', name: '查询表结构', description: '查看指定表或集合的结构定义,包括字段、类型、索引等信息', category: '表结构', sql: { mysql: 'SHOW CREATE TABLE {table};', postgresql: "SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_name = '{table}' ORDER BY ordinal_position;", mongodb: 'db.{collection}.getIndexes()', }, supportedTypes: ['mysql', 'postgresql', 'mongodb'], }, { id: 'table-size', name: '查询表大小', description: '查看数据库中各表或集合的存储空间占用情况', category: '数据统计', sql: { mysql: "SELECT table_name, data_length, index_length FROM information_schema.tables WHERE table_schema = '{db}';", postgresql: "SELECT relname, pg_relation_size(oid) FROM pg_class WHERE relkind='r';", redis: 'INFO memory', mongodb: 'db.stats()', }, supportedTypes: ['mysql', 'postgresql', 'redis', 'mongodb'], }, { id: 'active-connections', name: '查询活跃连接', description: '查看当前数据库的活跃连接数及连接详情', category: '性能分析', sql: { mysql: 'SHOW PROCESSLIST;', postgresql: 'SELECT * FROM pg_stat_activity;', redis: 'CLIENT LIST', mongodb: 'db.currentOp()', }, supportedTypes: ['mysql', 'postgresql', 'redis', 'mongodb'], }, { id: 'indexes', name: '查询索引', description: '查看指定表或集合的索引信息', category: '表结构', sql: { mysql: 'SHOW INDEX FROM {table};', postgresql: "SELECT * FROM pg_indexes WHERE tablename = '{table}';", mongodb: 'db.{collection}.getIndexes()', }, supportedTypes: ['mysql', 'postgresql', 'mongodb'], }, { id: 'lock-waits', name: '查询锁等待', description: '查看当前数据库中存在的锁等待情况,排查锁冲突', category: '性能分析', sql: { mysql: 'SELECT * FROM information_schema.innodb_lock_waits;', postgresql: 'SELECT * FROM pg_locks WHERE NOT granted;', }, supportedTypes: ['mysql', 'postgresql'], }, { id: 'top-n', name: 'TOP N查询', description: '查询指定表中按ID倒序排列的前N条记录', category: '基础查询', sql: { mysql: 'SELECT * FROM {table} ORDER BY id DESC LIMIT {n};', postgresql: 'SELECT * FROM {table} ORDER BY id DESC LIMIT {n};', mongodb: 'db.{collection}.find().sort({_id:-1}).limit({n})', }, supportedTypes: ['mysql', 'postgresql', 'mongodb'], }, { id: 'fuzzy-search', name: '模糊搜索', description: '根据关键词对指定字段进行模糊匹配查询', category: '基础查询', sql: { mysql: "SELECT * FROM {table} WHERE {column} LIKE '%{keyword}%';", postgresql: "SELECT * FROM {table} WHERE {column} LIKE '%{keyword}%';", redis: 'KEYS *{keyword}*', mongodb: 'db.{collection}.find({field: /{keyword}/})', }, supportedTypes: ['mysql', 'postgresql', 'redis', 'mongodb'], }, { id: 'statistics', name: '查询统计信息', description: '查看表或集合的统计信息,包括行数、大小、索引等', category: '数据统计', sql: { mysql: "SHOW TABLE STATUS LIKE '{table}';", postgresql: "SELECT * FROM pg_stat_user_tables WHERE relname='{table}';", redis: 'INFO keyspace', mongodb: 'db.{collection}.stats()', }, supportedTypes: ['mysql', 'postgresql', 'redis', 'mongodb'], }, { id: 'execution-plan', name: '查询执行计划', description: '查看查询语句的执行计划,用于SQL性能优化分析', category: '性能分析', sql: { mysql: 'EXPLAIN SELECT ...', postgresql: 'EXPLAIN ANALYZE SELECT ...', mongodb: 'db.{collection}.find({...}).explain()', }, supportedTypes: ['mysql', 'postgresql', 'mongodb'], }, { id: 'redis-get', name: 'GET 查询', description: '获取 Redis String 类型 key 的值', category: '基础查询', sql: { redis: 'GET {key}' }, supportedTypes: ['redis'], }, { id: 'redis-hgetall', name: 'HGETALL 查询', description: '获取 Redis Hash 类型 key 的所有字段和值', category: '基础查询', sql: { redis: 'HGETALL {key}' }, supportedTypes: ['redis'], }, { id: 'redis-hget', name: 'HGET 查询', description: '获取 Redis Hash 类型 key 中指定字段的值', category: '基础查询', sql: { redis: 'HGET {key} {field}' }, supportedTypes: ['redis'], }, ]; /** * 获取模板在指定数据库类型下的查询语句 * 自动替换占位符: {table} → 当前选中的表, {db} → 当前数据库, {collection} → 集合名 */ export function getTemplateSQL( template: QueryTemplate, dbType: DBType, vars?: { table?: string; db?: string; collection?: string } ): string { const sql = template.sql[dbType]; if (!sql) { return `-- 当前模板「${template.name}」不支持 ${dbType} 数据库类型`; } let result = sql; const v = vars || {}; if (v.table) result = result.replace(/\{table\}/g, v.table); if (v.db) result = result.replace(/\{db\}/g, v.db); if (v.collection) result = result.replace(/\{collection\}/g, v.collection); // 未替换的占位符保留,用户手动填写 return result; } /** * 获取指定数据库类型支持的所有模板 * @param dbType 数据库类型 * @returns 该数据库类型支持的模板列表 */ export function getTemplatesForType(dbType: DBType): QueryTemplate[] { return queryTemplates.filter((template) => template.supportedTypes.includes(dbType) ); }