using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.IO; namespace Sdaleo { /// /// For SQL Server you can fill all locations, for SQLCE there are no Views,Sprocs /// FullResourceLocationContainingScripts, is the full resource folder where the sql scripts are located /// f.e. Application.DatabaseLayer.SQLScripts /// SQLScript_FileExt specifies which files to look for (f.e. .sql or .sqlce) /// public struct DBResource { public Assembly AssemblyContainingResources; public string FullResourceLocationContaingScripts_Tables; public string FullResourceLocationContaingScripts_Views; public string FullResourceLocationContaingScripts_Sprocs; public string SQLScript_FileExt; /// /// Useful for SQLCE DBResources, which only contains Tables /// /// the assembly that contains the resources /// The Location of where the table .sqlce scripts are located, can be blank, if not used, but that would not make sense /// file extension of the scripts, like .sqlce public DBResource(Assembly asm, string FRLCS_Tables, string FileExt = ".sqlce") { this.AssemblyContainingResources = asm; this.FullResourceLocationContaingScripts_Tables = FRLCS_Tables; this.FullResourceLocationContaingScripts_Views = String.Empty; this.FullResourceLocationContaingScripts_Sprocs = String.Empty; this.SQLScript_FileExt = FileExt; } /// /// Useful for SQLServer DBResources, which can contain Tables, Views, Sprocs /// /// the assembly that contains the resources /// The Location of where the table .sql scripts are located, can be blank, if not used /// The Location of where the views .sql scripts are located, can be blank, if not used /// The Location of where the sprocs .sql scripts are located, can be blank, if not used /// file extension of the scripts, like .sql public DBResource(Assembly asm, string FRLCS_Tables, string FRLCS_Views, string FRLCS_Sprocs, string FileExt = ".sql") { this.AssemblyContainingResources = asm; this.FullResourceLocationContaingScripts_Tables = FRLCS_Tables; this.FullResourceLocationContaingScripts_Views = FRLCS_Views; this.FullResourceLocationContaingScripts_Sprocs = FRLCS_Sprocs; this.SQLScript_FileExt = FileExt; } } /// /// This class allows external callers to set SQL Resource Scripts, /// for easy table, view, sprocs creation and updating /// public static class DBResources { #region Private Statics private static Dictionary _DBResources = new Dictionary(); #endregion #region DBResource Add/Get/Delete /// /// Used to Add a DBResource into DatabaseLayer /// /// Unique Name to identify the Resource by /// a DBResource structure that must contain the assembly and at least one resource, and the file extension for the scripts /// true if added, false otherwise public static bool AddDBResource(string uniqueName, DBResource dbResource) { if (!String.IsNullOrEmpty(uniqueName) && !_DBResources.ContainsKey(uniqueName)) { _DBResources.Add(uniqueName, dbResource); return true; } return false; } /// /// Used to Delete a DBResource from DatabaseLayer /// /// Unique Name to identify the Resource by /// true if deleted, false otherwise public static bool DeleteDBResource(string uniqueName) { if (!String.IsNullOrEmpty(uniqueName) && _DBResources.ContainsKey(uniqueName)) { _DBResources.Remove(uniqueName); return true; } return false; } /// /// Used to Retrieve a DBResource from DatabaseLayer /// /// Unique Name to identify the Resource by /// the DBResource that was previously added by AddDBResource(), otherwise throws an error public static DBResource GetDBResource(string uniqueName) { if (!String.IsNullOrEmpty(uniqueName) && _DBResources.ContainsKey(uniqueName)) return _DBResources[uniqueName]; else throw new Exception("DBResource not found. Call AddDBResource First"); } #endregion #region Internal Table, View, Sprocs, Resource Getter Functions /// /// Returns the Table Scripts for a specified DBResource /// /// Unique Name to identify the Resource by /// an array of FRLCS Script Names internal static string[] GetTableDBResourceScripts(string uniqueName) { if (!String.IsNullOrEmpty(GetDBResource(uniqueName).FullResourceLocationContaingScripts_Tables)) return GetScriptsFromResourceLocation(GetDBResource(uniqueName).AssemblyContainingResources, GetDBResource(uniqueName).FullResourceLocationContaingScripts_Tables, GetDBResource(uniqueName).SQLScript_FileExt); return null; } /// /// Returns the Views Scripts for a specified DBResource /// /// Unique Name to identify the Resource by /// an array of FRLCS Script Names internal static string[] GetViewsDBResourceScripts(string uniqueName) { if (!String.IsNullOrEmpty(GetDBResource(uniqueName).FullResourceLocationContaingScripts_Views)) return GetScriptsFromResourceLocation(GetDBResource(uniqueName).AssemblyContainingResources, GetDBResource(uniqueName).FullResourceLocationContaingScripts_Views, GetDBResource(uniqueName).SQLScript_FileExt); return null; } /// /// Returns the Sprocs Scripts for a specified DBResource /// /// Unique Name to identify the Resource by /// an array of FRLCS Script Names internal static string[] GetSprocsDBResourceScripts(string uniqueName) { if(!String.IsNullOrEmpty(GetDBResource(uniqueName).FullResourceLocationContaingScripts_Sprocs)) return GetScriptsFromResourceLocation(GetDBResource(uniqueName).AssemblyContainingResources, GetDBResource(uniqueName).FullResourceLocationContaingScripts_Sprocs, GetDBResource(uniqueName).SQLScript_FileExt); return null; } #endregion #region Internal ReadScript /// /// Use this to read the contents of a Script from a specified Resource /// /// Unique Name to identify the Resource by /// the Full Resource Location Name N Path of the Script to read /// the contents of the script, split by '\n', or null if not found internal static string[] Read_FRLCS_Script(string uniqueName, string FRLCS_Script_Name) { using (StreamReader stream = new StreamReader(GetDBResource(uniqueName).AssemblyContainingResources.GetManifestResourceStream(FRLCS_Script_Name))) { if (stream != null) { string txtContent = stream.ReadToEnd(); // Get Rid of carriage returns and new lines string[] retVal = txtContent.Replace("\r","").Split('\n'); // iterate each line and trim whitespaces for (int i = 0; i < retVal.Length; ++i) retVal[i] = retVal[i].Trim(); return retVal; } } return null; } #endregion #region Private Static Helper Functions /// /// Returns the Scripts found from the specified Resource Location /// /// a FRLCS Path to where the Scripts are located /// File Extension used by the scripts /// the FRLCS names of the Scripts, or null if none found private static string[] GetScriptsFromResourceLocation(Assembly asm, string ResourceLocation, string FileExt) { List scriptsFRLCS = new List(); string[] resourceNames = asm.GetManifestResourceNames(); foreach (string ResourceName in resourceNames) { if (ResourceName.StartsWith(ResourceLocation) && ResourceName.EndsWith(FileExt)) scriptsFRLCS.Add(ResourceName); // found } scriptsFRLCS.Sort(); return scriptsFRLCS.ToArray(); } #endregion } }