2022/12/28

NestedDataSets in ClientDataSets


Overview

The nested dataset feature of ClientDataSet is used to solve the Master-Details design pattern in which a TDataSet field is inserted into the column list. This feature has always been considered a magical technique.

However, the core of database technology is still SQL, so let's take a look at how Delphi uses SQL to pull data in the nested dataset mode.

Surprisingly, the way to pull data is to fetch all details to the client-side at once when fetching data from the header.

2022/08/12

Creating Modern JSON Configuration Files Using Delphi

English

Recently, EMBT published an interesting article in their official news: "This Is How To Store Cross Platform App Settings In JSON" by the Softacom Information team. This emerging team collects and publishes articles from Delphi enthusiasts worldwide, so it's unclear who the original author is.

The article discusses that in addition to XML (Extensible Markup Language) and INI (Initial) formats, modern cross-platform applications often use JSON for configuration files, with Visual Studio Code as a classic example.

Visual Studio Code Workspace Settings File

The article pointed out that Delphi provides two JSON libraries: JSON Objects Framework and Readers and Writers JSON Framework. The author also mentioned the most widely used alternative open-source option, X-SuperObject, provided by Turkish engineer Onur YILDIZ. It's interesting to note that the JSON Objects Framework provides JSON Marshaling/Un-Marshaling, so what makes it different from X-SuperObject?

Delphi developers are spoiled for choice when it comes to using JSON. Realy?

The author complains that Delphi engineers have been spoiled by the official JSON frameworks. Is that true?

The article points out that Delphi provides two JSON libraries: JSON Objects Framework and Readers and Writers JSON Framework. As an alternative, the author suggests using the widely popular open-source variant X-SuperObject by Turkish engineer Onur YILDIZ. Curiously, JSON Objects Framework provides JSON Marshaling/Un-Marshaling, so how does it differ from X-SuperObject?


Example using JSON Objects Framework

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
TContact = class(TObject)
  public
    Name: string;
    Age: Integer;
    function ToString: string; override;
  end;
 
procedure TForm1.JSONMarshalClick(Sender: TObject);
var
  LContact: TContact;
  oMarshaller: TJSONMarshal;
  crtVal: TJSONValue;
begin
  LContact := TContact.Create; //our custom class
  LContact.Name:='Hello Eden';
  LContact.Age := 20; //fill with some data
  oMarshaller := TJSONMarshal.Create(TJSONConverter.Create); //our engine
  crtVal := oMarshaller.Marshal(LContact); //serialize to JSON
  try
    Memo1.Text := crtVal.ToString; //display
  finally //cleanup
    FreeAndNil(LContact);
    FreeAndNil(oMarshaller);
    crtVal.Free;
  end;
end;

Example execution result using JSON Objects Framework

1
2
3
4
5
6
7
8
{
  "type": "Unit2.TContact",
  "id": 1,
  "fields": {
    "Name": "Hello Eden",
    "Age": 20
  }
}

Example execution result using JSON Objects Framework

The JSON format produced by TObject using the JSON Objects Framework (JOF) is not as intuitive as the one produced by X-SuperObject. Users might need some time to learn the JOF format to manage their configurations.

This has to do with the original purpose of JOF. Around 2009, Delphi 2009 made a transition in DataSnap Framework from XML RPC to JSON RPC. As mentioned earlier, JOF was initially created to serve DataSnap, among other things, such as handling Delphi TObject's strong typing. Therefore, the format produced by JOF is not a weakly-typed compatible JSON format.

Why doesn't JOF produce widely accepted JSON formats?

So, the author's claim that Delphi engineers are spoiled by the two built-in JSON frameworks is not baseless. After getting used to Delphi's native tools, it may be difficult to accept content that deviates from the "Delphi standard."

Creating your desired format using JOF

Knowing that the built-in JOF won't produce a generic JSON format, you can still create your own. Have you heard of TDBXJSONTools? It can be used to convert TDataSet to JSON, so let's add TObject-to-JSON functionality to it!

TDBXJSONToolsHelper adds ObjToJSON functionality

The original example is excellent and deserves a 5-star rating. I modified the example into a VCL project and replaced X-SuperObject usage with JOF, such as in the configuration file loading section:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
procedure TMyProgSettings.LoadFromFile(AFileName: string = '');
var
  LJObj: TJSONObject;
begin
  if AFileName = '' then
    AFileName := GetDefaultSettingsFilename();
 
  if not FileExists(AFileName) then
    Exit;
 
  LJObj := TJSONObject.Create;
  try
    if LJObj.Parse(TFile.ReadAllBytes(AFileName), 0) > 0 then
    begin
      // Magic method from Eden's TDBXJSONToolsHelper unit
      TDBXJSONTools.JsonToObj(LJObj, Self);  
    end;
  finally
    LJObj.Free;
  end;
end;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
procedure TMyProgSettings.SaveToFile(AFileName: string = '');
var
  Json: string;
begin
  if AFileName = '' then
    AFileName := GetDefaultSettingsFilename();
 
  // Magic method from Eden's TDBXJSONToolsHelper unit
  with TDBXJSONTools.ObjToJSON(Self) do
  begin
    Json := ToJson;
    TFile.WriteAllText(AFileName, Json, TEncoding.UTF8);
    Free;
  end;
end;

Conclusion

Being "spoiled" by JOF is, in my opinion, a blessing. JOF is a framework well worth learning, and X-SuperObject offers even more features for JSON writing. If you frequently need to write JSON objects, X-SuperObject will definitely win your heart. Additionally, I enjoyed the original author's example of a login process using a while loop to control the login window; it's a great concept to learn.

You can download the TDBXJSONToolsHelper and VCL example program from my Github website: https://github.com/Eden5Wu/HelperClass/tree/master/Demo-JSON/JsonMarshalling

In summary, both the JSON Objects Framework (JOF) and X-SuperObject have their own unique advantages when working with JSON in Delphi. Developers should choose between them based on their specific needs and preferences.

For developers familiar with the Delphi style, JOF provides a familiar environment for working with JSON. However, if you need more control over JSON object writing, then X-SuperObject may be a better choice.

In any case, exploring and learning about the different JSON frameworks available is essential. By understanding their strengths and weaknesses, you can make more informed decisions about which tools to use in your projects.

Remember, practice and experimentation are key to becoming proficient in any programming language or framework. Therefore, don't be afraid to try different approaches and learn from the examples and experiences of other developers in the Delphi community.

中文版:使用 Delphi 寫出現代化 JSON 格式的設定檔案

最近 EMBT 官方新聞出現我很感興趣的文章:This Is How To Store Cross Platform App Settings In JSON,是由 Softacom Information 團隊執筆,這是一個新興團隊,匯整來自全球各地 Delphi 愛好者的文章並發表,所以還不知道真實原作者是誰。

原文作者說明在跨平台應用程式在讀寫設定檔時,除了 XML (Extensible Markup Language) 和 INI (Initial) 外,還有一個現代化應用程式常使用的設定檔格式:JSON,例如 Visual Studio Code 就是一個經典的參考。

Visual Studio Code 工作區設定檔

Delphi developers are spoiled for choice when it comes to using JSON. Realy?

原作控訴 Delphi 工程師已經被官方 JSON 框架寵壞了 (笑),是真的嗎?

2022/08/01

2022 RAD Studio 品質大廳 (Quality Portal) 使用者手冊

在使用 Delphi 或 C++ Builder 時往往會遇到一些操作不習慣、漏洞 (Bugs) 等問題,以前的 Borland, CodeGear 時期就有品質控制 (QC) 網站,但實際使用上不是連不上就是不知道哪一個才是有在維護的連結,所以會很依賴代理商代為傳遞問題回報的工作,回報者很難得到最快最直接的回應。

2022/04/10

安裝 SQL Server 2019 Express LocalDB 初體驗


原本測試環境打算使用 Interbase,想到自己已經有 SQL Server Driver,不拿來用有點可惜。

索性就試著拿 SQL SERVER LocalDB 來試試。

安裝 SQL SERVER 2019 Express LocalDB

安裝和建立很簡單,到微軟的線上手冊即可下載:

 

點擊紅框處就可以下載  SQL2019-SSEI-Expr

 

建立預設執行個體

C:\Users\Eden>sqllocaldb c
LocalDB instance "MSSQLLocalDB" created with version 15.0.2000.5.

C:\Users\Eden>sqllocaldb start
LocalDB instance "MSSQLLocalDB" started.

C:\Users\Eden>sqllocaldb info "MSSQLLocalDB"
Name:               MSSQLLocalDB
Version:            15.0.2000.5
Shared name:
Owner:              EDEN-WIN10\Eden
Auto-create:        Yes
State:              Running
Last start time:    2022/4/10 下午 11:00:17
Instance pipe name: np:\\.\pipe\LOCALDB#<use your pipe name>\tsql\query

*<use your pipe name>在每次啟動時都會不一樣。

之後可以使用 SSMS 以 pipe 提供的內容進行連接;我在這裡使用 sqlcmd 來建立,而 sqlcmd 在安裝前必須先安裝 ODBC 17 for SQL SERVER。

資料庫管理工具 -- SQLCMD

 

建立南風資料庫 

github -- instnwnd.sql 下載 SQL 檔,並且更換 mdf / ldf 路徑後 (不換也可以),執行即可建立南風範例資料庫。


建立方式相當簡單,搞懂一些小細節後,可以很快的把開發環境建構起來。

關於 SQL Server 2019 Express LocalDB 的更多資訊

在本文中,我們介紹了如何安裝和建立 SQL Server 2019 Express LocalDB,以及如何使用 SQLCMD 建立南風資料庫。不過,如果你想進一步了解 LocalDB 的功能和優點,以下是一些相關的資訊:

  • LocalDB 是 SQL Server Express 的一個版本,它是一個輕量級的版本,通常用於開發和測試用途。與其他 SQL Server 版本相比,LocalDB 的安裝和部署更加簡單,並且不需要太多的系統資源。
  • LocalDB 支援多種 SQL Server 功能,例如 T-SQL、XML、JSON、Spatial 和 Full-Text Search 等。此外,LocalDB 還支援了多個版本的 SQL Server,包括 SQL Server 2019、SQL Server 2017、SQL Server 2016 和 SQL Server 2014 等。
  • LocalDB 提供了一些獨特的功能,例如自動啟動和關閉。這意味著當你需要使用 LocalDB 時,它會自動啟動,而當你完成使用後,它會自動關閉。這讓開發者更加方便,並且可以減少系統資源的浪費。
  • 不過,需要注意的是,LocalDB 有一些限制。例如,它只能在本機運行,無法用於生產環境;它也不能使用 SQL Server Management Studio 進行管理,只能使用命令列工具或其他軟體進行管理。

和你分享。 😘


See also