2018-01-29 17:30:53 +01:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2018-01-29 17:30:53 +01:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2017-04-24 00:19:53 +02:00
|
|
|
unit MVCFramework.DataSet.Utils;
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2017-04-24 00:19:53 +02:00
|
|
|
interface
|
|
|
|
|
2018-07-16 12:34:07 +02:00
|
|
|
uses
|
|
|
|
System.SysUtils,
|
2020-09-23 23:33:30 +02:00
|
|
|
System.Classes,
|
2018-07-16 12:34:07 +02:00
|
|
|
Data.DB,
|
|
|
|
System.Generics.Collections,
|
|
|
|
System.JSON,
|
|
|
|
System.Rtti,
|
|
|
|
JsonDataObjects,
|
|
|
|
MVCFramework.Commons,
|
2020-03-20 23:08:45 +01:00
|
|
|
MVCFramework.Serializer.Commons,
|
2020-09-18 01:06:24 +02:00
|
|
|
MVCFramework.RESTClient.Intf,
|
2020-09-02 16:13:29 +02:00
|
|
|
MVCFramework.RESTClient;
|
2017-04-24 00:19:53 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TFieldNamePolicy = (fpLowerCase, fpUpperCase, fpAsIs);
|
|
|
|
|
|
|
|
TDataSetHelper = class helper for TDataSet
|
|
|
|
public
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadFromTValue(const Value: TValue;
|
|
|
|
const aNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase);
|
2020-04-19 12:23:55 +02:00
|
|
|
function AsJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): string;
|
2020-10-19 12:26:45 +02:00
|
|
|
function AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase)
|
|
|
|
: TJDOJsonArray;
|
|
|
|
function MetadataAsJSONObject(FieldNameCase: TMVCNameCase = ncLowerCase)
|
|
|
|
: TJSONObject;
|
2019-10-24 15:34:40 +02:00
|
|
|
function AsJSONArrayOfValues: TJDOJsonArray;
|
2017-04-24 00:19:53 +02:00
|
|
|
function AsJSONArrayString: string; deprecated 'Use AsJSONArray';
|
2020-10-19 12:26:45 +02:00
|
|
|
function AsJSONObject(FieldNameCase: TMVCNameCase = ncLowerCase;
|
|
|
|
const IgnoredFields: TArray<string> = nil): string;
|
2017-04-24 00:19:53 +02:00
|
|
|
function AsJSONObjectString: string; deprecated 'Use AsJSONObject';
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadFromJSONObject(const JSONObject: TJSONObject;
|
|
|
|
const FieldNameCase: TMVCNameCase); overload;
|
|
|
|
procedure LoadFromJSONObject(const JSONObject: TJSONObject;
|
|
|
|
const AIgnoredFields: TArray<string> = nil;
|
2020-04-19 12:23:55 +02:00
|
|
|
const FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
2020-04-23 17:20:53 +02:00
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
procedure LoadFromJSONArray(AJSONArray: string;
|
2020-04-19 12:23:55 +02:00
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
2018-12-17 00:39:29 +01:00
|
|
|
procedure LoadFromJSONArray(AJSONArray: TJSONArray;
|
2020-04-23 17:20:53 +02:00
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadJSONArrayFromJSONObjectProperty(PropertyName: string;
|
2020-04-23 17:20:53 +02:00
|
|
|
JSONObject: string;
|
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadJSONArrayFromJSONObjectProperty(PropertyName: string;
|
2020-04-23 17:20:53 +02:00
|
|
|
JSONObject: TJSONObject;
|
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadJSONObjectFromJSONObjectProperty(PropertyName: string;
|
2020-04-23 17:20:53 +02:00
|
|
|
JSONObject: string;
|
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadJSONObjectFromJSONObjectProperty(PropertyName: string;
|
2020-04-23 17:20:53 +02:00
|
|
|
JSONObject: TJSONObject;
|
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
|
|
|
|
|
|
|
procedure LoadFromJSONArrayString(AJSONArrayString: string;
|
|
|
|
AIgnoredFields: TArray<string>;
|
|
|
|
FieldNameCase: TMVCNameCase = ncLowerCase); overload; deprecated;
|
|
|
|
procedure LoadFromJSONArrayString(AJSONArrayString: string;
|
2020-04-19 12:23:55 +02:00
|
|
|
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
|
2017-04-24 00:19:53 +02:00
|
|
|
procedure LoadFromJSONObjectString(AJSONObjectString: string); overload;
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure LoadFromJSONObjectString(const JSONObjectString: string;
|
|
|
|
const IgnoredFields: TArray<string>;
|
2020-04-19 12:23:55 +02:00
|
|
|
const FieldNameCase: TMVCNameCase = ncLowerCase); overload;
|
2020-04-23 17:20:53 +02:00
|
|
|
// procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string;
|
|
|
|
// const FieldNameCase: TMVCNameCase = ncLowerCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
procedure AppendFromJSONArrayString(AJSONArrayString: string); overload;
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure AppendFromJSONArrayString(AJSONArrayString: string;
|
|
|
|
AIgnoredFields: TArray<string>;
|
2020-04-19 12:23:55 +02:00
|
|
|
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
|
2020-10-19 12:26:45 +02:00
|
|
|
function AsObjectList<T: class, constructor>(CloseAfterScroll
|
|
|
|
: boolean = false; OwnsObjects: boolean = true): TObjectList<T>;
|
|
|
|
function AsObject<T: class, constructor>(CloseAfterScroll
|
|
|
|
: boolean = false): T;
|
2020-03-20 23:08:45 +01:00
|
|
|
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TDataSetUtils = class sealed
|
|
|
|
private
|
|
|
|
class var CTX: TRttiContext;
|
|
|
|
public
|
|
|
|
class constructor Create;
|
|
|
|
class destructor Destroy;
|
|
|
|
class procedure DataSetToObject(ADataSet: TDataSet; AObject: TObject);
|
2020-10-19 12:26:45 +02:00
|
|
|
class procedure DataSetToObjectList<T: class, constructor>
|
|
|
|
(ADataSet: TDataSet; AObjectList: TObjectList<T>;
|
2019-03-10 16:29:18 +01:00
|
|
|
ACloseDataSetAfterScroll: boolean = true);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-07-16 12:34:07 +02:00
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
TDataSetHolder = class
|
|
|
|
private
|
2020-03-20 23:08:45 +01:00
|
|
|
fDataSet: TDataSet;
|
|
|
|
fMetadata: TMVCStringDictionary;
|
|
|
|
fOwns: boolean;
|
|
|
|
fDataSetSerializationType: TMVCDatasetSerializationType;
|
2018-07-16 12:34:07 +02:00
|
|
|
public
|
2019-09-30 00:05:46 +02:00
|
|
|
constructor Create(const ADataSet: TDataSet; const AOwns: boolean = false;
|
2020-10-19 12:26:45 +02:00
|
|
|
const ADataSetSerializationType
|
|
|
|
: TMVCDatasetSerializationType = TMVCDatasetSerializationType.
|
2019-09-30 00:05:46 +02:00
|
|
|
dstAllRecords); virtual;
|
2018-07-16 12:34:07 +02:00
|
|
|
destructor Destroy; override;
|
2019-09-30 00:05:46 +02:00
|
|
|
function SerializationType: TMVCDatasetSerializationType;
|
|
|
|
[MVCNameAs('data')]
|
2020-03-20 23:08:45 +01:00
|
|
|
property Items: TDataSet read fDataSet;
|
2018-07-16 12:34:07 +02:00
|
|
|
[MVCNameAs('meta')]
|
2020-03-20 23:08:45 +01:00
|
|
|
property Metadata: TMVCStringDictionary read fMetadata;
|
2020-04-20 17:56:17 +02:00
|
|
|
end deprecated 'Use function "ObjectDict(boolean)" instead';
|
2020-03-20 23:08:45 +01:00
|
|
|
|
|
|
|
TMVCAPIBinder = class
|
2020-10-19 12:26:45 +02:00
|
|
|
protected type
|
2020-03-20 23:08:45 +01:00
|
|
|
TMVCAPIBinderItem = class
|
|
|
|
private
|
2020-09-18 01:06:24 +02:00
|
|
|
fRESTClient: IMVCRESTClient;
|
2020-03-20 23:08:45 +01:00
|
|
|
fDataSet: TDataSet;
|
|
|
|
fURI: string;
|
2020-09-02 16:13:29 +02:00
|
|
|
fPrimaryKeyNAme: string;
|
2020-03-20 23:08:45 +01:00
|
|
|
fLoading: boolean;
|
2020-09-18 01:06:24 +02:00
|
|
|
procedure ShowError(const AResponse: IMVCRESTResponse);
|
2020-03-20 23:08:45 +01:00
|
|
|
public
|
2020-09-18 01:06:24 +02:00
|
|
|
constructor Create(const aRESTClient: IMVCRESTClient; const ADataSet: TDataSet;
|
2020-03-20 23:08:45 +01:00
|
|
|
const aURI, aPrimaryKeyName: string);
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure HookBeforePost(DataSet: TDataSet);
|
|
|
|
procedure HookBeforeDelete(DataSet: TDataSet);
|
|
|
|
procedure HookBeforeRefresh(DataSet: TDataSet);
|
|
|
|
procedure HookAfterOpen(DataSet: TDataSet);
|
|
|
|
// procedure HookBeforeRowRequest(DataSet: TFDDataSet);
|
|
|
|
end;
|
|
|
|
private
|
2020-09-18 01:06:24 +02:00
|
|
|
fRESTClient: IMVCRESTClient;
|
2020-03-20 23:08:45 +01:00
|
|
|
|
|
|
|
protected
|
|
|
|
fItems: TObjectList<TMVCAPIBinderItem>;
|
|
|
|
public
|
2020-09-18 01:06:24 +02:00
|
|
|
constructor Create(const aRESTClient: IMVCRESTClient);
|
2020-03-20 23:08:45 +01:00
|
|
|
destructor Destroy; override;
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure BindDataSetToAPI(const ADataSet: TDataSet; const aURI: string;
|
|
|
|
const aPrimaryKeyName: string);
|
2018-07-16 12:34:07 +02:00
|
|
|
end;
|
|
|
|
|
2017-04-24 00:19:53 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2020-05-25 19:34:14 +02:00
|
|
|
System.TypInfo,
|
2018-07-16 12:34:07 +02:00
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
2017-04-24 00:19:53 +02:00
|
|
|
MVCFramework.Serializer.Intf;
|
|
|
|
|
|
|
|
{ TDataSetHelper }
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromTValue(const Value: TValue;
|
|
|
|
const aNameCase: TMVCNameCase);
|
2017-09-28 00:14:34 +02:00
|
|
|
var
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
begin
|
2019-03-10 16:29:18 +01:00
|
|
|
if not({$IFDEF TOKYOORBETTER}Value.IsObjectInstance and
|
2019-05-16 00:16:55 +02:00
|
|
|
{$ENDIF} (Value.AsObject is TJDOJsonArray)) then
|
2020-10-19 12:26:45 +02:00
|
|
|
raise Exception.Create
|
|
|
|
('LoadFromTValue requires a TValue containing a TJDOJsonArray');
|
2017-09-28 00:14:34 +02:00
|
|
|
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
2020-10-19 12:26:45 +02:00
|
|
|
lSer.JsonArrayToDataSet(TJSONArray(Value.AsObject), Self, [],
|
|
|
|
TMVCNameCase.ncLowerCase);
|
2017-09-28 00:14:34 +02:00
|
|
|
finally
|
|
|
|
lSer.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2020-04-23 17:20:53 +02:00
|
|
|
// procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string;
|
|
|
|
// const aPropertyName: string; const FieldNameCase: TMVCNameCase);
|
|
|
|
// var
|
|
|
|
// lJson: TJSONObject;
|
|
|
|
// begin
|
|
|
|
// lJson := TJSONObject.Create;
|
|
|
|
// try
|
|
|
|
// lJson.FromJSON(AJSONObjectString);
|
|
|
|
// LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase);
|
|
|
|
// finally
|
|
|
|
// lJson.Free;
|
|
|
|
// end;
|
|
|
|
// end;
|
2019-11-27 19:04:06 +01:00
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
function TDataSetHelper.AsJDOJSONArray(FieldNameCase
|
|
|
|
: TMVCNameCase = ncLowerCase): TJDOJsonArray;
|
2019-05-16 00:16:55 +02:00
|
|
|
var
|
|
|
|
lSerializer: TMVCJsonDataObjectsSerializer;
|
|
|
|
begin
|
|
|
|
Result := TJDOJsonArray.Create;
|
|
|
|
try
|
|
|
|
if not Eof then
|
|
|
|
begin
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
lSerializer.DataSetToJsonArray(Self, Result, FieldNameCase, []);
|
2019-05-16 00:16:55 +02:00
|
|
|
finally
|
|
|
|
lSerializer.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-10-24 15:34:40 +02:00
|
|
|
function TDataSetHelper.AsJSONArrayOfValues: TJDOJsonArray;
|
|
|
|
var
|
|
|
|
lSerializer: TMVCJsonDataObjectsSerializer;
|
|
|
|
begin
|
|
|
|
Result := TJDOJsonArray.Create;
|
|
|
|
try
|
|
|
|
if not Eof then
|
|
|
|
begin
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
|
|
|
lSerializer.DataSetToJsonArrayOfValues(Self, Result, []);
|
|
|
|
finally
|
|
|
|
lSerializer.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
function TDataSetHelper.AsJSONArray(FieldNameCase
|
|
|
|
: TMVCNameCase = ncLowerCase): string;
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
|
|
|
lSerializer: IMVCSerializer;
|
|
|
|
begin
|
|
|
|
Result := '[]';
|
|
|
|
if not Eof then
|
|
|
|
begin
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-04-19 12:23:55 +02:00
|
|
|
Result := lSerializer.SerializeDataSet(Self, [], FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDataSetHelper.AsJSONArrayString: string;
|
|
|
|
begin
|
|
|
|
Result := AsJSONArray;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
function TDataSetHelper.AsJSONObject(FieldNameCase: TMVCNameCase;
|
|
|
|
const IgnoredFields: TArray<string>): string;
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
|
|
|
lSerializer: IMVCSerializer;
|
|
|
|
begin
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-10-19 12:26:45 +02:00
|
|
|
Result := lSerializer.SerializeDataSetRecord(Self,
|
|
|
|
TMVCIgnoredList(IgnoredFields), FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TDataSetHelper.AsJSONObjectString: string;
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
Result := AsJSONObject(ncLowerCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TDataSetHelper.AsObject<T>(CloseAfterScroll: boolean): T;
|
|
|
|
var
|
|
|
|
Obj: T;
|
|
|
|
begin
|
|
|
|
if not Self.Eof then
|
|
|
|
begin
|
|
|
|
Obj := T.Create;
|
|
|
|
try
|
|
|
|
TDataSetUtils.DataSetToObject(Self, Obj);
|
|
|
|
Result := Obj;
|
|
|
|
except
|
|
|
|
FreeAndNil(Obj);
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := nil;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
function TDataSetHelper.AsObjectList<T>(CloseAfterScroll: boolean;
|
|
|
|
OwnsObjects: boolean): TObjectList<T>;
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
lObjs: TObjectList<T>;
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
lObjs := TObjectList<T>.Create(OwnsObjects);
|
2017-04-24 00:19:53 +02:00
|
|
|
try
|
2019-01-08 12:48:27 +01:00
|
|
|
TDataSetUtils.DataSetToObjectList<T>(Self, lObjs, CloseAfterScroll);
|
|
|
|
Result := lObjs;
|
2017-04-24 00:19:53 +02:00
|
|
|
except
|
2019-01-08 12:48:27 +01:00
|
|
|
FreeAndNil(lObjs);
|
2017-04-24 00:19:53 +02:00
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
function TDataSetHelper.MetadataAsJSONObject(FieldNameCase: TMVCNameCase)
|
|
|
|
: TJSONObject;
|
2020-09-23 23:33:30 +02:00
|
|
|
var
|
|
|
|
I: Integer;
|
|
|
|
lObj: TJSONObject;
|
|
|
|
lJArr: TJSONArray;
|
|
|
|
begin
|
|
|
|
|
|
|
|
Result := TJSONObject.Create;
|
|
|
|
try
|
|
|
|
lJArr := Result.A['fielddefs'];
|
|
|
|
for I := 0 to FieldDefs.Count - 1 do
|
|
|
|
begin
|
|
|
|
lObj := lJArr.AddObject;
|
2020-10-19 12:26:45 +02:00
|
|
|
lObj.S['fieldname'] := TMVCSerializerHelper.ApplyNameCase(FieldNameCase,
|
|
|
|
FieldDefList[I].Name);
|
2020-09-23 23:33:30 +02:00
|
|
|
lObj.S['displayname'] := FieldDefList[I].DisplayName;
|
|
|
|
lObj.I['datatype'] := Ord(FieldDefList[I].DataType);
|
|
|
|
lObj.I['size'] := FieldDefList[I].Size;
|
|
|
|
lObj.I['precision'] := FieldDefList[I].Precision;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string;
|
|
|
|
FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
2021-04-14 15:53:02 +02:00
|
|
|
lSerializer: IMVCSerializer;
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
|
|
|
Self.DisableControls;
|
|
|
|
try
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-04-19 12:23:55 +02:00
|
|
|
lSerializer.DeserializeDataSet(AJSONArray, Self, nil, FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
finally
|
|
|
|
Self.EnableControls;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string;
|
|
|
|
AIgnoredFields: TArray<string>; FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
AppendFromJSONArrayString(AJSONArrayString, AIgnoredFields, FieldNameCase);
|
2018-12-17 00:39:29 +01:00
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray;
|
|
|
|
FieldNameCase: TMVCNameCase);
|
2018-12-17 00:39:29 +01:00
|
|
|
var
|
2021-04-14 15:53:02 +02:00
|
|
|
lSerializer: IMVCSerializer;
|
2018-12-17 00:39:29 +01:00
|
|
|
lBookmark: TArray<Byte>;
|
|
|
|
begin
|
|
|
|
lBookmark := Self.Bookmark;
|
2017-04-24 00:19:53 +02:00
|
|
|
Self.DisableControls;
|
|
|
|
try
|
2018-12-17 00:39:29 +01:00
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2021-04-14 15:53:02 +02:00
|
|
|
TMVCJsonDataObjectsSerializer(lSerializer).JsonArrayToDataSet(AJSONArray, Self, nil, FieldNameCase);
|
2018-12-17 00:39:29 +01:00
|
|
|
if Self.BookmarkValid(lBookmark) then
|
|
|
|
Self.GotoBookmark(lBookmark);
|
2017-04-24 00:19:53 +02:00
|
|
|
finally
|
|
|
|
Self.EnableControls;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty
|
|
|
|
(PropertyName: string; JSONObject: TJSONObject; FieldNameCase: TMVCNameCase);
|
2020-04-23 17:20:53 +02:00
|
|
|
begin
|
|
|
|
LoadFromJSONArray(JSONObject.A[PropertyName], FieldNameCase);
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadJSONObjectFromJSONObjectProperty
|
|
|
|
(PropertyName: string; JSONObject: TJSONObject; FieldNameCase: TMVCNameCase);
|
2020-04-23 17:20:53 +02:00
|
|
|
begin
|
|
|
|
LoadFromJSONObject(JSONObject.O[PropertyName], FieldNameCase);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDataSetHelper.LoadJSONObjectFromJSONObjectProperty(PropertyName,
|
|
|
|
JSONObject: string; FieldNameCase: TMVCNameCase);
|
|
|
|
var
|
|
|
|
lJObj: TJSONObject;
|
|
|
|
begin
|
|
|
|
lJObj := StrToJSONObject(JSONObject);
|
|
|
|
try
|
|
|
|
LoadJSONObjectFromJSONObjectProperty(PropertyName, lJObj, FieldNameCase);
|
|
|
|
finally
|
|
|
|
lJObj.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(PropertyName,
|
|
|
|
JSONObject: string; FieldNameCase: TMVCNameCase);
|
|
|
|
var
|
|
|
|
lJson: TJSONObject;
|
|
|
|
begin
|
|
|
|
lJson := StrToJSONObject(JSONObject);
|
|
|
|
try
|
|
|
|
LoadJSONArrayFromJSONObjectProperty(PropertyName, lJson, FieldNameCase);
|
|
|
|
finally
|
|
|
|
lJson.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string;
|
|
|
|
FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-10-19 12:26:45 +02:00
|
|
|
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(),
|
|
|
|
FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string;
|
|
|
|
AIgnoredFields: TArray<string>; FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
LoadFromJSONArray(AJSONArrayString, FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string);
|
|
|
|
begin
|
|
|
|
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create());
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject;
|
|
|
|
const AIgnoredFields: TArray<string>; const FieldNameCase: TMVCNameCase);
|
2020-04-23 17:20:53 +02:00
|
|
|
var
|
2021-04-14 15:53:02 +02:00
|
|
|
lSerializer: IMVCSerializer;
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-04-23 17:20:53 +02:00
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2021-04-14 15:53:02 +02:00
|
|
|
TMVCJsonDataObjectsSerializer(lSerializer).JsonObjectToDataSet(JSONObject, Self,
|
2020-10-19 12:26:45 +02:00
|
|
|
TMVCIgnoredList(AIgnoredFields), FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString
|
|
|
|
: string; const IgnoredFields: TArray<string>;
|
2020-04-19 12:23:55 +02:00
|
|
|
const FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
|
|
|
lSerializer: IMVCSerializer;
|
|
|
|
begin
|
|
|
|
lSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-10-19 12:26:45 +02:00
|
|
|
lSerializer.DeserializeDataSetRecord(JSONObjectString, Self,
|
|
|
|
TMVCIgnoredList(IgnoredFields), FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject;
|
|
|
|
const FieldNameCase: TMVCNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
LoadFromJSONObject(JSONObject, TArray<string>.Create(), FieldNameCase);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDataSetHelper.LoadFromJSONObjectString(AJSONObjectString: string);
|
|
|
|
begin
|
|
|
|
LoadFromJSONObjectString(AJSONObjectString, TArray<string>.Create());
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TDataSetUtils }
|
|
|
|
|
|
|
|
class constructor TDataSetUtils.Create;
|
|
|
|
begin
|
|
|
|
TDataSetUtils.CTX := TRttiContext.Create;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
class procedure TDataSetUtils.DataSetToObject(ADataSet: TDataSet;
|
|
|
|
AObject: TObject);
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
|
|
|
_type: TRttiType;
|
|
|
|
_fields: TArray<TRttiProperty>;
|
2020-02-05 23:46:38 +01:00
|
|
|
lRttiProp: TRttiProperty;
|
2017-04-24 00:19:53 +02:00
|
|
|
_attribute: TCustomAttribute;
|
|
|
|
_dict: TDictionary<string, string>;
|
|
|
|
_keys: TDictionary<string, boolean>;
|
|
|
|
mf: MVCColumnAttribute;
|
|
|
|
field_name: string;
|
|
|
|
Value: TValue;
|
2020-03-08 19:35:17 +01:00
|
|
|
lNeedToSet, FoundAttribute: boolean;
|
2017-04-24 00:19:53 +02:00
|
|
|
FoundTransientAttribute: boolean;
|
2018-03-14 12:30:41 +01:00
|
|
|
LField: TField;
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
|
|
|
_dict := TDictionary<string, string>.Create();
|
|
|
|
_keys := TDictionary<string, boolean>.Create();
|
|
|
|
_type := CTX.GetType(AObject.ClassInfo);
|
|
|
|
_fields := _type.GetProperties;
|
2020-02-05 23:46:38 +01:00
|
|
|
for lRttiProp in _fields do
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
|
|
|
FoundAttribute := false;
|
|
|
|
FoundTransientAttribute := false;
|
2020-02-05 23:46:38 +01:00
|
|
|
for _attribute in lRttiProp.GetAttributes do
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
|
|
|
if _attribute is MVCColumnAttribute then
|
|
|
|
begin
|
2019-03-10 16:29:18 +01:00
|
|
|
FoundAttribute := true;
|
2017-04-24 00:19:53 +02:00
|
|
|
mf := MVCColumnAttribute(_attribute);
|
2020-02-05 23:46:38 +01:00
|
|
|
_dict.Add(lRttiProp.Name, mf.FieldName);
|
|
|
|
_keys.Add(lRttiProp.Name, mf.IsPK);
|
2017-04-24 00:19:53 +02:00
|
|
|
end
|
|
|
|
else if _attribute is MVCDoNotSerializeAttribute then
|
2019-03-10 16:29:18 +01:00
|
|
|
FoundTransientAttribute := true;
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
if ((not FoundAttribute) and (not FoundTransientAttribute)) then
|
|
|
|
begin
|
2020-02-05 23:46:38 +01:00
|
|
|
_dict.Add(lRttiProp.Name, lRttiProp.Name);
|
|
|
|
_keys.Add(lRttiProp.Name, false);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
2020-02-05 23:46:38 +01:00
|
|
|
for lRttiProp in _fields do
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2020-02-05 23:46:38 +01:00
|
|
|
if not _dict.TryGetValue(lRttiProp.Name, field_name) then
|
2017-04-24 00:19:53 +02:00
|
|
|
Continue;
|
2018-03-14 12:30:41 +01:00
|
|
|
LField := ADataSet.FindField(field_name);
|
|
|
|
if not Assigned(LField) then
|
|
|
|
Continue;
|
2020-03-08 19:35:17 +01:00
|
|
|
lNeedToSet := true;
|
2020-02-05 23:46:38 +01:00
|
|
|
case lRttiProp.PropertyType.TypeKind of
|
2017-04-24 00:19:53 +02:00
|
|
|
tkEnumeration: // tristan
|
|
|
|
begin
|
2020-02-05 23:46:38 +01:00
|
|
|
if lRttiProp.PropertyType.Handle = TypeInfo(boolean) then
|
2017-04-24 00:19:53 +02:00
|
|
|
begin
|
2018-03-14 12:30:41 +01:00
|
|
|
case LField.DataType of
|
2017-04-24 00:19:53 +02:00
|
|
|
ftInteger, ftSmallint, ftLargeint:
|
|
|
|
begin
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := (LField.AsInteger = 1);
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
ftBoolean:
|
|
|
|
begin
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsBoolean;
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
else
|
|
|
|
Continue;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
tkInteger:
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsInteger;
|
2017-04-24 00:19:53 +02:00
|
|
|
tkInt64:
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsLargeInt;
|
2017-04-24 00:19:53 +02:00
|
|
|
tkFloat:
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsFloat;
|
2017-04-24 00:19:53 +02:00
|
|
|
tkString:
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsString;
|
2017-04-24 00:19:53 +02:00
|
|
|
tkUString, tkWChar, tkLString, tkWString:
|
2018-03-14 12:30:41 +01:00
|
|
|
Value := LField.AsWideString;
|
2020-03-08 19:35:17 +01:00
|
|
|
tkRecord:
|
|
|
|
begin
|
2020-10-19 12:26:45 +02:00
|
|
|
MapDataSetFieldToNullableRTTIProperty(lRttiProp.GetValue(AObject),
|
|
|
|
LField, lRttiProp, AObject);
|
2020-03-08 19:35:17 +01:00
|
|
|
lNeedToSet := false;
|
|
|
|
end
|
2017-04-24 00:19:53 +02:00
|
|
|
else
|
|
|
|
Continue;
|
|
|
|
end;
|
2020-03-08 19:35:17 +01:00
|
|
|
if lNeedToSet then
|
|
|
|
begin
|
|
|
|
lRttiProp.SetValue(AObject, Value);
|
|
|
|
end;
|
2017-04-24 00:19:53 +02:00
|
|
|
end;
|
|
|
|
_dict.Free;
|
|
|
|
_keys.Free;
|
|
|
|
end;
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
class procedure TDataSetUtils.DataSetToObjectList<T>(ADataSet: TDataSet;
|
|
|
|
AObjectList: TObjectList<T>; ACloseDataSetAfterScroll: boolean);
|
2017-04-24 00:19:53 +02:00
|
|
|
var
|
|
|
|
Obj: T;
|
|
|
|
SavedPosition: TArray<Byte>;
|
|
|
|
begin
|
|
|
|
ADataSet.DisableControls;
|
|
|
|
try
|
|
|
|
SavedPosition := ADataSet.Bookmark;
|
|
|
|
while not ADataSet.Eof do
|
|
|
|
begin
|
|
|
|
Obj := T.Create;
|
|
|
|
DataSetToObject(ADataSet, Obj);
|
|
|
|
AObjectList.Add(Obj);
|
|
|
|
ADataSet.Next;
|
|
|
|
end;
|
|
|
|
if ADataSet.BookmarkValid(SavedPosition) then
|
|
|
|
ADataSet.Bookmark := SavedPosition;
|
|
|
|
finally
|
|
|
|
ADataSet.EnableControls;
|
|
|
|
end;
|
|
|
|
if ACloseDataSetAfterScroll then
|
|
|
|
ADataSet.Close;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class destructor TDataSetUtils.Destroy;
|
|
|
|
begin
|
|
|
|
CTX.Free;
|
|
|
|
end;
|
|
|
|
|
2018-07-16 12:34:07 +02:00
|
|
|
{ TDataSetHolder }
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
constructor TDataSetHolder.Create(const ADataSet: TDataSet;
|
|
|
|
const AOwns: boolean = false; const ADataSetSerializationType
|
|
|
|
: TMVCDatasetSerializationType = TMVCDatasetSerializationType.dstAllRecords);
|
2018-07-16 12:34:07 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2020-03-20 23:08:45 +01:00
|
|
|
fDataSet := ADataSet;
|
|
|
|
fMetadata := TMVCStringDictionary.Create;
|
|
|
|
fOwns := AOwns;
|
|
|
|
fDataSetSerializationType := ADataSetSerializationType;
|
2018-07-16 12:34:07 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TDataSetHolder.Destroy;
|
|
|
|
begin
|
2020-03-20 23:08:45 +01:00
|
|
|
fMetadata.Free;
|
|
|
|
if fOwns then
|
2019-09-30 00:05:46 +02:00
|
|
|
begin
|
2020-03-20 23:08:45 +01:00
|
|
|
fDataSet.Free;
|
2019-09-30 00:05:46 +02:00
|
|
|
end;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDataSetHolder.SerializationType: TMVCDatasetSerializationType;
|
|
|
|
begin
|
2020-03-20 23:08:45 +01:00
|
|
|
Result := fDataSetSerializationType;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TMVCAPIBinder }
|
|
|
|
|
2020-10-19 12:26:45 +02:00
|
|
|
procedure TMVCAPIBinder.BindDataSetToAPI(const ADataSet: TDataSet;
|
|
|
|
const aURI, aPrimaryKeyName: string);
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
2020-10-19 12:26:45 +02:00
|
|
|
fItems.Add(TMVCAPIBinderItem.Create(fRESTClient, ADataSet, aURI,
|
|
|
|
aPrimaryKeyName));
|
2020-03-20 23:08:45 +01:00
|
|
|
end;
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
constructor TMVCAPIBinder.Create(const aRESTClient: IMVCRESTClient);
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fItems := TObjectList<TMVCAPIBinderItem>.Create(true);
|
|
|
|
fRESTClient := aRESTClient;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCAPIBinder.Destroy;
|
|
|
|
begin
|
|
|
|
fItems.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TMVCAPIBinder.TMVCAPIBinderItem }
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
constructor TMVCAPIBinder.TMVCAPIBinderItem.Create(const aRESTClient: IMVCRESTClient; const ADataSet: TDataSet;
|
2020-03-20 23:08:45 +01:00
|
|
|
const aURI, aPrimaryKeyName: string);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fRESTClient := aRESTClient;
|
|
|
|
fDataSet := ADataSet;
|
|
|
|
fURI := aURI;
|
2020-09-02 16:13:29 +02:00
|
|
|
fPrimaryKeyNAme := aPrimaryKeyName;
|
2020-03-20 23:08:45 +01:00
|
|
|
|
|
|
|
fDataSet.BeforePost := HookBeforePost;
|
|
|
|
fDataSet.BeforeDelete := HookBeforeDelete;
|
|
|
|
fDataSet.BeforeRefresh := HookBeforeRefresh;
|
|
|
|
fDataSet.AfterOpen := HookAfterOpen;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCAPIBinder.TMVCAPIBinderItem.Destroy;
|
|
|
|
begin
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCAPIBinder.TMVCAPIBinderItem.HookAfterOpen(DataSet: TDataSet);
|
|
|
|
var
|
2020-09-18 01:06:24 +02:00
|
|
|
Res: IMVCRESTResponse;
|
2020-03-20 23:08:45 +01:00
|
|
|
lData: TJSONObject;
|
|
|
|
begin
|
2020-06-25 00:09:07 +02:00
|
|
|
|
2020-03-20 23:08:45 +01:00
|
|
|
// this a simple sychronous request...
|
2020-09-18 01:06:24 +02:00
|
|
|
Res := fRESTClient.Get(fURI);
|
2020-09-24 20:03:11 +02:00
|
|
|
if not Res.Success then
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
|
|
|
ShowError(Res);
|
|
|
|
end;
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
lData := StrToJSONObject(Res.Content);
|
2020-03-20 23:08:45 +01:00
|
|
|
try
|
|
|
|
DataSet.DisableControls;
|
|
|
|
try
|
|
|
|
fLoading := true;
|
2020-06-25 00:09:07 +02:00
|
|
|
DataSet.LoadFromJSONArray(lData.A['data']);
|
2020-03-20 23:08:45 +01:00
|
|
|
fLoading := false;
|
|
|
|
DataSet.First;
|
|
|
|
finally
|
|
|
|
DataSet.EnableControls;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lData.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCAPIBinder.TMVCAPIBinderItem.HookBeforeDelete(DataSet: TDataSet);
|
|
|
|
var
|
2020-09-18 01:06:24 +02:00
|
|
|
Res: IMVCRESTResponse;
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
|
|
|
if DataSet.State = dsBrowse then
|
2020-10-19 12:26:45 +02:00
|
|
|
Res := fRESTClient.DataSetDelete(fURI, DataSet.FieldByName(fPrimaryKeyNAme)
|
|
|
|
.AsString);
|
2020-09-18 01:06:24 +02:00
|
|
|
if not(Res.StatusCode in [200]) then
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
|
|
|
ShowError(Res);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCAPIBinder.TMVCAPIBinderItem.HookBeforePost(DataSet: TDataSet);
|
|
|
|
var
|
2020-09-18 01:06:24 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2020-03-20 23:08:45 +01:00
|
|
|
lLastID: Integer;
|
|
|
|
begin
|
|
|
|
if not fLoading then
|
|
|
|
begin
|
|
|
|
lLastID := -1;
|
|
|
|
if fDataSet.State = dsInsert then
|
|
|
|
begin
|
|
|
|
lRes := fRESTClient.DataSetInsert(fURI, DataSet)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-09-02 16:13:29 +02:00
|
|
|
lLastID := fDataSet.FieldByName(fPrimaryKeyNAme).AsInteger;
|
2020-09-18 01:06:24 +02:00
|
|
|
lRes := fRESTClient.DataSetUpdate(fURI, lLastID.ToString, DataSet);
|
2020-03-20 23:08:45 +01:00
|
|
|
end;
|
2020-09-18 01:06:24 +02:00
|
|
|
if not(lRes.StatusCode in [200, 201]) then
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
|
|
|
ShowError(lRes);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
DataSet.Refresh;
|
|
|
|
if lLastID > -1 then
|
|
|
|
begin
|
|
|
|
DataSet.Locate('id', lLastID, []);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCAPIBinder.TMVCAPIBinderItem.HookBeforeRefresh(DataSet: TDataSet);
|
|
|
|
begin
|
|
|
|
DataSet.Close;
|
|
|
|
DataSet.Open;
|
|
|
|
end;
|
|
|
|
|
2020-09-18 01:06:24 +02:00
|
|
|
procedure TMVCAPIBinder.TMVCAPIBinderItem.ShowError(const AResponse: IMVCRESTResponse);
|
2020-03-20 23:08:45 +01:00
|
|
|
begin
|
2020-09-18 01:06:24 +02:00
|
|
|
if not AResponse.Success then
|
2020-09-02 16:13:29 +02:00
|
|
|
raise EMVCException.Create(
|
2020-09-18 01:06:24 +02:00
|
|
|
AResponse.StatusCode.ToString + ': ' + AResponse.StatusText + sLineBreak + AResponse.Content)
|
2020-06-25 00:09:07 +02:00
|
|
|
else
|
2020-09-18 01:06:24 +02:00
|
|
|
raise EMVCException.Create(AResponse.Content);
|
2018-07-16 12:34:07 +02:00
|
|
|
end;
|
|
|
|
|
2017-04-24 00:19:53 +02:00
|
|
|
end.
|