Changeset 13 for trunk/USystem.pas


Ignore:
Timestamp:
Jun 10, 2011, 8:23:37 AM (13 years ago)
Author:
george
Message:
  • Added: Type separated to base types and custom type instances. Every type which have parameters should have own SQL table. Types which have structured values should have tables for values too.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/USystem.pas

    r12 r13  
    1616  PropertyTypeTable = 'Type';
    1717  PropertyGroupTable = 'PropertyGroup';
     18  TypeEnumeration = 'TypeEnumeration';
     19  TypeFile = 'TypeFile';
     20  TypeGPS = 'TypeGPS';
     21  TypeRelationOne = 'TypeRelationOne';
     22  TypeRelationMany = 'TypeRelationMany';
     23  CustomType = 'TypeCustom';
     24  Enumeration = 'Enumeration';
     25  EnumerationState = 'EnumerationState';
     26  TypeNumber = 'TypeNumber';
     27  TypeString = 'TypeString';
    1828
    1929type
    20   TDbValueType = (vtNone, vtInteger, vtString, vtText, vtDateTime, vtFloat, vtImage, vtBoolean,
    21   vtIPv4, vtMAC, vtIPv6, vtFile, vtGPS, vtEnumeration, vtHyperlink, vtPassword,
    22   vtReference, vtDate, vtTime, vtColor, vtCurrency);
     30  TDbValueType = (vtNone, vtInteger, vtString, vtText, vtDateTime, vtFloat, vtImage,
     31  vtBoolean, vtIPv4, vtIPv6, vtMAC, vtFile, vtGPS, vtCurrency, vtEnumeration,
     32  vtTime, vtDate, vtColor, vtHyperlink, vtRelationOne, vtRelationMany, vtPassword);
    2333
    2434  TChronisType = class;
     
    90100    function AddObject(Name, TableName, Schema: string; GroupId: Integer): Integer;
    91101    function AddProperty(ObjectId: Integer; Name, ColumnName: string; DataType: Integer): Integer;
     102    function AddPropertyNumber(ObjectId: Integer; Name,
     103      ColumnName: string; Default: Integer = 0; Min: Integer = 0;
     104      Max: Integer = High(Integer)): Integer;
     105    function AddPropertyString(ObjectId: Integer; Name, ColumnName: string;
     106      Default: string = ''; MaxLength: Integer = 255): Integer;
    92107    function AddObjectGroup(Name: string): Integer;
     108    function AddEnumeration(Name: string): Integer;
     109    function AddEnumerationState(Enum: Integer; Name: string): Integer;
    93110    procedure LoadTypes;
    94111    constructor Create;
    95112    destructor Destroy; override;
     113  private
    96114  end;
    97115
     
    99117
    100118implementation
     119
     120resourcestring
     121  SUnsupportedType = 'Unsupported property type "%s"';
    101122
    102123{ TChronisTypeList }
     
    159180      NewColumn.TypeDef := Base.Types.FindByTypeIndex(Integer(vtInteger));
    160181      for I := 0 to Properties.Count - 1 do
    161       if Properties[I].Values['Type'] <> IntToStr(20) then begin
     182      if Properties[I].Values['Type'] <> IntToStr(Integer(vtRelationMany)) then begin
    162183        NewColumn := TReportColumn.Create;
    163184        Columns.Add(NewColumn);
     
    165186        NewColumn.ColumnName := Properties[I].Values['ColumnName'];;
    166187        NewColumn.TypeDef := Base.Types.FindById(StrToInt(Properties[I].Values['Type']));
     188        if not Assigned(NewColumn.TypeDef) then
     189          raise Exception.Create(Format(SUnsupportedType, [Properties[I].Values['Type']]));
    167190      end;
    168191
     
    181204        NewItem.Id := StrToInt(Values[I].Values[Obj.PrimaryKey]);
    182205        for C := 0 to Properties.Count - 1 do
    183         if Properties[C].Values['Type'] <> IntToStr(20) then begin
     206        if Properties[C].Values['Type'] <> IntToStr(Integer(vtRelationMany)) then begin
    184207          NewItem.Items.Add(Values[I].Values[Properties[C].Values['ColumnName']]);
    185208        end;
     
    244267    Data.Add('Object', IntToStr(ObjectId));
    245268    Data.Add('ColumnName', ColumnName);
    246     Data.Add('Type', IntToStr(DataType));
     269    Data.Add('CustomType', IntToStr(DataType));
     270    Data.Add('Editable', '1');
    247271    Database.Insert(PropertyTable, Data);
    248272    Result := Database.LastInsertId;
     
    253277end;
    254278
     279function TChronisBase.AddPropertyNumber(ObjectId: Integer; Name,
     280  ColumnName: string; Default: Integer = 0; Min: Integer = 0;
     281  Max: Integer = High(Integer)): Integer;
     282var
     283  DbRows: TDbRows;
     284  Data: TDictionaryStringString;
     285  CustomTypeId: Integer;
     286begin
     287  try
     288    DbRows := TDbRows.Create;
     289    Data := TDictionaryStringString.Create;
     290
     291    Data.Clear;
     292    Data.Add('Type', IntToStr(Integer(vtInteger)));
     293    Database.Insert(CustomType, Data);
     294    CustomTypeId := Database.LastInsertId;
     295
     296    Data.Clear;
     297    Data.Add('CustomType', IntToStr(CustomTypeId));
     298    Data.Add('Min', IntToStr(Min));
     299    Data.Add('Max', IntToStr(Max));
     300    Data.Add('Default', IntToStr(Default));
     301    Database.Insert(TypeNumber, Data);
     302    CustomTypeId := Database.LastInsertId;
     303
     304    Result := AddProperty(ObjectId, Name, ColumnName, CustomTypeId);
     305  finally
     306    Data.Free;
     307    DbRows.Free;
     308  end;
     309end;
     310
     311function TChronisBase.AddPropertyString(ObjectId: Integer; Name,
     312  ColumnName: string; Default: string = ''; MaxLength: Integer = 255): Integer;
     313var
     314  DbRows: TDbRows;
     315  Data: TDictionaryStringString;
     316  CustomTypeId: Integer;
     317begin
     318  try
     319    DbRows := TDbRows.Create;
     320    Data := TDictionaryStringString.Create;
     321
     322    Data.Clear;
     323    Data.Add('Type', IntToStr(Integer(vtString)));
     324    Database.Insert(CustomType, Data);
     325    CustomTypeId := Database.LastInsertId;
     326
     327    Data.Clear;
     328    Data.Add('CustomType', IntToStr(CustomTypeId));
     329    Data.Add('MaxLength', IntToStr(MaxLength));
     330    Data.Add('Default', Default);
     331    Database.Insert(TypeString, Data);
     332    CustomTypeId := Database.LastInsertId;
     333
     334    Result := AddProperty(ObjectId, Name, ColumnName, CustomTypeId);
     335  finally
     336    Data.Free;
     337    DbRows.Free;
     338  end;
     339end;
     340
    255341function TChronisBase.AddObjectGroup(Name: string): Integer;
    256342var
     
    263349    Data.Add('Name', Name);
    264350    Database.Insert(ObjectGroupTable, Data);
     351    Result := Database.LastInsertId;
     352  finally
     353    Data.Free;
     354    DbRows.Free;
     355  end;
     356end;
     357
     358function TChronisBase.AddEnumeration(Name: string): Integer;
     359var
     360  DbRows: TDbRows;
     361  Data: TDictionaryStringString;
     362begin
     363  try
     364    DbRows := TDbRows.Create;
     365    Data := TDictionaryStringString.Create;
     366    Data.Add('Name', Name);
     367    Database.Insert(Enumeration, Data);
     368    Result := Database.LastInsertId;
     369  finally
     370    Data.Free;
     371    DbRows.Free;
     372  end;
     373end;
     374
     375function TChronisBase.AddEnumerationState(Enum: Integer; Name: string
     376  ): Integer;
     377var
     378  DbRows: TDbRows;
     379  Data: TDictionaryStringString;
     380begin
     381  try
     382    DbRows := TDbRows.Create;
     383    Data := TDictionaryStringString.Create;
     384    Data.Add('Enumeration', IntToStr(Enum));
     385    Data.Add('Name', Name);
     386    Database.Insert(EnumerationState, Data);
    265387    Result := Database.LastInsertId;
    266388  finally
Note: See TracChangeset for help on using the changeset viewer.