Writing PL/SQL
Announce at start: "Using pythia-write — mining the codebase's conventions first."
Phase: Learn → Do — mine the neighbours first, then draft; landing it is pythia-apply's job
Before anything: if the project has .pythia/conventions.md, read it —
house rules there outrank every generic pattern below, and
pythia conventions shows the naming patterns the apply preview will check. If the project has none and the developer
describes a house style, offer pythia conventions --init — captured once,
it applies to every future session instead of being re-explained.
A codebase with thousands of procedures has already decided how procedures
look. Your job is to write one that a maintainer cannot tell from the
existing ones — not to introduce a better style.
The Workflow
- Find the models.
pythia similar <NEW_NAME> ranks existing programs
sharing name tokens; the MATCHED_TOKENS column says why. Open the top
two or three with pythia src and imitate: naming, parameter prefixes,
cursor style, error handling, comment style.
- Anchor the signatures. For every program you call:
pythia args NAME — real parameter names, order, types, defaults. Never
guess a signature from memory of similar code.
- Anchor the types. For every table you touch:
pythia cols TABLE —
then declare variables with %TYPE / %ROWTYPE against those columns
instead of copying the current type by hand. The declaration then
survives column changes.
- Write the file. Rules the write path enforces — follow them here:
- One object per file. Package spec and body are two files.
- End the file with the PL/SQL block's
; and a final line holding /.
- Name the object unqualified, or qualified with the exact schema the
connection targets — a mismatch is refused at apply time.
- Non-ASCII string literals (Vietnamese messages, any accented text):
never paste raw — run
pythia unistr "<text>" and use the printed
unistr('...'), so the text survives every client/DB charset exactly.
- Check yourself before handing off. Reread against
reference/patterns.md — cursor and bulk patterns, exception discipline,
bind variables, commit ownership.
Scope is the developer's sentence, not yours
Change ONLY the objects the developer named. When impact shows dependents
that will break, you REPORT them with the list and a proposal — fixing
them is a separate request that needs its own explicit approval. "While I
was there" is how an asked-for table edit becomes seven unasked procedure
rewrites.
Conventions outrank preferences
If the codebase writes explicit cursors where you would write FOR r IN,
write explicit cursors. If its parameter prefixes look dated, use them
anyway. A mixed-style codebase is worse than a consistently dated one —
propose style changes to the developer separately, never silently.
When NOT to use this skill
- Understanding existing code →
pythia-explore.
- Measuring what a change breaks →
pythia-impact (must already be done).
- Landing the file on the database →
pythia-apply, always — never
run-sql, never sqlplus.
1---2name: pythia-write3description: Use when writing or modifying PL/SQL source - a procedure, function, package, trigger, or view - after impact is known and the spec is settled (open decisions go through pythia-spec first). The codebase's conventions already exist; copy them instead of inventing style, and anchor every type to the database's reality.4---56# Writing PL/SQL78**Announce at start:** "Using pythia-write — mining the codebase's conventions first."910**Phase:** Learn → Do — mine the neighbours first, then draft; landing it is pythia-apply's job1112**Before anything:** if the project has `.pythia/conventions.md`, read it —13house rules there outrank every generic pattern below, and14`pythia conventions` shows the naming patterns the apply preview will check. If the project has none and the developer15describes a house style, offer `pythia conventions --init` — captured once,16it applies to every future session instead of being re-explained.1718A codebase with thousands of procedures has already decided how procedures19look. Your job is to write one that a maintainer cannot tell from the20existing ones — not to introduce a better style.2122## The Workflow23241. **Find the models.** `pythia similar <NEW_NAME>` ranks existing programs25 sharing name tokens; the `MATCHED_TOKENS` column says why. Open the top26 two or three with `pythia src` and imitate: naming, parameter prefixes,27 cursor style, error handling, comment style.282. **Anchor the signatures.** For every program you call:29 `pythia args NAME` — real parameter names, order, types, defaults. Never30 guess a signature from memory of similar code.313. **Anchor the types.** For every table you touch: `pythia cols TABLE` —32 then declare variables with `%TYPE` / `%ROWTYPE` against those columns33 instead of copying the current type by hand. The declaration then34 survives column changes.354. **Write the file.** Rules the write path enforces — follow them here:36 - **One object per file.** Package spec and body are two files.37 - End the file with the PL/SQL block's `;` and a final line holding `/`.38 - Name the object unqualified, or qualified with the exact schema the39 connection targets — a mismatch is refused at apply time.40 - **Non-ASCII string literals** (Vietnamese messages, any accented text):41 never paste raw — run `pythia unistr "<text>"` and use the printed42 `unistr('...')`, so the text survives every client/DB charset exactly.435. **Check yourself before handing off.** Reread against44 `reference/patterns.md` — cursor and bulk patterns, exception discipline,45 bind variables, commit ownership.4647## Scope is the developer's sentence, not yours4849Change ONLY the objects the developer named. When impact shows dependents50that will break, you REPORT them with the list and a proposal — fixing51them is a separate request that needs its own explicit approval. "While I52was there" is how an asked-for table edit becomes seven unasked procedure53rewrites.5455## Conventions outrank preferences5657If the codebase writes explicit cursors where you would write `FOR r IN`,58write explicit cursors. If its parameter prefixes look dated, use them59anyway. A mixed-style codebase is worse than a consistently dated one —60propose style changes to the developer separately, never silently.6162## When NOT to use this skill6364- Understanding existing code → `pythia-explore`.65- Measuring what a change breaks → `pythia-impact` (must already be done).66- Landing the file on the database → `pythia-apply`, always — never67 `run-sql`, never `sqlplus`.