|
| 1 | +using DbSyncKit.DB.Interface; |
| 2 | +using System.Data; |
| 3 | + |
| 4 | + |
| 5 | +namespace DbSyncKit.DB |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Manages database operations for a specific database provider implementing the IDatabase interface. |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="T">Type of the database provider implementing IDatabase.</typeparam> |
| 11 | + public class DatabaseManager<T> : IDisposable where T : IDatabase |
| 12 | + { |
| 13 | + private readonly T _databaseProvider; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the DatabaseManager class. |
| 17 | + /// </summary> |
| 18 | + /// <param name="databaseProvider">The instance of the database provider.</param> |
| 19 | + public DatabaseManager(T databaseProvider) |
| 20 | + { |
| 21 | + _databaseProvider = databaseProvider ?? throw new ArgumentNullException(nameof(databaseProvider)); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Executes a query against the database and returns a list of results for a specific data type. |
| 26 | + /// </summary> |
| 27 | + /// <typeparam name="TItem">The type of data to retrieve, constrained to be an instance of DataContractUtility.</typeparam> |
| 28 | + /// <param name="query">The SQL query to execute.</param> |
| 29 | + /// <param name="tableName">The name of the table associated with the query.</param> |
| 30 | + /// <returns>A list of results of type <typeparamref name="TItem"/>.</returns> |
| 31 | + public List<TItem> ExecuteQuery<TItem>(string query, string tableName) where TItem : IDataContractComparer |
| 32 | + { |
| 33 | + List<TItem> result = new List<TItem>(); |
| 34 | + |
| 35 | + var dataset = _databaseProvider.ExecuteQuery(query, tableName); |
| 36 | + |
| 37 | + if (dataset.Tables.Contains(tableName)) |
| 38 | + { |
| 39 | + foreach (DataRow row in dataset.Tables[tableName]!.Rows) |
| 40 | + { |
| 41 | + result.Add((TItem)Activator.CreateInstance(typeof(TItem), new object[] { row })!); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Disposes of the DatabaseManager. |
| 50 | + /// </summary> |
| 51 | + public void Dispose() |
| 52 | + { |
| 53 | + // No additional cleanup required |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments