Add DB controller search method
This commit is contained in:
parent
146eba94f6
commit
38876d441e
@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.VisualBasic;
|
||||
using NLog;
|
||||
|
||||
namespace ChaosBot.Database
|
||||
@ -88,5 +90,58 @@ namespace ChaosBot.Database
|
||||
_logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
}
|
||||
|
||||
public static DataTable SelectQuery(string table, string selectColumns = "*", Dictionary<string, object> filterColumns = null, string orderByKey = null)
|
||||
{
|
||||
DataTable dt = new DataTable();
|
||||
|
||||
try
|
||||
{
|
||||
using (_conn)
|
||||
{
|
||||
_conn.Open();
|
||||
SqliteCommand cmd = _conn.CreateCommand();
|
||||
|
||||
string filter = null;
|
||||
if (filterColumns != null)
|
||||
{
|
||||
List<string> filterList = new List<string>();
|
||||
foreach (string key in filterColumns.Keys)
|
||||
{
|
||||
filterList.Add($"{key} = @{key}");
|
||||
}
|
||||
|
||||
filter = $"WHERE {Strings.Join(filterList.ToArray(), " AND ")}";
|
||||
|
||||
foreach (string key in filterColumns.Keys)
|
||||
{
|
||||
cmd.Parameters.AddWithValue($@"{key}", filterColumns.GetValueOrDefault(key));
|
||||
}
|
||||
}
|
||||
|
||||
string order = null;
|
||||
if (orderByKey != null)
|
||||
{
|
||||
order = $"ORDER BY ${orderByKey}";
|
||||
}
|
||||
|
||||
string query = $"SELECT {selectColumns} FROM {table} {filter} {order}";
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Prepare();
|
||||
SqliteDataReader executeReader = cmd.ExecuteReader();
|
||||
|
||||
dt.Load(executeReader);
|
||||
|
||||
_conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user