Dynamic Select sample (console) (Delphi)
The Dynamic Select sample demonstrates how to use Dynamic Select to selectively retrieve data from a table in an instance of Relativity Server by limited the fields retrieved to only those that are needed.
There are a couple of different methods available in Data Abstract for Delphi to use the Dynamic Select feature. This sample demonstrates the simpler, although not so obvious, approach of removing the un-needed fields from the Data Table before passing the table to the server.
The other approach requires some more boilerplate code to set up, using an instance of TableRequestInfoV5, but it is a bit more straightforward.
Getting Started
The sample is located in C:\Users\Public\Documents\RemObjects Samples\Data Abstract for Delphi\DynamicSelect (Console)
.
To build it, you will need a Delphi RAD Studio and like all the samples provided with Data Abstract for Delphi you will need to be running Relativity Server with the DASamples
Domain and the Simple
Schema available.
Included with the sample is a uLogSupport
class which has a LogTable
procedure that takes an instance of TDADataTable and creates a nicely formatted string representation of the table that it then prints to the Console which can be useful for debugging purposes. (An example of its output can be seen at the end of this document)
Running the Sample
When run the sample sets up a connection to an instance of Relativity Server and logs in (Step 1). It then builds an array of field names to retrieve and configures the data table to have those fields before retrieving the data (Step 2). The table is then reconfigured with different fields and the data retrieved again to show that it is different (Step 3).
Examining the Code
All of the code can be found in the uDynamicSelect
source file. The schema table "Clients
" used in this sample is comprised of 10 fields, which this sample will limit to just 2 using Dynamic Select.
Defining the Data Table
The sample uses a Data Table to store the data retrieved from the server, and also to inform the server about the table we are interested in.
In Step 1 the sample creates an instance of TDAMemDataTable and assigns the newly created and the configured TDARemoteDataAdapter to its RemoteDataAdapter property. The table will now use the data adapter for any operations that require interacting with the server.
Finally to retrieve particular table data from a schema, we need to assign the name of the table we want to the retrieve to the LogicalName property of the TDAMemDataTable object.
lTable := TDAMemDataTable.Create(nil);
lTable.RemoteDataAdapter := lRemoteDataAdapter;
lTable.LogicalName := 'Clients';
Retrieving data using Dynamic Select (Step 2 & 3)
As mentioned at the beginning of this page, this sample uses the approach of only adding the fields we are interested in to the table. This is handled is two steps in this sample, first creating a string array of the field names we are interested in and second calling the ConfigureTableForDynamicSelect
procedure which reconfigures the table for us.
While this sample breaks out creating the lDynamicSelect
array for the field names and assigning the field name to each element of the array, you could as easily build the array inline to the procedure call; for example using:
ConfigureTableForDynamicSelect(lTable,['ClientId','ClientName']);
The ConfigureTableForDynamicSelect
procedure can be found in uDynamicSelect
and is a helper procedure that takes two arguments; the first an instance of TDADataTable and the second argument is the array of the fields we want to retrieve. The procedure empties the table of all existing fields, and then adds a new field, which are instances of TDAField, for each element of the array of field names and assigns the field name to the Name. This is important as the field names will be used by the server to decide what data it is returning.
procedure ConfigureTableForDynamicSelect(aTable: TDADataTable; aDynamicSelect: array of string);
var
i: integer;
lField: TDAField;
begin
aTable.Close;
aTable.Fields.Clear;
for I := Low(aDynamicSelect) to High(aDynamicSelect) do begin
lField := aTable.Fields.Add;
lField.Name := aDynamicSelect[i];
end;
end;
Having reconfigured the table we ensure that Dynamic Select is turned on in the TDARemoteDataAdapter by assigning True
to the DynamicSelect property.
Finally we call the Fill procedure of TDARemoteDataAdapter passing it the table object, a False
to indicate that cursor should not be saved, and True
that the schema should be included. Note there is an optional fourth boolean argument which has a default value of false and does not need to be supplied.
This then retrieves the required table data and it will be output in this sample to the console.
SetLength(lDynamicSelect,2);
lDynamicSelect[0] :='ClientId';
lDynamicSelect[1] :='ClientName';
Log('DynamicSelect is [%s,%s]',[lDynamicSelect[0],lDynamicSelect[1]]);
ConfigureTableForDynamicSelect(lTable, lDynamicSelect);
// Turn on DynamicSelect feature...
lRemoteDataAdapter.DynamicSelect:=True;
Log('Dynamic Select feature is ON');
lRemoteDataAdapter.Fill([lTable],False,True);
LogTable(lTable,10);
SetLength(lDynamicSelect,2);
lDynamicSelect[0] :='ClientId';
lDynamicSelect[1] :='ClientPhone';
Log('DynamicSelect is [%s,%s]',[lDynamicSelect[0],lDynamicSelect[1]]);
ConfigureTableForDynamicSelect(lTable, lDynamicSelect);
lRemoteDataAdapter.Fill([lTable],False,True);
Example Output
This is an example of the output you will see when the sample is run. You can see in both Step 2 and Step 3 that the tables printed contain only the specified fields, the other fields were not retrieved from the server.
Dynamic Select sample has been started.
Target URL is http://localhost:7099/bin
RO DataAbstract layer is configured.
STEP 1: Login to DataService
Connecting to Relativity server: http://localhost:7099/bin
Login string is :
User Id="simple";Password="simple";Domain="DASamples";Schema="Simple"
Login has been successfull. Going further...
STEP 2. Select ClientId, ClientName from Clients ...
DynamicSelect is [ClientId,ClientName]
Dynamic Select feature is ON
Table: Clients (7 rows from 7)
---------------------------------
| ClientId | ClientName |
---------------------------------
| 18 | Brian L. Rowles |
| 19 | Angela W. Vanov |
| 20 | Anna H. Kugler |
| 21 | Randy R. Howard |
| 22 | Kenny S. Lay |
| 23 | Maryann C. Bach |
| 24 | Lillie R. Schro |
---------------------------------
STEP 3. Select ClientId, ClientPhone from Clients ...
DynamicSelect is [ClientId,ClientPhone]
Table: Clients (7 rows from 7)
---------------------------------
| ClientId | ClientPhone |
---------------------------------
| 18 | (908) 385-7809 |
| 19 | (610) 463-9999 |
| 20 | (817) 249-9525 |
| 21 | (234) 567-8909 |
| 22 | (817) 249-9525 |
| 23 | (907) 722-6777 |
| 24 | (312) 476-1404 |
---------------------------------
Done!
Cleanup!
Press Return to exit...