Changeset 20 for trunk/Forms


Ignore:
Timestamp:
Jun 15, 2011, 11:11:59 AM (13 years ago)
Author:
george
Message:
  • Added: Import relation one and many from existed database.
Location:
trunk/Forms
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UImportStructureForm.lfm

    r19 r20  
    11object ImportStructureForm: TImportStructureForm
    2   Left = 320
    3   Height = 233
    4   Top = 144
    5   Width = 528
     2  Left = 321
     3  Height = 465
     4  Top = 145
     5  Width = 553
    66  Caption = 'Import structure'
    7   ClientHeight = 233
    8   ClientWidth = 528
     7  ClientHeight = 465
     8  ClientWidth = 553
     9  OnCreate = FormCreate
     10  OnDestroy = FormDestroy
    911  OnShow = FormShow
    1012  LCLVersion = '0.9.31'
     
    7072  end
    7173  object Button1: TButton
    72     Left = 448
     74    Left = 473
    7375    Height = 25
    74     Top = 201
     76    Top = 433
    7577    Width = 75
    7678    Anchors = [akRight, akBottom]
     
    8183  object Memo1: TMemo
    8284    Left = 184
    83     Height = 180
     85    Height = 412
    8486    Top = 8
    85     Width = 335
     87    Width = 360
    8688    Anchors = [akTop, akLeft, akRight, akBottom]
    8789    ScrollBars = ssAutoBoth
  • 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
  • trunk/Forms/UItemAdd.lfm

    r12 r20  
    11object ItemAddForm: TItemAddForm
    2   Left = 400
    3   Height = 421
    4   Top = 130
    5   Width = 567
     2  Left = 401
     3  Height = 419
     4  Top = 131
     5  Width = 565
    66  ActiveControl = Panel1
    77  Caption = 'Add item'
    8   ClientHeight = 421
    9   ClientWidth = 567
     8  ClientHeight = 419
     9  ClientWidth = 565
    1010  OnClose = FormClose
    1111  OnCreate = FormCreate
     
    1515  object Panel1: TPanel
    1616    Left = 0
    17     Height = 384
     17    Height = 382
    1818    Top = 0
    19     Width = 567
     19    Width = 565
    2020    Align = alTop
    2121    Anchors = [akLeft, akRight, akBottom]
     
    2424  end
    2525  object ButtonCancel: TButton
    26     Left = 384
     26    Left = 382
    2727    Height = 25
    28     Top = 392
     28    Top = 390
    2929    Width = 75
    3030    Anchors = [akRight, akBottom]
     
    3434  end
    3535  object ButtonSave: TButton
    36     Left = 472
     36    Left = 470
    3737    Height = 25
    38     Top = 392
     38    Top = 390
    3939    Width = 75
    4040    Anchors = [akRight, akBottom]
  • trunk/Forms/UItemEdit.lfm

    r12 r20  
    11object ItemEditForm: TItemEditForm
    2   Left = 295
    3   Height = 429
    4   Top = 122
    5   Width = 558
     2  Left = 296
     3  Height = 427
     4  Top = 123
     5  Width = 556
    66  ActiveControl = Panel1
    77  Caption = 'Edit item'
    8   ClientHeight = 429
    9   ClientWidth = 558
     8  ClientHeight = 427
     9  ClientWidth = 556
    1010  OnClose = FormClose
    1111  OnCreate = FormCreate
     
    1515  object Panel1: TPanel
    1616    Left = 0
    17     Height = 386
     17    Height = 384
    1818    Top = 0
    19     Width = 558
     19    Width = 556
    2020    Align = alTop
    2121    Anchors = [akLeft, akRight, akBottom]
     
    2424  end
    2525  object ButtonCancel: TButton
    26     Left = 384
     26    Left = 382
    2727    Height = 25
    28     Top = 400
     28    Top = 398
    2929    Width = 75
    3030    Anchors = [akRight, akBottom]
     
    3434  end
    3535  object ButtonSave: TButton
    36     Left = 472
     36    Left = 470
    3737    Height = 25
    38     Top = 400
     38    Top = 398
    3939    Width = 75
    4040    Anchors = [akRight, akBottom]
  • trunk/Forms/UItemView.lfm

    r17 r20  
    11object ItemViewForm: TItemViewForm
    2   Left = 311
    3   Height = 469
    4   Top = 110
    5   Width = 626
     2  Left = 312
     3  Height = 467
     4  Top = 111
     5  Width = 624
    66  Caption = 'View item'
    7   ClientHeight = 469
    8   ClientWidth = 626
     7  ClientHeight = 467
     8  ClientWidth = 624
    99  OnClose = FormClose
    1010  OnCreate = FormCreate
     
    1414  LCLVersion = '0.9.31'
    1515  object ButtonClose: TButton
    16     Left = 546
     16    Left = 544
    1717    Height = 25
    18     Top = 439
     18    Top = 437
    1919    Width = 75
    2020    Anchors = [akRight, akBottom]
     
    2424  end
    2525  object ButtonEdit: TButton
    26     Left = 458
     26    Left = 456
    2727    Height = 25
    28     Top = 439
     28    Top = 437
    2929    Width = 75
    3030    Anchors = [akRight, akBottom]
     
    3535  object Panel1: TPanel
    3636    Left = 0
    37     Height = 433
     37    Height = 431
    3838    Top = 0
    39     Width = 626
     39    Width = 624
    4040    Align = alTop
    4141    Anchors = [akTop, akLeft, akRight, akBottom]
    4242    BevelOuter = bvNone
    43     ClientHeight = 433
    44     ClientWidth = 626
     43    ClientHeight = 431
     44    ClientWidth = 624
    4545    TabOrder = 2
    4646    object Panel2: TPanel
    4747      Left = 0
    4848      Height = 200
    49       Top = 233
    50       Width = 626
     49      Top = 231
     50      Width = 624
    5151      Align = alBottom
    5252      BevelOuter = bvNone
    5353      ClientHeight = 200
    54       ClientWidth = 626
     54      ClientWidth = 624
    5555      TabOrder = 0
    5656      object TabControl1: TTabControl
     
    5858        Height = 24
    5959        Top = 0
    60         Width = 626
     60        Width = 624
    6161        Align = alTop
    6262        OnChange = TabControl1Change
     
    7474        Height = 176
    7575        Top = 24
    76         Width = 626
     76        Width = 624
    7777        Align = alClient
    7878        Columns = <>
     
    8989      Left = 0
    9090      Height = 3
    91       Top = 230
    92       Width = 626
     91      Top = 228
     92      Width = 624
    9393      Align = alBottom
    9494      ResizeAnchor = akBottom
     
    9696    object PanelControls: TPanel
    9797      Left = 0
    98       Height = 230
     98      Height = 228
    9999      Top = 0
    100       Width = 626
     100      Width = 624
    101101      Align = alClient
    102102      Anchors = [akLeft, akRight, akBottom]
  • trunk/Forms/ULoginForm.lfm

    r10 r20  
    11object LoginForm: TLoginForm
    2   Left = 479
    3   Height = 177
    4   Top = 255
    5   Width = 405
     2  Left = 480
     3  Height = 175
     4  Top = 256
     5  Width = 403
    66  BorderIcons = [biSystemMenu]
    77  BorderStyle = bsDialog
    88  Caption = 'Login'
    9   ClientHeight = 177
    10   ClientWidth = 405
     9  ClientHeight = 175
     10  ClientWidth = 403
    1111  LCLVersion = '0.9.31'
    1212  object Label1: TLabel
  • trunk/Forms/UMainForm.lfm

    r19 r20  
    11object MainForm: TMainForm
    2   Left = 268
    3   Height = 451
    4   Top = 156
    5   Width = 646
     2  Left = 269
     3  Height = 450
     4  Top = 157
     5  Width = 644
    66  ActiveControl = Panel1
    77  Caption = 'ChronIS'
    8   ClientHeight = 432
    9   ClientWidth = 646
     8  ClientHeight = 430
     9  ClientWidth = 644
    1010  Menu = MainMenu1
    1111  OnClose = FormClose
     
    1616  object Panel1: TPanel
    1717    Left = 0
    18     Height = 432
     18    Height = 430
    1919    Top = 0
    2020    Width = 184
    2121    Align = alLeft
    2222    BevelOuter = bvNone
    23     ClientHeight = 432
     23    ClientHeight = 430
    2424    ClientWidth = 184
    2525    TabOrder = 0
     
    3434    object TreeView1: TTreeView
    3535      Left = 4
    36       Height = 408
     36      Height = 406
    3737      Top = 19
    3838      Width = 180
     
    4848  object Panel2: TPanel
    4949    Left = 189
    50     Height = 432
     50    Height = 430
    5151    Top = 0
    52     Width = 457
     52    Width = 455
    5353    Align = alClient
    5454    BevelOuter = bvNone
    55     ClientHeight = 432
    56     ClientWidth = 457
     55    ClientHeight = 430
     56    ClientWidth = 455
    5757    TabOrder = 1
    5858    object Label2: TLabel
     
    6666    object ListView1: TListView
    6767      Left = 2
    68       Height = 376
     68      Height = 374
    6969      Top = 19
    70       Width = 453
     70      Width = 451
    7171      Anchors = [akTop, akLeft, akRight, akBottom]
    7272      Columns = <>
     
    8686      Left = 3
    8787      Height = 25
    88       Top = 403
     88      Top = 401
    8989      Width = 75
    9090      Action = AItemAdd
     
    9595      Left = 83
    9696      Height = 25
    97       Top = 403
     97      Top = 401
    9898      Width = 75
    9999      Action = AItemDelete
     
    104104  object Splitter1: TSplitter
    105105    Left = 184
    106     Height = 432
     106    Height = 430
    107107    Top = 0
    108108    Width = 5
  • trunk/Forms/UMainForm.pas

    r19 r20  
    361361    end;
    362362
     363    if Tables.IndexOf(TypeDateTime) = -1 then begin
     364      Database.Query(DbRows, 'CREATE TABLE IF NOT EXISTS `' + TypeDateTime + '` ( ' +
     365        '`Id` int(11) NOT NULL AUTO_INCREMENT,' +
     366        '`CustomType` int NOT NULL,' +
     367        '`Default` datetime NOT NULL,' +
     368        '`Min` datetime NOT NULL,' +
     369        '`Max` datetime NOT NULL,' +
     370        'KEY `CustomType` (`CustomType`),' +
     371        'PRIMARY KEY (`Id`)' +
     372        ') ENGINE=InnoDB  DEFAULT CHARSET=utf8');
     373    end;
     374
    363375    if Tables.IndexOf(TypeString) = -1 then begin
    364376      Database.Query(DbRows, 'CREATE TABLE IF NOT EXISTS `' + TypeString + '` ( ' +
  • trunk/Forms/USettingForm.lfm

    r19 r20  
    11object SettingForm: TSettingForm
    2   Left = 371
    3   Height = 328
    4   Top = 138
    5   Width = 446
     2  Left = 372
     3  Height = 326
     4  Top = 139
     5  Width = 444
    66  Caption = 'Settings'
    7   ClientHeight = 328
    8   ClientWidth = 446
     7  ClientHeight = 326
     8  ClientWidth = 444
    99  OnClose = FormClose
    1010  OnShow = FormShow
     
    2626  end
    2727  object ButtonSave: TButton
    28     Left = 368
     28    Left = 366
    2929    Height = 25
    30     Top = 296
     30    Top = 294
    3131    Width = 75
    3232    Anchors = [akRight, akBottom]
     
    3636  end
    3737  object ButtonCancel: TButton
    38     Left = 280
     38    Left = 278
    3939    Height = 25
    40     Top = 296
     40    Top = 294
    4141    Width = 75
    4242    Anchors = [akRight, akBottom]
Note: See TracChangeset for help on using the changeset viewer.