Files
marechai/Cicm.Database/Operations/Processor.cs

285 lines
9.4 KiB
C#
Raw Normal View History

2018-04-12 10:20:31 +01:00
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
2018-04-15 17:51:07 +01:00
// Filename : Processor.cs
2018-04-12 10:20:31 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
2018-04-15 17:51:07 +01:00
// Contains operations to manage processors.
2018-04-12 10:20:31 +01:00
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/
2018-04-12 11:50:02 +01:00
2018-04-12 10:20:31 +01:00
using System;
2018-04-12 10:05:42 +01:00
using System.Collections.Generic;
using System.Data;
using Cicm.Database.Schemas;
using Console = System.Console;
namespace Cicm.Database
{
public partial class Operations
{
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Gets all processors
2018-04-12 11:50:02 +01:00
/// </summary>
/// <param name="entries">All CPUs</param>
/// <returns><c>true</c> if <see cref="entries" /> is correct, <c>false</c> otherwise</returns>
2018-04-15 17:51:07 +01:00
public bool GetProcessors(out List<Processor> entries)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Getting all processors...");
2018-04-12 10:05:42 +01:00
#endif
try
{
2018-04-15 17:51:07 +01:00
const string SQL = "SELECT * from processors";
2018-04-12 10:05:42 +01:00
IDbCommand dbCmd = dbCon.CreateCommand();
IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter();
dbCmd.CommandText = SQL;
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = dbCmd;
dataAdapter.Fill(dataSet);
2018-04-15 17:51:07 +01:00
entries = ProcessorsFromDataTable(dataSet.Tables[0]);
2018-04-12 10:05:42 +01:00
return true;
}
catch(Exception ex)
{
2018-04-15 17:51:07 +01:00
Console.WriteLine("Error getting processors.");
2018-04-12 10:05:42 +01:00
Console.WriteLine(ex);
entries = null;
return false;
}
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Gets the specified number of processors since the specified start
2018-04-12 11:50:02 +01:00
/// </summary>
2018-04-15 17:51:07 +01:00
/// <param name="entries">List of processors</param>
2018-04-12 11:50:02 +01:00
/// <param name="start">Start of query</param>
/// <param name="count">How many entries to retrieve</param>
/// <returns><c>true</c> if <see cref="entries" /> is correct, <c>false</c> otherwise</returns>
2018-04-15 17:51:07 +01:00
public bool GetProcessors(out List<Processor> entries, ulong start, ulong count)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Getting {0} processors from {1}...", count, start);
2018-04-12 10:05:42 +01:00
#endif
try
{
2018-04-15 17:51:07 +01:00
string sql = $"SELECT * FROM processors LIMIT {start}, {count}";
2018-04-12 10:05:42 +01:00
IDbCommand dbCmd = dbCon.CreateCommand();
IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter();
dbCmd.CommandText = sql;
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = dbCmd;
dataAdapter.Fill(dataSet);
2018-04-15 17:51:07 +01:00
entries = ProcessorsFromDataTable(dataSet.Tables[0]);
2018-04-12 10:05:42 +01:00
return true;
}
catch(Exception ex)
{
2018-04-15 17:51:07 +01:00
Console.WriteLine("Error getting processors.");
2018-04-12 10:05:42 +01:00
Console.WriteLine(ex);
entries = null;
return false;
}
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Gets processor by specified id
2018-04-12 11:50:02 +01:00
/// </summary>
/// <param name="id">Id</param>
2018-04-15 17:51:07 +01:00
/// <returns>Processor with specified id, <c>null</c> if not found or error</returns>
public Processor GetProcessor(int id)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Getting processor with id {0}...", id);
2018-04-12 10:05:42 +01:00
#endif
try
{
2018-04-15 17:51:07 +01:00
string sql = $"SELECT * from processors WHERE id = '{id}'";
2018-04-12 10:05:42 +01:00
IDbCommand dbCmd = dbCon.CreateCommand();
IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter();
dbCmd.CommandText = sql;
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = dbCmd;
dataAdapter.Fill(dataSet);
2018-04-15 17:51:07 +01:00
List<Processor> entries = ProcessorsFromDataTable(dataSet.Tables[0]);
2018-04-12 10:05:42 +01:00
return entries == null || entries.Count == 0 ? null : entries[0];
}
catch(Exception ex)
{
2018-04-15 17:51:07 +01:00
Console.WriteLine("Error getting processor.");
2018-04-12 10:05:42 +01:00
Console.WriteLine(ex);
return null;
}
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Counts the number of processors in the database
2018-04-12 11:50:02 +01:00
/// </summary>
/// <returns>Entries in database</returns>
2018-04-15 17:51:07 +01:00
public long CountProcessors()
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Counting processors...");
2018-04-12 10:05:42 +01:00
#endif
IDbCommand dbcmd = dbCon.CreateCommand();
2018-04-15 17:51:07 +01:00
dbcmd.CommandText = "SELECT COUNT(*) FROM processors";
2018-04-12 10:05:42 +01:00
object count = dbcmd.ExecuteScalar();
dbcmd.Dispose();
try { return Convert.ToInt64(count); }
catch { return 0; }
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Adds a new processor to the database
2018-04-12 11:50:02 +01:00
/// </summary>
/// <param name="entry">Entry to add</param>
/// <param name="id">ID of added entry</param>
/// <returns><c>true</c> if added correctly, <c>false</c> otherwise</returns>
2018-04-15 17:51:07 +01:00
public bool AddProcessor(Processor entry, out long id)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.Write("Adding processor `{0}`...", entry.Name);
2018-04-12 10:05:42 +01:00
#endif
2018-04-15 17:51:07 +01:00
IDbCommand dbcmd = GetCommandProcessor(entry);
2018-04-12 10:05:42 +01:00
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
2018-04-15 17:51:07 +01:00
const string SQL = "INSERT INTO processors (name)" + " VALUES (@name)";
2018-04-12 10:05:42 +01:00
dbcmd.CommandText = SQL;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
id = dbCore.LastInsertRowId;
#if DEBUG
Console.WriteLine(" id {0}", id);
#endif
return true;
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Updated a processor in the database
2018-04-12 11:50:02 +01:00
/// </summary>
/// <param name="entry">Updated entry</param>
/// <returns><c>true</c> if updated correctly, <c>false</c> otherwise</returns>
2018-04-15 17:51:07 +01:00
public bool UpdateProcessor(Processor entry)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Updating processor `{0}`...", entry.Name);
2018-04-12 10:05:42 +01:00
#endif
2018-04-15 17:51:07 +01:00
IDbCommand dbcmd = GetCommandProcessor(entry);
2018-04-12 10:05:42 +01:00
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
2018-04-15 17:51:07 +01:00
string sql = "UPDATE processors SET name = @name " + $"WHERE id = {entry.Id}";
2018-04-12 10:05:42 +01:00
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
return true;
}
2018-04-12 11:50:02 +01:00
/// <summary>
2018-04-15 17:51:07 +01:00
/// Removes a processor from the database
2018-04-12 11:50:02 +01:00
/// </summary>
/// <param name="id">ID of entry to remove</param>
/// <returns><c>true</c> if removed correctly, <c>false</c> otherwise</returns>
2018-04-15 17:51:07 +01:00
public bool RemoveProcessor(long id)
2018-04-12 10:05:42 +01:00
{
#if DEBUG
2018-04-15 17:51:07 +01:00
Console.WriteLine("Removing processor widh id `{0}`...", id);
2018-04-12 10:05:42 +01:00
#endif
IDbCommand dbcmd = dbCon.CreateCommand();
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
2018-04-15 17:51:07 +01:00
string sql = $"DELETE FROM processors WHERE id = '{id}';";
2018-04-12 10:05:42 +01:00
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
return true;
}
2018-04-15 17:51:07 +01:00
IDbCommand GetCommandProcessor(Processor entry)
2018-04-12 10:05:42 +01:00
{
IDbCommand dbcmd = dbCon.CreateCommand();
IDbDataParameter param1 = dbcmd.CreateParameter();
2018-04-15 17:51:07 +01:00
param1.ParameterName = "@name";
2018-04-12 10:05:42 +01:00
param1.DbType = DbType.String;
2018-04-12 12:02:56 +01:00
param1.Value = entry.Name;
2018-04-12 10:05:42 +01:00
dbcmd.Parameters.Add(param1);
return dbcmd;
}
2018-04-15 17:51:07 +01:00
static List<Processor> ProcessorsFromDataTable(DataTable dataTable)
2018-04-12 10:05:42 +01:00
{
2018-04-15 17:51:07 +01:00
List<Processor> entries = new List<Processor>();
2018-04-12 10:05:42 +01:00
foreach(DataRow dataRow in dataTable.Rows)
{
2018-04-15 17:51:07 +01:00
Processor entry = new Processor
{
Id = int.Parse(dataRow["id"].ToString()),
Name = dataRow["name"].ToString()
};
2018-04-12 10:05:42 +01:00
entries.Add(entry);
}
return entries;
}
}
}