agentleFS
totvstotvs/engpro-advpl-tlpp-skills/skills/advpl-tlpp/query-builder/SKILL.md

query-builder

Skill138 starsChanged 6 months agoMITView on GitHub

Build optimized and safe SQL queries for Protheus tables. Automatically includes mandatory filters (D_E_L_E_T_, branch), suggests appropriate indexes from SIX patterns, generates both Embedded SQL (preferring FWExecStatement) and Workarea (DBSelectArea/DBSeek) versions, and warns about common Protheus SQL pitfalls. Use when user says 'build query', 'SQL for Protheus table', 'FWExecStatement', 'TCQuery', 'Workarea vs SQL'.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

10 rules. About 0 are common, 1 is distinctive, and 9 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 9

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when:

    line 21
  2. Very specific:

    Use RetSQLName("SA1") to get the correct physical table name dynamically.

    line 71
  3. Very specific:

    Never concatenate user input directly into SQL strings: Use FWExecStatement to parameterize all dynamic values.

    line 177
  4. Very specific:

    Always close opened aliases ((cAlias)->(DBCloseArea())) and call oStatement:Destroy().

    line 188
  5. Very specific:

    Best practice: Always use ANSI-compatible SQL (COALESCE, CASE WHEN, CONCAT, FETCH FIRST) or rely on ChangeQuery().

    line 261
  6. Very specific:

    Missing D_E_L_E_T_ filter: All Protheus queries must include D_E_L_E_T_ = ' ' (or equivalent %notDel%) to exclude logically deleted …

    line 269
  7. Very specific:

    Wrong physical table name: Use RetSQLName('alias') to get the physical table name.

    line 270
  8. Very specific:

    Index not being used: Ensure the WHERE clause column order matches the SIX index key expression.

    line 271
  9. Very specific:

    Branch/company filter missing: Use FWxFilial('alias') for the branch filter.

    line 272
  10. Very specific:

    SQL injection via macro-execution: Never use &(cExpr) to build SQL strings.

    line 273

≈ “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

Protheus Query Builder

Overview

Build correct, safe, and optimized SQL queries for Protheus ERP tables. Protheus has unique database conventions — mandatory soft-delete filters, multi-branch filtering, Hungarian notation for fields, data dictionary-driven schemas, and specific index patterns — that every query must respect. This skill generates queries that follow these conventions and helps choose between Workarea access and Embedded SQL.

When to Use

Use this skill when:

  • Writing SQL queries against Protheus tables (SA1, SD1, SF2, etc.)
  • Building parameterized SQL queries (FWExecStatement, TCSqlExec) in AdvPL/TLPP
  • Optimizing existing queries for Protheus-specific patterns
  • Deciding between Workarea access and Embedded SQL
  • Ensuring mandatory filters are not missing
  • Generating safe queries that prevent SQL injection

Bundled Reference Files

This skill uses progressive disclosure. The SKILL.md body covers conventions, decision logic, the checklist, and anti-patterns. Detailed code templates and cross-database reference tables are in the references/ directory — read them on demand based on the scenario:

Reference FileWhen to ReadContent
references/query-patterns-and-examples.mdGenerating query code — Workarea, Embedded SQL, multi-table JOINs, TCSqlExec updates, counting, or reviewing SQL injection prevention examplesFull code templates for all 5 query patterns, safe/unsafe FWExecStatement examples, LIKE clause parameterization
references/cross-database-compatibility.mdHandling cross-database concerns — ChangeQuery(), TCGetDB(), DBAccess macros, or translating functions between MSSQL / PostgreSQL / OracleChangeQuery and TCGetDB code examples, DBAccess macros table, cross-database function equivalents (9 operations × 4 dialects)

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


Protheus Database Conventions

Table Naming

Protheus table aliases follow the pattern XXN where XX is the module prefix and N is a sequence:

PrefixModuleExample Tables
SACustomers/VendorsSA1 (Customers), SA2 (Vendors), SA3 (Salespeople)
SBProductsSB1 (Products), SB2 (Stock Balances), SB5 (Supplements)
SCPurchasingSC1 (Purchase Requests), SC5 (Sales Orders Header), SC6 (Sales Orders Items), SC7 (Purchase Orders)
SDDocumentsSD1 (Incoming Invoices Items), SD2 (Outgoing Invoices Items), SD3 (Internal Movements)
SEFinancialSE1 (Accounts Receivable), SE2 (Accounts Payable), SE5 (Cash Movements)
SFInvoicesSF1 (Incoming Invoices Header), SF2 (Outgoing Invoices Header)
SXData DictionarySX1 (Parameters), SX2 (Tables), SX3 (Fields), SX5 (Lookup Tables), SX6 (System Parameters), SX7 (Triggers), SIX (Indexes)
ZZCustomZZ1-ZZZ (Custom tables created by customer, also Z01-Z99, ZA0-ZAZ, etc.)

Physical Table Names

The physical table name in the database appends the company code:

AliasPhysical NameRule
SA1SA1010Alias + Company ("01") + Branch padding ("0")
SD1SD1010Same pattern

Use RetSQLName("SA1") to get the correct physical table name dynamically.

Field Naming

Fields follow the pattern XX_FIELD where XX matches the table alias prefix:

TableFieldMeaning
SA1A1_CODCustomer code
SA1A1_NOMECustomer name
SD1D1_DOCInvoice document number
SD1D1_TOTALInvoice item total
SF2F2_DOCOutgoing invoice number

Mandatory System Fields

Every query against Protheus tables MUST include these filters:

FieldFilterPurpose
D_E_L_E_T_= ' ' (single space)Soft-delete flag. Records with '*' are logically deleted
XX_FILIAL= cFilAnt or = FWxFilial("XXX")Multi-branch filter
-- ALWAYS include both filters
SELECT A1_COD, A1_NOME
FROM SA1010 SA1
WHERE SA1.D_E_L_E_T_ = ' '
  AND SA1.A1_FILIAL = '01'

Warning: Omitting D_E_L_E_T_ will return deleted records. Omitting the branch filter will return records from all branches, which is usually incorrect and a security risk.


Query Patterns

This skill supports five query patterns. Read references/query-patterns-and-examples.md for the full code templates.

PatternApproachWhen to Use
1 — Simple Select (Workarea)DbSelectArea + DbSeekSingle record lookup by existing index, record-by-record operations
2 — Simple Select (Embedded SQL)FWExecStatement + OpenAlias / ExecScalarAggregations, complex filters, no suitable index for Workarea
3 — Multi-Table JoinEmbedded SQL with INNER JOINCross-table reporting, invoice details with product names
4 — INSERT/UPDATE (TCSqlExec)FWExecStatement + TCSqlExecDirect SQL writes (bypasses data dictionary triggers)
5 — Counting RecordsFWExecStatement:ExecScalar() on SELECT COUNT(*)Record counts with filters

Important: Pattern 4 (TCSqlExec) bypasses data dictionary validations and triggers. Prefer Workarea RecLock/MsUnlock for standard CRUD operations.


Workarea vs. Embedded SQL Decision Matrix

CriterionWorkarea (DBSeek)Embedded SQL (FWExecStatement)
Single record lookup by keyBest choiceAcceptable
Sequential scan by indexBest choiceAcceptable
Complex multi-table joinsPoor (requires nested seeks)Best choice
Aggregations (SUM, COUNT)Very poorBest choice
Large result setsBetter memory controlGood (but watch alias handling)
Record locking for updateRequiredUse FWExecStatement + TCSqlExec (bypasses triggers)
Performance (key-based)FastestSlight overhead
Data dictionary trigger executionAutomaticNot triggered
Index requirementMust have suitable SIX indexAny column
Code readability for complex queriesPoorBest choice

Rule of thumb: Use Workarea for CRUD operations on single records. Use Embedded SQL for reporting, aggregation, and complex joins.


Index Awareness (SIX Dictionary)

Protheus indexes are defined in the SIX table. The first index of each table (order 1) is typically the primary key.

Common Index Patterns

TableOrderKey ExpressionUse Case
SA11A1_FILIAL + A1_COD + A1_LOJAPrimary key — customer lookup
SA13A1_FILIAL + A1_CGCFind customer by tax ID
SA21A2_FILIAL + A2_COD + A2_LOJAPrimary key — vendor lookup
SB11B1_FILIAL + B1_CODPrimary key — product lookup
SD11D1_FILIAL + D1_DOC + D1_SERIE + D1_FORNECE + D1_LOJA + D1_COD + D1_ITEMIncoming invoice item
SD21D2_FILIAL + D2_DOC + D2_SERIE + D2_CLIENTE + D2_LOJA + D2_COD + D2_ITEMOutgoing invoice item
SE11E1_FILIAL + E1_PREFIXO + E1_NUM + E1_PARCELA + E1_TIPOAccounts receivable
SF21F2_FILIAL + F2_DOC + F2_SERIEOutgoing invoice header

Using Indexes in Embedded SQL

For SQL Server, use the %nolock% DBAccess macro on read queries. This macro translates to WITH (NOLOCK) on MSSQL and is silently ignored on PostgreSQL/Oracle (MVCC), making it safe to use in cross-database code:

SELECT A1_COD, A1_NOME
FROM SA1010 SA1 WITH (%nolock%)
WHERE SA1.D_E_L_E_T_ = ' '
  AND SA1.A1_FILIAL = '01'
  AND SA1.A1_COD = '000001'
  AND SA1.A1_LOJA = '01'

Tip: Always order WHERE clauses to match the index key expression order for the query optimizer.


SQL Injection Prevention

Never concatenate user input directly into SQL strings. Use FWExecStatement to parameterize all dynamic values.

The safe pattern:

  1. Build the SQL with ? placeholders for every dynamic value (filter, branch, deletion flag).
  2. Wrap it in ChangeQuery() for cross-database compatibility.
  3. Instantiate with FWExecStatement():New(cQuery) (or FWPreparedStatement on libs older than 20211116).
  4. Bind values 1..N with SetString(), SetNumeric(), SetDate(), SetBoolean(), SetIn().
  5. Execute:
    • SELECT returning a cursor → cAlias := oStatement:OpenAlias([cAlias][, cLifeTime, cTimeout])
    • SELECT returning a single scalar → xValue := oStatement:ExecScalar(cColumn[, cLifeTime, cTimeout])
    • INSERT / UPDATE / DELETE → TCSqlExec(oStatement:GetFixQuery())
  6. Always close opened aliases ((cAlias)->(DBCloseArea())) and call oStatement:Destroy().

For LIKE clauses, build the % wildcard on the AdvPL side ("%" + cSearch + "%") and bind the whole string as a single ? parameter — this avoids cross-DB concat operator differences.

Do not use SetUnsafe() with user input. It bypasses bind safety and reintroduces SQL injection risk; reserve it for identifiers built from constants.

Cache (optional): OpenAlias / ExecScalar accept cLifeTime and cTimeout (seconds, as character) to reuse a cached result from DBAccess for hot, repeatedly executed queries.

Read references/query-patterns-and-examples.md for complete safe/unsafe code examples, the full method reference, and LIKE-clause examples.


Common Anti-Patterns

Anti-PatternProblemFix
Missing D_E_L_E_T_ filterReturns deleted recordsAlways add WHERE D_E_L_E_T_ = ' '
Missing branch filterReturns records from all branchesAlways add AND XX_FILIAL = '...'
SELECT * on Protheus tablesReturns dozens of system fields, slowList only the fields you need
Full table scan on SD1/SD2Millions of rows, very slowUse indexed columns in WHERE
Not using %nolock%Lock contention on read queriesAdd WITH (%nolock%) to SELECT tables — cross-DB safe (ignored on PostgreSQL/Oracle)
D_E_L_E_T_ filter on JOINs missingJoined table returns deleted recordsAdd D_E_L_E_T_ = ' ' to every table in JOIN
Hardcoded company/branch codesBreaks in multi-company environmentsUse FWxFilial(), RetSQLName()
Not closing query aliases / FWExecStatementMemory leak, alias exhaustionAlways call (cAlias)->(DBCloseArea()) and oStatement:Destroy()
Macro execution in SQLSQL injection riskUse FWExecStatement for parameterized values
Missing RetSQLName()Wrong physical table name in multi-companyAlways use RetSQLName("XXX")
Creating procedures in sourceProhibited by SonarQubeUse SPManager for procedure management
Using IIF in SQL expressionsProhibited — use CASE WHEN or If/Else in AdvPL logicReplace IIF() with If/Else/EndIf or SQL CASE WHEN
Direct access to SIX/SX2/SX3 via DbSelectAreaProhibited metadata accessUse RetSqlName() for SX2, FWSX3Util() for SX3, standard APIs for SIX
GetMV/ExistBlock inside loopsPerformance degradationCache result in a variable before the loop

Query Building Checklist

Mandatory

  • D_E_L_E_T_ = ' ' included for every table in the query
  • Branch filter (XX_FILIAL) included for every table
  • Physical table name obtained via RetSQLName("XXX")
  • User-provided values parameterized with FWExecStatement (preferred over raw TCQuery)
  • Result alias name generated with GetNextAlias() and opened via FWExecStatement:OpenAlias()
  • Opened alias closed with DBCloseArea() in all code paths (including errors) and FWExecStatement:Destroy() is called

Performance

  • Only required fields listed (no SELECT *)
  • WHERE clause order matches index key expression
  • %nolock% hint used for SQL Server read queries
  • Pagination used for large result sets
  • JOINs reference indexed columns

Safety

  • No macro-execution (&cExpr) in SQL strings
  • No hardcoded company/branch codes
  • Error handling wraps the query execution (Try-Catch)
  • Workarea area saved and restored with GetArea()/RestArea()
  • No IIF() in query construction or expressions — use If/Else/EndIf or SQL CASE WHEN
  • No CREATE PROCEDURE in source — use SPManager
  • GetMV()/ExistBlock() results cached before use in loops

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


Cross-Database Compatibility

Protheus supports PostgreSQL, MSSQL Server, and Oracle. All generated SQL must work across all three, or use framework tools to handle differences:

  • ChangeQuery(cQuery) — translates SQL syntax to the active database dialect (TOP → LIMIT, etc.). BeginSQL/EndSQL calls it automatically.
  • TCGetDB() — returns "MSSQL", "ORACLE", or "POSTGRES" for DB-specific branching when ChangeQuery() is insufficient.
  • DBAccess macros — %nolock%, %notDel%, %table:XXX%, %Order:XXX% — translated per database in SQL strings.
  • Best practice: Always use ANSI-compatible SQL (COALESCE, CASE WHEN, CONCAT, FETCH FIRST) or rely on ChangeQuery().

Read references/cross-database-compatibility.md for full code examples, the DBAccess macros table, and the cross-database function equivalents reference (MSSQL vs PostgreSQL vs Oracle).


Troubleshooting

  • Missing D_E_L_E_T_ filter: All Protheus queries must include D_E_L_E_T_ = ' ' (or equivalent %notDel%) to exclude logically deleted records. Omitting this returns deleted rows.
  • Wrong physical table name: Use RetSQLName('alias') to get the physical table name. Hardcoded names like SA1010 will break across environments with different company codes.
  • Index not being used: Ensure the WHERE clause column order matches the SIX index key expression. Use %nolock% hint for SQL Server read-only queries to avoid lock contention.
  • Branch/company filter missing: Use FWxFilial('alias') for the branch filter. Hardcoding branch codes causes cross-branch data leakage.
  • SQL injection via macro-execution: Never use &(cExpr) to build SQL strings. Use FWExecStatement to parameterize all dynamic values in queries.

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.