Changeset 8 for trunk/UDatabase.pas


Ignore:
Timestamp:
Jan 23, 2015, 12:00:37 AM (10 years ago)
Author:
chronos
Message:
  • Modified: All data types have own table which is used to specify supported data types by database engines.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UDatabase.pas

    r7 r8  
    1212  TDatabaseEngine = class;
    1313  TDatabaseClient = class;
     14  TDatabase = class;
     15  TDataType = class;
    1416
    1517  TFieldType = (ftString, ftInteger, ftDateTime, ftBoolean, ftFloat, ftImage,
    16     ftDate, tfTime, ftMapPosition);
     18    ftDate, ftTime, ftMapPosition);
    1719
    1820  { TValue }
     
    4446  TField = class
    4547  private
    46     FFieldType: TFieldType;
    47     procedure SetFieldType(AValue: TFieldType);
     48    FDataType: TDataType;
     49    procedure SetDataType(AValue: TDataType);
    4850  public
    4951    Name: string;
     
    5759    Pos: TPoint;
    5860    Size: TPoint;
     61    Table: TTable;
    5962    procedure Assign(Source: TField);
    6063    function GetValueClass: TValueClass;
    61     property FieldType: TFieldType read FFieldType write SetFieldType;
     64    property DataType: TDataType read FDataType write SetDataType;
    6265    constructor Create;
    6366  end;
     
    6669
    6770  TFields = class(TObjectList)
     71    Table: TTable;
    6872    function FindByName(Name: string): TField;
    6973    procedure Assign(Source: TFields);
     
    9498    Records: TRecords;
    9599    Fields: TFields;
     100    Database: TDatabase;
    96101    procedure Assign(Source: TTable);
    97102    constructor Create;
     
    100105
    101106  TTables = class(TObjectList)
    102 
     107    Database: TDatabase;
    103108  end;
    104109
     
    134139    Name: string;
    135140    FieldType: TFieldType;
    136   end;
     141    FieldTypeClass: TFieldTypeSpecificClass;
     142  end;
     143
     144  { TDataTypes }
    137145
    138146  TDataTypes = class(TObjectList)
    139     function RegisterType(Name: string; FieldType: TFieldType): TDataType;
     147    function RegisterType(Name: string; FieldType: TFieldType; FieldTypeClass: TFieldTypeSpecificClass): TDataType;
     148    function FindByType(FieldType: TFieldType): TDataType;
    140149  end;
    141150
     
    150159  TDatabaseClientClass = class of TDatabaseClient;
    151160
     161  TFieldTypeSet = set of TFieldType;
     162
    152163  { TDatabaseEngine }
    153164
     
    156167    DataTypes: TDataTypes;
    157168    DatabaseClientClass: TDatabaseClientClass;
     169    procedure UseTypes(ADataTypes: TDataTypes; Types: TFieldTypeSet);
    158170    constructor Create;
    159171    destructor Destroy; override;
     
    208220{ TDataTypes }
    209221
    210 function TDataTypes.RegisterType(Name: string; FieldType: TFieldType): TDataType;
     222function TDataTypes.RegisterType(Name: string; FieldType: TFieldType;
     223  FieldTypeClass: TFieldTypeSpecificClass): TDataType;
    211224begin
    212225  Result := TDataType.Create;
    213226  Result.Name := Name;
    214227  Result.FieldType := FieldType;
     228  Result.FieldTypeClass := FieldTypeClass;
    215229  Add(Result);
    216230end;
    217231
     232function TDataTypes.FindByType(FieldType: TFieldType): TDataType;
     233var
     234  I: Integer;
     235begin
     236  I := 0;
     237  while (I < Count) and (TDataType(Items[I]).FieldType <> FieldType) do Inc(I);
     238  if I < Count then Result := TDataType(Items[I])
     239    else Result := nil;
     240end;
     241
    218242{ TDatabaseEngine }
    219243
     244procedure TDatabaseEngine.UseTypes(ADataTypes: TDataTypes; Types: TFieldTypeSet);
     245var
     246  I: TFieldType;
     247begin
     248  DataTypes.Clear;
     249  for I := Low(TFieldType) to High(TFieldType) do
     250    if I in Types then DataTypes.Add(ADataTypes.FindByType(I));
     251end;
     252
    220253constructor TDatabaseEngine.Create;
    221254begin
    222255  DataTypes := TDataTypes.Create;
     256  DataTypes.OwnsObjects := False;
    223257end;
    224258
     
    335369{ TField }
    336370
    337 procedure TField.SetFieldType(AValue: TFieldType);
    338 begin
    339   if FFieldType = AValue then Exit;
     371procedure TField.SetDataType(AValue: TDataType);
     372begin
     373  if FDataType = AValue then Exit;
    340374  if Assigned(TypeRelated) then TypeRelated.Free;
    341   FFieldType := AValue;
    342   TypeRelated := FieldTypeClass[FFieldType].Create;
     375  FDataType := AValue;
     376  if Assigned(AValue) then
     377    TypeRelated := AValue.FieldTypeClass.Create
     378    else TypeRelated := nil;
    343379end;
    344380
     
    346382begin
    347383  Name := Source.Name;
    348   FieldType := Source.FieldType;
     384  DataType := Source.DataType;
    349385  TextAfter := Source.TextAfter;
    350386  TextBefore := Source.TextBefore;
     
    382418  Records.Parent := Self;
    383419  Fields := TFields.Create;
     420  Fields.Table := Self;
    384421end;
    385422
     
    408445begin
    409446  Tables := TTables.Create;
     447  Tables.Database := Self;
    410448  Engine := nil;
    411449end;
Note: See TracChangeset for help on using the changeset viewer.