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}
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
Shortened Form
findkey ; Equivalent to: findkey $entname, 1
Dynamic Key Checking
findkey $entname, $NEWKEYVALUE
π Best Practices
When to Use findkey vs retrieve/o
- Use
findkey
invalidateKey
triggers - Perfect for quick existence checks - Use
retrieve/o
inleaveModifiedKey
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)