From 0bea6032aba9171c7216d84ec388a3ea632ac303 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sat, 30 Nov 2019 23:27:14 +1000 Subject: [PATCH] Migrate SQLLuaLibrary to ApiHawk delegation --- .../lua/EmuLuaLibrary.SQL.cs | 117 ++---------------- 1 file changed, 9 insertions(+), 108 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs index 0969c2502f..3d3a870923 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.SQL.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data.SQLite; + using NLua; // ReSharper disable UnusedMember.Global namespace BizHawk.Client.Common { [Description("A library for performing SQLite operations.")] - public sealed class SqlLuaLibrary : LuaLibraryBase + public sealed class SqlLuaLibrary : DelegatingLuaLibrary { public SqlLuaLibrary(Lua lua) : base(lua) { } @@ -18,131 +18,32 @@ namespace BizHawk.Client.Common public override string Name => "SQL"; - SQLiteConnection _mDBConnection; - [LuaMethodExample("local stSQLcre = SQL.createdatabase( \"eg_db\" );")] [LuaMethod("createdatabase", "Creates a SQLite Database. Name should end with .db")] - public string CreateDatabase(string name) - { - try - { - SQLiteConnection.CreateFile(name); - return "Database Created Successfully"; - } - catch (SQLiteException sqlEx) - { - return sqlEx.Message; - } - } - + public string CreateDatabase(string name) => APIs.Sql.CreateDatabase(name); [LuaMethodExample("local stSQLope = SQL.opendatabase( \"eg_db\" );")] [LuaMethod("opendatabase", "Opens a SQLite database. Name should end with .db")] - public string OpenDatabase(string name) - { - try - { - var connBuilder = new SQLiteConnectionStringBuilder - { - DataSource = name, - Version = 3, - JournalMode = SQLiteJournalModeEnum.Wal, // Allows for reads and writes to happen at the same time - DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted, // This only helps make the database lock left. May be pointless now - SyncMode = SynchronizationModes.Off, // This shortens the delay for do synchronous calls. - }; - - _mDBConnection = new SQLiteConnection(connBuilder.ToString()); - _mDBConnection.Open(); - _mDBConnection.Close(); - return "Database Opened Successfully"; - } - catch (SQLiteException sqlEx) - { - return sqlEx.Message; - } - } + public string OpenDatabase(string name) => APIs.Sql.OpenDatabase(name); [LuaMethodExample("local stSQLwri = SQL.writecommand( \"CREATE TABLE eg_tab ( eg_tab_id integer PRIMARY KEY, eg_tab_row_name text NOT NULL ); INSERT INTO eg_tab ( eg_tab_id, eg_tab_row_name ) VALUES ( 1, 'Example table row' );\" );")] [LuaMethod("writecommand", "Runs a SQLite write command which includes CREATE,INSERT, UPDATE. " + "Ex: create TABLE rewards (ID integer PRIMARY KEY, action VARCHAR(20)) ")] - public string WriteCommand(string query = "") - { - if (query == "") - { - return "query is empty"; - } - try - { - _mDBConnection.Open(); - string sql = query; - SQLiteCommand command = new SQLiteCommand(sql, _mDBConnection); - command.ExecuteNonQuery(); - _mDBConnection.Close(); - - return "Command ran successfully"; - - } - catch (NullReferenceException) - { - return "Database not open."; - } - catch (SQLiteException sqlEx) - { - _mDBConnection.Close(); - return sqlEx.Message; - } - } + public string WriteCommand(string query = "") => APIs.Sql.WriteCommand(query); [LuaMethodExample("local obSQLrea = SQL.readcommand( \"SELECT * FROM eg_tab WHERE eg_tab_id = 1;\" );")] [LuaMethod("readcommand", "Run a SQLite read command which includes Select. Returns all rows into a LuaTable." + "Ex: select * from rewards")] public dynamic ReadCommand(string query = "") { - if (query == "") - { - return "query is empty"; - } - try + var result = APIs.Sql.ReadCommand(query); + if (result is Dictionary dict) { var table = Lua.NewTable(); - _mDBConnection.Open(); - string sql = $"PRAGMA read_uncommitted =1;{query}"; - using var command = new SQLiteCommand(sql, _mDBConnection); - SQLiteDataReader reader = command.ExecuteReader(); - bool rows = reader.HasRows; - long rowCount = 0; - var columns = new List(); - for (int i = 0; i < reader.FieldCount; ++i) //Add all column names into list - { - columns.Add(reader.GetName(i)); - } - while (reader.Read()) - { - for (int i = 0; i < reader.FieldCount; ++i) - { - table[$"{columns[i]} {rowCount}"] = reader.GetValue(i); - } - rowCount += 1; - } - reader.Close(); - _mDBConnection.Close(); - if (rows == false) - { - return "No rows found"; - } - + foreach (var kvp in dict) table[kvp.Key] = kvp.Value; return table; - - } - catch (NullReferenceException) - { - return "Database not opened."; - } - catch (SQLiteException sqlEx) - { - _mDBConnection.Close(); - return sqlEx.Message; } + return result; } } }