agentleFS
totvstotvs/engpro-advpl-tlpp-skills/skills/advpl-tlpp/sql-code-review/SKILL.md

sql-code-review

Skill138 starsChanged 6 months agoMITView on GitHub

Universal SQL code review assistant that performs comprehensive security, maintainability, and code quality analysis across SQL databases (PostgreSQL, SQL Server, Oracle). Focuses on SQL injection prevention, access control, code standards, and anti-pattern detection. Complements SQL optimization prompt for complete development coverage. Use when user says "review SQL", "SQL security audit", "SQL anti-patterns", "check SQL quality".

2 other projects use a version of this, 2 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use the Issue Template (below) for each finding.

    line 86
  2. Distinctive:

    [Performance Optimization]: Add missing indexes or optimize queries

    line 156
  3. Very specific:

    When reviewing SQL in the TOTVS Protheus ecosystem — whether embedded SQL (preferring FWExecStatement, with legacy TCQuery / TCSqlExec …

    line 167
  4. Distinctive:

    Every query against a Protheus table must include these filters unless there is an explicit reason to omit them:

    line 194
  5. Very specific:

    Check available indexes: before choosing DbSetOrder() — using the wrong order leads to full scans

    line 291
  6. Very specific:

    Composite indexes: should be leveraged in WHERE clauses matching left-to-right column order

    line 293

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SQL Code Review

Perform a thorough SQL code review of ${selection} (or entire project if no selection) focusing on security, performance, maintainability, and database best practices.

Review Categories

🔒 Security

  • SQL Injection Prevention — all user inputs must be parameterized; no string concatenation in queries
  • Access Control — principle of least privilege, role-based permissions, schema security
  • Data Protection — avoid SELECT * on sensitive tables, enforce audit logging and data masking

⚡ Performance

  • Query Structure — eliminate SELECT DISTINCT *, prefer explicit JOINs over comma-separated FROM
  • Index Strategy — verify indexes for WHERE/JOIN columns, flag over-indexing and unused indexes
  • Join Optimization — correct join types, optimal join order, no accidental Cartesian products
  • Aggregation — replace correlated subqueries with JOIN/GROUP BY when possible

🛠️ Code Quality

  • Formatting — consistent uppercase keywords, aligned columns, proper indentation
  • Naming — descriptive table/column names, no reserved words as identifiers, consistent casing
  • Schema Design — appropriate normalization, optimal data types, proper constraints and defaults

🗄️ Database Compatibility

  • ANSI SQL first — use COALESCE, CASE WHEN, ANSI JOINs, FETCH FIRST for portability
  • Database-specific idioms — leverage engine-specific features (JSONB, COLUMNSTORE, sequences) where appropriate

Protheus note: Use ChangeQuery() to automatically translate SQL syntax to the active database dialect. BeginSQL/EndSQL calls ChangeQuery() automatically.


Bundled Reference Files

This skill uses progressive disclosure. The SKILL.md body covers the review workflow, category definitions, checklist, and output format. Detailed code examples, anti-patterns, and database-specific best practices are in the references/ directory — read them on demand based on the scenario:

Reference FileWhen to ReadContent
references/sql-security-patterns.mdReviewing security concerns — SQL injection, access control, data protection, or dynamic SQLInjection examples, parameterization patterns, access control checklist, data masking examples
references/sql-performance-and-quality-patterns.mdReviewing query performance, code style, anti-patterns, or testing strategiesQuery optimization examples, JOIN/aggregation patterns, N+1 and DISTINCT anti-patterns, formatting standards, data integrity checks
references/database-specific-best-practices.mdReviewing SQL targeting a specific database (PostgreSQL, SQL Server, Oracle) or cross-database portabilityANSI SQL cross-database patterns, PostgreSQL (JSONB, GIN), SQL Server (NVARCHAR, COLUMNSTORE), Oracle (sequences, VARCHAR2)

Also refer to references/sonarqube-rules-reference.md for the complete SonarQube rules reference shared across skills.


Review Workflow

Step 1: Identify Scope and Database

Determine from the user's request:

  • Which SQL files or queries to review
  • Target database engine (PostgreSQL, SQL Server, Oracle, or multi-database)
  • Review focus (security, performance, quality, or full review)

Step 2: Load Relevant References

Based on the review focus and database, read the appropriate reference files:

  • Security focus → read references/sql-security-patterns.md
  • Performance / quality focus → read references/sql-performance-and-quality-patterns.md
  • Database-specific review → read references/database-specific-best-practices.md
  • Full review → read all three reference files

Step 3: Analyze Code Against Checklist

Walk through each category in the checklist below. For each finding, classify priority as CRITICAL, HIGH, MEDIUM, or LOW.

Step 4: Generate Report

Use the Issue Template (below) for each finding. Group findings by category and sort by priority.


SQL Review Checklist

Security

  • All user inputs are parameterized
  • No dynamic SQL construction with string concatenation
  • Appropriate access controls and permissions
  • Sensitive data is properly protected
  • SQL injection attack vectors are eliminated

Performance

  • Indexes exist for frequently queried columns
  • No unnecessary SELECT * statements
  • JOINs are optimized and use appropriate types
  • WHERE clauses are selective and use indexes
  • Subqueries are optimized or converted to JOINs

Code Quality

  • Consistent naming conventions
  • Proper formatting and indentation
  • Meaningful comments for complex logic
  • Appropriate data types are used
  • Error handling is implemented

Schema Design

  • Tables are properly normalized
  • Constraints enforce data integrity
  • Indexes support query patterns
  • Foreign key relationships are defined
  • Default values are appropriate

Review Output Format

Issue Template

## [PRIORITY] [CATEGORY]: [Brief Description]

**Location**: [Table/View/Procedure name and line number if applicable]
**Issue**: [Detailed explanation of the problem]
**Security Risk**: [If applicable - injection risk, data exposure, etc.]
**Performance Impact**: [Query cost, execution time impact]
**Recommendation**: [Specific fix with code example]

**Before**:
```sql
-- Problematic SQL

After:

-- Improved SQL

Expected Improvement: [Performance gain, security benefit]


### Summary Assessment
- **Security Score**: [1-10] - SQL injection protection, access controls
- **Performance Score**: [1-10] - Query efficiency, index usage
- **Maintainability Score**: [1-10] - Code quality, documentation
- **Schema Quality Score**: [1-10] - Design patterns, normalization

### Top 3 Priority Actions
1. **[Critical Security Fix]**: Address SQL injection vulnerabilities
2. **[Performance Optimization]**: Add missing indexes or optimize queries
3. **[Code Quality]**: Improve naming conventions and documentation

Focus on providing actionable, database-agnostic recommendations while highlighting platform-specific optimizations and best practices.

> **Cross-Database Review Tip:** When reviewing Protheus SQL, verify the query is compatible with all three supported databases (PostgreSQL, MSSQL, Oracle). Use `ChangeQuery()` for automatic SQL dialect translation, or `TCGetDB()` for runtime DB detection when DB-specific logic is unavoidable.

---

## Protheus SQL Patterns

When reviewing SQL in the TOTVS Protheus ecosystem — whether embedded SQL (preferring `FWExecStatement`, with legacy `TCQuery` / `TCSqlExec` calls), Workarea-based access, or standalone queries — apply the following additional checks.

### Table and Field Naming Conventions

Protheus uses standardized short-name tables and fields managed through the data dictionary:

| Alias | Module | Description |
|-------|--------|-------------|
| SA1 | All | Customers |
| SA2 | All | Suppliers |
| SB1 | SIGAEST | Products |
| SC5 | SIGAFAT | Sales Orders (header) |
| SC6 | SIGAFAT | Sales Orders (items) |
| SD1 | SIGACOM | Purchase document items |
| SD2 | SIGAFAT | Sales document items |
| SE1 | SIGAFIN | Accounts Receivable |
| SE2 | SIGAFIN | Accounts Payable |
| SF1 | SIGACOM | Incoming invoices (header) |
| SF2 | SIGAFAT | Outgoing invoices (header) |
| SX2 | System | Table dictionary |
| SX3 | System | Field dictionary |
| SIX | System | Index dictionary |

Fields follow the pattern `<prefix>_<name>`, e.g., `A1_COD` (customer code), `A1_NOME` (customer name), `D1_DOC` (document number).

### Mandatory Filters for Protheus Tables

Every query against a Protheus table **must** include these filters unless there is an explicit reason to omit them:

```sql
-- BAD: Missing mandatory filters — returns deleted records and all branches
SELECT A1_COD, A1_NOME FROM SA1010

-- GOOD: Proper Protheus query with mandatory filters
SELECT A1_COD, A1_NOME
FROM SA1010
WHERE D_E_L_E_T_ = ' '
  AND A1_FILIAL  = '01'
FilterPurpose
D_E_L_E_T_ = ' 'Excludes logically deleted records (soft delete). Always required.
<prefix>_FILIAL = cFilAntMulti-branch filter (tenant isolation). Required unless deliberately querying across branches.

Workarea vs. Embedded SQL Decision Matrix

ScenarioRecommended Approach
Single-record lookup by index keyWorkarea (DbSelectArea / DbSetOrder / DbSeek)
Sequential processing of a filtered setWorkarea with While loop
Complex joins across multiple tablesEmbedded SQL via FWExecStatement
Aggregate queries (SUM, COUNT, AVG)Embedded SQL via FWExecStatement (use :ExecScalar() for single values)
Bulk INSERT/UPDATE/DELETE operationsFWExecStatement + TCSqlExec(oStatement:GetFixQuery())
Reports with heavy filteringEmbedded SQL via FWExecStatement, or temporary tables via TCSqlExec

Workarea Access Review Checklist

// GOOD: Complete workarea access pattern
DbSelectArea("SA1")
DbSetOrder(1)           // Set the index defined in SIX
If DbSeek(FWxFilial("SA1") + cCodCli)
  // process record...
EndIf
  • DbSelectArea() called before any workarea operation
  • DbSetOrder() set to the correct SIX index
  • DbSeek() includes branch prefix via FWxFilial()
  • Write operations wrapped with RecLock() / MsUnlock()
  • While loops include !Eof() and SA1->(DbSkip()) pattern

Embedded SQL Review Checklist

// GOOD: Parameterized embedded SQL via FWExecStatement (DB-side bind, cacheable)
Local cQuery as Character
Local oExec  as Object
Local cAlias as Character

cQuery := "SELECT A1_COD, A1_NOME "
cQuery += "FROM " + RetSqlName("SA1") + " SA1 "
cQuery += "WHERE D_E_L_E_T_ = ? "
cQuery += "AND A1_FILIAL = ? "
cQuery += "AND A1_COD = ? "

oExec := FWExecStatement():New(ChangeQuery(cQuery))
oExec:SetString(1, ' ')
oExec:SetString(2, FWxFilial("SA1"))
oExec:SetString(3, cCodCli)

cAlias := oExec:OpenAlias("QRY_CLI")
// ... consume (cAlias)->A1_COD / A1_NOME ...
(cAlias)->(DbCloseArea())
oExec:Destroy()
  • Uses RetSqlName() to get the physical table name (handles company suffix)
  • Includes D_E_L_E_T_ = ' ' filter
  • Includes branch filter via FWxFilial()
  • Temporary alias is closed after use (QRY_CLI->(DbCloseArea()))
  • String values are sanitized to prevent SQL injection (use FWExecStatement — no raw user input concatenated)

Common Protheus SQL Anti-Patterns

Anti-PatternProblemFix
Missing D_E_L_E_T_ filterReturns deleted recordsAlways add D_E_L_E_T_ = ' '
SELECT * on Protheus tablesProtheus tables have many system fields; wastes bandwidthSelect only needed columns
Full table scan on SD1/SD2/SE1/SE2These transactional tables can have millions of rowsUse indexed columns in WHERE clause
Missing %nolock% on SQL ServerCauses lock escalation on read queriesAdd %nolock% hint: FROM SA1010 WITH (%nolock%) — the %nolock% DBAccess macro translates to WITH (NOLOCK) on MSSQL and is silently ignored on PostgreSQL/Oracle (MVCC)
Concatenating user input into SQLSQL injection vulnerabilityUse FWExecStatement or TcGenQry2 to parameterize queries
Not closing temporary aliasesResource leak, workarea pollutionAlways QRY->(DbCloseArea()) after use
Hardcoding table suffix (e.g., SA1010)Breaks in multi-company environmentsUse RetSqlName("SA1")
Creating procedures directly in sourceProhibited — violates SonarQube rulesUse SPManager for procedure management
Direct queries without evaluation (Cloud)Queries may not be Cloud-compatibleEvaluate Cloud impact; prefer framework APIs where available
Using SE5 table directlySE5 is deprecatedUse FKx family functions + ExecAuto
Using IIF in SQL or AdvPL expressionsProhibited for clean codeReplace with CASE WHEN (SQL) or If/Else/EndIf (AdvPL)

SIX Index Dictionary Awareness

Protheus indexes are defined in the SIX dictionary. When writing queries or using workareas:

  • Check available indexes before choosing DbSetOrder() — using the wrong order leads to full scans
  • Index 1 is typically: branch + primary key (e.g., A1_FILIAL + A1_COD + A1_LOJA)
  • Composite indexes should be leveraged in WHERE clauses matching left-to-right column order
  • Never create ad-hoc indexes in production without SIX registration

Protheus SQL Security

  • Never concatenate raw user input into SQL strings — use FWExecStatement or TcGenQry2 for parameterized queries
  • Never expose table structure in error messages returned to the client
  • Validate input length against SX3 field size before inserting/updating
  • Use FWExecView for user-facing queries when possible (respects field-level security)

Refer to references/sonarqube-rules-reference.md for the complete SonarQube rules reference.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.