Page History
Table of Contents | style | upper-roman
---|
Stoc |
The Servoy Foundset is a developer's window into Servoy's Data Binding layer. A single foundset always maps to a single database table (or view) and is responsible for reading from and writing to that table. From the user interface, a foundset controls which records are loaded and displayed, as well as how records are created, edited and deleted. From the developer's perspective, a foundset is a programmable object with specific behaviors and run-time properties that provide a high-level abstraction to facilitate low-level data operations.
Note |
---|
For all programming reference information, see the JSFoundSet API documention in the reference guide. |
Forms Bound to a Foundset
A Servoy Form is typically bound to a single database table and the form will always contain a single Foundset object which is bound to the same table. Much of the action in the user interface, such as a user editing data fields, directly affects the form's foundset. Conversely, actions taken on the foundset, such as programmatically editing data, is immediately reflected in the form.
...
Note |
---|
See also the namedFoundset property of a form. |
Loading Records
One of the primary jobs of a Foundset is to load records from the table to which it is bound. A Foundset object is always based on an underlying SQL query, which may change often during the lifetime of the Foundset. However the query will always take the form of selecting the Primary Key column(s) from the table and will also always include an Order By clause, which in its simplest form will sort the results based on the Primary Key column(s).
...
Note |
---|
See also the Database Manager's getSQL and getSQLParameters methods |
Loading Records Programmatically
The loadRecords method is used to directly modify the underlying query that loads PK data. There are several uses.
Load by a single PK
This is the simplest approach, which loads a single recordy by its primary key value.
Code Block |
---|
foundset.loadRecords(123); |
Load by PK data set
This approach simply dictates that a foundset will load records based on specified primary key data.
...
Note |
---|
Notice the array was converted first to a JSDataset object. This object, which is like a 2-dimensional array, is used to provide support for composite primary keys. |
Load by another foundset
This approach is useful to essentially copy the query of another foundset.
Code Block |
---|
foundset.loadRecords(anotherFoundset); |
Load by Query
This approach allows a SQL query fragment to be used to set the foundset's underlying query. There are certain restriction on the form that a query can take. For obvious reasons, the query must return the primary key column(s) from the table to which the foundset is bound. For a full description see the reference guide.
...
Note |
---|
See also the loadRecords API in the reference guide for complete usage options. |
Sorting
All foundsets contain a sorting definition that determines the order in which records are loaded and displayed. Sorting is always expressed in the ORDER BY clause of a foundset's query and thus handled by the database.
...
Tip |
---|
Sorting on related columns and aggregates changes is simple and powerful. However this changes the nature of the foundset's query. One should be advised of this and ensure that the database is tuned accordingly. |
Scrolling Result Set
The Foundset maintains a scrollable interface for traversing record data. This interface includes a numeric index for every record that is returned by the Foundset's query.
...
Code Block |
---|
// Foundset size grows dynamically as the Foundset is traversed foundset.getSize(); // returns 200 foundset.setSelectedIndex(200); foundset.getSize(); // returns 400 because the foundset loaded the next 200 record pks |
Iterating over a Foundset
Often, as part of some programming operation, it is necessary to iterate over part or all of a foundset. There are several approaches to iterating, each having their appropriate usage. In general, a Javascript for or while statement is used to control the flow of execution.
Changing the Selected Index
Perhaps the most intuitive approach is to programmatically change the foundset's selected index property.
...
Note |
---|
See also the JSFoundset's setSelectedIndex method. |
Accessing a Record Object
While setting the selected index of the foundset is sometimes necessary, it also contains some overhead and therefore is not always the most efficient way to iterate over a foundset. However, one can iterate in a similar manner, access a record object without changing the selected index of a foundset by using the getRecord method of the foundset.
...
Note |
---|
See also the JSFoundset's getRecord method |
Accessing Data Provider Values as an Array
Sometimes the purpose of iterating over a foundset is to access all of the values for a particular data provider. The most efficient way to do this is to obtain an array of values for the foundset's data provider using the getFoundSetDataProviderAsArray method of the databaseManager API.
...
Note |
---|
See also the JSFoundset's getFoundSetDataProviderAsArray method |
Related Foundsets
Foundsets are often constrained or filtered by a Relation. In this situation, the foundset is said to be a Related Foundset and its default SQL query will include in its Where Clause, the parameters by which to constrain the foundset.
...
Gliffy Diagram | ||
---|---|---|
|
Foundsets and Data Broadcasting
A Foundset may be automatically updated when the client receives a Data Broadcast Event . If the data change affected the table to which the foundset is bound, the foundset will be refreshed to reflect the change.
Performing Batch Updates
Foundsets are typically updated on a record-by-record basis, either as the user operates on a foundset-bound GUI component, or through programmatic interactions. However, sometimes it is necessary to perform an update to an entire foundset. For performance reasons, it is not advised that this be done by programmatically iterating over the foundset's records. Rather, it is recommended that batch updates be performed using the JSFoundsetUpdater API.
The Foundset Updater API is ideal to use for the following situations:
Updating an entire foundset
This essentially has the effect of issuing a SQL UPDATE statement using the WHERE clause that constrains the foundset. This presents a significant performance advantage over updating records individually. In the example below, a related foundset is updated, meaning all orders belonging to the selected customer will be affected.
Code Block |
---|
var fsUpdater = databaseManager.getFoundSetUpdater(customers_to_orders); fsUpdater.setColumn('status',101); fsUpdater.performUpdate(); |
Updating a partial foundset with different values for each record
The Foundset Updater API can also be used to update part of a foundset. Moreover, unlike the above example, this approach allows for different values for each record. In the example below, the first 4 records (starting from the selected index) are updated by specifying an array of values for each column that is affected.
...
Note |
---|
When using this approach, it matters what the selected index of the foundset is. The update will start with this record. |
Updating each record individually
The Foundset Updater API can also be used to update records individually, but still holds a performance advantage over iterating on a foundset, which has more overhead and can cause the foundset's cache size to increase unnecessarily. In the example below, each record in the foundset is updated with a unique value (A simple counter is incremented, which is arbitrary, but demonstrates that each record can be updated with a unique value.)
...