| 1 | unit UPDClientMySQL;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, SqlDatabase, UPDClient, Generics;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 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 |
|
|---|
| 47 | implementation
|
|---|
| 48 |
|
|---|
| 49 | resourcestring
|
|---|
| 50 | SMissingBaseType = 'Missing base typ for %s';
|
|---|
| 51 | SUndefinedType = 'Undefined type in %0:s.%1:s';
|
|---|
| 52 |
|
|---|
| 53 | { TPDClientMySQL }
|
|---|
| 54 |
|
|---|
| 55 | procedure TPDClientMySQL.InitSystemTypes;
|
|---|
| 56 | begin
|
|---|
| 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');
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | function TPDClientMySQL.GetConnected: Boolean;
|
|---|
| 70 | begin
|
|---|
| 71 | Result := Database.Connected;
|
|---|
| 72 | end;
|
|---|
| 73 |
|
|---|
| 74 | procedure TPDClientMySQL.ObjectLoad(AObject: TObjectProxy);
|
|---|
| 75 | var
|
|---|
| 76 | DbRows: TDbRows;
|
|---|
| 77 | NewObject: TObjectProxy;
|
|---|
| 78 | Table: string;
|
|---|
| 79 | begin
|
|---|
| 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;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | procedure TPDClientMySQL.ObjectSave(AObject: TObjectProxy);
|
|---|
| 94 | var
|
|---|
| 95 | DbRows: TDbRows;
|
|---|
| 96 | NewObject: TObjectProxy;
|
|---|
| 97 | Table: string;
|
|---|
| 98 | begin
|
|---|
| 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;
|
|---|
| 109 | end;
|
|---|
| 110 |
|
|---|
| 111 | procedure TPDClientMySQL.ObjectDelete(AObject: TObjectProxy);
|
|---|
| 112 | begin
|
|---|
| 113 | Database.Delete(AObject.ObjectName, 'Id=' + IntToStr(AObject.Id),
|
|---|
| 114 | AObject.Path);
|
|---|
| 115 | end;
|
|---|
| 116 |
|
|---|
| 117 | procedure TPDClientMySQL.ListLoad(AList: TListProxy);
|
|---|
| 118 | var
|
|---|
| 119 | DbRows: TDbRows;
|
|---|
| 120 | Filter: string;
|
|---|
| 121 | DbCondition: string;
|
|---|
| 122 | I: Integer;
|
|---|
| 123 | NewObject: TObjectProxy;
|
|---|
| 124 | Table: string;
|
|---|
| 125 | begin
|
|---|
| 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;
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | procedure TPDClientMySQL.ListSave(AList: TListProxy);
|
|---|
| 154 | begin
|
|---|
| 155 | end;
|
|---|
| 156 |
|
|---|
| 157 | procedure TPDClientMySQL.TypeDefine(AType: TPDType);
|
|---|
| 158 | var
|
|---|
| 159 | DbRows: TDbRows;
|
|---|
| 160 | I: Integer;
|
|---|
| 161 | Query: string;
|
|---|
| 162 | RefType: TPDType;
|
|---|
| 163 | begin
|
|---|
| 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;
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | procedure TPDClientMySQL.TypeUndefine(AType: TPDType);
|
|---|
| 187 | var
|
|---|
| 188 | DbRows: TDbRows;
|
|---|
| 189 | I: Integer;
|
|---|
| 190 | Query: string;
|
|---|
| 191 | RefType: TPDType;
|
|---|
| 192 | begin
|
|---|
| 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;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | function TPDClientMySQL.TypeIsDefined(AType: TPDType): Boolean;
|
|---|
| 203 | var
|
|---|
| 204 | NewProxy: TListProxy;
|
|---|
| 205 | begin
|
|---|
| 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;
|
|---|
| 218 | end;
|
|---|
| 219 |
|
|---|
| 220 | procedure TPDClientMySQL.Install;
|
|---|
| 221 | begin
|
|---|
| 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'];*)
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | procedure TPDClientMySQL.Uninstall;
|
|---|
| 235 | begin
|
|---|
| 236 |
|
|---|
| 237 | end;
|
|---|
| 238 |
|
|---|
| 239 | procedure TPDClientMySQL.Init;
|
|---|
| 240 | begin
|
|---|
| 241 | inherited;
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | function TPDClientMySQL.GetConnectionString: string;
|
|---|
| 245 | begin
|
|---|
| 246 | Result := 'Host:' + Host + ',Port:' + IntToStr(Port) + ',User:' + User +
|
|---|
| 247 | ',Password:' + Password + ',Schema:' + Schema;
|
|---|
| 248 | end;
|
|---|
| 249 |
|
|---|
| 250 | constructor TPDClientMySQL.Create(AOwner: TComponent);
|
|---|
| 251 | begin
|
|---|
| 252 | inherited;
|
|---|
| 253 | FDatabase := TSqlDatabase.Create(nil);
|
|---|
| 254 | BackendName := 'MySQL';
|
|---|
| 255 | end;
|
|---|
| 256 |
|
|---|
| 257 | destructor TPDClientMySQL.Destroy;
|
|---|
| 258 | begin
|
|---|
| 259 | FreeAndNil(FDatabase);
|
|---|
| 260 | inherited;
|
|---|
| 261 | end;
|
|---|
| 262 |
|
|---|
| 263 | procedure TPDClientMySQL.Connect;
|
|---|
| 264 | begin
|
|---|
| 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;
|
|---|
| 274 | end;
|
|---|
| 275 |
|
|---|
| 276 | procedure TPDClientMySQL.Disconnect;
|
|---|
| 277 | begin
|
|---|
| 278 | if Connected then Database.Disconnect;
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | end.
|
|---|
| 282 |
|
|---|