Changeset 43 for trunk/Application
- Timestamp:
- Mar 9, 2012, 1:09:52 PM (13 years ago)
- Location:
- trunk/Application
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Application/Clients/UChronisClientMySQL.pas
r40 r43 14 14 TChronisClientMySQL = class(TChronisClient) 15 15 protected 16 procedure InitSystemTypes; override; 16 17 function GetConnected: Boolean; override; 17 18 public … … 23 24 procedure ListLoad(AList: TListProxy); override; 24 25 procedure ListSave(AList: TListProxy); override; 25 procedure DefineType(AType: TChronisType); override;26 procedure UndefineType(AType: TChronisType); override;26 procedure TypeDefine(AType: TChronisType); override; 27 procedure TypeUndefine(AType: TChronisType); override; 27 28 procedure Install; 28 29 procedure Uninstall; … … 35 36 implementation 36 37 38 39 resourcestring 40 SMissingBaseType = 'Missing base typ for %s'; 41 SUndefinedType = 'Undefinned type %s'; 42 43 37 44 { TChronisClientMySQL } 45 46 procedure TChronisClientMySQL.InitSystemTypes; 47 begin 48 inherited InitSystemTypes; 49 Types.AddType('Integer', 'int(11)'); 50 Types.AddType('String', 'varchar(255)'); 51 Types.AddType('RelationOne', 'int(11)'); 52 Types.AddType('Double', 'double'); 53 Types.AddType('DateTime', 'datetime'); 54 Types.AddType('Date', 'date'); 55 Types.AddType('Time', 'time'); 56 Types.AddType('Text', 'text'); 57 Types.AddType('Boolean', 'bool'); 58 end; 38 59 39 60 function TChronisClientMySQL.GetConnected: Boolean; … … 103 124 end; 104 125 105 procedure TChronisClientMySQL. DefineType(AType: TChronisType);126 procedure TChronisClientMySQL.TypeDefine(AType: TChronisType); 106 127 var 107 D ata: TDictionaryStringString;128 DbRows: TDbRows; 108 129 I: Integer; 130 Query: string; 131 RefType: TChronisType; 109 132 begin 110 133 try 111 Data := TDictionaryStringString.Create; 112 Data.Add('Name', AType.Name); 113 Database.Insert('Type', Data); 134 DbRows := TDbRows.Create; 135 Query := 'CREATE TABLE IF NOT EXISTS `' + AType.Name + '` ( ' + 136 '`Id` int(11) NOT NULL AUTO_INCREMENT,'; 137 for I := 0 to AType.Properties.Count - 1 do 138 with AType.Properties do begin 139 RefType := Types.SearchByName(Items[I].Value); 140 if not Assigned(RefType) then 141 raise Exception.Create(Format(SUndefinedType, [Items[I].Value])); 142 if RefType.DbType = '' then 143 raise Exception.Create(Format(SMissingBaseType, [RefType.Name])); 144 145 Query := Query + '`' + Items[I].Key + '` ' + RefType.DbType + ' NOT NULL,'; 146 end; 147 Query := Query + 'PRIMARY KEY (`Id`)' + 148 ') ENGINE=InnoDB DEFAULT CHARSET=utf8'; 149 Database.Query(DbRows, Query); 114 150 finally 115 Data.Free; 116 end; 117 if AType is TChronisTypeRecord then begin 118 for I := 0 to TChronisTypeRecord(AType).Items.Count - 1 do 119 try 120 Data := TDictionaryStringString.Create; 121 Data.Add('Name', 122 TChronisTypeRecordItem(TChronisTypeRecord(AType).Items[I]).Name); 123 Data.Add('Type', IntToStr( 124 TChronisTypeRecordItem(TChronisTypeRecord(AType).Items[I]).ItemType.OID)); 125 Database.Insert('TypeRecordItem', Data); 126 finally 127 Data.Free; 128 end; 129 end; 130 end; 131 132 procedure TChronisClientMySQL.UndefineType(AType: TChronisType); 151 DbRows.Free; 152 end; 153 end; 154 155 procedure TChronisClientMySQL.TypeUndefine(AType: TChronisType); 133 156 begin 134 157 … … 156 179 constructor TChronisClientMySQL.Create; 157 180 begin 181 inherited; 158 182 Database := TSqlDatabase.Create(nil); 159 183 end; -
trunk/Application/Clients/UChronisClientXMLRPC.pas
r32 r43 6 6 7 7 uses 8 Classes, SysUtils; 8 Classes, SysUtils, UChronisClient; 9 10 type 11 TChronisClientXMLRPC = class(TChronisClient) 12 end; 9 13 10 14 implementation -
trunk/Application/UChronisClient.pas
r37 r43 9 9 10 10 type 11 EClientNotSet = class(Exception); 12 11 13 TChronisClient = class; 12 14 … … 52 54 end; 53 55 56 { TChronisType } 57 54 58 TChronisType = class 55 OID: Integer;59 Client: TChronisClient; 56 60 Name: string; 57 end; 58 59 TChronisTypeRecordItem = class 60 Name: string; 61 ItemType: TChronisType; 62 end; 63 64 { TChronisTypeRecord } 65 66 TChronisTypeRecord = class(TChronisType) 67 Items: TListObject; // TListObject<TChronisTypeRecordItem> 61 DbType: string; 62 Properties: TDictionaryStringString; 63 procedure Define; 64 procedure Undefine; 68 65 constructor Create; 69 66 destructor Destroy; override; 70 67 end; 71 68 69 { TChronisTypeList } 70 71 TChronisTypeList = class(TListObject) 72 Client: TChronisClient; 73 function AddType(Name: string; DbType: string = ''): TChronisType; 74 function SearchByName(Name: string): TChronisType; 75 end; 76 72 77 { TChronisClient } 73 78 74 79 TChronisClient = class 80 private 75 81 protected 82 procedure InitSystemTypes; virtual; 76 83 function GetConnected: Boolean; virtual; 77 84 public … … 81 88 User: string; 82 89 Password: string; 90 Types: TChronisTypeList; 83 91 procedure ObjectLoad(AObject: TObjectProxy); virtual; abstract; 84 92 procedure ObjectSave(AObject: TObjectProxy); virtual; abstract; … … 87 95 procedure ListLoad(AList: TListProxy); virtual; abstract; 88 96 procedure ListSave(AList: TListProxy); virtual; abstract; 89 procedure DefineType(AType: TChronisType); virtual; abstract; 90 procedure UndefineType(AType: TChronisType); virtual; abstract; 97 procedure TypeDefine(AType: TChronisType); virtual; abstract; 98 procedure TypeUndefine(AType: TChronisType); virtual; abstract; 99 procedure CheckTypes; 91 100 constructor Create; virtual; 101 destructor Destroy; override; 92 102 procedure Connect; virtual; abstract; 93 103 procedure Disconnect; virtual; abstract; … … 95 105 end; 96 106 107 97 108 implementation 98 109 99 { TChronisTypeRecord } 100 101 constructor TChronisTypeRecord.Create; 102 begin 103 Items := TListObject.Create; 104 end; 105 106 destructor TChronisTypeRecord.Destroy; 107 begin 108 Items.Free; 110 resourcestring 111 SClientNotSet = 'Client not set'; 112 113 { TChronisTypeList } 114 115 function TChronisTypeList.AddType(Name: string; DbType: string = ''): TChronisType; 116 begin 117 Result := TChronisType(AddNew(TChronisType.Create)); 118 Result.Client := Client; 119 Result.Name := Name; 120 Result.DbType := DbType; 121 end; 122 123 function TChronisTypeList.SearchByName(Name: string): TChronisType; 124 var 125 I: Integer; 126 begin 127 I := 0; 128 while (I < Count) and (TChronisType(Items[I]).Name <> Name) do Inc(I); 129 if I < Count then Result := TChronisType(Items[I]) 130 else Result := nil; 131 end; 132 133 procedure TChronisType.Define; 134 begin 135 if Assigned(Client) then Client.TypeDefine(Self) 136 else raise EClientNotSet.Create(SClientNotSet); 137 end; 138 139 procedure TChronisType.Undefine; 140 begin 141 if Assigned(Client) then Client.TypeUndefine(Self) 142 else raise EClientNotSet.Create(SClientNotSet); 143 end; 144 145 constructor TChronisType.Create; 146 begin 147 Properties := TDictionaryStringString.Create; 148 end; 149 150 destructor TChronisType.Destroy; 151 begin 152 Properties.Free; 109 153 inherited Destroy; 110 154 end; … … 114 158 procedure TObjectProxy.Load; 115 159 begin 116 Client.ObjectLoad(Self); 160 if Assigned(Client) then Client.ObjectLoad(Self) 161 else raise EClientNotSet.Create(SClientNotSet); 117 162 end; 118 163 119 164 procedure TObjectProxy.Save; 120 165 begin 121 Client.ObjectSave(Self); 166 if Assigned(Client) then Client.ObjectSave(Self) 167 else raise EClientNotSet.Create(SClientNotSet); 122 168 end; 123 169 124 170 procedure TObjectProxy.Delete; 125 171 begin 126 Client.ObjectDelete(Self); 172 if Assigned(Client) then Client.ObjectDelete(Self) 173 else raise EClientNotSet.Create(SClientNotSet); 127 174 end; 128 175 129 176 procedure TObjectProxy.Add; 130 177 begin 131 Client.ObjectAdd(Self); 178 if Assigned(Client) then Client.ObjectAdd(Self) 179 else raise EClientNotSet.Create(SClientNotSet); 132 180 end; 133 181 … … 168 216 procedure TListProxy.Load; 169 217 begin 170 Client.ListLoad(Self); 218 if Assigned(Client) then Client.ListLoad(Self) 219 else raise EClientNotSet.Create(SClientNotSet); 171 220 end; 172 221 173 222 procedure TListProxy.Save; 174 223 begin 175 Client.ListSave(Self); 224 if Assigned(Client) then Client.ListSave(Self) 225 else raise EClientNotSet.Create(SClientNotSet); 176 226 end; 177 227 178 228 { TChronisClient } 179 229 230 procedure TChronisClient.InitSystemTypes; 231 begin 232 end; 233 180 234 function TChronisClient.GetConnected: Boolean; 181 235 begin … … 183 237 end; 184 238 239 procedure TChronisClient.CheckTypes; 240 var 241 StructureVersion: string; 242 Data: TDictionaryStringString; 243 ObjectId: Integer; 244 Tables: TListString; 245 I: Integer; 246 NewProxy: TListProxy; 247 begin 248 try 249 Tables := TListString.Create; 250 Data := TDictionaryStringString.Create; 251 252 NewProxy := TListProxy.Create; 253 NewProxy.Client := Self; 254 NewProxy.SchemaName := 'information_schema'; 255 NewProxy.ObjectName := 'TABLES'; 256 NewProxy.Condition := 'TABLE_SCHEMA = "' + Schema + '"'; 257 NewProxy.Load; 258 //Database.Query(DbRows, 'SHOW TABLES'); 259 Tables.Count := NewProxy.Objects.Count; 260 for I := 0 to NewProxy.Objects.Count - 1 do 261 Tables[I] := TObjectProxy(NewProxy.Objects[I]).Properties.Values['TABLE_NAME']; 262 263 for I := 0 to Types.Count - 1 do 264 with TChronisType(Types[I]) do begin 265 if (DbType = '') and (Tables.IndexOf(Name) = -1) then begin 266 Define; 267 end; 268 end; 269 finally 270 NewProxy.Free; 271 Data.Free; 272 Tables.Free; 273 end; 274 end; 275 185 276 constructor TChronisClient.Create; 186 277 begin 278 Types := TChronisTypeList.Create; 279 Types.Client := Self; 280 InitSystemTypes; 281 end; 282 283 destructor TChronisClient.Destroy; 284 begin 285 Types.Free; 286 inherited Destroy; 187 287 end; 188 288
Note:
See TracChangeset
for help on using the changeset viewer.