← Back to Skills Marketplace
willing-lin

sql-reflect

by will_lin · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
123
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install sql-reflect
Description
快速定位 SQL 语句在 PHP/Laravel 代码中的触发位置,通过分析 SQL 结构反向追踪到具体的代码文件、方法和行号
README (SKILL.md)

触发条件

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

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

执行流程

1. 提取关键信息

从 SQL 语句中提取:

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

2. 搜索代码位置

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

  1. 搜索表名(最精确)

    • 搜索模式:pk_表名
    • 文件类型:*.php
    • 搜索范围:app/
  2. 搜索模型类名

    • 转换规则:去掉 pk_ 前缀,转 PascalCase
    • 如:pk_transaction_meet_recordsTransactionMeetRecord
  3. 搜索特殊字段组合

    • 提取 WHERE 条件中的 2-3 个特征字段
    • 搜索这些字段的组合出现位置

3. 分析搜索结果

对比 SQL 特征和代码:

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

4. 定位关联关系

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

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

5. 追踪调用链路

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

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

输出格式

## SQL 语句触发位置分析

### 📍 触发代码位置

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

**方法:** `methodName`(第 X 行开始)

**具体代码段:** 第 X-Y 行

---

### 🔍 SQL 生成逻辑

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

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

🔗 关联关系链

查询涉及的模型关联:

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

📋 生成的 SQL 结构

-- SQL 结构说明

📂 调用链路

Controller.php:行号
  ↓ (调用说明)
Service.php:行号
  ↓
Model::query()
  ↓
最终 SQL

💡 使用场景

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


## 表名映射参考

| 表名 | 模型类 |
|------|--------|
| pk_transaction_meet_records | TransactionMeetRecord |
| pk_customer | Customers |
| pk_customer_card | CustomerCard |
| pk_order_offline | OrderOffline |
| pk_order_offline_refund_logs | OrderOfflineRefundLogs |
| pk_order_online | OrderOnline |
| pk_template_activity | TemplateActivity |
| pk_guide | StoreGuide |
| pk_store | Store |
| pk_customer_goods_attribute_tag | CustomerGoodsAttributeTag |
| pk_guide_customer | GuideCustomer |
| pk_promotion_goods | PromotionGoods |
| pk_order_goods_online | OrderGoodsOnline |
| pk_order_goods_online_records | OrderGoodsOnlineRecord |

## SQL 结构对应

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

## 特殊字段组合

| 字段组合 | 可能业务 |
|---------|---------|
| business_no + source_type + is_met | TransactionMeetRecord |
| order_no + refund_order_no + is_valid | OrderOfflineRefundLogs |
| customer_id + first_consume_time + second_consume_time | CustomerCard |
| guide_id + customer_id + relation_type | GuideCustomer |
| attribute_pid + attribute_id + num | CustomerGoodsAttributeTag |

## 注意事项

1. 优先使用精确搜索(完整表名),再使用模糊搜索
2. 注意表名前缀(pk_)和模型命名规则
3. 如果找到多个匹配位置,按匹配度排序并说明差异
4. SQL 中的 `?` 在代码中对应变量或请求参数
5. 某些 SQL 可能由多个代码位置生成,需列出所有可能
Usage Guidance
This skill is coherent and doesn't request credentials or installs, but it operates by reading your codebase. Before using it: 1) Confirm the agent running the skill has read access only to the repository (app/, Models) you want searched — avoid granting broad system access. 2) Ensure your environment has standard tools (grep) available, since SKILL.md assumes they exist. 3) Treat results as pointers: the skill may return multiple candidates or false positives (especially for dynamically built SQL, raw DB queries, or third-party packages), so review suggested file/method locations manually before making code changes. 4) Do not supply production DB credentials or run code the agent recommends without inspection. If you want stricter guarantees, ask the skill author to declare required binaries (grep) explicitly and to include exact search commands so you can review them ahead of time.
Capability Analysis
Type: OpenClaw Skill Name: sql-reflect Version: 1.0.0 The 'sql-reflect' skill is a developer utility designed to trace SQL queries back to their origin in a PHP/Laravel codebase. It uses standard search patterns (grep) to identify table names and model relationships within the 'app/' directory and provides mapping logic for specific database structures. No indicators of data exfiltration, malicious execution, or prompt injection were found.
Capability Assessment
Purpose & Capability
The name/description (locate SQL trigger points in PHP/Laravel) align with the instructions: extracting table/field patterns and searching app/ and Models for matching code. The mapping table and Laravel conventions used are appropriate for the stated goal.
Instruction Scope
The SKILL.md explicitly instructs using grep and searching the repository (app/, Models, etc.). The skill metadata did not list required binaries but the instructions assume filesystem access and availability of grep; the agent must have read access to the project files (app/ and Models) for the skill to work. This is reasonable but worth noting: the skill will read source files and may present multiple candidate locations that need human review.
Install Mechanism
Instruction-only skill with no install spec and no code files — nothing will be written to disk or downloaded during install.
Credentials
The skill declares no environment variables, credentials, or config paths and the instructions do not request any secrets. This is proportionate to the stated purpose.
Persistence & Privilege
always:false and normal invocation settings. The skill does not request persistent privileges or modify other skills/system settings.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install sql-reflect
  3. After installation, invoke the skill by name or use /sql-reflect
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
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.
Metadata
Slug sql-reflect
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is sql-reflect?

快速定位 SQL 语句在 PHP/Laravel 代码中的触发位置,通过分析 SQL 结构反向追踪到具体的代码文件、方法和行号. It is an AI Agent Skill for Claude Code / OpenClaw, with 123 downloads so far.

How do I install sql-reflect?

Run "/install sql-reflect" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is sql-reflect free?

Yes, sql-reflect is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does sql-reflect support?

sql-reflect is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created sql-reflect?

It is built and maintained by will_lin (@willing-lin); the current version is v1.0.0.

💬 Comments