DEV Community

Peter + AI
Peter + AI

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)