/// <summary> /// Loads the columns which are specified for this table and which have allowed data types. /// </summary> /// <param name="db">The database.</param> /// <param name="table">The table.</param> /// <param name="allowedTypes">The allowed types.</param> /// <returns></returns> /// <exception cref="Exception">MDF File is missing required columns or the column's data type is invalid in table {table.Name}: {missingColumns}.</exception> private IOrderedEnumerable <Column> GetAllowedColumns(Database db, Table table) { var allowedColumns = F1SqlSchemaDefiniton.Columns[table.Name].Select(n => n.ToLower()).ToList(); var columns = db.Dmvs.Columns .Where(c => c.ObjectID == table.ObjectID) .Where(c => allowedColumns.Contains(c.Name.ToLower())) .Where(c => _allowedTypes.Contains(c.SystemTypeID)) .OrderBy(c => c.Name); int columnCount = columns.Count(); if (columnCount != allowedColumns.Count) { // Halt and explain that the MDF file is invalid. var presentColumns = columns.Select(c => c.Name.ToLower()).ToList(); var missingColumns = ListUtilities.GetMissingValues(presentColumns, allowedColumns); throw new Exception($"MDF File is missing required columns or the column's data type is invalid in table {table.Name}: {missingColumns}."); } return(columns); }
/// <summary> /// Initializes a new instance of the <see cref="F1SqlDatabase"/> class. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="loadData">if set to <c>true</c> [load data].</param> /// <exception cref="Exception">MDF File is missing required tables: {missingTables}.</exception> public F1SqlDatabase(string fileName, bool loadData = false) { using (var db = new Database(fileName)) { Name = db.Name; // Only read tables specified in the allowed table list so that we don't load a bunch of // meaningless data in memory. var allowedTables = F1SqlSchemaDefiniton.Tables.Select(n => n.ToLower()).ToList(); var tables = db.Dmvs.Tables .Where(t => allowedTables.Contains(t.Name.ToLower())) .OrderBy(t => t.Name); int tableCount = tables.Count(); if (tableCount != allowedTables.Count) { // Halt and explain that the MDF file is invalid. var presentTables = tables.Select(t => t.Name.ToLower()).ToList(); var missingTables = ListUtilities.GetMissingValues(presentTables, allowedTables); throw new Exception($"MDF File is missing required tables: {missingTables}."); } // Get allowed data types (these are used when fetching column data). var allowedTypes = db.Dmvs.Types .Where(t => F1SqlSchemaDefiniton.SupportedDataTypes.Contains(t.Name)) .Select(t => t.SystemTypeID) .ToList(); // Load the tables. Tables = new F1SqlTable[tableCount]; for (int i = 0; i < tableCount; i++) { var table = tables.ElementAt(i); Tables[i] = new F1SqlTable(db, table, allowedTypes, fileName, loadData); } // Unload the MDF file. db.Dispose(); } }