|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.Linq;
|
6 | 6 | using System.Text.RegularExpressions;
|
| 7 | +using System.Threading.Tasks; |
7 | 8 |
|
8 | 9 | namespace QueryDB
|
9 | 10 | {
|
@@ -56,13 +57,13 @@ public DBContext(DB database, string connectionString)
|
56 | 57 | }
|
57 | 58 |
|
58 | 59 | /// <summary>
|
59 |
| - /// Retrieves records for 'Select' queries from the database. |
| 60 | + /// Executes and retrieves records for 'Select' queries from the database. |
60 | 61 | /// Converts column names to keys holding values, with multiple database rows returned into a list.
|
61 | 62 | /// Note: Use aliases in query for similar column names.
|
62 | 63 | /// </summary>
|
63 | 64 | /// <param name="selectSql">'Select' query.</param>
|
64 |
| - /// <param name="upperCaseKeys">Boolean parameter to return dictionary keys in uppercase. Default - 'false'.</param> |
65 |
| - /// <returns>List of data Dictionary with column names as keys holding values into a list for multiple rows of data.</returns> |
| 65 | + /// <param name="upperCaseKeys">Boolean parameter to return dictionary keys in uppercase. Default - <c>false</c>.</param> |
| 66 | + /// <returns>List of <see cref="DataDictionary"/> with column names as keys holding values into a list for multiple rows of data.</returns> |
66 | 67 | public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = false)
|
67 | 68 | {
|
68 | 69 | var dataList = new List<DataDictionary>();
|
@@ -102,12 +103,58 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
|
102 | 103 | }
|
103 | 104 |
|
104 | 105 | /// <summary>
|
105 |
| - /// Retrieves records for 'Select' queries from the database. |
| 106 | + /// Asynchronously executes and retrieves records for 'Select' queries from the database. |
| 107 | + /// Converts column names to keys holding values, with multiple database rows returned into a list. |
| 108 | + /// Note: Use aliases in query for similar column names. |
| 109 | + /// </summary> |
| 110 | + /// <param name="selectSql">'Select' query.</param> |
| 111 | + /// <param name="upperCaseKeys">Boolean parameter to return dictionary keys in uppercase. Default - <c>false</c>.</param> |
| 112 | + /// <returns>List of <see cref="DataDictionary"/> with column names as keys holding values into a list for multiple rows of data.</returns> |
| 113 | + public async Task<List<DataDictionary>> FetchDataAsync(string selectSql, bool upperCaseKeys = false) |
| 114 | + { |
| 115 | + var dataList = new List<DataDictionary>(); |
| 116 | + if (Database.Equals(DB.MSSQL)) |
| 117 | + { |
| 118 | + using (var msSqlDBConnection = GetSqlServerConnection()) |
| 119 | + { |
| 120 | + var _systemAdapter = new MSSQL.Adapter(); |
| 121 | + dataList = await _systemAdapter.FetchDataAsync(selectSql, msSqlDBConnection.SqlConnection, upperCaseKeys); |
| 122 | + } |
| 123 | + } |
| 124 | + else if (Database.Equals(DB.MySQL)) |
| 125 | + { |
| 126 | + using (var mySqlDBConnection = GetMySqlConnection()) |
| 127 | + { |
| 128 | + var _systemAdapter = new MySQL.Adapter(); |
| 129 | + dataList = await _systemAdapter.FetchDataAsync(selectSql, mySqlDBConnection.MySqlConnection, upperCaseKeys); |
| 130 | + } |
| 131 | + } |
| 132 | + else if (Database.Equals(DB.Oracle)) |
| 133 | + { |
| 134 | + using (var oracleDBConnection = GetOracleConnection()) |
| 135 | + { |
| 136 | + var _systemAdapter = new Oracle.Adapter(); |
| 137 | + dataList = await _systemAdapter.FetchDataAsync(selectSql, oracleDBConnection.OracleConnection, upperCaseKeys); |
| 138 | + } |
| 139 | + } |
| 140 | + else if (Database.Equals(DB.PostgreSQL)) |
| 141 | + { |
| 142 | + using (var postgreSqlDBConnection = GetPostgreSqlConnection()) |
| 143 | + { |
| 144 | + var _systemAdapter = new PostgreSQL.Adapter(); |
| 145 | + dataList = await _systemAdapter.FetchDataAsync(selectSql, postgreSqlDBConnection.PostgreSQLConnection, upperCaseKeys); |
| 146 | + } |
| 147 | + } |
| 148 | + return dataList; |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Executes and retrieves records for 'Select' queries from the database. |
106 | 153 | /// </summary>
|
107 | 154 | /// <typeparam name="T">Object entity to return data mapped into.</typeparam>
|
108 | 155 | /// <param name="selectSql">'Select' query.</param>
|
109 |
| - /// <param name="strict">Enables fetch data only for object <T> properties existing in database query result. Default - 'false'.</param> |
110 |
| - /// <returns>List of data rows mapped into object entity into a list for multiple rows of data.</returns> |
| 156 | + /// <param name="strict">Enables fetch data only for object type <typeparamref name="T"/> properties existing in database query result. Default - <c>false</c>.</param> |
| 157 | + /// <returns>List of data rows mapped into object of type <typeparamref name="T"/>.</returns> |
111 | 158 | public List<T> FetchData<T>(string selectSql, bool strict = false) where T : new()
|
112 | 159 | {
|
113 | 160 | var dataList = new List<T>();
|
@@ -146,6 +193,51 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
|
146 | 193 | return dataList;
|
147 | 194 | }
|
148 | 195 |
|
| 196 | + /// <summary> |
| 197 | + /// Asynchronously executes and retrieves records for 'Select' queries from the database. |
| 198 | + /// </summary> |
| 199 | + /// <typeparam name="T">Object entity to return data mapped into.</typeparam> |
| 200 | + /// <param name="selectSql">'Select' query.</param> |
| 201 | + /// <param name="strict">Enables fetch data only for object type <typeparamref name="T"/> properties existing in database query result. Default - <c>false</c>.</param> |
| 202 | + /// <returns>List of data rows mapped into object of type <typeparamref name="T"/>.</returns> |
| 203 | + public async Task<List<T>> FetchDataAsync<T>(string selectSql, bool strict = false) where T : new() |
| 204 | + { |
| 205 | + var dataList = new List<T>(); |
| 206 | + if (Database.Equals(DB.MSSQL)) |
| 207 | + { |
| 208 | + using (var msSqlDBConnection = GetSqlServerConnection()) |
| 209 | + { |
| 210 | + var _systemAdapter = new MSSQL.Adapter(); |
| 211 | + dataList = await _systemAdapter.FetchDataAsync<T>(selectSql, msSqlDBConnection.SqlConnection, strict); |
| 212 | + } |
| 213 | + } |
| 214 | + else if (Database.Equals(DB.MySQL)) |
| 215 | + { |
| 216 | + using (var mySqlDBConnection = GetMySqlConnection()) |
| 217 | + { |
| 218 | + var _systemAdapter = new MySQL.Adapter(); |
| 219 | + dataList = await _systemAdapter.FetchDataAsync<T>(selectSql, mySqlDBConnection.MySqlConnection, strict); |
| 220 | + } |
| 221 | + } |
| 222 | + else if (Database.Equals(DB.Oracle)) |
| 223 | + { |
| 224 | + using (var oracleDBConnection = GetOracleConnection()) |
| 225 | + { |
| 226 | + var _systemAdapter = new Oracle.Adapter(); |
| 227 | + dataList = await _systemAdapter.FetchDataAsync<T>(selectSql, oracleDBConnection.OracleConnection, strict); |
| 228 | + } |
| 229 | + } |
| 230 | + else if (Database.Equals(DB.PostgreSQL)) |
| 231 | + { |
| 232 | + using (var postgreSqlDBConnection = GetPostgreSqlConnection()) |
| 233 | + { |
| 234 | + var _systemAdapter = new PostgreSQL.Adapter(); |
| 235 | + dataList = await _systemAdapter.FetchDataAsync<T>(selectSql, postgreSqlDBConnection.PostgreSQLConnection, strict); |
| 236 | + } |
| 237 | + } |
| 238 | + return dataList; |
| 239 | + } |
| 240 | + |
149 | 241 | /// <summary>
|
150 | 242 | /// Executes a SQL query and returns the result as a string.
|
151 | 243 | /// </summary>
|
|
0 commit comments