Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Using DataSnap Importer to Work with Datasets

Posted on 10.01.2015 by gabr42 Posted in Documentation, News

Last time we looked at how Smart can be used to write a client for a REST server, written in Delphi. This time we’ll see how we could use this approach to work with datasets.

In theory, it should be possible to write a client which works seamlessly with datasets, just like Delphi does when you use a “normal” DataSnap server. In practice, however, even Delphi’s own clients don’t support this with REST servers in versions up to XE6. Only in XE7 a better support for sending FireDAC datasets over REST was added, but this is not yet supported in Smart. So for the time being you will have to do some manual work and write some dataset-wrapping code on the server (and also on the client).

Delphi Server

For a demo server we’ll be using the FishFactsIndy application, which is included in all modern Delphis up to XE6. It is missing from the XE7 (it was replaced with the FireDAC JSON Reflection Demo “Customers”), but you can still compile demo from older Delphi if you install BDE (available to registered users only).

This demo exposes access to the well-known FishFacts database by implementing few REST APIs which your client can call. Following methods are implemented (in the FishFactsServerMethods unit).

function GetKeys: TJSONArray; overload;
function GetImage(Key: String): TStream;
function GetFacts(const AKey: string;
  out ASpeciesName: string;
  out ACategory: string;
  out ACommonName: string;
  out ALengthIn: Double;
  out ALengthCm: Double): Boolean;
function GetNotes(const AKey: string;
  out ANotes: string): Boolean;

For example, the GetKeys method returns an array of database primary keys. The code can then use those keys to get additional information from the database.

function TFishFactsServerMethods.GetKeys: TJSONArray;
var
  Container: TJSONArray;
begin
  Container := TJSONArray.Create;
  GetKeys(Container);
  Result := Container;
end;

procedure TFishFactsServerMethods.GetKeys(const Container: TJSONArray);
begin
  DataModule.Table1.Active := True;
  try
    DataModule.Table1.First;
    while not DataModule.Table1.Eof do
    begin
      Container.AddElement(TJSONString.Create(DataModule.Table1SpeciesNo.AsString));
      DataModule.Table1.Next;
    end;
  finally
    DataModule.Table1.Active := False;
  end;
end;

To set up the test environment, just run the program and click Start.

FishFacts server

Smart Client

On the Smart side, we proceed as in the previous installment (link):

  • Create new visual application.
  • Save.
  • Tools, Import DataSnap connector.
  • Enter host name and port of the server.
  • Click Import.

DataSnap importer is only available in the Smart Mobile Studio Enterprise edition. To use it, you will also need Delphi Enterprise (or RAD Studio Enterprise), XE or newer, installed on the same computer.

FishFacts client - design time

Our program has two edit fields where you can configure address and port of the server. Next, a Get keys button downloads all keys from the server and stores them in the cbxKeys combo box. Each time a combo box is changed, database is queried for the new key and information about the selected fish is displayed in the edit box below the combo box. An image of the fish is also loaded and displayed at the bottom.

FishFacts client - runtime

The code for the client part is available here.

Let’s take a look at some critical parts of the program. First, the data loading.

When the Get keys button is clicked, TFishFactsServerMethods_Client object is created. It’s GetKeys method is then called. A for loop iterates over all returned keys and stores them in the combo box.

procedure TForm1.btnGetKeysClick(Sender: TObject);
begin
  FFishClient := TFishFactsServerMethods_Client.Create(
    TDataSnap.CreateConnection(inpServer.Text, StrToInt(inpPort.Text), ''));
  cbxKeys.Items.Clear;
  for var s in FFishClient.GetKeys.result do
    cbxKeys.Add(s);
end;

Information on the fish and its image are retrieved in the cbxKeys’ OnChange event. Information is retrieved by calling the GetFacts method. For the image part we could call the GetImage method but then we would have to load returned data into the image control somehow. An easier way to achieve this is to just generate image-fetching URL by calling GetImage_URL and passing that to the image control.

procedure TForm1.cbxKeysChanged(Sender: TObject);
begin
  var key := cbxKeys.Items[cbxKeys.SelectedIndex];

  var facts := FFishClient.GetFacts(key);
  outName.Text := Format('%s [%s], %.1f cm', [facts.ASpeciesName, facts.ACategory, facts.ALengthCm]);

  outImage.LoadFromURL(FFishClient.GetImage_URL(key));
end;

Bright future ahead

This covers the current state of DataSnap support in Smart. As you can see, it is usable but maybe not as efficient as a pure Delphi solution. (But then, on the other hand, we could say the same for Delphi REST server + JavaScript client combination – it will take the same amount of work as the Delphi + Smart way.)

Better and simpler ways of working may appear in one of the following releases now that the Embarcadero is supporting FireDAC reflection in XE7. Stay tuned!

« Using DataSnap Connector Importer
Smart Mobile Studio 2.2 (beta-1) »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS