diff --git a/.gitignore b/.gitignore index fd5204b..10016f0 100644 --- a/.gitignore +++ b/.gitignore @@ -181,3 +181,4 @@ UpgradeLog*.htm # Microsoft Fakes FakesAssemblies/ +/.vs diff --git a/NSF2SQL/App.config b/NSF2SQL/App.config index 8e15646..1c8b9cd 100644 --- a/NSF2SQL/App.config +++ b/NSF2SQL/App.config @@ -1,6 +1,11 @@ - + - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/NSF2SQL/ExportTarget.Designer.cs b/NSF2SQL/ExportTarget.Designer.cs new file mode 100644 index 0000000..9ddd8b1 --- /dev/null +++ b/NSF2SQL/ExportTarget.Designer.cs @@ -0,0 +1,144 @@ +namespace NSF2SQL +{ + partial class ExportTarget + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ServerRadioButton = new System.Windows.Forms.RadioButton(); + this.FileRadioButton = new System.Windows.Forms.RadioButton(); + this.label1 = new System.Windows.Forms.Label(); + this.ServerTypeComboBox = new System.Windows.Forms.ComboBox(); + this.label2 = new System.Windows.Forms.Label(); + this.btnOK = new System.Windows.Forms.Button(); + this.btnCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // ServerRadioButton + // + this.ServerRadioButton.AutoSize = true; + this.ServerRadioButton.Checked = true; + this.ServerRadioButton.Location = new System.Drawing.Point(195, 89); + this.ServerRadioButton.Name = "ServerRadioButton"; + this.ServerRadioButton.Size = new System.Drawing.Size(135, 36); + this.ServerRadioButton.TabIndex = 1; + this.ServerRadioButton.TabStop = true; + this.ServerRadioButton.Text = "Server"; + this.ServerRadioButton.UseVisualStyleBackColor = true; + // + // FileRadioButton + // + this.FileRadioButton.AutoSize = true; + this.FileRadioButton.Location = new System.Drawing.Point(355, 89); + this.FileRadioButton.Name = "FileRadioButton"; + this.FileRadioButton.Size = new System.Drawing.Size(99, 36); + this.FileRadioButton.TabIndex = 2; + this.FileRadioButton.Text = "File"; + this.FileRadioButton.UseVisualStyleBackColor = true; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(13, 13); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(176, 32); + this.label1.TabIndex = 3; + this.label1.Text = "Server Type:"; + // + // ServerTypeComboBox + // + this.ServerTypeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.ServerTypeComboBox.FormattingEnabled = true; + this.ServerTypeComboBox.Items.AddRange(new object[] { + "MySQL", + "SQL Server (T-SQL)"}); + this.ServerTypeComboBox.Location = new System.Drawing.Point(195, 10); + this.ServerTypeComboBox.Name = "ServerTypeComboBox"; + this.ServerTypeComboBox.Size = new System.Drawing.Size(352, 39); + this.ServerTypeComboBox.TabIndex = 4; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(84, 89); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(105, 32); + this.label2.TabIndex = 5; + this.label2.Text = "Target:"; + // + // btnOK + // + this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnOK.Location = new System.Drawing.Point(19, 151); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(259, 46); + this.btnOK.TabIndex = 6; + this.btnOK.Text = "OK"; + this.btnOK.UseVisualStyleBackColor = true; + // + // btnCancel + // + this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.btnCancel.Location = new System.Drawing.Point(288, 151); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.Size = new System.Drawing.Size(259, 46); + this.btnCancel.TabIndex = 7; + this.btnCancel.Text = "Cancel"; + this.btnCancel.UseVisualStyleBackColor = true; + // + // ExportTarget + // + this.AcceptButton = this.btnOK; + this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.btnCancel; + this.ClientSize = new System.Drawing.Size(574, 225); + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.btnOK); + this.Controls.Add(this.label2); + this.Controls.Add(this.ServerTypeComboBox); + this.Controls.Add(this.label1); + this.Controls.Add(this.FileRadioButton); + this.Controls.Add(this.ServerRadioButton); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Name = "ExportTarget"; + this.Text = "Export SQL"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.RadioButton ServerRadioButton; + private System.Windows.Forms.RadioButton FileRadioButton; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.ComboBox ServerTypeComboBox; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Button btnOK; + private System.Windows.Forms.Button btnCancel; + } +} \ No newline at end of file diff --git a/NSF2SQL/ExportTarget.cs b/NSF2SQL/ExportTarget.cs new file mode 100644 index 0000000..b3f2367 --- /dev/null +++ b/NSF2SQL/ExportTarget.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace NSF2SQL +{ + public partial class ExportTarget : Form + { + public ExportTarget() + { + InitializeComponent(); + + this.ServerTypeComboBox.SelectedIndex = 0; + } + + public bool FileExport + { + get + { + return this.FileRadioButton.Checked; + } + } + + public bool ServerExport + { + get + { + return this.ServerRadioButton.Checked; + } + } + + public bool MySqlExport + { + get + { + return this.ServerTypeComboBox.SelectedIndex == 0; + } + } + + public bool SqlServerExport + { + get + { + return this.ServerTypeComboBox.SelectedIndex == 1; + } + } + } +} diff --git a/NSF2SQL/ExportTarget.resx b/NSF2SQL/ExportTarget.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/NSF2SQL/ExportTarget.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/NSF2SQL/Form1.cs b/NSF2SQL/Form1.cs index 822c12e..18a8cb0 100644 --- a/NSF2SQL/Form1.cs +++ b/NSF2SQL/Form1.cs @@ -6,6 +6,9 @@ using System.IO; using MySql.Data.MySqlClient; using System.Text.RegularExpressions; +using System.Data.Common; +using System.Data.SqlClient; +using System.Linq; namespace NSF2SQL { @@ -21,6 +24,7 @@ public partial class Form1 : Form string mysqlPassword = ""; int mysqlNumRowsPerInsert = 1000; Regex excludeField = new Regex("^(Form)$");//new Regex("^(\\$.*|Form|Readers)$"); + private static ISqlGenerator sqlGenerator; public Form1() { @@ -333,6 +337,8 @@ private void bExportDocuments_Click(object sender, EventArgs ea) #region Export Documents pDialog.DoWork += delegate(object dialog, DoWorkEventArgs e) { + ExportTarget exportDialog = new ExportTarget(); + try { //export documents @@ -542,13 +548,29 @@ private void bExportDocuments_Click(object sender, EventArgs ea) //format to sql total = newTables.Count; bool complete = false; + + lastTicks = 0; + count = 0; + DialogResult result = DialogResult.Cancel; + Invoke((MethodInvoker)delegate() { result = exportDialog.ShowDialog(this); }); + if (result == DialogResult.Cancel) + { + e.Cancel = true; + return; + } + + if (exportDialog.SqlServerExport) + { + sqlGenerator = new TsqlGenerator(); + } + else if (exportDialog.MySqlExport) + { + sqlGenerator = new MySqlGenerator(); + } + do { - lastTicks = 0; - count = 0; - DialogResult result = DialogResult.Cancel; - Invoke((MethodInvoker)delegate() { result = MessageBox.Show(pDialog.Window, "Do you want to export to a MySQL server?", "Export to a server?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); }); - if (result == DialogResult.Yes) + if (exportDialog.ServerExport) { InputBox input = null; Invoke((MethodInvoker)delegate() { input = InputBox.Show(pDialog.Window, "SQL server info?", new InputBoxItem[] { new InputBoxItem("Server", mysqlServer), new InputBoxItem("Database", mysqlDatabase), new InputBoxItem("Username", mysqlUsername), new InputBoxItem("Password", mysqlPassword, true), new InputBoxItem("Number of rows per INSERT", mysqlNumRowsPerInsert.ToString()) }, InputBoxButtons.OKCancel); }); @@ -560,18 +582,39 @@ private void bExportDocuments_Click(object sender, EventArgs ea) mysqlPassword = input.Items["Password"]; int.TryParse(input.Items["Number of rows per INSERT"], out mysqlNumRowsPerInsert); - MySqlConnection conn = new MySqlConnection("SERVER=" + mysqlServer + ";USERNAME=" + mysqlUsername + ";PASSWORD=" + mysqlPassword + ";"); + DbConnection conn = null; + if (exportDialog.MySqlExport) + { + conn = new MySqlConnection("SERVER=" + mysqlServer + ";USERNAME=" + mysqlUsername + ";PASSWORD=" + mysqlPassword + ";"); + } + else if (exportDialog.SqlServerExport) + { + conn = new SqlConnection("Server=" + mysqlServer + ";User Id=" + mysqlUsername + ";Password=" + mysqlPassword + ";"); + } try { startTicks = DateTime.Now.Ticks; conn.Open(); - MySqlCommand command = conn.CreateCommand(); - command.CommandText = createDatabase(mysqlDatabase); + string[] tokens = null; + if (exportDialog.SqlServerExport) + { + tokens = sqlGenerator.CreateDatabase(mysqlDatabase).Split(new string[] { "GO\r\n" }, StringSplitOptions.None); + } + else + { + tokens = new string[] { sqlGenerator.CreateDatabase(mysqlDatabase) + sqlGenerator.SetVariables() }; + } + + DbCommand command = conn.CreateCommand(); + foreach (string sqlString in tokens) + { + command.CommandText = sqlString; + command.ExecuteNonQuery(); + } - command.ExecuteNonQuery(); - foreach (Table table in newTables.Values) + foreach (Table table in newTables.Values.Where(t => string.IsNullOrEmpty(t.LinkedTable))) { //check if cancelled if (pDialog.IsCancelled) @@ -582,21 +625,50 @@ private void bExportDocuments_Click(object sender, EventArgs ea) pDialog.ReportProgress(++count, "Inserting SQL"); if (table.Columns.Count > 0) { - command.CommandText = createTable(table); + command.CommandText = sqlGenerator.CreateTable(table); command.ExecuteNonQuery(); - List rows = insertTableRows(table); + List rows = sqlGenerator.InsertTableRowsToList(table); for (int i = 0; i < rows.Count; i += mysqlNumRowsPerInsert) { - command.CommandText = beginInsertTable(table); + command.CommandText = sqlGenerator.BeginInsertTable(table); command.CommandText += String.Join(",", rows.GetRange(i, Math.Min(rows.Count - i, mysqlNumRowsPerInsert))) + ";\n"; - command.CommandText += endInsertTable(table); + command.CommandText += sqlGenerator.EndInsertTable(table); command.ExecuteNonQuery(); pDialog.ReportProgress(count, "Inserting SQL"); } } } - command.CommandText = restoreVariables(); - command.ExecuteNonQuery(); + + foreach (Table table in newTables.Values.Where(t => !string.IsNullOrEmpty(t.LinkedTable))) + { + //check if cancelled + if (pDialog.IsCancelled) + { + e.Cancel = true; + return; + } + pDialog.ReportProgress(++count, "Inserting SQL"); + if (table.Columns.Count > 0) + { + command.CommandText = sqlGenerator.CreateTable(table); + command.ExecuteNonQuery(); + List rows = sqlGenerator.InsertTableRowsToList(table); + for (int i = 0; i < rows.Count; i += mysqlNumRowsPerInsert) + { + command.CommandText = sqlGenerator.BeginInsertTable(table); + command.CommandText += String.Join(",", rows.GetRange(i, Math.Min(rows.Count - i, mysqlNumRowsPerInsert))) + ";\n"; + command.CommandText += sqlGenerator.EndInsertTable(table); + command.ExecuteNonQuery(); + pDialog.ReportProgress(count, "Inserting SQL"); + } + } + } + + command.CommandText = sqlGenerator.RestoreVariables(); + if (!string.IsNullOrEmpty(command.CommandText)) + { + command.ExecuteNonQuery(); + } complete = true; } catch (Exception ex) @@ -607,9 +679,11 @@ private void bExportDocuments_Click(object sender, EventArgs ea) { conn.Close(); } + + conn.Dispose(); } } - else if (result == DialogResult.No) + else if (exportDialog.FileExport) { saveFileDialog1.FileName = "export.sql"; result = DialogResult.Cancel; @@ -625,8 +699,26 @@ private void bExportDocuments_Click(object sender, EventArgs ea) try { startTicks = DateTime.Now.Ticks; - file.WriteLine(createDatabase(mysqlDatabase)); - foreach (Table table in newTables.Values) + file.WriteLine(sqlGenerator.CreateDatabase(mysqlDatabase)); + file.WriteLine(sqlGenerator.SetVariables()); + foreach (Table table in newTables.Values.Where(t => string.IsNullOrEmpty(t.LinkedTable))) + { + //check if cancelled + if (pDialog.IsCancelled) + { + e.Cancel = true; + return; + } + pDialog.ReportProgress(++count, "Formatting SQL"); + if (table.Columns.Count > 0) + { + file.WriteLine(sqlGenerator.CreateTable(table)); + file.WriteLine(sqlGenerator.BeginInsertTable(table)); + file.WriteLine(sqlGenerator.InsertTableRowsToString(table)); + file.WriteLine(sqlGenerator.EndInsertTable(table)); + } + } + foreach (Table table in newTables.Values.Where(t => !string.IsNullOrEmpty(t.LinkedTable))) { //check if cancelled if (pDialog.IsCancelled) @@ -637,13 +729,13 @@ private void bExportDocuments_Click(object sender, EventArgs ea) pDialog.ReportProgress(++count, "Formatting SQL"); if (table.Columns.Count > 0) { - file.WriteLine(createTable(table)); - file.WriteLine(beginInsertTable(table)); - file.WriteLine(String.Join(",", insertTableRows(table)) + ";"); - file.WriteLine(endInsertTable(table)); + file.WriteLine(sqlGenerator.CreateTable(table)); + file.WriteLine(sqlGenerator.BeginInsertTable(table)); + file.WriteLine(sqlGenerator.InsertTableRowsToString(table)); + file.WriteLine(sqlGenerator.EndInsertTable(table)); } } - file.WriteLine(restoreVariables()); + file.WriteLine(sqlGenerator.RestoreVariables()); complete = true; } catch (Exception ex) @@ -657,11 +749,6 @@ private void bExportDocuments_Click(object sender, EventArgs ea) } } } - else - { - e.Cancel = true; - return; - } } while (!complete); } catch (Exception ex) @@ -669,6 +756,8 @@ private void bExportDocuments_Click(object sender, EventArgs ea) MessageBox.Show(ex.ToString()); e.Cancel = true; } + + exportDialog.Dispose(); }; #endregion pDialog.ProgressChanged += delegate(object dialog, ProgressChangedEventArgs e) @@ -714,164 +803,15 @@ private void bExportDocuments_Click(object sender, EventArgs ea) private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { - if (treeView1.SelectedNode.Nodes.Count == 0) - { - bExportDocuments_Click(sender, e); - } - } - - private string createDatabase(string databaseName) - { - string query = - "DROP DATABASE IF EXISTS `" + databaseName + "`;\n" + - "\n" + - "CREATE DATABASE `" + databaseName + "`;\n" + - "USE `" + databaseName + "`;\n" + - "\n" + - "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n" + - "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n" + - "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n" + - "/*!40101 SET NAMES utf8 */;\n" + - "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n" + - "/*!40103 SET TIME_ZONE='+00:00' */;\n" + - "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n" + - "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n" + - "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n" + - "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n"; - return query; - } - - private string createTable(Table table) - { - string query = - "CREATE TABLE `" + table.Name + "` (\n" + - "`id` INT NOT NULL,\n"; - foreach (Column column in table.Columns.Values) - { - query += "`" + column.Name + "` " + column.Type + " NULL,\n"; - } - query += "PRIMARY KEY (`id`)"; - if (table.LinkedTable != null) + if (treeView1.SelectedNode == null) { - query += - ",\n" + - "KEY `" + table.Name + "_idx` (`" + table.LinkedTable + "id`),\n" + - "CONSTRAINT `" + table.Name + "` FOREIGN KEY (`" + table.LinkedTable + "id`) REFERENCES `" + table.LinkedTable + "` (`id`) ON DELETE CASCADE ON UPDATE CASCADE"; + return; } - query += ");\n"; - return query; - } - - private string beginInsertTable(Table table) - { - string query = - "LOCK TABLES `" + table.Name + "` WRITE;\n" + - "/*!40000 ALTER TABLE `" + table.Name + "` DISABLE KEYS */;\n" + - "INSERT INTO `" + table.Name + "` (`id`,`" + String.Join("`,`", table.Columns.Keys) + "`) VALUES"; - return query; - } - - private List insertTableRows(Table table) - { - List rows = new List(table.RowCount); - for (int i = 0; i < table.RowCount; i++) + if (treeView1.SelectedNode.Nodes.Count == 0) { - List columnValues = new List(table.Columns.Count); - foreach (Column column in table.Columns.Values) - { - if (column.Values.ContainsKey(i + 1)) - { - if (column.Values[i + 1] != null) - { - string value = column.Values[i + 1].ToString(); - switch (column.Type) - { - case "decimal(20,10)": - if (value == "") - { - value = "NULL"; - } - else if (value == "Infinity") - { - value = "9999999999.9999999999"; - } - else - { - double temp; - if (!double.TryParse(value, out temp)) - { - value = "NULL"; - } - } - break; - case "datetime": - if (value == "") - { - value = "NULL"; - } - else - { - DateTime temp; - if (DateTime.TryParse(value, out temp)) - { - if (column.Values[i + 1] is DateTime) - { - value = ((DateTime)column.Values[i + 1]).ToString("yyyy-MM-dd HH:mm:ss"); - } - else - { - value = temp.ToString("yyyy-MM-dd HH:mm:ss"); - } - value = "'" + value + "'"; - } - else - { - value = "NULL"; - } - } - break; - default: - value = "'" + value.ToString().Replace("'", "''") + "'"; - break; - } - columnValues.Add(value); - } - else - { - columnValues.Add("NULL"); - } - } - else - { - columnValues.Add("NULL"); - } - } - rows.Add("(" + (i + 1) + "," + String.Join(",", columnValues) + ")"); + bExportDocuments_Click(sender, e); } - return rows; - } - - private string endInsertTable(Table table) - { - string query = - "/*!40000 ALTER TABLE `" + table.Name + "` ENABLE KEYS */;\n" + - "UNLOCK TABLES;\n"; - return query; - } - - private string restoreVariables() - { - string query = - "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n" + - "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n" + - "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n" + - "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n" + - "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n" + - "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n" + - "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n" + - "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"; - return query; } private string ticksToString(long ticks) diff --git a/NSF2SQL/ISqlGenerator.cs b/NSF2SQL/ISqlGenerator.cs new file mode 100644 index 0000000..92aea96 --- /dev/null +++ b/NSF2SQL/ISqlGenerator.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NSF2SQL +{ + interface ISqlGenerator + { + string CreateDatabase(string databaseName); + + string SetVariables(); + + string CreateTable(Table table); + + string BeginInsertTable(Table table); + + List InsertTableRowsToList(Table table); + + string InsertTableRowsToString(Table table); + + string EndInsertTable(Table table); + + string RestoreVariables(); + } +} diff --git a/NSF2SQL/MySqlGenerator.cs b/NSF2SQL/MySqlGenerator.cs new file mode 100644 index 0000000..9beac57 --- /dev/null +++ b/NSF2SQL/MySqlGenerator.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NSF2SQL +{ + class MySqlGenerator : ISqlGenerator + { + public string CreateDatabase(string databaseName) + { + return "DROP DATABASE IF EXISTS `" + databaseName + "`;\n" + + "\n" + + "CREATE DATABASE `" + databaseName + "`;\n" + + "USE `" + databaseName + "`;\n" + + "\n"; + } + + public string SetVariables() + { + return "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n" + + "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n" + + "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n" + + "/*!40101 SET NAMES utf8 */;\n" + + "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n" + + "/*!40103 SET TIME_ZONE='+00:00' */;\n" + + "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n" + + "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n" + + "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n" + + "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n"; + } + + public string CreateTable(Table table) + { + string query = + "CREATE TABLE `" + table.Name + "` (\n" + + "`id` INT NOT NULL,\n"; + foreach (Column column in table.Columns.Values) + { + query += "`" + column.Name + "` " + column.Type + " NULL,\n"; + } + query += "PRIMARY KEY (`id`)"; + if (table.LinkedTable != null) + { + query += + ",\n" + + "KEY `" + table.Name + "_idx` (`" + table.LinkedTable + "id`),\n" + + "CONSTRAINT `" + table.Name + "` FOREIGN KEY (`" + table.LinkedTable + "id`) REFERENCES `" + table.LinkedTable + "` (`id`) ON DELETE CASCADE ON UPDATE CASCADE"; + } + query += ");\n"; + return query; + } + + public string BeginInsertTable(Table table) + { + + string query = + "LOCK TABLES `" + table.Name + "` WRITE;\n" + + "/*!40000 ALTER TABLE `" + table.Name + "` DISABLE KEYS */;\n" + + "INSERT INTO `" + table.Name + "` (`id`,`" + String.Join("`,`", table.Columns.Keys) + "`) VALUES"; + return query; + } + + public List InsertTableRowsToList(Table table) + { + List rows = new List(table.RowCount); + for (int i = 0; i < table.RowCount; i++) + { + List columnValues = new List(table.Columns.Count); + foreach (Column column in table.Columns.Values) + { + if (column.Values.ContainsKey(i + 1)) + { + if (column.Values[i + 1] != null) + { + string value = column.Values[i + 1].ToString(); + switch (column.Type) + { + case "decimal(20,10)": + if (value == "") + { + value = "NULL"; + } + else if (value == "Infinity") + { + value = "9999999999.9999999999"; + } + else + { + double temp; + if (!double.TryParse(value, out temp)) + { + value = "NULL"; + } + } + break; + case "datetime": + if (value == "") + { + value = "NULL"; + } + else + { + DateTime temp; + if (DateTime.TryParse(value, out temp)) + { + if (column.Values[i + 1] is DateTime) + { + value = ((DateTime)column.Values[i + 1]).ToString("yyyy-MM-dd HH:mm:ss"); + } + else + { + value = temp.ToString("yyyy-MM-dd HH:mm:ss"); + } + value = "'" + value + "'"; + } + else + { + value = "NULL"; + } + } + break; + default: + value = "'" + value.ToString().Replace("'", "''") + "'"; + break; + } + columnValues.Add(value); + } + else + { + columnValues.Add("NULL"); + } + } + else + { + columnValues.Add("NULL"); + } + } + rows.Add("(" + (i + 1) + "," + String.Join(",", columnValues) + ")"); + } + return rows; + } + + public string InsertTableRowsToString(Table table) + { + return string.Join(",", InsertTableRowsToList(table)) + ";"; + } + + public string EndInsertTable(Table table) + { + string query = + "/*!40000 ALTER TABLE `" + table.Name + "` ENABLE KEYS */;\n" + + "UNLOCK TABLES;\n"; + return query; + } + + public string RestoreVariables() + { + string query = + "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n" + + "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n" + + "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n" + + "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n" + + "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n" + + "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n" + + "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n" + + "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"; + return query; + } + } +} diff --git a/NSF2SQL/NSF2SQL.csproj b/NSF2SQL/NSF2SQL.csproj index 19e194b..64b2587 100644 --- a/NSF2SQL/NSF2SQL.csproj +++ b/NSF2SQL/NSF2SQL.csproj @@ -59,9 +59,8 @@ False bin\Debug\Microsoft.WindowsAPICodePack.Shell.dll - - False - ..\..\..\..\..\..\..\..\Program Files (x86)\MySQL\MySQL Installer for Windows\MySql.Data.dll + + ..\packages\MySql.Data.6.8.8\lib\net45\MySql.Data.dll True @@ -76,6 +75,12 @@ + + Form + + + ExportTarget.cs + Form @@ -83,10 +88,16 @@ Form1.cs + + + + + ExportTarget.cs + Form1.cs @@ -99,6 +110,7 @@ True Resources.resx + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/NSF2SQL/TsqlGenerator.cs b/NSF2SQL/TsqlGenerator.cs new file mode 100644 index 0000000..653c36c --- /dev/null +++ b/NSF2SQL/TsqlGenerator.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NSF2SQL +{ + class TsqlGenerator : ISqlGenerator + { + public string CreateDatabase(string databaseName) + { + return string.Format(@"DROP DATABASE IF EXISTS {0}; +GO + +CREATE DATABASE {0}; +GO + +USE [{0}];", databaseName); + } + + public string SetVariables() + { + return string.Empty; + } + + public string CreateTable(Table table) + { + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.AppendFormat("CREATE TABLE [{0}] (\r\n", table.Name); + sqlBuilder.AppendLine("[id] INT NOT NULL,"); + foreach (Column column in table.Columns.Values) + { + sqlBuilder.AppendFormat("[{0}] {1} NULL,\r\n", column.Name, column.Type); + } + sqlBuilder.Append("PRIMARY KEY ([id])"); + if (table.LinkedTable != null) + { + sqlBuilder.Append(",\r\n"); + sqlBuilder.AppendFormat("CONSTRAINT[{0}_constraint] FOREIGN KEY([{1}id]) REFERENCES [{1}]([id]) ON DELETE CASCADE ON UPDATE CASCADE);\r\n", table.Name, table.LinkedTable); + sqlBuilder.AppendFormat("CREATE INDEX[{0}_idx] ON [{0}]([{1}id]);\r\n", table.Name, table.LinkedTable); + } + else + { + sqlBuilder.Append(");\r\n"); + } + + return sqlBuilder.ToString(); + } + + public string BeginInsertTable(Table table) + { + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.AppendFormat("INSERT INTO [{0}] ([id],[", table.Name); + sqlBuilder.Append(string.Join("],[", table.Columns.Keys)); + sqlBuilder.AppendLine("]) VALUES"); + + return sqlBuilder.ToString(); + } + + public List InsertTableRowsToList(Table table) + { + List rows = new List(table.RowCount); + for (int i = 0; i < table.RowCount; i++) + { + List columnValues = new List(table.Columns.Count); + foreach (Column column in table.Columns.Values) + { + if (column.Values.ContainsKey(i + 1)) + { + if (column.Values[i + 1] != null) + { + string value = column.Values[i + 1].ToString(); + switch (column.Type) + { + case "decimal(20,10)": + if (value == "") + { + value = "NULL"; + } + else if (value == "Infinity") + { + value = "9999999999.9999999999"; + } + else + { + double temp; + if (!double.TryParse(value, out temp)) + { + value = "NULL"; + } + } + break; + case "datetime": + if (value == "") + { + value = "NULL"; + } + else + { + DateTime temp; + if (DateTime.TryParse(value, out temp)) + { + if (column.Values[i + 1] is DateTime) + { + value = ((DateTime)column.Values[i + 1]).ToString("yyyy-MM-dd HH:mm:ss"); + } + else + { + value = temp.ToString("yyyy-MM-dd HH:mm:ss"); + } + value = "'" + value + "'"; + } + else + { + value = "NULL"; + } + } + break; + default: + value = "'" + value.ToString().Replace("'", "''") + "'"; + break; + } + columnValues.Add(value); + } + else + { + columnValues.Add("NULL"); + } + } + else + { + columnValues.Add("NULL"); + } + } + rows.Add("(" + (i + 1) + "," + String.Join(",", columnValues) + ")"); + } + return rows; + } + + public string InsertTableRowsToString(Table table) + { + StringBuilder sqlBuilder = new StringBuilder(); + List rows = InsertTableRowsToList(table); + int index = 0; + do + { + sqlBuilder.Append(string.Join(",\r\n", rows.GetRange(index, Math.Min(1000, rows.Count - index)))); + sqlBuilder.AppendLine(";"); + index += 1000; + + if(index < rows.Count) + { + sqlBuilder.AppendLine(); + sqlBuilder.Append(BeginInsertTable(table)); + } + } + while (index < rows.Count); + + return sqlBuilder.ToString(); + } + + public string EndInsertTable(Table table) + { + return string.Empty; + } + + public string RestoreVariables() + { + return string.Empty; + } + } +} diff --git a/NSF2SQL/bin/Debug/MySql.Data.dll b/NSF2SQL/bin/Debug/MySql.Data.dll index d7dc2ab..b5997e5 100644 Binary files a/NSF2SQL/bin/Debug/MySql.Data.dll and b/NSF2SQL/bin/Debug/MySql.Data.dll differ diff --git a/NSF2SQL/bin/Debug/NSF2SQL.exe b/NSF2SQL/bin/Debug/NSF2SQL.exe index f97994d..86d4d83 100644 Binary files a/NSF2SQL/bin/Debug/NSF2SQL.exe and b/NSF2SQL/bin/Debug/NSF2SQL.exe differ diff --git a/NSF2SQL/bin/Debug/NSF2SQL.exe.config b/NSF2SQL/bin/Debug/NSF2SQL.exe.config index 8e15646..1c8b9cd 100644 --- a/NSF2SQL/bin/Debug/NSF2SQL.exe.config +++ b/NSF2SQL/bin/Debug/NSF2SQL.exe.config @@ -1,6 +1,11 @@ - + - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/NSF2SQL/bin/Debug/NSF2SQL.pdb b/NSF2SQL/bin/Debug/NSF2SQL.pdb index bc350e2..de4fbed 100644 Binary files a/NSF2SQL/bin/Debug/NSF2SQL.pdb and b/NSF2SQL/bin/Debug/NSF2SQL.pdb differ diff --git a/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferences.cache index d825abf..ad56c42 100644 Binary files a/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index cf9c8af..fb3e38f 100644 Binary files a/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/NSF2SQL/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/NSF2SQL/obj/Debug/Interop.Domino.dll b/NSF2SQL/obj/Debug/Interop.Domino.dll index ca36977..595de73 100644 Binary files a/NSF2SQL/obj/Debug/Interop.Domino.dll and b/NSF2SQL/obj/Debug/Interop.Domino.dll differ diff --git a/NSF2SQL/obj/Debug/NSF2SQL.csproj.FileListAbsolute.txt b/NSF2SQL/obj/Debug/NSF2SQL.csproj.FileListAbsolute.txt index e6a43e2..2b4eccc 100644 --- a/NSF2SQL/obj/Debug/NSF2SQL.csproj.FileListAbsolute.txt +++ b/NSF2SQL/obj/Debug/NSF2SQL.csproj.FileListAbsolute.txt @@ -23,3 +23,17 @@ C:\Users\tjbrix\Google Drive\Tonys Stuff\Projects\CS\NSF2SQL\NSF2SQL\bin\Debug\N C:\Users\tjbrix\Google Drive\Tonys Stuff\Projects\CS\NSF2SQL\NSF2SQL\bin\Debug\NSF2SQL.pdb C:\Users\tjbrix\Google Drive\Tonys Stuff\Projects\CS\NSF2SQL\NSF2SQL\bin\Debug\MySql.Data.dll C:\Users\tjbrix\Google Drive\Tonys Stuff\Projects\CS\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.csprojResolveAssemblyReference.cache +C:\dev\NSF2SQL\NSF2SQL\bin\Debug\NSF2SQL.exe.config +C:\dev\NSF2SQL\NSF2SQL\bin\Debug\NSF2SQL.exe +C:\dev\NSF2SQL\NSF2SQL\bin\Debug\NSF2SQL.pdb +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\Interop.Domino.dll +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.csproj.ResolveComReference.cache +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.Form1.resources +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.Properties.Resources.resources +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.csproj.GenerateResource.cache +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.csproj.CoreCompileInputs.cache +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.csproj.CopyComplete +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.exe +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.pdb +C:\dev\NSF2SQL\NSF2SQL\obj\Debug\NSF2SQL.ExportTarget.resources +C:\dev\NSF2SQL\NSF2SQL\bin\Debug\MySql.Data.dll diff --git a/NSF2SQL/obj/Debug/NSF2SQL.csproj.GenerateResource.Cache b/NSF2SQL/obj/Debug/NSF2SQL.csproj.GenerateResource.Cache index b7cbbaf..868ba73 100644 Binary files a/NSF2SQL/obj/Debug/NSF2SQL.csproj.GenerateResource.Cache and b/NSF2SQL/obj/Debug/NSF2SQL.csproj.GenerateResource.Cache differ diff --git a/NSF2SQL/obj/Debug/NSF2SQL.csproj.ResolveComReference.cache b/NSF2SQL/obj/Debug/NSF2SQL.csproj.ResolveComReference.cache index dfae5a3..465bad8 100644 Binary files a/NSF2SQL/obj/Debug/NSF2SQL.csproj.ResolveComReference.cache and b/NSF2SQL/obj/Debug/NSF2SQL.csproj.ResolveComReference.cache differ diff --git a/NSF2SQL/obj/Debug/NSF2SQL.exe b/NSF2SQL/obj/Debug/NSF2SQL.exe index f97994d..86d4d83 100644 Binary files a/NSF2SQL/obj/Debug/NSF2SQL.exe and b/NSF2SQL/obj/Debug/NSF2SQL.exe differ diff --git a/NSF2SQL/obj/Debug/NSF2SQL.pdb b/NSF2SQL/obj/Debug/NSF2SQL.pdb index bc350e2..de4fbed 100644 Binary files a/NSF2SQL/obj/Debug/NSF2SQL.pdb and b/NSF2SQL/obj/Debug/NSF2SQL.pdb differ diff --git a/NSF2SQL/packages.config b/NSF2SQL/packages.config new file mode 100644 index 0000000..6a29821 --- /dev/null +++ b/NSF2SQL/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file