Changeset 6 for trunk/UCore.pas


Ignore:
Timestamp:
Jan 20, 2015, 12:31:24 AM (10 years ago)
Author:
chronos
Message:
  • Added: Allow to create multiple database connections and set their parmaters.
  • Added: Defined database engines to support various database backends. Each supports different set of field data types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r3 r6  
    2222  private
    2323    Initialized: Boolean;
     24    procedure InitEngines;
    2425  public
    25     Database: TDatabase;
     26    Databases: TDatabases;
     27    Engines: TDatabaseEngines;
    2628    procedure LoadConfig;
    2729    procedure SaveConfig;
     
    3638
    3739uses
    38   UDatabaseXML, UFormTables;
     40  UEngineXML, UEngineMySQL, UEngineSQLite, UFormTables, UFormConnect, UFormDatabases;
    3941
    4042{$R *.lfm}
     
    4446procedure TCore.DataModuleCreate(Sender: TObject);
    4547begin
    46   Database := TDatabaseXML.Create;
     48  Databases := TDatabases.Create;
     49  Engines := TDatabaseEngines.Create;
    4750end;
    4851
    4952procedure TCore.AExitExecute(Sender: TObject);
    5053begin
    51   FormMain.Close;
     54  FormTables.Close;
    5255end;
    5356
    5457procedure TCore.DataModuleDestroy(Sender: TObject);
    5558begin
    56   Database.Free;
     59  Engines.Free;
     60  Databases.Free;
     61end;
     62
     63procedure TCore.InitEngines;
     64var
     65  NewEngine: TDatabaseEngine;
     66begin
     67  Engines.Clear;
     68
     69  NewEngine := Engines.RegisterEngine('XML file', TDatabaseXML);
     70  with NewEngine.DataTypes do begin
     71    RegisterType('String', ftString);
     72    RegisterType('Integer', ftInteger);
     73    RegisterType('Date and time', ftDateTime);
     74    RegisterType('Boolean', ftBoolean);
     75    RegisterType('Float', ftFloat);
     76  end;
     77
     78  NewEngine := Engines.RegisterEngine('MySQL', TDatabaseMySQL);
     79  with NewEngine.DataTypes do begin
     80    RegisterType('String', ftString);
     81    RegisterType('Integer', ftInteger);
     82    RegisterType('Date and time', ftDateTime);
     83    RegisterType('Boolean', ftBoolean);
     84    RegisterType('Float', ftFloat);
     85  end;
     86
     87  NewEngine := Engines.RegisterEngine('SQLite', TDatabaseSQLite);
     88  with NewEngine.DataTypes do begin
     89    RegisterType('String', ftString);
     90    RegisterType('Integer', ftInteger);
     91    RegisterType('Date and time', ftDateTime);
     92    RegisterType('Boolean', ftBoolean);
     93    RegisterType('Float', ftFloat);
     94  end;
    5795end;
    5896
    5997procedure TCore.LoadConfig;
     98var
     99  I: Integer;
     100  NewDatabase: TDatabase;
    60101begin
    61   Database.ConnectionString := XMLConfig1.GetValue('ConnectionString', 'file://' + ExtractFileDir(Application.ExeName) + '/data.xml');
     102  Databases.Count := XMLConfig1.GetValue('Database/Count', 0);
     103  for I := 0 to Databases.Count - 1 do begin
     104    NewDatabase := TDatabase.Create;
     105    NewDatabase.Name := XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/Name', '');
     106    NewDatabase.ConnectionString := XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/ConnectionString', '');
     107    NewDatabase.Engine := Core.Engines.FindByName(XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/Engine', ''));
     108    Databases[I] := NewDatabase;
     109  end;
     110  //Databases.ConnectionString := XMLConfig1.GetValue('ConnectionString', 'file://' + ExtractFileDir(Application.ExeName) + '/data.xml');
    62111end;
    63112
    64113procedure TCore.SaveConfig;
     114var
     115  I: Integer;
    65116begin
    66   XMLConfig1.SetValue('ConnectionString', Database.ConnectionString);
     117  XMLConfig1.SetValue('Database/Count', Databases.Count);
     118  for I := 0 to Databases.Count - 1 do begin
     119    XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/Name', TDatabase(Databases[I]).Name);
     120    XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/ConnectionString', TDatabase(Databases[I]).ConnectionString);
     121    XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/Engine', TDatabase(Databases[I]).Engine.Name);
     122  end;
    67123end;
    68124
     
    70126begin
    71127  if not Initialized then begin
     128    InitEngines;
     129    FormConnect.Init;
    72130    Initialized := True;
    73131    LoadConfig;
    74     Database.Load;
    75     FormMain.ReloadList;
     132    FormDatabases.ReloadList;
    76133  end;
    77134end;
     
    79136procedure TCore.Done;
    80137begin
    81   Database.Save;
    82138  SaveConfig;
    83139end;
Note: See TracChangeset for help on using the changeset viewer.