Changeset 4 for trunk/UDatabase.pas


Ignore:
Timestamp:
Jan 18, 2015, 11:29:26 PM (10 years ago)
Author:
chronos
Message:
  • Moved: Field and value data type declaration moved to separate unit.
  • Modified: Fields edit form is now modal and made need changes need to be accepted.
  • Added: 32x32 icons to actions.
  • Fixed: If fileds count or type changed then table values are updated.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UDatabase.pas

    r3 r4  
    66
    77uses
    8   Classes, SysUtils, Contnrs, ExtCtrls, StdCtrls, EditBtn;
     8  Classes, SysUtils, Contnrs, ExtCtrls, StdCtrls, EditBtn, dialogs;
    99
    1010type
    1111  TTable = class;
    1212
    13   TFieldType = (ftString, ftNumeric, ftDateTime, ftBoolean, ftFloat, ftImage,
     13  TFieldType = (ftString, ftInteger, ftDateTime, ftBoolean, ftFloat, ftImage,
    1414    ftDate, tfTime, ftMapPosition);
    1515
     
    1717
    1818  TValue = class
     19    procedure Assign(Source: TValue); virtual;
    1920    function GetString: string; virtual;
    2021  end;
     
    2223  TValueClass = class of TValue;
    2324
    24   { TValueString }
    25 
    26   TValueString = class(TValue)
    27     Value: string;
    28     function GetString: string; override;
    29   end;
    30 
    31   TValueInteger = class(TValue)
    32     Value: Integer;
    33   end;
    34 
    35   { TValueDateTime }
    36 
    37   TValueDateTime = class(TValue)
    38     Value: TDateTime;
    39     function GetString: string; override;
    40   end;
     25  { TValues }
    4126
    4227  TValues = class(TObjectList)
     28    procedure Assign(Source: TValues);
    4329  end;
    4430
     
    4632
    4733  TFieldTypeSpecific = class
     34    procedure Assign(Source: TFieldTypeSpecific); virtual;
    4835    function GetValueClass: TValueClass; virtual;
    4936  end;
    5037
    5138  TFieldTypeSpecificClass = class of TFieldTypeSpecific;
    52 
    53   { TFieldString }
    54 
    55   TFieldString = class(TFieldTypeSpecific)
    56     DefaultValue: string;
    57     function GetValueClass: TValueClass; override;
    58   end;
    59 
    60   TFieldInteger = class(TFieldTypeSpecific)
    61     Min: Integer;
    62     Max: Integer;
    63     DefaultValue: Integer;
    64   end;
    65 
    66   TFieldFloat = class(TFieldTypeSpecific)
    67     Min: Double;
    68     Max: Double;
    69     DefaultValue: Double;
    70   end;
    71 
    72   { TFieldDateTime }
    73 
    74   TFieldDateTime = class(TFieldTypeSpecific)
    75     Min: TDateTime;
    76     Max: TDateTime;
    77     function GetValueClass: TValueClass; override;
    78   end;
    79 
    80   TFieldDate = class(TFieldTypeSpecific)
    81     Min: TDate;
    82     Max: TDate;
    83   end;
    84 
    85    TFieldTime = class(TFieldTypeSpecific)
    86     Min: TTime;
    87     Max: TTime;
    88   end;
    89 
    90   TFieldImage = class(TFieldTypeSpecific)
    91     MinSize: TPoint;
    92     MaxSize: TPoint;
    93   end;
    94 
    95   TFieldBoolean = class(TFieldTypeSpecific)
    96     DefaultValue: Boolean;
    97   end;
    98 
    99   TFieldMapPosition = class(TFieldTypeSpecific)
    100   end;
    10139
    10240  { TField }
     
    11755    Pos: TPoint;
    11856    Size: TPoint;
     57    procedure Assign(Source: TField);
    11958    function GetValueClass: TValueClass;
    12059    property FieldType: TFieldType read FFieldType write SetFieldType;
     
    12261  end;
    12362
     63  { TFields }
     64
    12465  TFields = class(TObjectList)
     66    function FindByName(Name: string): TField;
     67    procedure Assign(Source: TFields);
    12568  end;
    12669
     
    13073    Parent: TTable;
    13174    Values: TValues;
     75    procedure Assign(Source: TRecord);
    13276    constructor Create;
    13377    destructor Destroy; override;
    13478  end;
    13579
     80  { TRecords }
     81
    13682  TRecords = class(TObjectList)
    13783    Parent: TTable;
     84    procedure Assign(Source: TRecords);
    13885  end;
    13986
     
    14289  TTable = class
    14390    Name: string;
     91    Caption: string;
    14492    Records: TRecords;
    14593    Fields: TFields;
     94    procedure Assign(Source: TTable);
    14695    constructor Create;
    14796    destructor Destroy; override;
     
    166115  end;
    167116
    168 const
    169   FieldTypeString: array[TFieldType] of string = ('String', 'Numeric', 'DateTime',
    170     'Boolean', 'Float', 'Image', 'Date', 'Time', 'MapPosition');
    171   FieldTypeClass: array[TFieldType] of TFieldTypeSpecificClass = (TFieldString,
    172     TFieldInteger, TFieldDateTime, TFieldBoolean, TFieldFloat, TFieldImage,
    173     TFieldDate, TFieldTime, TFieldMapPosition);
    174 
    175117
    176118implementation
    177119
    178 { TFieldDateTime }
    179 
    180 function TFieldDateTime.GetValueClass: TValueClass;
    181 begin
    182   Result := TValueDateTime;
    183 end;
    184 
    185 { TValueDateTime }
    186 
    187 function TValueDateTime.GetString: string;
    188 begin
    189   Result := DateTimeToStr(Value);
     120uses
     121  UDataTypes;
     122
     123{ TValues }
     124
     125procedure TValues.Assign(Source: TValues);
     126var
     127  I: Integer;
     128  OldCount: Integer;
     129begin
     130  OldCount := Count;
     131  Count := Source.Count;
     132  for I := OldCount to Count - 1 do
     133    //ShowMessage(TValue(Source.Items[I]).ClassName);
     134    Items[I] := TValueClass(TValue(Source.Items[I]).ClassType).Create;
     135  for I := 0 to Count - 1 do
     136    if TValue(Items[I]).ClassType <> TValue(Source.Items[I]).ClassType then begin
     137      Items[I] := TValueClass(TValue(Source.Items[I]).ClassType).Create;
     138    end;
     139  for I := 0 to Source.Count - 1 do begin
     140    TValue(Items[I]).Assign(TValue(Source.Items[I]));
     141  end;
     142end;
     143
     144{ TRecords }
     145
     146procedure TRecords.Assign(Source: TRecords);
     147var
     148  I: Integer;
     149  OldCount: Integer;
     150begin
     151  OldCount := Count;
     152  Count := Source.Count;
     153  for I := OldCount to Count - 1 do
     154    Items[I] := TRecord.Create;
     155  for I := 0 to Source.Count - 1 do begin
     156    TRecord(Items[I]).Assign(TRecord(Source.Items[I]));
     157  end;
     158end;
     159
     160{ TFields }
     161
     162function TFields.FindByName(Name: string): TField;
     163var
     164  I: Integer;
     165begin
     166  I := 0;
     167  while (I < Count) and (TField(Items[I]).Name <> Name) do Inc(I);
     168  if I < Count then Result := TField(Items[I])
     169    else Result := nil;
     170end;
     171
     172procedure TFields.Assign(Source: TFields);
     173var
     174  I: Integer;
     175  OldCount: Integer;
     176begin
     177  OldCount := Count;
     178  Count := Source.Count;
     179  for I := OldCount to Count - 1 do
     180    Items[I] := TField.Create;
     181  for I := 0 to Source.Count - 1 do begin
     182    TField(Items[I]).Assign(TField(Source.Items[I]));
     183  end;
    190184end;
    191185
    192186{ TRecord }
     187
     188procedure TRecord.Assign(Source: TRecord);
     189begin
     190  Values.Assign(Source.Values);
     191end;
    193192
    194193constructor TRecord.Create;
     
    203202end;
    204203
    205 { TValueString }
    206 
    207 function TValueString.GetString: string;
    208 begin
    209   Result := Value;
    210 end;
    211 
    212204{ TValue }
    213205
     206procedure TValue.Assign(Source: TValue);
     207begin
     208end;
     209
    214210function TValue.GetString: string;
    215211begin
     
    217213end;
    218214
    219 { TFieldString }
    220 
    221 function TFieldString.GetValueClass: TValueClass;
    222 begin
    223   Result := TValueString;
    224 end;
    225 
    226215{ TFieldTypeSpecific }
     216
     217procedure TFieldTypeSpecific.Assign(Source: TFieldTypeSpecific);
     218begin
     219
     220end;
    227221
    228222function TFieldTypeSpecific.GetValueClass: TValueClass;
     
    241235end;
    242236
     237procedure TField.Assign(Source: TField);
     238begin
     239  Name := Source.Name;
     240  FieldType := Source.FieldType;
     241  TextAfter := Source.TextAfter;
     242  TextBefore := Source.TextBefore;
     243  Required := Source.Required;
     244  ReadOnly := Source.ReadOnly;
     245  Description := Source.Description;
     246  AllowNull := Source.AllowNull;
     247  TypeRelated.Assign(Source.TypeRelated);
     248end;
     249
    243250function TField.GetValueClass: TValueClass;
    244251begin
     
    252259end;
    253260
    254 { TField }
    255 
    256 
    257261{ TTable }
     262
     263procedure TTable.Assign(Source: TTable);
     264begin
     265  Name := Source.Name;
     266  Caption := Source.Caption;
     267  Fields.Assign(Source.Fields);
     268  Records.Assign(Source.Records);
     269end;
    258270
    259271constructor TTable.Create;
Note: See TracChangeset for help on using the changeset viewer.