forked from olejorgensen/CompactView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportForm.cs
440 lines (390 loc) · 17.4 KB
/
ExportForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**************************************************************************
Copyright (C) 2011-2014 Iván Costales Suárez
This file is part of CompactView.
CompactView is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompactView is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompactView. If not, see <http://www.gnu.org/licenses/>.
CompactView web site <http://sourceforge.net/p/compactview/>.
**************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace CompactView
{
public partial class ExportForm : Form
{
private SqlCeDb db;
private TreeNode dbNode;
public ExportForm(SqlCeDb sqlCeDb, TreeNode dbNode)
{
InitializeComponent();
SetCultureTexts();
db = sqlCeDb;
this.dbNode = dbNode;
if (dbNode.ImageIndex == 1)
saveFileDialog1.Filter = saveFileDialog1.Filter.Replace("All files|*.*", "CSV files|*.csv|All files|*.*");
int nItems = FillTreeDb();
int maxHeight = Screen.PrimaryScreen.WorkingArea.Height - 20;
int height = nItems * treeDb.ItemHeight + 90;
if (height < 400)
height = 400;
if (height > maxHeight)
height = maxHeight;
Height = height;
var doubleClickIntercept = new DoubleClickIntercept(treeDb.Handle); // Disable double click to avoid TreeView double click check bug
}
private void SetCultureTexts()
{
Text = GlobalText.GetValue("Export");
btnExport.Text = GlobalText.GetValue("Export");
rbAll.Text = GlobalText.GetValue("All");
rbSchema.Text = GlobalText.GetValue("OnlySchema");
rbData.Text = GlobalText.GetValue("OnlyData");
}
private int FillTreeDb()
{
string tableName = dbNode.ImageIndex == 0 ? null : dbNode.Text;
treeDb.Nodes.Clear();
treeDb.Nodes.AddRange(db.GetSchemaNodes(tableName));
treeDb.ExpandAll();
return GetTotalNodes(treeDb.Nodes);
}
private int GetTotalNodes(TreeNodeCollection nodes)
{
int rootNodes = nodes.Count;
foreach (TreeNode node in nodes)
rootNodes += GetTotalNodes(node.Nodes);
return rootNodes;
}
private void RemoveTable(List<string> ddl, string tableName)
{
int pos = ddl.FindIndex(cmd => cmd.StartsWith($"CREATE TABLE [{tableName}]"));
if (pos >= 0)
ddl.RemoveAt(pos);
while ((pos = ddl.FindIndex(cmd => cmd.StartsWith($"ALTER TABLE [{tableName}]"))) >= 0)
ddl.RemoveAt(pos);
string pattern = $@"^CREATE.* INDEX.* ON \[{tableName}\]";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern))) >= 0)
ddl.RemoveAt(pos);
pattern = $@"^ALTER TABLE.* ADD CONSTRAINT.* FOREIGN KEY.* REFERENCES \[{tableName}\]";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern, RegexOptions.Singleline))) >= 0)
ddl.RemoveAt(pos);
}
private void RemoveField(List<string> ddl, string tableName, string fieldName)
{
int pos = ddl.FindIndex(cmd => cmd.StartsWith($"CREATE TABLE [{tableName}]"));
if (pos >= 0)
{
ddl[pos] = Regex.Replace(ddl[pos], $@"^\s*\[{fieldName}\].*\r\n", string.Empty, RegexOptions.Multiline);
ddl[pos] = Regex.Replace(ddl[pos], @",\r\n\)$", "\r\n)");
}
string pattern = $@"^ALTER TABLE \[{tableName}\].* PRIMARY KEY (.*\[{fieldName}\].*)";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern))) >= 0)
ddl.RemoveAt(pos);
pattern = $@"^CREATE.* INDEX.* ON \[{tableName}\] \(.*\[{fieldName}\].*\)";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern))) >= 0)
ddl.RemoveAt(pos);
pattern = $@"^ALTER TABLE \[{tableName}\] ADD CONSTRAINT.* FOREIGN KEY \(.*\[{fieldName}\].*\)";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern))) >= 0)
ddl.RemoveAt(pos);
pattern = $@"^ALTER TABLE.* ADD CONSTRAINT.* FOREIGN KEY.* REFERENCES \[{tableName}\] \(.*\[{fieldName}\].*\)";
while ((pos = ddl.FindIndex(cmd => Regex.IsMatch(cmd, pattern, RegexOptions.Singleline))) >= 0)
ddl.RemoveAt(pos);
}
private string RemoveUncheck(List<string> ddl, TreeView treeView)
{
foreach (TreeNode node in treeView.Nodes)
{
if (node.Checked)
{
foreach (TreeNode n in node.Nodes)
if (!n.Checked)
RemoveField(ddl, node.Text, n.Text);
}
else
{
RemoveTable(ddl, node.Text);
}
}
var sb = new StringBuilder();
foreach (string s in ddl)
sb.AppendLine($"{s};{Environment.NewLine}");
return sb.ToString();
}
private string GetExportTablesSchema(SqlCeDb db, TreeView treeView)
{
var ddl = new List<string>(Regex.Split(db.GetDatabaseDdl(), @";\s*").Where(s => !string.IsNullOrWhiteSpace(s)));
for (int i = ddl.Count - 1; i >= 0; i--)
if (!ddl[i].StartsWith("CREATE TABLE "))
ddl.RemoveAt(i);
return RemoveUncheck(ddl, treeView);
}
private string GetExportConstraintsSchema(SqlCeDb db, TreeView treeView)
{
var ddl = new List<string>(Regex.Split(db.GetDatabaseDdl(), @";\s*").Where(s => !string.IsNullOrWhiteSpace(s)));
for (int i = ddl.Count - 1; i >= 0; i--)
if (ddl[i].StartsWith("CREATE TABLE "))
ddl.RemoveAt(i);
return RemoveUncheck(ddl, treeView);
}
private string ChangeIdentity(string tablesDdl)
{
var ddl = new List<string>(Regex.Split(tablesDdl, @";\s*").Where(s => !string.IsNullOrWhiteSpace(s)));
for (int i = 0; i < ddl.Count; i++)
{
string identity = null;
Match match = Regex.Match(ddl[i], @"(?<=^CREATE TABLE \[)[^\]]*(?=\])", RegexOptions.Singleline);
if (match.Success)
identity = db.GetAutoincNext(match.Value);
if (identity != null)
ddl[i] = Regex.Replace(ddl[i], @"(?<=\r\n\ *\[.*\].* IDENTITY \()\d+(?=,)", identity, RegexOptions.Singleline);
}
var sb = new StringBuilder();
foreach (string s in ddl)
sb.AppendLine($"{s};{Environment.NewLine}");
return sb.ToString();
}
private string GetExportData(SqlCeDb db, TreeView treeView)
{
var ddl = new StringBuilder();
foreach (TreeNode node in treeView.Nodes)
{
var sb = new StringBuilder();
int count = 0;
if (node.Checked)
foreach (TreeNode n in node.Nodes)
{
if (n.Checked)
{
sb.Append($"[{n.Text}], ");
count++;
}
}
if (count == 0)
continue;
string schema = db.GetTableDdl(node.Text, true, false, false, false);
string identity = db.GetAutoincNext(node.Text);
if (identity != null)
ddl.AppendLine($"SET IDENTITY_INSERT [{node.Text}] ON;{Environment.NewLine}");
string fields = sb.ToString().TrimEnd(',', ' ');
string sqlSelect = $"SELECT {fields} FROM [{node.Text}]";
var dr = (IDataReader)db.ExecuteSql(sqlSelect, false);
object[] values = new object[count];
while (dr.Read())
{
ddl.Append($"INSERT INTO [{node.Text}] ({fields}) VALUES (");
dr.GetValues(values);
for (int i = 0; i < count; i++)
{
string s;
if (dr.IsDBNull(i))
{
s = "NULL";
}
else
{
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Type t = values[i].GetType();
bool numeric = t == typeof(Byte) || t == typeof(Int16) || t == typeof(Int32) ||
t == typeof(Int64) || t == typeof(Decimal) || t == typeof(Double) || t == typeof(Single);
if (t == typeof(Byte[]))
{
s = $"0x{BitConverter.ToString(values[i] as Byte[]).Replace("-", string.Empty)}";
}
else if (t == typeof(DateTime))
{
s = $"'{((DateTime)values[i]).ToString("yyyy.MM.dd HH:mm:ss.fff")}'";
}
else
{
s = numeric ? values[i].ToString() : $"'{values[i].ToString().Replace("'", "''")}'";
}
Thread.CurrentThread.CurrentCulture = ci;
}
ddl.Append(s);
if (i < count - 1)
ddl.Append(", ");
}
ddl.AppendLine($");{Environment.NewLine}");
}
dr.Close();
if (identity != null)
ddl.AppendLine($"SET IDENTITY_INSERT [{node.Text}] OFF;{Environment.NewLine}");
}
return ddl.ToString();
}
private string GetCsvExportData(SqlCeDb db, TreeView treeView, bool titles, bool data)
{
int ncount = 0;
foreach (TreeNode node in treeView.Nodes)
if (node.Checked)
ncount++;
if (ncount != 1)
return null;
var csv = new StringBuilder();
foreach (TreeNode node in treeView.Nodes)
{
var sb = new StringBuilder();
var sbTitles = new StringBuilder();
int count = 0;
if (node.Checked)
foreach (TreeNode n in node.Nodes)
{
if (n.Checked)
{
sbTitles.Append($"\"{n.Text}\",");
sb.Append($"[{n.Text}], ");
count++;
}
}
if (count == 0)
continue;
if (titles)
csv.AppendLine(sbTitles.ToString().TrimEnd(','));
if (!data)
continue;
string fields = sb.ToString().TrimEnd(',', ' ');
string sqlSelect = $"SELECT {fields} FROM [{node.Text}]";
var dr = (IDataReader)db.ExecuteSql(sqlSelect, false);
object[] values = new object[count];
while (dr.Read())
{
dr.GetValues(values);
for (int i = 0; i < count; i++)
{
string s;
if (dr.IsDBNull(i))
{
s = string.Empty;
}
else
{
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Type t = values[i].GetType();
bool numeric = t == typeof(Byte) || t == typeof(Int16) || t == typeof(Int32) ||
t == typeof(Int64) || t == typeof(Decimal) || t == typeof(Double) || t == typeof(Single);
if (t == typeof(Byte[]))
{
s = $"0x{BitConverter.ToString(values[i] as Byte[]).Replace("-", string.Empty)}";
}
else if (t == typeof(DateTime))
{
s = ((DateTime)values[i]).ToString("yyyy.MM.dd HH:mm:ss.fff");
}
else
{
s = numeric ? values[i].ToString() : values[i].ToString().Replace("\"", "''");
}
Thread.CurrentThread.CurrentCulture = ci;
}
csv.Append($"\"{s}\"");
if (i < count - 1)
csv.Append(",");
}
csv.AppendLine();
}
dr.Close();
}
return csv.ToString();
}
private void btnExport_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
saveFileDialog1.InitialDirectory = Path.GetDirectoryName(db.FileName);
string s = dbNode.ImageIndex == 0 ? string.Empty : $"_{dbNode.Text}";
saveFileDialog1.FileName = $"{Path.GetFileNameWithoutExtension(db.FileName)}{s}_export.sql";
}
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
var writer = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8);
if (dbNode.ImageIndex == 1 && saveFileDialog1.FilterIndex == 3) // CSV
{
writer.Write(GetCsvExportData(db, treeDb, rbAll.Checked || rbSchema.Checked, rbAll.Checked || rbData.Checked));
}
else
{
if (rbAll.Checked || rbSchema.Checked)
{
string ddl = GetExportTablesSchema(db, treeDb);
if (rbAll.Checked)
writer.Write(ChangeIdentity(ddl));
else
writer.Write(ddl);
}
if (rbAll.Checked || rbData.Checked)
writer.Write(GetExportData(db, treeDb));
if (rbAll.Checked || rbSchema.Checked)
writer.Write(GetExportConstraintsSchema(db, treeDb));
}
writer.Close();
Close();
}
private void treeDb_AfterCheck(object sender, TreeViewEventArgs e)
{
foreach (TreeNode node in e.Node.Nodes)
node.Checked = e.Node.Checked;
if (e.Node.Checked && e.Node.Parent != null)
{
treeDb.AfterCheck -= treeDb_AfterCheck;
e.Node.Parent.Checked = true;
treeDb.AfterCheck += treeDb_AfterCheck;
}
}
private void treeDb_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true; // Disable collapse
}
private void treeDb_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true; // Disable select to allow hide selection
}
private void treeDb_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Parent == null || !e.Node.Checked)
return;
TreeNode[] nodes = treeDb.Nodes.Find($"{e.Node.Parent.Text}@{e.Node.Text}", true);
if (nodes.Length <= 0 || nodes[0].Parent == null)
return;
bool used = false;
for (int i = 0; i < nodes.Length; i++)
if (nodes[i].Checked)
used = true;
if (!used)
return;
string txt = $"[{e.Node.Parent.Text}]-[{e.Node.Text}] {GlobalText.GetValue("UsedAsAForeignKey")}{Environment.NewLine}[{nodes[0].Parent.Text}]-[{nodes[0].Text}].";
GlobalText.ShowWarning(txt);
}
}
class DoubleClickIntercept : System.Windows.Forms.NativeWindow
{
public DoubleClickIntercept(IntPtr hWnd)
{
AssignHandle(hWnd);
}
protected override void WndProc(ref Message m)
{
const int WM_LBUTTONDBLCLK = 0x203;
if (m.Msg == WM_LBUTTONDBLCLK)
m.Result = IntPtr.Zero;
else
base.WndProc(ref m);
}
}
}