602
社区成员
发帖
与我相关
我的任务
分享
#include "System.Json.hpp"
#include "Data.FireDACJSONReflect.hpp"
TJSONObject* TServerMethods1::GetDepartmentNamesJSON()
{
FDQueryDepartmentNames->Close();
TFDJSONDataSets *ds = new TFDJSONDataSets();
TFDJSONDataSetsWriter::ListAdd(ds, FDQueryDepartmentNames);
TJSONObject *obj = new TJSONObject();
TFDJSONInterceptor::DataSetsToJSONObject(ds, obj);
return obj;
}
//----------------------------------------------------------------------------
#include <fmx.h>
//
#include <tchar.h>
#include <stdio.h>
#include <memory>
#pragma hdrstop
#include "ServerMethodsUnit1.h"
#include "ServerContainerUnit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma classgroup "FMX.Controls.TControl"
#pragma resource "*.dfm"
TServerContainer1 *ServerContainer1;
TComponent *FModule;
TDSServer *FDSServer;
TDSAuthenticationManager *FDSAuthenticationManager;
//---------------------------------------------------------------------------
__fastcall TServerContainer1::TServerContainer1(TComponent* Owner)
: TDataModule(Owner)
{
FDSServer = DSServer1;
FDSAuthenticationManager = DSAuthenticationManager1;
}
//---------------------------------------------------------------------------
__fastcall TServerContainer1::~TServerContainer1(void)
{
FDSServer = NULL;
FDSAuthenticationManager = NULL;
}
//----------------------------------------------------------------------------
void __fastcall TServerContainer1::DSServerClass1GetClass(TDSServerClass *DSServerClass,
TPersistentClass &PersistentClass)
{
PersistentClass = __classid(TServerMethods1);
}
//---------------------------------------------------------------------------
void __fastcall TServerContainer1::DSAuthenticationManager1UserAuthenticate(TObject *Sender,
const UnicodeString Protocol, const UnicodeString Context, const UnicodeString User,
const UnicodeString Password, bool &valid, TStrings *UserRoles)
{
if (User == "")
valid = false;
else
valid = true;
if (User == "admin")
UserRoles->Add("admins");
}
//----------------------------------------------------------------------------
TDSServer *DSServer(void)
{
return FDSServer;
}
TDSAuthenticationManager *DSAuthenticationManager(void)
{
return FDSAuthenticationManager;
}
//---------------------------------------------------------------------------
static void createContainer()
{
FModule = new TServerContainer1(NULL);
}
//---------------------------------------------------------------------------
static void freeContainer()
{
free(FModule);
}
//---------------------------------------------------------------------------
#pragma startup createContainer 33 //编译优先级 // 开始之前建立
#pragma exit freeContainer 33 // 退出之前释放
//freeWebModules表示在程序通过_exit退出之前,call名为 freeWebModules 这个函数。
//但是 freeWebModules 这个函数,必须是不携带参数,且不返回值的函数,即:void func(void);
// 33 这个优先级是可选的,但是必须在[64, 255]这个闭区间内。
// [1, 63]这个区间的优先级是用作ISO的startup和shutdown机制使用的,不要将优先级定义在此区间内。
// 如果不设定优先级,则默认设置为100。
#include "System.Json.hpp"
#include "Data.FireDACJSONReflect.hpp"
TJSONObject* TServerMethods1::GetDepartmentNamesJSON()
{
FDQueryDepartmentNames->Close();
TFDJSONDataSets *ds = new TFDJSONDataSets();
TFDJSONDataSetsWriter::ListAdd(ds, FDQueryDepartmentNames);
TJSONObject *obj = new TJSONObject();
TFDJSONInterceptor::DataSetsToJSONObject(ds, obj);
return obj;
}
虽然new 和delete不匹配,正常情况下也没问题的。 (正常情况是指client调用成功)
TFDJSONInterceptor::DataSetsToJSONObject 内部应该会delete(free) 指针ds的。 而返回值 obj 会由datasnap销毁。
class DECLSPEC_DRTTI TServerMethods1 : public TDataModule
{
__published: // IDE-managed Components
TFDStanStorageBinLink *FDStanStorageBinLink1;
TFDQuery *FDQueryDepartment;
TFDQuery *FDQueryDepartmentEmployees;
TFDGUIxWaitCursor *FDGUIxWaitCursor1;
TFDQuery *FDQueryDepartmentNames;
TFDConnection *FDConnectionEMPLOYEE;
private: // User declarations
public: // User declarations
__fastcall TServerMethods1(TComponent* Owner);
System::UnicodeString EchoString(System::UnicodeString value);
System::UnicodeString ReverseString(System::UnicodeString value);
TJSONObject* GetDepartmentNamesJSON();
TJSONObject* GetDepartmentEmployeesJSON(System::UnicodeString AID);
void ApplyChangesDepartmentEmployeesJSON(TJSONObject* AJSONObject);
};
//---------------------------------------------------------------------------
extern PACKAGE TServerMethods1 *ServerMethods1;
//---------------------------------------------------------------------------
实现
//----------------------------------------------------------------------------
const System::String sEmployees = "Employees";
const System::String sDepartment = "Department";
//----------------------------------------------------------------------------
TJSONObject* TServerMethods1::GetDepartmentNamesJSON()
{
FDQueryDepartmentNames->Close();
TFDJSONDataSets *ds = new TFDJSONDataSets();
TFDJSONDataSetsWriter::ListAdd(ds, FDQueryDepartmentNames);
TJSONObject *obj = new TJSONObject();
TFDJSONInterceptor::DataSetsToJSONObject(ds, obj);
return obj;
}
//----------------------------------------------------------------------------
TJSONObject* TServerMethods1::GetDepartmentEmployeesJSON(System::UnicodeString AID)
{
FDQueryDepartmentEmployees->Active = false;
FDQueryDepartment->Active = false;
FDQueryDepartment->Params->operator [](0)->Value = AID;
FDQueryDepartmentEmployees->Params->operator [](0)->Value = AID;
// Create dataset list
TFDJSONDataSets *ds = new TFDJSONDataSets();
// Add departments dataset
TFDJSONDataSetsWriter::ListAdd(ds, sDepartment, FDQueryDepartment);
// Add employees dataset
TFDJSONDataSetsWriter::ListAdd(ds, sEmployees, FDQueryDepartmentEmployees);
TJSONObject *obj = new TJSONObject();
TFDJSONInterceptor::DataSetsToJSONObject(ds, obj);
return obj;
}
//----------------------------------------------------------------------------
void TServerMethods1::ApplyChangesDepartmentEmployeesJSON(TJSONObject* AJSONObject)
{
TFDJSONDeltas *LDeltas = new TFDJSONDeltas();
TFDJSONInterceptor::JSONObjectToDataSets(AJSONObject, LDeltas);
TFDJSONErrors *errs = new TFDJSONErrors();
// Apply the department delta
TFDJSONDeltasApplyUpdates::ListApplyUpdates(LDeltas, sDepartment, FDQueryDepartment->Command, errs);
// If no errors, apply the employee delta
if (errs->Count == 0) {
TFDJSONDeltasApplyUpdates::ListApplyUpdates(LDeltas, sEmployees, FDQueryDepartmentEmployees->Command, errs);
}
// Raise an exception if any errors.
if (errs->Count > 0) {
throw new Exception(errs->Strings->Text);
}
}
//----------------------------------------------------------------------------