101 lines
3.6 KiB
C#
101 lines
3.6 KiB
C#
using MedicalDatabaseContracts;
|
|
using MedicalDatabaseContracts.Models;
|
|
using Npgsql;
|
|
using System.Diagnostics;
|
|
|
|
namespace MedicalPostgresqlDatabase
|
|
{
|
|
public abstract class AbstractPostgresqlStorage<T> : IStorage<T> where T : AbstractModel
|
|
{
|
|
protected readonly string TABLE_NAME;
|
|
protected readonly string PRIMARY_KEY_COLUMN_NAME;
|
|
|
|
protected AbstractPostgresqlStorage(string tableName, string primaryKeyColumnName)
|
|
{
|
|
TABLE_NAME = tableName;
|
|
PRIMARY_KEY_COLUMN_NAME = primaryKeyColumnName;
|
|
}
|
|
|
|
protected abstract T CreateEntityFromReader(NpgsqlDataReader reader);
|
|
|
|
public virtual void Delete(int id)
|
|
{
|
|
long elapsedMilliseconds;
|
|
Delete(id, out elapsedMilliseconds);
|
|
}
|
|
public virtual void Delete(int id, out long elapsedMilliseconds)
|
|
{
|
|
using var connection = GetConnection();
|
|
connection.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM {TABLE_NAME} WHERE {PRIMARY_KEY_COLUMN_NAME} = @id", connection);
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
Stopwatch stopwatch = new();
|
|
stopwatch.Start();
|
|
cmd.ExecuteNonQuery();
|
|
stopwatch.Stop();
|
|
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
|
}
|
|
public virtual T? Get(int id)
|
|
{
|
|
long elapsedMilliseconds;
|
|
return Get(id, out elapsedMilliseconds);
|
|
}
|
|
public virtual T? Get(int id, out long elapsedMilliseconds)
|
|
{
|
|
using var connection = GetConnection();
|
|
connection.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM {TABLE_NAME} WHERE {PRIMARY_KEY_COLUMN_NAME} = @id", connection);
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
Stopwatch stopwatch = new();
|
|
stopwatch.Start();
|
|
using var reader = cmd.ExecuteReader();
|
|
stopwatch.Stop();
|
|
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
|
if (reader.Read())
|
|
{
|
|
return CreateEntityFromReader(reader);
|
|
}
|
|
return null;
|
|
}
|
|
public virtual List<T> GetAll()
|
|
{
|
|
long elapsedMilliseconds;
|
|
return GetAll(out elapsedMilliseconds);
|
|
}
|
|
public virtual List<T> GetAll(out long elapsedMilliseconds)
|
|
{
|
|
var items = new List<T>();
|
|
using var connection = GetConnection();
|
|
connection.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM {TABLE_NAME} ORDER BY {PRIMARY_KEY_COLUMN_NAME}", connection);
|
|
Stopwatch stopwatch = new();
|
|
stopwatch.Start();
|
|
using var reader = cmd.ExecuteReader();
|
|
stopwatch.Stop();
|
|
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
|
while (reader.Read())
|
|
{
|
|
items.Add(CreateEntityFromReader(reader));
|
|
}
|
|
return items;
|
|
}
|
|
public virtual void Insert(T item)
|
|
{
|
|
long elapsedMilliseconds;
|
|
Insert(item, out elapsedMilliseconds);
|
|
}
|
|
public abstract void Insert(T item, out long elapsedMilliseconds);
|
|
public virtual void Update(T item)
|
|
{
|
|
long elapsedMilliseconds;
|
|
Update(item, out elapsedMilliseconds);
|
|
}
|
|
public abstract void Update(T item, out long elapsedMilliseconds);
|
|
|
|
protected NpgsqlConnection GetConnection()
|
|
{
|
|
return new NpgsqlConnection("Host=127.0.0.1;Port=5555;Username=postgres;Database=medicalbase;Password=postgres");
|
|
}
|
|
}
|
|
}
|