My favorites | Sign in
Project Home Source
Checkout   Browse   Changes  
Changes to /trunk/source/exportgrid.pas
r4179 vs. r4180 Compare: vs.  Format:
Revision r4180
Go to: 
/trunk/source/exportgrid.pas   r4179 /trunk/source/exportgrid.pas   r4180
1 unit exportgrid; 1 unit exportgrid;
2 2
3 interface 3 interface
4 4
5 uses 5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML; 7 Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML;
8 8
9 type 9 type
10 TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray); 10 TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray);
11 11
12 TfrmExportGrid = class(TForm) 12 TfrmExportGrid = class(TForm)
13 btnOK: TButton; 13 btnOK: TButton;
14 btnCancel: TButton; 14 btnCancel: TButton;
15 grpFormat: TRadioGroup; 15 grpFormat: TRadioGroup;
16 grpSelection: TRadioGroup; 16 grpSelection: TRadioGroup;
17 grpOutput: TGroupBox; 17 grpOutput: TGroupBox;
18 radioOutputCopyToClipboard: TRadioButton; 18 radioOutputCopyToClipboard: TRadioButton;
19 radioOutputFile: TRadioButton; 19 radioOutputFile: TRadioButton;
20 editFilename: TButtonedEdit; 20 editFilename: TButtonedEdit;
21 grpOptions: TGroupBox; 21 grpOptions: TGroupBox;
22 chkColumnHeader: TCheckBox; 22 chkColumnHeader: TCheckBox;
23 editSeparator: TButtonedEdit; 23 editSeparator: TButtonedEdit;
24 editEncloser: TButtonedEdit; 24 editEncloser: TButtonedEdit;
25 editTerminator: TButtonedEdit; 25 editTerminator: TButtonedEdit;
26 lblSeparator: TLabel; 26 lblSeparator: TLabel;
27 lblEncloser: TLabel; 27 lblEncloser: TLabel;
28 lblTerminator: TLabel; 28 lblTerminator: TLabel;
29 popupCSVchar: TPopupMenu; 29 popupCSVchar: TPopupMenu;
30 menuCSVtab: TMenuItem; 30 menuCSVtab: TMenuItem;
31 menuCSVunixlinebreak: TMenuItem; 31 menuCSVunixlinebreak: TMenuItem;
32 menuCSVmaclinebreak: TMenuItem; 32 menuCSVmaclinebreak: TMenuItem;
33 menuCSVwinlinebreak: TMenuItem; 33 menuCSVwinlinebreak: TMenuItem;
34 menuCSVnul: TMenuItem; 34 menuCSVnul: TMenuItem;
35 menuCSVbackspace: TMenuItem; 35 menuCSVbackspace: TMenuItem;
36 menuCSVcontrolz: TMenuItem; 36 menuCSVcontrolz: TMenuItem;
37 comboEncoding: TComboBox; 37 comboEncoding: TComboBox;
38 lblEncoding: TLabel; 38 lblEncoding: TLabel;
39 popupRecentFiles: TPopupMenu; 39 popupRecentFiles: TPopupMenu;
40 menuCSVsinglequote: TMenuItem; 40 menuCSVsinglequote: TMenuItem;
41 menuCSVdoublequote: TMenuItem; 41 menuCSVdoublequote: TMenuItem;
42 menuCSVcomma: TMenuItem; 42 menuCSVcomma: TMenuItem;
43 menuCSVsemicolon: TMenuItem; 43 menuCSVsemicolon: TMenuItem;
44 N1: TMenuItem; 44 N1: TMenuItem;
45 N2: TMenuItem; 45 N2: TMenuItem;
46 N3: TMenuItem; 46 N3: TMenuItem;
47 chkIncludeAutoIncrement: TCheckBox; 47 chkIncludeAutoIncrement: TCheckBox;
48 chkIncludeQuery: TCheckBox;
48 procedure FormCreate(Sender: TObject); 49 procedure FormCreate(Sender: TObject);
49 procedure FormDestroy(Sender: TObject); 50 procedure FormDestroy(Sender: TObject);
50 procedure CalcSize(Sender: TObject); 51 procedure CalcSize(Sender: TObject);
51 procedure FormClose(Sender: TObject; var Action: TCloseAction); 52 procedure FormClose(Sender: TObject; var Action: TCloseAction);
52 procedure editFilenameRightButtonClick(Sender: TObject); 53 procedure editFilenameRightButtonClick(Sender: TObject);
53 procedure editFilenameChange(Sender: TObject); 54 procedure editFilenameChange(Sender: TObject);
54 procedure SelectRecentFile(Sender: TObject); 55 procedure SelectRecentFile(Sender: TObject);
55 procedure popupRecentFilesPopup(Sender: TObject); 56 procedure popupRecentFilesPopup(Sender: TObject);
56 procedure menuCSVClick(Sender: TObject); 57 procedure menuCSVClick(Sender: TObject);
57 procedure editCSVRightButtonClick(Sender: TObject); 58 procedure editCSVRightButtonClick(Sender: TObject);
58 procedure editCSVChange(Sender: TObject); 59 procedure editCSVChange(Sender: TObject);
59 procedure ValidateControls(Sender: TObject); 60 procedure ValidateControls(Sender: TObject);
60 procedure btnOKClick(Sender: TObject); 61 procedure btnOKClick(Sender: TObject);
61 procedure FormShow(Sender: TObject); 62 procedure FormShow(Sender: TObject);
62 private 63 private
63 { Private declarations } 64 { Private declarations }
64 FCSVEditor: TButtonedEdit; 65 FCSVEditor: TButtonedEdit;
65 FCSVSeparator, FCSVEncloser, FCSVTerminator: String; 66 FCSVSeparator, FCSVEncloser, FCSVTerminator: String;
66 FGrid: TVirtualStringTree; 67 FGrid: TVirtualStringTree;
67 FRecentFiles: TStringList; 68 FRecentFiles: TStringList;
68 procedure SaveDialogTypeChange(Sender: TObject); 69 procedure SaveDialogTypeChange(Sender: TObject);
69 function GetExportFormat: TGridExportFormat; 70 function GetExportFormat: TGridExportFormat;
70 procedure SetExportFormat(Value: TGridExportFormat); 71 procedure SetExportFormat(Value: TGridExportFormat);
71 procedure SetExportFormatByFilename; 72 procedure SetExportFormatByFilename;
72 public 73 public
73 { Public declarations } 74 { Public declarations }
74 property Grid: TVirtualStringTree read FGrid write FGrid; 75 property Grid: TVirtualStringTree read FGrid write FGrid;
75 property ExportFormat: TGridExportFormat read GetExportFormat write SetExportFormat; 76 property ExportFormat: TGridExportFormat read GetExportFormat write SetExportFormat;
76 end; 77 end;
77 78
78 79
79 implementation 80 implementation
80 81
81 uses main, helpers, dbconnection, mysql_structures; 82 uses main, helpers, dbconnection, mysql_structures;
82 83
83 {$R *.dfm} 84 {$R *.dfm}
84 85
85 86
86 87
87 procedure TfrmExportGrid.FormCreate(Sender: TObject); 88 procedure TfrmExportGrid.FormCreate(Sender: TObject);
88 begin 89 begin
89 InheritFont(Font); 90 InheritFont(Font);
90 editFilename.Text := AppSettings.ReadString(asGridExportFilename); 91 editFilename.Text := AppSettings.ReadString(asGridExportFilename);
91 radioOutputCopyToClipboard.Checked := AppSettings.ReadBool(asGridExportOutputCopy); 92 radioOutputCopyToClipboard.Checked := AppSettings.ReadBool(asGridExportOutputCopy);
92 radioOutputFile.Checked := AppSettings.ReadBool(asGridExportOutputFile); 93 radioOutputFile.Checked := AppSettings.ReadBool(asGridExportOutputFile);
93 FRecentFiles := Explode(DELIM, AppSettings.ReadString(asGridExportRecentFiles)); 94 FRecentFiles := Explode(DELIM, AppSettings.ReadString(asGridExportRecentFiles));
94 comboEncoding.Items.Assign(MainForm.FileEncodings); 95 comboEncoding.Items.Assign(MainForm.FileEncodings);
95 comboEncoding.Items.Delete(0); // Remove "Auto detect" 96 comboEncoding.Items.Delete(0); // Remove "Auto detect"
96 comboEncoding.ItemIndex := AppSettings.ReadInt(asGridExportEncoding); 97 comboEncoding.ItemIndex := AppSettings.ReadInt(asGridExportEncoding);
97 grpFormat.ItemIndex := AppSettings.ReadInt(asGridExportFormat); 98 grpFormat.ItemIndex := AppSettings.ReadInt(asGridExportFormat);
98 grpSelection.ItemIndex := AppSettings.ReadInt(asGridExportSelection); 99 grpSelection.ItemIndex := AppSettings.ReadInt(asGridExportSelection);
99 chkColumnHeader.Checked := AppSettings.ReadBool(asGridExportColumnNames); 100 chkColumnHeader.Checked := AppSettings.ReadBool(asGridExportColumnNames);
101 chkIncludeQuery.Checked := AppSettings.ReadBool(asGridExportIncludeQuery);
100 FCSVSeparator := AppSettings.ReadString(asGridExportSeparator); 102 FCSVSeparator := AppSettings.ReadString(asGridExportSeparator);
101 FCSVEncloser := AppSettings.ReadString(asGridExportEncloser); 103 FCSVEncloser := AppSettings.ReadString(asGridExportEncloser);
102 FCSVTerminator := AppSettings.ReadString(asGridExportTerminator); 104 FCSVTerminator := AppSettings.ReadString(asGridExportTerminator);
103 ValidateControls(Sender); 105 ValidateControls(Sender);
104 end; 106 end;
105 107
106 108
107 procedure TfrmExportGrid.FormDestroy(Sender: TObject); 109 procedure TfrmExportGrid.FormDestroy(Sender: TObject);
108 begin 110 begin
109 // Store settings 111 // Store settings
110 if ModalResult = mrOK then begin 112 if ModalResult = mrOK then begin
111 AppSettings.WriteBool(asGridExportOutputCopy, radioOutputCopyToClipboard.Checked); 113 AppSettings.WriteBool(asGridExportOutputCopy, radioOutputCopyToClipboard.Checked);
112 AppSettings.WriteBool(asGridExportOutputFile, radioOutputFile.Checked); 114 AppSettings.WriteBool(asGridExportOutputFile, radioOutputFile.Checked);
113 AppSettings.WriteString(asGridExportFilename, editFilename.Text); 115 AppSettings.WriteString(asGridExportFilename, editFilename.Text);
114 AppSettings.WriteString(asGridExportRecentFiles, ImplodeStr(DELIM, FRecentFiles)); 116 AppSettings.WriteString(asGridExportRecentFiles, ImplodeStr(DELIM, FRecentFiles));
115 AppSettings.WriteInt(asGridExportEncoding, comboEncoding.ItemIndex); 117 AppSettings.WriteInt(asGridExportEncoding, comboEncoding.ItemIndex);
116 AppSettings.WriteInt(asGridExportFormat, grpFormat.ItemIndex); 118 AppSettings.WriteInt(asGridExportFormat, grpFormat.ItemIndex);
117 AppSettings.WriteInt(asGridExportSelection, grpSelection.ItemIndex); 119 AppSettings.WriteInt(asGridExportSelection, grpSelection.ItemIndex);
118 AppSettings.WriteBool(asGridExportColumnNames, chkColumnHeader.Checked); 120 AppSettings.WriteBool(asGridExportColumnNames, chkColumnHeader.Checked);
119 AppSettings.WriteBool(asGridExportIncludeAutoInc, chkIncludeAutoIncrement.Checked); 121 AppSettings.WriteBool(asGridExportIncludeAutoInc, chkIncludeAutoIncrement.Checked);
122 AppSettings.WriteBool(asGridExportIncludeQuery, chkIncludeQuery.Checked);
120 AppSettings.WriteString(asGridExportSeparator, FCSVSeparator); 123 AppSettings.WriteString(asGridExportSeparator, FCSVSeparator);
121 AppSettings.WriteString(asGridExportEncloser, FCSVEncloser); 124 AppSettings.WriteString(asGridExportEncloser, FCSVEncloser);
122 AppSettings.WriteString(asGridExportTerminator, FCSVTerminator); 125 AppSettings.WriteString(asGridExportTerminator, FCSVTerminator);
123 end; 126 end;
124 end; 127 end;
125 128
126 129
127 procedure TfrmExportGrid.FormShow(Sender: TObject); 130 procedure TfrmExportGrid.FormShow(Sender: TObject);
128 begin 131 begin
129 // Show dialog. Expect "Grid" property to be set now by the caller. 132 // Show dialog. Expect "Grid" property to be set now by the caller.
130 chkIncludeAutoIncrement.Checked := AppSettings.ReadBool(asGridExportIncludeAutoInc); 133 chkIncludeAutoIncrement.Checked := AppSettings.ReadBool(asGridExportIncludeAutoInc);
131 CalcSize(Sender); 134 CalcSize(Sender);
132 end; 135 end;
133 136
134 137
135 procedure TfrmExportGrid.FormClose(Sender: TObject; var Action: TCloseAction); 138 procedure TfrmExportGrid.FormClose(Sender: TObject; var Action: TCloseAction);
136 begin 139 begin
137 // Destroy dialog - not cached 140 // Destroy dialog - not cached
138 Action := caFree; 141 Action := caFree;
139 end; 142 end;
140 143
141 144
142 procedure TfrmExportGrid.ValidateControls(Sender: TObject); 145 procedure TfrmExportGrid.ValidateControls(Sender: TObject);
143 var 146 var
144 Enable: Boolean; 147 Enable: Boolean;
145 begin 148 begin
146 // Display the actually used control characters, even if they cannot be changed 149 // Display the actually used control characters, even if they cannot be changed
147 case ExportFormat of 150 case ExportFormat of
148 efExcel: begin 151 efExcel: begin
149 // Tab for pasting, semicolon if comma is also the decimal separator, and comma for the rest 152 // Tab for pasting, semicolon if comma is also the decimal separator, and comma for the rest
150 // see http://en.wikipedia.org/wiki/Comma-separated_values 153 // see http://en.wikipedia.org/wiki/Comma-separated_values
151 if radioOutputCopyToClipboard.Checked then 154 if radioOutputCopyToClipboard.Checked then
152 editSeparator.Text := '\t' 155 editSeparator.Text := '\t'
153 else if FormatSettings.DecimalSeparator=',' then 156 else if FormatSettings.DecimalSeparator=',' then
154 editSeparator.Text := ';' 157 editSeparator.Text := ';'
155 else 158 else
156 editSeparator.Text := ','; 159 editSeparator.Text := ',';
157 editEncloser.Text := '"'; 160 editEncloser.Text := '"';
158 editTerminator.Text := '\r\n'; 161 editTerminator.Text := '\r\n';
159 end; 162 end;
160 efCSV: begin 163 efCSV: begin
161 editSeparator.Text := FCSVSeparator; 164 editSeparator.Text := FCSVSeparator;
162 editEncloser.Text := FCSVEncloser; 165 editEncloser.Text := FCSVEncloser;
163 editTerminator.Text := FCSVTerminator; 166 editTerminator.Text := FCSVTerminator;
164 end; 167 end;
165 else begin 168 else begin
166 editSeparator.Text := ''; 169 editSeparator.Text := '';
167 editEncloser.Text := ''; 170 editEncloser.Text := '';
168 editTerminator.Text := ''; 171 editTerminator.Text := '';
169 end; 172 end;
170 end; 173 end;
171 174
175 chkColumnHeader.Enabled := ExportFormat <> efXML;
176 chkIncludeQuery.Enabled := ExportFormat = efHTML;
172 Enable := ExportFormat = efCSV; 177 Enable := ExportFormat = efCSV;
173 lblSeparator.Enabled := Enable; 178 lblSeparator.Enabled := Enable;
174 editSeparator.Enabled := Enable; 179 editSeparator.Enabled := Enable;
175 editSeparator.RightButton.Enabled := Enable; 180 editSeparator.RightButton.Enabled := Enable;
176 lblEncloser.Enabled := Enable; 181 lblEncloser.Enabled := Enable;
177 editEncloser.Enabled := Enable; 182 editEncloser.Enabled := Enable;
178 editEncloser.RightButton.Enabled := Enable; 183 editEncloser.RightButton.Enabled := Enable;
179 lblTerminator.Enabled := Enable; 184 lblTerminator.Enabled := Enable;
180 editTerminator.Enabled := Enable; 185 editTerminator.Enabled := Enable;
181 editTerminator.RightButton.Enabled := Enable; 186 editTerminator.RightButton.Enabled := Enable;
182 btnOK.Enabled := radioOutputCopyToClipboard.Checked or (radioOutputFile.Checked and (editFilename.Text <> '')); 187 btnOK.Enabled := radioOutputCopyToClipboard.Checked or (radioOutputFile.Checked and (editFilename.Text <> ''));
183 if radioOutputFile.Checked then 188 if radioOutputFile.Checked then
184 editFilename.Font.Color := clWindowText 189 editFilename.Font.Color := clWindowText
185 else 190 else
186 editFilename.Font.Color := clGrayText; 191 editFilename.Font.Color := clGrayText;
187 comboEncoding.Enabled := radioOutputFile.Checked; 192 comboEncoding.Enabled := radioOutputFile.Checked;
188 lblEncoding.Enabled := radioOutputFile.Checked; 193 lblEncoding.Enabled := radioOutputFile.Checked;
189 end; 194 end;
190 195
191 196
192 function TfrmExportGrid.GetExportFormat: TGridExportFormat; 197 function TfrmExportGrid.GetExportFormat: TGridExportFormat;
193 begin 198 begin
194 Result := TGridExportFormat(grpFormat.ItemIndex); 199 Result := TGridExportFormat(grpFormat.ItemIndex);
195 end; 200 end;
196 201
197 202
198 procedure TfrmExportGrid.SetExportFormat(Value: TGridExportFormat); 203 procedure TfrmExportGrid.SetExportFormat(Value: TGridExportFormat);
199 begin 204 begin
200 grpFormat.ItemIndex := Integer(Value); 205 grpFormat.ItemIndex := Integer(Value);
201 end; 206 end;
202 207
203 208
204 procedure TfrmExportGrid.SetExportFormatByFilename; 209 procedure TfrmExportGrid.SetExportFormatByFilename;
205 var 210 var
206 ext: String; 211 ext: String;
207 begin 212 begin
208 // Set format by file extension 213 // Set format by file extension
209 ext := LowerCase(Copy(ExtractFileExt(editFilename.Text), 2, 10)); 214 ext := LowerCase(Copy(ExtractFileExt(editFilename.Text), 2, 10));
210 if (ext = 'csv') and (not (ExportFormat in [efExcel, efCSV])) 215 if (ext = 'csv') and (not (ExportFormat in [efExcel, efCSV]))
211 then ExportFormat := efCSV 216 then ExportFormat := efCSV
212 else if ext = 'html' then ExportFormat := efHTML 217 else if ext = 'html' then ExportFormat := efHTML
213 else if ext = 'xml' then ExportFormat := efXML 218 else if ext = 'xml' then ExportFormat := efXML
214 else if ext = 'sql' then ExportFormat := efSQLInsert 219 else if ext = 'sql' then ExportFormat := efSQLInsert
215 else if ext = 'latex' then ExportFormat := efLaTeX 220 else if ext = 'latex' then ExportFormat := efLaTeX
216 else if ext = 'wiki' then ExportFormat := efWiki 221 else if ext = 'wiki' then ExportFormat := efWiki
217 else if ext = 'php' then ExportFormat := efPHPArray; 222 else if ext = 'php' then ExportFormat := efPHPArray;
218 end; 223 end;
219 224
220 225
221 procedure TfrmExportGrid.editFilenameChange(Sender: TObject); 226 procedure TfrmExportGrid.editFilenameChange(Sender: TObject);
222 begin 227 begin
223 radioOutputFile.Checked := True; 228 radioOutputFile.Checked := True;
224 end; 229 end;
225 230
226 231
227 procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject); 232 procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject);
228 var 233 var
229 Dialog: TSaveDialog; 234 Dialog: TSaveDialog;
230 begin 235 begin
231 // Select file target 236 // Select file target
232 Dialog := TSaveDialog.Create(Self); 237 Dialog := TSaveDialog.Create(Self);
233 Dialog.InitialDir := ExtractFilePath(editFilename.Text); 238 Dialog.InitialDir := ExtractFilePath(editFilename.Text);
234 Dialog.FileName := ExtractFileName(editFilename.Text); 239 Dialog.FileName := ExtractFileName(editFilename.Text);
235 Dialog.FileName := Copy(Dialog.FileName, 1, Length(Dialog.FileName)-Length(ExtractFileExt(Dialog.FileName))); 240 Dialog.FileName := Copy(Dialog.FileName, 1, Length(Dialog.FileName)-Length(ExtractFileExt(Dialog.FileName)));
236 Dialog.OnTypeChange := SaveDialogTypeChange; 241 Dialog.OnTypeChange := SaveDialogTypeChange;
237 Dialog.OnTypeChange(Dialog); 242 Dialog.OnTypeChange(Dialog);
238 Dialog.FilterIndex := grpFormat.ItemIndex+1; 243 Dialog.FilterIndex := grpFormat.ItemIndex+1;
239 Dialog.Filter := 'Tab separated values (*.csv)|*.csv|'+ 244 Dialog.Filter := 'Tab separated values (*.csv)|*.csv|'+
240 'Comma separated values (*.csv)|*.csv|'+ 245 'Comma separated values (*.csv)|*.csv|'+
241 'Hypertext markup language (*.html)|*.html|'+ 246 'Hypertext markup language (*.html)|*.html|'+
242 'Extensible markup language (*.xml)|*.xml|'+ 247 'Extensible markup language (*.xml)|*.xml|'+
243 'Structured query language (*.sql)|*.sql|'+ 248 'Structured query language (*.sql)|*.sql|'+
244 'LaTeX table (*.latex)|*.latex|'+ 249 'LaTeX table (*.latex)|*.latex|'+
245 'Wiki markup table (*.wiki)|*.wiki|'+ 250 'Wiki markup table (*.wiki)|*.wiki|'+
246 'PHP script (*.php)|*.php|'+ 251 'PHP script (*.php)|*.php|'+
247 'All files (*.*)|*.*'; 252 'All files (*.*)|*.*';
248 if Dialog.Execute then begin 253 if Dialog.Execute then begin
249 editFilename.Text := Dialog.FileName; 254 editFilename.Text := Dialog.FileName;
250 SetExportFormatByFilename; 255 SetExportFormatByFilename;
251 ValidateControls(Sender); 256 ValidateControls(Sender);
252 end; 257 end;
253 Dialog.Free; 258 Dialog.Free;
254 end; 259 end;
255 260
256 261
257 procedure TfrmExportGrid.popupRecentFilesPopup(Sender: TObject); 262 procedure TfrmExportGrid.popupRecentFilesPopup(Sender: TObject);
258 var 263 var
259 Filename: String; 264 Filename: String;
260 Menu: TPopupMenu; 265 Menu: TPopupMenu;
261 Item: TMenuItem; 266 Item: TMenuItem;
262 begin 267 begin
263 // Clear and populate drop down menu with recent files 268 // Clear and populate drop down menu with recent files
264 Menu := Sender as TPopupMenu; 269 Menu := Sender as TPopupMenu;
265 Menu.Items.Clear; 270 Menu.Items.Clear;
266 for Filename in FRecentFiles do begin 271 for Filename in FRecentFiles do begin
267 Item := TMenuItem.Create(Menu); 272 Item := TMenuItem.Create(Menu);
268 Menu.Items.Add(Item); 273 Menu.Items.Add(Item);
269 Item.Caption := Filename; 274 Item.Caption := Filename;
270 Item.Hint := Filename; 275 Item.Hint := Filename;
271 Item.OnClick := SelectRecentFile; 276 Item.OnClick := SelectRecentFile;
272 Item.Checked := Filename = editFilename.Text; 277 Item.Checked := Filename = editFilename.Text;
273 end; 278 end;
274 end; 279 end;
275 280
276 281
277 procedure TfrmExportGrid.SelectRecentFile(Sender: TObject); 282 procedure TfrmExportGrid.SelectRecentFile(Sender: TObject);
278 begin 283 begin
279 // Select file from recently used files 284 // Select file from recently used files
280 editFilename.Text := (Sender as TMenuItem).Hint; 285 editFilename.Text := (Sender as TMenuItem).Hint;
281 SetExportFormatByFilename; 286 SetExportFormatByFilename;
282 end; 287 end;
283 288
284 289
285 procedure TfrmExportGrid.CalcSize(Sender: TObject); 290 procedure TfrmExportGrid.CalcSize(Sender: TObject);
286 var 291 var
287 GridData: TDBQuery; 292 GridData: TDBQuery;
288 Node: PVirtualNode; 293 Node: PVirtualNode;
289 Col, ExcludeCol: TColumnIndex; 294 Col, ExcludeCol: TColumnIndex;
290 RowNum: PCardinal; 295 RowNum: PCardinal;
291 SelectionSize, AllSize: Int64; 296 SelectionSize, AllSize: Int64;
292 begin 297 begin
293 GridData := Mainform.GridResult(Grid); 298 GridData := Mainform.GridResult(Grid);
294 AllSize := 0; 299 AllSize := 0;
295 SelectionSize := 0; 300 SelectionSize := 0;
296 chkIncludeAutoIncrement.Enabled := GridData.AutoIncrementColumn > -1; 301 chkIncludeAutoIncrement.Enabled := GridData.AutoIncrementColumn > -1;
297 ExcludeCol := -1; 302 ExcludeCol := -1;
298 if chkIncludeAutoIncrement.Enabled and (not chkIncludeAutoIncrement.Checked) then 303 if chkIncludeAutoIncrement.Enabled and (not chkIncludeAutoIncrement.Checked) then
299 ExcludeCol := GridData.AutoIncrementColumn; 304 ExcludeCol := GridData.AutoIncrementColumn;
300 305
301 Node := GetNextNode(Grid, nil, False); 306 Node := GetNextNode(Grid, nil, False);
302 while Assigned(Node) do begin 307 while Assigned(Node) do begin
303 RowNum := Grid.GetNodeData(Node); 308 RowNum := Grid.GetNodeData(Node);
304 GridData.RecNo := RowNum^; 309 GridData.RecNo := RowNum^;
305 Col := Grid.Header.Columns.GetFirstVisibleColumn; 310 Col := Grid.Header.Columns.GetFirstVisibleColumn;
306 while Col > NoColumn do begin 311 while Col > NoColumn do begin
307 if Col <> ExcludeCol then begin 312 if Col <> ExcludeCol then begin
308 Inc(AllSize, GridData.ColumnLengths(Col)); 313 Inc(AllSize, GridData.ColumnLengths(Col));
309 if vsSelected in Node.States then 314 if vsSelected in Node.States then
310 Inc(SelectionSize, GridData.ColumnLengths(Col)); 315 Inc(SelectionSize, GridData.ColumnLengths(Col));
311 end; 316 end;
312 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 317 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
313 end; 318 end;
314 Node := GetNextNode(Grid, Node, False); 319 Node := GetNextNode(Grid, Node, False);
315 end; 320 end;
316 grpSelection.Items[0] := 'Selection ('+FormatNumber(Grid.SelectedCount)+' rows, '+FormatByteNumber(SelectionSize)+')'; 321 grpSelection.Items[0] := 'Selection ('+FormatNumber(Grid.SelectedCount)+' rows, '+FormatByteNumber(SelectionSize)+')';
317 grpSelection.Items[1] := 'Complete ('+FormatNumber(Grid.RootNodeCount)+' rows, '+FormatByteNumber(AllSize)+')'; 322 grpSelection.Items[1] := 'Complete ('+FormatNumber(Grid.RootNodeCount)+' rows, '+FormatByteNumber(AllSize)+')';
318 end; 323 end;
319 324
320 325
321 procedure TfrmExportGrid.editCSVChange(Sender: TObject); 326 procedure TfrmExportGrid.editCSVChange(Sender: TObject);
322 var 327 var
323 Edit: TButtonedEdit; 328 Edit: TButtonedEdit;
324 begin 329 begin
325 // Remember csv settings 330 // Remember csv settings
326 Edit := Sender as TButtonedEdit; 331 Edit := Sender as TButtonedEdit;
327 if ExportFormat = efCSV then begin 332 if ExportFormat = efCSV then begin
328 if Edit = editSeparator then 333 if Edit = editSeparator then
329 FCSVSeparator := Edit.Text 334 FCSVSeparator := Edit.Text
330 else if Edit = editEncloser then 335 else if Edit = editEncloser then
331 FCSVEncloser := Edit.Text 336 FCSVEncloser := Edit.Text
332 else if Edit = editTerminator then 337 else if Edit = editTerminator then
333 FCSVTerminator := Edit.Text; 338 FCSVTerminator := Edit.Text;
334 end; 339 end;
335 end; 340 end;
336 341
337 342
338 procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject); 343 procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject);
339 var 344 var
340 Dialog: TSaveDialog; 345 Dialog: TSaveDialog;
341 begin 346 begin
342 // Set default file-extension of saved file and options on the dialog to show 347 // Set default file-extension of saved file and options on the dialog to show
343 Dialog := Sender as TSaveDialog; 348 Dialog := Sender as TSaveDialog;
344 case Dialog.FilterIndex of 349 case Dialog.FilterIndex of
345 1: Dialog.DefaultExt := 'csv'; 350 1: Dialog.DefaultExt := 'csv';
346 2: Dialog.DefaultExt := 'html'; 351 2: Dialog.DefaultExt := 'html';
347 3: Dialog.DefaultExt := 'xml'; 352 3: Dialog.DefaultExt := 'xml';
348 4: Dialog.DefaultExt := 'sql'; 353 4: Dialog.DefaultExt := 'sql';
349 5: Dialog.DefaultExt := 'LaTeX'; 354 5: Dialog.DefaultExt := 'LaTeX';
350 6: Dialog.DefaultExt := 'wiki'; 355 6: Dialog.DefaultExt := 'wiki';
351 end; 356 end;
352 end; 357 end;
353 358
354 359
355 procedure TfrmExportGrid.editCSVRightButtonClick(Sender: TObject); 360 procedure TfrmExportGrid.editCSVRightButtonClick(Sender: TObject);
356 var 361 var
357 p: TPoint; 362 p: TPoint;
358 Item: TMenuItem; 363 Item: TMenuItem;
359 begin 364 begin
360 // Remember editor and prepare popup menu items 365 // Remember editor and prepare popup menu items
361 FCSVEditor := Sender as TButtonedEdit; 366 FCSVEditor := Sender as TButtonedEdit;
362 p := FCSVEditor.ClientToScreen(FCSVEditor.ClientRect.BottomRight); 367 p := FCSVEditor.ClientToScreen(FCSVEditor.ClientRect.BottomRight);
363 for Item in popupCSVchar.Items do begin 368 for Item in popupCSVchar.Items do begin
364 Item.OnClick := menuCSVClick; 369 Item.OnClick := menuCSVClick;
365 Item.Checked := FCSVEditor.Text = Item.Hint; 370 Item.Checked := FCSVEditor.Text = Item.Hint;
366 end; 371 end;
367 popupCSVchar.Popup(p.X-16, p.Y); 372 popupCSVchar.Popup(p.X-16, p.Y);
368 end; 373 end;
369 374
370 375
371 procedure TfrmExportGrid.menuCSVClick(Sender: TObject); 376 procedure TfrmExportGrid.menuCSVClick(Sender: TObject);
372 begin 377 begin
373 // Insert char from menu 378 // Insert char from menu
374 FCSVEditor.Text := TMenuItem(Sender).Hint; 379 FCSVEditor.Text := TMenuItem(Sender).Hint;
375 end; 380 end;
376 381
377 382
378 procedure TfrmExportGrid.btnOKClick(Sender: TObject); 383 procedure TfrmExportGrid.btnOKClick(Sender: TObject);
379 var 384 var
380 Col, ExcludeCol: TColumnIndex; 385 Col, ExcludeCol: TColumnIndex;
381 Header, Data, tmp, Encloser, Separator, Terminator, TableName: String; 386 Header, Data, tmp, Encloser, Separator, Terminator, TableName: String;
382 Node: PVirtualNode; 387 Node: PVirtualNode;
383 GridData: TDBQuery; 388 GridData: TDBQuery;
384 SelectionOnly: Boolean; 389 SelectionOnly: Boolean;
385 i: Integer; 390 i: Integer;
386 NodeCount: Cardinal; 391 NodeCount: Cardinal;
387 RowNum: PCardinal; 392 RowNum: PCardinal;
388 HTML: TStream; 393 HTML: TStream;
389 S: TStringStream; 394 S: TStringStream;
390 Exporter: TSynExporterHTML; 395 Exporter: TSynExporterHTML;
391 Encoding: TEncoding; 396 Encoding: TEncoding;
392 begin 397 begin
393 // Confirmation dialog if file exists 398 // Confirmation dialog if file exists
394 if radioOutputFile.Checked 399 if radioOutputFile.Checked
395 and FileExists(editFilename.Text) 400 and FileExists(editFilename.Text)
396 and (MessageDialog('File exists', 'Overwrite file '+editFilename.Text+'?', mtConfirmation, [mbYes, mbCancel]) = mrCancel) 401 and (MessageDialog('File exists', 'Overwrite file '+editFilename.Text+'?', mtConfirmation, [mbYes, mbCancel]) = mrCancel)
397 then begin 402 then begin
398 ModalResult := mrNone; 403 ModalResult := mrNone;
399 Exit; 404 Exit;
400 end; 405 end;
401 406
402 Screen.Cursor := crHourglass; 407 Screen.Cursor := crHourglass;
403 408
404 SelectionOnly := grpSelection.ItemIndex = 0; 409 SelectionOnly := grpSelection.ItemIndex = 0;
405 Mainform.DataGridEnsureFullRows(Grid, SelectionOnly); 410 Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
406 GridData := Mainform.GridResult(Grid); 411 GridData := Mainform.GridResult(Grid);
407 if SelectionOnly then 412 if SelectionOnly then
408 NodeCount := Grid.SelectedCount 413 NodeCount := Grid.SelectedCount
409 else 414 else
410 NodeCount := Grid.RootNodeCount; 415 NodeCount := Grid.RootNodeCount;
411 MainForm.EnableProgress(NodeCount); 416 MainForm.EnableProgress(NodeCount);
412 TableName := BestTableName(GridData); 417 TableName := BestTableName(GridData);
413 ExcludeCol := -1; 418 ExcludeCol := -1;
414 if (not chkIncludeAutoIncrement.Checked) or (not chkIncludeAutoIncrement.Enabled) then 419 if (not chkIncludeAutoIncrement.Checked) or (not chkIncludeAutoIncrement.Enabled) then
415 ExcludeCol := GridData.AutoIncrementColumn; 420 ExcludeCol := GridData.AutoIncrementColumn;
416 421
417 if radioOutputCopyToClipboard.Checked then 422 if radioOutputCopyToClipboard.Checked then
418 Encoding := TEncoding.UTF8 423 Encoding := TEncoding.UTF8
419 else begin 424 else begin
420 Encoding := MainForm.GetEncodingByName(comboEncoding.Text); 425 Encoding := MainForm.GetEncodingByName(comboEncoding.Text);
421 // Add selected file to file list, and sort it onto the top of the list 426 // Add selected file to file list, and sort it onto the top of the list
422 i := FRecentFiles.IndexOf(editFilename.Text); 427 i := FRecentFiles.IndexOf(editFilename.Text);
423 if i > -1 then 428 if i > -1 then
424 FRecentFiles.Delete(i); 429 FRecentFiles.Delete(i);
425 FRecentFiles.Insert(0, editFilename.Text); 430 FRecentFiles.Insert(0, editFilename.Text);
426 for i:=FRecentFiles.Count-1 downto 10 do 431 for i:=FRecentFiles.Count-1 downto 10 do
427 FRecentFiles.Delete(i); 432 FRecentFiles.Delete(i);
428 end; 433 end;
429 S := TStringStream.Create(Header, Encoding); 434 S := TStringStream.Create(Header, Encoding);
430 Header := ''; 435 Header := '';
431 case ExportFormat of 436 case ExportFormat of
432 efHTML: begin 437 efHTML: begin
433 Header := 438 Header :=
434 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + CRLF + 439 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + CRLF +
435 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF + CRLF + 440 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF + CRLF +
436 '<html>' + CRLF + 441 '<html>' + CRLF +
437 ' <head>' + CRLF + 442 ' <head>' + CRLF +
438 ' <title>' + TableName + '</title>' + CRLF + 443 ' <title>' + TableName + '</title>' + CRLF +
439 ' <meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + CRLF + 444 ' <meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + CRLF +
440 ' <meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + CRLF + 445 ' <meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + CRLF +
441 ' <style type="text/css">' + CRLF + 446 ' <style type="text/css">' + CRLF +
442 ' thead tr {background-color: ActiveCaption; color: CaptionText;}' + CRLF + 447 ' thead tr {background-color: ActiveCaption; color: CaptionText;}' + CRLF +
443 ' th, td {vertical-align: top; font-family: "'+Grid.Font.Name+'"; font-size: '+IntToStr(Grid.Font.Size)+'pt; padding: '+IntToStr(Grid.TextMargin-1)+'px; }' + CRLF + 448 ' th, td {vertical-align: top; font-family: "'+Grid.Font.Name+'"; font-size: '+IntToStr(Grid.Font.Size)+'pt; padding: '+IntToStr(Grid.TextMargin-1)+'px; }' + CRLF +
444 ' table, td {border: 1px solid silver;}' + CRLF + 449 ' table, td {border: 1px solid silver;}' + CRLF +
445 ' table {border-collapse: collapse;}' + CRLF; 450 ' table {border-collapse: collapse;}' + CRLF;
446 Col := Grid.Header.Columns.GetFirstVisibleColumn; 451 Col := Grid.Header.Columns.GetFirstVisibleColumn;
447 while Col > NoColumn do begin 452 while Col > NoColumn do begin
448 // Adjust preferred width of columns. 453 // Adjust preferred width of columns.
449 Header := Header + 454 Header := Header +
450 ' thead .col' + IntToStr(Col) + ' {width: ' + IntToStr(Grid.Header.Columns[Col].Width) + 'px;}' + CRLF; 455 ' thead .col' + IntToStr(Col) + ' {width: ' + IntToStr(Grid.Header.Columns[Col].Width) + 'px;}' + CRLF;
451 // Right-justify all cells to match the grid on screen. 456 // Right-justify all cells to match the grid on screen.
452 if Grid.Header.Columns[Col].Alignment = taRightJustify then 457 if Grid.Header.Columns[Col].Alignment = taRightJustify then
453 Header := Header + ' .col' + IntToStr(Col) + ' {text-align: right;}' + CRLF; 458 Header := Header + ' .col' + IntToStr(Col) + ' {text-align: right;}' + CRLF;
454 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 459 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
455 end; 460 end;
456 Header := Header + 461 Header := Header +
457 ' </style>' + CRLF + 462 ' </style>' + CRLF +
458 ' </head>' + CRLF + CRLF + 463 ' </head>' + CRLF + CRLF +
459 ' <body>' + CRLF + CRLF + 464 ' <body>' + CRLF + CRLF;
460 ' <table caption="' + TableName + ' (' + inttostr(NodeCount) + ' rows)">' + CRLF; 465 if chkIncludeQuery.Checked then
466 Header := Header + '<p style="font-family: monospace; white-space: pre;">' + GridData.SQL + '</p>' + CRLF + CRLF;
467 Header := Header + ' <table caption="' + TableName + ' (' + inttostr(NodeCount) + ' rows)">' + CRLF;
461 if chkColumnHeader.Checked then begin 468 if chkColumnHeader.Checked then begin
462 Header := Header + 469 Header := Header +
463 ' <thead>' + CRLF + 470 ' <thead>' + CRLF +
464 ' <tr>' + CRLF; 471 ' <tr>' + CRLF;
465 Col := Grid.Header.Columns.GetFirstVisibleColumn; 472 Col := Grid.Header.Columns.GetFirstVisibleColumn;
466 while Col > NoColumn do begin 473 while Col > NoColumn do begin
467 if Col <> ExcludeCol then 474 if Col <> ExcludeCol then
468 Header := Header + ' <th class="col' + IntToStr(Col) + '">' + Grid.Header.Columns[Col].Text + '</th>' + CRLF; 475 Header := Header + ' <th class="col' + IntToStr(Col) + '">' + Grid.Header.Columns[Col].Text + '</th>' + CRLF;
469 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 476 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
470 end; 477 end;
471 Header := Header + 478 Header := Header +
472 ' </tr>' + CRLF + 479 ' </tr>' + CRLF +
473 ' </thead>' + CRLF; 480 ' </thead>' + CRLF;
474 end; 481 end;
475 Header := Header + 482 Header := Header +
476 ' <tbody>' + CRLF; 483 ' <tbody>' + CRLF;
477 end; 484 end;
478 485
479 efExcel, efCSV: begin 486 efExcel, efCSV: begin
480 Separator := GridData.Connection.UnescapeString(editSeparator.Text); 487 Separator := GridData.Connection.UnescapeString(editSeparator.Text);
481 Encloser := GridData.Connection.UnescapeString(editEncloser.Text); 488 Encloser := GridData.Connection.UnescapeString(editEncloser.Text);
482 Terminator := GridData.Connection.UnescapeString(editTerminator.Text); 489 Terminator := GridData.Connection.UnescapeString(editTerminator.Text);
483 if chkColumnHeader.Checked then begin 490 if chkColumnHeader.Checked then begin
484 Col := Grid.Header.Columns.GetFirstVisibleColumn; 491 Col := Grid.Header.Columns.GetFirstVisibleColumn;
485 while Col > NoColumn do begin 492 while Col > NoColumn do begin
486 // Alter column name in header if data is not raw. 493 // Alter column name in header if data is not raw.
487 if Col <> ExcludeCol then begin 494 if Col <> ExcludeCol then begin
488 Data := Grid.Header.Columns[Col].Text; 495 Data := Grid.Header.Columns[Col].Text;
489 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 496 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
490 Data := 'HEX(' + Data + ')'; 497 Data := 'HEX(' + Data + ')';
491 // Add header item. 498 // Add header item.
492 if Header <> '' then 499 if Header <> '' then
493 Header := Header + Separator; 500 Header := Header + Separator;
494 Header := Header + Encloser + Data + Encloser; 501 Header := Header + Encloser + Data + Encloser;
495 end; 502 end;
496 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 503 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
497 end; 504 end;
498 Header := Header + Terminator; 505 Header := Header + Terminator;
499 end; 506 end;
500 end; 507 end;
501 508
502 efXML: begin 509 efXML: begin
503 Header := '<?xml version="1.0" encoding="'+MainForm.GetCharsetByEncoding(Encoding)+'"?>' + CRLF + CRLF + 510 Header := '<?xml version="1.0" encoding="'+MainForm.GetCharsetByEncoding(Encoding)+'"?>' + CRLF + CRLF +
504 '<table name="'+TableName+'">' + CRLF; 511 '<table name="'+TableName+'">' + CRLF;
505 end; 512 end;
506 513
507 efLaTeX: begin 514 efLaTeX: begin
508 Header := '\begin{tabular}'; 515 Header := '\begin{tabular}';
509 Separator := ' & '; 516 Separator := ' & ';
510 Encloser := ''; 517 Encloser := '';
511 Terminator := '\\ '+CRLF; 518 Terminator := '\\ '+CRLF;
512 Header := Header + '{'; 519 Header := Header + '{';
513 Col := Grid.Header.Columns.GetFirstVisibleColumn; 520 Col := Grid.Header.Columns.GetFirstVisibleColumn;
514 while Col > NoColumn do begin 521 while Col > NoColumn do begin
515 if Col <> ExcludeCol then 522 if Col <> ExcludeCol then
516 Header := Header + ' c '; 523 Header := Header + ' c ';
517 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 524 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
518 end; 525 end;
519 Header := Header + '}' + CRLF; 526 Header := Header + '}' + CRLF;
520 if chkColumnHeader.Checked then begin 527 if chkColumnHeader.Checked then begin
521 Col := Grid.Header.Columns.GetFirstVisibleColumn; 528 Col := Grid.Header.Columns.GetFirstVisibleColumn;
522 while Col > NoColumn do begin 529 while Col > NoColumn do begin
523 if Col <> ExcludeCol then 530 if Col <> ExcludeCol then
524 Header := Header + Grid.Header.Columns[Col].Text + Separator; 531 Header := Header + Grid.Header.Columns[Col].Text + Separator;
525 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 532 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
526 end; 533 end;
527 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator)); 534 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
528 Header := Header + Terminator; 535 Header := Header + Terminator;
529 end; 536 end;
530 end; 537 end;
531 538
532 efWiki: begin 539 efWiki: begin
533 Separator := ' || '; 540 Separator := ' || ';
534 Encloser := ''; 541 Encloser := '';
535 Terminator := ' ||'+CRLF; 542 Terminator := ' ||'+CRLF;
536 if chkColumnHeader.Checked then begin 543 if chkColumnHeader.Checked then begin
537 Header := '|| '; 544 Header := '|| ';
538 Col := Grid.Header.Columns.GetFirstVisibleColumn; 545 Col := Grid.Header.Columns.GetFirstVisibleColumn;
539 while Col > NoColumn do begin 546 while Col > NoColumn do begin
540 if Col <> ExcludeCol then 547 if Col <> ExcludeCol then
541 Header := Header + '*' + Grid.Header.Columns[Col].Text + '*' + Separator; 548 Header := Header + '*' + Grid.Header.Columns[Col].Text + '*' + Separator;
542 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 549 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
543 end; 550 end;
544 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator)); 551 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
545 Header := Header + Terminator; 552 Header := Header + Terminator;
546 end; 553 end;
547 end; 554 end;
548 555
549 efPHPArray: begin 556 efPHPArray: begin
550 if radioOutputFile.Checked then 557 if radioOutputFile.Checked then
551 Header := '<?php'+CRLF+'$'+TableName+' = array('+CRLF 558 Header := '<?php'+CRLF+'$'+TableName+' = array('+CRLF
552 else 559 else
553 Header := '$'+TableName+' = array('+CRLF; 560 Header := '$'+TableName+' = array('+CRLF;
554 end; 561 end;
555 562
556 end; 563 end;
557 S.WriteString(Header); 564 S.WriteString(Header);
558 565
559 Node := GetNextNode(Grid, nil, SelectionOnly); 566 Node := GetNextNode(Grid, nil, SelectionOnly);
560 while Assigned(Node) do begin 567 while Assigned(Node) do begin
561 // Update status once in a while. 568 // Update status once in a while.
562 if (Node.Index+1) mod 100 = 0 then begin 569 if (Node.Index+1) mod 100 = 0 then begin
563 Mainform.ShowStatusMsg('Exporting row '+FormatNumber(Node.Index+1)+' of '+FormatNumber(NodeCount)+ 570 Mainform.ShowStatusMsg('Exporting row '+FormatNumber(Node.Index+1)+' of '+FormatNumber(NodeCount)+
564 ' ('+IntToStr(Trunc((Node.Index+1) / NodeCount *100))+'%, '+FormatByteNumber(S.Size)+')' 571 ' ('+IntToStr(Trunc((Node.Index+1) / NodeCount *100))+'%, '+FormatByteNumber(S.Size)+')'
565 ); 572 );
566 MainForm.ProgressStep; 573 MainForm.ProgressStep;
567 end; 574 end;
568 RowNum := Grid.GetNodeData(Node); 575 RowNum := Grid.GetNodeData(Node);
569 GridData.RecNo := RowNum^; 576 GridData.RecNo := RowNum^;
570 577
571 // Row preamble 578 // Row preamble
572 case ExportFormat of 579 case ExportFormat of
573 efHTML: tmp := ' <tr>' + CRLF; 580 efHTML: tmp := ' <tr>' + CRLF;
574 581
575 efXML: tmp := #9'<row>' + CRLF; 582 efXML: tmp := #9'<row>' + CRLF;
576 583
577 efSQLInsert, efSQLReplace: begin 584 efSQLInsert, efSQLReplace: begin
578 if ExportFormat = efSQLInsert then 585 if ExportFormat = efSQLInsert then
579 tmp := 'INSERT' 586 tmp := 'INSERT'
580 else 587 else
581 tmp := 'REPLACE'; 588 tmp := 'REPLACE';
582 tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename); 589 tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename);
583 if chkColumnHeader.Checked then begin 590 if chkColumnHeader.Checked then begin
584 tmp := tmp + ' ('; 591 tmp := tmp + ' (';
585 Col := Grid.Header.Columns.GetFirstVisibleColumn; 592 Col := Grid.Header.Columns.GetFirstVisibleColumn;
586 while Col > NoColumn do begin 593 while Col > NoColumn do begin
587 if Col <> ExcludeCol then 594 if Col <> ExcludeCol then
588 tmp := tmp + GridData.Connection.QuoteIdent(Grid.Header.Columns[Col].Text)+', '; 595 tmp := tmp + GridData.Connection.QuoteIdent(Grid.Header.Columns[Col].Text)+', ';
589 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 596 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
590 end; 597 end;
591 Delete(tmp, Length(tmp)-1, 2); 598 Delete(tmp, Length(tmp)-1, 2);
592 tmp := tmp + ')'; 599 tmp := tmp + ')';
593 end; 600 end;
594 tmp := tmp + ' VALUES ('; 601 tmp := tmp + ' VALUES (';
595 end; 602 end;
596 603
597 efWiki: tmp := TrimLeft(Separator); 604 efWiki: tmp := TrimLeft(Separator);
598 605
599 efPHPArray: tmp := #9 + 'array( // row #'+FormatNumber(GridData.RecNo)+CRLF; 606 efPHPArray: tmp := #9 + 'array( // row #'+FormatNumber(GridData.RecNo)+CRLF;
600 607
601 else tmp := ''; 608 else tmp := '';
602 end; 609 end;
603 610
604 Col := Grid.Header.Columns.GetFirstVisibleColumn; 611 Col := Grid.Header.Columns.GetFirstVisibleColumn;
605 while Col > NoColumn do begin 612 while Col > NoColumn do begin
606 if Col <> ExcludeCol then begin 613 if Col <> ExcludeCol then begin
607 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 614 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
608 Data := GridData.HexValue(Col) 615 Data := GridData.HexValue(Col)
609 else 616 else
610 Data := GridData.Col(Col); 617 Data := GridData.Col(Col);
611 // Keep formatted numeric values 618 // Keep formatted numeric values
612 if (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) 619 if (GridData.DataType(Col).Category in [dtcInteger, dtcReal])
613 and (ExportFormat in [efExcel, efHTML]) then 620 and (ExportFormat in [efExcel, efHTML]) then
614 Data := FormatNumber(Data, False); 621 Data := FormatNumber(Data, False);
615 622
616 case ExportFormat of 623 case ExportFormat of
617 efHTML: begin 624 efHTML: begin
618 // Escape HTML control characters in data. 625 // Escape HTML control characters in data.
619 Data := htmlentities(Data); 626 Data := htmlentities(Data);
620 tmp := tmp + ' <td class="col' + IntToStr(Col) + '">' + Data + '</td>' + CRLF; 627 tmp := tmp + ' <td class="col' + IntToStr(Col) + '">' + Data + '</td>' + CRLF;
621 end; 628 end;
622 629
623 efExcel, efCSV, efLaTeX, efWiki: begin 630 efExcel, efCSV, efLaTeX, efWiki: begin
624 // Escape encloser characters inside data per de-facto CSV. 631 // Escape encloser characters inside data per de-facto CSV.
625 Data := StringReplace(Data, Encloser, Encloser+Encloser, [rfReplaceAll]); 632 Data := StringReplace(Data, Encloser, Encloser+Encloser, [rfReplaceAll]);
626 Data := Encloser + Data + Encloser; 633 Data := Encloser + Data + Encloser;
627 tmp := tmp + Data + Separator; 634 tmp := tmp + Data + Separator;
628 end; 635 end;
629 636
630 efXML: begin 637 efXML: begin
631 // Print cell start tag. 638 // Print cell start tag.
632 tmp := tmp + #9#9'<' + Grid.Header.Columns[Col].Text; 639 tmp := tmp + #9#9'<' + Grid.Header.Columns[Col].Text;
633 if GridData.IsNull(Col) then 640 if GridData.IsNull(Col) then
634 tmp := tmp + ' isnull="true" />' + CRLF 641 tmp := tmp + ' isnull="true" />' + CRLF
635 else begin 642 else begin
636 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 643 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
637 tmp := tmp + ' format="hex"'; 644 tmp := tmp + ' format="hex"';
638 tmp := tmp + '>' + htmlentities(Data) + '</' + Grid.Header.Columns[Col].Text + '>' + CRLF; 645 tmp := tmp + '>' + htmlentities(Data) + '</' + Grid.Header.Columns[Col].Text + '>' + CRLF;
639 end; 646 end;
640 end; 647 end;
641 648
642 efSQLInsert, efSQLReplace: begin 649 efSQLInsert, efSQLReplace: begin
643 if GridData.IsNull(Col) then 650 if GridData.IsNull(Col) then
644 Data := 'NULL' 651 Data := 'NULL'
645 else if GridData.DataType(Col).Index = dtBit then 652 else if GridData.DataType(Col).Index = dtBit then
646 Data := 'b' + esc(Data) 653 Data := 'b' + esc(Data)
647 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal, dtcBinary, dtcSpatial]) then 654 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal, dtcBinary, dtcSpatial]) then
648 Data := esc(Data); 655 Data := esc(Data);
649 tmp := tmp + Data + ', '; 656 tmp := tmp + Data + ', ';
650 end; 657 end;
651 658
652 efPHPArray: begin 659 efPHPArray: begin
653 if GridData.IsNull(Col) then 660 if GridData.IsNull(Col) then
654 Data := 'NULL' 661 Data := 'NULL'
655 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) then 662 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) then
656 Data := esc(Data); 663 Data := esc(Data);
657 if chkColumnHeader.Checked then 664 if chkColumnHeader.Checked then
658 tmp := tmp + #9#9 + '''' + Grid.Header.Columns[Col].Text + ''' => ' + Data + ','+CRLF 665 tmp := tmp + #9#9 + '''' + Grid.Header.Columns[Col].Text + ''' => ' + Data + ','+CRLF
659 else 666 else
660 tmp := tmp + #9#9 + Data + ','+CRLF; 667 tmp := tmp + #9#9 + Data + ','+CRLF;
661 end; 668 end;
662 669
663 end; 670 end;
664 end; 671 end;
665 672
666 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 673 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
667 end; 674 end;
668 675
669 // Row epilogue 676 // Row epilogue
670 case ExportFormat of 677 case ExportFormat of
671 efHTML: 678 efHTML:
672 tmp := tmp + ' </tr>' + CRLF; 679 tmp := tmp + ' </tr>' + CRLF;
673 efExcel, efCSV, efLaTeX, efWiki: begin 680 efExcel, efCSV, efLaTeX, efWiki: begin
674 Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator)); 681 Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator));
675 tmp := tmp + Terminator; 682 tmp := tmp + Terminator;
676 end; 683 end;
677 efXML: 684 efXML:
678 tmp := tmp + #9'</row>' + CRLF; 685 tmp := tmp + #9'</row>' + CRLF;
679 efSQLInsert, efSQLReplace: begin 686 efSQLInsert, efSQLReplace: begin
680 Delete(tmp, Length(tmp)-1, 2); 687 Delete(tmp, Length(tmp)-1, 2);
681 tmp := tmp + ');' + CRLF; 688 tmp := tmp + ');' + CRLF;
682 end; 689 end;
683 efPHPArray: 690 efPHPArray:
684 tmp := tmp + #9 + '),' + CRLF; 691 tmp := tmp + #9 + '),' + CRLF;
685 end; 692 end;
686 S.WriteString(tmp); 693 S.WriteString(tmp);
687 694
688 Node := GetNextNode(Grid, Node, SelectionOnly); 695 Node := GetNextNode(Grid, Node, SelectionOnly);
689 end; 696 end;
690 697
691 // Footer 698 // Footer
692 case ExportFormat of 699 case ExportFormat of
693 efHTML: begin 700 efHTML: begin
694 tmp := 701 tmp :=
695 ' </tbody>' + CRLF + 702 ' </tbody>' + CRLF +
696 ' </table>' + CRLF + CRLF + 703 ' </table>' + CRLF + CRLF +
697 ' <p>' + CRLF + 704 ' <p>' + CRLF +
698 ' <em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) + 705 ' <em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) +
699 ' by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + CRLF + 706 ' by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + CRLF +
700 ' </p>' + CRLF + CRLF + 707 ' </p>' + CRLF + CRLF +
701 ' </body>' + CRLF + 708 ' </body>' + CRLF +
702 '</html>' + CRLF; 709 '</html>' + CRLF;
703 end; 710 end;
704 efXML: 711 efXML:
705 tmp := '</table>' + CRLF; 712 tmp := '</table>' + CRLF;
706 efLaTeX: 713 efLaTeX:
707 tmp := '\end{tabular}' + CRLF; 714 tmp := '\end{tabular}' + CRLF;
708 efPHPArray: begin 715 efPHPArray: begin
709 tmp := ');' + CRLF; 716 tmp := ');' + CRLF;
710 if radioOutputFile.Checked then 717 if radioOutputFile.Checked then
711 tmp := tmp + '?>'; 718 tmp := tmp + '?>';
712 end; 719 end;
713 else 720 else
714 tmp := ''; 721 tmp := '';
715 end; 722 end;
716 S.WriteString(tmp); 723 S.WriteString(tmp);
717 724
718 if radioOutputCopyToClipboard.Checked then begin 725 if radioOutputCopyToClipboard.Checked then begin
719 // SynEdit's exporter is slow on large strings, see issue #2903 726 // SynEdit's exporter is slow on large strings, see issue #2903
720 if S.Size < 100*SIZE_KB then begin 727 if S.Size < 100*SIZE_KB then begin
721 case ExportFormat of 728 case ExportFormat of
722 efSQLInsert, efSQLReplace: begin 729 efSQLInsert, efSQLReplace: begin
723 Exporter := TSynExporterHTML.Create(Self); 730 Exporter := TSynExporterHTML.Create(Self);
724 Exporter.Highlighter := MainForm.SynSQLSyn1; 731 Exporter.Highlighter := MainForm.SynSQLSyn1;
725 Exporter.ExportAll(Explode(CRLF, S.DataString)); 732 Exporter.ExportAll(Explode(CRLF, S.DataString));
726 HTML := TMemoryStream.Create; 733 HTML := TMemoryStream.Create;
727 Exporter.SaveToStream(HTML); 734 Exporter.SaveToStream(HTML);
728 Exporter.Free; 735 Exporter.Free;
729 end; 736 end;
730 efHTML: HTML := S; 737 efHTML: HTML := S;
731 else HTML := nil; 738 else HTML := nil;
732 end; 739 end;
733 end; 740 end;
734 StreamToClipboard(S, HTML, (ExportFormat=efHTML) and (HTML <> nil)); 741 StreamToClipboard(S, HTML, (ExportFormat=efHTML) and (HTML <> nil));
735 end else begin 742 end else begin
736 try 743 try
737 S.SaveToFile(editFilename.Text); 744 S.SaveToFile(editFilename.Text);
738 except 745 except
739 on E:EFCreateError do begin 746 on E:EFCreateError do begin
740 // Keep form open if file cannot be created 747 // Keep form open if file cannot be created
741 ModalResult := mrNone; 748 ModalResult := mrNone;
742 MainForm.SetProgressState(pbsError); 749 MainForm.SetProgressState(pbsError);
743 ErrorDialog(E.Message); 750 ErrorDialog(E.Message);
744 end; 751 end;
745 end; 752 end;
746 end; 753 end;
747 754
748 Mainform.DisableProgress; 755 Mainform.DisableProgress;
749 Mainform.ShowStatusMsg('Freeing data...'); 756 Mainform.ShowStatusMsg('Freeing data...');
750 FreeAndNil(S); 757 FreeAndNil(S);
751 Mainform.ShowStatusMsg; 758 Mainform.ShowStatusMsg;
752 Screen.Cursor := crDefault; 759 Screen.Cursor := crDefault;
753 end; 760 end;
754 761
755 762
756 end. 763 end.
Powered by Google Project Hosting