diff --git a/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Commands.uexml b/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Commands.uexml new file mode 100644 index 00000000..f909936e --- /dev/null +++ b/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Commands.uexml @@ -0,0 +1,198 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Views/DesignView.glade b/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Views/DesignView.glade new file mode 100644 index 00000000..87572296 --- /dev/null +++ b/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Views/DesignView.glade @@ -0,0 +1,350 @@ + + + + + + + + + + + + + + + + + + + + False + + + + + + True + False + vertical + + + True + False + 8 + 8 + 8 + 8 + 8 + 8 + + + True + False + _Name + True + 0 + + + 0 + 0 + + + + + True + False + _Description + True + 0 + 0 + + + 0 + 1 + + + + + True + True + + + 1 + 0 + + + + + True + True + True + True + in + + + True + True + + + + + 1 + 1 + + + + + False + True + 8 + 0 + + + + + True + True + + + True + False + vertical + + + True + False + + + True + False + Add + _Add + True + gtk-add + + + False + True + + + + + True + False + Edit + _Edit + True + gtk-edit + + + False + True + + + + + True + False + Remove + _Remove + True + gtk-remove + + + False + True + + + + + True + False + + + False + True + + + + + True + False + Move Up + Move _Up + True + gtk-go-up + + + False + True + + + + + True + False + Move Down + Move _Down + True + gtk-go-down + + + False + True + + + + + False + True + 0 + + + + + True + True + in + + + True + True + tsColumns + + + + + + True + Name + True + True + 0 + + + + 0 + + + + + + + True + Data type + True + True + 1 + + + + 1 + + + + + + + True + Not null + True + True + 2 + + + + 2 + + + + + + + True + Identity + True + True + 3 + + + + 3 + + + + + + + True + Default value + True + True + 4 + + + + 4 + + + + + + + + + True + True + 1 + + + + + + + True + False + Columns + + + False + + + + + + + + True + False + Constraints + + + 1 + False + + + + + + + + True + False + Indexes + + + 2 + False + + + + + True + True + 1 + + + + + + diff --git a/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/DatabaseEditor.glade b/Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Views/ScriptView.glade similarity index 100% rename from Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/DatabaseEditor.glade rename to Content/UniversalEditor.Content.PlatformIndependent/Editors/Database/Views/ScriptView.glade diff --git a/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj b/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj index f6c2fd17..f317fb1d 100644 --- a/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj +++ b/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj @@ -723,7 +723,6 @@ - @@ -750,6 +749,9 @@ + + + @@ -818,6 +820,7 @@ + diff --git a/Libraries/UniversalEditor.UserInterface/Editors/Database/DatabaseEditor.cs b/Libraries/UniversalEditor.UserInterface/Editors/Database/DatabaseEditor.cs index 2c6fa957..4a15bbc2 100644 --- a/Libraries/UniversalEditor.UserInterface/Editors/Database/DatabaseEditor.cs +++ b/Libraries/UniversalEditor.UserInterface/Editors/Database/DatabaseEditor.cs @@ -24,7 +24,8 @@ using System.Collections.Generic; using MBS.Framework.UserInterface; using MBS.Framework.UserInterface.Controls; - +using MBS.Framework.UserInterface.Dialogs; +using MBS.Framework.UserInterface.Layouts; using UniversalEditor.ObjectModels.Database; using UniversalEditor.UserInterface; @@ -33,23 +34,61 @@ namespace UniversalEditor.Editors.Database /// /// Provides a UWT-based for manipulating database files. /// - [ContainerLayout("~/Editors/Database/DatabaseEditor.glade")] public class DatabaseEditor : Editor { - private SyntaxTextBox txtQuery; - private ListView lvResults; - private static EditorReference _er = null; public override EditorReference MakeReference() { if (_er == null) { _er = base.MakeReference(); + _er.Views.Add("Design"); + _er.Views.Add("Script"); _er.SupportedObjectModels.Add(typeof(DatabaseObjectModel)); } return _er; } + public DatabaseEditor() + { + Layout = new BoxLayout(Orientation.Vertical); + + DesignView = new Views.DesignView(); + Controls.Add(DesignView, new BoxLayout.Constraints(true, true)); + + ScriptView = new Views.ScriptView(); + Controls.Add(ScriptView, new BoxLayout.Constraints(true, true)); + + DesignView.Editor = this; + ScriptView.Editor = this; + + ScriptView.Visible = false; + } + + protected override void OnViewChanged(EditorViewChangedEventArgs e) + { + base.OnViewChanged(e); + + switch (e.NewView.Title) + { + case "Design": + { + DesignView.Visible = true; + ScriptView.Visible = false; + break; + } + case "Script": + { + DesignView.Visible = false; + ScriptView.Visible = true; + break; + } + } + } + + private Views.DesignView DesignView = null; + private Views.ScriptView ScriptView = null; + public override void UpdateSelections() { throw new NotImplementedException(); @@ -60,10 +99,14 @@ namespace UniversalEditor.Editors.Database throw new NotImplementedException(); } - protected override void OnCreated(EventArgs e) + private string ToString(object value) { - base.OnCreated(e); - OnObjectModelChanged(EventArgs.Empty); + if (value == null) return "NULL"; + if (value is string) + { + return String.Format("'{0}'", value); + } + return value.ToString(); } protected override void OnObjectModelChanged(EventArgs e) @@ -72,9 +115,12 @@ namespace UniversalEditor.Editors.Database if (!IsCreated) return; + // eww + DesignView.ObjectModel = ObjectModel; + ScriptView.ObjectModel = ObjectModel; + DatabaseObjectModel db = (ObjectModel as DatabaseObjectModel); - if (db == null) - return; + if (db == null) return; EditorDocumentExplorerNode nodeTables = new EditorDocumentExplorerNode("Tables"); @@ -86,7 +132,7 @@ namespace UniversalEditor.Editors.Database EditorDocumentExplorerNode nodeColumns = new EditorDocumentExplorerNode("Columns"); foreach (DatabaseField field in table.Fields) { - EditorDocumentExplorerNode nodeColumn = new EditorDocumentExplorerNode(String.Format("{0} ({1}, default {2})", field.Name, field.Value == null ? (field.DataType != null ? field.DataType.Name : String.Empty) : field.Value.GetType().Name, field.Value == null ? "NULL" : field.Value.ToString())); + EditorDocumentExplorerNode nodeColumn = new EditorDocumentExplorerNode(String.Format("{0} ({1}, default {2})", field.Name, field.Value == null ? (field.DataType != null ? field.DataType.Name : String.Empty) : field.Value.GetType().Name, ToString(field.Value))); nodeColumn.SetExtraData("column", field); nodeColumns.Nodes.Add(nodeColumn); @@ -97,31 +143,87 @@ namespace UniversalEditor.Editors.Database } DocumentExplorer.Nodes.Add(nodeTables); + } - if (db.Tables.Count > 0) + protected internal override void OnDocumentExplorerSelectionChanged(EditorDocumentExplorerSelectionChangedEventArgs e) + { + base.OnDocumentExplorerSelectionChanged(e); + + if (e.Node == null) return; + + DatabaseTable table = e.Node.GetExtraData("table"); + if (table != null) { - List list = new List(); - for (int i = 0; i < db.Tables[0].Fields.Count; i++) - { - list.Add(db.Tables[0].Fields[i].DataType == null ? typeof(string) : db.Tables[0].Fields[i].DataType); - } - DefaultTreeModel tmResults = new DefaultTreeModel(list.ToArray()); - for (int i = 0; i < db.Tables[0].Fields.Count; i++) - { - lvResults.Columns.Add(new ListViewColumnText(tmResults.Columns[i], db.Tables[0].Fields[i].Name)); - } - foreach (DatabaseRecord rec in db.Tables[0].Records) - { - TreeModelRow row = new TreeModelRow(); - for (int c = 0; c < rec.Fields.Count; c++) - { - row.RowColumns.Add(new TreeModelRowColumn(tmResults.Columns[c], rec.Fields[c].Value == null ? "NULL" : rec.Fields[c].Value)); - } - tmResults.Rows.Add(row); - } - lvResults.Model = tmResults; - this.txtQuery.Text = "SELECT * FROM '" + db.Tables[0].Name + "'"; + DesignView.Table = table; } } + + protected override void OnCreated(EventArgs e) + { + base.OnCreated(e); + + DocumentExplorer.BeforeContextMenu += DocumentExplorer_BeforeContextMenu; + OnObjectModelChanged(EventArgs.Empty); + + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_NewTable", delegate (object sender, EventArgs ee) + { + DatabaseObjectModel db = (ObjectModel as DatabaseObjectModel); + if (db != null) + { + int newTableCount = 0; + for (int i = 0; i < db.Tables.Count; i++) + { + if (db.Tables[i].Name.StartsWith("New Table ") && Int32.TryParse(db.Tables[i].Name.Substring("New Table ".Length), out int dummy)) + { + newTableCount++; + } + } + newTableCount++; + + BeginEdit(); + + DatabaseTable dt = new DatabaseTable(); + dt.Name = "New Table " + newTableCount.ToString(); + db.Tables.Add(dt); + + EndEdit(); + + EditorDocumentExplorerNode node = new EditorDocumentExplorerNode(dt.Name); + node.SetExtraData("table", dt); + DocumentExplorer.Nodes[0].Nodes.Add(node); + } + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Design", delegate (object sender, EventArgs ee) + { + ScriptView.Visible = false; + DesignView.Visible = true; + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Select", delegate (object sender, EventArgs ee) + { + DesignView.Visible = false; + ScriptView.Visible = true; + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Edit", delegate (object sender, EventArgs ee) + { + + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Encrypt", delegate (object sender, EventArgs ee) + { + + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Rename", delegate (object sender, EventArgs ee) + { + + }); + Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Delete", delegate (object sender, EventArgs ee) + { + + }); + } + + void DocumentExplorer_BeforeContextMenu(object sender, EditorDocumentExplorerBeforeContextMenuEventArgs e) + { + e.ContextMenuCommandID = "DatabaseEditor_ContextMenu_Table"; + } } } diff --git a/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableMode.cs b/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableMode.cs new file mode 100644 index 00000000..1d96d23f --- /dev/null +++ b/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableMode.cs @@ -0,0 +1,38 @@ +// +// ScriptTableMode.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.Editors.Database +{ + public enum ScriptTableMode + { + Create, + Alter, + Drop, + DropCreate, + + Select, + Insert, + Update, + Delete, + + Execute + } +} diff --git a/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableTo.cs b/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableTo.cs new file mode 100644 index 00000000..1a53e1fb --- /dev/null +++ b/Libraries/UniversalEditor.UserInterface/Editors/Database/ScriptTableTo.cs @@ -0,0 +1,31 @@ +// +// ScriptTableTo.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.Editors.Database +{ + public enum ScriptTableTo + { + Window, + File, + Clipboard, + Job + } +} diff --git a/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/DesignView.cs b/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/DesignView.cs new file mode 100644 index 00000000..fc4a3d7a --- /dev/null +++ b/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/DesignView.cs @@ -0,0 +1,89 @@ +// +// DesignView.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using MBS.Framework.UserInterface; +using MBS.Framework.UserInterface.Controls; +using UniversalEditor.ObjectModels.Database; +using UniversalEditor.UserInterface; + +namespace UniversalEditor.Editors.Database.Views +{ + [ContainerLayout("~/Editors/Database/Views/DesignView.glade")] + public class DesignView : View + { + private TextBox txtName; + private TextBox txtDescription; + private Toolbar tbColumns; + private ListView tvColumns; + + private DatabaseTable _Table = null; + public DatabaseTable Table { get { return _Table; } set { _Table = value; Update(); } } + + private bool _InhibitEditing = false; + + [EventHandler(nameof(txtName), "Changed")] + private void txtName_Changed(object sender, EventArgs e) + { + if (_InhibitEditing) return; + if (Table == null) return; + + Editor.BeginEdit(); + + Table.Name = txtName.Text; + + Editor.EndEdit(); + } + + protected override void OnObjectModelChanged(EventArgs e) + { + base.OnObjectModelChanged(e); + + DatabaseObjectModel db = (ObjectModel as DatabaseObjectModel); + if (db == null) return; + + Update(); + } + private void Update() + { + if (Table != null) + { + _InhibitEditing = true; + txtName.Text = Table.Name; + _InhibitEditing = false; + + tvColumns.Model.Rows.Clear(); + + for (int i = 0; i < Table.Fields.Count; i++) + { + TreeModelRow row = new TreeModelRow(new TreeModelRowColumn[] + { + new TreeModelRowColumn(tvColumns.Model.Columns[0], Table.Fields[i].Name), + new TreeModelRowColumn(tvColumns.Model.Columns[1], Table.Fields[i].DataType), + new TreeModelRowColumn(tvColumns.Model.Columns[2], Table.Fields[i].Value == null ? "NULL" : "NOT NULL"), + new TreeModelRowColumn(tvColumns.Model.Columns[3], String.Empty), + new TreeModelRowColumn(tvColumns.Model.Columns[4], Table.Fields[i].Value) + }); + tvColumns.Model.Rows.Add(row); + } + } + } + } +} diff --git a/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/ScriptView.cs b/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/ScriptView.cs new file mode 100644 index 00000000..f92ae3e1 --- /dev/null +++ b/Libraries/UniversalEditor.UserInterface/Editors/Database/Views/ScriptView.cs @@ -0,0 +1,370 @@ +// +// ScriptView.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; +using MBS.Framework.UserInterface; +using MBS.Framework.UserInterface.Controls; +using MBS.Framework.UserInterface.Dialogs; +using UniversalEditor.ObjectModels.Database; +using UniversalEditor.UserInterface; + +namespace UniversalEditor.Editors.Database.Views +{ + [ContainerLayout("~/Editors/Database/Views/ScriptView.glade")] + public class ScriptView : View + { + private SyntaxTextBox txtQuery; + private ListView lvResults; + + private string ScriptTable(DatabaseTable dt, ScriptTableMode mode) + { + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + + switch (mode) + { + case ScriptTableMode.Create: + { + sb.Append("CREATE"); + break; + } + case ScriptTableMode.Delete: + { + sb.Append("DELETE "); + break; + } + case ScriptTableMode.Alter: + { + sb.Append("ALTER"); + break; + } + case ScriptTableMode.Drop: + { + sb.Append("DROP"); + break; + } + case ScriptTableMode.DropCreate: + { + sb.Append("DROP"); + break; + } + case ScriptTableMode.Select: + { + sb.Append("SELECT"); + break; + } + case ScriptTableMode.Insert: + { + sb.Append("INSERT INTO"); + break; + } + } + + switch (mode) + { + case ScriptTableMode.Create: + case ScriptTableMode.Alter: + case ScriptTableMode.Drop: + case ScriptTableMode.DropCreate: + { + sb.Append(" TABLE ["); + sb.Append(dt.Name); + sb.Append("]"); + break; + } + case ScriptTableMode.Select: + case ScriptTableMode.Delete: + { + if (mode == ScriptTableMode.Select) + { + sb.Append("* "); + } + sb.Append("FROM '"); + sb.Append(dt.Name); + sb.Append("'"); + break; + } + case ScriptTableMode.Insert: + { + sb.Append(' '); + sb.Append(dt.Name); + sb.Append(" ("); + for (int i = 0; i < dt.Fields.Count; i++) + { + sb.Append(dt.Fields[i].Name); + if (i < dt.Fields.Count - 1) + { + sb.Append(", "); + } + } + sb.Append(") VALUES ("); + for (int i = 0; i < dt.Fields.Count; i++) + { + if (dt.Fields[i].Value == null) + { + sb.Append("NULL"); + } + else + { + if (dt.Fields[i].Value is string) + { + sb.Append("'"); + } + sb.Append(dt.Fields[i].Value); + if (dt.Fields[i].Value is string) + { + sb.Append("'"); + } + } + if (i < dt.Fields.Count - 1) + { + sb.Append(", "); + } + } + sb.Append(')'); + break; + } + } + switch (mode) + { + case ScriptTableMode.Create: + { + sb.Append("\n(\n"); + for (int i = 0; i < dt.Fields.Count; i++) + { + sb.Append('\t'); + sb.Append('['); + sb.Append(dt.Fields[i].Name); + sb.Append(']'); + sb.Append(' '); + sb.Append('['); + sb.Append(TypeToSQL(dt.Fields[i].DataType)); + sb.Append(']'); + sb.Append(' '); + sb.Append("DEFAULT "); + if (dt.Fields[i].Value != null) + { + if (dt.Fields[i].Value is string) sb.Append('\''); + sb.Append(dt.Fields[i].Value.ToString()); + if (dt.Fields[i].Value is string) sb.Append('\''); + } + else + { + sb.Append("NULL"); + } + if (i < dt.Fields.Count - 1) + sb.AppendLine(", "); + } + sb.Append("\n)"); + break; + } + } + sb.Append(';'); + return sb.ToString(); + } + + private void ScriptTable(DatabaseTable dt, ScriptTableMode mode, ScriptTableTo where) + { + string tableText = ScriptTable(dt, mode); + if (mode == ScriptTableMode.DropCreate) + { + tableText = ScriptTable(dt, ScriptTableMode.Drop) + "\r\n\r\n" + ScriptTable(dt, ScriptTableMode.Create); + } + switch (where) + { + case ScriptTableTo.Window: + { + ObjectModels.Text.Plain.PlainTextObjectModel text = new ObjectModels.Text.Plain.PlainTextObjectModel(); + text.Text = tableText; + + Document d = new Document(text, new DataFormats.Text.Plain.PlainTextDataFormat(), null); + d.IsSaved = false; + d.IsChanged = true; + d.Title = "New Query"; + HostApplication.CurrentWindow.OpenFile(new Document[] + { + d + }); + break; + } + case ScriptTableTo.File: + { + FileDialog dlg = new FileDialog(); + dlg.Mode = FileDialogMode.Save; + dlg.FileNameFilters.Add("SQL script", "*.sql"); + dlg.Text = "Script Table to File"; + if (dlg.ShowDialog() == DialogResult.OK) + { + System.IO.File.WriteAllText(dlg.SelectedFileName, tableText); + } + break; + } + case ScriptTableTo.Clipboard: + { + Clipboard.Default.SetText(tableText); + break; + } + case ScriptTableTo.Job: + { + Clipboard.Default.SetText(tableText); + break; + } + } + } + + private void DatabaseEditor_ContextMenu_Table_Script(object sender, EventArgs ee) + { + CommandEventArgs cea = (ee as CommandEventArgs); + string wpcmdid = cea.Command.ID.Substring("DatabaseEditor_ContextMenu_Table_Script_".Length); + string wpcmd_type = wpcmdid.Substring(0, wpcmdid.IndexOf('_')); + string wpcmd_dest = wpcmdid.Substring(wpcmdid.IndexOf('_') + 1); + + ScriptTableMode mode = ScriptTableMode.Create; + ScriptTableTo dest = ScriptTableTo.Window; + switch (wpcmd_type.ToLower()) + { + case "alter": mode = ScriptTableMode.Alter; break; + case "create": mode = ScriptTableMode.Create; break; + case "delete": mode = ScriptTableMode.Delete; break; + case "drop": mode = ScriptTableMode.Drop; break; + case "dropcreate": mode = ScriptTableMode.DropCreate; break; + case "execute": mode = ScriptTableMode.Execute; break; + case "insert": mode = ScriptTableMode.Insert; break; + case "select": mode = ScriptTableMode.Select; break; + case "update": mode = ScriptTableMode.Update; break; + } + switch (wpcmd_dest.ToLower()) + { + case "newwindow": dest = ScriptTableTo.Window; break; + case "clipboard": dest = ScriptTableTo.Clipboard; break; + case "file": dest = ScriptTableTo.File; break; + case "job": dest = ScriptTableTo.Job; break; + } + DatabaseTable dt = Editor.DocumentExplorer.SelectedNode?.GetExtraData("table"); + if (dt != null) + { + ScriptTable(dt, mode, dest); + } + } + + protected override void OnCreated(EventArgs e) + { + base.OnCreated(e); + txtQuery.ContextMenuCommandID = "DatabaseEditor_ContextMenu_Query"; + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Create_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Create_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Create_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Create_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Alter_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Alter_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Alter_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Alter_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Drop_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Drop_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Drop_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Drop_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_DropCreate_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_DropCreate_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_DropCreate_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_DropCreate_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Select_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Select_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Select_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Select_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Insert_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Insert_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Insert_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Insert_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Update_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Update_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Update_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Update_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Delete_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Delete_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Delete_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Delete_Job", DatabaseEditor_ContextMenu_Table_Script); + + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Execute_NewWindow", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Execute_File", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Execute_Clipboard", DatabaseEditor_ContextMenu_Table_Script); + Editor.Context.AttachCommandEventHandler("DatabaseEditor_ContextMenu_Table_Script_Execute_Job", DatabaseEditor_ContextMenu_Table_Script); + + OnObjectModelChanged(EventArgs.Empty); + } + + private static string TypeToSQL(Type dataType) + { + if (dataType == typeof(string)) + { + return "TEXT"; + } + else if (dataType == typeof(short)) + { + return "SMALLINT"; + } + else if (dataType == typeof(int)) + { + return "INT"; + } + return "ANY"; + } + + protected override void OnObjectModelChanged(EventArgs e) + { + DatabaseObjectModel db = (ObjectModel as DatabaseObjectModel); + if (db == null) + return; + + if (db.Tables.Count > 0) + { + List list = new List(); + for (int i = 0; i < db.Tables[0].Fields.Count; i++) + { + list.Add(db.Tables[0].Fields[i].DataType == null ? typeof(string) : db.Tables[0].Fields[i].DataType); + } + DefaultTreeModel tmResults = new DefaultTreeModel(list.ToArray()); + for (int i = 0; i < db.Tables[0].Fields.Count; i++) + { + lvResults.Columns.Add(new ListViewColumnText(tmResults.Columns[i], db.Tables[0].Fields[i].Name)); + } + foreach (DatabaseRecord rec in db.Tables[0].Records) + { + TreeModelRow row = new TreeModelRow(); + for (int c = 0; c < rec.Fields.Count; c++) + { + row.RowColumns.Add(new TreeModelRowColumn(tmResults.Columns[c], rec.Fields[c].Value == null ? "NULL" : rec.Fields[c].Value)); + } + tmResults.Rows.Add(row); + } + lvResults.Model = tmResults; + this.txtQuery.Text = "SELECT * FROM '" + db.Tables[0].Name + "'"; + } + } + + } +} diff --git a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj index 84ff8e5c..2136d5e0 100644 --- a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj +++ b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj @@ -133,6 +133,10 @@ + + + + @@ -188,6 +192,7 @@ +