In the C++Builder of RAD Server, FireDAC + SQLServer connection.[JAPAN]

Posted by on in Programming

I explained the FireDAC + SQL Server connection in the previous parallel processing.

// Use FireDAC to MSSQL Server in parallel(std::vector<std::thread>)[JAPAN]

The "japan post office data"(SQL import) used last time is used from RAD Server.

We will create a new RAD Server project this time. That's C++Builder.

I have created one resource data module(TTestResource1). And we prepared a data module(TDataModule2) for one FireDAC.

We will create a single suffix for the resource. It is a member function.

class TTestResource1 : public TDataModule
	TDataModule2* test{new TDataModule2(this)};
	__fastcall TTestResource1(TComponent* Owner);
	void Get(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);
	//Custom SuffixName = zip/{z1}/{z2}
	void GetItem(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);
	void GetFullZip(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);
	void Post(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse);

Added setting to register function.

static void Register()
		std::auto_ptr<TEMSResourceAttributes> attributes(new TEMSResourceAttributes());
		attributes->ResourceName = "test";
		attributes->ResourceSuffix["GetItem"] = "{item}";
		//add to
		attributes->ResourceSuffix["GetFullZip"] = "zip/{z1}/{z2}";
		RegisterResource(__typeinfo(TTestResource1), attributes.release());

Place TFDConnection+TFDQuery on the other data module(TDataModule2).

//USEFORM("Unit2.cpp", DataModule2); of that time _libmain() file is commented out.

Add member functions.

class TDataModule2 : public TDataModule
__published:	// IDE で管理されるコンポーネント
	TFDConnection *FDConnection1;
	TFDQuery *FDQuery1;
private:	// ユーザー宣言
public:		// ユーザー宣言
	__fastcall TDataModule2(TComponent* Owner);

	//added this.
	TJSONObject* __fastcall zipnum_to_json(String p1, String p2);

Function implementation.

TJSONObject* __fastcall TDataModule2::zipnum_to_json(String p1, String p2)
	constexpr wchar_t zip_sql1[] 		= L" = '%s%s' ";
	constexpr wchar_t zip_sql2[] 		= L" like '%s%%' ";
	constexpr wchar_t ziptable_sql[] 	= L"select * from T_ZIP where zip_id";
	String zip_sql;
	TJSONArray* json1 = new TJSONArray();
		(p2.Length() == 0)?
			zip_sql = ziptable_sql + Format(zip_sql2, ARRAYOFCONST((p1))):
			zip_sql = ziptable_sql + Format(zip_sql1, ARRAYOFCONST((p1,p2)));
		FDQuery1->SQL->Text	= zip_sql;
		FDQuery1->Active    = true;
		while (! FDQuery1->Eof)
			TJSONObject* _line = new TJSONObject();
			for (auto Field:FDQuery1->Fields)
				_line->AddPair(new TJSONPair(Field->FieldName, Field->AsString));
			json1->Add(_line );
		FDQuery1->Active       = false;
	catch(Exception& e1)
		json1->Add(new TJSONPair("error", e1.Message));
	return new TJSONObject(new TJSONPair(L"zipnum_to_json",json1));


The argument of this function(zipnum_to_json) is the first half and the second half of the postal code.

Finally TJSONObject* is returned.

Add TDataModule2* test{new TDataModule2(this)}; to TTestResource1. Includes are also required.#include "Unit2.h"

void TTestResource1::GetFullZip(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse)
	String z1 = ARequest->Params->Values["z1"];
	String z2 = ARequest->Params->Values["z2"];
		test->zipnum_to_json(z1, z2),false);
void TTestResource1::GetItem(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse)
	String z1 = ARequest->Params->Values["item"];
		test->zipnum_to_json(z1, ""),false);


http://localhost:8080/test/zip/112/0004 to access.



Gold User, No rank,
Delphi / C++Builder blogger


Check out more tips and tricks in this development video: