Changing Data

You can also write scripts that will create, update, or delete data from the database.

Again, it's critical to understand paths before you start using these features.

Firstly, you can update a variable that contains a record simply by setting the fields:

set ##rec/Price = 500
 

This changes the value of the field Price to be 500.

But this just changes the contents of the variable in memory, it doesn't commit it. To commit it to the database, we use the save statement:

set #rec/Price = 500
 save #rec
 

Now when you load that record, it will have the new value you specified for that field.

It's worth noting that saving a record will invoke any EVENTS that you have associated with that record type. If the event has conditions, then it will only invoke if the fields in the conditions have changed.

If you don't want to have a record in the database, you can also delete it. When you want to delete something, you simply specify the path, not the record itself. You don't have to load data to delete it.

delete at &path
 

This deletes the record that's held in the &path variable. If you have a record and can't remember where it came from, you can use GetPath to get the path of the record:

delete at GetPath(#rec)
 

You have to be very careful about how you use delete. Such a simple command can potentially delete a LOT of data. If there are no predicates on the end, such that the path refers to a list, then the delete command will delete ALL the records at that list.

Delete will also delete any SUB-records in lists underneath that record. Please use with caution!

Next we'll discuss adding NEW records to the database, using the insert command.

To do this, you can take an existing record and make a copy of it.

insert new #rec at &root/Products
 

This takes the variable #rec and inserts a new copy of it at /Products.

If it's not an existing record, you don't need to put new. But you'll have to create the record yourself. To do this, you use the "new record" statement:

var #rec = new record for &root/Products
 

This creates a new record in memory ready to be inserted into /Products. We can then set it up:

set #rec/Title = "TV"
 set #rec/Price = 1000
 

This then sets the title and price of the record.

Finally we'll insert it into the database at a specific path:

insert #rec at &root/Products
 

Of course you could insert it at a different path, maybe a sub-path of your current position:

insert #rec at &path/MyProductList
 

Again, inserting or deleting will raise events. If you have scripts under those events, they will be invoked at the same time the record is inserted or deleted.


Next Topic:
v4.2.0.956 (beta)
Up Since 2/29/2024 12:02:23 AM