返回顶部
s

sql-reflectSQL反射定位

快速定位 SQL 语句在 PHP/Laravel 代码中的触发位置,通过分析 SQL 结构反向追踪到具体的代码文件、方法和行号

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
107
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

sql-reflect

触发条件

当用户使用以下表述时触发此技能:

  • - 帮我查找一下这句 sql 语句是在哪个地方的代码触发的
  • 查询这个 SQL 代码位置
  • 这条 SQL 是从哪里调用的
  • 定位这条 SQL 的触发代码
  • 查找 SQL 触发位置

执行流程

1. 提取关键信息

从 SQL 语句中提取:

  • - 主表名:FROM 后的表名(如 pktransactionmeet_records)
  • 关联表名:JOIN、EXISTS 子句中的表名
  • 关键条件:WHERE 中的特殊字段组合
  • 查询特征:特殊结构(如嵌套 EXISTS、特定字段组合)

2. 搜索代码位置

使用 grep 工具执行以下搜索(按优先级):

  1. 1. 搜索表名(最精确)
- 搜索模式:pk_表名 - 文件类型:*.php - 搜索范围:app/
  1. 2. 搜索模型类名
- 转换规则:去掉 pk_ 前缀,转 PascalCase - 如:pktransactionmeet_records → TransactionMeetRecord
  1. 3. 搜索特殊字段组合
- 提取 WHERE 条件中的 2-3 个特征字段 - 搜索这些字段的组合出现位置

3. 分析搜索结果

对比 SQL 特征和代码:

  • - 字段匹配:SQL 中的字段是否在代码的 select/where 中出现
  • 条件匹配:WHERE 条件是否与代码一致
  • 结构匹配:JOIN/EXISTS 结构是否对应 whereHas/join 等
  • 参数匹配:绑定参数的位置和数量

4. 定位关联关系

查找涉及的模型关联定义:

  • - 搜索 function 关联名() 在 Models 目录
  • 分析 belongsTo/hasMany 等关联类型
  • 确认外键字段

5. 追踪调用链路

从找到的代码位置向上追踪:

  • - 哪个 Controller 调用了这个 Service
  • 哪个方法实例化了这个 Model
  • 完整的调用路径

输出格式

markdown

SQL 语句触发位置分析

📍 触发代码位置

文件路径: /path/to/file.php

方法: methodName(第 X 行开始)

具体代码段: 第 X-Y 行



🔍 SQL 生成逻辑

这段代码通过 [Laravel Eloquent 特性] 生成:

php
// 关键代码片段(10-20 行)



🔗 关联关系链

查询涉及的模型关联:

  1. 1. 关联名 - 模型类::关联方法()(第 X 行)

📋 生成的 SQL 结构

sql
-- SQL 结构说明



📂 调用链路

Controller.php:行号
↓ (调用说明)
Service.php:行号

Model::query()

最终 SQL



💡 使用场景

这个查询用于 [业务场景说明]

表名映射参考

表名模型类
pktransactionmeetrecordsTransactionMeetRecord
pkcustomer
Customers | | pkcustomercard | CustomerCard | | pkorderoffline | OrderOffline | | pkorderofflinerefundlogs | OrderOfflineRefundLogs | | pkorderonline | OrderOnline | | pktemplateactivity | TemplateActivity | | pk_guide | StoreGuide | | pk_store | Store | | pkcustomergoodsattributetag | CustomerGoodsAttributeTag | | pkguidecustomer | GuideCustomer | | pkpromotiongoods | PromotionGoods | | pkordergoods_online | OrderGoodsOnline | | pkordergoodsonlinerecords | OrderGoodsOnlineRecord |

SQL 结构对应

SQL 结构Laravel 写法
EXISTS (SELECT...)whereHas() / whereExists()
LEFT JOIN
leftJoin() / with() | | INNER JOIN | join() | | BETWEEN ... AND ... | whereBetween() | | IN (...) | whereIn() | | NOT EXISTS | whereDoesntHave() |

特殊字段组合

字段组合可能业务
businessno + sourcetype + ismetTransactionMeetRecord
orderno + refundorderno + is_valid
OrderOfflineRefundLogs | | customerid + firstconsumetime + secondconsume_time | CustomerCard | | guideid + customerid + relation_type | GuideCustomer | | attributepid + attributeid + num | CustomerGoodsAttributeTag |

注意事项

  1. 1. 优先使用精确搜索(完整表名),再使用模糊搜索
  2. 注意表名前缀(pk_)和模型命名规则
  3. 如果找到多个匹配位置,按匹配度排序并说明差异
  4. SQL 中的 ? 在代码中对应变量或请求参数
  5. 某些 SQL 可能由多个代码位置生成,需列出所有可能

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 sql-reflect-1775936500 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 sql-reflect-1775936500 技能

通过命令行安装

skillhub install sql-reflect-1775936500

下载

⬇ 下载 sql-reflect v1.0.0(免费)

文件大小: 2.69 KB | 发布时间: 2026-4-12 11:30

v1.0.0 最新 2026-4-12 11:30
Initial release of sql-reflect: locate PHP/Laravel code that triggers specific SQL queries.

- Extracts key info from SQL (tables, fields, conditions) to reverse-locate code.
- Uses prioritized grep searches for table/model names and field patterns in PHP files.
- Analyzes and matches SQL structures with Laravel methods (e.g., join, whereHas).
- Maps results to clear output: file/method/line, involved models/relations, calling chain, and use case.
- Includes detailed mapping tables for SQL-to-Laravel patterns and table-model correspondences.
- Designed to handle ambiguous cases and provide multiple possible source locations if needed.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部