From 3a2711c70a89cffbd717d05d8c6dea144a5face8 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 12 Oct 2020 22:19:40 -0400 Subject: [PATCH] working on supporting more database dataformats --- .../Associations/Database/SQL.uexml | 20 ++ .../DataFormats/SQL/SQLDataFormat.cs | 190 ++++++++++++++++++ .../DataFormats/SQL/SQLQuotingCharacter.cs | 35 ++++ .../DataFormats/SQL/SQLSettings.cs | 29 +++ .../Properties/AssemblyInfo.cs | 46 +++++ .../UniversalEditor.Plugins.Database.csproj | 59 ++++++ UniversalEditor.sln | 7 + 7 files changed, 386 insertions(+) create mode 100644 Plugins/UniversalEditor.Plugins.Database/Associations/Database/SQL.uexml create mode 100644 Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLDataFormat.cs create mode 100644 Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLQuotingCharacter.cs create mode 100644 Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLSettings.cs create mode 100644 Plugins/UniversalEditor.Plugins.Database/Properties/AssemblyInfo.cs create mode 100644 Plugins/UniversalEditor.Plugins.Database/UniversalEditor.Plugins.Database.csproj diff --git a/Plugins/UniversalEditor.Plugins.Database/Associations/Database/SQL.uexml b/Plugins/UniversalEditor.Plugins.Database/Associations/Database/SQL.uexml new file mode 100644 index 00000000..0ed215a8 --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/Associations/Database/SQL.uexml @@ -0,0 +1,20 @@ + + + + + + + + *.sql + + + + + + + + + + + + \ No newline at end of file diff --git a/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLDataFormat.cs b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLDataFormat.cs new file mode 100644 index 00000000..664463e4 --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLDataFormat.cs @@ -0,0 +1,190 @@ +// +// MyClass.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 System.Text; +using UniversalEditor.DataFormats.Text.Plain; +using UniversalEditor.IO; +using UniversalEditor.ObjectModels.Database; +using UniversalEditor.ObjectModels.Text.Plain; + +namespace UniversalEditor.Plugins.Database.DataFormats.SQL +{ + public class SQLDataFormat : DataFormat + { + private static DataFormatReference _dfr = null; + protected override DataFormatReference MakeReferenceInternal() + { + if (_dfr == null) + { + _dfr = new DataFormatReference(GetType()); + _dfr.Capabilities.Add(typeof(DatabaseObjectModel), DataFormatCapabilities.All); + } + return _dfr; + } + + public SQLSettings Settings { get; set; } = new SQLSettings(); + public SQLQuotingCharacter QuotingCharacter { get; set; } = SQLQuotingCharacter.Brackets; + + protected override void LoadInternal(ref ObjectModel objectModel) + { + DatabaseObjectModel db = (objectModel as DatabaseObjectModel); + if (db == null) throw new ObjectModelNotSupportedException(); + + Reader reader = Accessor.Reader; + while (!reader.EndOfStream) + { + string token = ReadToken(reader).Trim(); + if (token.StartsWith("/*") && token.EndsWith("*/")) + { + // is comment + continue; + } + switch (token.ToLower()) + { + case "use": + { + string dbName = ReadToken(reader); + db.Name = dbName; + break; + } + case "go": + { + break; + } + case "set": + { + StringBuilder sbVariableName = new StringBuilder(); + string variableName = ReadToken(reader); + string value = ReadToken(reader); + + break; + } + case "create": + { + string objectType = ReadToken(reader).ToLower(); + switch (objectType) + { + case "table": + { + DatabaseTable table = new DatabaseTable(); + + string tableName = ReadToken(reader, out string rest); + table.Name = tableName; + + List fullyQualifiedTableName = new List(); + fullyQualifiedTableName.Add(tableName); + while (rest == ".") + { + tableName = ReadToken(reader, out rest); + fullyQualifiedTableName.Add(tableName); + } + + if (rest == "(") + { + do + { + DatabaseField field = new DatabaseField(); + + string columnName = ReadToken(reader); + field.Name = columnName; + + string dataType = ReadToken(reader, out rest); + if (rest == "(") + { + // next token is dataLength + string dataLength = ReadToken(reader); + } + + string paramz = ReadToken(reader, out rest); + if (paramz.ToLower() == "not") + { + // not what? + paramz = ReadToken(reader, out rest); + if (paramz.ToLower() == "null") + { + + } + } + + table.Fields.Add(field); + } + while (rest == ","); + } + + db.Tables.Add(table); + break; + } + } + break; + } + } + } + } + + private string ReadToken(Reader reader) + { + string rest = null; + return ReadToken(reader, out rest); + } + private string ReadToken(Reader reader, out string rest) + { + string[] tokenDelimiters = new string[] { " ", "\n", ".", "(", ")" }; + + string token = reader.ReadUntil(tokenDelimiters, out rest).Trim(); + while (String.IsNullOrEmpty(token)) + token = reader.ReadUntil(tokenDelimiters, out rest).Trim(); + + if (token.StartsWith("/*")) + { + rest = reader.ReadUntil("*/", true); + string cmntend = reader.ReadFixedLengthString(2); + token += rest + cmntend; + return token; + } + if (token.StartsWith(Settings.QuotingCharacterStart) && !token.EndsWith(Settings.QuotingCharacterEnd)) + { + StringBuilder sbWholeToken = new StringBuilder(); + sbWholeToken.Append(token); + while (!token.Contains(Settings.QuotingCharacterEnd)) + { + token = ReadToken(reader); + sbWholeToken.Append(' '); + sbWholeToken.Append(token); + } + token = sbWholeToken.ToString(); + } + if (token.StartsWith(Settings.QuotingCharacterStart) && token.Contains(Settings.QuotingCharacterEnd)) + { + token = token.Substring(Settings.QuotingCharacterStart.Length, token.IndexOf(Settings.QuotingCharacterEnd) - Settings.QuotingCharacterStart.Length); + } + + if (token.EndsWith("\r")) + token = token.Trim(); + return token; + } + + protected override void SaveInternal(ObjectModel objectModel) + { + throw new NotImplementedException(); + } + } +} diff --git a/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLQuotingCharacter.cs b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLQuotingCharacter.cs new file mode 100644 index 00000000..92edfae5 --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLQuotingCharacter.cs @@ -0,0 +1,35 @@ +// +// SQLQuotingCharacter.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.Plugins.Database.DataFormats.SQL +{ + public enum SQLQuotingCharacter + { + /// + /// Brackets (used in Microsoft SQL Server) + /// + Brackets, + /// + /// Backticks (used in MySQL) + /// + Backticks + } +} diff --git a/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLSettings.cs b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLSettings.cs new file mode 100644 index 00000000..b6a226cc --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/DataFormats/SQL/SQLSettings.cs @@ -0,0 +1,29 @@ +// +// SQLSettings.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.Plugins.Database.DataFormats.SQL +{ + public class SQLSettings + { + public string QuotingCharacterStart { get; set; } = "["; + public string QuotingCharacterEnd { get; set; } = "]"; + } +} diff --git a/Plugins/UniversalEditor.Plugins.Database/Properties/AssemblyInfo.cs b/Plugins/UniversalEditor.Plugins.Database/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..55329e9d --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/Properties/AssemblyInfo.cs @@ -0,0 +1,46 @@ +// +// AssemblyInfo.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.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("UniversalEditor.Plugins.Database")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Mike Becker's Software")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Mike Becker's Software")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/Plugins/UniversalEditor.Plugins.Database/UniversalEditor.Plugins.Database.csproj b/Plugins/UniversalEditor.Plugins.Database/UniversalEditor.Plugins.Database.csproj new file mode 100644 index 00000000..95e7eeb8 --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Database/UniversalEditor.Plugins.Database.csproj @@ -0,0 +1,59 @@ + + + + Debug + AnyCPU + {2EDA483D-E413-4CC5-A944-EBB898611268} + Library + UniversalEditor.Plugins.Database + UniversalEditor.Plugins.Database + v4.7 + 4.0.2019.12 + + + true + full + false + ..\..\Output\Debug\Plugins + DEBUG; + prompt + 4 + false + + + true + ..\..\Output\Release\Plugins + prompt + 4 + false + + + + + + + + + + + + + {2D4737E6-6D95-408A-90DB-8DFF38147E85} + UniversalEditor.Core + + + {30467E5C-05BC-4856-AADC-13906EF4CADD} + UniversalEditor.Essential + + + + + + + + + + + + + \ No newline at end of file diff --git a/UniversalEditor.sln b/UniversalEditor.sln index f666cd0c..04675849 100644 --- a/UniversalEditor.sln +++ b/UniversalEditor.sln @@ -203,6 +203,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MBS.Framework.UserInterface EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Plugins.Lighting.UserInterface", "Plugins.UserInterface\UniversalEditor.Plugins.Lighting.UserInterface\UniversalEditor.Plugins.Lighting.UserInterface.csproj", "{77C96685-268E-4CAD-867E-C19BBBEB1F3F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Plugins.Database", "Plugins\UniversalEditor.Plugins.Database\UniversalEditor.Plugins.Database.csproj", "{2EDA483D-E413-4CC5-A944-EBB898611268}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Surodoine", "..\Surodoine\Surodoine\Surodoine.csproj", "{E0897B7B-617A-4709-A4C6-FC0F6B441B2A}" EndProject Global @@ -585,6 +587,10 @@ Global {77C96685-268E-4CAD-867E-C19BBBEB1F3F}.Debug|Any CPU.Build.0 = Debug|Any CPU {77C96685-268E-4CAD-867E-C19BBBEB1F3F}.Release|Any CPU.ActiveCfg = Release|Any CPU {77C96685-268E-4CAD-867E-C19BBBEB1F3F}.Release|Any CPU.Build.0 = Release|Any CPU + {2EDA483D-E413-4CC5-A944-EBB898611268}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2EDA483D-E413-4CC5-A944-EBB898611268}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2EDA483D-E413-4CC5-A944-EBB898611268}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2EDA483D-E413-4CC5-A944-EBB898611268}.Release|Any CPU.Build.0 = Release|Any CPU {E0897B7B-617A-4709-A4C6-FC0F6B441B2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E0897B7B-617A-4709-A4C6-FC0F6B441B2A}.Debug|Any CPU.Build.0 = Debug|Any CPU {E0897B7B-617A-4709-A4C6-FC0F6B441B2A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -684,6 +690,7 @@ Global {63E2F10F-27A6-4BAA-BF4A-4422D0934E91} = {20F315E0-52AE-479F-AF43-3402482C1FC8} {FAE48F29-DB35-4CD6-8A55-6C1FDDFBE6AF} = {63E2F10F-27A6-4BAA-BF4A-4422D0934E91} {77C96685-268E-4CAD-867E-C19BBBEB1F3F} = {7B535D74-5496-4802-B809-89ED88274A91} + {2EDA483D-E413-4CC5-A944-EBB898611268} = {2ED32D16-6C06-4450-909A-40D32DA67FB4} {E0897B7B-617A-4709-A4C6-FC0F6B441B2A} = {20F315E0-52AE-479F-AF43-3402482C1FC8} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution