本文档介绍数据库表查询相关的API接口,包括获取表列表和表结构分析功能。
API端点 | 功能 | 必需参数 | 可选参数 |
---|---|---|---|
POST /api/v0/database/tables |
获取数据库表列表 | 无 | db_connection , schema |
POST /api/v0/database/table/ddl |
获取表DDL和结构分析 | table |
db_connection , business_context , type |
POST /api/v0/database/tables
参数名 | 类型 | 必需 | 说明 | 示例 |
---|---|---|---|---|
db_connection |
string | 否 | PostgreSQL连接字符串 不传则使用默认配置 |
"postgresql://user:pass@host:port/db" |
schema |
string | 否 | Schema名称,支持逗号分隔多个 默认为"public" |
"public,ods,dw" |
{
"schema": "public,ods"
}
{
"db_connection": "postgresql://postgres:postgres@192.168.67.1:5432/bank_db",
"schema": "public"
}
此时的数据库连接配置来自于app_config.py
{}
{
"code": 200,
"data": {
"db_connection_info": {
"database": "highway_db"
},
"response": "获取表列表成功",
"schemas": [
"public"
],
"tables": [
"public.bss_branch_copy",
"public.bss_business_day_data",
"public.bss_car_day_count",
"public.bss_company",
"public.bss_section_route",
"public.bss_section_route_area_link",
"public.bss_service_area",
"public.bss_service_area_mapper"
],
"total": 8
},
"message": "操作成功",
"success": true
}
POST /api/v0/database/table/ddl
参数名 | 类型 | 必需 | 说明 | 示例 |
---|---|---|---|---|
table |
string | 是 | 表名,支持schema.table格式 | "public.bank_churners" |
db_connection |
string | 否 | PostgreSQL连接字符串 不传则使用默认配置 |
"postgresql://user:pass@host:port/db" |
business_context |
string | 否 | 业务上下文描述 传入则启用AI注释生成 |
"银行信用卡持卡人信息" |
type |
string | 否 | 输出类型:ddl /md /both 默认为"ddl" |
"md" |
{
"table": "public.bank_churners"
}
{
"db_connection": "postgresql://postgres:postgres@192.168.67.1:5432/bank_db",
"table": "public.bank_churners",
"business_context": "银行信用卡用户统计表"
}
注意:只有提供 "business_context" 参数且不为空时,才会在返回结果中生成字段注释。
{
"code": 200,
"data": {
"ddl": "-- 中文名: 银行信用卡客户流失分析表\n-- 描述: 银行信用卡客户流失分析表,记录用户人口统计特征及流失状态,用于预测客户流失风险并制定客户保留策略。\ncreate table public.bank_churners (\n client_num bigint not null -- 客户编号,主键,\n attrition_flag varchar(32) -- 客户流失标识,\n customer_age smallint -- 客户年龄,\n gender varchar(8) -- 性别,\n dependent_count smallint -- 家属数量,\n education_level varchar(32) -- 学历等级,\n marital_status varchar(16) -- 婚姻状况,\n income_category varchar(32) -- 收入等级,\n card_category varchar(16) -- 信用卡类别,\n months_on_book smallint -- 开户月份数,\n credit_limit numeric(12,2) -- 信用额度,\n total_revolving_bal numeric(12,2) -- 总循环余额,\n avg_open_to_buy numeric(12,2) -- 平均可用额度,\n total_amt_chng_q4_q1 double precision -- 季度交易金额变化率,\n total_trans_amt numeric(12,2) -- 总交易金额,\n total_trans_ct smallint -- 总交易次数,\n total_ct_chng_q4_q1 double precision -- 季度交易次数变化率,\n avg_utilization_ratio double precision -- 平均利用率,\n nb_classifier_attrition_flag_1 double precision -- 流失预测模型1得分,\n nb_classifier_attrition_flag_2 double precision -- 流失预测模型2得分,\n primary key (client_num)\n);",
"fields": [
{
"comment": "客户编号",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": true,
"name": "client_num",
"nullable": false,
"type": "bigint"
},
{
"comment": "客户流失标识",
"default_value": null,
"enum_values": [
"Existing Customer",
"Attrited Customer"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "attrition_flag",
"nullable": true,
"type": "character varying"
},
{
"comment": "客户年龄",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "customer_age",
"nullable": true,
"type": "smallint"
},
{
"comment": "性别",
"default_value": null,
"enum_values": [
"F",
"M"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "gender",
"nullable": true,
"type": "character varying"
},
{
"comment": "家属数量",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "dependent_count",
"nullable": true,
"type": "smallint"
},
{
"comment": "学历等级",
"default_value": null,
"enum_values": [
"Graduate",
"High School",
"Unknown",
"Uneducated",
"College",
"Post-Graduate",
"Doctorate"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "education_level",
"nullable": true,
"type": "character varying"
},
{
"comment": "婚姻状况",
"default_value": null,
"enum_values": [
"Married",
"Single",
"Unknown",
"Divorced"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "marital_status",
"nullable": true,
"type": "character varying"
},
{
"comment": "收入等级",
"default_value": null,
"enum_values": [
"Less than $40K",
"$40K - $60K",
"$80K - $120K",
"$60K - $80K",
"Unknown",
"$120K +"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "income_category",
"nullable": true,
"type": "character varying"
},
{
"comment": "信用卡类别",
"default_value": null,
"enum_values": [
"Blue",
"Silver",
"Gold",
"Platinum"
],
"is_enum": true,
"is_foreign_key": false,
"is_primary_key": false,
"name": "card_category",
"nullable": true,
"type": "character varying"
},
{
"comment": "开户月份数",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "months_on_book",
"nullable": true,
"type": "smallint"
},
{
"comment": "信用额度",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "credit_limit",
"nullable": true,
"type": "numeric"
},
{
"comment": "总循环余额",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "total_revolving_bal",
"nullable": true,
"type": "numeric"
},
{
"comment": "平均可用额度",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "avg_open_to_buy",
"nullable": true,
"type": "numeric"
},
{
"comment": "季度交易金额变化率",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "total_amt_chng_q4_q1",
"nullable": true,
"type": "double precision"
},
{
"comment": "总交易金额",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "total_trans_amt",
"nullable": true,
"type": "numeric"
},
{
"comment": "总交易次数",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "total_trans_ct",
"nullable": true,
"type": "smallint"
},
{
"comment": "季度交易次数变化率",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "total_ct_chng_q4_q1",
"nullable": true,
"type": "double precision"
},
{
"comment": "平均利用率",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "avg_utilization_ratio",
"nullable": true,
"type": "double precision"
},
{
"comment": "流失预测模型1得分",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "nb_classifier_attrition_flag_1",
"nullable": true,
"type": "double precision"
},
{
"comment": "流失预测模型2得分",
"default_value": null,
"enum_values": null,
"is_enum": false,
"is_foreign_key": false,
"is_primary_key": false,
"name": "nb_classifier_attrition_flag_2",
"nullable": true,
"type": "double precision"
}
],
"generation_info": {
"business_context": "银行信用卡用户统计表",
"database": "bank_db",
"has_llm_comments": true,
"output_type": "ddl"
},
"response": "获取表DDL成功",
"table_info": {
"comment": "银行信用卡客户流失分析表,记录用户人口统计特征及流失状态,用于预测客户流失风险并制定客户保留策略。",
"field_count": 20,
"full_name": "public.bank_churners",
"row_count": 10127,
"schema_name": "public",
"table_name": "bank_churners",
"table_size": "2008 kB"
}
},
"message": "操作成功",
"success": true
}
{
"table": "public.bank_churners",
"business_context": "银行信用卡持卡人信息表,用于分析客户流失情况",
"type": "md"
}
{
"db_connection": "postgresql://postgres:postgres@192.168.67.1:5432/bank_db",
"table": "public.bank_churners",
"business_context": "银行信用卡持卡人信息",
"type": "both"
}
{
"success": true,
"code": 200,
"message": "获取表DDL成功",
"data": {
"ddl": "-- 中文名: 银行信用卡持卡人信息表\ncreate table public.bank_churners (\n client_num bigint not null,\n attrition_flag varchar(32),\n ...\n);",
"table_info": {
"table_name": "bank_churners",
"schema_name": "public",
"full_name": "public.bank_churners",
"comment": "银行信用卡持卡人信息表,记录客户流失状态、人口统计特征及账户活跃度数据",
"field_count": 20,
"row_count": 10127,
"table_size": "2008 kB"
},
"fields": [
{
"name": "client_num",
"type": "bigint",
"nullable": false,
"comment": "客户编号",
"is_primary_key": true,
"is_foreign_key": false,
"default_value": null,
"is_enum": false,
"enum_values": null
},
{
"name": "attrition_flag",
"type": "character varying",
"nullable": true,
"comment": "客户流失标志",
"is_primary_key": false,
"is_foreign_key": false,
"default_value": null,
"is_enum": true,
"enum_values": ["Existing Customer", "Attrited Customer"]
}
],
"generation_info": {
"business_context": "银行信用卡持卡人信息",
"output_type": "ddl",
"has_llm_comments": true,
"database": "bank_db"
}
}
}
当传入 business_context
参数时,系统会:
类型 | 说明 | 适用场景 |
---|---|---|
ddl |
返回CREATE TABLE语句 | 数据库迁移、表结构复制 |
md |
返回Markdown格式文档 | 文档生成、团队共享 |
both |
同时返回DDL和MD | 完整的表分析需求 |
当不传入 db_connection
参数时,系统会自动使用 app_config.py
中的 APP_DB_CONFIG
配置:
错误码 | 说明 | 解决方案 |
---|---|---|
400 | 缺少必需参数 | 检查请求参数,确保传入必需的参数 |
500 | 数据库连接失败 | 检查数据库连接字符串和网络连接 |
500 | 表不存在 | 确认表名正确,检查schema和表是否存在 |
{
"success": false,
"code": 400,
"message": "请求参数错误",
"data": {
"response": "缺少必需参数:table",
"missing_params": ["table"],
"timestamp": "2025-07-02T10:30:00"
}
}
db_connection
,使用默认配置db_connection
参数business_context
以获得更好的AI注释import requests
# 获取表列表
def get_tables(schema="public"):
url = "http://localhost:8084/api/v0/database/tables"
data = {"schema": schema}
response = requests.post(url, json=data)
return response.json()
# 获取表DDL
def get_table_ddl(table, business_context=None, output_type="ddl"):
url = "http://localhost:8084/api/v0/database/table/ddl"
data = {
"table": table,
"type": output_type
}
if business_context:
data["business_context"] = business_context
response = requests.post(url, json=data)
return response.json()
# 使用示例
tables = get_tables("public")
ddl = get_table_ddl("public.bank_churners", "银行客户信息", "md")
// 获取表列表
async function getTables(schema = 'public') {
const response = await fetch('/api/v0/database/tables', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ schema })
});
return await response.json();
}
// 获取表DDL
async function getTableDDL(table, businessContext = null, type = 'ddl') {
const data = { table, type };
if (businessContext) {
data.business_context = businessContext;
}
const response = await fetch('/api/v0/database/table/ddl', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return await response.json();
}
db_connection
参数改为可选,支持使用默认配置如有问题或建议,请联系开发团队。