diff --git a/SpellWork/Extensions/Extensions.cs b/SpellWork/Extensions/Extensions.cs index 992c3d3..3ae3dbc 100644 --- a/SpellWork/Extensions/Extensions.cs +++ b/SpellWork/Extensions/Extensions.cs @@ -304,5 +304,15 @@ namespace SpellWork { return str == String.Empty; } + public static String ToMySqlString(this Object val) + { + if (val == null) + return String.Empty; + + String v = val.ToString(); + if (v.IndexOf(',') > 0) + v = v.Replace(',', '.'); + return v; + } } } diff --git a/SpellWork/Forms/FormMain.Designer.cs b/SpellWork/Forms/FormMain.Designer.cs index bdba237..39efe7b 100644 --- a/SpellWork/Forms/FormMain.Designer.cs +++ b/SpellWork/Forms/FormMain.Designer.cs @@ -209,6 +209,7 @@ // // statusStrip1 // + this._tsmExport = new System.Windows.Forms.ToolStripMenuItem(); this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this._dbConnect, this._status, @@ -250,6 +251,7 @@ this._tsmFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this._Connected, this._tsmSettings, + this._tsmExport, this._tsmExit}); this._tsmFile.Name = "_tsmFile"; this._tsmFile.Size = new System.Drawing.Size(37, 20); @@ -290,6 +292,11 @@ this._tsmAbout.Size = new System.Drawing.Size(113, 22); this._tsmAbout.Text = "About.."; this._tsmAbout.Click += new System.EventHandler(this.About_Click); + // _tsmExport + this._tsmExport.Name = "_tsmExport"; + this._tsmExport.Size = new System.Drawing.Size(152, 22); + this._tsmExport.Text = "Export to SQL"; + this._tsmExport.Click += new System.EventHandler(this._tsmExport_Click); // // tabControl1 // @@ -1883,5 +1890,6 @@ private System.Windows.Forms.ColumnHeader spellfamilymaskC0; private System.Windows.Forms.ColumnHeader spellfamilymaskC1; private System.Windows.Forms.ColumnHeader spellfamilymaskC2; + private System.Windows.Forms.ToolStripMenuItem _tsmExport; } } \ No newline at end of file diff --git a/SpellWork/Forms/FormMain.cs b/SpellWork/Forms/FormMain.cs index 7640c8e..de28f3b 100644 --- a/SpellWork/Forms/FormMain.cs +++ b/SpellWork/Forms/FormMain.cs @@ -634,5 +634,118 @@ namespace SpellWork } #endregion + #region SQL EXPORT + private void _tsmExport_Click(object sender, EventArgs e) + { + Dictionary TypeConvert = new Dictionary(); + TypeConvert.Add("Int32", "INT(11) NOT NULL DEFAULT 0"); + TypeConvert.Add("UInt32", "INT(10) UNSIGNED NOT NULL DEFAULT 0"); + TypeConvert.Add("UInt64", "INT(20) UNSIGNED NOT NULL DEFAULT 0"); + TypeConvert.Add("Single", "FLOAT NOT NULL DEFAULT 0"); + TypeConvert.Add("String", "VARCHAR(512) DEFAULT NULL"); + String[] Property = { "SpellName", "Rank", "SpellNameRank", "Description", "ToolTip" }; + + FileDialog FD = new SaveFileDialog(); + FD.DefaultExt = ".sql"; + FD.FileName = "spells.sql"; + if (FD.ShowDialog() != DialogResult.OK) + return; + + TextWriter streamWriter = new StreamWriter(FD.FileName); + + StringBuilder SB = new StringBuilder(""); ; + + Type ObjType = typeof(SpellEntry); + SpellEntry S = DBC.Spell[11]; + + String TypeName = "", CurrentName = ""; + + //Generate table script + SB.AppendLine("CREATE TABLE spells ("); + FieldInfo[] fields = ObjType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + foreach (FieldInfo f in fields) + { + + TypeName = f.FieldType.Name; + CurrentName = f.Name; + if (f.FieldType.IsArray) + { + Type t = f.FieldType.GetElementType(); + TypeName = t.Name; + Array a = (Array)f.GetValue(S); + for (int i = 0; i < a.Length; i++) + SB.AppendFormatLine("{0}{1} {2},", CurrentName, i.ToString(), TypeConvert[TypeName]); + } + else + SB.AppendFormatLine("{0} {1},", CurrentName, TypeConvert[TypeName]); + + } + List PInfo = new List(); + foreach (string pname in Property) + { + PropertyInfo p = ObjType.GetProperty(pname); + if (p != null) + PInfo.Add(p); + } + foreach (PropertyInfo p in PInfo) + SB.AppendFormatLine("{0} {1},", p.Name, TypeConvert[p.PropertyType.Name]); + + + SB.AppendLine(" PRIMARY KEY (Id));"); + + + //Generate DATA + + int spell_count = DBC.Spell.Count; + foreach (SpellEntry SE in DBC.Spell.Values) + { + + SB.Append("INSERT INTO spells VALUES ("); + foreach (FieldInfo f in fields) + { + if (f.FieldType.IsArray) + { + + if (f.FieldType.GetElementType().Name == "Single") + { + Single[] a = (Single[])f.GetValue(SE); + for (int i = 0; i < a.Length; i++) + { + SB.AppendFormat("{0},", a[i].ToMySqlString()); + } + } + else + { + Array a = (Array)f.GetValue(SE); + for (int i = 0; i < a.Length; i++) + { + SB.AppendFormat("{0},", a.GetValue(i).ToString()); + } + } + } + else + { + + SB.AppendFormat("{0},", f.GetValue(SE).ToMySqlString()); + } + + } + + int p_count = PInfo.Count; + foreach (PropertyInfo p in PInfo) + { + object o = p.GetValue(SE, null) ?? ""; + SB.AppendFormat("'{0}'{1}", o.ToString().Replace("'", "''"), ((--p_count) != 0 ? "," : ")")); + } + SB.AppendFormatLine(";"); + + } + + streamWriter.Write(SB); + streamWriter.Close(); + + } + #endregion + } }