1 | unit UFormImportStructure;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | EditBtn, USystem, SpecializedList, USqlDatabase, SpecializedDictionary;
|
---|
10 |
|
---|
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;
|
---|
22 |
|
---|
23 | { TImportStructureForm }
|
---|
24 |
|
---|
25 | TImportStructureForm = class(TForm)
|
---|
26 | Button1: TButton;
|
---|
27 | EditSchema: TEdit;
|
---|
28 | EditHost: TEdit;
|
---|
29 | EditUser: TEdit;
|
---|
30 | EditPassword: TEdit;
|
---|
31 | Label1: TLabel;
|
---|
32 | Label2: TLabel;
|
---|
33 | Label3: TLabel;
|
---|
34 | Label4: TLabel;
|
---|
35 | Memo1: TMemo;
|
---|
36 | procedure Button1Click(Sender: TObject);
|
---|
37 | procedure FormCreate(Sender: TObject);
|
---|
38 | procedure FormDestroy(Sender: TObject);
|
---|
39 | procedure FormShow(Sender: TObject);
|
---|
40 | private
|
---|
41 | function ObjectIdByName(Name: string): Integer;
|
---|
42 | { private declarations }
|
---|
43 | public
|
---|
44 | SQLTables: TListObject; // TListObject<TSQLTable>
|
---|
45 | end;
|
---|
46 |
|
---|
47 | var
|
---|
48 | ImportStructureForm: TImportStructureForm;
|
---|
49 |
|
---|
50 | implementation
|
---|
51 |
|
---|
52 | uses
|
---|
53 | UCore, UFormMain, UFormMenu;
|
---|
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;
|
---|
79 |
|
---|
80 | {$R *.lfm}
|
---|
81 |
|
---|
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;
|
---|
93 |
|
---|
94 | procedure TImportStructureForm.FormShow(Sender: TObject);
|
---|
95 | begin
|
---|
96 | EditHost.Text := Core.System.Client.Host;
|
---|
97 | EditUser.Text := Core.System.Client.User;
|
---|
98 | EditSchema.Text := Core.System.Client.Schema;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TImportStructureForm.Button1Click(Sender: TObject);
|
---|
102 | var
|
---|
103 | Database: TSqlDatabase;
|
---|
104 | DbRows: TDbRows;
|
---|
105 | DbRows2: TDbRows;
|
---|
106 | T, C: Integer;
|
---|
107 | TableName: string;
|
---|
108 | PropertyName: string;
|
---|
109 | PropType: string;
|
---|
110 | GroupId: Integer;
|
---|
111 | ObjectId: Integer;
|
---|
112 | PropertyId: Integer;
|
---|
113 | NewTable: TSQLTable;
|
---|
114 | RefObjectIndex: Integer;
|
---|
115 | RefPropertyId: Integer;
|
---|
116 | begin
|
---|
117 | try
|
---|
118 | Database := TSqlDatabase.Create(nil);
|
---|
119 | DbRows := TDbRows.Create;
|
---|
120 | DbRows2 := TDbRows.Create;
|
---|
121 | Database.HostName := EditHost.Text;
|
---|
122 | Database.Database := EditSchema.Text;
|
---|
123 | Database.UserName := EditUser.Text;
|
---|
124 | Database.Password := EditPassword.Text;
|
---|
125 | Database.Connect;
|
---|
126 |
|
---|
127 | SQLTables.Clear;
|
---|
128 | GroupId := Core.System.AddMenu(Database.Database, 0);
|
---|
129 | Database.Query(DbRows, 'SHOW TABLES');
|
---|
130 | for T := 0 to DbRows.Count - 1 do begin
|
---|
131 | NewTable := TSQLTable(SQLTables.AddNew(TSQLTable.Create));
|
---|
132 | NewTable.Table.Assign(DbRows[T]);
|
---|
133 | TableName := DbRows[T].Items[0].Value;
|
---|
134 | Memo1.Lines.Add('Create object "' + TableName + '"');
|
---|
135 | NewTable.Table.Add('ObjId', IntToStr(Core.System.AddObject(TableName, TableName, Database.Database)));
|
---|
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 | RefObjectIndex := ObjectIdByName(Columns[C].Values['REFERENCED_TABLE_NAME']);
|
---|
163 | PropertyId := Core.System.AddPropertyRelationOne(ObjectId, PropertyName, PropertyName,
|
---|
164 | True, StrToInt(TSQLTable(SQLTables[RefObjectIndex]).Table.Values['ObjId']));
|
---|
165 | end else
|
---|
166 | if PropType = 'int' then
|
---|
167 | PropertyId := Core.System.AddPropertyNumber(ObjectId, PropertyName, PropertyName, True);
|
---|
168 | if PropType = 'varchar' then
|
---|
169 | PropertyId := Core.System.AddPropertyString(ObjectId, PropertyName, PropertyName, True);
|
---|
170 | if PropType = 'float' then
|
---|
171 | PropertyId := Core.System.AddPropertyFloat(ObjectId, PropertyName, PropertyName, True);
|
---|
172 | if PropType = 'datetime' then
|
---|
173 | PropertyId := Core.System.AddPropertyDateTime(ObjectId, PropertyName, PropertyName, True);
|
---|
174 | Columns[C].Add('Id', IntToStr(PropertyId));
|
---|
175 | Memo1.Lines.Add('Create property "' + PropertyName + '" of type ' + PropType + '"');
|
---|
176 | //Memo1.Lines.Add(Columns[C].Values['CONSTRAINT_TYPE']);
|
---|
177 | //TSQLTable(SQLTables[T]).Columns.Add();
|
---|
178 | end;
|
---|
179 | end;
|
---|
180 |
|
---|
181 | for T := 0 to SQLTables.Count - 1 do
|
---|
182 | with TSQLTable(SQLTables[T]) do begin
|
---|
183 | TableName := Table.Items[0].Value;
|
---|
184 | ObjectId := StrToInt(Table.Values['ObjId']);
|
---|
185 | for C := 0 to Columns.Count - 1 do begin
|
---|
186 | PropertyName := Columns[C].Values['COLUMN_NAME'];
|
---|
187 | PropType := Columns[C].Values['DATA_TYPE'];
|
---|
188 | if Columns[C].Values['CONSTRAINT_TYPE'] = 'FOREIGN KEY' then begin
|
---|
189 | Memo1.Lines.Add('Add relation 1:n for "' + TableName + '.' + PropertyName + '"');
|
---|
190 | RefObjectIndex := ObjectIdByName(Columns[C].Values['REFERENCED_TABLE_NAME']);
|
---|
191 |
|
---|
192 | RefPropertyId := TSQLTable(SQLTables[T]).ColumnByName(Columns[C].Values['COLUMN_NAME']);
|
---|
193 | RefPropertyId := StrToInt(TSQLTable(SQLTables[T]).Columns[RefPropertyId].Values['Id']);
|
---|
194 | PropertyId := Core.System.AddPropertyRelationMany(StrToInt(TSQLTable(SQLTables[RefObjectIndex]).Table.Values['ObjId']), TableName, TableName,
|
---|
195 | True, RefPropertyId);
|
---|
196 | end;
|
---|
197 | end;
|
---|
198 | end;
|
---|
199 | finally
|
---|
200 | DbRows2.Free;
|
---|
201 | DbRows.Free;
|
---|
202 | Database.Free;
|
---|
203 | FormMenu.LoadTree;
|
---|
204 | end;
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TImportStructureForm.FormCreate(Sender: TObject);
|
---|
208 | begin
|
---|
209 | SQLTables := TListObject.Create;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TImportStructureForm.FormDestroy(Sender: TObject);
|
---|
213 | begin
|
---|
214 | SQLTables.Free;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | end.
|
---|
218 |
|
---|