Changeset 20 for trunk/UCore.pas
- Timestamp:
- Mar 23, 2018, 1:59:25 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UCore.pas
r19 r20 6 6 7 7 uses 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; 10 10 11 11 type … … 23 23 ImageList1: TImageList; 24 24 PersistentForm1: TPersistentForm; 25 XMLConfig1: TXMLConfig;26 25 procedure AAboutExecute(Sender: TObject); 27 26 procedure ADatabaseConnectExecute(Sender: TObject); … … 33 32 procedure DataModuleDestroy(Sender: TObject); 34 33 private 35 FD atabase: TDatabase;34 FDbClient: TDbClient; 36 35 Initialized: Boolean; 37 procedure InitEngines; 38 procedure InitDataTypes; 39 procedure SetDatabase(AValue: TDatabase); 36 procedure SetDbClient(AValue: TDbClient); 40 37 public 41 38 Preferences: TPreferences; 42 Databases: TDatabases; 43 Engines: TDatabaseEngines; 44 DataTypes: TDataTypes; 39 DbManager: TDbManager; 45 40 procedure LoadConfig; 46 41 procedure SaveConfig; … … 48 43 procedure Done; 49 44 procedure UpdateInterface; 50 property D atabase: TDatabase read FDatabase write SetDatabase;45 property DbClient: TDbClient read FDbClient write SetDbClient; 51 46 end; 52 47 … … 54 49 Core: TCore; 55 50 51 const 52 DefaultRegKey = '\Software\Chronosoft\MyData'; 53 56 54 57 55 implementation 58 56 59 57 uses 60 U EngineXML, UEngineMySQL, UEngineSQLite, UFormTables, UFormConnect,61 UFormDatabases, U DataTypes, UFormPreferences, UFormMain, UFormAbout;58 UFormTables, UFormConnect, 59 UFormDatabases, UFormPreferences, UFormMain, UFormAbout; 62 60 63 61 {$R *.lfm} 64 62 65 resourcestring66 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 77 63 { TCore } 78 64 79 65 procedure TCore.DataModuleCreate(Sender: TObject); 80 66 begin 81 Databases := TDatabases.Create; 82 Engines := TDatabaseEngines.Create; 83 DataTypes := TDataTypes.Create; 67 DbManager := TDbManager.Create; 84 68 Preferences := TPreferences.Create; 85 69 end; 86 70 71 procedure TCore.DataModuleDestroy(Sender: TObject); 72 begin 73 DbClient := nil; 74 FreeAndNil(Preferences); 75 FreeAndNil(DbManager); 76 end; 77 87 78 procedure TCore.AExitExecute(Sender: TObject); 88 79 begin … … 92 83 procedure TCore.ADatabaseDisconnectExecute(Sender: TObject); 93 84 begin 94 D atabase:= nil;85 DbClient := nil; 95 86 end; 96 87 97 88 procedure TCore.ADatabaseConnectExecute(Sender: TObject); 89 var 90 NewClient: TDbClient; 98 91 begin 99 92 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; 101 97 end; 102 98 end; … … 122 118 I: Integer; 123 119 begin 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; 123 end; 124 125 procedure TCore.SetDbClient(AValue: TDbClient); 126 begin 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; 183 140 UpdateInterface; 184 141 end; 185 142 186 143 procedure 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', '')); 144 begin 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')); 207 156 end; 208 157 209 158 procedure 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); 159 begin 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')); 222 170 end; 223 171 224 172 procedure TCore.Init; 173 var 174 ConnectProfile: TDbConnectProfile; 225 175 begin 226 176 if not Initialized then begin … … 230 180 CoolTranslator1.POFilesFolder := '/usr/share/MyData/languages'; 231 181 {$ENDIF} 232 InitDataTypes;233 InitEngines;234 182 FormConnect.Init; 235 183 Initialized := True; … … 238 186 UpdateInterface; 239 187 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; 241 191 end else ADatabaseConnect.Execute; 242 192 end; … … 250 200 procedure TCore.UpdateInterface; 251 201 begin 252 ADatabaseDisconnect.Enabled := Assigned( FormTables.Database);202 ADatabaseDisconnect.Enabled := Assigned(DbClient); 253 203 FormMain.UpdateInterface; 254 204 end;
Note:
See TracChangeset
for help on using the changeset viewer.