# SQLite 内置自省工具集
# 这些工具帮助 AI 理解数据库结构,无需人工提供 schema 信息
sources:
sqlite-db:
kind: sqlite
database: ${DATABASE_NAME:./database.db}
tools:
# ==================== 数据库级别查询 ====================
sqlite_list_tables:
kind: sqlite-sql
source: sqlite-db
description: |
列出数据库中的所有表。
返回表名和类型(table 或 view)。
这是探索未知数据库的第一步。
statement: |
SELECT
name,
type
FROM sqlite_master
WHERE type IN ('table', 'view')
AND name NOT LIKE 'sqlite_%'
ORDER BY name;
sqlite_list_all_tables_with_info:
kind: sqlite-sql
source: sqlite-db
description: |
列出所有表及其详细信息,包括表名、类型和创建 SQL。
用于了解表的完整结构定义。
statement: |
SELECT
name,
type,
sql as create_statement
FROM sqlite_master
WHERE type IN ('table', 'view')
AND name NOT LIKE 'sqlite_%'
ORDER BY name;
# ==================== 表结构查询 ====================
sqlite_describe_table:
kind: sqlite-sql
source: sqlite-db
description: |
查看指定表的完整结构信息。
返回每个字段的名称、类型、是否可为空、默认值、是否主键等信息。
相当于 SQL 的 DESCRIBE 或 PostgreSQL 的 \d 命令。
parameters:
- name: table_name
type: string
description: 要查询的表名
statement: |
PRAGMA table_info(?1);
sqlite_get_table_schema:
kind: sqlite-sql
source: sqlite-db
description: |
获取表的创建 SQL 语句。
显示完整的 CREATE TABLE 定义,包括所有约束和索引。
parameters:
- name: table_name
type: string
description: 要查询的表名
statement: |
SELECT sql
FROM sqlite_master
WHERE type = 'table'
AND name = ?1;
sqlite_list_columns:
kind: sqlite-sql
source: sqlite-db
description: |
列出指定表的所有字段名和类型。
简化版的表结构查询,只返回最常用的信息。
parameters:
- name: table_name
type: string
description: 要查询的表名
statement: |
SELECT
name as column_name,
type as data_type,
"notnull" as not_null,
dflt_value as default_value,
pk as is_primary_key
FROM pragma_table_info(?1);
# ==================== 索引查询 ====================
sqlite_list_indexes:
kind: sqlite-sql
source: sqlite-db
description: |
列出数据库中的所有索引。
返回索引名称、所属表和创建 SQL。
statement: |
SELECT
name as index_name,
tbl_name as table_name,
sql as create_statement
FROM sqlite_master
WHERE type = 'index'
AND name NOT LIKE 'sqlite_%'
ORDER BY tbl_name, name;
sqlite_list_table_indexes:
kind: sqlite-sql
source: sqlite-db
description: |
列出指定表的所有索引及其详细信息。
parameters:
- name: table_name
type: string
description: 要查询的表名
statement: |
SELECT
name as index_name,
"unique" as is_unique,
origin as index_origin,
partial as is_partial
FROM pragma_index_list(?1);
sqlite_describe_index:
kind: sqlite-sql
source: sqlite-db
description: |
查看索引包含的字段信息。
parameters:
- name: index_name
type: string
description: 要查询的索引名
statement: |
SELECT
seqno as sequence,
cid as column_id,
name as column_name
FROM pragma_index_info(?1)
ORDER BY seqno;
# ==================== 外键关系查询 ====================
sqlite_list_foreign_keys:
kind: sqlite-sql
source: sqlite-db
description: |
列出指定表的所有外键关系。
显示引用的表、字段和级联操作规则。
parameters:
- name: table_name
type: string
description: 要查询的表名
statement: |
SELECT
id as fk_id,
seq as sequence,
"table" as referenced_table,
"from" as from_column,
"to" as to_column,
on_update,
on_delete
FROM pragma_foreign_key_list(?1);
# ==================== 数据统计查询 ====================
sqlite_count_rows:
kind: sqlite-sql
source: sqlite-db
description: |
统计指定表的总行数。
注意:对于大表,此操作可能较慢。
parameters:
- name: table_name
type: string
description: 要统计的表名
statement: |
SELECT COUNT(*) as row_count
FROM ?1;
sqlite_table_stats:
kind: sqlite-sql
source: sqlite-db
description: |
获取所有表的统计信息(表名和大致行数)。
注意:这个查询使用 sqlite_stat1,可能不准确。
statement: |
SELECT
tbl as table_name,
idx as index_name,
stat as statistics
FROM sqlite_stat1
ORDER BY tbl;
# ==================== 数据预览 ====================
sqlite_preview_table:
kind: sqlite-sql
source: sqlite-db
description: |
预览表的前几行数据。
用于快速了解表中存储的数据格式和内容。
默认返回前 10 行。
parameters:
- name: table_name
type: string
description: 要预览的表名
- name: limit
type: integer
description: 返回的行数,默认 10
statement: |
SELECT * FROM ?1 LIMIT ?2;
# ==================== 数据库元信息 ====================
sqlite_database_info:
kind: sqlite-sql
source: sqlite-db
description: |
获取数据库的基本信息,包括文件格式、编码、页大小等。
statement: |
PRAGMA database_list;
sqlite_get_schema_version:
kind: sqlite-sql
source: sqlite-db
description: |
获取数据库的 schema 版本号。
每次修改数据库结构时版本号会递增。
statement: |
PRAGMA schema_version;
# ==================== 完整数据库概览 ====================
sqlite_database_summary:
kind: sqlite-sql
source: sqlite-db
description: |
获取数据库的完整摘要:所有对象(表、索引、触发器、视图)的列表。
这是了解整个数据库结构的最全面的查询。
statement: |
SELECT
type,
name,
tbl_name as table_name,
CASE
WHEN length(sql) > 100 THEN substr(sql, 1, 97) || '...'
ELSE sql
END as definition_preview
FROM sqlite_master
WHERE name NOT LIKE 'sqlite_%'
ORDER BY type, name;
# ==================== 工具集定义 ====================
toolsets:
# 基础自省工具集 - AI 探索数据库的必备工具
sqlite_introspection:
- sqlite_list_tables
- sqlite_describe_table
- sqlite_list_columns
- sqlite_preview_table
- sqlite_database_summary
# 高级自省工具集 - 深入了解数据库结构
sqlite_advanced_introspection:
- sqlite_list_all_tables_with_info
- sqlite_get_table_schema
- sqlite_list_indexes
- sqlite_list_table_indexes
- sqlite_describe_index
- sqlite_list_foreign_keys
# 统计分析工具集
sqlite_statistics:
- sqlite_count_rows
- sqlite_table_stats
- sqlite_database_info
- sqlite_get_schema_version
# 完整工具集 - 包含所有内置工具
sqlite_complete:
- sqlite_list_tables
- sqlite_list_all_tables_with_info
- sqlite_describe_table
- sqlite_get_table_schema
- sqlite_list_columns
- sqlite_list_indexes
- sqlite_list_table_indexes
- sqlite_describe_index
- sqlite_list_foreign_keys
- sqlite_count_rows
- sqlite_table_stats
- sqlite_preview_table
- sqlite_database_info
- sqlite_get_schema_version
- sqlite_database_summary