source: trunk/Packages/PersistentData/Backend/UPDClientMySQL.pas

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 7.8 KB
Line 
1unit UPDClientMySQL;
2
3interface
4
5uses
6 Classes, SysUtils, SqlDatabase, UPDClient, Generics;
7
8type
9
10 { TPDClientMySQL }
11
12 TPDClientMySQL = class(TPDClient)
13 protected
14 FHost: string;
15 FPort: Word;
16 FUser: string;
17 FPassword: string;
18 FDatabase: TSqlDatabase;
19 procedure InitSystemTypes; override;
20 function GetConnected: Boolean; override;
21 procedure Init; override;
22 function GetConnectionString: string; override;
23 public
24 procedure ObjectLoad(AObject: TObjectProxy); override;
25 procedure ObjectSave(AObject: TObjectProxy); override;
26 procedure ObjectDelete(AObject: TObjectProxy); override;
27 procedure ListLoad(AList: TListProxy); override;
28 procedure ListSave(AList: TListProxy); override;
29 procedure TypeDefine(AType: TPDType); override;
30 procedure TypeUndefine(AType: TPDType); override;
31 function TypeIsDefined(AType: TPDType): Boolean; override;
32 procedure Install;
33 procedure Uninstall;
34 constructor Create(AOwner: TComponent); override;
35 destructor Destroy; override;
36 procedure Connect; override;
37 procedure Disconnect; override;
38 published
39 property Database: TSqlDatabase read FDatabase write FDatabase;
40 property Host: string read FHost write FHost;
41 property Port: Word read FPort write FPort;
42 property User: string read FUser write FUser;
43 property Password: string read FPassword write FPassword;
44 end;
45
46
47implementation
48
49resourcestring
50 SMissingBaseType = 'Missing base typ for %s';
51 SUndefinedType = 'Undefined type in %0:s.%1:s';
52
53{ TPDClientMySQL }
54
55procedure TPDClientMySQL.InitSystemTypes;
56begin
57 inherited InitSystemTypes;
58 Types.AddType('Integer', 'int(11)');
59 Types.AddType('String', 'varchar(255)');
60 Types.AddType('RelationOne', 'int(11)');
61 Types.AddType('Double', 'double');
62 Types.AddType('DateTime', 'datetime');
63 Types.AddType('Date', 'date');
64 Types.AddType('Time', 'time');
65 Types.AddType('Text', 'text');
66 Types.AddType('Boolean', 'bool');
67end;
68
69function TPDClientMySQL.GetConnected: Boolean;
70begin
71 Result := Database.Connected;
72end;
73
74procedure TPDClientMySQL.ObjectLoad(AObject: TObjectProxy);
75var
76 DbRows: TDbRows;
77 NewObject: TObjectProxy;
78 Table: string;
79begin
80 if AObject.Id = 0 then raise Exception.Create(SCantLoadObjectWithoutId);
81 try
82 DbRows := TDbRows.Create;
83 Table := '`' + AObject.ObjectName + '`';
84 if AObject.Path <> '' then Table := '`' + AObject.Path + '`.' + Table;
85 Database.Query(DbRows, 'SELECT * FROM ' + Table +
86 ' WHERE `Id`=' + IntToStr(AObject.Id));
87 AObject.Properties.Assign(TDictionaryStringString(DbRows[0]));
88 finally
89 DbRows.Free;
90 end;
91end;
92
93procedure TPDClientMySQL.ObjectSave(AObject: TObjectProxy);
94var
95 DbRows: TDbRows;
96 NewObject: TObjectProxy;
97 Table: string;
98begin
99 try
100 DbRows := TDbRows.Create;
101 if AObject.Id = 0 then begin
102 Database.Insert(AObject.ObjectName, AObject.Properties, AObject.Path);
103 AObject.Id := Database.LastInsertId;
104 end else Database.Update(AObject.ObjectName, AObject.Properties,
105 'Id=' + IntToStr(AObject.Id), AObject.Path);
106 finally
107 DbRows.Free;
108 end;
109end;
110
111procedure TPDClientMySQL.ObjectDelete(AObject: TObjectProxy);
112begin
113 Database.Delete(AObject.ObjectName, 'Id=' + IntToStr(AObject.Id),
114 AObject.Path);
115end;
116
117procedure TPDClientMySQL.ListLoad(AList: TListProxy);
118var
119 DbRows: TDbRows;
120 Filter: string;
121 DbCondition: string;
122 I: Integer;
123 NewObject: TObjectProxy;
124 Table: string;
125begin
126 try
127 DbRows := TDbRows.Create;
128 if AList.ColummsFilterUse then begin
129 Filter := '';
130 for I := 0 to AList.ColumnsFilter.Count - 1 do
131 Filter := Filter + '`' + AList.ColumnsFilter[I] + '`, ';
132 Delete(Filter, Length(Filter) - 1, 2);
133 end else Filter := '*';
134 if AList.Condition <> '' then DbCondition := ' WHERE ' + AList.Condition
135 else DbCondition := '';
136 Table := '`' + AList.ObjectName + '`';
137 if AList.Path <> '' then Table := '`' + AList.Path + '`.' + Table;
138 Database.Query(DbRows, 'SELECT ' + Filter + ' FROM ' + Table + DbCondition);
139 AList.Objects.Clear;
140 for I := 0 to DbRows.Count - 1 do begin
141 NewObject := TObjectProxy.Create;
142 NewObject.Client := AList.Client;
143 NewObject.ObjectName := AList.ObjectName;
144 NewObject.Path := AList.Path;
145 NewObject.Properties.Assign(TDictionaryStringString(DbRows[I]));
146 AList.Objects.Add(NewObject);
147 end;
148 finally
149 DbRows.Free;
150 end;
151end;
152
153procedure TPDClientMySQL.ListSave(AList: TListProxy);
154begin
155end;
156
157procedure TPDClientMySQL.TypeDefine(AType: TPDType);
158var
159 DbRows: TDbRows;
160 I: Integer;
161 Query: string;
162 RefType: TPDType;
163begin
164 try
165 DbRows := TDbRows.Create;
166 Query := 'CREATE TABLE IF NOT EXISTS `' + AType.Name + '` ( ' +
167 '`Id` int(11) NOT NULL AUTO_INCREMENT,';
168 for I := 0 to AType.Properties.Count - 1 do
169 with AType.Properties do begin
170 RefType := TPDTypeProperty(Items[I]).DbType;
171 if not Assigned(RefType) then
172 raise Exception.Create(Format(SUndefinedType, [AType.Name, TPDTypeProperty(Items[I]).Name]));
173 if RefType.DbType = '' then
174 raise Exception.Create(Format(SMissingBaseType, [RefType.Name]));
175
176 Query := Query + '`' + TPDTypeProperty(Items[I]).Name + '` ' + RefType.DbType + ' NULL,';
177 end;
178 Query := Query + 'PRIMARY KEY (`Id`)' +
179 ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
180 Database.Query(DbRows, Query);
181 finally
182 DbRows.Free;
183 end;
184end;
185
186procedure TPDClientMySQL.TypeUndefine(AType: TPDType);
187var
188 DbRows: TDbRows;
189 I: Integer;
190 Query: string;
191 RefType: TPDType;
192begin
193 try
194 DbRows := TDbRows.Create;
195 Query := 'DROP TABLE IF EXISTS `' + AType.Name + '`';
196 Database.Query(DbRows, Query);
197 finally
198 DbRows.Free;
199 end;
200end;
201
202function TPDClientMySQL.TypeIsDefined(AType: TPDType): Boolean;
203var
204 NewProxy: TListProxy;
205begin
206 try
207 NewProxy := TListProxy.Create;
208 NewProxy.Client := Self;
209 NewProxy.Path := 'information_schema';
210 NewProxy.ObjectName := 'TABLES';
211 NewProxy.Condition := '(TABLE_SCHEMA = "' + Schema +
212 '") AND (TABLE_NAME = "' + AType.Name + '")';
213 NewProxy.Load;
214 Result := NewProxy.Objects.Count > 0;
215 finally
216 NewProxy.Free;
217 end;
218end;
219
220procedure TPDClientMySQL.Install;
221begin
222(* if Tables.IndexOf(InformationTable) = -1 then begin
223 Database.Query(DbRows, 'CREATE TABLE IF NOT EXISTS `' + InformationTable + '` ( ' +
224'`Version` varchar(255) NOT NULL,' +
225'`LastUpdateTime` datetime NOT NULL' +
226') ENGINE=InnoDB DEFAULT CHARSET=utf8;');
227 Database.Query(DbRows, 'INSERT INTO `' + InformationTable + '` (`Version`, `LastUpdateTime`) VALUES ' +
228'("0.1", "0000-00-00 00:00:00");');
229 end;
230 Database.Select(DbRows, InformationTable);
231 StructureVersion := DbRows[0].Values['Version'];*)
232end;
233
234procedure TPDClientMySQL.Uninstall;
235begin
236
237end;
238
239procedure TPDClientMySQL.Init;
240begin
241 inherited;
242end;
243
244function TPDClientMySQL.GetConnectionString: string;
245begin
246 Result := 'Host:' + Host + ',Port:' + IntToStr(Port) + ',User:' + User +
247 ',Password:' + Password + ',Schema:' + Schema;
248end;
249
250constructor TPDClientMySQL.Create(AOwner: TComponent);
251begin
252 inherited;
253 FDatabase := TSqlDatabase.Create(nil);
254 BackendName := 'MySQL';
255end;
256
257destructor TPDClientMySQL.Destroy;
258begin
259 FreeAndNil(FDatabase);
260 inherited;
261end;
262
263procedure TPDClientMySQL.Connect;
264begin
265 if not Connected then begin
266 Database.Port := Port;
267 Database.UserName := User;
268 Database.Password := Password;
269 Database.HostName := Host;
270 Database.Database := Schema;
271 Database.Connect;
272 Init;
273 end;
274end;
275
276procedure TPDClientMySQL.Disconnect;
277begin
278 if Connected then Database.Disconnect;
279end;
280
281end.
282
Note: See TracBrowser for help on using the repository browser.