#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Diagnostics; using System.IO; using System.Text; using System.Xml.Serialization; using MiniSqlQuery.Core.Properties; namespace MiniSqlQuery.Core { /// /// Some basic helper functions. /// public static class Utility { /// /// Writes a default file if none present. /// public static void CreateConnectionStringsIfRequired() { string filename = GetConnectionStringFilename(); if (!File.Exists(filename)) { File.WriteAllText(filename, Resources.DefaultConnectionDefinitionFile); } } /// /// Resolves the full filename of the connection string file, by default in the application data folder /// for "MiniSqlQuery", e.g. "C:\Users\(username)\AppData\Roaming\MiniSqlQuery\connections.xml". /// Allows for the override vis the "MiniSqlQuery.Core.dll.config" file "DefaultConnectionDefinitionFilename" /// setting. /// /// A filename. public static string GetConnectionStringFilename() { string filename = ApplicationServices.Instance.Settings.DefaultConnectionDefinitionFilename; if (!string.IsNullOrEmpty(filename) && File.Exists(filename)) { return filename; } string folder = GetAppFolderPath(); filename = Path.Combine(folder, "connections.xml"); return filename; } /// /// Returns an array of SQL provider types supported by the current platform. /// /// An array of SQL provider types. public static string[] GetSqlProviderNames() { DataTable providers = DbProviderFactories.GetFactoryClasses(); var providerNames = new List(); foreach (DataRow row in providers.Rows) { providerNames.Add(row["InvariantName"].ToString()); } return providerNames.ToArray(); } /// /// Loads the connection string data from the file. /// /// The text file contents as a single string. /// public static string LoadConnections() { string filename = GetConnectionStringFilename(); string data = File.ReadAllText(filename); return data; } /// /// Loads the db connection definitions from an XML file. /// /// A instance or null if the file does not exist. public static DbConnectionDefinitionList LoadDbConnectionDefinitions() { string filename = GetConnectionStringFilename(); DbConnectionDefinitionList definitionList = null; if (File.Exists(filename)) { definitionList = DbConnectionDefinitionList.FromXml(File.ReadAllText(filename)); } return definitionList; } /// /// Attempts to convert a database object name to it's "bracketed for", e.g. "Name" -> "[Name]". /// /// The name of the object. /// The SQL friendly conversion. public static string MakeSqlFriendly(string name) { if (name == null) { return string.Empty; } if (!name.StartsWith("[") && (name.Contains(" ") || name.Contains("$"))) { // TODO - reserved wods? return string.Concat("[", name, "]"); } return name; } /// /// The render safe schema object name. /// /// The schema. /// The object name. /// The render safe schema object name. public static string RenderSafeSchemaObjectName(string schema, string objectName) { if (string.IsNullOrEmpty(schema)) { return string.Format("[{0}]", objectName); } return string.Format("[{0}].[{1}]", schema, objectName); } /// /// Saves the to the connection string file. /// /// The contents of the file. /// public static void SaveConnections(DbConnectionDefinitionList definitionList) { string filename = GetConnectionStringFilename(); string newXml = definitionList.ToXml(); File.WriteAllText(filename, newXml); } /// /// Shows the URL in the defaut browser. /// /// The URL to display. public static void ShowUrl(string url) { Process.Start(url); } /// /// Serializes the . /// /// The type. /// The object to serialize. /// A UTF8 XML string representing . public static string ToXml(T obj) { using (StringWriter sw = new StringWriterWithEncoding(Encoding.UTF8)) { var serializer = new XmlSerializer(typeof(T)); serializer.Serialize(sw, obj); return sw.ToString(); } } /// /// Resolves the "(application data path)\MiniSqlQuery" for this user. /// /// A folder path. private static string GetAppFolderPath() { string folder = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Resources.ShortAppTitle); if (Directory.Exists(folder) == false) { Directory.CreateDirectory(folder); } return folder; } } }