From 04c0e47d9c1c79a5d474f5d508134ee84f18bd80 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 28 Apr 2018 18:39:09 +0100 Subject: [PATCH] Update DB to version 19: Resolutions are gpu dependant, move to separate table and use an interconnection table, `resolution_by_gpu`. --- Cicm.Database/Operations/Gpu.cs | 9 +- Cicm.Database/Operations/Init.cs | 64 +-- Cicm.Database/Operations/Machine.cs | 84 ++-- Cicm.Database/Operations/Operations.cs | 2 +- Cicm.Database/Operations/Resolution.cs | 418 ++++++++++++++++++++ Cicm.Database/Operations/ResolutionByGpu.cs | 80 ++++ Cicm.Database/Operations/Update.cs | 146 +++++++ Cicm.Database/Schemas/Machine.cs | 4 - Cicm.Database/Schemas/Resolution.cs | 49 +++ Cicm.Database/Schemas/Sql/V19.cs | 145 +++++++ cicm_web/Models/Machine.cs | 4 - cicm_web/Views/Machine/View.cshtml | 33 +- cicm_web/cicm_web.csproj | 2 +- 13 files changed, 921 insertions(+), 119 deletions(-) create mode 100644 Cicm.Database/Operations/Resolution.cs create mode 100644 Cicm.Database/Operations/ResolutionByGpu.cs create mode 100644 Cicm.Database/Schemas/Resolution.cs create mode 100644 Cicm.Database/Schemas/Sql/V19.cs diff --git a/Cicm.Database/Operations/Gpu.cs b/Cicm.Database/Operations/Gpu.cs index 72558442..d5a190da 100644 --- a/Cicm.Database/Operations/Gpu.cs +++ b/Cicm.Database/Operations/Gpu.cs @@ -32,7 +32,6 @@ using System; using System.Collections.Generic; using System.Data; using Cicm.Database.Schemas; -using Console = System.Console; namespace Cicm.Database { @@ -300,6 +299,14 @@ namespace Cicm.Database param9.Value = entry.Transistors; dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + dbcmd.Parameters.Add(param8); + dbcmd.Parameters.Add(param9); return dbcmd; } diff --git a/Cicm.Database/Operations/Init.cs b/Cicm.Database/Operations/Init.cs index 3cdb91d8..12bed6e3 100644 --- a/Cicm.Database/Operations/Init.cs +++ b/Cicm.Database/Operations/Init.cs @@ -49,115 +49,123 @@ namespace Cicm.Database IDbCommand dbCmd = dbCon.CreateCommand(); Console.WriteLine("Creating table `admins`"); - dbCmd.CommandText = V18.Admins; + dbCmd.CommandText = V19.Admins; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `browser_tests`"); - dbCmd.CommandText = V18.BrowserTests; + dbCmd.CommandText = V19.BrowserTests; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `cicm_db`"); - dbCmd.CommandText = V18.CicmDb; + dbCmd.CommandText = V19.CicmDb; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `companies`"); - dbCmd.CommandText = V18.Companies; + dbCmd.CommandText = V19.Companies; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `machines`"); - dbCmd.CommandText = V18.Machines; + dbCmd.CommandText = V19.Machines; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `disk_formats`"); - dbCmd.CommandText = V18.DiskFormats; + dbCmd.CommandText = V19.DiskFormats; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `forbidden`"); - dbCmd.CommandText = V18.Forbidden; + dbCmd.CommandText = V19.Forbidden; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `gpus`"); - dbCmd.CommandText = V18.Gpus; + dbCmd.CommandText = V19.Gpus; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `log`"); - dbCmd.CommandText = V18.Logs; + dbCmd.CommandText = V19.Logs; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `money_donations`"); - dbCmd.CommandText = V18.MoneyDonations; + dbCmd.CommandText = V19.MoneyDonations; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `news`"); - dbCmd.CommandText = V18.News; + dbCmd.CommandText = V19.News; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `owned_computers`"); - dbCmd.CommandText = V18.OwnedComputers; + dbCmd.CommandText = V19.OwnedComputers; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `owned_consoles`"); - dbCmd.CommandText = V18.OwnedConsoles; + dbCmd.CommandText = V19.OwnedConsoles; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_sets`"); - dbCmd.CommandText = V18.InstructionSets; + dbCmd.CommandText = V19.InstructionSets; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_set_extensions`"); - dbCmd.CommandText = V18.InstructionSetExtensions; + dbCmd.CommandText = V19.InstructionSetExtensions; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `processors`"); - dbCmd.CommandText = V18.Processors; + dbCmd.CommandText = V19.Processors; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_set_extensions_by_processor`"); - dbCmd.CommandText = V18.InstructionSetExtensionsByProcessor; + dbCmd.CommandText = V19.InstructionSetExtensionsByProcessor; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `sound_synths`"); - dbCmd.CommandText = V18.SoundSynths; + dbCmd.CommandText = V19.SoundSynths; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `iso3166_1_numeric`"); - dbCmd.CommandText = V18.Iso3166Numeric; + dbCmd.CommandText = V19.Iso3166Numeric; dbCmd.ExecuteNonQuery(); Console.WriteLine("Filling table `iso3166_1_numeric`"); - dbCmd.CommandText = V18.Iso3166NumericValues; + dbCmd.CommandText = V19.Iso3166NumericValues; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating foreign keys for table `companies`"); - dbCmd.CommandText = V18.CompaniesForeignKeys; + dbCmd.CommandText = V19.CompaniesForeignKeys; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating foreign keys for table `machines`"); - dbCmd.CommandText = V18.MachinesForeignKeys; + dbCmd.CommandText = V19.MachinesForeignKeys; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `company_logos`"); - dbCmd.CommandText = V18.CompanyLogos; + dbCmd.CommandText = V19.CompanyLogos; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `company_descriptions`"); - dbCmd.CommandText = V18.CompanyDescriptions; + dbCmd.CommandText = V19.CompanyDescriptions; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `processors_by_machine`"); - dbCmd.CommandText = V18.ProcessorsByMachine; + dbCmd.CommandText = V19.ProcessorsByMachine; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `gpus_by_machine`"); - dbCmd.CommandText = V18.GpusByMachine; + dbCmd.CommandText = V19.GpusByMachine; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `sound_by_machine`"); - dbCmd.CommandText = V18.SoundByMachine; + dbCmd.CommandText = V19.SoundByMachine; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `memory_by_machine`"); - dbCmd.CommandText = V18.MemoryByMachine; + dbCmd.CommandText = V19.MemoryByMachine; + dbCmd.ExecuteNonQuery(); + + Console.WriteLine("Creating table `resolutions`"); + dbCmd.CommandText = V19.Resolutions; + dbCmd.ExecuteNonQuery(); + + Console.WriteLine("Creating table `resolutions_by_gpu`"); + dbCmd.CommandText = V19.ResolutionsByGpu; dbCmd.ExecuteNonQuery(); return true; diff --git a/Cicm.Database/Operations/Machine.cs b/Cicm.Database/Operations/Machine.cs index e1b75a68..259a4292 100644 --- a/Cicm.Database/Operations/Machine.cs +++ b/Cicm.Database/Operations/Machine.cs @@ -215,7 +215,7 @@ namespace Cicm.Database const string SQL = "INSERT INTO machines (company, year, model, colors, res, hdd1, hdd2, hdd3, disk1, cap1, disk2, cap2, " + - "type) VALUES (@company, @year, @model, @colors, @res, @hdd1, @hdd2, @hdd3, @disk1, @cap1, @disk2, " + + "type) VALUES (@company, @year, @model, @colors, @res, @hdd1, @hdd2, @hdd3, @disk1, @cap1, @disk2, " + "@cap2, @type)"; dbcmd.CommandText = SQL; @@ -299,55 +299,47 @@ namespace Cicm.Database IDbDataParameter param4 = dbcmd.CreateParameter(); IDbDataParameter param5 = dbcmd.CreateParameter(); IDbDataParameter param6 = dbcmd.CreateParameter(); - IDbDataParameter param7 = dbcmd.CreateParameter(); - IDbDataParameter param8 = dbcmd.CreateParameter(); - IDbDataParameter param9 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); + IDbDataParameter param8 = dbcmd.CreateParameter(); + IDbDataParameter param9 = dbcmd.CreateParameter(); IDbDataParameter param10 = dbcmd.CreateParameter(); IDbDataParameter param11 = dbcmd.CreateParameter(); - IDbDataParameter param12 = dbcmd.CreateParameter(); - IDbDataParameter param13 = dbcmd.CreateParameter(); param1.ParameterName = "@company"; param2.ParameterName = "@year"; param3.ParameterName = "@model"; - param4.ParameterName = "@colors"; - param5.ParameterName = "@res"; - param6.ParameterName = "@hdd1"; - param7.ParameterName = "@hdd2"; - param8.ParameterName = "@hdd3"; - param9.ParameterName = "@disk1"; - param10.ParameterName = "@cap1"; - param11.ParameterName = "@disk2"; - param12.ParameterName = "@cap2"; - param13.ParameterName = "@type"; + param4.ParameterName = "@hdd1"; + param5.ParameterName = "@hdd2"; + param6.ParameterName = "@hdd3"; + param7.ParameterName = "@disk1"; + param8.ParameterName = "@cap1"; + param9.ParameterName = "@disk2"; + param10.ParameterName = "@cap2"; + param11.ParameterName = "@type"; param1.DbType = DbType.Int32; param2.DbType = DbType.Int32; param3.DbType = DbType.String; param4.DbType = DbType.Int32; - param5.DbType = DbType.String; + param5.DbType = DbType.Int32; param6.DbType = DbType.Int32; - param7.DbType = DbType.Int32; - param8.DbType = DbType.Int32; - param9.DbType = DbType.Int32; + param7.DbType = DbType.Int32; + param8.DbType = DbType.String; + param9.DbType = DbType.Int32; param10.DbType = DbType.String; param11.DbType = DbType.Int32; - param12.DbType = DbType.String; - param13.DbType = DbType.Int32; param1.Value = entry.Company; param2.Value = entry.Year; param3.Value = entry.Model; - param4.Value = entry.Colors; - param5.Value = entry.Resolution; - param6.Value = entry.Hdd1; - param7.Value = entry.Hdd2; - param8.Value = entry.Hdd3; - param9.Value = entry.Disk1; - param10.Value = entry.Cap1; - param11.Value = entry.Disk2; - param12.Value = entry.Cap2; - param13.Value = entry.Type; + param4.Value = entry.Hdd1; + param5.Value = entry.Hdd2; + param6.Value = entry.Hdd3; + param7.Value = entry.Disk1; + param8.Value = entry.Cap1; + param9.Value = entry.Disk2; + param10.Value = entry.Cap2; + param11.Value = entry.Type; dbcmd.Parameters.Add(param1); dbcmd.Parameters.Add(param2); @@ -360,8 +352,6 @@ namespace Cicm.Database dbcmd.Parameters.Add(param9); dbcmd.Parameters.Add(param10); dbcmd.Parameters.Add(param11); - dbcmd.Parameters.Add(param12); - dbcmd.Parameters.Add(param13); return dbcmd; } @@ -374,20 +364,18 @@ namespace Cicm.Database { Machine entry = new Machine { - Id = (int)dataRow["id"], - Company = (int)dataRow["company"], - Year = (int)dataRow["year"], - Model = (string)dataRow["model"], - Colors = (int)dataRow["colors"], - Resolution = (string)dataRow["res"], - Hdd1 = (int)dataRow["hdd1"], - Hdd2 = dataRow["hdd2"] == DBNull.Value ? 0 : (int)dataRow["hdd2"], - Hdd3 = dataRow["hdd3"] == DBNull.Value ? 0 : (int)dataRow["hdd3"], - Disk1 = (int)dataRow["disk1"], - Cap1 = (string)dataRow["cap1"], - Disk2 = dataRow["disk2"] == DBNull.Value ? 0 : (int)dataRow["disk2"], - Cap2 = dataRow["cap2"] == DBNull.Value ? null : (string)dataRow["cap2"], - Type = (MachineType)dataRow["type"] + Id = (int)dataRow["id"], + Company = (int)dataRow["company"], + Year = (int)dataRow["year"], + Model = (string)dataRow["model"], + Hdd1 = (int)dataRow["hdd1"], + Hdd2 = dataRow["hdd2"] == DBNull.Value ? 0 : (int)dataRow["hdd2"], + Hdd3 = dataRow["hdd3"] == DBNull.Value ? 0 : (int)dataRow["hdd3"], + Disk1 = (int)dataRow["disk1"], + Cap1 = (string)dataRow["cap1"], + Disk2 = dataRow["disk2"] == DBNull.Value ? 0 : (int)dataRow["disk2"], + Cap2 = dataRow["cap2"] == DBNull.Value ? null : (string)dataRow["cap2"], + Type = (MachineType)dataRow["type"] }; entries.Add(entry); diff --git a/Cicm.Database/Operations/Operations.cs b/Cicm.Database/Operations/Operations.cs index cc2fc374..b4914631 100644 --- a/Cicm.Database/Operations/Operations.cs +++ b/Cicm.Database/Operations/Operations.cs @@ -35,7 +35,7 @@ namespace Cicm.Database public partial class Operations { /// Last known database version - const int DB_VERSION = 18; + const int DB_VERSION = 19; /// The column with this value indicates there is no item of this type. public const int DB_NONE = -1; /// diff --git a/Cicm.Database/Operations/Resolution.cs b/Cicm.Database/Operations/Resolution.cs new file mode 100644 index 00000000..65d970ac --- /dev/null +++ b/Cicm.Database/Operations/Resolution.cs @@ -0,0 +1,418 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : Resolution.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Contains operations to manage Resolutions. +// +// --[ License ] -------------------------------------------------------------- +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Data; +using Cicm.Database.Schemas; + +namespace Cicm.Database +{ + public partial class Operations + { + /// + /// Gets all Resolutions + /// + /// All Resolutions + /// true if is correct, false otherwise + public bool GetResolutions(out List entries) + { + #if DEBUG + Console.WriteLine("Getting all Resolutions..."); + #endif + + try + { + const string SQL = "SELECT * from resolutions"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = SQL; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolutions."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets the specified number of Resolutions since the specified start + /// + /// List of Resolutions + /// Start of query + /// How many entries to retrieve + /// true if is correct, false otherwise + public bool GetResolutions(out List entries, ulong start, ulong count) + { + #if DEBUG + Console.WriteLine("Getting {0} Resolutions from {1}...", count, start); + #endif + + try + { + string sql = $"SELECT * FROM resolutions LIMIT {start}, {count}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolutions."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets Resolution by specified id + /// + /// Id + /// Resolution with specified id, null if not found or error + public Resolution GetResolution(int id) + { + #if DEBUG + Console.WriteLine("Getting Resolution with id {0}...", id); + #endif + + try + { + string sql = $"SELECT * from resolutions WHERE id = '{id}'"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + List entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return entries == null || entries.Count == 0 ? null : entries[0]; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolution."); + Console.WriteLine(ex); + return null; + } + } + + /// + /// Gets Resolution by specified id + /// + /// Id + /// Resolution with specified id, null if not found or error + public Resolution GetResolution(int width, int height) + { + #if DEBUG + Console.WriteLine("Getting first resolution of {0}x{1}...", width, height); + #endif + + try + { + string sql = $"SELECT * from resolutions WHERE width = {width} AND height = {height}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + List entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return entries == null || entries.Count == 0 ? null : entries[0]; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolution."); + Console.WriteLine(ex); + return null; + } + } + + /// + /// Gets Resolution by specified id + /// + /// Id + /// Resolution with specified id, null if not found or error + public Resolution GetResolution(int width, int height, long colors) + { + #if DEBUG + Console.WriteLine("Getting first resolution of {0}x{1} with {2} colors...", width, height, colors); + #endif + + try + { + string sql = + $"SELECT * from resolutions WHERE width = {width} AND height = {height} AND colors = {colors}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + List entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return entries == null || entries.Count == 0 ? null : entries[0]; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolution."); + Console.WriteLine(ex); + return null; + } + } + + /// + /// Gets Resolution by specified id + /// + /// Id + /// Resolution with specified id, null if not found or error + public Resolution GetResolution(int width, int height, long colors, long palette) + { + #if DEBUG + Console.WriteLine("Getting first resolution of {0}x{1} with {2} colors from a palette of {3} colors...", + width, height, colors, palette); + #endif + + try + { + string sql = + $"SELECT * from resolutions WHERE width = {width} AND height = {height} AND colors = {colors} AND palette = {palette}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + List entries = ResolutionsFromDataTable(dataSet.Tables[0]); + + return entries == null || entries.Count == 0 ? null : entries[0]; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolution."); + Console.WriteLine(ex); + return null; + } + } + + /// + /// Counts the number of Resolutions in the database + /// + /// Entries in database + public long CountResolutions() + { + #if DEBUG + Console.WriteLine("Counting resolutions..."); + #endif + + IDbCommand dbcmd = dbCon.CreateCommand(); + dbcmd.CommandText = "SELECT COUNT(*) FROM resolutions"; + object count = dbcmd.ExecuteScalar(); + dbcmd.Dispose(); + try { return Convert.ToInt64(count); } + catch { return 0; } + } + + /// + /// Adds a new Resolution to the database + /// + /// Entry to add + /// ID of added entry + /// true if added correctly, false otherwise + public bool AddResolution(Resolution entry, out long id) + { + #if DEBUG + Console.Write("Adding Resolution..."); + #endif + + IDbCommand dbcmd = GetCommandResolution(entry); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + const string SQL = + "INSERT INTO resolutions (width, height, colors, palette, chars) VALUES (@width, @height, @colors, " + + "@palette, @chars)"; + + dbcmd.CommandText = SQL; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + id = dbCore.LastInsertRowId; + + #if DEBUG + Console.WriteLine(" id {0}", id); + #endif + + return true; + } + + /// + /// Updated a Resolution in the database + /// + /// Updated entry + /// true if updated correctly, false otherwise + public bool UpdateResolution(Resolution entry) + { + #if DEBUG + Console.WriteLine("Updating Resolution `{0}`...", entry.Id); + #endif + + IDbCommand dbcmd = GetCommandResolution(entry); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = + "UPDATE resolutions SET width = @width, height = @height, colors = @colors, palette = @palette, " + + "chars = @chars " + + $"WHERE id = {entry.Id}"; + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + + /// + /// Removes a Resolution from the database + /// + /// ID of entry to remove + /// true if removed correctly, false otherwise + public bool RemoveResolution(long id) + { + #if DEBUG + Console.WriteLine("Removing Resolution widh id `{0}`...", id); + #endif + + IDbCommand dbcmd = dbCon.CreateCommand(); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = $"DELETE FROM resolutions WHERE id = '{id}';"; + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + + IDbCommand GetCommandResolution(Resolution entry) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + + param1.ParameterName = "@width"; + param2.ParameterName = "@height"; + param3.ParameterName = "@colors"; + param4.ParameterName = "@palette"; + param5.ParameterName = "@chars"; + + param1.DbType = DbType.Int32; + param2.DbType = DbType.Int32; + param3.DbType = DbType.Int64; + param4.DbType = DbType.Int64; + param5.DbType = DbType.Boolean; + + param1.Value = entry.Width; + param2.Value = entry.Height; + param3.Value = entry.Colors == 0 ? (object)null : entry.Colors; + param4.Value = entry.Palette == 0 ? (object)null : entry.Palette; + param5.Value = entry.Chars; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + + return dbcmd; + } + + static List ResolutionsFromDataTable(DataTable dataTable) + { + List entries = new List(); + + foreach(DataRow dataRow in dataTable.Rows) + { + Resolution entry = new Resolution + { + Id = (int)dataRow["id"], + Width = (int)dataRow["width"], + Height = (int)dataRow["height"], + Colors = dataRow["colors"] == DBNull.Value ? 0 : (long)dataRow["colors"], + Palette = dataRow["palette"] == DBNull.Value ? 0 : (long)dataRow["palette"], + Chars = (bool)dataRow["chars"] + }; + + entries.Add(entry); + } + + return entries; + } + } +} \ No newline at end of file diff --git a/Cicm.Database/Operations/ResolutionByGpu.cs b/Cicm.Database/Operations/ResolutionByGpu.cs new file mode 100644 index 00000000..86c36512 --- /dev/null +++ b/Cicm.Database/Operations/ResolutionByGpu.cs @@ -0,0 +1,80 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : Resolution.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Contains operations to manage Resolutions. +// +// --[ License ] -------------------------------------------------------------- +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Data; +using Cicm.Database.Schemas; + +namespace Cicm.Database +{ + public partial class Operations + { + /// + /// Gets all Resolutions + /// + /// All Resolutions + /// true if is correct, false otherwise + public bool GetResolutions(out List entries, int gpuId) + { + #if DEBUG + Console.WriteLine("Getting all resolutions for GPU {0}...", gpuId); + #endif + + try + { + string sql = $"SELECT * FROM resolutions_by_gpu WHERE gpu = {gpuId}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = new List(); + foreach(DataRow dataRow in dataSet.Tables[0].Rows) + entries.Add(GetResolution((int)dataRow["resolution"])); + + if(entries.Count != 0) return true; + + entries = null; + return false; + } + catch(Exception ex) + { + Console.WriteLine("Error getting Resolutions."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + } +} \ No newline at end of file diff --git a/Cicm.Database/Operations/Update.cs b/Cicm.Database/Operations/Update.cs index f672ae6d..8fed8bb1 100644 --- a/Cicm.Database/Operations/Update.cs +++ b/Cicm.Database/Operations/Update.cs @@ -159,6 +159,11 @@ namespace Cicm.Database UpdateDatabaseToV18(); break; } + case 18: + { + UpdateDatabaseToV19(); + break; + } } OptimizeDatabase(); @@ -2026,6 +2031,147 @@ namespace Cicm.Database dbCmd.Dispose(); } + void UpdateDatabaseToV19() + { + Console.WriteLine("Updating database to version 19"); + + Console.WriteLine("Creating table `resolutions`"); + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbTransaction trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = V19.Resolutions; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Creating table `resolutions_by_gpu`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = V19.ResolutionsByGpu; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Getting all items from `machines`"); + + dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = "SELECT * from machines"; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + foreach(DataRow dataRow in dataSet.Tables[0].Rows) + { + if(dataRow["colors"] == DBNull.Value || dataRow["res"] == DBNull.Value || + (int)dataRow["colors"] == 0 || + (string)dataRow["res"] == "???") continue; + + dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter2 = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = $"SELECT * FROM gpus_by_machine WHERE machine = {(int)dataRow["id"]}"; + DataSet dataSet2 = new DataSet(); + dataAdapter2.SelectCommand = dbCmd; + dataAdapter2.Fill(dataSet2); + + if(dataSet2.Tables[0].Rows.Count == 0) continue; + + int gpuId = (int)dataSet2.Tables[0].Rows[0]["gpu"]; + + string[] resPieces = ((string)dataRow["res"]).Split('x'); + + if(!int.TryParse(resPieces[0], out int width)) continue; + if(!int.TryParse(resPieces[1], out int height)) continue; + + dbCmd = dbCon.CreateCommand(); + dataAdapter2 = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = + $"SELECT * FROM resolutions WHERE width = {width} AND height = {height} AND colors = {(int)dataRow["colors"]}"; + dataSet2 = new DataSet(); + dataAdapter2.SelectCommand = dbCmd; + dataAdapter2.Fill(dataSet2); + + int resId; + string sql; + + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + + if(dataSet2.Tables[0].Rows.Count == 0) + { + param1.ParameterName = "@width"; + param2.ParameterName = "@height"; + param3.ParameterName = "@colors"; + + param1.DbType = DbType.Int32; + param2.DbType = DbType.Int32; + param3.DbType = DbType.Int64; + + param1.Value = width; + param2.Value = height; + param3.Value = (int)dataRow["colors"]; + + sql = "INSERT INTO `resolutions` (`width`, `height`, `colors`) VALUES (@width, @height, @colors)"; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + dbcmd.Dispose(); + + resId = (int)dbCore.LastInsertRowId; + } + else resId = (int)dataSet2.Tables[0].Rows[0]["id"]; + + dbcmd = dbCon.CreateCommand(); + + param1 = dbcmd.CreateParameter(); + param2 = dbcmd.CreateParameter(); + + param1.ParameterName = "@gpu"; + param2.ParameterName = "@resolution"; + + param1.DbType = DbType.Int32; + param2.DbType = DbType.Int32; + + param1.Value = gpuId; + param2.Value = resId; + + sql = "INSERT INTO `resolutions_by_gpu` (`gpu`, `resolution`) VALUES (@gpu, @resolution)"; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + dbcmd.Dispose(); + } + + Console.WriteLine("Removing resolution columns from table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "ALTER TABLE `machines` DROP COLUMN `res`;\n" + + "ALTER TABLE `machines` DROP COLUMN `colors`;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Setting new database version to 19..."); + dbCmd = dbCon.CreateCommand(); + dbCmd.CommandText = "INSERT INTO cicm_db (version) VALUES ('19')"; + dbCmd.ExecuteNonQuery(); + dbCmd.Dispose(); + } + void OptimizeDatabase() { IDbCommand dbCmd = dbCon.CreateCommand(); diff --git a/Cicm.Database/Schemas/Machine.cs b/Cicm.Database/Schemas/Machine.cs index 81d6ce3c..1945078d 100644 --- a/Cicm.Database/Schemas/Machine.cs +++ b/Cicm.Database/Schemas/Machine.cs @@ -37,8 +37,6 @@ namespace Cicm.Database.Schemas public string Cap1; /// Capacity of second removable disk format public string Cap2; - /// Maximum colors on screen - public int Colors; /// Manufacturer's company ID public int Company; /// ID of first removable disk format @@ -55,8 +53,6 @@ namespace Cicm.Database.Schemas public int Id; /// Model name public string Model; - /// Resolution in WxH pixels - public string Resolution; /// Machine type public MachineType Type; /// Introduction date, 0 if unknown, 1000 if prototype diff --git a/Cicm.Database/Schemas/Resolution.cs b/Cicm.Database/Schemas/Resolution.cs new file mode 100644 index 00000000..46330bdc --- /dev/null +++ b/Cicm.Database/Schemas/Resolution.cs @@ -0,0 +1,49 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : Processor.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// High level representation of a processor . +// +// --[ License ] -------------------------------------------------------------- +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +namespace Cicm.Database.Schemas +{ + /// Processor + public class Resolution + { + /// If true width and height indicate characters, else they indicate pixels + public bool Chars; + /// Colors + public long Colors; + /// Height + public int Height; + /// Resolution ID + public int Id; + /// Palette, 0 if same as + public long Palette; + /// Width + public int Width; + } +} \ No newline at end of file diff --git a/Cicm.Database/Schemas/Sql/V19.cs b/Cicm.Database/Schemas/Sql/V19.cs new file mode 100644 index 00000000..9638b9c0 --- /dev/null +++ b/Cicm.Database/Schemas/Sql/V19.cs @@ -0,0 +1,145 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : V19.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Contains SQL queries to create the database version 7. +// +// --[ License ] -------------------------------------------------------------- +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +namespace Cicm.Database.Schemas.Sql +{ + public static class V19 + { + public static readonly string Admins = V18.Admins; + + public static readonly string BrowserTests = V18.BrowserTests; + + public static readonly string CicmDb = "CREATE TABLE `cicm_db` (\n" + + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + + "`version` int(11) NOT NULL,\n" + + "`updated` datetime DEFAULT CURRENT_TIMESTAMP,\n" + + "PRIMARY KEY (`id`));\n" + + "INSERT INTO cicm_db (version) VALUES ('19');"; + + public static readonly string Companies = V18.Companies; + + public static readonly string Machines = "CREATE TABLE `machines` (;\n" + + "`id` int(11) NOT NULL AUTO_INCREMENT,;\n" + + "`company` int(11) NOT NULL DEFAULT '0',;\n" + + "`year` int(11) NOT NULL DEFAULT '0',;\n" + + "`model` char(50) NOT NULL DEFAULT '',;\n" + + "`hdd1` int(11) NOT NULL DEFAULT '0',;\n" + + "`hdd2` int(11) DEFAULT NULL,;\n" + + "`hdd3` int(11) DEFAULT NULL,;\n" + + "`disk1` int(11) NOT NULL DEFAULT '0',;\n" + + "`cap1` char(25) NOT NULL DEFAULT '0',;\n" + + "`disk2` int(11) DEFAULT NULL,;\n" + + "`cap2` char(25) DEFAULT NULL,;\n" + + "`type` int(11) NOT NULL DEFAULT '0',;\n" + + "PRIMARY KEY (`id`),;\n" + + "KEY `idx_machines_company` (`company`),;\n" + + "KEY `idx_machines_year` (`year`),;\n" + + "KEY `idx_machines_model` (`model`),;\n" + + "KEY `idx_machines_hdd1` (`hdd1`),;\n" + + "KEY `idx_machines_hdd2` (`hdd2`),;\n" + + "KEY `idx_machines_hdd3` (`hdd3`),;\n" + + "KEY `idx_machines_disk1` (`disk1`),;\n" + + "KEY `idx_machines_disk2` (`disk2`),;\n" + + "KEY `idx_machines_cap1` (`cap1`),;\n" + + "KEY `idx_machines_cap2` (`cap2`),;\n" + + "KEY `idx_machines_type` (`type`));"; + + public static readonly string DiskFormats = V18.DiskFormats; + + public static readonly string Forbidden = V18.Forbidden; + + public static readonly string Gpus = V18.Gpus; + + public static readonly string Logs = V18.Logs; + + public static readonly string MoneyDonations = V18.MoneyDonations; + + public static readonly string News = V18.News; + + public static readonly string OwnedComputers = V18.OwnedComputers; + + public static readonly string OwnedConsoles = V18.OwnedConsoles; + + public static readonly string Processors = V18.Processors; + + public static readonly string SoundSynths = V18.SoundSynths; + + public static readonly string MachinesForeignKeys = V18.MachinesForeignKeys; + + public static readonly string Iso3166Numeric = V18.Iso3166Numeric; + + public static readonly string Iso3166NumericValues = V18.Iso3166NumericValues; + + public static readonly string CompaniesForeignKeys = V18.CompaniesForeignKeys; + + public static readonly string CompanyLogos = V18.CompanyLogos; + + public static readonly string CompanyDescriptions = V18.CompanyDescriptions; + + public static readonly string InstructionSets = V18.InstructionSets; + + public static readonly string InstructionSetExtensions = V18.InstructionSetExtensions; + + public static readonly string InstructionSetExtensionsByProcessor = V18.InstructionSetExtensionsByProcessor; + + public static readonly string ProcessorsByMachine = V18.ProcessorsByMachine; + + public static readonly string GpusByMachine = V18.GpusByMachine; + + public static readonly string SoundByMachine = V18.SoundByMachine; + + public static readonly string MemoryByMachine = V18.MemoryByMachine; + + public static readonly string Resolutions = + "CREATE TABLE `resolutions` (\n" + + "`id` INT NOT NULL AUTO_INCREMENT,\n" + + "`width` INT NOT NULL DEFAULT 0,\n" + + "`height` INT NOT NULL DEFAULT 0,\n" + + "`colors` BIGINT DEFAULT NULL,\n" + + "`palette` BIGINT DEFAULT NULL,\n" + + "`chars` BOOL NOT NULL DEFAULT 0,\n" + "PRIMARY KEY (`id`),\n" + + "KEY `idx_resolutions_width` (`width`),\n" + + "KEY `idx_resolutions_height` (`height`),\n" + + "KEY `idx_resolutions_colors` (`colors`),\n" + + "KEY `idx_resolutions_palette` (`palette`),\n" + + "INDEX `idx_resolutions_resolution` (`width`,`height`),\n" + + "INDEX `idx_resolutions_resolution_with_color` (`width`,`height`,`colors`),\n" + + "INDEX `idx_resolutions_resolution_with_color_and_palette` (`width`,`height`,`colors`,`palette`));"; + + public static readonly string ResolutionsByGpu = + "CREATE TABLE `resolutions_by_gpu` (\n" + + "`gpu` INT NOT NULL,\n" + + "`resolution` INT NOT NULL,\n" + + "KEY `idx_resolutions_by_gpu_gpu` (`gpu`),\n" + + "KEY `idx_resolutions_by_gpu_resolution` (`resolution`),\n" + + "CONSTRAINT `fk_resolutions_by_gpu_gpu` FOREIGN KEY (`gpu`) REFERENCES `gpus` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,\n" + + "CONSTRAINT `fk_resolutions_by_gpu_resolution` FOREIGN KEY (`resolution`) REFERENCES `resolutions` (`id`) ON UPDATE CASCADE ON DELETE CASCADE);"; + } +} \ No newline at end of file diff --git a/cicm_web/Models/Machine.cs b/cicm_web/Models/Machine.cs index f37e1b1b..44b82119 100644 --- a/cicm_web/Models/Machine.cs +++ b/cicm_web/Models/Machine.cs @@ -39,7 +39,6 @@ namespace cicm_web.Models { public string Cap1; public string Cap2; - public int Colors; public Company Company; public DiskFormat Disk1; public DiskFormat Disk2; @@ -51,7 +50,6 @@ namespace cicm_web.Models public MemoryByMachine[] Memories; public string Model; public ProcessorByMachine[] Processors; - public string Resolution; public SoundByMachine[] SoundSynths; public MachineType Type; public int Year; @@ -87,7 +85,6 @@ namespace cicm_web.Models { Machine item = new Machine { - Colors = dbItem.Colors, Company = Company.GetItem(dbItem.Company), Gpus = GpuByMachine.GetAllItems(dbItem.Id), Hdd1 = DiskFormat.GetItem(dbItem.Hdd1), @@ -95,7 +92,6 @@ namespace cicm_web.Models Hdd3 = DiskFormat.GetItem(dbItem.Hdd3), Id = dbItem.Id, Model = dbItem.Model, - Resolution = dbItem.Resolution, Year = dbItem.Year, Type = dbItem.Type, Processors = ProcessorByMachine.GetAllItems(dbItem.Id), diff --git a/cicm_web/Views/Machine/View.cshtml b/cicm_web/Views/Machine/View.cshtml index 01d5960a..2486ccb9 100644 --- a/cicm_web/Views/Machine/View.cshtml +++ b/cicm_web/Views/Machine/View.cshtml @@ -28,7 +28,7 @@ // ---------------------------------------------------------------------------- // Copyright © 2003-2018 Natalia Portillo *******************************************************************************/ - + // TODO: Resolutions ViewData["Title"] = "Computer"; } @using System.IO @@ -489,37 +489,6 @@ } - - -
- Video resolution -
- - @if(Model.Resolution != "???") - { - @Model.Resolution - } - else - { - Unknown data - } - - - -
- Colors -
- - - @if(Model.Colors > 0) - { - @Model.Colors - } - else - { - Unknown data - } - @if(Model.SoundSynths != null && Model.SoundSynths.Length > 0) { diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index 17a503de..65be754e 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - 3.0.99.215 + 3.0.99.220 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website