-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmValue.pas
415 lines (348 loc) · 10.2 KB
/
mValue.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
unit mValue; // mValue
interface
uses
Classes, SysUtils, StrUtils;
type
TTipoField = (
tfKey,
tfReq,
tfNul);
TTipoValue = (
tvBoolean,
tvDateTime,
tvFloat,
tvInteger,
tvString);
TmValue = class;
TmValueClass = class of TmValue;
TmValue = class
private
fNome : String;
fTipoField : TTipoField;
function GetTipo: TTipoValue;
protected
function GetValueBase() : String; virtual; abstract;
function GetValueInt() : String; virtual; abstract;
function GetValueStr() : String; virtual; abstract;
procedure SetValueStr(const Value : String); virtual; abstract;
public
published
property Nome : String read fNome write fNome;
property Tipo : TTipoValue read GetTipo;
property TipoField : TTipoField read fTipoField write fTipoField;
property ValueInt : String read GetValueInt;
property ValueStr : String read GetValueStr write SetValueStr;
end;
//-- database
TmValueBool = class(TmValue)
private
fValue : Boolean;
protected
function GetValueBase() : String; override;
function GetValueInt() : String; override;
function GetValueStr() : String; override;
procedure SetValueStr(const Value : String); override;
public
constructor Create(const ANome : String; AValue : Boolean; ATipo : TTipoField = tfNul);
published
property Value : Boolean read fValue write fValue;
end;
TmValueDate = class(TmValue)
private
fValue : TDateTime;
protected
function GetValueBase() : String; override;
function GetValueInt() : String; override;
function GetValueStr() : String; override;
procedure SetValueStr(const Value : String); override;
public
constructor Create(const ANome : String; AValue : TDateTime; ATipo : TTipoField = tfNul);
published
property Value : TDateTime read fValue write fValue;
end;
TmValueFloat = class(TmValue)
private
fValue : Real;
protected
function GetValueBase() : String; override;
function GetValueInt() : String; override;
function GetValueStr() : String; override;
procedure SetValueStr(const Value : String); override;
public
constructor Create(const ANome : String; AValue : Real; ATipo : TTipoField = tfNul);
published
property Value : Real read fValue write fValue;
end;
TmValueInt = class(TmValue)
private
fValue : Integer;
protected
function GetValueBase() : String; override;
function GetValueInt() : String; override;
function GetValueStr() : String; override;
procedure SetValueStr(const Value : String); override;
public
constructor Create(const ANome : String; AValue : Integer; ATipo : TTipoField = tfNul);
published
property Value : Integer read fValue write fValue;
end;
TmValueStr = class(TmValue)
private
fValue : String;
protected
function GetValueBase() : String; override;
function GetValueInt() : String; override;
function GetValueStr() : String; override;
procedure SetValueStr(const Value : String); override;
public
constructor Create(const ANome : String; AValue : String; ATipo : TTipoField = tfNul);
published
property Value : String read fValue write fValue;
end;
//-- lista
TmValueList = class(TList)
private
function GetItem(Index: Integer): TmValue;
procedure SetItem(Index: Integer; const AValue: TmValue);
function GetItemB(Index: Integer): TmValueBool;
function GetItemD(Index: Integer): TmValueDate;
function GetItemF(Index: Integer): TmValueFloat;
function GetItemI(Index: Integer): TmValueInt;
function GetItemS(Index: Integer): TmValueStr;
public
function AddB(const ANome : String; AValue : Boolean; ATipo : TTipoField = tfNul) : TmValue;
function AddD(const ANome : String; AValue : TDateTime; ATipo : TTipoField = tfNul) : TmValue;
function AddF(const ANome : String; AValue : Real; ATipo : TTipoField = tfNul) : TmValue;
function AddI(const ANome : String; AValue : Integer; ATipo : TTipoField = tfNul) : TmValue;
function AddS(const ANome : String; AValue : String; ATipo : TTipoField = tfNul) : TmValue;
function IndexOf(ANome : String) : TmValue;
property Items[Index : Integer] : TmValue read GetItem write SetItem;
property ItemsB[Index : Integer] : TmValueBool read GetItemB;
property ItemsD[Index : Integer] : TmValueDate read GetItemD;
property ItemsF[Index : Integer] : TmValueFloat read GetItemF;
property ItemsI[Index : Integer] : TmValueInt read GetItemI;
property ItemsS[Index : Integer] : TmValueStr read GetItemS;
end;
function StrToTipoValue(pStr : String) : TTipoValue;
function TipoValueToStr(pTip : TTipoValue) : String;
implementation
const
LTipoValue : Array [TTipoValue] Of TmValueClass = (
TmValueBool,
TmValueDate,
TmValueFloat,
TmValueInt,
TmValueStr);
STipoValue : Array [TTipoValue] Of String = (
'boolean',
'tdatetime',
'float',
'integer',
'string');
function StrToTipoValue(pStr : String) : TTipoValue;
var
I: Integer;
begin
Result := TTipoValue(Ord(-1));
for I := 0 to Ord(High(TTipoValue)) do
if STipoValue[TTipoValue(I)] = pStr then begin
Result := TTipoValue(I);
Exit;
end;
end;
function TipoValueToStr(pTip : TTipoValue) : String;
begin
Result := STipoValue[pTip];
end;
{ TmValue }
function TmValue.GetTipo: TTipoValue;
var
I : Integer;
begin
Result := TTipoValue(Ord(-1));
for I := Ord(Low(TTipoValue)) to Ord(High(TTipoValue)) do
if ClassType = LTipoValue[TTipoValue(I)] then
Result := TTipoValue(I);
end;
{ TmValueBool }
constructor TmValueBool.Create(const ANome: String; AValue: Boolean; ATipo : TTipoField);
begin
fNome := ANome;
fValue := AValue;
fTipoField := ATipo;
end;
function TmValueBool.GetValueBase: String;
begin
Result := '''' + IfThen(fValue, 'T', 'F') + '''';
end;
function TmValueBool.GetValueInt: String;
begin
Result := IfThen(fValue, 'T', 'F');
end;
function TmValueBool.GetValueStr: String;
begin
Result := IfThen(fValue, 'T', 'F');
end;
procedure TmValueBool.SetValueStr(const Value: String);
begin
fValue := (Value = 'T');
end;
{ TmValueDate }
constructor TmValueDate.Create(const ANome: String; AValue: TDateTime; ATipo : TTipoField);
begin
fNome := ANome;
fValue := AValue;
fTipoField := ATipo;
end;
function TmValueDate.GetValueBase: String;
begin
Result := '''' + FormatDateTime('dd/mm/yyyy hh:nn:ss', fValue) + '''';
end;
function TmValueDate.GetValueInt: String;
begin
Result := FormatDateTime('yyyymmdd_hhnnss', fValue);
end;
function TmValueDate.GetValueStr: String;
begin
Result := FormatDateTime('dd/mm/yyyy hh:nn:ss', fValue);
end;
procedure TmValueDate.SetValueStr(const Value: String);
begin
fValue := StrToDateTimeDef(Value, 0);
end;
{ TmValueFloat }
constructor TmValueFloat.Create(const ANome: String; AValue: Real; ATipo : TTipoField);
begin
fNome := ANome;
fValue := AValue;
fTipoField := ATipo;
end;
function TmValueFloat.GetValueBase: String;
begin
Result := AnsiReplaceStr(FloatToStr(fValue), ',', '.');
end;
function TmValueFloat.GetValueInt: String;
begin
Result := FormatFloat('0000000000', fValue * 100);
end;
function TmValueFloat.GetValueStr: String;
begin
Result := FloatToStr(fValue);
end;
procedure TmValueFloat.SetValueStr(const Value: String);
begin
fValue := StrToFloatDef(Value, 0);
end;
{ TmValueInt }
constructor TmValueInt.Create(const ANome: String; AValue: Integer; ATipo : TTipoField);
begin
fNome := ANome;
fValue := AValue;
fTipoField := ATipo;
end;
function TmValueInt.GetValueBase: String;
begin
Result := IntToStr(fValue);
end;
function TmValueInt.GetValueInt: String;
begin
Result := FormatFloat('000000', fValue);
end;
function TmValueInt.GetValueStr: String;
begin
Result := IntToStr(fValue);
end;
procedure TmValueInt.SetValueStr(const Value: String);
begin
fValue := StrToIntDef(Value, 0);
end;
{ TmValueStr }
constructor TmValueStr.Create(const ANome: String; AValue: String; ATipo : TTipoField);
begin
fNome := ANome;
fValue := AValue;
fTipoField := ATipo;
end;
function TmValueStr.GetValueBase: String;
begin
Result := '''' + AnsiReplaceStr(fValue, '''', '''''') + '''';
end;
function TmValueStr.GetValueInt: String;
begin
Result := Trim(fValue);
end;
function TmValueStr.GetValueStr: String;
begin
Result := fValue;
end;
procedure TmValueStr.SetValueStr(const Value: String);
begin
fValue := Value;
end;
{ TmValueList }
function TmValueList.AddB(const ANome : String; AValue : Boolean; ATipo : TTipoField) : TmValue;
begin
Result := TmValueBool.Create(ANome, AValue, ATipo);
Self.Add(Result);
end;
function TmValueList.AddD(const ANome : String; AValue : TDateTime; ATipo : TTipoField) : TmValue;
begin
Result := TmValueDate.Create(ANome, AValue, ATipo);
Self.Add(Result);
end;
function TmValueList.AddF(const ANome : String; AValue : Real; ATipo : TTipoField) : TmValue;
begin
Result := TmValueFloat.Create(ANome, AValue, ATipo);
Self.Add(Result);
end;
function TmValueList.AddI(const ANome : String; AValue : Integer; ATipo : TTipoField) : TmValue;
begin
Result := TmValueInt.Create(ANome, AValue, ATipo);
Self.Add(Result);
end;
function TmValueList.AddS(const ANome : String; AValue : String; ATipo : TTipoField) : TmValue;
begin
Result := TmValueStr.Create(ANome, AValue, ATipo);
Self.Add(Result);
end;
function TmValueList.GetItem(Index: Integer): TmValue;
begin
Result := TmValue(Self[Index]);
end;
procedure TmValueList.SetItem(Index: Integer; const AValue: TmValue);
begin
Self[Index] := AValue;
end;
function TmValueList.IndexOf(ANome: String): TmValue;
var
I : Integer;
begin
Result := nil;
for I := 0 to Count - 1 do
with TmValue(Self[I]) do
if LowerCase(Nome) = LowerCase(ANome) then begin
Result := TmValue(Self[I]);
Exit;
end;
end;
function TmValueList.GetItemB(Index: Integer): TmValueBool;
begin
Result := TmValueBool(Self[Index]);
end;
function TmValueList.GetItemD(Index: Integer): TmValueDate;
begin
Result := TmValueDate(Self[Index]);
end;
function TmValueList.GetItemF(Index: Integer): TmValueFloat;
begin
Result := TmValueFloat(Self[Index]);
end;
function TmValueList.GetItemI(Index: Integer): TmValueInt;
begin
Result := TmValueInt(Self[Index]);
end;
function TmValueList.GetItemS(Index: Integer): TmValueStr;
begin
Result := TmValueStr(Self[Index]);
end;
end.