sql_validation_example.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """
  2. SQL验证器使用示例
  3. 演示如何使用SQL验证功能
  4. """
  5. import asyncio
  6. import json
  7. import sys
  8. import os
  9. from pathlib import Path
  10. # 添加项目根目录到Python路径
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. from schema_tools import SQLValidationAgent
  13. from schema_tools.utils.logger import setup_logging
  14. async def example_basic_validation():
  15. """基本SQL验证示例"""
  16. print("=" * 60)
  17. print("基本SQL验证示例")
  18. print("=" * 60)
  19. # 创建测试数据
  20. test_data = [
  21. {
  22. "question": "查询所有用户",
  23. "sql": "SELECT * FROM users;"
  24. },
  25. {
  26. "question": "按年龄分组统计用户数",
  27. "sql": "SELECT age, COUNT(*) as user_count FROM users GROUP BY age ORDER BY age;"
  28. },
  29. {
  30. "question": "查询不存在的表",
  31. "sql": "SELECT * FROM non_existent_table;"
  32. },
  33. {
  34. "question": "语法错误的SQL",
  35. "sql": "SELECT * FORM users;" # FORM而不是FROM
  36. }
  37. ]
  38. # 保存测试数据到文件
  39. test_file = Path("test_sql_data.json")
  40. with open(test_file, 'w', encoding='utf-8') as f:
  41. json.dump(test_data, f, ensure_ascii=False, indent=2)
  42. print(f"创建测试文件: {test_file}")
  43. print(f"包含 {len(test_data)} 个Question-SQL对")
  44. # 这里需要实际的数据库连接字符串
  45. # 请根据实际情况修改
  46. db_connection = "postgresql://user:password@localhost:5432/test_db"
  47. try:
  48. # 创建SQL验证Agent
  49. agent = SQLValidationAgent(
  50. db_connection=db_connection,
  51. input_file=str(test_file),
  52. output_dir="./validation_example_output"
  53. )
  54. print(f"\n开始验证...")
  55. # 执行验证
  56. report = await agent.validate()
  57. print(f"\n验证完成!")
  58. print(f"成功率: {report['summary']['success_rate']:.1%}")
  59. print(f"有效SQL: {report['summary']['valid_sqls']}/{report['summary']['total_questions']}")
  60. # 显示错误详情
  61. if report['errors']:
  62. print(f"\n错误详情:")
  63. for i, error in enumerate(report['errors'], 1):
  64. print(f" {i}. {error['error']}")
  65. print(f" SQL: {error['sql'][:100]}...")
  66. except Exception as e:
  67. print(f"验证失败: {e}")
  68. print("请检查数据库连接字符串和数据库权限")
  69. finally:
  70. # 清理测试文件
  71. if test_file.exists():
  72. test_file.unlink()
  73. print(f"\n清理测试文件: {test_file}")
  74. async def example_with_real_data():
  75. """使用真实数据的SQL验证示例"""
  76. print("=" * 60)
  77. print("真实数据SQL验证示例")
  78. print("=" * 60)
  79. # 检查是否有现有的Question-SQL文件
  80. possible_files = list(Path(".").glob("qs_*_pair.json"))
  81. if not possible_files:
  82. print("未找到现有的Question-SQL文件")
  83. print("请先运行 qs_generator 生成Question-SQL对,或使用基本示例")
  84. return
  85. input_file = possible_files[0]
  86. print(f"找到文件: {input_file}")
  87. # 读取文件内容预览
  88. with open(input_file, 'r', encoding='utf-8') as f:
  89. data = json.load(f)
  90. print(f"文件包含 {len(data)} 个Question-SQL对")
  91. print(f"前3个问题预览:")
  92. for i, item in enumerate(data[:3], 1):
  93. print(f" {i}. {item['question']}")
  94. # 数据库连接(需要根据实际情况修改)
  95. db_connection = "postgresql://user:password@localhost:5432/your_db"
  96. try:
  97. agent = SQLValidationAgent(
  98. db_connection=db_connection,
  99. input_file=str(input_file),
  100. output_dir="./validation_real_output"
  101. )
  102. print(f"\n开始验证...")
  103. report = await agent.validate()
  104. print(f"\n验证结果:")
  105. print(f" 总SQL数: {report['summary']['total_questions']}")
  106. print(f" 有效SQL: {report['summary']['valid_sqls']}")
  107. print(f" 无效SQL: {report['summary']['invalid_sqls']}")
  108. print(f" 成功率: {report['summary']['success_rate']:.1%}")
  109. print(f" 平均耗时: {report['summary']['average_execution_time']:.3f}秒")
  110. except Exception as e:
  111. print(f"验证失败: {e}")
  112. async def example_configuration_demo():
  113. """配置演示示例"""
  114. print("=" * 60)
  115. print("配置选项演示")
  116. print("=" * 60)
  117. from schema_tools.config import SCHEMA_TOOLS_CONFIG
  118. print("当前SQL验证配置:")
  119. sql_config = SCHEMA_TOOLS_CONFIG['sql_validation']
  120. for key, value in sql_config.items():
  121. print(f" {key}: {value}")
  122. print("\n可以通过命令行参数覆盖配置:")
  123. print(" --max-concurrent 10 # 最大并发数")
  124. print(" --batch-size 20 # 批处理大小")
  125. print(" --timeout 60 # 验证超时时间")
  126. print("\n或者在代码中修改配置:")
  127. print(" SCHEMA_TOOLS_CONFIG['sql_validation']['max_concurrent_validations'] = 10")
  128. def print_usage_examples():
  129. """打印使用示例"""
  130. print("=" * 60)
  131. print("SQL验证器命令行使用示例")
  132. print("=" * 60)
  133. examples = [
  134. {
  135. "title": "基本验证",
  136. "command": """python -m schema_tools.sql_validator \\
  137. --db-connection "postgresql://user:pass@localhost:5432/dbname" \\
  138. --input-file ./qs_data.json"""
  139. },
  140. {
  141. "title": "指定输出目录",
  142. "command": """python -m schema_tools.sql_validator \\
  143. --db-connection "postgresql://user:pass@localhost:5432/dbname" \\
  144. --input-file ./qs_data.json \\
  145. --output-dir ./reports"""
  146. },
  147. {
  148. "title": "调整性能参数",
  149. "command": """python -m schema_tools.sql_validator \\
  150. --db-connection "postgresql://user:pass@localhost:5432/dbname" \\
  151. --input-file ./qs_data.json \\
  152. --max-concurrent 10 \\
  153. --batch-size 20 \\
  154. --timeout 60"""
  155. },
  156. {
  157. "title": "预检查模式",
  158. "command": """python -m schema_tools.sql_validator \\
  159. --db-connection "postgresql://user:pass@localhost:5432/dbname" \\
  160. --input-file ./qs_data.json \\
  161. --dry-run"""
  162. },
  163. {
  164. "title": "详细日志",
  165. "command": """python -m schema_tools.sql_validator \\
  166. --db-connection "postgresql://user:pass@localhost:5432/dbname" \\
  167. --input-file ./qs_data.json \\
  168. --verbose \\
  169. --log-file validation.log"""
  170. }
  171. ]
  172. for example in examples:
  173. print(f"\n{example['title']}:")
  174. print(example['command'])
  175. async def main():
  176. """主函数"""
  177. # 设置日志
  178. setup_logging(verbose=True)
  179. print("Schema Tools SQL验证器示例")
  180. print("请选择要运行的示例:")
  181. print("1. 基本SQL验证示例")
  182. print("2. 真实数据验证示例")
  183. print("3. 配置选项演示")
  184. print("4. 命令行使用示例")
  185. print("0. 退出")
  186. try:
  187. choice = input("\n请输入选择 (0-4): ").strip()
  188. if choice == "1":
  189. await example_basic_validation()
  190. elif choice == "2":
  191. await example_with_real_data()
  192. elif choice == "3":
  193. await example_configuration_demo()
  194. elif choice == "4":
  195. print_usage_examples()
  196. elif choice == "0":
  197. print("退出示例程序")
  198. else:
  199. print("无效选择")
  200. except KeyboardInterrupt:
  201. print("\n\n用户中断,退出程序")
  202. except Exception as e:
  203. print(f"\n示例执行失败: {e}")
  204. if __name__ == "__main__":
  205. asyncio.run(main())