4D v16Sets and named selections |
||||||||||||||||||||||||||||||||
|
4D v16
Sets and named selections
|
Theme | Set | Named Selection |
Memory space for a record | 1 bit | 4 bytes |
Keep order by | No | Yes |
Keep the current record | No | Yes |
Union | Yes | No |
Intersection | Yes | No |
Difference | Yes | No |
Save to disk | Yes | No |
Memory size of a selection of 10 records out of 20,000 | 20,000 bits or 2500 bytes | 10 x 4 bytes = 40 bytes |
Scope | Local, Process, Interprocess | Process, Interprocess |
You can only compare sets from the same table.
Warning: The behavior of a set implies during a limited time and eventually using semaphores (see 4D documentation for more information).
In fact, a set makes a bit correspond to the physical position of each table record. In case of deleting and then adding records, the former content of a physical record could be replaced by the new content that is no longer in line with what the set is supposed to represent.
Be methodical in the use of sets, which remain an efficient and fast way to compare selections.
To keep a selection, there’s a third solution that involves using an array that contains your identification using the SELECTION TO ARRAY command.
You can use a fourth solution using stored clusters; for example, in BLOBs.
Of course, when selections are no longer necessary, you can free up memory by erasing them.
CLEAR SET("SetName")
CLEAR NAMED SELECTION("SelectionName")
In this video, we're going to learn how to keep a selection and perform functions (union, intersection, and difference).
Sets are a simple way to intersect lists of records from a single table.
They are one of the ways you can use to put a selection on hold so that it can be used again later on.
By performing an operation on two sets, we get a new set containing the result of the operation performed (according to conventional set theory).
We're going to apply these principles right away to the list of interventions in the navigation form. We can create a set from the current selection.
We're going to create a button that searches for all the interventions that were started before 9:00 a.m.
Here is the method of the button:
QUERY([Interventions];[Interventions]Intervention_Time
and we're going to create a 2nd button that will give us all the interventions that are not yet 100% completed. We can program this button as follows:
QUERY([Interventions];[Interventions]Progress<100)
For the moment, in the file we imported, the progress % is not filled in.
So first we're going to apply a formula on all the interventions. Here we have a selection of 11,732 interventions where we're going to apply a formula and specify that:
If we now produce a report to check what progress % values have been filled in, we see that they range from 0 to 100.
Of course, if we click on the 1st button and then on the 2nd, we will have lost the selection created during the 1st click.
So in the 1st button, it may be useful, after the query, to keep the information in a set that we can call "Morning_Interventions".
CREATE SET([Interventions];"Morning_Interventions")
Similarly, here, after retrieving the set of interventions that have not yet been completed, we can call this set "Interventions_in_progress".
CREATE SET([Interventions];"Interventions_in_progress")
Then we can compare the sets:
UNION("Morning_Interventions";"Interventions_in_progress";"Interventions_Result")
We can test these different possibilities by executing the navigation form.
We take all the interventions, there are 11,732, and here (after clicking on the query buttons), we still have 11,732.
Why is that? Remember that in the previous video, in the Navigation_Function we added, at the very bottom, this line that gives us the number of records found after a new selection is created.
vNumRecords:=Records in selection([Interventions])
We're going to copy this code and apply it to the 2 query buttons.
Currently, we do not have the table pointer here; we're going to process the interventions.
Then we can copy this line of code for 4 other buttons:
Now, if we go back to the navigation form, we have:
Interventions before 9:00 a.m. + those at 100%, 9:00 a.m. interventions still in progress or 9:00 a.m. interventions that are finished, don't seem to change the number that is here.
Why is that? It's because simply creating a set does not imply its use.
In this button here, after we have created the union that creates a 3rd set, we must indicate the particular set that needs to be used.
So we're going to modify the code and:
And that way we can test:
To complete this approach, we're going to close the form and launch the Navigation method again to check its operation.
If we click on one of the three buttons to the right, an error message indicates that the sets do not exist. Naturally, the sets must exist before we can use them.
We're going to trace the creation of these sets by clicking on the 1st query button.
We can see, in the list on the left, the "Sets" theme that shows the list of existing sets in the process being traced. Currently, there are none.
When the method is executed:
And then we'll see, because the set already exists, it's only the quantity that changes.
You need to keep in mind that a set is only a list of records that is not necessarily the current selection since right now the current selection of the Interventions table contains 601 records.
When we switch to the USE SET command, the selection now contains a number of records matching the number indicated in the set and the vNumRecords variable is also adapted accordingly.
Another use of sets that is very convenient consists in selecting a certain number of records and then indicating that we want to make them the current selection; in other words, when we click the button, there are now only these X records.
To do this, we must:
The code looks like this:
$SetName:="Userset_"+String(Milliseconds)
GET HIGHLIGHTED RECORDS($TablePointer->;$SetName)
USE SET($SetName)
vNumRecords:=Records in selection($TablePointer->))
In other words, we're going to:
We can test:
At this level, we have:
In our array, we now have the list of records that we selected.
A set is made up of one bit per record and therefore does not preserve the sort order.
When you need to keep this information, you must use commands related to named selections:
COPY NAMED SELECTION and USE NAMED SELECTION
Here we're going to add a button that allows us to:
It's now possible to store 2 selections that will take the sorting order into account.
They exist in memory but will not be used.
Then we're going to:
If we take all the interventions:
Now:
Product: 4D
Theme: Sets and named selections
Self-training ( 4D v16)