childwin.pas

来自「MysqlFront的源码」· PAS 代码 · 共 2,085 行 · 第 1/5 页

PAS
2,085
字号
  // initialization: establish connection and read some vars from registry
  Screen.Cursor := crHourGlass;
  mainform.Showstatus('Creating window...', 2, 51);

  // temporarily disable AutoReconnect in Registry
  // in case of unexpected application-termination
  AutoReconnect := false;
  with TRegistry.Create do
  begin
    openkey(regpath, true);
    if Valueexists('Autoreconnect') then
    if ReadBool('AutoReconnect') then
    begin
      AutoReconnect := true;
      WriteBool('AutoReconnect', false);
    end;
    closekey();
  end;

  ReadWindowOptions;

  ZConn.Hostname := connform.EditHost.Text;
  ZConn.User := connform.EditBenutzer.Text;
  ZConn.Password := connform.EditPasswort.Text;
  ZConn.Port := strToIntDef(connform.EditPort.Text, MYSQL_PORT);
  try
    ZConn.Connect;
  except
    on E: Exception do
    begin
      MessageDlg( E.Message, mtError, [mbOK], 0 );
      Screen.Cursor := crDefault;
      timer5.Enabled := true;
      Exit;
    end;
  end;

  Description := connform.ComboBoxDescription.Text;
  Caption := Description;
  OnlyDBs := explode(';', connform.EditOnlyDBs.Text);

  // Versions and Statistics
  LogSQL( ' Connection established with host "' + ZConn.hostname + '" on port ' + inttostr(ZConn.Port));
  LogSQL( ' Server-Version: ' + GetVar( 'SELECT VERSION()' ) );

  ShowVariablesAndProcesses(self);
  ReadDatabasesAndTables(self);

  // re-enable AutoReconnect in Registry!
  if AutoReconnect then
  with TRegistry.Create do begin
    openkey(regpath, true);
    WriteBool('AutoReconnect', true);
    closekey();
  end;

  ActualDatabase := '';
  ActualTable := '';
  Screen.Cursor := crDefault;

end;


procedure TMDIChild.ReadWindowOptions;
var
  ws : String;
  i : Integer;
  menuitem : Tmenuitem;
begin
  with TRegistry.Create do
  begin
    if OpenKey(regpath, true) then
    begin
      ws := ReadString('childwinstate');
      if mainform.MDIChildCount > 1 then
        if mainform.MDIChildren[0].WindowState = wsNormal then
          ws := '';
      if ws = 'Normal' then
      begin
        windowstate := wsNormal;
        if valueexists('childwinleft') then
        begin
          left := ReadInteger('childwinleft');
          top := ReadInteger('childwintop');
          width := ReadInteger('childwinwidth');
          height := ReadInteger('childwinheight');
        end;
      end else
      if ws = 'Minimized'
        then windowstate := wsMinimized else
      if ws = 'Maximized'
        then windowstate := wsMaximized;

      // other values:
      if valueExists('querymemoheight') then
        panel7.Height := ReadInteger('querymemoheight');
      if valueExists('dbtreewidth') then
        dbtree.Width := ReadInteger('dbtreewidth');
      if valueExists('sqloutheight') then
        PageControl3.Height := ReadInteger('sqloutheight');
      if valueExists('DefaultColWidth') then
        Mainform.DefaultColWidth := ReadInteger('DefaultColWidth')
      else
        Mainform.DefaultColWidth := 100;

      // SQL-Font:
      if (ValueExists('FontName')) and (ValueExists('FontSize')) then begin
        SynMemo1.Font.Name := ReadString('FontName');
        SynMemo2.Font.Name := ReadString('FontName');
        SynMemo1.Font.Size := ReadInteger('FontSize');
        SynMemo2.Font.Size := ReadInteger('FontSize');
      end;

      // Data-Font:
      if (ValueExists('DataFontName')) and (ValueExists('DataFontSize')) then begin
        DBGrid1.Font.Name := ReadString('DataFontName');
        DBGrid2.Font.Name := ReadString('DataFontName');
        DBMemo1.Font.Name := ReadString('DataFontName');
        DBGrid1.Font.Size := ReadInteger('DataFontSize');
        DBGrid2.Font.Size := ReadInteger('DataFontSize');
        DBMemo1.Font.Size := ReadInteger('DataFontSize');
      end;

      // color coding:
      if ValueExists('SQLColKeyAttri') then
        SynSQLSyn1.KeyAttri.Foreground := StringToColor(ReadString('SQLColKeyAttri'));
      if ValueExists('SQLColFunctionAttri') then
        SynSQLSyn1.FunctionAttri.Foreground := StringToColor(ReadString('SQLColFunctionAttri'));
      if ValueExists('SQLColDataTypeAttri') then
        SynSQLSyn1.DataTypeAttri.Foreground := StringToColor(ReadString('SQLColDataTypeAttri'));
      if ValueExists('SQLColNumberAttri') then
        SynSQLSyn1.NumberAttri.Foreground := StringToColor(ReadString('SQLColNumberAttri'));
      if ValueExists('SQLColStringAttri') then
        SynSQLSyn1.StringAttri.Foreground := StringToColor(ReadString('SQLColStringAttri'));
      if ValueExists('SQLColCommentAttri') then
        SynSQLSyn1.CommentAttri.Foreground := StringToColor(ReadString('SQLColCommentAttri'));

      // SQL-Filter-Files-History
      i := 1;
      PopUpMenu7.Items.Clear;
      while ValueExists('SQLWhereFile'+inttostr(i)) do begin
        menuitem := Tmenuitem.Create(self);
        menuitem.Caption := inttostr(PopUpMenu7.Items.count+1) + ' ' + ReadString('SQLWhereFile'+inttostr(i));
        menuitem.OnClick := LoadSQLWhereFile;
        PopUpMenu7.Items.Add(menuitem);
        inc(i);
      end;

    end;
    CloseKey;
  end;
end;


procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
var
  ws : String;
begin
  // closing connection and saving some vars into registry
  if windowstate = wsNormal then
    ws := 'Normal' else
  if windowstate = wsMinimized
    then ws := 'Minimized' else
  if windowstate = wsMaximized
    then ws := 'Maximized';

  with TRegistry.Create do
  begin
    if OpenKey(regpath, true) then
    begin
      WriteString('childwinstate', ws);
      WriteInteger('childwinleft', left);
      WriteInteger('childwintop', top);
      WriteInteger('childwinwidth', width);
      WriteInteger('childwinheight', height);

      WriteInteger('querymemoheight', panel7.Height);
      WriteInteger('dbtreewidth', dbtree.width);
      WriteInteger('sqloutheight', PageControl3.Height);
    end;
  end;
  mainform.ToolBarData.visible := false;
  FormDeactivate(sender);
  Action := caFree;
end;


procedure TMDIChild.showstatus(msg: string='';  panel : Integer=0;  Icon: Integer=50);
begin
  if mainform.ActiveMDIChild <> self then
    exit;
  mainform.showstatus(msg, panel, Icon);
end;


procedure TMDIChild.LogSQL(msg: string = '');
begin
  // add a sql-command or info-line to history-memo
  if SynMemo2.Lines.Count > mainform.logsqlnum then
    SynMemo2.Lines.Clear;
  msg := Copy(msg, 0, 2000);
  msg := StringReplace( msg, #9, ' ', [rfReplaceAll] );
  msg := StringReplace( msg, #10, ' ', [rfReplaceAll] );
  msg := StringReplace( msg, #13, ' ', [rfReplaceAll] );
  msg := StringReplace( msg, '  ', ' ', [rfReplaceAll] );
  SynMemo2.Lines.Add(msg);
  SynMemo2.SetBookMark(0,0,SynMemo2.Lines.Count);
  SynMemo2.GotoBookMark(0);
  SynMemo2.ClearBookMark(0);
  //Application.ProcessMessages;
end;


procedure TMDIChild.ReadDatabasesAndTables(Sender: TObject);
var
  tnode, tchild, tmpSelected: TTreeNode;
  i, j : Integer;
begin
  // Fill DBTree
  Screen.Cursor := crHourGlass;
  dataselected := false;
  DBTree.OnChange := nil;
  DBTree.items.Clear;

  tnodehost := DBtree.Items.Add(nil, ZConn.User + '@' + ZConn.Password);  // Host or Root
  tnodehost.ImageIndex := 13;
  tnodehost.SelectedIndex := 6;

  Screen.Cursor := crSQLWait;
  mainform.Showstatus('Reading Databases...', 2, 51);
  if OnlyDBs.Count = 0 then
  begin
    OnlyDBs2 := TStringList.Create;
    ZQuery3.SQL.Clear();
    ZQuery3.SQL.Add('SHOW DATABASES');
    ZQuery3.Open;
    ZQuery3.First;
    for i:=1 to ZQuery3.RecordCount do begin
      OnlyDBs2.Add(ZQuery3.FieldByName('Database').AsString);
      ZQuery3.Next;
    end;
    zconn.Database := ZQuery3.FieldByName('Database').AsString;
  end else
    OnlyDBs2 := OnlyDBs;
  if OnlyDBs2.Count > 50 then with SelectFromManyDatabases do begin
    CheckListBoxDBs.Items.Clear;
    CheckListBoxDBs.Items := OnlyDBs2;
    ShowModal;
  end;

  // List Databases and Tables-Names
  for i:=0 to OnlyDBs2.Count-1 do
  try
    ZQuery3.Close;
    ZQuery3.SQL.Clear;
    ZQuery3.SQL.Add( 'SHOW TABLES FROM ' + OnlyDBs2[i] );
    ZQuery3.Open;
    ZQuery3.First;
    tnode := DBtree.Items.AddChild(tnodehost, OnlyDBs2[i]);
    tnode.ImageIndex := 12;
    tnode.SelectedIndex := 0;
    if ActualDatabase = OnlyDBs2[i] then
      tmpSelected := tnode;
    for j:=1 to ZQuery3.RecordCount do
    begin
      tchild := DBtree.Items.AddChild( tnode, ZQuery3.Fields[0].AsString );
      tchild.ImageIndex := 1;
      tchild.SelectedIndex := 11;
      if (ActualTable = ZQuery3.Fields[0].AsString) and (tmpSelected.Text = OnlyDBs2[i]) then
        tmpSelected := tchild;
      ZQuery3.Next;
    end;
  except
    LogSQL('# Could not open database ''' + OnlyDBs2[i] + ''' - ignoring.');
    continue;
  end;

  showstatus(inttostr(OnlyDBs2.count) + ' Databases');
  tnodehost.Expand(false);
  DBTree.OnChange := DBtreeChange;
  if tmpSelected <> nil then
    DBTree.Selected := tmpSelected
  else
    DBTree.Selected := tnodehost;
  DBtreeChange(self, tnodehost);
  mainform.Showstatus('Ready', 2);
  Screen.Cursor := crDefault;
end;



procedure TMDIChild.DBtreeChange(Sender: TObject; Node: TTreeNode);
var
  strdb, strtable : String;
begin
  // react on dbtree-clicks
  Screen.Cursor := crHourGlass;
  case Node.Level of
    0 : begin                                   // Root / Host chosen
      if not DBTree.Dragging then
        PageControl1.ActivePage := SheetHost;
      SheetDatabase.TabVisible := false;
      SheetTable.TabVisible := false;
      SheetData.TabVisible := false;
      ActualDatabase := '';
      ActualTable := '';
      Caption := Description;
    end;
    1 : begin                                   // DB chosen
      SheetDatabase.TabVisible := true;
      SheetTable.TabVisible := false;
      SheetData.TabVisible := false;
      if not DBTree.Dragging then
        PageControl1.ActivePage := SheetDatabase;
      strdb := Node.Text;
      showstatus(strdb +': ' + inttostr(Node.count) +' table(s)');
      if ActualDatabase <> strdb then
      begin
        ActualDatabase := strdb;
        ActualTable := '';
        ToolButton9.Enabled := False;
        FeldListe.Items.Clear;
        Panel3.Caption := 'Table-Properties';
        ShowDBProperties(self);
      end;
      Caption := Description + ' - /' + ActualDatabase;
    end;
    2 : begin                                   // Table chosen
      SheetDatabase.TabVisible := true;
      SheetTable.TabVisible := true;
      SheetData.TabVisible := true;
      strdb := Node.Parent.Text;
      strtable := Node.Text;
      if (ActualDatabase <> strdb) or (ActualTable <> strtable) then
        dataselected := false;
      if ActualDatabase <> strdb then begin
        ActualDatabase := strdb;
        ShowDBProperties(self);
      end;
      if (ActualTable <> strtable) or (ActualDatabase <> strdb) then begin
        ActualTable := strtable;
        ShowTableProperties(self);
      end;
      showstatus(strdb + ': '+strtable +': ' + inttostr(FeldListe.Items.count) +' field(s)');
//      if not dataselected then pcChange(self);
      Caption := Description + ' - /' + ActualDatabase + '/' + ActualTable;
    end;
  end;

  pcChange(self);
  Screen.Cursor := crDefault;
end;


procedure TMDIChild.viewdata(Sender: TObject);
var
  sorting               : String;
  DropDown              : TStringList;
  i, j                  : Integer;
  Columns,
  PrimaryKeyColumns     : TStringList;
begin
  // view table-data with zeos
  if viewingdata then
    abort;
  viewingdata := true;

  // rowcount:
  ZQuery3.SQL.Clear;
  ZQuery3.SQL.Add( 'SELECT COUNT(*) FROM ' + mainform.mask(ActualTable) );
  Try
    ZQuery3.Open;
    ZQuery3.First;
    rowcount := ZQuery3.Fields[0].AsInteger;
  except
    rowcount := 0;
  end;

  // set db-aware-component's properties...
  DBMemo1.DataField := '';
  DBMemo1.DataSource := DataSource1;
  ZDBImage1.DataField := '';
  ZDBImage1.DataSource := DataSource1;

  if not dataselected then
    dbgrid1.SortColumns.Clear
  else
  begin
    sorting := '';
    for i:=0 to dbgrid1.SortColumns.Count-1 do
    begin
      with dbgrid1.SortColumns[i] do begin
        if SortType <> stNone then begin
          if sorting <> '' then
            sorting := sorting + ', ';
          sorting := sorting + FieldName;
        end;
        if SortType = stAscending then
          sorting := sorting + ' DESC'
        else if SortType = stDescending then
          sorting := sorting + ' ASC';
      end;
    end;
    if sorting <> '' then
      sorting := 'ORDER BY ' + sorting;
  end;
  MenuLimit.Checked := Mainform.CheckBoxLimit.Checked;
  Columns := TStringList.Create;
  PrimaryKeyColumns := TStringList.Create;

  if (ActualTable <> '') and (ActualDatabase <> '') then
  begin
    SheetTable.TabVisible := true;
    SheetData.TabVisible := true;
    if Mainform.DataAlwaysEditMode then
      DBGrid1.Options := DBGrid1.Options + [dgAlwaysShowEditor]
    else

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?