From 347ed4880ecbcbbbb30df0069a8ba7833453e175 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sun, 3 Apr 2016 09:45:04 +0000 Subject: [PATCH] Add "SQL DELETEs/INSERTs" to grid export formats. See http://www.heidisql.com/forum.php?t=20929 --- source/dbconnection.pas | 3 ++- source/exportgrid.pas | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 4494be52..cea0d04f 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -573,7 +573,6 @@ type procedure SetDBObject(Value: TDBObject); procedure CreateUpdateRow; function GetKeyColumns: TStringList; - function GetWhereClause: String; function GridQuery(QueryType, QueryBody: String): String; public constructor Create(AOwner: TComponent); override; @@ -599,6 +598,7 @@ type function IsNull(Column: String): Boolean; overload; function IsFunction(Column: Integer): Boolean; function HasResult: Boolean; virtual; abstract; + function GetWhereClause: String; procedure CheckEditable; procedure DeleteRow; function InsertRow: Int64; @@ -6688,6 +6688,7 @@ var begin // Compose WHERE clause including values from best key for editing NeededCols := GetKeyColumns; + Result := ''; for i:=0 to NeededCols.Count-1 do begin j := FColumnOrgNames.IndexOf(NeededCols[i]); diff --git a/source/exportgrid.pas b/source/exportgrid.pas index 39ce9cfa..dc4ca2f4 100644 --- a/source/exportgrid.pas +++ b/source/exportgrid.pas @@ -7,7 +7,7 @@ uses Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML, gnugettext; type - TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray, efMarkDown, efJSON); + TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efSQLDeleteInsert, efLaTeX, efWiki, efPHPArray, efMarkDown, efJSON); TfrmExportGrid = class(TForm) btnOK: TButton; @@ -69,9 +69,9 @@ type FGrid: TVirtualStringTree; FRecentFiles: TStringList; const FFormatToFileExtension: Array[TGridExportFormat] of String = - (('csv'), ('csv'), ('html'), ('xml'), ('sql'), ('sql'), ('LaTeX'), ('wiki'), ('php'), ('md'), ('json')); + (('csv'), ('csv'), ('html'), ('xml'), ('sql'), ('sql'), ('sql'), ('LaTeX'), ('wiki'), ('php'), ('md'), ('json')); const FFormatToDescription: Array[TGridExportFormat] of String = - (('Excel CSV'), ('Delimited text'), ('HTML table'), ('XML'), ('SQL INSERTs'), ('SQL REPLACEs'), ('LaTeX'), ('Wiki markup'), ('PHP Array'), ('Markdown Here'), ('JSON')); + (('Excel CSV'), ('Delimited text'), ('HTML table'), ('XML'), ('SQL INSERTs'), ('SQL REPLACEs'), ('SQL DELETEs/INSERTs'), ('LaTeX'), ('Wiki markup'), ('PHP Array'), ('Markdown Here'), ('JSON')); procedure SaveDialogTypeChange(Sender: TObject); function GetExportFormat: TGridExportFormat; procedure SetExportFormat(Value: TGridExportFormat); @@ -710,11 +710,16 @@ begin efXML: tmp := #9'' + CRLF; - efSQLInsert, efSQLReplace: begin - if ExportFormat = efSQLInsert then - tmp := 'INSERT' + efSQLInsert, efSQLReplace, efSQLDeleteInsert: begin + tmp := ''; + if ExportFormat = efSQLDeleteInsert then begin + tmp := tmp + 'DELETE FROM ' + GridData.Connection.QuoteIdent(Tablename) + ' WHERE' + GridData.GetWhereClause + ';' + CRLF; + end; + + if ExportFormat in [efSQLInsert, efSQLDeleteInsert] then + tmp := tmp + 'INSERT' else - tmp := 'REPLACE'; + tmp := tmp + 'REPLACE'; tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename); if chkIncludeColumnNames.Checked then begin tmp := tmp + ' ('; @@ -789,7 +794,7 @@ begin end; end; - efSQLInsert, efSQLReplace: begin + efSQLInsert, efSQLReplace, efSQLDeleteInsert: begin if GridData.IsNull(Col) then Data := 'NULL' else if GridData.DataType(Col).Index = dtBit then @@ -857,7 +862,7 @@ begin end; efXML: tmp := tmp + #9'' + CRLF; - efSQLInsert, efSQLReplace: begin + efSQLInsert, efSQLReplace, efSQLDeleteInsert: begin Delete(tmp, Length(tmp)-1, 2); tmp := tmp + ');' + CRLF; end; @@ -918,7 +923,7 @@ begin // SynEdit's exporter is slow on large strings, see issue #2903 if S.Size < 100*SIZE_KB then begin case ExportFormat of - efSQLInsert, efSQLReplace: begin + efSQLInsert, efSQLReplace, efSQLDeleteInsert: begin Exporter := TSynExporterHTML.Create(Self); Exporter.Highlighter := MainForm.SynSQLSyn1; Exporter.ExportAll(Explode(CRLF, S.DataString));