|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Data; |
3 | 4 | using System.Data.SqlClient; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.Linq; |
@@ -61,6 +62,226 @@ public static bool Save<T>(string connString, List<T> entities, string[] keys = |
61 | 62 |
|
62 | 63 | #region Load Methods |
63 | 64 |
|
| 65 | + #region Connection Depending |
| 66 | + /// <summary> |
| 67 | + /// Load single entity from database |
| 68 | + /// </summary> |
| 69 | + /// <typeparam name="T">entity type</typeparam> |
| 70 | + /// <param name="connection">connection to database</param> |
| 71 | + /// <param name="keyValue"></param> |
| 72 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 73 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 74 | + /// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns> |
| 75 | + public static T Load<T>(SqlConnection connection, object keyValue, string identity = "Id", |
| 76 | + string tableName = null) where T : class |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + ValidateConnection(connection); |
| 81 | + |
| 82 | + Dictionary<string, object> filter = new Dictionary<string, object>(); |
| 83 | + filter[identity += "=@0"] = keyValue; |
| 84 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 85 | + return entityHandler.LoadSingleEntity<T>(tableName ?? typeof(T).Name, filter); |
| 86 | + } |
| 87 | + catch (Exception ex) |
| 88 | + { |
| 89 | + Debug.WriteLine(ex.ToString()); |
| 90 | + return null; |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Load single entity from database. |
| 98 | + /// </summary> |
| 99 | + /// <typeparam name="T">entity type</typeparam> |
| 100 | + /// <param name="connection">current active sql connection</param> |
| 101 | + /// <param name="filter">filter made in [string key, object value]</param> |
| 102 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 103 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 104 | + /// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns> |
| 105 | + public static T Load<T>(SqlConnection connection, Dictionary<string, object> filter = null, string identity = "Id", |
| 106 | + string tableName = null) where T : class |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + ValidateConnection(connection); |
| 111 | + |
| 112 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 113 | + return entityHandler.LoadSingleEntity<T>(tableName ?? typeof(T).Name, filter); |
| 114 | + |
| 115 | + } |
| 116 | + catch (Exception ex) |
| 117 | + { |
| 118 | + Debug.WriteLine(ex.ToString()); |
| 119 | + return null; |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Get list of entities from database |
| 126 | + /// </summary> |
| 127 | + /// <typeparam name="T">Entity type</typeparam> |
| 128 | + /// <param name="connection">Current active sql connection</param> |
| 129 | + /// <param name="tableName">name of talbe, if not provided - classname will be used</param> |
| 130 | + /// <returns>List of found items of type T</returns> |
| 131 | + public static List<T> LoadList<T>(SqlConnection connection, string tableName = null) where T : class |
| 132 | + { |
| 133 | + try |
| 134 | + { |
| 135 | + ValidateConnection(connection); |
| 136 | + |
| 137 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 138 | + |
| 139 | + return entityHandler.LoadEntities<T>(tableName ?? typeof(T).Name, new Dictionary<string, object>()); |
| 140 | + } |
| 141 | + catch (SqlException ex) |
| 142 | + { |
| 143 | + Debug.Write(ex.ToString()); |
| 144 | + return null; |
| 145 | + } |
| 146 | + } |
| 147 | + /// <summary> |
| 148 | + /// Get list of enities from database, based on filter |
| 149 | + /// </summary> |
| 150 | + /// <typeparam name="T">Entity type</typeparam> |
| 151 | + /// <param name="connection">Current active SQL connection</param> |
| 152 | + /// <param name="filter">filter made in [string key, object value]</param> |
| 153 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 154 | + /// <returns>List of T</returns> |
| 155 | + public static List<T> LoadList<T>(SqlConnection connection, Dictionary<string, object> filter, string tableName = null) |
| 156 | + where T : class |
| 157 | + { |
| 158 | + try |
| 159 | + { |
| 160 | + ValidateConnection(connection); |
| 161 | + connection.Open(); |
| 162 | + |
| 163 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 164 | + return entityHandler.LoadEntities<T>(tableName ?? typeof(T).Name, filter ?? new Dictionary<string, object>()); |
| 165 | + } |
| 166 | + catch (SqlException ex) |
| 167 | + { |
| 168 | + Debug.Write(ex.ToString()); |
| 169 | + return null; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Loads list of T from query. |
| 175 | + /// </summary> |
| 176 | + /// <typeparam name="T">Entity type</typeparam> |
| 177 | + /// <param name="connection">Current active SQL connection</param> |
| 178 | + /// <param name="query">Select query</param> |
| 179 | + /// <param name="keyValue"></param> |
| 180 | + /// <param name="identity">Identity column</param> |
| 181 | + /// <param name="orderBy">Column to order result by</param> |
| 182 | + /// <returns>List of T</returns> |
| 183 | + public static List<T> LoadListByQuery<T>(SqlConnection connection, string query, object keyValue, |
| 184 | + string identity = "Id", string orderBy = "") where T : class |
| 185 | + { |
| 186 | + try |
| 187 | + { |
| 188 | + ValidateConnection(connection); |
| 189 | + |
| 190 | + Dictionary<string, object> filter = new Dictionary<string, object>(); |
| 191 | + if (keyValue != null) |
| 192 | + filter[identity += "=@0"] = keyValue; |
| 193 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 194 | + return entityHandler.LoadEntitiesByQuery<T>(query, filter, orderBy); |
| 195 | + |
| 196 | + } |
| 197 | + catch (SqlException ex) |
| 198 | + { |
| 199 | + Debug.Write(ex.ToString()); |
| 200 | + return null; |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + /// <summary> |
| 205 | + /// Get list of entities from database by query |
| 206 | + /// </summary> |
| 207 | + /// <typeparam name="T">enitity type</typeparam> |
| 208 | + /// <param name="connection">Current active SQL connection</param> |
| 209 | + /// <param name="query">sql query</param> |
| 210 | + /// <param name="filter"></param> |
| 211 | + /// <param name="orderBy">how to order / sort the results</param> |
| 212 | + /// <returns>list of specified entities if found</returns> |
| 213 | + public static List<T> LoadListByQuery<T>(SqlConnection connection, string query, Dictionary<string, object> filter, |
| 214 | + string orderBy = "") where T : class |
| 215 | + { |
| 216 | + try |
| 217 | + { |
| 218 | + ValidateConnection(connection); |
| 219 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 220 | + return entityHandler.LoadEntitiesByQuery<T>(query, filter, orderBy); |
| 221 | + |
| 222 | + } |
| 223 | + catch (SqlException ex) |
| 224 | + { |
| 225 | + Debug.Write(ex.ToString()); |
| 226 | + return null; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + /// <summary> |
| 231 | + /// Get a single antity from database, by custom query |
| 232 | + /// </summary> |
| 233 | + /// <typeparam name="T"></typeparam> |
| 234 | + /// <param name="connection">Current active SQL connection</param> |
| 235 | + /// <param name="query">sql query</param> |
| 236 | + /// <param name="keyValue"></param> |
| 237 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 238 | + /// <returns>single entity found be query</returns> |
| 239 | + public static T LoadByQuery<T>(SqlConnection connection, string query, object keyValue, string identity = "Id") |
| 240 | + where T : class |
| 241 | + { |
| 242 | + try |
| 243 | + { |
| 244 | + ValidateConnection(connection); |
| 245 | + var result = LoadListByQuery<T>(connection, query, keyValue, identity)?.FirstOrDefault(); |
| 246 | + |
| 247 | + return result; |
| 248 | + } |
| 249 | + catch (SqlException ex) |
| 250 | + { |
| 251 | + Debug.Write(ex.ToString()); |
| 252 | + return null; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + /// <summary> |
| 257 | + /// Get a single antity from database, by custom query |
| 258 | + /// </summary> |
| 259 | + /// <typeparam name="T">entity type</typeparam> |
| 260 | + /// <param name="connection">Current active SQL connection</param> |
| 261 | + /// <param name="query">sql query</param> |
| 262 | + /// <param name="filter"></param> |
| 263 | + /// <returns>single entity found be query</returns> |
| 264 | + public static T LoadByQuery<T>(SqlConnection connection, string query, Dictionary<string, object> filter) |
| 265 | + where T : class |
| 266 | + { |
| 267 | + try |
| 268 | + { |
| 269 | + ValidateConnection(connection); |
| 270 | + |
| 271 | + var result = LoadListByQuery<T>(connection, query, filter); |
| 272 | + return result?.FirstOrDefault(); |
| 273 | + } |
| 274 | + catch (SqlException ex) |
| 275 | + { |
| 276 | + Debug.Write(ex.ToString()); |
| 277 | + return null; |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + #endregion Connection Depending |
| 282 | + |
| 283 | + |
| 284 | + |
64 | 285 | /// <summary> |
65 | 286 | /// Load single entity from database. |
66 | 287 | /// </summary> |
@@ -288,5 +509,17 @@ public static T LoadByQuery<T>(string connString, string query, Dictionary<strin |
288 | 509 | } |
289 | 510 | } |
290 | 511 | #endregion Load |
| 512 | + |
| 513 | + #region Private methods |
| 514 | + private static void ValidateConnection(SqlConnection connection) |
| 515 | + { |
| 516 | + if (connection == null) |
| 517 | + throw new Exception("COnnection cannot be null - please privide valid connection"); |
| 518 | + |
| 519 | + if (connection.State == ConnectionState.Closed) |
| 520 | + connection.Open(); |
| 521 | + } |
| 522 | + |
| 523 | + #endregion Private methods |
291 | 524 | } |
292 | 525 | } |
0 commit comments