-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
277 lines (252 loc) · 10.5 KB
/
Form1.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
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Relational;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Drawing;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace MySQL2SQLite
{
public partial class Form1 : Form
{
MySqlConnection link;
SQLiteConnection sqlite;
public Form1()
{
InitializeComponent();
lblTable.Visible = false;
progTables.Visible = false;
progDB.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
saveDlgSQLite.Title = "Create SQLite Database...";
saveDlgSQLite.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
saveDlgSQLite.ShowDialog();
if (saveDlgSQLite.FileName != "")
{
/*
string host = "localhost";
string user = "root";
string pass = "";
string dbname = "ace_world_pcap";
*/
string host = txtHost.Text;
string user = txtUsername.Text;
string pass = txtPassword.Text;
string dbname = txtDatabase.Text;
string connect = $"server={host};database={dbname};uid={user};pwd={pass}";
link = new MySqlConnection(connect);
try
{
link.Open();
//MessageBox.Show("Connected");
string query = "show tables";
SetUpSQLiteDB(saveDlgSQLite.FileName);
List<string> tables = new List<string>();
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = link;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
tables.Add(sdr.GetString(0));
}
}
}
// For testing...
//tables.Clear();
//tables.Add("weenie_properties_string");
// Set up the Progress Bar...
progDB.Maximum = tables.Count;
progDB.Step = 1;
progDB.Value = 0;
progDB.Visible = true;
foreach (var t in tables)
{
try
{
if (!t.StartsWith("_") && !t.StartsWith("ace_content"))
{
DumpTable(t);
}
progDB.PerformStep();
//MessageBox.Show("Done with " + t);
}
catch (Exception ex)
{
MessageBox.Show("Error with " + t + "\n" + ex.Message);
}
}
link.Close();
lblTable.Visible = false;
progDB.Visible = false;
progTables.Visible = false;
MessageBox.Show("Done!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + "Could not connect to database.");
}
}
}
private void SetUpSQLiteDB(string dbName)
{
string filename = dbName;
if (File.Exists(filename))
{
File.Delete(filename);
}
sqlite = new SQLiteConnection("Data Source=" + dbName);
sqlite.Open();
}
private long GetRowCount(string tableName)
{
//return 50;
long count = 0;
string query = $"select count(*) as MyCount from {tableName}";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = link;
count = (long)cmd.ExecuteScalar();
}
return count;
}
private void DumpTable(string table)
{
string CreateTable = $"DROP TABLE IF EXISTS `{table}`;\n";
string baseInsertSQL;
progTables.Step = 1;
progTables.Value = 0;
progTables.Maximum = 100;
progTables.Visible = true;
var TotalRows = GetRowCount(table);
lblTable.Text = $"Processing `{table}` with {TotalRows:N0} rows...";
lblTable.Visible = true;
this.Refresh();
Dictionary<string, string> Fields = new Dictionary<string, string>();
//string query = $"select * from {table} limit 50";
string query = $"select * from {table}";
long RowCount = 0;
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = link;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
// Get the table defs
var colSchema = sdr.GetColumnSchema();
CreateTable += $"CREATE TABLE IF NOT EXISTS `{table}` (";
baseInsertSQL = $"INSERT INTO `{table}` (";
for (var i = 0; i < colSchema.Count; i++)
{
var col = colSchema[i];
CreateTable += $"`{col.ColumnName}`";
baseInsertSQL += $"`{col.ColumnName}`";
Fields.Add(col.ColumnName, col.DataType.Name);
switch (col.DataType.Name)
{
case "UInt16": CreateTable += "smallint unsigned"; break;
case "Int16": CreateTable += "smallint"; break;
case "UInt32": CreateTable += " int unsigned"; break;
case "Int32": CreateTable += " int"; break;
case "SByte": CreateTable += " tinyint"; break;
case "Byte": CreateTable += " tinyint"; break;
case "DateTime": CreateTable += " datetime"; break;
case "String": CreateTable += " text"; break;
case "Single": CreateTable += " float"; break;
case "Double": CreateTable += " double"; break;
// This is how MySQL connector treats the bit() types (also true bigint)
case "UInt64": CreateTable += " bigint unsigned"; break;
case "Int64": CreateTable += " bigint"; break;
case "Boolean": CreateTable += "tinyint"; break;
default:
var UNHANDLED = col.DataType.Name;
break;
}
if (i < colSchema.Count - 1)
{
CreateTable += ", \n";
baseInsertSQL += ", ";
}
}
CreateTable += ");\n\n";
baseInsertSQL += ") VALUES (";
// Finish flushing out the insert SQL
int counter = 0;
foreach (var f in Fields)
{
switch (f.Value)
{
case "__String":
case "__DateTime":
baseInsertSQL += "'@" + f.Key + "'";
break;
default:
baseInsertSQL += "@" + f.Key;
break;
}
if (counter++ < Fields.Count - 1)
baseInsertSQL += ", ";
}
baseInsertSQL += ")";
string FileName = Path.Combine("D:\\Web Development\\sqlite\\dump", $"{table}.txt");
File.WriteAllText(FileName, CreateTable);
RunSQLiteQuery(CreateTable);
// using filename append=false mode
using (SQLiteTransaction oTransaction = sqlite.BeginTransaction())
{
while (sdr.Read())
{
RowCount++;
AdjustTablesProgressBar(RowCount, TotalRows);
string sql = baseInsertSQL;
counter = 0;
var command = sqlite.CreateCommand();
command.Transaction = oTransaction;
command.CommandText = baseInsertSQL;
foreach (var f in Fields)
{
var atField = "@" + f.Key;
//command.Parameters.AddWithValue(atField, sdr.GetValue(counter));
command.Parameters.Add(new SQLiteParameter(atField, sdr.GetValue(counter)));
counter++;
}
try
{
command.ExecuteNonQuery();
//RunSQLiteQuery(sql);
}
catch (Exception e)
{
MessageBox.Show(sql);
}
}
oTransaction.Commit();
}
}
}
}
void AdjustTablesProgressBar(long current, long total)
{
double percDone = ((double)current / (double)total) * 100.0;
if (percDone > progTables.Value)
{
// while (progTables.Value < percDone)
progTables.PerformStep();
}
}
void RunSQLiteQuery(string sql)
{
if (sql == null) return;
var command = sqlite.CreateCommand();
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}