Skip to content

Commit 10a8b06

Browse files
committed
added load methods
1 parent 68c9161 commit 10a8b06

3 files changed

Lines changed: 293 additions & 38 deletions

File tree

src/GenericSQLEntityHandler/GenericSQLEntityHandler.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<Compile Include="Enums.cs" />
4444
<Compile Include="GenericSQLEntityHandler.cs" />
4545
<Compile Include="Properties\AssemblyInfo.cs" />
46-
<Compile Include="SQLHandler.cs" />
46+
<Compile Include="GenericSQLHandler.cs" />
4747
<Compile Include="SQLTableCreator.cs" />
4848
</ItemGroup>
4949
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.SqlClient;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace GenericSQLEntityHandler
10+
{
11+
public static class GenericSQLHandler
12+
{
13+
#region Save Methods
14+
/// <summary>
15+
/// Save a single entity to the database
16+
/// </summary>
17+
/// <param name="connString">connection string</param>
18+
/// <param name="entity">entity that needs to be committed to the database</param>
19+
/// <param name="keys"></param>
20+
/// <param name="identity">coloumn in database the represents the id</param>
21+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
22+
/// <typeparam name="T">entity type</typeparam>
23+
/// <returns>true or false for success status</returns>
24+
public static bool Save<T>(string connString, T entity, string[] keys = null, string identity = "Id", string tableName = null) where T : class
25+
{
26+
if (keys == null || keys.Length == 0)
27+
keys = new[] { identity };
28+
29+
using (SqlConnection connection = new SqlConnection(connString))
30+
{
31+
connection.Open();
32+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
33+
return entityDataHandler.SaveEntity(entity, tableName ?? typeof(T).Name, keys, SaveType.InsertOrUpdate, identity);
34+
}
35+
}
36+
37+
38+
/// <summary>
39+
/// Save a list of entities to the database
40+
/// </summary>
41+
/// <param name="connString">connection string</param>
42+
/// <param name="entities">list of entities that needs to be committed to the database</param>
43+
/// <param name="keys"></param>
44+
/// <param name="identity">coloumn in database the represents the id</param>
45+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
46+
/// <typeparam name="T">entity type</typeparam>
47+
/// <returns>true or false for success status</returns>
48+
public static bool Save<T>(string connString, List<T> entities, string[] keys = null, string identity = "Id", string tableName = null) where T : class
49+
{
50+
if (keys == null || keys.Length == 0)
51+
keys = new[] { identity };
52+
53+
using (SqlConnection connection = new SqlConnection(connString))
54+
{
55+
connection.Open();
56+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
57+
return entityDataHandler.SaveEntities(entities, tableName ?? typeof(T).Name, keys, SaveType.InsertOrUpdate, identity);
58+
}
59+
}
60+
#endregion Save Methods
61+
62+
#region Load Methods
63+
64+
/// <summary>
65+
/// Load single entity from database.
66+
/// </summary>
67+
/// <param name="connString">connection string to database</param>
68+
/// <param name="keyValue"></param>
69+
/// <param name="identity">coloumn in database the represents the id</param>
70+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
71+
/// <typeparam name="T">entity type</typeparam>
72+
/// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns>
73+
public static T Load<T>(string connString, object keyValue, string identity = "Id", string tableName = null) where T : class
74+
{
75+
using (SqlConnection connection = new SqlConnection(connString))
76+
{
77+
try
78+
{
79+
connection.Open();
80+
Dictionary<string, object> filter = new Dictionary<string, object>();
81+
filter[identity += "=@0"] = keyValue;
82+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
83+
return entityDataHandler.LoadSingleEntity<T>(tableName ?? typeof(T).Name, filter);
84+
}
85+
catch (SqlException ex)
86+
{
87+
Debug.WriteLine(ex.ToString());
88+
return null;
89+
}
90+
}
91+
}
92+
93+
/// <summary>
94+
/// Load single entity from database.
95+
/// </summary>
96+
/// <param name="connString">connection string to database</param>
97+
/// <param name="filter"></param>
98+
/// <param name="identity">coloumn in database the represents the id</param>
99+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
100+
/// <typeparam name="T">entity type</typeparam>
101+
/// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns>
102+
public static T Load<T>(string connString, Dictionary<string, object> filter = null, string identity = "Id", string tableName = null) where T : class
103+
{
104+
using (SqlConnection connection = new SqlConnection(connString))
105+
{
106+
try
107+
{
108+
connection.Open();
109+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
110+
return entityDataHandler.LoadSingleEntity<T>(tableName ?? typeof(T).Name, filter);
111+
}
112+
catch (SqlException ex)
113+
{
114+
Debug.WriteLine(ex.ToString());
115+
return null;
116+
}
117+
}
118+
}
119+
120+
/// <summary>
121+
/// Get list of entities from database
122+
/// </summary>
123+
/// <param name="connString">connection string to database</param>
124+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
125+
/// <typeparam name="T">entity type</typeparam>
126+
/// <returns>list of found entities - null if none are found or caused an exception</returns>
127+
public static List<T> LoadList<T>(string connString, string tableName = null) where T : class
128+
{
129+
try
130+
{
131+
using (SqlConnection connection = new SqlConnection(connString))
132+
{
133+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
134+
connection.Open();
135+
return entityDataHandler.LoadEntities<T>(tableName ?? typeof(T).Name, new Dictionary<string, object>());
136+
}
137+
}
138+
catch (SqlException ex)
139+
{
140+
Debug.Write(ex.ToString());
141+
return null;
142+
}
143+
}
144+
145+
/// <summary>
146+
/// Get list of enities from database, based on filter
147+
/// </summary>
148+
/// <typeparam name="T">entity type</typeparam>
149+
/// <param name="connString">connectionstring</param>
150+
/// <param name="filter">filter made in [string key, object value]</param>
151+
/// <param name="tableName">name of table in db - if null uses entity name</param>
152+
/// <returns></returns>
153+
public static List<T> LoadList<T>(string connString, Dictionary<string, object> filter, string tableName = null) where T : class
154+
{
155+
try
156+
{
157+
using (SqlConnection connection = new SqlConnection(connString))
158+
{
159+
connection.Open();
160+
161+
GenericSQLEntityHandler handler = new GenericSQLEntityHandler(connection);
162+
163+
return handler.LoadEntities<T>(tableName ?? typeof(T).Name, filter ?? new Dictionary<string, object>());
164+
165+
}
166+
}
167+
catch (SqlException ex)
168+
{
169+
170+
Debug.WriteLine(ex.ToString());
171+
return null;
172+
}
173+
}
174+
175+
176+
/// <summary>
177+
/// Get list of entities from database by query
178+
/// </summary>
179+
/// <param name="connString">connection string</param>
180+
/// <param name="query">sql query</param>
181+
/// <param name="keyValue"></param>
182+
/// <param name="identity">coloumn in database the represents the id</param>
183+
/// <param name="orderBy">how to order / sort the results</param>
184+
/// <typeparam name="T">enitity type</typeparam>
185+
/// <returns>list of specified entities if found</returns>
186+
public static List<T> LoadListByQuery<T>(string connString, string query, object keyValue, string identity = "Id", string orderBy = "") where T : class
187+
{
188+
using (SqlConnection connection = new SqlConnection(connString))
189+
{
190+
try
191+
{
192+
connection.Open();
193+
Dictionary<string, object> filter = new Dictionary<string, object>();
194+
if (keyValue != null)
195+
filter[identity += "=@0"] = keyValue;
196+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
197+
return entityDataHandler.LoadEntitiesByQuery<T>(query, filter, orderBy);
198+
}
199+
catch (SqlException ex)
200+
{
201+
Debug.WriteLine(ex.ToString());
202+
return null;
203+
}
204+
}
205+
}
206+
207+
/// <summary>
208+
/// Get list of entities from database by query
209+
/// </summary>
210+
/// <param name="connString">connection string</param>
211+
/// <param name="query">sql query</param>
212+
/// <param name="filter"></param>
213+
/// <param name="orderBy">how to order / sort the results</param>
214+
/// <typeparam name="T">enitity type</typeparam>
215+
/// <returns>list of specified entities if found</returns>
216+
public static List<T> LoadListByQuery<T>(string connString, string query, Dictionary<string, object> filter, string orderBy = "") where T : class
217+
{
218+
using (SqlConnection connection = new SqlConnection(connString))
219+
{
220+
try
221+
{
222+
connection.Open();
223+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
224+
return entityDataHandler.LoadEntitiesByQuery<T>(query, filter, orderBy);
225+
}
226+
catch (SqlException ex)
227+
{
228+
Debug.WriteLine(ex.ToString());
229+
return null;
230+
}
231+
}
232+
}
233+
234+
235+
/// <summary>
236+
/// Get a single antity from database, by custom query
237+
/// </summary>
238+
/// <param name="connString">connection string</param>
239+
/// <param name="query">sql query</param>
240+
/// <param name="keyValue"></param>
241+
/// <param name="identity">coloumn in database the represents the id</param>
242+
/// <typeparam name="T">entity type</typeparam>
243+
/// <returns>single entity found be query</returns>
244+
public static T LoadByQuery<T>(string connString, string query, object keyValue, string identity = "Id") where T : class
245+
{
246+
using (SqlConnection connection = new SqlConnection(connString))
247+
{
248+
connection.Open();
249+
Dictionary<string, object> filter = new Dictionary<string, object>();
250+
251+
if (keyValue != null)
252+
filter[identity += "=@0"] = keyValue;
253+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
254+
var result = entityDataHandler.LoadEntitiesByQuery<T>(query, filter, "Id");
255+
256+
if (result != null && result.Count > 0)
257+
return result[0];
258+
else
259+
{
260+
return null;
261+
}
262+
}
263+
}
264+
265+
/// <summary>
266+
/// Get a single antity from database, by custom query
267+
/// </summary>
268+
/// <param name="connString">connection string</param>
269+
/// <param name="query">sql query</param>
270+
/// <param name="filter"></param>
271+
/// <typeparam name="T">entity type</typeparam>
272+
/// <returns>single entity found be query</returns>
273+
public static T LoadByQuery<T>(string connString, string query, Dictionary<string, object> filter) where T : class
274+
{
275+
using (SqlConnection connection = new SqlConnection(connString))
276+
{
277+
connection.Open();
278+
279+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
280+
var result = entityDataHandler.LoadEntitiesByQuery<T>(query, filter, "Id");
281+
282+
if (result != null && result.Count > 0)
283+
return result[0];
284+
else
285+
{
286+
return null;
287+
}
288+
}
289+
}
290+
#endregion Load
291+
}
292+
}

src/GenericSQLEntityHandler/SQLHandler.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)