This repository has been archived on 2023-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
cuipod/Chatter/Connector/SqLiteConnector.cs
Dmitrii Kollerov 388ebd964b add basic app
2022-03-10 15:39:31 +07:00

134 lines
4.0 KiB
C#

using Chatter.Dto;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chatter.Connector
{
public interface IDbConnector: IDisposable
{
IEnumerable<MessageDto> GetMessages();
void AddMessage(MessageDto msg);
}
//Not building a context will do it quick and dirty without entity framework
internal class SqLiteConnector : IDbConnector
{
private readonly SQLiteConnectionStringBuilder _connectionStringBuilder;
private readonly SQLiteConnection _connection;
private readonly string _addCommand = "INSERT INTO messages(text, createdate, user) VALUES (@text, @createdate, @user);";
private readonly string _getCommand = "SELECT text, createdate, user FROM messages ORDER BY createdate DESC;";
private readonly string _createCommand = "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT NOT NULL, createdate DATETIME NOT NULL, user varchar);";
private bool disposedValue;
public SqLiteConnector()
{
_connectionStringBuilder = new SQLiteConnectionStringBuilder() { DataSource = ":memory:", Version = 3 };
//_connectionStringBuilder = new SQLiteConnectionStringBuilder() { DataSource = "D:\\sql.s3db", Version = 3 };
_connection = GetDbConnection();
_connection.Open();
PrepareTable();
}
SQLiteConnection GetDbConnection()
{
return new SQLiteConnection(_connectionStringBuilder.ToString());
}
private void PrepareTable()
{
//_connection.Open();
using (var cmd = new SQLiteCommand(_createCommand, _connection))
{
var data = cmd.ExecuteNonQuery();
}
//_connection.Close(); // no async for you today
}
public void AddMessage(MessageDto msg)
{
//_connection.Open();
using (var cmd = new SQLiteCommand(_addCommand, _connection))
{
var arguments = new Dictionary<string, object>()
{
{ "@text", msg.Text ?? string.Empty },
{ "@createdate", msg.CreateDate },
{ "@user", msg.User }
};
foreach (var pair in arguments)
{
cmd.Parameters.AddWithValue(pair.Key, pair.Value);
}
cmd.ExecuteNonQuery();
}
//_connection.Close(); // no async for you today
}
public IEnumerable<MessageDto> GetMessages()
{
var result = new List<MessageDto>();
using (var cmd = new SQLiteCommand(_getCommand, _connection))
{
using (var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
var text = reader.GetString(0);
var createDate = reader.GetString(1);
var user = reader.GetValue(2);
var message = new MessageDto()
{
Text = text,
CreateDate = DateTime.Parse(createDate),
User = user?.ToString()
};
result.Add(message);
}
}
}
//_connection.Close(); // no async for you today
return result;
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_connection.Close();
_connection.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}