-
Notifications
You must be signed in to change notification settings - Fork 8
/
DX.Data.Utils.pas
135 lines (115 loc) · 3.16 KB
/
DX.Data.Utils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
unit DX.Data.Utils;
interface
uses
System.Classes, System.SysUtils,
Data.DB,
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.FB, FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait,
FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet,
System.Generics.Collections;
type
TSimpleField = record
Name: string;
Value: Variant;
end;
IRecord = interface(IInterface)
function GetFields: TList<TSimpleField>;
procedure SetFields(const AValue: TList<TSimpleField>);
property Fields: TList<TSimpleField> read GetFields write SetFields;
function ValueByName(const AFieldName: string): Variant;
end;
TRecord = class(TInterfacedObject, IRecord)
strict private
FFields: TList<TSimpleField>;
strict protected
function GetFields: TList<TSimpleField>;
procedure SetFields(const AValue: TList<TSimpleField>);
public
constructor Create;
destructor Destroy; override;
property Fields: TList<TSimpleField> read GetFields write SetFields;
function ValueByName(const AFieldName: string): Variant;
end;
TFDDatasetHelper = class helper for TFDDataSet
public
function CloneRecord: IRecord;
procedure AssignRecord(const ASource: IRecord);
end;
TFDConnectionHelper = class helper for TFDConnection
public
procedure SetParamsFromConnectionString(const AConnectionString: string);
end;
implementation
{ TFDDatasetHelper }
procedure TFDDatasetHelper.AssignRecord(const ASource: IRecord);
var
LField: TField;
begin
for LField in Self.Fields do
begin
if (not LField.ReadOnly) and (LField.AutoGenerateValue = TAutoRefreshFlag.arNone) then
begin
LField.Value := ASource.ValueByName(LField.FieldName);
end;
end;
end;
function TFDDatasetHelper.CloneRecord: IRecord;
var
LField: TSimpleField;
LSourceField: TField;
begin
result := TRecord.Create;
for LSourceField in Self.Fields do
begin
LField.Name := LSourceField.FieldName;
LField.Value := LSourceField.Value;
result.Fields.Add(LField);
end;
end;
constructor TRecord.Create;
begin
inherited;
FFields := TList<TSimpleField>.Create;
end;
destructor TRecord.Destroy;
begin
FreeAndNIL(FFields);
inherited;
end;
function TRecord.GetFields: TList<TSimpleField>;
begin
result := FFields;
end;
procedure TRecord.SetFields(const AValue: TList<TSimpleField>);
begin
FFields := AValue;
end;
function TRecord.ValueByName(const AFieldName: string): Variant;
var
LField: TSimpleField;
begin
for LField in Fields do
begin
if SameText(LField.Name, AFieldName) then
begin
result := LField.Value;
break;
end;
end;
end;
{ TFDConnectionHelper }
procedure TFDConnectionHelper.SetParamsFromConnectionString(const AConnectionString: string);
var
LConnectionString: TStringList;
begin
Params.Clear;
LConnectionString := TStringList.Create;
try
LConnectionString.Delimiter := ';';
LConnectionString.DelimitedText := AConnectionString;
Params.AddStrings(LConnectionString);
finally
FreeAndNIL(LConnectionString);
end;
end;
end.