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)