Skip to content

Commit

Permalink
Make DB backends a little more flexible with overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 2, 2024
1 parent b875e2f commit 1bf0f7a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 57 deletions.
33 changes: 15 additions & 18 deletions MCGalaxy/Database/Backends/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,33 @@ permissions and limitations under the Licenses.
namespace MCGalaxy.SQL
{
/// <summary> Abstracts a connection to a SQL database </summary>
public abstract class ISqlConnection : IDisposable
public interface ISqlConnection : IDisposable
{
public abstract ISqlTransaction BeginTransaction();
public abstract ISqlCommand CreateCommand(string sql);
ISqlTransaction BeginTransaction();
ISqlCommand CreateCommand(string sql);

public abstract void Open();
public abstract void ChangeDatabase(string name);
public abstract void Close();
public abstract void Dispose();
void Open();
void ChangeDatabase(string name);
void Close();
}

/// <summary> Abstracts a SQL command/statement </summary>
public abstract class ISqlCommand : IDisposable
public interface ISqlCommand : IDisposable
{
public abstract void ClearParameters();
public abstract void AddParameter(string name, object value);
void ClearParameters();
void AddParameter(string name, object value);

public abstract void Prepare();
void Prepare();
/// <summary> Executes this command and returns the number of rows affected </summary>
public abstract int ExecuteNonQuery();
int ExecuteNonQuery();
/// <summary> Executes this command and returns an ISqlReader for reading the results </summary>
public abstract ISqlReader ExecuteReader();
public abstract void Dispose();
ISqlReader ExecuteReader();
}

public abstract class ISqlTransaction : IDisposable
public interface ISqlTransaction : IDisposable
{
public abstract void Commit();
public abstract void Rollback();
public abstract void Dispose();
void Commit();
void Rollback();
}

/// <summary> Abstracts iterating over the results from executing a SQL command </summary>
Expand Down
36 changes: 18 additions & 18 deletions MCGalaxy/Database/Backends/MySQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ permissions and limitations under the Licenses.

namespace MCGalaxy.SQL
{
public sealed class MySQLBackend : IDatabaseBackend
public class MySQLBackend : IDatabaseBackend
{
public static IDatabaseBackend Instance = new MySQLBackend();
public MySQLBackend() {
Expand All @@ -33,10 +33,10 @@ public MySQLBackend() {
CaselessLikeSuffix = "";
}

public override bool EnforcesTextLength { get { return true; } }
public override bool EnforcesTextLength { get { return true; } }
public override bool EnforcesIntegerLimits { get { return true; } }
public override bool MultipleSchema { get { return true; } }
public override string EngineName { get { return "MySQL"; } }
public override string EngineName { get { return "MySQL"; } }

public override ISqlConnection CreateConnection() {
const string format = "Data Source={0};Port={1};User ID={2};Password={3};Pooling={4};Treat Tiny As Boolean=false;";
Expand Down Expand Up @@ -164,39 +164,39 @@ sealed class MySQLConnection : ISqlConnection
public readonly MySqlConnection conn;
public MySQLConnection(MySqlConnection conn) { this.conn = conn; }

public override ISqlTransaction BeginTransaction() {
public ISqlTransaction BeginTransaction() {
MySqlTransaction trn = conn.BeginTransaction();
return new MySQLTransaction(trn);
}

public override ISqlCommand CreateCommand(string sql) {
public ISqlCommand CreateCommand(string sql) {
MySqlCommand cmd = new MySqlCommand(sql, conn);
return new MySQLCommand(cmd);
}

public override void Open() { conn.Open(); }
public override void ChangeDatabase(string name) { conn.ChangeDatabase(name); }
public override void Close() { conn.Close(); }
public override void Dispose() { conn.Dispose(); }
public void Open() { conn.Open(); }
public void ChangeDatabase(string name) { conn.ChangeDatabase(name); }
public void Close() { conn.Close(); }
public void Dispose() { conn.Dispose(); }
}

sealed class MySQLCommand : ISqlCommand
{
readonly MySqlCommand cmd;
public MySQLCommand(MySqlCommand cmd) { this.cmd = cmd; }

public override void ClearParameters() {
public void ClearParameters() {
cmd.Parameters.Clear();
}
public override void AddParameter(string name, object value) {
public void AddParameter(string name, object value) {
cmd.Parameters.AddWithValue(name, value);
}

public override void Dispose() { cmd.Dispose(); }
public override void Prepare() { cmd.Prepare(); }
public override int ExecuteNonQuery() { return cmd.ExecuteNonQuery(); }
public void Dispose() { cmd.Dispose(); }
public void Prepare() { cmd.Prepare(); }
public int ExecuteNonQuery() { return cmd.ExecuteNonQuery(); }

public override ISqlReader ExecuteReader() {
public ISqlReader ExecuteReader() {
MySqlDataReader rdr = cmd.ExecuteReader();
return new MySQLReader(rdr);
}
Expand All @@ -207,9 +207,9 @@ sealed class MySQLTransaction : ISqlTransaction
readonly MySqlTransaction trn;
public MySQLTransaction(MySqlTransaction trn) { this.trn = trn; }

public override void Commit() { trn.Commit(); }
public override void Rollback() { trn.Rollback(); }
public override void Dispose() { trn.Dispose(); }
public void Commit() { trn.Commit(); }
public void Rollback() { trn.Rollback(); }
public void Dispose() { trn.Dispose(); }
}

sealed class MySQLReader : ISqlReader
Expand Down
6 changes: 3 additions & 3 deletions MCGalaxy/Database/Backends/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ permissions and limitations under the Licenses.

namespace MCGalaxy.SQL
{
public sealed class SQLiteBackend : IDatabaseBackend
public class SQLiteBackend : IDatabaseBackend
{
public static IDatabaseBackend Instance = new SQLiteBackend();
public SQLiteBackend() {
CaselessWhereSuffix = " COLLATE NOCASE";
CaselessLikeSuffix = " COLLATE NOCASE";
}

public override bool EnforcesTextLength { get { return false; } }
public override bool EnforcesTextLength { get { return false; } }
public override bool EnforcesIntegerLimits { get { return false; } }
public override bool MultipleSchema { get { return false; } }
public override string EngineName { get { return "SQLite"; } }
public override string EngineName { get { return "SQLite"; } }

public override ISqlConnection CreateConnection() {
return new MCGSQLiteConnection();
Expand Down
32 changes: 16 additions & 16 deletions MCGalaxy/Database/Backends/SQLiteBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public abstract class SQLiteConnection : ISqlConnection
protected abstract bool ConnectionPooling { get; }
protected abstract string DBPath { get; }

public override ISqlTransaction BeginTransaction() {
public ISqlTransaction BeginTransaction() {
return new SQLiteTransaction(this);
}

public override void ChangeDatabase(string databaseName) { }
public void ChangeDatabase(string databaseName) { }

public override ISqlCommand CreateCommand(string sql) { return new SQLiteCommand(sql, this); }
public ISqlCommand CreateCommand(string sql) { return new SQLiteCommand(sql, this); }

public long LastInsertRowId {
get {
Expand Down Expand Up @@ -170,7 +170,7 @@ internal SQLiteStatement Prepare(string strSql, ref string strRemain) {
}
}

public override void Open() {
public void Open() {
if (handle != IntPtr.Zero) throw new InvalidOperationException();

try {
Expand Down Expand Up @@ -226,8 +226,8 @@ internal bool Reset(bool canThrow) {
return false;
}

public override void Dispose() { Close(false); }
public override void Close() { Close(true); }
public void Dispose() { Close(false); }
public void Close() { Close(true); }

void Close(bool canThrow) {
if (handle == IntPtr.Zero) return;
Expand Down Expand Up @@ -291,7 +291,7 @@ void DisposeStatement() {
stmt = null;
}

public override void Dispose() {
public void Dispose() {
conn = null;
param_names.Clear();
param_values.Clear();
Expand All @@ -316,27 +316,27 @@ internal SQLiteStatement NextStatement() {
return stmt;
}

public override void Prepare() { }
public void Prepare() { }

public override void ClearParameters() {
public void ClearParameters() {
param_names.Clear();
param_values.Clear();
}

public override void AddParameter(string name, object value) {
public void AddParameter(string name, object value) {
param_names.Add(name);
param_values.Add(value);
}

public override ISqlReader ExecuteReader() {
public ISqlReader ExecuteReader() {
SQLiteConnection.Check(conn);

SQLiteDataReader reader = new SQLiteDataReader(this);
reader.NextResult();
return reader;
}

public override int ExecuteNonQuery() {
public int ExecuteNonQuery() {
using (ISqlReader reader = ExecuteReader()) {
while (reader.Read()) { }
return reader.RowsAffected;
Expand Down Expand Up @@ -835,7 +835,7 @@ internal byte[] GetBytes(int index) {
}
}

public sealed class SQLiteTransaction : ISqlTransaction
public class SQLiteTransaction : ISqlTransaction
{
SQLiteConnection conn;

Expand All @@ -855,13 +855,13 @@ internal SQLiteTransaction(SQLiteConnection connection) {
}

bool disposed;
public override void Dispose() {
public void Dispose() {
if (disposed) return;
if (IsValid(false)) IssueRollback(false);
disposed = true;
}

public override void Commit() {
public void Commit() {
SQLiteConnection.Check(conn);
IsValid(true);

Expand All @@ -873,7 +873,7 @@ public override void Commit() {
conn = null;
}

public override void Rollback() {
public void Rollback() {
SQLiteConnection.Check(conn);
IsValid(true);
IssueRollback(true);
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Database/IDatabaseBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected string InsertSql(string cmd, string table, string columns, int numArgs
#region Raw SQL functions

/// <summary> Executes an SQL command and returns the number of affected rows. </summary>
public int Execute(string sql, object[] parameters, bool createDB) {
public virtual int Execute(string sql, object[] parameters, bool createDB) {
int rows = 0;

using (ISqlConnection conn = CreateConnection()) {
Expand All @@ -170,7 +170,7 @@ public int Execute(string sql, object[] parameters, bool createDB) {
}

/// <summary> Excecutes an SQL query, invoking a callback on the returned rows one by one. </summary>
public int Iterate(string sql, object[] parameters, ReaderCallback callback) {
public virtual int Iterate(string sql, object[] parameters, ReaderCallback callback) {
int rows = 0;

using (ISqlConnection conn = CreateConnection()) {
Expand Down

0 comments on commit 1bf0f7a

Please sign in to comment.