1 | unit ModuleSystem;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, ModularSystem, Generics;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TModuleSystem }
|
---|
11 |
|
---|
12 | TModuleSystem = class(TModule)
|
---|
13 | private
|
---|
14 | procedure ModuleChange(Sender: TObject; Module: TModule);
|
---|
15 | public
|
---|
16 | constructor Create(Owner: TComponent); override;
|
---|
17 | destructor Destroy; override;
|
---|
18 | procedure Start; override;
|
---|
19 | procedure Stop; override;
|
---|
20 | procedure Install; override;
|
---|
21 | procedure Uninstall; override;
|
---|
22 | procedure Upgrade; override;
|
---|
23 | procedure UpdateModuleList;
|
---|
24 | end;
|
---|
25 |
|
---|
26 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | uses
|
---|
30 | Core, SqlDatabase;
|
---|
31 |
|
---|
32 | { TModuleSystem }
|
---|
33 |
|
---|
34 | procedure TModuleSystem.ModuleChange(Sender: TObject; Module: TModule);
|
---|
35 | var
|
---|
36 | DbRows: TDbRows;
|
---|
37 | Data: TDictionaryStringString;
|
---|
38 | begin
|
---|
39 | try
|
---|
40 | DbRows := TDbRows.Create;
|
---|
41 | Data := TDictionaryStringString.Create;
|
---|
42 | if Module.Installed then Data.Add('Installed', '1')
|
---|
43 | else Data.Add('Installed', '0');
|
---|
44 | Core.Core.CommonDatabase.Update('SystemModule', Data, 'Name="' + Module.Identification + '"');
|
---|
45 | finally
|
---|
46 | Data.Free;
|
---|
47 | DbRows.Free;
|
---|
48 | end;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | constructor TModuleSystem.Create(Owner: TComponent);
|
---|
52 | begin
|
---|
53 | inherited Create(Owner);
|
---|
54 | Identification := 'System';
|
---|
55 | Title := 'Base modular system';
|
---|
56 | Version := '1.0';
|
---|
57 | License := 'GNU/LGPL v3';
|
---|
58 | Author := 'Chronosoft';
|
---|
59 | end;
|
---|
60 |
|
---|
61 | destructor TModuleSystem.Destroy;
|
---|
62 | begin
|
---|
63 | inherited;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | procedure TModuleSystem.Start;
|
---|
67 | var
|
---|
68 | DbRows: TDbRows;
|
---|
69 | I: Integer;
|
---|
70 | Module: TModule;
|
---|
71 | begin
|
---|
72 | try
|
---|
73 | DbRows := TDbRows.Create;
|
---|
74 | Core.Core.CommonDatabase.Select(DbRows, 'SystemModule', '`Name`, `Installed`');
|
---|
75 | for I := 0 to DbRows.Count - 1 do
|
---|
76 | with DbRows[I] do begin
|
---|
77 | Module := Manager.FindModuleByName(Items['Name']);
|
---|
78 | if Assigned(Module) then
|
---|
79 | if Items['Installed'] = '1' then Module.SetInstalledState(True)
|
---|
80 | else Module.SetInstalledState(False);
|
---|
81 | end;
|
---|
82 | finally
|
---|
83 | DbRows.Free;
|
---|
84 | end;
|
---|
85 | Manager.OnModuleChange := ModuleChange;
|
---|
86 | inherited;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TModuleSystem.Stop;
|
---|
90 | begin
|
---|
91 | BeforeStop;
|
---|
92 | Manager.OnModuleChange := nil;
|
---|
93 | AfterStop;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TModuleSystem.Install;
|
---|
97 | var
|
---|
98 | DbRows: TDbRows;
|
---|
99 | begin
|
---|
100 | try
|
---|
101 | DbRows := TDbRows.Create;
|
---|
102 | Core.Core.CommonDatabase.Query(DbRows,
|
---|
103 | 'CREATE TABLE IF NOT EXISTS `SystemModule` (' +
|
---|
104 | ' `Id` int(11) NOT NULL AUTO_INCREMENT,' +
|
---|
105 | ' `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
106 | ' `Title` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
107 | ' `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
108 | ' `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
109 | ' `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
110 | ' `Installed` int(11) NOT NULL,' +
|
---|
111 | ' `Description` text COLLATE utf8_czech_ci NOT NULL,' +
|
---|
112 | ' `Dependencies` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
|
---|
113 | ' PRIMARY KEY (`Id`)' +
|
---|
114 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;');
|
---|
115 | finally
|
---|
116 | DbRows.Free;
|
---|
117 | end;
|
---|
118 | UpdateModuleList;
|
---|
119 | inherited;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TModuleSystem.Uninstall;
|
---|
123 | var
|
---|
124 | DbRows: TDbRows;
|
---|
125 | begin
|
---|
126 | inherited;
|
---|
127 | try
|
---|
128 | DbRows := TDbRows.Create;
|
---|
129 | Core.Core.CommonDatabase.Query(DbRows, 'DROP TABLE IF EXISTS `SystemModule`');
|
---|
130 | finally
|
---|
131 | DbRows.Free;
|
---|
132 | end;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TModuleSystem.Upgrade;
|
---|
136 | begin
|
---|
137 | inherited;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TModuleSystem.UpdateModuleList;
|
---|
141 | var
|
---|
142 | DbRows: TDbRows;
|
---|
143 | I: Integer;
|
---|
144 | Index: Integer;
|
---|
145 | Data: TDictionaryStringString;
|
---|
146 | begin
|
---|
147 | try
|
---|
148 | DbRows := TDbRows.Create;
|
---|
149 | Data := TDictionaryStringString.Create;
|
---|
150 |
|
---|
151 | Core.Core.CommonDatabase.Select(DbRows, 'SystemModule', 'Name');
|
---|
152 |
|
---|
153 | for I := 0 to Manager.Modules.Count - 1 do
|
---|
154 | with TModule(Manager.Modules[I]) do begin
|
---|
155 | Data.Clear;
|
---|
156 | Data.Add('Name', Identification);
|
---|
157 | Data.Add('Version', Version);
|
---|
158 | Data.Add('License', License);
|
---|
159 | Data.Add('Creator', Author);
|
---|
160 | Data.Add('Description', Description.Text);
|
---|
161 | Data.Add('Title', Title);
|
---|
162 | Data.Add('Dependencies', Dependencies.Text);
|
---|
163 | if Installed then Data.Add('Installed', '1')
|
---|
164 | else Data.Add('Installed', '0');
|
---|
165 |
|
---|
166 | Index := 0;
|
---|
167 | while (Index < DbRows.Count) and (DbRows[Index].Items['Name'] <> Identification) do Inc(Index);
|
---|
168 | if Index >= DbRows.Count then Core.Core.CommonDatabase.Insert('SystemModule', Data)
|
---|
169 | else Core.Core.CommonDatabase.Update('SystemModule', Data, 'Name="' + Identification + '"');
|
---|
170 | end;
|
---|
171 | finally
|
---|
172 | Data.Free;
|
---|
173 | DbRows.Free;
|
---|
174 | end;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | end.
|
---|
178 |
|
---|