Changeset 43 for trunk/Application


Ignore:
Timestamp:
Mar 9, 2012, 1:09:52 PM (13 years ago)
Author:
chronos
Message:
  • Fixed: Selection of available protocols in profile manager.
  • Modified: Initialization of persistent data structures rewrited to more native structures.
Location:
trunk/Application
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Application/Clients/UChronisClientMySQL.pas

    r40 r43  
    1414  TChronisClientMySQL = class(TChronisClient)
    1515  protected
     16    procedure InitSystemTypes; override;
    1617    function GetConnected: Boolean; override;
    1718  public
     
    2324    procedure ListLoad(AList: TListProxy); override;
    2425    procedure ListSave(AList: TListProxy); override;
    25     procedure DefineType(AType: TChronisType); override;
    26     procedure UndefineType(AType: TChronisType); override;
     26    procedure TypeDefine(AType: TChronisType); override;
     27    procedure TypeUndefine(AType: TChronisType); override;
    2728    procedure Install;
    2829    procedure Uninstall;
     
    3536implementation
    3637
     38
     39resourcestring
     40  SMissingBaseType = 'Missing base typ for %s';
     41  SUndefinedType = 'Undefinned type %s';
     42
     43
    3744{ TChronisClientMySQL }
     45
     46procedure TChronisClientMySQL.InitSystemTypes;
     47begin
     48  inherited InitSystemTypes;
     49  Types.AddType('Integer', 'int(11)');
     50  Types.AddType('String', 'varchar(255)');
     51  Types.AddType('RelationOne', 'int(11)');
     52  Types.AddType('Double', 'double');
     53  Types.AddType('DateTime', 'datetime');
     54  Types.AddType('Date', 'date');
     55  Types.AddType('Time', 'time');
     56  Types.AddType('Text', 'text');
     57  Types.AddType('Boolean', 'bool');
     58end;
    3859
    3960function TChronisClientMySQL.GetConnected: Boolean;
     
    103124end;
    104125
    105 procedure TChronisClientMySQL.DefineType(AType: TChronisType);
     126procedure TChronisClientMySQL.TypeDefine(AType: TChronisType);
    106127var
    107   Data: TDictionaryStringString;
     128  DbRows: TDbRows;
    108129  I: Integer;
     130  Query: string;
     131  RefType: TChronisType;
    109132begin
    110133  try
    111     Data := TDictionaryStringString.Create;
    112     Data.Add('Name', AType.Name);
    113     Database.Insert('Type', Data);
     134    DbRows := TDbRows.Create;
     135    Query := 'CREATE TABLE IF NOT EXISTS `' + AType.Name + '` ( ' +
     136      '`Id` int(11) NOT NULL AUTO_INCREMENT,';
     137    for I := 0 to AType.Properties.Count - 1 do
     138    with AType.Properties do begin
     139      RefType := Types.SearchByName(Items[I].Value);
     140      if not Assigned(RefType) then
     141        raise Exception.Create(Format(SUndefinedType, [Items[I].Value]));
     142      if RefType.DbType = '' then
     143        raise Exception.Create(Format(SMissingBaseType, [RefType.Name]));
     144
     145      Query := Query + '`' + Items[I].Key + '` ' + RefType.DbType + ' NOT NULL,';
     146    end;
     147    Query := Query + 'PRIMARY KEY (`Id`)' +
     148      ') ENGINE=InnoDB  DEFAULT CHARSET=utf8';
     149    Database.Query(DbRows, Query);
    114150  finally
    115     Data.Free;
    116   end;
    117   if AType is TChronisTypeRecord then begin
    118     for I := 0 to TChronisTypeRecord(AType).Items.Count - 1 do
    119       try
    120         Data := TDictionaryStringString.Create;
    121         Data.Add('Name',
    122           TChronisTypeRecordItem(TChronisTypeRecord(AType).Items[I]).Name);
    123         Data.Add('Type', IntToStr(
    124           TChronisTypeRecordItem(TChronisTypeRecord(AType).Items[I]).ItemType.OID));
    125         Database.Insert('TypeRecordItem', Data);
    126       finally
    127         Data.Free;
    128       end;
    129   end;
    130 end;
    131 
    132 procedure TChronisClientMySQL.UndefineType(AType: TChronisType);
     151    DbRows.Free;
     152  end;
     153end;
     154
     155procedure TChronisClientMySQL.TypeUndefine(AType: TChronisType);
    133156begin
    134157
     
    156179constructor TChronisClientMySQL.Create;
    157180begin
     181  inherited;
    158182  Database := TSqlDatabase.Create(nil);
    159183end;
  • trunk/Application/Clients/UChronisClientXMLRPC.pas

    r32 r43  
    66
    77uses
    8   Classes, SysUtils;
     8  Classes, SysUtils, UChronisClient;
     9
     10type
     11  TChronisClientXMLRPC = class(TChronisClient)
     12  end;
    913
    1014implementation
  • trunk/Application/UChronisClient.pas

    r37 r43  
    99
    1010type
     11  EClientNotSet = class(Exception);
     12
    1113  TChronisClient = class;
    1214
     
    5254  end;
    5355
     56  { TChronisType }
     57
    5458  TChronisType = class
    55     OID: Integer;
     59    Client: TChronisClient;
    5660    Name: string;
    57   end;
    58 
    59   TChronisTypeRecordItem = class
    60     Name: string;
    61     ItemType: TChronisType;
    62   end;
    63 
    64   { TChronisTypeRecord }
    65 
    66   TChronisTypeRecord = class(TChronisType)
    67     Items: TListObject; // TListObject<TChronisTypeRecordItem>
     61    DbType: string;
     62    Properties: TDictionaryStringString;
     63    procedure Define;
     64    procedure Undefine;
    6865    constructor Create;
    6966    destructor Destroy; override;
    7067  end;
    7168
     69  { TChronisTypeList }
     70
     71  TChronisTypeList = class(TListObject)
     72    Client: TChronisClient;
     73    function AddType(Name: string; DbType: string = ''): TChronisType;
     74    function SearchByName(Name: string): TChronisType;
     75  end;
     76
    7277  { TChronisClient }
    7378
    7479  TChronisClient = class
     80  private
    7581  protected
     82    procedure InitSystemTypes; virtual;
    7683    function GetConnected: Boolean; virtual;
    7784  public
     
    8188    User: string;
    8289    Password: string;
     90    Types: TChronisTypeList;
    8391    procedure ObjectLoad(AObject: TObjectProxy); virtual; abstract;
    8492    procedure ObjectSave(AObject: TObjectProxy); virtual; abstract;
     
    8795    procedure ListLoad(AList: TListProxy); virtual; abstract;
    8896    procedure ListSave(AList: TListProxy); virtual; abstract;
    89     procedure DefineType(AType: TChronisType); virtual; abstract;
    90     procedure UndefineType(AType: TChronisType); virtual; abstract;
     97    procedure TypeDefine(AType: TChronisType); virtual; abstract;
     98    procedure TypeUndefine(AType: TChronisType); virtual; abstract;
     99    procedure CheckTypes;
    91100    constructor Create; virtual;
     101    destructor Destroy; override;
    92102    procedure Connect; virtual; abstract;
    93103    procedure Disconnect; virtual; abstract;
     
    95105  end;
    96106
     107
    97108implementation
    98109
    99 { TChronisTypeRecord }
    100 
    101 constructor TChronisTypeRecord.Create;
    102 begin
    103   Items := TListObject.Create;
    104 end;
    105 
    106 destructor TChronisTypeRecord.Destroy;
    107 begin
    108   Items.Free;
     110resourcestring
     111  SClientNotSet = 'Client not set';
     112
     113{ TChronisTypeList }
     114
     115function TChronisTypeList.AddType(Name: string; DbType: string = ''): TChronisType;
     116begin
     117  Result := TChronisType(AddNew(TChronisType.Create));
     118  Result.Client := Client;
     119  Result.Name := Name;
     120  Result.DbType := DbType;
     121end;
     122
     123function TChronisTypeList.SearchByName(Name: string): TChronisType;
     124var
     125  I: Integer;
     126begin
     127  I := 0;
     128  while (I < Count) and (TChronisType(Items[I]).Name <> Name) do Inc(I);
     129  if I < Count then Result := TChronisType(Items[I])
     130    else Result := nil;
     131end;
     132
     133procedure TChronisType.Define;
     134begin
     135  if Assigned(Client) then Client.TypeDefine(Self)
     136    else raise EClientNotSet.Create(SClientNotSet);
     137end;
     138
     139procedure TChronisType.Undefine;
     140begin
     141  if Assigned(Client) then Client.TypeUndefine(Self)
     142    else raise EClientNotSet.Create(SClientNotSet);
     143end;
     144
     145constructor TChronisType.Create;
     146begin
     147  Properties := TDictionaryStringString.Create;
     148end;
     149
     150destructor TChronisType.Destroy;
     151begin
     152  Properties.Free;
    109153  inherited Destroy;
    110154end;
     
    114158procedure TObjectProxy.Load;
    115159begin
    116   Client.ObjectLoad(Self);
     160  if Assigned(Client) then Client.ObjectLoad(Self)
     161    else raise EClientNotSet.Create(SClientNotSet);
    117162end;
    118163
    119164procedure TObjectProxy.Save;
    120165begin
    121   Client.ObjectSave(Self);
     166  if Assigned(Client) then Client.ObjectSave(Self)
     167    else raise EClientNotSet.Create(SClientNotSet);
    122168end;
    123169
    124170procedure TObjectProxy.Delete;
    125171begin
    126   Client.ObjectDelete(Self);
     172  if Assigned(Client) then Client.ObjectDelete(Self)
     173    else raise EClientNotSet.Create(SClientNotSet);
    127174end;
    128175
    129176procedure TObjectProxy.Add;
    130177begin
    131   Client.ObjectAdd(Self);
     178  if Assigned(Client) then Client.ObjectAdd(Self)
     179    else raise EClientNotSet.Create(SClientNotSet);
    132180end;
    133181
     
    168216procedure TListProxy.Load;
    169217begin
    170   Client.ListLoad(Self);
     218  if Assigned(Client) then Client.ListLoad(Self)
     219    else raise EClientNotSet.Create(SClientNotSet);
    171220end;
    172221
    173222procedure TListProxy.Save;
    174223begin
    175   Client.ListSave(Self);
     224  if Assigned(Client) then Client.ListSave(Self)
     225    else raise EClientNotSet.Create(SClientNotSet);
    176226end;
    177227
    178228{ TChronisClient }
    179229
     230procedure TChronisClient.InitSystemTypes;
     231begin
     232end;
     233
    180234function TChronisClient.GetConnected: Boolean;
    181235begin
     
    183237end;
    184238
     239procedure TChronisClient.CheckTypes;
     240var
     241  StructureVersion: string;
     242  Data: TDictionaryStringString;
     243  ObjectId: Integer;
     244  Tables: TListString;
     245  I: Integer;
     246  NewProxy: TListProxy;
     247begin
     248  try
     249    Tables := TListString.Create;
     250    Data := TDictionaryStringString.Create;
     251
     252    NewProxy := TListProxy.Create;
     253    NewProxy.Client := Self;
     254    NewProxy.SchemaName := 'information_schema';
     255    NewProxy.ObjectName := 'TABLES';
     256    NewProxy.Condition := 'TABLE_SCHEMA = "' + Schema + '"';
     257    NewProxy.Load;
     258    //Database.Query(DbRows, 'SHOW TABLES');
     259    Tables.Count := NewProxy.Objects.Count;
     260    for I := 0 to NewProxy.Objects.Count - 1 do
     261      Tables[I] := TObjectProxy(NewProxy.Objects[I]).Properties.Values['TABLE_NAME'];
     262
     263    for I := 0 to Types.Count - 1 do
     264    with TChronisType(Types[I]) do begin
     265      if (DbType = '') and (Tables.IndexOf(Name) = -1) then begin
     266        Define;
     267      end;
     268    end;
     269  finally
     270    NewProxy.Free;
     271    Data.Free;
     272    Tables.Free;
     273  end;
     274end;
     275
    185276constructor TChronisClient.Create;
    186277begin
     278  Types := TChronisTypeList.Create;
     279  Types.Client := Self;
     280  InitSystemTypes;
     281end;
     282
     283destructor TChronisClient.Destroy;
     284begin
     285  Types.Free;
     286  inherited Destroy;
    187287end;
    188288
Note: See TracChangeset for help on using the changeset viewer.