Retrieve a Record

The following example shows how to retrieve (or "select") a record by reference:

       DatastoreRecord filter = new DatastoreRecord();
       filter.setString("Reference", "C:\\data\\file1.txt");
       DatastoreRecord selected = datastore.recordSelectOne(
                                     "Documents",
                                     filter,
                                     new String[] { "Reference", "LastModified"});

The first argument specifies the table to perform the query on. The second specifies a query to perform and selects records where the Reference column is equal to "C:\data\file1.txt". The query is expressed in terms of a filter - of all the records, only those that match the columns set in the filtering record are selected. The third argument specifies the columns to retrieve.

You can extract the values from the DatastoreRecord object using one of the get methods for common types:

       String reference = selected.getString("Reference");

The recordSelectOne method returns the first record to match the filter, or null if no records match. To retrieve more than the first match you can use the recordSelect method instead.

Retrieve Multiple Records

To retrieve multiple records, use the recordSelect function:

       SelectHandler selectHandler = new SelectHandler(task);
       datastore.recordSelect(
         "Documents",
         filter,
         new String[] { "Reference", "LastModified"},
         selectHandler,
         "handle");

The first three arguments are identical to those of the recordSelectOne method. They specify the table, the filter and the columns to return. The fourth and fifth arguments specify a function that is called for each record found, which in this case writes the reference to the log. A simple select handler class is shown below:

       public class SelectHandler
       {
           private SynchronizeTask task;
        
           public SelectHandler(SynchronizeTask task)
           {
               this.task = task;
           }
        
           public void handle(DatastoreRecord record)
           {
               task.getLog().writeln(Log.NORMAL, "Found record: "
                      + record.getString("Reference"));
           } 
       };