219 lines
8.6 KiB
C#
219 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Data.SQLite;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Test_Merge
|
|
{
|
|
public class SqliteStorage
|
|
{
|
|
private const string DATABASE_FOLDER = "./Data";
|
|
private const string DATABASE_FILE = "/database.sqlite";
|
|
private const string CONNECTION_STRING = "Data Source=" + DATABASE_FOLDER + DATABASE_FILE + ";Version=3;";
|
|
|
|
private SQLiteConnection Connection;
|
|
public SqliteStorage()
|
|
{
|
|
Load();
|
|
}
|
|
private void Load()
|
|
{
|
|
if (!Directory.Exists(DATABASE_FOLDER))
|
|
Directory.CreateDirectory(DATABASE_FOLDER);
|
|
|
|
if (!File.Exists(DATABASE_FOLDER + DATABASE_FILE))
|
|
{
|
|
SQLiteConnection.CreateFile(DATABASE_FOLDER + DATABASE_FILE);
|
|
}
|
|
else
|
|
{
|
|
//We are not using the existing DataBase
|
|
File.Delete(DATABASE_FOLDER + DATABASE_FILE);
|
|
}
|
|
|
|
Connection = new SQLiteConnection(CONNECTION_STRING);
|
|
Connection.Open();
|
|
|
|
//Create the drivers table
|
|
string createDriversTableQuery = @"CREATE TABLE IF NOT EXISTS Drivers
|
|
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
Name VARCHAR NOT NULL);";
|
|
using (var command = new SQLiteCommand(createDriversTableQuery, Connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
//Create the drivers table
|
|
string createPitstopTableQuery = @"CREATE TABLE Pitstops
|
|
(Lap INTEGER NOT NULL,
|
|
DriverID INTEGER NOT NULL,
|
|
Tyre VARCHAR,
|
|
PRIMARY KEY (Lap,DriverID));";
|
|
using (var command = new SQLiteCommand(createPitstopTableQuery, Connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
//Create the stats
|
|
string createStatsTableQuery = @"CREATE TABLE IF NOT EXISTS Stats
|
|
(Lap INTEGER NOT NULL,
|
|
DriverID INTEGER NOT NULL,
|
|
Tyre VARCHAR NOT NULL,
|
|
LapTime INTEGER NOT NULL,
|
|
Sector1 INTEGER NOT NULL,
|
|
Sector2 INTEGER NOT NULL,
|
|
Sector3 INTEGER NOT NULL,
|
|
GapToLeader INTEGER NOT NULL,
|
|
Position INTEGER NOT NULL,
|
|
PRIMARY KEY (Lap, DriverID));";
|
|
using (var command = new SQLiteCommand(createStatsTableQuery, Connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
public void AddDriver(string name)
|
|
{
|
|
string insertQuery = "INSERT INTO Drivers (Name) VALUES (@name);";
|
|
|
|
using (var command = new SQLiteCommand(insertQuery,Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@Name",name);
|
|
|
|
try
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("An error has occured while trying to insert a new driver into de Database");
|
|
}
|
|
}
|
|
}
|
|
private int GetDriverID(string name)
|
|
{
|
|
string selectQuery = "SELECT ID FROM Drivers where Name LIKE @driverName";
|
|
int result = 0;
|
|
using (var command = new SQLiteCommand(selectQuery,Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@driverName",name);
|
|
try
|
|
{
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result = reader.GetInt32(0);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("There has been an error while trying to retrieve the ID of a Driver from the database");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public List<int> GetSectorsFromLapTime(string driverName,int lap)
|
|
{
|
|
int driverId = GetDriverID(driverName);
|
|
string selectQuery = "SELECT Sector1,Sector2,Sector3 FROM Stats WHERE DriverID = @driverID AND Lap = @lap";
|
|
List<int> result = new List<int>();
|
|
using (var command = new SQLiteCommand(selectQuery, Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@driverID", driverId);
|
|
command.Parameters.AddWithValue("@lap", lap);
|
|
try
|
|
{
|
|
SQLiteDataReader reader = command.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
result.Add(reader.GetInt32(0));
|
|
result.Add(reader.GetInt32(1));
|
|
result.Add(reader.GetInt32(2));
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("There has been an error while trying to retrieve the ID of a Driver from the database");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public List<(int LapTime, int Lap)> GetDriverLaptimes(string driverName,int numberOfLaptimes)
|
|
{
|
|
int driverId = GetDriverID(driverName);
|
|
List<(int LapTime, int Lap)> lapData = new List<(int LapTime, int Lap)>();
|
|
string selectQuery = "Select LapTime,Lap from Stats WHERE DriverID = @driverID ORDER BY Lap DESC LIMIT @limit";
|
|
using (var command = new SQLiteCommand(selectQuery, Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@driverID", driverId);
|
|
command.Parameters.AddWithValue("@limit", numberOfLaptimes);
|
|
try
|
|
{
|
|
SQLiteDataReader reader = command.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
int lapTime = reader.GetInt32(0);
|
|
int lap = reader.GetInt32(1);
|
|
lapData.Add((lapTime, lap));
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("There has been an error while trying to retrieve the ID of a Driver from the database");
|
|
}
|
|
}
|
|
return lapData;
|
|
}
|
|
public void AddPitstop(string driverName,int lap,string tyre)
|
|
{
|
|
string insertQuery = "INSERT INTO Pitstops (Lap,DriverID,Tyre) VALUES (@Lap,@DriverID,@Tyre)";
|
|
|
|
using (var command = new SQLiteCommand(insertQuery,Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@Lap",lap);
|
|
command.Parameters.AddWithValue("@DriverID",GetDriverID(driverName));
|
|
command.Parameters.AddWithValue("@Tyre",tyre);
|
|
try
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("An error has occured while trying to insert a new pitstop into the DB" + Environment.NewLine + "Request :"+ command.ToString());
|
|
}
|
|
}
|
|
}
|
|
public void AddDriverStat(DriverData data,int lap)
|
|
{
|
|
string insertQuery = "INSERT INTO Stats (Lap,DriverID,Tyre,LapTime,Sector1,Sector2,Sector3,GapToLeader,Position) VALUES (@Lap,@DriverID,@Tyre,@LapTime,@Sector1,@Sector2,@Sector3,@GapToLeader,@Position);";
|
|
|
|
using (var command = new SQLiteCommand(insertQuery,Connection))
|
|
{
|
|
command.Parameters.AddWithValue("@Lap",lap);
|
|
command.Parameters.AddWithValue("@DriverID",GetDriverID(data.Name));
|
|
command.Parameters.AddWithValue("@Tyre",data.CurrentTyre.Coumpound.ToString());
|
|
command.Parameters.AddWithValue("@LapTime",data.LapTime);
|
|
command.Parameters.AddWithValue("@Sector1",data.Sector1);
|
|
command.Parameters.AddWithValue("@Sector2", data.Sector2);
|
|
command.Parameters.AddWithValue("@Sector3", data.Sector3);
|
|
command.Parameters.AddWithValue("@GapToLeader", data.GapToLeader);
|
|
command.Parameters.AddWithValue("@Position", data.Position);
|
|
|
|
try
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show("An error has occured while trying to insert infos about a driver");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|