DEV Community

Peter
Peter

Posted on

๐Ÿ—‘๏ธ Mastering the Uniface Erase Command: A Complete Guide

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}}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

3. For Complete Database Deletion

Need to delete everything? Use this pattern:

setocc "*",-1  ; Retrieve all occurrences
erase          ; Then erase them
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ 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)