Ignore:
Timestamp:
Jun 15, 2011, 11:11:59 AM (13 years ago)
Author:
george
Message:
  • Added: Import relation one and many from existed database.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UImportStructureForm.pas

    r19 r20  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   USystem;
     9  EditBtn, USystem, SpecializedList, USqlDatabase, SpecializedDictionary;
    1010
    1111type
     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;
    1222
    1323  { TImportStructureForm }
     
    2535    Memo1: TMemo;
    2636    procedure Button1Click(Sender: TObject);
     37    procedure FormCreate(Sender: TObject);
     38    procedure FormDestroy(Sender: TObject);
    2739    procedure FormShow(Sender: TObject);
    2840  private
     41    function ObjectIdByName(Name: string): Integer;
    2942    { private declarations }
    3043  public
    31     { public declarations }
     44    SQLTables: TListObject; // TListObject<TSQLTable>
    3245  end;
    3346
     
    3851
    3952uses
    40   UCore, USqlDatabase, UMainForm;
     53  UCore, UMainForm;
     54
     55{ TSQLTable }
     56
     57function TSQLTable.ColumnByName(Name: string): Integer;
     58var
     59  I: Integer;
     60begin
     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;
     65end;
     66
     67constructor TSQLTable.Create;
     68begin
     69  Columns := TDbRows.Create;
     70  Table := TDictionaryStringString.Create;
     71end;
     72
     73destructor TSQLTable.Destroy;
     74begin
     75  Columns.Free;
     76  Table.Free;
     77  inherited Destroy;
     78end;
    4179
    4280{$R *.lfm}
    4381
    4482{ TImportStructureForm }
     83
     84function TImportStructureForm.ObjectIdByName(Name: string): Integer;
     85var
     86  I: Integer;
     87begin
     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;
     92end;
    4593
    4694procedure TImportStructureForm.FormShow(Sender: TObject);
     
    63111  ObjectId: Integer;
    64112  PropertyId: Integer;
     113  NewTable: TSQLTable;
     114  RefObjectIndex: Integer;
     115  RefPropertyId: Integer;
    65116begin
    66117  try
     
    73124    Database.Password := EditPassword.Text;
    74125    Database.Connect;
     126
     127    SQLTables.Clear;
    75128    GroupId := Core.System.AddGroup(Database.Database, 0);
    76129    Database.Query(DbRows, 'SHOW TABLES');
    77130    for T := 0 to DbRows.Count - 1 do begin
     131      NewTable := TSQLTable(SQLTables.AddNew(TSQLTable.Create));
     132      NewTable.Table.Assign(DbRows[T]);
    78133      TableName := DbRows[T].Items[0].Value;
    79134      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
    86165        if PropType = 'int' then
    87166          PropertyId := Core.System.AddPropertyNumber(ObjectId, PropertyName, PropertyName);
     
    90169        if PropType = 'float' then
    91170          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));
    92174        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;
    93196      end;
    94197    end;
     
    101204end;
    102205
     206procedure TImportStructureForm.FormCreate(Sender: TObject);
     207begin
     208  SQLTables := TListObject.Create;
     209end;
     210
     211procedure TImportStructureForm.FormDestroy(Sender: TObject);
     212begin
     213  SQLTables.Free;
     214end;
     215
    103216end.
    104217
Note: See TracChangeset for help on using the changeset viewer.