Working with Uniface can be challenging, especially when dealing with data deletion operations. Today, I'm diving deep into the erase command in Uniface 10.4 - one of the most powerful yet potentially dangerous operations you can perform! ๐ฃ
Note: This post is based on the official Uniface Documentation 10.4, and I had some assistance from AI to organize this comprehensive guide.
๐ฏ What is the Erase Command?
The erase
statement in Uniface activates delete or deleteUp triggers for all occurrences in a component. Think of it as the nuclear option for data deletion - it's powerful, but you need to handle it with care! โ ๏ธ
๐ Basic Syntax
erase{/e {Entity}}
๐ ๏ธ Key Features & Qualifiers
The /e Qualifier
The /e
qualifier is your friend when you need to target specific entities:
- โ Erases all occurrences of the specified Entity
- ๐ Includes inner entities if Cascading Delete relationship exists
- ๐ฏ Provides more granular control over what gets deleted
๐ Return Values You Need to Know
Understanding return values is crucial for error handling! Here are the most important ones:
Value | Meaning | Action Required |
---|---|---|
0 | โ Success | Data successfully erased |
1 | ๐ซ Not allowed | Check component activation mode |
-2 | ๐ญ Empty table | No data to erase |
-6 | ๐พ I/O error | Check disk space, permissions, constraints |
-11 | ๐ Already locked | Wait or handle concurrency |
๐ Relationship Handling
The erase command behaves differently based on your entity relationships:
๐ Cascading Delete
Related entities get deleted automatically - like a domino effect! ๐ฏ
๐ซ Nullify Delete
Foreign keys in related entities become null - safer but requires cleanup ๐งน
โ Restricted Delete
Erase fails if related entities exist - your safety net! ๐ก๏ธ
๐ก Pro Tips for Safe Usage
1. Always Ask for Confirmation
trigger erase ; of component
if ($totocc(CUSTOMER) >= 1)
askmess "%%$totocc(CUSTOMER) occurrences. Erase them all?"
if ($status = 0)
return
endif
endif
erase
2. Handle Errors Gracefully
if ($status < 0)
message "Erase error; see message frame"
rollback
else
if ($status = 1)
message "Erase is not allowed"
else
message "Erase was successful"
commit
endif
endif
3. For Complete Database Deletion
Need to delete everything? Use this pattern:
setocc "*",-1 ; Retrieve all occurrences
erase ; Then erase them
โ ๏ธ Critical Warning
The erase command only deletes fetched occurrences! If you haven't retrieved data into your component, it won't be deleted. This is both a safety feature and a potential gotcha! ๐ญ
๐ Best Practices
- ๐ Always validate data before erasing
- ๐พ Use transactions (commit/rollback)
- ๐ Log important deletions
- ๐งช Test thoroughly in development
- ๐ฅ Consider user permissions and roles
- ๐ Monitor performance with large datasets
๐ฌ Conclusion
The Uniface erase command is incredibly powerful for bulk data operations, but with great power comes great responsibility! ๐ท๏ธ Always implement proper safeguards, error handling, and user confirmations.
Remember: it's much easier to prevent accidental data loss than to recover from it! ๐ช
Happy coding, and may your erases be intentional! ๐ฏโจ
Top comments (0)