-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlGenerator.cs
More file actions
273 lines (238 loc) · 9.84 KB
/
SqlGenerator.cs
File metadata and controls
273 lines (238 loc) · 9.84 KB
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
using System;
using System.Configuration;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace SqlStatementGenerator
{
/// <summary>
/// Summary description
/// </summary>
public partial class SqlGenerator : System.Windows.Forms.Form
{
private string m_sSqlConnectionString = string.Empty;
private DataTable m_TableInfo = null;
private string m_sSqlStatementText = string.Empty;
private enum STATEMENT_TYPES { INSERT, UPDATE, DELETE }
public SqlGenerator()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
void SqlGenerator_Load(object sender, EventArgs e)
{
cmbSqlType.SelectedIndex = (int)STATEMENT_TYPES.INSERT;
m_sSqlConnectionString = Convert.ToString(ConfigurationManager.AppSettings["SqlConnectionString"]);
InitializeDatabaseControls();
}
private void btnShowQueryResults_Click(object sender, System.EventArgs e)
{
LoadQueryRecords(txtSelectSQL.Text.Trim());
}
private void btnGenerate_Click(object sender, System.EventArgs e)
{
GenerateSqlStatements();
// if user cancelled or problem occured, just exit
if (m_sSqlStatementText.Trim() == string.Empty)
return;
string sFilePath = FileUtilities.GetUniqueTempFileName(".txt");
FileUtilities.WriteFileContents(sFilePath, m_sSqlStatementText, true);
Process.Start(sFilePath);
}
private void cmbDatabases_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbDatabases.SelectedIndex < 0)
return;
// user selected a different database, so load the table combobox
try
{
Cursor.Current = Cursors.WaitCursor;
cmbTables.DataSource = null;
cmbTables.Items.Clear();
cmbTables.DisplayMember = "TABLE_NAME";
cmbTables.ValueMember = "TABLE_NAME";
DataTable dt = DatabaseUtilities.GetDatabaseTables(m_sSqlConnectionString, cmbDatabases.Text);
cmbTables.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("cmbDatabases_SelectedIndexChanged error: " + ex.Message);
}
finally
{
Cursor.Current = Cursors.Default;
UpdateControls();
}
}
private void cmbTables_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTables.SelectedIndex < 0)
return;
// automatically setup a query based upon the table selected
string sTableName = cmbTables.Text;
txtSelectSQL.Text = "SELECT * FROM " + sTableName;
txtTargetTable.Text = sTableName;
dgTableInfo.DataSource = null;
UpdateControls();
}
private void cmbSqlType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTables.SelectedIndex < 0)
return;
// user changed the type of statement to generate,
// so flip the checkboxes for user convenience
for (int i = 0; i < chklstIncludeFields.Items.Count; i++)
{
if(cmbSqlType.SelectedIndex == (int)STATEMENT_TYPES.DELETE)
{
// assume that the primary key is in column 0,
// and that deletes trigger off that
chklstIncludeFields.SetItemChecked(i, (i == 0));
}
else
{
// assume that the primary key is in column 0,
// and that it should NOT be included in inserts and updates
chklstIncludeFields.SetItemChecked(i, (i != 0));
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// Initializes the database combobox control
/// </summary>
private void InitializeDatabaseControls()
{
try
{
Cursor.Current = Cursors.WaitCursor;
cmbDatabases.DataSource = null;
cmbDatabases.Items.Clear();
cmbTables.DataSource = null;
cmbTables.Items.Clear(); // clear the tables also since they'll be invalid
cmbDatabases.DisplayMember = "DATABASE_NAME";
cmbDatabases.ValueMember = "DATABASE_NAME";
DataTable dt = DatabaseUtilities.GetDatabases(m_sSqlConnectionString);
cmbDatabases.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error loading database-tables into list: " + ex.Message);
}
finally
{
Cursor.Current = Cursors.Default;
UpdateControls();
}
}
/// <summary>
/// Updates the enable states of various form controls
/// </summary>
private void UpdateControls()
{
btnShowQueryResults.Enabled = (cmbDatabases.SelectedIndex >= 0);
btnGenerate.Enabled = (dgTableInfo.DataSource != null);
}
/// <summary>
/// Loads the grid and column list controls according to the specified query
/// </summary>
/// <param name="sSQL"></param>
private void LoadQueryRecords(string sSQL)
{
if (sSQL.Trim() == string.Empty)
{
MessageBox.Show("The SQL query was empty! Please enter a valid SQL query!");
return;
}
try
{
Cursor.Current = Cursors.WaitCursor;
m_TableInfo = DatabaseUtilities.LoadDataTable(m_sSqlConnectionString, cmbDatabases.Text, sSQL);
dgTableInfo.DataSource = m_TableInfo;
// load the list of columns to include in the generator
chklstIncludeFields.Items.Clear();
foreach (DataColumn col in m_TableInfo.Columns)
{
// exclude the primary/auto-increment key by default, but select/check all the others
chklstIncludeFields.Items.Add(col.ColumnName, (chklstIncludeFields.Items.Count > 0));
}
}
catch (Exception ex)
{
MessageBox.Show("LoadQueryRecords error: " + ex.Message);
}
finally
{
Cursor.Current = Cursors.Default;
UpdateControls();
}
}
/// <summary>
/// Generates sql statements based upon the generator type selected and the tableinfo provided
/// </summary>
private void GenerateSqlStatements()
{
// clear the string member
m_sSqlStatementText = string.Empty;
// create an array of all the columns that are to be included
ArrayList aryColumns = new ArrayList();
for (int i = 0; i < chklstIncludeFields.CheckedItems.Count; i++)
{
aryColumns.Add(chklstIncludeFields.CheckedItems[i].ToString());
}
// if no columns included, return with a msg
if(aryColumns.Count <= 0)
{
MessageBox.Show("No columns selected! Please check/select some columns to include!");
return;
}
string sTargetTableName = txtTargetTable.Text.Trim();
if(sTargetTableName == string.Empty)
{
MessageBox.Show("No valid target table name! Please enter a table name to be used in the SQL statements!");
return;
}
// generate the sql by type
if (cmbSqlType.SelectedIndex == (int)STATEMENT_TYPES.INSERT)
{
m_sSqlStatementText = SqlScriptGenerator.GenerateSqlInserts(aryColumns, m_TableInfo, sTargetTableName);
}
else if (cmbSqlType.SelectedIndex == (int)STATEMENT_TYPES.UPDATE)
{
// get an array of all the active table columns
ArrayList aryWhereColumns = new ArrayList();
for (int i = 0; i < chklstIncludeFields.Items.Count; i++)
{
aryWhereColumns.Add(chklstIncludeFields.GetItemText(chklstIncludeFields.Items[i]));
}
// create dlg and pass in array of columns
SelectMultipleItems dlg = new SelectMultipleItems();
dlg.Text = "Select WHERE Columns";
dlg.Description = "Select WHERE-Clause Columns for UPDATE SQLs:";
dlg.Initialize(aryWhereColumns, string.Empty, false);
// user cancelled, so exit
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
aryWhereColumns.Clear();
aryWhereColumns = dlg.UserSelectedItems;
m_sSqlStatementText = SqlScriptGenerator.GenerateSqlUpdates(aryColumns, aryWhereColumns, m_TableInfo, sTargetTableName);
}
else if (cmbSqlType.SelectedIndex == (int)STATEMENT_TYPES.DELETE)
{
m_sSqlStatementText = SqlScriptGenerator.GenerateSqlDeletes(aryColumns, m_TableInfo, sTargetTableName);
}
}
}
}