diff --git a/PluginManager/Database/SqlDatabase.cs b/PluginManager/Database/SqlDatabase.cs index 03a94ee..383b2c2 100644 --- a/PluginManager/Database/SqlDatabase.cs +++ b/PluginManager/Database/SqlDatabase.cs @@ -10,6 +10,10 @@ namespace PluginManager.Database private string ConnectionString; private SQLiteConnection Connection; + /// + /// Initialize a SQL connection by specifing its private path + /// + /// The path to the database (it is starting from ./Data/Resources/) public SqlDatabase(string fileName) { if (!fileName.StartsWith("./Data/Resources/")) @@ -21,13 +25,23 @@ namespace PluginManager.Database } + /// + /// Open the SQL Connection. To close use the Stop() method + /// + /// public async Task Open() { await Connection.OpenAsync(); - - //Console.WriteLine("Opened database successfully"); } + /// + /// + /// Insert into a specified table some values + /// + /// + /// The table name + /// The values to be inserted (in the correct order and number) + /// public async Task InsertAsync(string tableName, params string[] values) { string query = $"INSERT INTO {tableName} VALUES ("; @@ -44,6 +58,14 @@ namespace PluginManager.Database await command.ExecuteNonQueryAsync(); } + /// + /// + /// Insert into a specified table some values + /// + /// + /// The table name + /// The values to be inserted (in the correct order and number) + /// public void Insert(string tableName, params string[] values) { string query = $"INSERT INTO {tableName} VALUES ("; @@ -60,6 +82,13 @@ namespace PluginManager.Database command.ExecuteNonQuery(); } + /// + /// Remove every row in a table that has a certain propery + /// + /// The table name + /// The column name that the search is made by + /// The value that is searched in the specified column + /// public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue) { string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'"; @@ -68,6 +97,13 @@ namespace PluginManager.Database await command.ExecuteNonQueryAsync(); } + /// + /// Remove every row in a table that has a certain propery + /// + /// The table name + /// The column name that the search is made by + /// The value that is searched in the specified column + /// public void RemoveKey(string tableName, string KeyName, string KeyValue) { string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'"; @@ -76,7 +112,13 @@ namespace PluginManager.Database command.ExecuteNonQuery(); } - + /// + /// Check if the key exists in the table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the specified column + /// public async Task KeyExistsAsync(string tableName, string keyName, string KeyValue) { string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'"; @@ -87,6 +129,13 @@ namespace PluginManager.Database return false; } + /// + /// Check if the key exists in the table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the specified column + /// public bool KeyExists(string tableName, string keyName, string KeyValue) { string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'"; @@ -97,6 +146,14 @@ namespace PluginManager.Database return false; } + /// + /// Set value of a column in a table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the column specified + /// The column that has to be modified + /// The new value that will replace the old value from the column specified public async Task SetValueAsync(string tableName, string keyName, string KeyValue, string ResultColumnName, string ResultColumnValue) @@ -108,6 +165,15 @@ namespace PluginManager.Database $"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'"); } + /// + /// Set value of a column in a table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the column specified + /// The column that has to be modified + /// The new value that will replace the old value from the column specified + public void SetValue(string tableName, string keyName, string KeyValue, string ResultColumnName, string ResultColumnValue) { @@ -117,7 +183,14 @@ namespace PluginManager.Database Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'"); } - + /// + /// Get value from a column in a table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the specified column + /// The column that has the result + /// A string that has the requested value (can be null if nothing found) public async Task GetValueAsync(string tableName, string keyName, string KeyValue, string ResultColumnName) { @@ -127,6 +200,15 @@ namespace PluginManager.Database return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'"); } + /// + /// Get value from a column in a table + /// + /// The table name + /// The column that the search is made by + /// The value that is searched in the specified column + /// The column that has the result + /// A string that has the requested value (can be null if nothing found) + public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName) { if (!TableExists(tableName)) @@ -134,13 +216,24 @@ namespace PluginManager.Database return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'"); } - + + /// + /// Stop the connection to the SQL Database + /// + /// public async void Stop() { await Connection.CloseAsync(); } - public async Task AddColumnsToTableAsync(string tableName, string[] columns) + /// + /// Change the structure of a table by adding new columns + /// + /// The table name + /// The columns to be added + /// The type of the columns (TEXT, INTEGER, FLOAT, etc) + /// + public async Task AddColumnsToTableAsync(string tableName, string[] columns, string TYPE = "TEXT") { var command = Connection.CreateCommand(); command.CommandText = $"SELECT * FROM {tableName}"; @@ -153,13 +246,21 @@ namespace PluginManager.Database { if (!tableColumns.Contains(column)) { - command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} TEXT"; + command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}"; await command.ExecuteNonQueryAsync(); } } } - public void AddColumnsToTable(string tableName, string[] columns) + /// + /// Change the structure of a table by adding new columns + /// + /// The table name + /// The columns to be added + /// The type of the columns (TEXT, INTEGER, FLOAT, etc) + /// + + public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT") { var command = Connection.CreateCommand(); command.CommandText = $"SELECT * FROM {tableName}"; @@ -172,12 +273,17 @@ namespace PluginManager.Database { if (!tableColumns.Contains(column)) { - command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} TEXT"; + command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}"; command.ExecuteNonQuery(); } } } + /// + /// Check if a table exists + /// + /// The table name + /// True if the table exists, false if not public async Task TableExistsAsync(string tableName) { var cmd = Connection.CreateCommand(); @@ -189,6 +295,11 @@ namespace PluginManager.Database return true; } + /// + /// Check if a table exists + /// + /// The table name + /// True if the table exists, false if not public bool TableExists(string tableName) { var cmd = Connection.CreateCommand(); @@ -200,6 +311,13 @@ namespace PluginManager.Database return true; } + /// + /// Create a table + /// + /// The table name + /// The columns of the table + /// + public async Task CreateTableAsync(string tableName, params string[] columns) { var cmd = Connection.CreateCommand(); @@ -207,6 +325,13 @@ namespace PluginManager.Database await cmd.ExecuteNonQueryAsync(); } + /// + /// Create a table + /// + /// The table name + /// The columns of the table + /// + public void CreateTable(string tableName, params string[] columns) { var cmd = Connection.CreateCommand(); @@ -214,6 +339,11 @@ namespace PluginManager.Database cmd.ExecuteNonQuery(); } + /// + /// Execute a custom query + /// + /// The query + /// The number of rows that the query modified public async Task ExecuteAsync(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -223,6 +353,11 @@ namespace PluginManager.Database return answer; } + /// + /// Execute a custom query + /// + /// The query + /// The number of rows that the query modified public int Execute(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -233,6 +368,11 @@ namespace PluginManager.Database return r; } + /// + /// Read data from the result table and return the first row + /// + /// The query + /// The result is a string that has all values separated by space character public async Task ReadDataAsync(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -250,6 +390,11 @@ namespace PluginManager.Database return null; } + /// + /// Read data from the result table and return the first row + /// + /// The query + /// The result is a string that has all values separated by space character public string? ReadData(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -267,6 +412,11 @@ namespace PluginManager.Database return null; } + /// + /// Read data from the result table and return the first row + /// + /// The query + /// The first row as separated items public async Task ReadDataArrayAsync(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -284,6 +434,35 @@ namespace PluginManager.Database return null; } + + /// + /// Read data from the result table and return the first row + /// + /// The query + /// The first row as separated items + public object[]? ReadDataArray(string query) + { + if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) + Connection.Open(); + var command = new SQLiteCommand(query, Connection); + var reader = command.ExecuteReader(); + + object[] values = new object[reader.FieldCount]; + if (reader.Read()) + { + reader.GetValues(values); + return values; + } + + return null; + } + + /// + /// Read all rows from the result table and return them as a list of string arrays. The string arrays contain the values of each row + /// + /// The query + /// A list of string arrays representing the values that the query returns + public async Task?> ReadAllRowsAsync(string query) { if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) @@ -306,22 +485,5 @@ namespace PluginManager.Database return rows; } - - public object[]? ReadDataArray(string query) - { - if (!Connection.State.HasFlag(System.Data.ConnectionState.Open)) - Connection.Open(); - var command = new SQLiteCommand(query, Connection); - var reader = command.ExecuteReader(); - - object[] values = new object[reader.FieldCount]; - if (reader.Read()) - { - reader.GetValues(values); - return values; - } - - return null; - } } } \ No newline at end of file