-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmain.cs
644 lines (560 loc) · 20.9 KB
/
fmain.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TDEngineClient.Entity;
using TDEngineClient.Helper;
using TDEngineClient.Services;
using static TDEngineClient.Services.MyService;
namespace TDEngineClient
{
public partial class fmain : Form
{
public fmain()
{
MyConfig = FileHelper.GetConfig();//读取配置文件
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitailForm();
//Thread.CurrentThread.CurrentUICulture = new CultureInfo(MyConfig.System.Language);//多语言设置
SetLanguage(MyConfig.System.Language);//设置语言
}
private void TextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5) //按F5时执行SQL语句
{
RunSql();
}
else if (e.KeyCode == Keys.Escape)
{
TipBox.Visible = false;
}
else if (e.KeyCode == Keys.Up)
{
if (TipBox.Visible && TipBox.CurrentCell!=null)
{
e.SuppressKeyPress = true;//取消输入
if (TipBox.CurrentCell.RowIndex > 0)
{
TipBox.CurrentCell = TipBox.Rows[TipBox.CurrentCell.RowIndex - 1].Cells[0];
}
else
{
TipBox.CurrentCell = TipBox.Rows[TipBox.Rows.Count-1].Cells[0];
}
}
}
else if (e.KeyCode == Keys.Down)
{
if (TipBox.Visible && TipBox.CurrentCell != null)
{
e.SuppressKeyPress = true;//取消输入
if (TipBox.CurrentCell.RowIndex < TipBox.Rows.Count - 1)
{
TipBox.CurrentCell = TipBox.Rows[TipBox.CurrentCell.RowIndex + 1].Cells[0];
}
else if (TipBox.CurrentCell.RowIndex == TipBox.Rows.Count - 1)
{
TipBox.CurrentCell = TipBox.Rows[0].Cells[0];
}
}
}
else if (e.KeyCode == Keys.Enter)
{
if (TipBox.Visible && TipBox.CurrentCell !=null)
{
e.SuppressKeyPress = true;//取消输入
//ReplaceInputText((sender as RichBox), TipBox.Items[TipBox.SelectedIndex].ToString());
ReplaceInputText((sender as RichBox), TipBox.CurrentCell.Value.ToString());
TipBox.Visible = false;
}
}
else if(e.KeyCode ==Keys.F1)//帮助
{
System.Diagnostics.Process.Start("explorer.exe", "https://docs.taosdata.com/reference/");
}
else
{
ts3.Text = "Text Length " + (sender as RichBox).Text.Length.ToString();
}
}
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
ShowTipBox(sender as RichBox, e.KeyCode); //显示智能提示框
}
private void TextBoxTextChanged(object sender, EventArgs e)
{
if (DisableTextChanged) return;
RefreshColors(sender as RichBox); //
}
private void TextBoxClick(object sender, KeyEventArgs e)
{
ShowTipBox(sender as RichBox, e.KeyCode); //显示智能提示框
}
private void TextBoxMouseClick(object sender, MouseEventArgs e)
{
if ((DateTime.Now - LastDbClickTime).TotalMilliseconds < 1000 && sender is RichBox)//触发三连击事件
{
var tbox = sender as RichBox;
var txt = tbox.Text;
if (txt.Length == 0) return;
var pos = tbox.SelectionStart;
var lineStart = tbox.GetFirstCharIndexOfCurrentLine(); //行首
var lineEnd = tbox.Text.IndexOf(Environment.NewLine, lineStart);//行尾
if (lineEnd < lineStart) lineEnd = tbox.Text.Length;
var leftPos = txt.Substring(0, pos).LastIndexOf("'") + 1; //左边引号位置
if (leftPos < lineStart) leftPos = lineStart; //不越过当前行
var rightPos = txt.Substring(pos).IndexOf("'") + pos; //右边引号位置
if (leftPos== lineStart || rightPos < leftPos || rightPos>lineEnd) rightPos = lineEnd; //不越过当前行
tbox.Select(leftPos, rightPos-leftPos);
}
}
private void TextBoxMouseDoubleClick(object sender, MouseEventArgs e)
{
LastDbClickTime = DateTime.Now; //记录最后双击时间
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
//if(item.Type == NodeItemType.Server)
var node = (sender as TreeView).SelectedNode;
if (node.Tag is Server) //服务器
{
if (node.Nodes.Count == 0)
OpenDB(node);
}
else if (node.Tag is StableDto)//超级表
{
var db = GetNodeDb(node);
var stable = (node.Tag as StableDto);
CreateTable(db.Account, $"{db.Name}.{stable.stable_name}");
}
else if (node.Tag is TableDto) //表
{
var db = GetNodeDb(node);
var table = (node.Tag as TableDto);
var tableName = $"{db.Name}.{table.table_name}";
CreateTable(db.Account, tableName);
}
}
//private void btnNext_Click(object sender, EventArgs e)
//{
// var type = (TurnPageType)Convert.ToInt32((sender as Button).Tag);
// TurnPage(tabControl1.SelectedTab, type);
//}
private void m_table_Click(object sender, EventArgs e)
{
CreateQueryResult(TabPageType.Tables); //显示表
}
private void m_field_Click(object sender, EventArgs e)
{
CreateQueryResult(TabPageType.Describe); //显示字段
}
private void m_query_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode == null) return;
var item = GetNodeItem(treeView1.SelectedNode);
CreateQueryWindow(item.Db, item.STable, item.Table); //创建SQL窗口
}
//private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
//{
// if (e.KeyCode == Keys.Enter)
// {
// if (int.TryParse(numericUpDown1.Value.ToString(), out int num))
// {
// TurnPage(tabControl1.SelectedTab, TurnPageType.ToPage,num);
// }
// }
//}
private void m_opensvr_Click(object sender, EventArgs e)
{
var node = treeView1.SelectedNode;
if (node?.Tag is Server)
{
OpenDB(node);
}
}
private void m_closesvr_Click(object sender, EventArgs e)
{
var node = treeView1.SelectedNode;
if (node?.Tag is Server)
{
node.Nodes.Clear();
node.ImageIndex = 0;
node.SelectedImageIndex = 0;
}
}
private void m_record_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
if (tbox == null) return;
var fieldStr = tbox.SelectedText.Length > 2 ? tbox.SelectedText : tbox.Text;
ftool frm2 = new ftool(fieldStr);
frm2.ShowDialog();
tbox.AppendText(frm2.Sql);
}
private void m_run_Click(object sender, EventArgs e)
{
RunSql();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var mylist = new List<TQueryBox>();
foreach ( var tab in tabControl1.TabPages)
{
if (tab is TabPage && (tab as TabPage).Text.Contains(CAPTION_QUERY))
{
var tabText = new TQueryBox();
tabText.Caption=(tab as TabPage).Text;
foreach (var ctl in (tab as TabPage).Controls)
{
if (ctl is RichBox)
{
if ((ctl as RichBox).Tag is QueryBox)
{
tabText.AccountServer = ((ctl as RichBox).Tag as QueryBox).Server.IP;
}
string sTxt = "";
foreach (string s in (ctl as RichBox).Lines)
{
sTxt = sTxt + s + "\\r\\n";//每行末尾都加一个回车 \r\n
}
tabText.Text = sTxt;
break;
}
}
mylist.Add(tabText);
}
}
FileHelper.SaveQueriesToIni(mylist);
}
/// <summary>
/// 设置右键菜单显示项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var node = (sender as TreeView).SelectedNode;
if (node == null)
{
SetMenu(new List<ToolStripMenuItem> {m_newsvr});
}
else if (node.Tag is Server) //服务器
{
SetMenu(new List<ToolStripMenuItem> {m_newsvr,m_editsvr,m_deletesvr, m_opensvr, m_closesvr, m_createdb }, new List<ToolStripSeparator> { sp1 });
}
else if (node.Tag is DataBaseDto)//数据库
{
SetMenu(new List<ToolStripMenuItem> { m_dropdb,m_createsuper,m_createtable, m_refresh, m_query },new List<ToolStripSeparator> { sp2,sp4});
}
else if (node.Tag is StableDto)//超级表
{
SetMenu(new List<ToolStripMenuItem> { m_createtable, m_table,m_field, m_export, m_import,m_imports, m_query },new List<ToolStripSeparator> {sp3, sp4 });
//var db = GetNodeDb(node);
//var stable = (node.Tag as StableDto);
}
else if (node.Tag is TableDto) //表
{
SetMenu(new List<ToolStripMenuItem> { m_field, m_droptable,m_export,m_import,m_imports, m_query }, new List<ToolStripSeparator> {sp3, sp4 });
//var db = GetNodeDb(node);
//var table = (node.Tag as TableDto);
//var tableName = $"{db.Name}.{table.table_name}";
}
}
}
/// <summary>
/// 创建删除的操作命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void m_command_Click(object sender, EventArgs e)
{
if (sender is ToolStripMenuItem && int.TryParse((sender as ToolStripMenuItem).Tag.ToString(),out int cmd))
{
var item = GetNodeItem(treeView1.SelectedNode);
CreateQueryWindow((SqlCommandType)cmd, item.Server, item.Db, item.STable, item.Table);
}
}
private void spInner_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void m_newsvr_Click(object sender, EventArgs e)
{
fserver svr = new fserver(MyConfig.System.Language);
if (svr.ShowDialog() == DialogResult.OK)
{
TreeNode item = new TreeNode();
item.Text = svr.Server.IP + (string.IsNullOrEmpty(svr.Server.AliasName) ? "" : "(" + svr.Server.AliasName + ")");
item.Tag = svr.Server;
item.ImageIndex = 0;
treeView1.Nodes.Add(item);
SaveConfigServers();
}
}
private void m_editsvr_Click(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
fserver svr = new fserver(MyConfig.System.Language,item.Server);
if (svr.ShowDialog() == DialogResult.OK)
{
item.Server = svr.Server;
item.Node.Text = svr.Server.IP+(string.IsNullOrEmpty(svr.Server.AliasName)?"": "("+ svr.Server.AliasName + ")");
SaveConfigServers();
}
}
private void m_deletesvr_Click(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
if (MessageBox.Show($"Confirm to Delete {item.Server.IP}?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
treeView1.SelectedNode.Remove();
SaveConfigServers();
}
}
private void explorerToolStripMenuItem_Click(object sender, EventArgs e)
{
explorerToolStripMenuItem.Checked = !explorerToolStripMenuItem.Checked;
spMain.Panel1Collapsed = !explorerToolStripMenuItem.Checked;
}
/// <summary>
/// 导入表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void m_impor_tables_Click(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
var dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
ImportTable(item, dlg.SelectedPath);
}
}
/// <summary>
/// 导入超级表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void m_impor_stable_Click(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
var dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
ImportStable(item, dlg.FileName);
}
}
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void m_export_Click(object sender, EventArgs e)
{
var item = GetNodeItem(treeView1.SelectedNode);
var exform = new fexport(item);
if (exform.ShowDialog() == DialogResult.OK)
{
ExportTable(item, exform.FolderName, exform.TableNames);
}
}
private void m_copy_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
//if(!string.IsNullOrEmpty(tbox?.SelectedText))
// Clipboard.SetText(tbox?.SelectedText);
tbox?.Copy();
}
private void m_paste_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
tbox?.Paste();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
tbox?.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
tbox?.Paste();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
tbox.SelectAll();
break;
}
}
tbox?.SelectAll();
}
private void newConnectionToolStripMenuItem_Click(object sender, EventArgs e)
{
m_newsvr_Click(sender, e);
}
private void closeServerToolStripMenuItem_Click(object sender, EventArgs e)
{
m_closesvr_Click(sender, e);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void newQueryToolStripMenuItem_Click(object sender, EventArgs e)
{
m_query_Click(sender, e);
}
private void m_case_Click(object sender, EventArgs e)
{
ChangeCase(false);
}
private void m_ucase_Click(object sender, EventArgs e)
{
ChangeCase(true);
}
private void m_about_Click(object sender, EventArgs e)
{
var info =$"{Application.ProductName}\r {Application.ProductVersion}\r {Application.CompanyName}";
MessageBox.Show(info, "About", MessageBoxButtons.OK);
}
private void m_en_Click(object sender, EventArgs e)
{
m_en.Checked = true;
m_cn.Checked = false;
SetLanguage(0);//英文
}
private void m_cn_Click(object sender, EventArgs e)
{
m_en.Checked = false;
m_cn.Checked = true;
SetLanguage(1);//中文
}
private void m_beauti_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
if (tbox != null && !string.IsNullOrEmpty(tbox.SelectedText))
{
var text = BeautiSql(tbox.SelectedText);
tbox.SelectedText = text;
}
RefreshColors(tbox);//刷新字体显示
}
private void m_notbeauti_Click(object sender, EventArgs e)
{
var tp = tabControl1.SelectedTab;
if (tp == null) return;
RichBox tbox = null;
foreach (var ctl in tp.Controls)
{
if (ctl is RichBox)
{
tbox = (ctl as RichBox);
break;
}
}
if (tbox != null && !string.IsNullOrEmpty(tbox.SelectedText))
{
var text = BeautiSql(tbox.SelectedText,true);
tbox.SelectedText = text;
}
RefreshColors(tbox);//刷新字体显示
}
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
var opForm = new foption(MyConfig.System);
if (opForm.ShowDialog() == DialogResult.OK)
{
MyConfig.System = opForm.Options;
FileHelper.SaveSystemConfig(MyConfig.System);//保存到配置文件
}
}
private void m_refresh_Click(object sender, EventArgs e)
{
var node = treeView1.SelectedNode;
if (node?.Tag is DataBaseDto)
{
RefreshDB();
}
}
}
}