打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Using DataSnap Importer to work with datasets

Using DataSnap Importer to Work with Datasets

Posted on by 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.

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.

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.

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!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
delphi xe2下的datasnap心跳包设置
[杂谈] RAD Studio X Update 1 官方发布了
DELPHI DATASNAP 2010 入门操作(2)不写一行代码,绿色三层我也行
delphi XE10 安卓开发环境配置2
Delphi2010中DataSnap高级技术(1)—为DataSnap系统服务程序添加描...
delphi XE2 回到古老的 delphi7 桌面方法。。。
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服