📄 exportsql.pas
字号:
unit exportsql;
// -------------------------------------
// HeidiSQL
// Export Tables
// -------------------------------------
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, CheckLst, Buttons, comctrls, Registry, ToolWin;
type
TExportSQLForm = class(TForm)
Bevel1: TBevel;
Button1: TButton;
Button2: TButton;
TablesCheckListBox: TCheckListBox;
Label1: TLabel;
DBComboBox: TComboBox;
GroupBox1: TGroupBox;
SaveDialog1: TSaveDialog;
BitBtn1: TBitBtn;
EditFileName: TEdit;
RadioButtonDB: TRadioButton;
RadioButtonFile: TRadioButton;
ComboBoxODB: TComboBox;
ProgressBar1: TProgressBar;
GroupBox2: TGroupBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
RadioButtonHost: TRadioButton;
ComboBoxHost: TComboBox;
Label2: TLabel;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
GroupBox3: TGroupBox;
CheckBoxWithUseDB: TCheckBox;
CheckBoxWithDropTable: TCheckBox;
CheckBoxCompleteInserts: TCheckBox;
CheckBoxUseBackticks: TCheckBox;
procedure Button2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure DBComboBoxChange(Sender: TObject);
procedure CheckListToggle(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure RadioButtonDBClick(Sender: TObject);
procedure RadioButtonFileClick(Sender: TObject);
procedure fillcombo_anotherdb(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure RadioButtonHostClick(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ExportSQLForm: TExportSQLForm;
const
CRLF = #13#10;
implementation
uses Main, Childwin, helpers;
{$R *.DFM}
procedure TExportSQLForm.Button2Click(Sender: TObject);
begin
close;
end;
procedure TExportSQLForm.FormShow(Sender: TObject);
var
tn : TTreeNode;
i,j : Integer;
begin
ProgressBar1.Position := 0;
Label2.Caption := '';
// read dbs and Tables from treeview
DBComboBox.Items.Clear;
with TMDIChild(Mainform.ActiveMDIChild) do begin
self.Caption := ZConn.HostName + ' - Export Tables...';
for i:=0 to DBTree.Items.Count-1 do begin
tn := DBTree.Items[i];
if tn.Level = 1 then
DBComboBox.Items.Add(tn.Text);
end;
for i:=0 to DBComboBox.Items.Count-1 do
if DBComboBox.Items[i] = ActualDatabase then
DBComboBox.ItemIndex := i;
if DBComboBox.ItemIndex = -1 then
DBComboBox.ItemIndex := 0;
end;
DBComboBoxChange(self);
// save filename
with TRegistry.Create do
if OpenKey(regpath, true) then begin
if Valueexists('exportfilename') then EditFileName.Text := ReadString('exportfilename');
if Valueexists('CompleteInserts') then CheckBoxCompleteInserts.Checked := ReadBool('CompleteInserts');
if Valueexists('WithDropTable') then CheckBoxWithDropTable.Checked := ReadBool('WithDropTable');
if Valueexists('WithUseDB') then CheckBoxWithUseDB.Checked := ReadBool('WithUseDB');
if Valueexists('ExportStructure') then CheckBox1.Checked := ReadBool('ExportStructure');
if Valueexists('ExportData') then CheckBox2.Checked := ReadBool('ExportData');
if Valueexists('UseBackticks') then CheckBoxUseBackticks.Checked := ReadBool('UseBackticks');
end;
CheckBox1Click(self);
CheckBox2Click(self);
if EditFileName.Text = '' then
EditFileName.Text := ExtractFilePath(paramstr(0)) + 'export.sql';
// Another Host / DB
ComboBoxHost.Items.Clear;
for i:=0 to MainForm.MDIChildCount-1 do begin
if MainForm.MDIChildren[i] <> MainForm.ActiveMDIChild then
with TMDIChild(MainForm.MDIChildren[i]) do begin
for j:=0 to tnodehost.Count-1 do
self.ComboBoxHost.Items.Add(ZConn.HostName + ':' + tnodehost.Item[j].text);
end;
end;
if ComboBoxHost.Items.Count > 0 then
ComboBoxHost.ItemIndex := 0;
RadioButtonFile.OnClick(self);
end;
procedure TExportSQLForm.DBComboBoxChange(Sender: TObject);
var
tn, child : TTreeNode;
i,j : Integer;
begin
// read tables from db
TablesCheckListBox.Items.Clear;
with TMDIChild(Application.Mainform.ActiveMDIChild) do
begin
for i:=0 to DBTree.Items.Count-1 do
begin
tn := DBTree.Items[i];
if tn.Text = DBComboBox.Text then
begin
child := tn.getFirstChild;
for j:=0 to tn.Count-1 do
begin
TablesCheckListBox.Items.Add(child.Text);
child := tn.getNextChild(child);
end;
end;
end;
end;
// select all:
for i:=0 to TablesCheckListBox.Items.Count-1 do
TablesCheckListBox.checked[i] := true;
// write items for "Another Databases":
fillcombo_anotherdb(self);
end;
procedure TExportSQLForm.CheckListToggle(Sender: TObject);
begin
// check all or none
ToggleCheckListBox(TablesCheckListBox, ((Sender as TControl).Tag = 1));
end;
procedure TExportSQLForm.BitBtn1Click(Sender: TObject);
begin
SaveDialog1.Filename := DBComboBox.Text;
if SaveDialog1.Execute then
if SaveDialog1.Filename <> '' then
EditFileName.Text := SaveDialog1.Filename;
end;
procedure TExportSQLForm.Button1Click(Sender: TObject);
function mask (str: String): String;
begin
if CheckBoxUseBackticks.Checked then
result := '`'+str+'`'
else
result := str;
end;
var
f : TFileStream;
i,j,k,fieldcount,m : Integer;
exportdata,exportstruc : boolean;
dropquery,createquery,insertquery,
feldnamen : String;
keylist : Array of TMyKey;
keystr,DB2export : String;
which : Integer;
tofile : boolean;
tcount,tablecounter : Integer;
HostDb : TStringList;
win2export : TMDIChild;
StrProgress : String;
value : String;
Escaped,fullvalue : PChar;
begin
// export!
with TRegistry.Create do
begin
OpenKey(regpath, true);
WriteString('exportfilename', EditFileName.Text);
WriteBool('CompleteInserts', CheckBoxCompleteInserts.Checked);
WriteBool('WithDropTable', CheckBoxWithDropTable.Checked);
WriteBool('WithUseDB', CheckBoxWithUseDB.Checked);
WriteBool('ExportStructure', CheckBox1.Checked);
WriteBool('ExportData', CheckBox2.Checked);
WriteBool('UseBackticks', CheckBoxUseBackticks.Checked);
CloseKey();
end;
Screen.Cursor := crHourGlass;
// export what?
exportstruc := CheckBox1.Checked;
exportdata := CheckBox2.Checked;
tofile := RadioButtonFile.Checked;
if tofile then begin
try
f := TFileStream.Create(EditFileName.Text, fmCreate);
except
messagedlg('File "'+EditFileName.Text+'" could not be opened!' + crlf + 'Maybe in use by another application?', mterror, [mbOK], 0);
f.free;
Screen.Cursor := crDefault;
abort;
end;
wfs(f, '# ' + main.appname + ' Dump ' + appversion);
wfs(f, '#');
end
else if RadioButtonDB.Checked then
DB2export := ComboBoxODB.Text
else if RadioButtonHost.Checked then begin
HostDb := explode(':', ComboBoxHost.Items[ComboBoxHost.ItemIndex]);
DB2export := HostDb[1];
for m:=0 to mainform.MDIChildCount-1 do begin
if (TMDIChild(mainform.MDIChildren[m]) <> TMDIChild(mainform.ActiveMDIChild)) and
(TMDIChild(mainform.MDIChildren[m]).ZConn.HostName = HostDb[0]) then
win2export := TMDIChild(mainform.MDIChildren[m]);
end;
end;
TRY
with TMDIChild(Mainform.ActiveMDIChild) do
begin
ExecQuery( 'USE ' + DBComBoBox.Text );
if tofile then
begin
wfs(f, '# Host: ' + ZConn.HostName + ' Database: ' + DBComBoBox.Text);
wfs(f, '# --------------------------------------------------------');
wfs(f, '# Server version: ' + GetVar( 'SELECT VERSION()' ) + ' ' + GetVar( 'SHOW VARIABLES LIKE "version_compile_os"' ) );
if CheckBoxWithUseDB.Checked then
begin
wfs(f);
wfs(f, 'USE ' + DBComBoBox.Text + ';');
end;
end;
// How many tables?
tcount := 0;
for i:=0 to TablesCheckListBox.Items.Count-1 do if TablesCheckListBox.checked[i] then
inc(tcount);
ProgressBar1.Max := 0;
if exportstruc then
ProgressBar1.Max := tcount;
if exportdata then
ProgressBar1.Max := ProgressBar1.Max + tcount;
TablesCheckListBox.ItemIndex := -1;
tablecounter := 0;
for i:=0 to TablesCheckListBox.Items.Count-1 do if TablesCheckListBox.checked[i] then
begin
inc(tablecounter);
if TablesCheckListBox.ItemIndex > -1 then
TablesCheckListBox.Checked[TablesCheckListBox.ItemIndex] := false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -