Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

It is a new function of 10.2.2, and it can be outputted by Ext JS store definition.

Author: h.mohri

New in 10.2.2 TFDBatchMoveJSONWriter was added.

TFDBatchMoveJSONWriter is a Writer that combines with TFDBatchMove to out to a simple JSON. But,  TFDBatchMoveJSONWriter has the function of generating Model definition of Ext JS.
I tried with a field dynamically created with TFDMemTable.

code

The code below is a simple 64-bit console application.
I wrote this code using C++Builder.

umain.cpp
#include <vcl.h>
#include <windows.h>

#pragma hdrstop
#pragma argsused
#pragma link "dbrtl.a"
#pragma link "FireDAC.a"
#pragma link "FireDACCommonDriver.a"
#pragma link "FireDACCommon.a"

#include <tchar.h>
#include <iostream>
#include <System.JSON.hpp>
#include <FireDAC.Stan.Intf.hpp>
#include <FireDAC.Stan.Option.hpp>
#include <FireDAC.Stan.Param.hpp>
#include <FireDAC.Stan.Error.hpp>
#include <FireDAC.DatS.hpp>
#include <FireDAC.Phys.Intf.hpp>
#include <FireDAC.DApt.Intf.hpp>
#include <FireDAC.Comp.BatchMove.JSON.hpp>
#include <FireDAC.Comp.BatchMove.hpp>
#include <FireDAC.Comp.BatchMove.DataSet.hpp>
#include <Data.DB.hpp>
#include <FireDAC.Comp.DataSet.hpp>
#include <FireDAC.Comp.Client.hpp>
#include <FireDAC.Stan.StorageJSON.hpp>
#include <functional>
#include <string>
#include <sstream>

struct dbs
{
    TFDMemTable* f_MemTable1;
    TFDBatchMove* f_BatchMove1;
    TFDBatchMoveDataSetReader* f_DataSetReader1;
    TFDBatchMoveJSONWriter* f_JSONWriter1;
    TFDStanStorageJSONLink* f_JSONLink1;
    dbs(std::function<void(TFDMemTable*)> func)
    {
        f_MemTable1         = new TFDMemTable(nullptr);
        f_BatchMove1        = new TFDBatchMove(nullptr);
        f_DataSetReader1    = new TFDBatchMoveDataSetReader(nullptr);
        f_JSONWriter1       = new TFDBatchMoveJSONWriter(nullptr);
        f_JSONLink1         = new TFDStanStorageJSONLink(nullptr);
        f_DataSetReader1->DataSet   = f_MemTable1;
        f_BatchMove1->Reader        = *f_DataSetReader1;
        f_BatchMove1->Writer        = *f_JSONWriter1;
        func(f_MemTable1);
    };
    static inline String IToString(int iin)
    {
        std::wostringstream ss;
        ss << iin;
        return ss.str().c_str();
    }

    ~dbs()
    {
        delete f_MemTable1;
        delete f_BatchMove1;
        delete f_DataSetReader1;
        delete f_JSONWriter1;
        delete f_JSONLink1;
    };
};

int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        TStringList* alist = new TStringList();
        try
        {
            dbs adbs([](TFDMemTable* memt)
                {
                    //Field definition of TFDMemTable* instance.
                    //Create five fields.
                    for (int i = 0; i < 5; i++)
                    {
                        TStringField* aField    = new TStringField(nullptr);
                        aField->FieldName       = "Column" + dbs::IToString(i);
                        aField->Size            = 50;
                        memt->Fields->Add(aField);
                    }
                });
            //Then execute the GenerateExtJSModel() method.
            //This GenerateExtJSModel() is a TFDBatchMoveJSONWriter class that has been added than 10.2.2.
            adbs.f_JSONWriter1->GenerateExtJSModel("Table1", true, alist);
            //Console out.
            std::wcout << alist->Text.c_str() << std::endl;
        }
        __finally
        {
            delete alist;
        }

    }
    catch(...)
    {

    }
    return 0;
}

result GenerateExtJSModel()

The output store definition is as follows.

Ext.define('Table1', {
  extend: 'Ext.data.Model',
  requires: [
    'Ext.data.field.Field'
  ],
  fields: [
    { name: 'Column0', type: 'string' },
    { name: 'Column1', type: 'string' },
    { name: 'Column2', type: 'string' },
    { name: 'Column3', type: 'string' },
    { name: 'Column4', type: 'string' }
  ]
});

It can be output in Ext JS store definition.

Exit mobile version