DataWizardForm.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using FastReport.Controls;
  2. using FastReport.Data;
  3. using FastReport.Design;
  4. using FastReport.Utils;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. namespace FastReport.Forms
  11. {
  12. internal partial class DataWizardForm : BaseWizardForm
  13. {
  14. private Report report;
  15. private LastConnections connections;
  16. private DataConnectionBase connection;
  17. private bool updating;
  18. private bool editMode;
  19. private enum NodeType { Tables, Procedures, Queries }
  20. #region Properties
  21. public DataConnectionBase Connection
  22. {
  23. get { return connection; }
  24. set
  25. {
  26. if (value != connection)
  27. {
  28. if (connection != null)
  29. {
  30. report.Dictionary.Connections.Remove(connection);
  31. connection.Clear();
  32. }
  33. connection = value;
  34. if (value != null)
  35. {
  36. connections.Add(connection);
  37. report.Dictionary.Connections.Add(connection);
  38. UpdateConnectionsCombo();
  39. }
  40. }
  41. if (value != null)
  42. tbConnString.Text = value.ConnectionString;
  43. UpdateButtons();
  44. }
  45. }
  46. public bool EditMode
  47. {
  48. get { return editMode; }
  49. set
  50. {
  51. editMode = value;
  52. if (value)
  53. {
  54. cbxConnections.Enabled = false;
  55. btnNewConnection.Enabled = false;
  56. tbConnName.Text = Connection.Name;
  57. }
  58. }
  59. }
  60. public override int VisiblePanelIndex
  61. {
  62. get { return base.VisiblePanelIndex; }
  63. set
  64. {
  65. base.VisiblePanelIndex = value;
  66. if (value == 1)
  67. UpdateTree();
  68. }
  69. }
  70. private bool ApplicationConnectionMode
  71. {
  72. get { return Config.DesignerSettings.ApplicationConnection != null; }
  73. }
  74. #endregion
  75. #region First page
  76. private void cbxConnections_DrawItem(object sender, DrawItemEventArgs e)
  77. {
  78. e.DrawBackground();
  79. if (e.Index >= 0)
  80. {
  81. try
  82. {
  83. DataConnectionBase connection = cbxConnections.Items[e.Index] as DataConnectionBase;
  84. this.DrawImageAndText(e, null, connection.GetConnectionId());
  85. }
  86. catch
  87. {
  88. }
  89. }
  90. }
  91. private void btnConnString_Click(object sender, EventArgs e)
  92. {
  93. if (!tbConnString.Visible)
  94. {
  95. btnConnString.Image = GetImage(184);
  96. tbConnString.Visible = true;
  97. }
  98. else
  99. {
  100. btnConnString.Image = GetImage(183);
  101. tbConnString.Visible = false;
  102. }
  103. }
  104. private void UpdateConnectionsCombo()
  105. {
  106. updating = true;
  107. cbxConnections.Items.Clear();
  108. cbxConnections.Items.AddRange(connections.ToArray());
  109. if (cbxConnections.Items.Count > 0)
  110. cbxConnections.SelectedIndex = 0;
  111. updating = false;
  112. }
  113. private void cbxConnections_SelectedIndexChanged(object sender, EventArgs e)
  114. {
  115. if (updating)
  116. return;
  117. updating = true;
  118. Connection = cbxConnections.SelectedItem as DataConnectionBase;
  119. updating = false;
  120. }
  121. private void btnNewConnection_Click(object sender, EventArgs e)
  122. {
  123. using (ConnectionForm form = new ConnectionForm())
  124. {
  125. if (form.ShowDialog() == DialogResult.OK)
  126. Connection = form.Connection;
  127. }
  128. }
  129. private void btnCloudDataSources_Click(object sender, EventArgs e)
  130. {
  131. var connection = CloudCommands.GetDataConnection();
  132. if (connection != null)
  133. {
  134. Connection = connection;
  135. this.tbConnName.Text = connection.Name;
  136. }
  137. }
  138. private void btnEditConnection_Click(object sender, EventArgs e)
  139. {
  140. using (ConnectionForm form = new ConnectionForm())
  141. {
  142. form.Connection = Connection;
  143. form.EditMode = true;
  144. if (form.ShowDialog() == DialogResult.OK)
  145. Connection = form.Connection;
  146. }
  147. }
  148. #endregion
  149. #region Second page
  150. private void ShowWaitMessage()
  151. {
  152. lblWait.Location = new Point(tvTables.Left + 1, tvTables.Top + tvTables.Height / 2 - 32);
  153. lblWait.Size = new Size(tvTables.Width - 2, 32);
  154. lblWait.Visible = true;
  155. lblWait.Refresh();
  156. }
  157. private void HideWaitMessage()
  158. {
  159. lblWait.Visible = false;
  160. }
  161. private void ReadTables(TreeNode node)
  162. {
  163. ShowWaitMessage();
  164. try
  165. {
  166. connection.CreateAllTables(false);
  167. }
  168. catch
  169. {
  170. }
  171. HideWaitMessage();
  172. node.Nodes.Clear();
  173. DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, true, false, false);
  174. }
  175. private void ReadProcedures(TreeNode node)
  176. {
  177. ShowWaitMessage();
  178. try
  179. {
  180. connection.CreateAllProcedures();
  181. }
  182. catch
  183. {
  184. }
  185. HideWaitMessage();
  186. node.Nodes.Clear();
  187. DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, false, false, true);
  188. }
  189. private void ReadQueries(TreeNode node)
  190. {
  191. node.Nodes.Clear();
  192. DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, false, true, false);
  193. }
  194. private bool ProcedureHasInputParameter(ProcedureDataSource proc)
  195. {
  196. foreach (CommandParameter p in proc.Parameters)
  197. {
  198. if (p.Direction == System.Data.ParameterDirection.Input || p.Direction == System.Data.ParameterDirection.InputOutput)
  199. return true;
  200. }
  201. return false;
  202. }
  203. private bool EditProcedureParameters(ProcedureDataSource proc)
  204. {
  205. using (QueryWizardForm form = new QueryWizardForm(proc))
  206. {
  207. form.VisiblePanelIndex = 2;
  208. form.ForParamEdit = true;
  209. form.Text = Res.Get("Forms,DataWizard,ChangeParameters") + proc.Name;
  210. return form.ShowDialog() == DialogResult.OK;
  211. }
  212. }
  213. private bool CheckTableSchema(TreeNode node)
  214. {
  215. if (node.Tag is TableDataSource data && node.Nodes.Count == 1 && node.Nodes[0].Text == "dummy")
  216. {
  217. if (data is ProcedureDataSource proc && ProcedureHasInputParameter(proc))
  218. {
  219. if (!EditProcedureParameters(proc))
  220. return false;
  221. }
  222. try
  223. {
  224. data.InitSchema();
  225. }
  226. catch (Exception ex)
  227. {
  228. FRMessageBox.Error(ex.Message);
  229. }
  230. finally
  231. {
  232. node.Nodes.Clear();
  233. DataTreeHelper.AddColumns(node.Nodes, data.Columns, false, true);
  234. }
  235. }
  236. return true;
  237. }
  238. private void tvTables_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  239. {
  240. updating = true;
  241. if (e.Node.Tag is NodeType t && e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "dummy")
  242. {
  243. if (t == NodeType.Tables)
  244. {
  245. ReadTables(e.Node);
  246. }
  247. else if (t == NodeType.Procedures)
  248. {
  249. ReadProcedures(e.Node);
  250. }
  251. else if (t == NodeType.Queries)
  252. {
  253. ReadQueries(e.Node);
  254. }
  255. }
  256. else
  257. {
  258. if (!CheckTableSchema(e.Node))
  259. e.Cancel = true;
  260. }
  261. updating = false;
  262. }
  263. private void tvTables_AfterCheck(object sender, TreeViewEventArgs e)
  264. {
  265. if (updating)
  266. return;
  267. bool isChecked = e.Node.Checked;
  268. if (e.Node.Tag is NodeType t)
  269. {
  270. if (isChecked)
  271. e.Node.Expand();
  272. // do not check all procedures
  273. if (t == NodeType.Procedures && isChecked)
  274. {
  275. e.Node.Checked = false;
  276. return;
  277. }
  278. foreach (TreeNode node in e.Node.Nodes)
  279. {
  280. node.Checked = isChecked;
  281. }
  282. }
  283. else
  284. {
  285. if (isChecked && !CheckTableSchema(e.Node))
  286. {
  287. updating = true;
  288. e.Node.Checked = false;
  289. updating = false;
  290. }
  291. }
  292. }
  293. private void UpdateTree()
  294. {
  295. updating = true;
  296. tvTables.BeginUpdate();
  297. tvTables.Nodes.Clear();
  298. var res = new MyRes("Forms,DataWizard");
  299. var tablesNode = tvTables.Nodes.Add(res.Get("Tables"));
  300. tablesNode.ImageIndex = tablesNode.SelectedImageIndex = 222;
  301. tablesNode.Tag = NodeType.Tables;
  302. tablesNode.Nodes.Add("dummy");
  303. if (Connection.IsSqlBased)
  304. {
  305. if (Connection.CanContainProcedures)
  306. {
  307. var proceduresNode = tvTables.Nodes.Add(res.Get("Procedures"));
  308. proceduresNode.ImageIndex = proceduresNode.SelectedImageIndex = 52;
  309. proceduresNode.Tag = NodeType.Procedures;
  310. proceduresNode.Nodes.Add("dummy");
  311. }
  312. var queriesNode = tvTables.Nodes.Add(res.Get("Queries"));
  313. queriesNode.ImageIndex = queriesNode.SelectedImageIndex = 222;
  314. queriesNode.Tag = NodeType.Queries;
  315. queriesNode.Nodes.Add("dummy");
  316. }
  317. btnAddQuery.Enabled = Connection.IsSqlBased;
  318. tvTables.EndUpdate();
  319. updating = false;
  320. ExpandExistingNodes();
  321. }
  322. private TreeNode GetNode(NodeType nodeType)
  323. {
  324. foreach (TreeNode node in tvTables.Nodes)
  325. {
  326. if (node.Tag is NodeType t && t == nodeType)
  327. return node;
  328. }
  329. return null;
  330. }
  331. private void ExpandExistingNodes()
  332. {
  333. bool hasTables = false;
  334. bool hasProcedures = false;
  335. bool hasQueries = false;
  336. foreach (TableDataSource table in Connection.Tables)
  337. {
  338. if (!table.Enabled)
  339. continue;
  340. if (table is ProcedureDataSource)
  341. hasProcedures = true;
  342. else if (!string.IsNullOrEmpty(table.SelectCommand))
  343. hasQueries = true;
  344. else
  345. hasTables = true;
  346. }
  347. // expanding a node will load its content
  348. if (hasTables)
  349. GetNode(NodeType.Tables)?.Expand();
  350. if (hasProcedures)
  351. GetNode(NodeType.Procedures)?.Expand();
  352. if (hasQueries)
  353. GetNode(NodeType.Queries)?.Expand();
  354. }
  355. private void btnAddTable_Click(object sender, EventArgs e)
  356. {
  357. TableDataSource table = new TableDataSource();
  358. table.Name = report.Dictionary.CreateUniqueName("Table");
  359. table.Alias = report.Dictionary.CreateUniqueAlias(table.Alias);
  360. table.Connection = Connection;
  361. using (QueryWizardForm form = new QueryWizardForm(table))
  362. {
  363. if (form.ShowDialog() != DialogResult.OK)
  364. table.Dispose();
  365. else
  366. {
  367. var node = GetNode(NodeType.Queries);
  368. ReadQueries(node);
  369. node.Expand();
  370. }
  371. }
  372. }
  373. #endregion
  374. private void DataWizardForm_FormClosed(object sender, FormClosedEventArgs e)
  375. {
  376. Done();
  377. }
  378. private void UpdateButtons()
  379. {
  380. bool enabled = Connection != null;
  381. btnEditConnection.Enabled = enabled;
  382. btnNext.Enabled = enabled;
  383. }
  384. private void ReadConnections()
  385. {
  386. if (Config.DesignerSettings.CustomConnections.Count > 0)
  387. {
  388. connections.Deserialize(Config.DesignerSettings.CustomConnections);
  389. }
  390. else
  391. {
  392. var storage = new StorageService("Designer,LastConnections");
  393. // read last connections
  394. storage.Read(connections);
  395. // write successfully readed connections back
  396. storage.Write(connections);
  397. }
  398. if (connections.Count > 0)
  399. Connection = connections[0];
  400. }
  401. private void Init()
  402. {
  403. if (ApplicationConnectionMode)
  404. {
  405. if (report.Dictionary.Connections.Count == 0)
  406. {
  407. Connection = Activator.CreateInstance(Config.DesignerSettings.ApplicationConnectionType) as DataConnectionBase;
  408. Connection.Name = report.Dictionary.CreateUniqueName("Connection");
  409. }
  410. else
  411. Connection = report.Dictionary.Connections[0];
  412. Connection.ConnectionString = Config.DesignerSettings.ApplicationConnection.ConnectionString;
  413. EditMode = true;
  414. VisiblePanelIndex = 1;
  415. btnPrevious.Visible = false;
  416. btnNext.Visible = false;
  417. }
  418. else
  419. {
  420. VisiblePanelIndex = 0;
  421. tbConnName.Text = report.Dictionary.CreateUniqueName("Connection");
  422. ReadConnections();
  423. }
  424. UpdateButtons();
  425. }
  426. private void Done()
  427. {
  428. if (DialogResult == DialogResult.OK)
  429. {
  430. Connection.Name = tbConnName.Text;
  431. var nodes = new List<TreeNode>();
  432. foreach (TreeNode node in tvTables.Nodes)
  433. {
  434. foreach (TreeNode subNode in node.Nodes)
  435. {
  436. if (subNode.Tag is TableDataSource)
  437. nodes.Add(subNode);
  438. }
  439. }
  440. // enable items that we have checked in the tree
  441. foreach (TreeNode tableNode in nodes)
  442. {
  443. if (tableNode.Tag is TableDataSource table)
  444. {
  445. table.Enabled = tableNode.Checked;
  446. foreach (TreeNode colNode in tableNode.Nodes)
  447. {
  448. if (colNode.Tag is Column column)
  449. column.Enabled = colNode.Checked;
  450. }
  451. }
  452. }
  453. // create relations if any
  454. Connection.CreateRelations();
  455. new StorageService("Designer,LastConnections").Write(connections);
  456. }
  457. if (ApplicationConnectionMode)
  458. {
  459. // in this mode, we don't have to store connection string in the report
  460. Connection.ConnectionString = "";
  461. }
  462. if (DialogResult == DialogResult.OK || EditMode)
  463. connections.Remove(Connection);
  464. connections.Dispose();
  465. }
  466. public override void Localize()
  467. {
  468. base.Localize();
  469. MyRes res = new MyRes("Forms,DataWizard");
  470. Text = res.Get("");
  471. pnDatabase.Text = res.Get("Page1");
  472. pnTables.Text = res.Get("Page2");
  473. lblWhichData.Text = res.Get("WhichData");
  474. lblHint.Text = res.Get("Hint");
  475. btnNewConnection.Text = res.Get("NewConnection");
  476. btnEditConnection.Text = res.Get("EditConnection");
  477. lblConnName.Text = res.Get("ConnectionName");
  478. lblConnString.Text = res.Get("ConnectionString");
  479. lblWhichTables.Text = res.Get("WhichTables");
  480. lblWait.Text = res.Get("Wait");
  481. btnAddQuery.Text = res.Get("AddQuery");
  482. btnCloudDataSources.Text = res.Get("CloudDataSources");
  483. }
  484. public override void UpdateDpiDependencies()
  485. {
  486. base.UpdateDpiDependencies();
  487. lblWhichData.Font = this.LogicalToDevice(new Font(DrawUtils.DefaultFont, FontStyle.Bold), true);
  488. lblWhichTables.Font = lblWhichData.Font;
  489. tbConnString.Height = pnDatabase.Height - tbConnString.Top - 12;
  490. picIcon.Image = GetImage("Images.DataWizard.png");
  491. btnConnString.Image = GetImage(183);
  492. #if AVALONIA
  493. btnConnString.Padding = new Padding(this.LogicalToDevice(-2),0,0,0);
  494. #endif
  495. tvTables.ImageList = GetImages();
  496. }
  497. public DataWizardForm(Report report)
  498. {
  499. this.report = report;
  500. connections = new LastConnections();
  501. CanSaveRestoreState = true;
  502. InitializeComponent();
  503. Localize();
  504. Init();
  505. UIUtils.CheckRTL(this);
  506. UpdateDpiDependencies();
  507. }
  508. private class LastConnections : IDisposable, IFRSerializable
  509. {
  510. private List<DataConnectionBase> connections;
  511. public DataConnectionBase this[int index]
  512. {
  513. get { return connections[index]; }
  514. }
  515. public int Count
  516. {
  517. get { return connections.Count; }
  518. }
  519. public void Serialize(FRWriter writer)
  520. {
  521. List<string> savedConnections = new List<string>();
  522. writer.ItemName = "LastConnections";
  523. foreach (DataConnectionBase connection in connections)
  524. {
  525. if (savedConnections.IndexOf(connection.GetConnectionId()) == -1)
  526. {
  527. writer.SaveChildren = false;
  528. writer.Write(connection);
  529. savedConnections.Add(connection.GetConnectionId());
  530. }
  531. }
  532. }
  533. public void Deserialize(FRReader reader)
  534. {
  535. while (reader.NextItem())
  536. {
  537. try
  538. {
  539. DataConnectionBase connection = reader.Read() as DataConnectionBase;
  540. if (connection != null)
  541. connections.Add(connection);
  542. }
  543. catch
  544. {
  545. }
  546. }
  547. }
  548. public void Deserialize(List<ConnectionEntry> connections)
  549. {
  550. foreach (ConnectionEntry ce in connections)
  551. {
  552. try
  553. {
  554. DataConnectionBase connection =
  555. Activator.CreateInstance(ce.type) as DataConnectionBase;
  556. connection.ConnectionString = ce.connectionString;
  557. this.connections.Add(connection);
  558. }
  559. catch
  560. {
  561. }
  562. }
  563. }
  564. public void Add(DataConnectionBase connection)
  565. {
  566. for (int i = 0; i < connections.Count; i++)
  567. {
  568. try
  569. {
  570. if (connections[i].GetConnectionId() == connection.GetConnectionId())
  571. {
  572. connections.RemoveAt(i);
  573. break;
  574. }
  575. }
  576. catch
  577. {
  578. }
  579. }
  580. connections.Insert(0, connection);
  581. }
  582. public void Remove(DataConnectionBase connection)
  583. {
  584. if (connections.Contains(connection))
  585. connections.Remove(connection);
  586. }
  587. public DataConnectionBase[] ToArray()
  588. {
  589. return connections.ToArray();
  590. }
  591. public void Dispose()
  592. {
  593. foreach (DataConnectionBase connection in connections)
  594. {
  595. connection.Dispose();
  596. }
  597. connections.Clear();
  598. }
  599. public LastConnections()
  600. {
  601. connections = new List<DataConnectionBase>();
  602. }
  603. }
  604. }
  605. }