123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- using FastReport.Controls;
- using FastReport.Data;
- using FastReport.Design;
- using FastReport.Utils;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace FastReport.Forms
- {
- internal partial class DataWizardForm : BaseWizardForm
- {
- private Report report;
- private LastConnections connections;
- private DataConnectionBase connection;
- private bool updating;
- private bool editMode;
- private enum NodeType { Tables, Procedures, Queries }
- #region Properties
- public DataConnectionBase Connection
- {
- get { return connection; }
- set
- {
- if (value != connection)
- {
- if (connection != null)
- {
- report.Dictionary.Connections.Remove(connection);
- connection.Clear();
- }
- connection = value;
- if (value != null)
- {
- connections.Add(connection);
- report.Dictionary.Connections.Add(connection);
- UpdateConnectionsCombo();
- }
- }
- if (value != null)
- tbConnString.Text = value.ConnectionString;
- UpdateButtons();
- }
- }
- public bool EditMode
- {
- get { return editMode; }
- set
- {
- editMode = value;
- if (value)
- {
- cbxConnections.Enabled = false;
- btnNewConnection.Enabled = false;
- tbConnName.Text = Connection.Name;
- }
- }
- }
- public override int VisiblePanelIndex
- {
- get { return base.VisiblePanelIndex; }
- set
- {
- base.VisiblePanelIndex = value;
- if (value == 1)
- UpdateTree();
- }
- }
- private bool ApplicationConnectionMode
- {
- get { return Config.DesignerSettings.ApplicationConnection != null; }
- }
- #endregion
- #region First page
- private void cbxConnections_DrawItem(object sender, DrawItemEventArgs e)
- {
- e.DrawBackground();
- if (e.Index >= 0)
- {
- try
- {
- DataConnectionBase connection = cbxConnections.Items[e.Index] as DataConnectionBase;
- this.DrawImageAndText(e, null, connection.GetConnectionId());
- }
- catch
- {
- }
- }
- }
- private void btnConnString_Click(object sender, EventArgs e)
- {
- if (!tbConnString.Visible)
- {
- btnConnString.Image = GetImage(184);
- tbConnString.Visible = true;
- }
- else
- {
- btnConnString.Image = GetImage(183);
- tbConnString.Visible = false;
- }
- }
- private void UpdateConnectionsCombo()
- {
- updating = true;
- cbxConnections.Items.Clear();
- cbxConnections.Items.AddRange(connections.ToArray());
- if (cbxConnections.Items.Count > 0)
- cbxConnections.SelectedIndex = 0;
- updating = false;
- }
- private void cbxConnections_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (updating)
- return;
- updating = true;
- Connection = cbxConnections.SelectedItem as DataConnectionBase;
- updating = false;
- }
- private void btnNewConnection_Click(object sender, EventArgs e)
- {
- using (ConnectionForm form = new ConnectionForm())
- {
- if (form.ShowDialog() == DialogResult.OK)
- Connection = form.Connection;
- }
- }
- private void btnCloudDataSources_Click(object sender, EventArgs e)
- {
- var connection = CloudCommands.GetDataConnection();
- if (connection != null)
- {
- Connection = connection;
- this.tbConnName.Text = connection.Name;
- }
- }
- private void btnEditConnection_Click(object sender, EventArgs e)
- {
- using (ConnectionForm form = new ConnectionForm())
- {
- form.Connection = Connection;
- form.EditMode = true;
- if (form.ShowDialog() == DialogResult.OK)
- Connection = form.Connection;
- }
- }
- #endregion
- #region Second page
- private void ShowWaitMessage()
- {
- lblWait.Location = new Point(tvTables.Left + 1, tvTables.Top + tvTables.Height / 2 - 32);
- lblWait.Size = new Size(tvTables.Width - 2, 32);
- lblWait.Visible = true;
- lblWait.Refresh();
- }
- private void HideWaitMessage()
- {
- lblWait.Visible = false;
- }
- private void ReadTables(TreeNode node)
- {
- ShowWaitMessage();
- try
- {
- connection.CreateAllTables(false);
- }
- catch
- {
- }
- HideWaitMessage();
- node.Nodes.Clear();
- DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, true, false, false);
- }
- private void ReadProcedures(TreeNode node)
- {
- ShowWaitMessage();
- try
- {
- connection.CreateAllProcedures();
- }
- catch
- {
- }
- HideWaitMessage();
- node.Nodes.Clear();
- DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, false, false, true);
- }
- private void ReadQueries(TreeNode node)
- {
- node.Nodes.Clear();
- DataTreeHelper.CreateDataTree(report.Dictionary, Connection, node.Nodes, false, true, false);
- }
- private bool ProcedureHasInputParameter(ProcedureDataSource proc)
- {
- foreach (CommandParameter p in proc.Parameters)
- {
- if (p.Direction == System.Data.ParameterDirection.Input || p.Direction == System.Data.ParameterDirection.InputOutput)
- return true;
- }
- return false;
- }
- private bool EditProcedureParameters(ProcedureDataSource proc)
- {
- using (QueryWizardForm form = new QueryWizardForm(proc))
- {
- form.VisiblePanelIndex = 2;
- form.ForParamEdit = true;
- form.Text = Res.Get("Forms,DataWizard,ChangeParameters") + proc.Name;
- return form.ShowDialog() == DialogResult.OK;
- }
- }
- private bool CheckTableSchema(TreeNode node)
- {
- if (node.Tag is TableDataSource data && node.Nodes.Count == 1 && node.Nodes[0].Text == "dummy")
- {
- if (data is ProcedureDataSource proc && ProcedureHasInputParameter(proc))
- {
- if (!EditProcedureParameters(proc))
- return false;
- }
- try
- {
- data.InitSchema();
- }
- catch (Exception ex)
- {
- FRMessageBox.Error(ex.Message);
- }
- finally
- {
- node.Nodes.Clear();
- DataTreeHelper.AddColumns(node.Nodes, data.Columns, false, true);
- }
- }
- return true;
- }
- private void tvTables_BeforeExpand(object sender, TreeViewCancelEventArgs e)
- {
- updating = true;
- if (e.Node.Tag is NodeType t && e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "dummy")
- {
- if (t == NodeType.Tables)
- {
- ReadTables(e.Node);
- }
- else if (t == NodeType.Procedures)
- {
- ReadProcedures(e.Node);
- }
- else if (t == NodeType.Queries)
- {
- ReadQueries(e.Node);
- }
- }
- else
- {
- if (!CheckTableSchema(e.Node))
- e.Cancel = true;
- }
- updating = false;
- }
- private void tvTables_AfterCheck(object sender, TreeViewEventArgs e)
- {
- if (updating)
- return;
- bool isChecked = e.Node.Checked;
- if (e.Node.Tag is NodeType t)
- {
- if (isChecked)
- e.Node.Expand();
- // do not check all procedures
- if (t == NodeType.Procedures && isChecked)
- {
- e.Node.Checked = false;
- return;
- }
- foreach (TreeNode node in e.Node.Nodes)
- {
- node.Checked = isChecked;
- }
- }
- else
- {
- if (isChecked && !CheckTableSchema(e.Node))
- {
- updating = true;
- e.Node.Checked = false;
- updating = false;
- }
- }
- }
- private void UpdateTree()
- {
- updating = true;
- tvTables.BeginUpdate();
- tvTables.Nodes.Clear();
- var res = new MyRes("Forms,DataWizard");
- var tablesNode = tvTables.Nodes.Add(res.Get("Tables"));
- tablesNode.ImageIndex = tablesNode.SelectedImageIndex = 222;
- tablesNode.Tag = NodeType.Tables;
- tablesNode.Nodes.Add("dummy");
- if (Connection.IsSqlBased)
- {
- if (Connection.CanContainProcedures)
- {
- var proceduresNode = tvTables.Nodes.Add(res.Get("Procedures"));
- proceduresNode.ImageIndex = proceduresNode.SelectedImageIndex = 52;
- proceduresNode.Tag = NodeType.Procedures;
- proceduresNode.Nodes.Add("dummy");
- }
- var queriesNode = tvTables.Nodes.Add(res.Get("Queries"));
- queriesNode.ImageIndex = queriesNode.SelectedImageIndex = 222;
- queriesNode.Tag = NodeType.Queries;
- queriesNode.Nodes.Add("dummy");
- }
- btnAddQuery.Enabled = Connection.IsSqlBased;
- tvTables.EndUpdate();
- updating = false;
- ExpandExistingNodes();
- }
- private TreeNode GetNode(NodeType nodeType)
- {
- foreach (TreeNode node in tvTables.Nodes)
- {
- if (node.Tag is NodeType t && t == nodeType)
- return node;
- }
- return null;
- }
- private void ExpandExistingNodes()
- {
- bool hasTables = false;
- bool hasProcedures = false;
- bool hasQueries = false;
- foreach (TableDataSource table in Connection.Tables)
- {
- if (!table.Enabled)
- continue;
- if (table is ProcedureDataSource)
- hasProcedures = true;
- else if (!string.IsNullOrEmpty(table.SelectCommand))
- hasQueries = true;
- else
- hasTables = true;
- }
- // expanding a node will load its content
- if (hasTables)
- GetNode(NodeType.Tables)?.Expand();
- if (hasProcedures)
- GetNode(NodeType.Procedures)?.Expand();
- if (hasQueries)
- GetNode(NodeType.Queries)?.Expand();
- }
- private void btnAddTable_Click(object sender, EventArgs e)
- {
- TableDataSource table = new TableDataSource();
- table.Name = report.Dictionary.CreateUniqueName("Table");
- table.Alias = report.Dictionary.CreateUniqueAlias(table.Alias);
- table.Connection = Connection;
- using (QueryWizardForm form = new QueryWizardForm(table))
- {
- if (form.ShowDialog() != DialogResult.OK)
- table.Dispose();
- else
- {
- var node = GetNode(NodeType.Queries);
- ReadQueries(node);
- node.Expand();
- }
- }
- }
- #endregion
- private void DataWizardForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- Done();
- }
- private void UpdateButtons()
- {
- bool enabled = Connection != null;
- btnEditConnection.Enabled = enabled;
- btnNext.Enabled = enabled;
- }
- private void ReadConnections()
- {
- if (Config.DesignerSettings.CustomConnections.Count > 0)
- {
- connections.Deserialize(Config.DesignerSettings.CustomConnections);
- }
- else
- {
- var storage = new StorageService("Designer,LastConnections");
- // read last connections
- storage.Read(connections);
- // write successfully readed connections back
- storage.Write(connections);
- }
- if (connections.Count > 0)
- Connection = connections[0];
- }
- private void Init()
- {
- if (ApplicationConnectionMode)
- {
- if (report.Dictionary.Connections.Count == 0)
- {
- Connection = Activator.CreateInstance(Config.DesignerSettings.ApplicationConnectionType) as DataConnectionBase;
- Connection.Name = report.Dictionary.CreateUniqueName("Connection");
- }
- else
- Connection = report.Dictionary.Connections[0];
- Connection.ConnectionString = Config.DesignerSettings.ApplicationConnection.ConnectionString;
- EditMode = true;
- VisiblePanelIndex = 1;
- btnPrevious.Visible = false;
- btnNext.Visible = false;
- }
- else
- {
- VisiblePanelIndex = 0;
- tbConnName.Text = report.Dictionary.CreateUniqueName("Connection");
- ReadConnections();
- }
- UpdateButtons();
- }
- private void Done()
- {
- if (DialogResult == DialogResult.OK)
- {
- Connection.Name = tbConnName.Text;
- var nodes = new List<TreeNode>();
- foreach (TreeNode node in tvTables.Nodes)
- {
- foreach (TreeNode subNode in node.Nodes)
- {
- if (subNode.Tag is TableDataSource)
- nodes.Add(subNode);
- }
- }
- // enable items that we have checked in the tree
- foreach (TreeNode tableNode in nodes)
- {
- if (tableNode.Tag is TableDataSource table)
- {
- table.Enabled = tableNode.Checked;
- foreach (TreeNode colNode in tableNode.Nodes)
- {
- if (colNode.Tag is Column column)
- column.Enabled = colNode.Checked;
- }
- }
- }
- // create relations if any
- Connection.CreateRelations();
- new StorageService("Designer,LastConnections").Write(connections);
- }
- if (ApplicationConnectionMode)
- {
- // in this mode, we don't have to store connection string in the report
- Connection.ConnectionString = "";
- }
- if (DialogResult == DialogResult.OK || EditMode)
- connections.Remove(Connection);
- connections.Dispose();
- }
- public override void Localize()
- {
- base.Localize();
- MyRes res = new MyRes("Forms,DataWizard");
- Text = res.Get("");
- pnDatabase.Text = res.Get("Page1");
- pnTables.Text = res.Get("Page2");
- lblWhichData.Text = res.Get("WhichData");
- lblHint.Text = res.Get("Hint");
- btnNewConnection.Text = res.Get("NewConnection");
- btnEditConnection.Text = res.Get("EditConnection");
- lblConnName.Text = res.Get("ConnectionName");
- lblConnString.Text = res.Get("ConnectionString");
- lblWhichTables.Text = res.Get("WhichTables");
- lblWait.Text = res.Get("Wait");
- btnAddQuery.Text = res.Get("AddQuery");
- btnCloudDataSources.Text = res.Get("CloudDataSources");
- }
- public override void UpdateDpiDependencies()
- {
- base.UpdateDpiDependencies();
- lblWhichData.Font = this.LogicalToDevice(new Font(DrawUtils.DefaultFont, FontStyle.Bold), true);
- lblWhichTables.Font = lblWhichData.Font;
- tbConnString.Height = pnDatabase.Height - tbConnString.Top - 12;
- picIcon.Image = GetImage("Images.DataWizard.png");
- btnConnString.Image = GetImage(183);
- #if AVALONIA
- btnConnString.Padding = new Padding(this.LogicalToDevice(-2),0,0,0);
- #endif
- tvTables.ImageList = GetImages();
- }
- public DataWizardForm(Report report)
- {
- this.report = report;
- connections = new LastConnections();
- CanSaveRestoreState = true;
- InitializeComponent();
- Localize();
- Init();
- UIUtils.CheckRTL(this);
- UpdateDpiDependencies();
- }
- private class LastConnections : IDisposable, IFRSerializable
- {
- private List<DataConnectionBase> connections;
- public DataConnectionBase this[int index]
- {
- get { return connections[index]; }
- }
- public int Count
- {
- get { return connections.Count; }
- }
- public void Serialize(FRWriter writer)
- {
- List<string> savedConnections = new List<string>();
- writer.ItemName = "LastConnections";
- foreach (DataConnectionBase connection in connections)
- {
- if (savedConnections.IndexOf(connection.GetConnectionId()) == -1)
- {
- writer.SaveChildren = false;
- writer.Write(connection);
- savedConnections.Add(connection.GetConnectionId());
- }
- }
- }
- public void Deserialize(FRReader reader)
- {
- while (reader.NextItem())
- {
- try
- {
- DataConnectionBase connection = reader.Read() as DataConnectionBase;
- if (connection != null)
- connections.Add(connection);
- }
- catch
- {
- }
- }
- }
- public void Deserialize(List<ConnectionEntry> connections)
- {
- foreach (ConnectionEntry ce in connections)
- {
- try
- {
- DataConnectionBase connection =
- Activator.CreateInstance(ce.type) as DataConnectionBase;
- connection.ConnectionString = ce.connectionString;
- this.connections.Add(connection);
- }
- catch
- {
- }
- }
- }
- public void Add(DataConnectionBase connection)
- {
- for (int i = 0; i < connections.Count; i++)
- {
- try
- {
- if (connections[i].GetConnectionId() == connection.GetConnectionId())
- {
- connections.RemoveAt(i);
- break;
- }
- }
- catch
- {
- }
- }
- connections.Insert(0, connection);
- }
- public void Remove(DataConnectionBase connection)
- {
- if (connections.Contains(connection))
- connections.Remove(connection);
- }
- public DataConnectionBase[] ToArray()
- {
- return connections.ToArray();
- }
- public void Dispose()
- {
- foreach (DataConnectionBase connection in connections)
- {
- connection.Dispose();
- }
- connections.Clear();
- }
- public LastConnections()
- {
- connections = new List<DataConnectionBase>();
- }
- }
- }
- }
|