Changeset 20 for trunk/UCore.pas


Ignore:
Timestamp:
Mar 23, 2018, 1:59:25 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Database classes reorganized. Now TDbConnectProfile is class which holds information about connection to database.
  • Modified: TDbManager is top most class for managing other database classes.
  • Modified: TDbConnectParams class contains client specific parameters for connect profile.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r19 r20  
    66
    77uses
    8   Classes, SysUtils, XMLConf, FileUtil, Controls, ActnList, UDatabase,
    9   UCoolTranslator, UApplicationInfo, UPersistentForm, Forms;
     8  Classes, SysUtils, FileUtil, Controls, ActnList, UDatabase,
     9  UCoolTranslator, UApplicationInfo, UPersistentForm, Forms, URegistry;
    1010
    1111type
     
    2323    ImageList1: TImageList;
    2424    PersistentForm1: TPersistentForm;
    25     XMLConfig1: TXMLConfig;
    2625    procedure AAboutExecute(Sender: TObject);
    2726    procedure ADatabaseConnectExecute(Sender: TObject);
     
    3332    procedure DataModuleDestroy(Sender: TObject);
    3433  private
    35     FDatabase: TDatabase;
     34    FDbClient: TDbClient;
    3635    Initialized: Boolean;
    37     procedure InitEngines;
    38     procedure InitDataTypes;
    39     procedure SetDatabase(AValue: TDatabase);
     36    procedure SetDbClient(AValue: TDbClient);
    4037  public
    4138    Preferences: TPreferences;
    42     Databases: TDatabases;
    43     Engines: TDatabaseEngines;
    44     DataTypes: TDataTypes;
     39    DbManager: TDbManager;
    4540    procedure LoadConfig;
    4641    procedure SaveConfig;
     
    4843    procedure Done;
    4944    procedure UpdateInterface;
    50     property Database: TDatabase read FDatabase write SetDatabase;
     45    property DbClient: TDbClient read FDbClient write SetDbClient;
    5146  end;
    5247
     
    5449  Core: TCore;
    5550
     51const
     52  DefaultRegKey = '\Software\Chronosoft\MyData';
     53
    5654
    5755implementation
    5856
    5957uses
    60   UEngineXML, UEngineMySQL, UEngineSQLite, UFormTables, UFormConnect,
    61   UFormDatabases, UDataTypes, UFormPreferences, UFormMain, UFormAbout;
     58  UFormTables, UFormConnect,
     59  UFormDatabases, UFormPreferences, UFormMain, UFormAbout;
    6260
    6361{$R *.lfm}
    6462
    65 resourcestring
    66   STypeString = 'String';
    67   STypeInteger = 'Integer';
    68   STypeFloat = 'Float';
    69   STypeBoolean = 'Boolean';
    70   STypeMapPosition = 'Map position';
    71   STypeImage = 'Image';
    72   STypeDate = 'Date';
    73   STypeTime = 'Time';
    74   STypeDateTime = 'Date and time';
    75   STypeReference = 'Reference';
    76 
    7763{ TCore }
    7864
    7965procedure TCore.DataModuleCreate(Sender: TObject);
    8066begin
    81   Databases := TDatabases.Create;
    82   Engines := TDatabaseEngines.Create;
    83   DataTypes := TDataTypes.Create;
     67  DbManager := TDbManager.Create;
    8468  Preferences := TPreferences.Create;
    8569end;
    8670
     71procedure TCore.DataModuleDestroy(Sender: TObject);
     72begin
     73  DbClient := nil;
     74  FreeAndNil(Preferences);
     75  FreeAndNil(DbManager);
     76end;
     77
    8778procedure TCore.AExitExecute(Sender: TObject);
    8879begin
     
    9283procedure TCore.ADatabaseDisconnectExecute(Sender: TObject);
    9384begin
    94   Database := nil;
     85  DbClient := nil;
    9586end;
    9687
    9788procedure TCore.ADatabaseConnectExecute(Sender: TObject);
     89var
     90  NewClient: TDbClient;
    9891begin
    9992  if FormDatabases.ShowModal = mrOk then begin
    100     Database := FormDatabases.SelectedDatabase;
     93    DbClient := nil;
     94    NewClient := FormDatabases.SelectedProfile.ClientType.DatabaseClientClass.Create;
     95    NewClient.ConnectProfile := FormDatabases.SelectedProfile;
     96    DbClient := NewClient;
    10197  end;
    10298end;
     
    122118  I: Integer;
    123119begin
    124   for I := 0 to DataTypes.Count - 1 do begin
    125     TDataType(DataTypes[I]).Title := CoolTranslator1.TranslateText(TDataType(DataTypes[I]).Title, TDataType(DataTypes[I]).Title);
    126   end;
    127 end;
    128 
    129 procedure TCore.DataModuleDestroy(Sender: TObject);
    130 begin
    131   FreeAndNil(Preferences);
    132   FreeAndNil(DataTypes);
    133   FreeAndNil(Engines);
    134   FreeAndNil(Databases);
    135 end;
    136 
    137 procedure TCore.InitEngines;
    138 var
    139   NewEngine: TDatabaseEngine;
    140 begin
    141   Engines.Clear;
    142   NewEngine := Engines.RegisterEngine('XML file', TDatabaseXML);
    143   NewEngine.UseTypes(DataTypes, [ftString, ftInteger, ftDateTime, ftBoolean, ftFloat]);
    144 
    145   NewEngine := Engines.RegisterEngine('MySQL', TDatabaseMySQL);
    146   NewEngine.UseTypes(DataTypes, [ftString, ftInteger, ftDateTime, ftBoolean, ftFloat,
    147     ftReference]);
    148 
    149   NewEngine := Engines.RegisterEngine('SQLite', TDatabaseSQLite);
    150   NewEngine.UseTypes(DataTypes, [ftString, ftInteger, ftDateTime, ftBoolean, ftFloat]);
    151 end;
    152 
    153 procedure TCore.InitDataTypes;
    154 begin
    155   DataTypes.Clear;
    156   with DataTypes do begin
    157     RegisterType(1, 'String', STypeString, ftString, TFieldString);
    158     RegisterType(2, 'Integer', STypeInteger, ftInteger, TFieldInteger);
    159     RegisterType(3, 'DateTime', STypeDateTime, ftDateTime, TFieldDateTime);
    160     RegisterType(4, 'Boolean', STypeBoolean, ftBoolean, TFieldBoolean);
    161     RegisterType(5, 'Float', STypeFloat, ftFloat, TFieldFloat);
    162     RegisterType(6, 'MapPosition', STypeMapPosition, ftMapPosition, TFieldMapPosition);
    163     RegisterType(7, 'Date', STypeDate, ftDate, TFieldDate);
    164     RegisterType(8, 'Time', STypeTime, ftTime, TFieldTime);
    165     RegisterType(9, 'Image', STypeImage, ftImage, TFieldImage);
    166     RegisterType(10, 'Reference', STypeReference, ftReference, TFieldReference);
    167   end;
    168 end;
    169 
    170 procedure TCore.SetDatabase(AValue: TDatabase);
    171 begin
    172   if FDatabase = AValue then Exit;
    173   if Assigned(FDatabase) then begin
    174     FDatabase.Save;
    175     FDatabase.Clear;
    176   end;
    177   FDatabase := AValue;
    178   if Assigned(FDatabase) then begin
    179     FDatabase.Load;
    180     Preferences.LastDatabaseName := FDatabase.Name;
    181   end;
    182   FormTables.Database := FDatabase;
     120  for I := 0 to DbManager.DataTypes.Count - 1 do begin
     121    TDataType(DbManager.DataTypes[I]).Title := CoolTranslator1.TranslateText(TDataType(DbManager.DataTypes[I]).Title, TDataType(DbManager.DataTypes[I]).Title);
     122  end;
     123end;
     124
     125procedure TCore.SetDbClient(AValue: TDbClient);
     126begin
     127  if FDbClient = AValue then Exit;
     128  if Assigned(FDbClient) then begin
     129    DbClient.Save;
     130    FDbClient.Free;
     131  end;
     132  FDbClient := AValue;
     133  if Assigned(FDbClient) then begin
     134    DbClient.Load;
     135    Preferences.LastDatabaseName := FDbClient.ConnectProfile.Name;
     136    if not Assigned(FormTables.Tables) then
     137      FormTables.Tables := TTables.Create;
     138    DbClient.LoadTables(FormTables.Tables);
     139  end;
    183140  UpdateInterface;
    184141end;
    185142
    186143procedure TCore.LoadConfig;
    187 var
    188   I: Integer;
    189   NewDatabase: TDatabase;
    190   Engine: TDatabaseEngine;
    191 begin
    192   XMLConfig1.FileName := 'Config.xml';
    193   Databases.Count := XMLConfig1.GetValue('Database/Count', 0);
    194   for I := 0 to Databases.Count - 1 do begin
    195     Engine := Core.Engines.FindByName(XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/Engine', ''));
    196 
    197     NewDatabase := TDatabase.Create;
    198     NewDatabase.Engine := Engine;
    199     NewDatabase.Name := XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/Name', '');
    200     NewDatabase.ConnectionString := XMLConfig1.GetValue('Database/Item' + IntToStr(I) + '/ConnectionString', '');
    201     Databases[I] := NewDatabase;
    202   end;
    203   //Databases.ConnectionString := XMLConfig1.GetValue('ConnectionString', 'file://' + ExtractFileDir(Application.ExeName) + '/data.xml');
    204   Preferences.RememberDatabase := XMLConfig1.GetValue('RememberDatabase', True);
    205   Preferences.LastDatabaseName := XMLConfig1.GetValue('LastDatabaseName', '');
    206   CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(XMLConfig1.GetValue('Language', ''));
     144begin
     145  with TRegistryEx.Create do
     146  try
     147    CurrentContext := TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey);
     148    Preferences.RememberDatabase := GetValue('RememberDatabase', True);
     149    Preferences.LastDatabaseName := GetValue('LastDatabaseName', '');
     150    CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(
     151      GetValue('Language', ''));
     152  finally
     153    Free;
     154  end;
     155  DbManager.ConnectProfiles.LoadFromRegistry(TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey + '\Database'));
    207156end;
    208157
    209158procedure TCore.SaveConfig;
    210 var
    211   I: Integer;
    212 begin
    213   XMLConfig1.SetValue('Database/Count', Databases.Count);
    214   for I := 0 to Databases.Count - 1 do begin
    215     XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/Name', TDatabase(Databases[I]).Name);
    216     XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/ConnectionString', TDatabase(Databases[I]).ConnectionString);
    217     XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/Engine', TDatabase(Databases[I]).Engine.Name);
    218   end;
    219   XMLConfig1.SetValue('RememberDatabase', Preferences.RememberDatabase);
    220   XMLConfig1.SetValue('LastDatabaseName', Preferences.LastDatabaseName);
    221   XMLConfig1.SetValue('Language', CoolTranslator1.Language.Code);
     159begin
     160  with TRegistryEx.Create do
     161  try
     162    CurrentContext := TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey);
     163    SetValue('RememberDatabase', Preferences.RememberDatabase);
     164    SetValue('LastDatabaseName', Preferences.LastDatabaseName);
     165    SetValue('Language', CoolTranslator1.Language.Code);
     166  finally
     167    Free;
     168  end;
     169  DbManager.ConnectProfiles.SaveToRegistry(TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey + '\Database'));
    222170end;
    223171
    224172procedure TCore.Init;
     173var
     174  ConnectProfile: TDbConnectProfile;
    225175begin
    226176  if not Initialized then begin
     
    230180      CoolTranslator1.POFilesFolder := '/usr/share/MyData/languages';
    231181    {$ENDIF}
    232     InitDataTypes;
    233     InitEngines;
    234182    FormConnect.Init;
    235183    Initialized := True;
     
    238186    UpdateInterface;
    239187    if Preferences.RememberDatabase then begin
    240       Database := Databases.FindByName(Preferences.LastDatabaseName);
     188      ConnectProfile := DbManager.ConnectProfiles.FindByName(Preferences.LastDatabaseName);
     189      if Assigned(ConnectProfile) then
     190        DbClient := ConnectProfile.ClientType.DatabaseClientClass.Create;
    241191    end else ADatabaseConnect.Execute;
    242192  end;
     
    250200procedure TCore.UpdateInterface;
    251201begin
    252   ADatabaseDisconnect.Enabled := Assigned(FormTables.Database);
     202  ADatabaseDisconnect.Enabled := Assigned(DbClient);
    253203  FormMain.UpdateInterface;
    254204end;
Note: See TracChangeset for help on using the changeset viewer.