Skip to content

Commit a09dca9

Browse files
authored
Merge pull request #4 from naseif/main
Refactoring
2 parents 8ebbe95 + 6d2b794 commit a09dca9

1 file changed

Lines changed: 60 additions & 105 deletions

File tree

Source/optimalDb/optimalDb.WinForms/MainForm.cs

Lines changed: 60 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -15,147 +15,104 @@ public MainForm()
1515
InitializeComponent();
1616
}
1717

18-
protected List<DatabaseConnection> localConnections;
18+
protected List<DatabaseConnection> localConnections = new List<DatabaseConnection>();
1919

2020
private void openToolStripMenuItem_Click(object sender, EventArgs e)
2121
{
2222

2323
if (openFileDialog1.ShowDialog() == DialogResult.OK)
2424
{
2525
var content = File.ReadAllText(openFileDialog1.FileName);
26-
JArray configArray = JArray.Parse(content);
2726

28-
if (localConnections == null)
29-
30-
{
31-
localConnections = new List<DatabaseConnection>();
32-
}
33-
34-
if (this.localConnections.Count() != 0)
35-
{
36-
// We want to clear the connections list in case a new config was imported and update the combobox
37-
this.localConnections.Clear();
38-
connectionsComboBox.Items.Clear();
39-
}
40-
41-
42-
43-
foreach (JObject item in configArray)
44-
45-
{
46-
string name = item.GetValue("Name").ToString();
47-
string connectionString = item.GetValue("ConnectionString").ToString();
48-
49-
if (connectionString == null || connectionString == "")
50-
{
51-
continue;
52-
}
53-
54-
55-
if (!connectionString.StartsWith("Server"))
56-
{
57-
continue;
58-
}
59-
60-
localConnections.Add(new DatabaseConnection(name, connectionString));
61-
}
62-
63-
64-
for (var i = 0; i < localConnections.Count; i++)
65-
{
66-
connectionsComboBox.Items.Add(localConnections[i].Name);
67-
}
27+
this.localConnections.Clear();
6828

29+
#pragma warning disable CS8604 // Possible null reference argument.
30+
localConnections.AddRange(JsonConvert.DeserializeObject<DatabaseConnection[]>(content));
31+
#pragma warning restore CS8604 // Possible null reference argument.
6932

33+
UpdateConnectionCombobox();
7034
}
7135
}
7236

7337
private void testButton_Click(object sender, EventArgs e)
7438
{
7539
DataTable alleViews;
7640

77-
string selected = this.connectionsComboBox.GetItemText(this.connectionsComboBox.SelectedItem);
78-
string connectionString = "";
79-
80-
81-
if (selected == "")
82-
{
83-
MessageBox.Show("You have to select a database to test!", "No Databse selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
84-
return;
85-
}
86-
87-
88-
if (localConnections.Count != 0)
89-
{
90-
connectionString = localConnections.Find(connection => connection.Name.Equals(selected)).ConnectionString;
91-
}
41+
var selected = connectionsComboBox.SelectedItem as DatabaseConnection;
9242

93-
if (connectionString == "")
43+
if (selected == null)
9444
{
95-
MessageBox.Show("A SQL Database connection URL was not found for the selected Database, make sure this Database has a valid connection URL", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
45+
MessageBox.Show("You have to select a database to test!", "No Database selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
9646
return;
9747
}
9848

99-
using (SqlConnection connection = new SqlConnection(connectionString))
49+
try
10050
{
101-
connection.Open();
51+
using (SqlConnection connection = new SqlConnection(selected.ConnectionString))
52+
{
53+
connection.Open();
10254

103-
var sql = @"
55+
var sql = @"
10456
SELECT t.TABLE_SCHEMA, t.TABLE_NAME
10557
FROM INFORMATION_SCHEMA.TABLES t
10658
WHERE t.TABLE_TYPE = 'VIEW'";
10759

108-
using (SqlCommand command = new SqlCommand(sql, connection))
109-
{
110-
SqlDataAdapter adapter = new SqlDataAdapter(command);
111-
DataSet dataset = new DataSet();
112-
adapter.Fill(dataset);
113-
connection.Close();
114-
alleViews = dataset.Tables[0];
60+
using (SqlCommand command = new SqlCommand(sql, connection))
61+
{
62+
SqlDataAdapter adapter = new SqlDataAdapter(command);
63+
DataSet dataset = new DataSet();
64+
adapter.Fill(dataset);
65+
connection.Close();
66+
alleViews = dataset.Tables[0];
67+
}
11568
}
116-
}
11769

118-
var result = new List<ViewPerformanceTestResult>();
70+
var result = new List<ViewPerformanceTestResult>();
11971

120-
foreach(DataRow row in alleViews.Rows)
121-
{
122-
result.Add(
123-
new ViewPerformanceTestResult(
124-
row["TABLE_SCHEMA"].ToString() + "." + row["TABLE_NAME"].ToString(),
125-
GetDurationOfViewExecution(row["TABLE_SCHEMA"].ToString() + "." + row["TABLE_NAME"].ToString(), connectionString)
126-
));
72+
foreach (DataRow row in alleViews.Rows)
73+
{
74+
result.Add(
75+
new ViewPerformanceTestResult(
76+
row["TABLE_SCHEMA"].ToString() + "." + row["TABLE_NAME"].ToString(),
77+
GetDurationOfViewExecution(row["TABLE_SCHEMA"].ToString() + "." + row["TABLE_NAME"].ToString(), selected.ConnectionString)
78+
));
12779

128-
}
129-
dataGridView1.DataSource = result;
80+
}
81+
dataGridView1.DataSource = result;
13082

131-
int columnCount = dataGridView1.Columns.Count;
132-
decimal warning = 5;
133-
decimal error = 25;
83+
int columnCount = dataGridView1.Columns.Count;
84+
decimal warning = 5;
85+
decimal error = 25;
13486

135-
for (int i = 1; (i - 1) < dataGridView1.Rows.Count; i++)
136-
{
137-
for (int j = 0; j < columnCount; j++)
87+
for (int i = 1; (i - 1) < dataGridView1.Rows.Count; i++)
13888
{
139-
if (dataGridView1.Rows[i - 1].Cells[j].Value.GetType() == typeof(decimal))
89+
for (int j = 0; j < columnCount; j++)
14090
{
141-
decimal value;
142-
if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= warning)
91+
if (dataGridView1.Rows[i - 1].Cells[j].Value.GetType() == typeof(decimal))
14392
{
93+
decimal value;
94+
if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= warning)
95+
{
14496

145-
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Yellow;
97+
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Yellow;
14698

147-
}
148-
else if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= error)
149-
{
99+
}
100+
else if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= error)
101+
{
150102

151-
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Red;
103+
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Red;
152104

153-
}
105+
}
154106

107+
}
155108
}
156109
}
157-
}
158110

111+
}
112+
catch (Exception ex)
113+
{
114+
MessageBox.Show(ex.Message);
115+
}
159116

160117
}
161118

@@ -220,19 +177,17 @@ public void ShowCreateDialog()
220177
prompt.AcceptButton = confirmation;
221178

222179
if (prompt.ShowDialog() == DialogResult.OK )
223-
224180
{
225-
226-
if (localConnections == null)
227-
{
228-
localConnections = new List<DatabaseConnection>();
229-
}
230-
231181
localConnections.Add(new DatabaseConnection(databasetextBox.Text, urltextbox.Text));
232-
connectionsComboBox.Items.Add(databasetextBox.Text.ToString());
233-
182+
UpdateConnectionCombobox();
234183
}
184+
}
235185

186+
private void UpdateConnectionCombobox()
187+
{
188+
connectionsComboBox.DataSource = localConnections.ToArray();
189+
connectionsComboBox.ValueMember = "ConnectionString";
190+
connectionsComboBox.DisplayMember = "Name";
236191
}
237192

238193
private void saveToolStripMenuItem_Click(object sender, EventArgs e)

0 commit comments

Comments
 (0)