DEV Community

Peter + AI
Peter + AI

Posted on

πŸ” Mastering Uniface's findkey Statement: A Developer's Guide to Key Existence Checking

As developers working with database applications, we often need to verify whether specific records exist before performing operations. In Uniface, the findkey statement provides an efficient way to check for key existence without the overhead of full data retrieval. This guide explores this powerful feature based on the official Uniface 10.4 documentation.

πŸš€ What is findkey?

The findkey statement is a specialized Uniface command that checks whether the values of fields that make up a specified key exist, either in the component or in the database. Unlike more complex retrieval operations, findkey performs no validationβ€”it simply checks for existence, making it incredibly fast and efficient.

πŸ“ Syntax and Parameters

The basic syntax is straightforward:

findkey {Entity, KeyNumber}
Enter fullscreen mode Exit fullscreen mode

Parameters:

Parameter Data Type Description
Entity String Name of the Entity where the key is to be located
KeyNumber Number Value identifying the key to locate (1 for primary key, 2+ for candidate keys)

🎯 Return Values

The findkey statement returns specific values in $status that tell you exactly what happened:

  • -1: ❌ An error occurred (check $procerror for details)
  • 0: 🚫 Key does not exist
  • 1: βœ… Key exists in the component ($result contains occurrence number)
  • 2: πŸ—„οΈ Key exists in the database

⚠️ Common Error Codes

When $status returns -1, $procerror provides specific error information:

  • -2 through -12: Database I/O errors
  • -16 through -30: Network I/O errors
  • -1128: The specified key number is an index (not allowed)
  • -1102: Invalid entity name or entity not painted on component
  • -1104: Invalid key number (out of range)

πŸ’‘ Practical Examples

Basic Usage

findkey $entname, 1  ; Check primary key
findkey "CUSTOMER", 2  ; Check candidate key #2
Enter fullscreen mode Exit fullscreen mode

Shortened Form

findkey  ; Equivalent to: findkey $entname, 1
Enter fullscreen mode Exit fullscreen mode

Dynamic Key Checking

findkey $entname, $NEWKEYVALUE
Enter fullscreen mode Exit fullscreen mode

πŸ† Best Practices

When to Use findkey vs retrieve/o

  • Use findkey in validateKey triggers - Perfect for quick existence checks
  • Use retrieve/o in leaveModifiedKey triggers - When you need additional functionality like repositioning

Performance Considerations

findkey is optimized for speed since it:

  • πŸš€ Performs no validation
  • 🎯 Only checks existence
  • ⚑ Doesn't retrieve full record data

πŸ”§ Integration in Your Workflow

The findkey statement works in all component types, making it versatile for various scenarios:

  • Data validation before insert operations
  • Duplicate prevention in user interfaces
  • Quick existence checks in business logic

πŸŽ‰ Conclusion

The findkey statement is an essential tool in the Uniface developer's arsenal. Its simplicity and efficiency make it perfect for scenarios where you need to quickly verify key existence without the overhead of full data retrieval. By understanding its return values and proper usage contexts, you can build more responsive and efficient database applications.

With assistance from AI, this guide synthesizes key concepts from the official Uniface documentation to help developers master this important feature.

Top comments (0)