Create, update and delete data with ORDA

Automatically translated from English

4D v17 introduces ORDA, a major evolution in 4D which is opening a world of new possibilities for 4D developers. To learn more about ORDA and its benefits, check out this post to see how ORDA will change the way you work. In order to guide you through the ORDA exploration, we’ve prepared a series of blog posts fully dedicated to the core concepts and related features. This blog post will detail how you can perform CRUD operations on your database with ORDA. CRUD is an acronym for the four basic operations you can perform on data: Create, Read, Update, and Delete.

INTRODUCTION

With ORDA, data is accessed through an abstraction layer: the datastore. A datastore is an object which provides an interface to the database and its data through objects. For example, a table is mapped to a dataclass object, a field is an attribute of a dataclass, and records are entities. The new ds command returns a reference on the application datastore. If you want to learn more, take a look at the overview article in the documentation.

To guide you through the exploration of ORDA features, we’ve prepared a glossary of the different terms and concepts, along with their definition.

CRUD operations

With ORDA, the Create, Update and Delete operations are not performed on a current selection or a current record, but on entities. This allows you to manage several set of entities (at the same time) to perform your actions.

ORDA – Perform CRUD operations

create

The Create operation is done simply by instantiating a new entity with the new() method and then saving it with the save() method.

C_OBJECT($status;$employee)
$employee:=ds.Employee.new() // Create a new employee
$employee.lastName:="Smith"  // Set the last name to "Smith"
$status:=$employee.save()    // Save employee

update

The Update operation is performed on an entity using the same save() method.

C_OBJECT($status;$employee)
$employee:=ds.Employee.query("lastName=:1";"Smith").first() // Get first employee whose name is "Smith"
If ($employee#Null)
  $employee.lastName:="Mac Arthur" // Set the last name to "Mac Arthur"
  $status:=$employee.save() // Save employee
End if

delete

The Delete operation is performed on an entity or an entity selection using the drop() method.

C_OBJECT($status;$employee)
$employee:=ds.Employee.query("lastName=:1";"Mac Arthur").first() // Get first employee whose name is "Mac Arthur"
If ($employee#Null)
  $status:=$employee.drop() // Delete the employee data but the $employee entity remains in memory
End if 

LOCKING ENTITIES

Working with pessimistic locking is possible with ORDA. In the classic way of coding, the Locked command is used to check whether or not the current record of a table is locked.

With ORDA the entity must be locked before updating it. The lock() method returns a result indicating if the lock was successful or not. Once the entity is updated, it must be unlocked with the unlock() method.

C_OBJECT($employee;$statusLock;$statusSave;$statusUnLock)
$employee:=ds.Employee.query("lastName=:1";"Wates").first()
If ($employee#Null)
  $statusLock:=$employee.lock() // Lock entity
  If ($statusLock.success) // The entity has been successfully locked
    $employee.lastName:="Mac Arthur" // Set name to "Mac Arthur"
    $statusSave:=$employee.save() // Save employee
    $statusUnLock:=$employee.unlock() // Unlock entity
  End if
End if

The good news is that ORDA lets you choose whether you want to work with optimistic or pessimistic locking. We’ll give you all the details in a dedicated post about both locking modes. Stay tuned!

• Product Owner •

Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017.As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer.Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.