Changeset 20 for trunk/Forms/UImportStructureForm.pas
- Timestamp:
- Jun 15, 2011, 11:11:59 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UImportStructureForm.pas
r19 r20 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 USystem;9 EditBtn, USystem, SpecializedList, USqlDatabase, SpecializedDictionary; 10 10 11 11 type 12 13 { TSQLTable } 14 15 TSQLTable = class 16 Table: TDictionaryStringString; 17 Columns: TDbRows; 18 function ColumnByName(Name: string): Integer; 19 constructor Create; 20 destructor Destroy; override; 21 end; 12 22 13 23 { TImportStructureForm } … … 25 35 Memo1: TMemo; 26 36 procedure Button1Click(Sender: TObject); 37 procedure FormCreate(Sender: TObject); 38 procedure FormDestroy(Sender: TObject); 27 39 procedure FormShow(Sender: TObject); 28 40 private 41 function ObjectIdByName(Name: string): Integer; 29 42 { private declarations } 30 43 public 31 { public declarations }44 SQLTables: TListObject; // TListObject<TSQLTable> 32 45 end; 33 46 … … 38 51 39 52 uses 40 UCore, USqlDatabase, UMainForm; 53 UCore, UMainForm; 54 55 { TSQLTable } 56 57 function TSQLTable.ColumnByName(Name: string): Integer; 58 var 59 I: Integer; 60 begin 61 I := 0; 62 while (I < Columns.Count) and (Columns[I].Values['COLUMN_NAME'] <> Name) do Inc(I); 63 if I < Columns.Count then Result := I 64 else Result := -1; 65 end; 66 67 constructor TSQLTable.Create; 68 begin 69 Columns := TDbRows.Create; 70 Table := TDictionaryStringString.Create; 71 end; 72 73 destructor TSQLTable.Destroy; 74 begin 75 Columns.Free; 76 Table.Free; 77 inherited Destroy; 78 end; 41 79 42 80 {$R *.lfm} 43 81 44 82 { TImportStructureForm } 83 84 function TImportStructureForm.ObjectIdByName(Name: string): Integer; 85 var 86 I: Integer; 87 begin 88 I := 0; 89 while (I < SQLTables.Count) and (TSQLTable(SQLTables[I]).Table.Items[0].Value <> Name) do Inc(I); 90 if I < SQLTables.Count then Result := I 91 else Result := -1; 92 end; 45 93 46 94 procedure TImportStructureForm.FormShow(Sender: TObject); … … 63 111 ObjectId: Integer; 64 112 PropertyId: Integer; 113 NewTable: TSQLTable; 114 RefObjectIndex: Integer; 115 RefPropertyId: Integer; 65 116 begin 66 117 try … … 73 124 Database.Password := EditPassword.Text; 74 125 Database.Connect; 126 127 SQLTables.Clear; 75 128 GroupId := Core.System.AddGroup(Database.Database, 0); 76 129 Database.Query(DbRows, 'SHOW TABLES'); 77 130 for T := 0 to DbRows.Count - 1 do begin 131 NewTable := TSQLTable(SQLTables.AddNew(TSQLTable.Create)); 132 NewTable.Table.Assign(DbRows[T]); 78 133 TableName := DbRows[T].Items[0].Value; 79 134 Memo1.Lines.Add('Create object "' + TableName + '"'); 80 ObjectId := Core.System.AddObject(TableName, TableName, Database.Database, GroupId); 81 Database.Query(DbRows2, 'SHOW COLUMNS FROM `' + TableName + '`'); 82 for C := 0 to DbRows2.Count - 1 do begin 83 PropertyName := DbRows2[C].Values['Field']; 84 PropType := DbRows2[C].Values['Type']; 85 PropType := Copy(PropType, 1, Pos('(', PropType) - 1); 135 NewTable.Table.Add('ObjId', IntToStr(Core.System.AddObject(TableName, TableName, Database.Database, GroupId))); 136 end; 137 138 for T := 0 to SQLTables.Count - 1 do 139 with TSQLTable(SQLTables[T]) do begin 140 TableName := Table.Items[0].Value; 141 Database.Query(Columns, 'SELECT `TCOL`.`DATA_TYPE`, `TCOL`.`COLUMN_NAME`, ' + 142 ' `KCU`.`REFERENCED_TABLE_SCHEMA`, `KCU`.`REFERENCED_TABLE_NAME`, `KCU`.`REFERENCED_COLUMN_NAME`, ' + 143 ' `TC`.`CONSTRAINT_TYPE` ' + 144 ' FROM `information_schema`.`COLUMNS` AS `TCOL`' + 145 ' LEFT JOIN `information_schema`.`KEY_COLUMN_USAGE` AS `KCU` ON ' + 146 ' (`KCU`.`TABLE_SCHEMA` = `TCOL`.`TABLE_SCHEMA`) AND ' + 147 ' (`KCU`.`TABLE_NAME` = `TCOL`.`TABLE_NAME`) AND ' + 148 ' (`KCU`.`COLUMN_NAME` = `TCOL`.`COLUMN_NAME`) ' + 149 ' LEFT JOIN `information_schema`.`TABLE_CONSTRAINTS` AS `TC` ON' + 150 ' (`KCU`.`CONSTRAINT_NAME` = `TC`.`CONSTRAINT_NAME`) AND ' + 151 ' (`KCU`.`TABLE_NAME` = `TC`.`TABLE_NAME`) AND ' + 152 ' (`KCU`.`CONSTRAINT_SCHEMA` = `TC`.`CONSTRAINT_SCHEMA`)' + 153 ' WHERE ' + 154 '(`TCOL`.`TABLE_SCHEMA` = "' + Database.Database + '") AND ' + 155 '(`TCOL`.`TABLE_NAME` = "' + TableName + '")'); 156 Memo1.Lines.Add('Add properies for object "' + TableName + '"'); 157 ObjectId := StrToInt(Table.Values['ObjId']); 158 for C := 0 to Columns.Count - 1 do begin 159 PropertyName := Columns[C].Values['COLUMN_NAME']; 160 PropType := Columns[C].Values['DATA_TYPE']; 161 if Columns[C].Values['CONSTRAINT_TYPE'] = 'FOREIGN KEY' then begin 162 PropertyId := Core.System.AddPropertyRelationOne(ObjectId, PropertyName, PropertyName, 163 ObjectIdByName(Columns[C].Values['REFERENCED_TABLE_NAME'])); 164 end else 86 165 if PropType = 'int' then 87 166 PropertyId := Core.System.AddPropertyNumber(ObjectId, PropertyName, PropertyName); … … 90 169 if PropType = 'float' then 91 170 PropertyId := Core.System.AddPropertyFloat(ObjectId, PropertyName, PropertyName); 171 if PropType = 'datetime' then 172 PropertyId := Core.System.AddPropertyDateTime(ObjectId, PropertyName, PropertyName); 173 Columns[C].Add('Id', IntToStr(PropertyId)); 92 174 Memo1.Lines.Add('Create property "' + PropertyName + '" of type ' + PropType + '"'); 175 //Memo1.Lines.Add(Columns[C].Values['CONSTRAINT_TYPE']); 176 //TSQLTable(SQLTables[T]).Columns.Add(); 177 end; 178 end; 179 180 for T := 0 to SQLTables.Count - 1 do 181 with TSQLTable(SQLTables[T]) do begin 182 TableName := Table.Items[0].Value; 183 ObjectId := StrToInt(Table.Values['ObjId']); 184 for C := 0 to Columns.Count - 1 do begin 185 PropertyName := Columns[C].Values['COLUMN_NAME']; 186 PropType := Columns[C].Values['DATA_TYPE']; 187 if Columns[C].Values['CONSTRAINT_TYPE'] = 'FOREIGN KEY' then begin 188 Memo1.Lines.Add('Add relation 1:n for "' + TableName + '.' + PropertyName + '"'); 189 RefObjectIndex := ObjectIdByName(Columns[C].Values['REFERENCED_TABLE_NAME']); 190 191 RefPropertyId := TSQLTable(SQLTables[T]).ColumnByName(Columns[C].Values['COLUMN_NAME']); 192 RefPropertyId := StrToInt(TSQLTable(SQLTables[T]).Columns[RefPropertyId].Values['Id']); 193 PropertyId := Core.System.AddPropertyRelationMany(StrToInt(TSQLTable(SQLTables[RefObjectIndex]).Table.Values['ObjId']), TableName, TableName, 194 RefPropertyId); 195 end; 93 196 end; 94 197 end; … … 101 204 end; 102 205 206 procedure TImportStructureForm.FormCreate(Sender: TObject); 207 begin 208 SQLTables := TListObject.Create; 209 end; 210 211 procedure TImportStructureForm.FormDestroy(Sender: TObject); 212 begin 213 SQLTables.Free; 214 end; 215 103 216 end. 104 217
Note:
See TracChangeset
for help on using the changeset viewer.