cleanup SqlApi.cs and fix a disposable problem

This commit is contained in:
adelikat 2019-11-14 17:19:42 -06:00
parent af50d74b98
commit 8586d36942
1 changed files with 37 additions and 39 deletions

View File

@ -1,17 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SQLite; using System.Data.SQLite;
namespace BizHawk.Client.ApiHawk namespace BizHawk.Client.ApiHawk
{ {
public sealed class SqlApi : ISql public sealed class SqlApi : ISql
{ {
public SqlApi() : base() SQLiteConnection _dbConnection;
{ }
SQLiteConnection m_dbConnection;
string connectionString;
public string CreateDatabase(string name) public string CreateDatabase(string name)
{ {
@ -20,75 +15,76 @@ namespace BizHawk.Client.ApiHawk
SQLiteConnection.CreateFile(name); SQLiteConnection.CreateFile(name);
return "Database Created Successfully"; return "Database Created Successfully";
} }
catch (SQLiteException sqlEX) catch (SQLiteException sqlEx)
{ {
return sqlEX.Message; return sqlEx.Message;
} }
} }
public string OpenDatabase(string name) public string OpenDatabase(string name)
{ {
try try
{ {
SQLiteConnectionStringBuilder connBuilder = new SQLiteConnectionStringBuilder(); var connBuilder = new SQLiteConnectionStringBuilder
connBuilder.DataSource = name; {
connBuilder.Version = 3; //SQLite version DataSource = name,
connBuilder.JournalMode = SQLiteJournalModeEnum.Wal; //Allows for reads and writes to happen at the same time Version = 3,
connBuilder.DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted; //This only helps make the database lock left. May be pointless now JournalMode = SQLiteJournalModeEnum.Wal, // Allows for reads and writes to happen at the same time
connBuilder.SyncMode = SynchronizationModes.Off; //This shortens the delay for do synchronous calls. DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted, // This only helps make the database lock left. May be pointless now
m_dbConnection = new SQLiteConnection(connBuilder.ToString()); SyncMode = SynchronizationModes.Off // This shortens the delay for do synchronous calls.
connectionString = connBuilder.ToString(); };
m_dbConnection.Open();
m_dbConnection.Close(); _dbConnection = new SQLiteConnection(connBuilder.ToString());
_dbConnection.Open();
_dbConnection.Close();
return "Database Opened Successfully"; return "Database Opened Successfully";
} }
catch (SQLiteException sqlEX) catch (SQLiteException sqlEx)
{ {
return sqlEX.Message; return sqlEx.Message;
} }
} }
public string WriteCommand(string query = "") public string WriteCommand(string query = "")
{ {
if (query == "") if (string.IsNullOrWhiteSpace(query))
{ {
return "query is empty"; return "query is empty";
} }
try try
{ {
m_dbConnection.Open(); _dbConnection.Open();
string sql = query; var command = new SQLiteCommand(query, _dbConnection);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
m_dbConnection.Close(); _dbConnection.Close();
return "Command ran successfully"; return "Command ran successfully";
} }
catch (NullReferenceException nullEX) catch (NullReferenceException)
{ {
return "Database not open."; return "Database not open.";
} }
catch (SQLiteException sqlEX) catch (SQLiteException sqlEx)
{ {
m_dbConnection.Close(); _dbConnection.Close();
return sqlEX.Message; return sqlEx.Message;
} }
} }
public dynamic ReadCommand(string query = "") public dynamic ReadCommand(string query = "")
{ {
if (query == "") if (string.IsNullOrWhiteSpace(query))
{ {
return "query is empty"; return "query is empty";
} }
try try
{ {
var table = new Dictionary<string, object>(); var table = new Dictionary<string, object>();
m_dbConnection.Open(); _dbConnection.Open();
string sql = $"PRAGMA read_uncommitted =1;{query}"; string sql = $"PRAGMA read_uncommitted =1;{query}";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); using var command = new SQLiteCommand(sql, _dbConnection);
SQLiteDataReader reader = command.ExecuteReader(); SQLiteDataReader reader = command.ExecuteReader();
bool rows = reader.HasRows; bool rows = reader.HasRows;
long rowCount = 0; long rowCount = 0;
@ -97,32 +93,34 @@ namespace BizHawk.Client.ApiHawk
{ {
columns.Add(reader.GetName(i)); columns.Add(reader.GetName(i));
} }
while (reader.Read()) while (reader.Read())
{ {
for (int i = 0; i < reader.FieldCount; ++i) for (int i = 0; i < reader.FieldCount; ++i)
{ {
table[$"{columns[i]} {rowCount}"] = reader.GetValue(i); table[$"{columns[i]} {rowCount}"] = reader.GetValue(i);
} }
rowCount += 1; rowCount += 1;
} }
reader.Close(); reader.Close();
m_dbConnection.Close(); _dbConnection.Close();
if (rows == false) if (rows == false)
{ {
return "No rows found"; return "No rows found";
} }
return table; return table;
} }
catch (NullReferenceException) catch (NullReferenceException)
{ {
return "Database not opened."; return "Database not opened.";
} }
catch (SQLiteException sqlEX) catch (SQLiteException sqlEx)
{ {
m_dbConnection.Close(); _dbConnection.Close();
return sqlEX.Message; return sqlEx.Message;
} }
} }
} }