C++Builder 10.2 Release 2でGenerateExtJSModel()を実行する。

201801011454.png

10.2.2の新機能 TFDBatchMoveJSONWriterが追加された

このTFDBatchMoveJSONWriterはTFDBatchMoveと組み合わせて使います。
そのTFDBatchMoveJSONWriter内に Ext JSのモデル定義をジェネレートできるメソッドがあるので試してみた。

TFDMemTableで動的にフィールドを作成した定義をExt JSのストア定義でアウトをつくった。

code

C++Builder 64-bitの簡単な console アプリケーションです。
10.2.2だとビルドが成功します。

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()

出力すると下記のようにモデルが作られています。

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.
2018-01-011450.png

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.