PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/BurnSystems.EntityConnector/EntityPool.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 186 lines | 139 code | 24 blank | 23 comment | 6 complexity | cd910ce833b9fce4376c2b74fd02cb02 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BurnSystems.AdoNet.Queries;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. namespace BurnSystems.EntityConnector
  9. {
  10. /// <summary>
  11. /// Gives access to a bunch of entity
  12. /// </summary>
  13. public class EntityPool
  14. {
  15. /// <summary>
  16. /// Defines the connectionstring
  17. /// </summary>
  18. private string connectionString;
  19. /// <summary>
  20. /// Gets the poolname
  21. /// </summary>
  22. public string PoolName
  23. {
  24. get;
  25. private set;
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of EntityPool class.
  29. /// </summary>
  30. /// <param name="connectionString">Connectionstring to be used for database access</param>
  31. public EntityPool(string poolName, string connectionString)
  32. {
  33. this.PoolName = poolName;
  34. this.connectionString = connectionString;
  35. }
  36. /// <summary>
  37. /// Adds an entity to pool
  38. /// </summary>
  39. /// <param name="entity">Entity to be added</param>
  40. public void Add(Entity entity)
  41. {
  42. using (var db = new SqlConnection(this.connectionString))
  43. {
  44. db.Open();
  45. var data = new Dictionary<string, object>();
  46. data["Pool"] = this.PoolName;
  47. if (entity.Id == 0)
  48. {
  49. var selectQuery = new FreeQuery("SELECT MAX(Id) FROM Entities WHERE Pool=@Pool GROUP BY Pool");
  50. selectQuery.AddParameter("@Pool", this.PoolName);
  51. long maxId = db.ExecuteScalar<long>(selectQuery);
  52. data["Id"] = maxId + 1;
  53. entity.Id = maxId + 1;
  54. }
  55. else
  56. {
  57. data["Id"] = entity.Id;
  58. }
  59. foreach (var entry in entity.Data)
  60. {
  61. data["DataKey"] = entry.Key;
  62. data["DataValue"] = entry.Value;
  63. var query = new InsertQuery("Entities", data);
  64. db.ExecuteNonQuery(query);
  65. }
  66. }
  67. }
  68. public void Delete(Entity entity)
  69. {
  70. this.Delete(entity.Id);
  71. }
  72. private void Delete(long id)
  73. {
  74. using (var db = new SqlConnection(this.connectionString))
  75. {
  76. db.Open();
  77. var where = new Dictionary<string, object>();
  78. where["Pool"] = this.PoolName;
  79. where["Id"] = id;
  80. var deleteQuery = new DeleteQuery("Entities", where);
  81. db.ExecuteNonQuery(deleteQuery);
  82. }
  83. }
  84. public void Update(Entity entity)
  85. {
  86. // Quite simple
  87. this.Delete(entity);
  88. this.Add(entity);
  89. }
  90. /// <summary>
  91. /// Gets by datareader
  92. /// </summary>
  93. /// <param name="reader">Datareader to be used</param>
  94. /// <returns>List of entities</returns>
  95. public IList<Entity> Get(IDataReader reader)
  96. {
  97. var entities = new Dictionary<long, Entity>();
  98. while (reader.Read())
  99. {
  100. var id = Convert.ToInt64(reader["Id"]);
  101. var poolName = reader["Pool"].ToString();
  102. var key = reader["DataKey"].ToString();
  103. var value = reader["DataValue"].ToString();
  104. if (poolName != this.PoolName)
  105. {
  106. continue;
  107. }
  108. Entity found;
  109. if (!entities.TryGetValue(id, out found))
  110. {
  111. found = new Entity();
  112. found.Id = id;
  113. entities[id] = found;
  114. }
  115. found[key] = value;
  116. }
  117. return entities.Select(x => x.Value).ToList();
  118. }
  119. public IList<Entity> Get(Query query)
  120. {
  121. using (var db = new SqlConnection(this.connectionString))
  122. {
  123. db.Open();
  124. using (var reader = db.ExecuteReader(query))
  125. {
  126. return this.Get(reader);
  127. }
  128. }
  129. }
  130. public IList<Entity> GetAll()
  131. {
  132. var where = new Dictionary<string, object>();
  133. where["Pool"] = this.PoolName;
  134. var query = new SelectQuery("Entities", where);
  135. query.OrderBy = "Id";
  136. return this.Get(query);
  137. }
  138. public Entity GetById(long id)
  139. {
  140. var where = new Dictionary<string, object>();
  141. where["Pool"] = this.PoolName;
  142. where["Id"] = id;
  143. return this.Get(new SelectQuery("Entities", where)).FirstOrDefault();
  144. }
  145. public IList<Entity> GetAllWith(string key, string value)
  146. {
  147. var freeQuery = new FreeQuery(
  148. @"SELECT [t0].[Id], [t0].[Pool], [t0].[DataKey], [t0].[DataValue]
  149. FROM [Entities] AS [t0]
  150. WHERE (EXISTS(
  151. SELECT NULL AS [EMPTY]
  152. FROM [Entities] AS [t1]
  153. WHERE ([t1].[Id] = [t0].[Id]) AND ([t1].[DataKey] = @p0) AND ([t1].[DataValue] = @p1) AND ([t1].[Pool] = @p2)
  154. )) AND ([t0].[Pool] = @p3)");
  155. freeQuery.AddParameter("@p0", key);
  156. freeQuery.AddParameter("@p1", value);
  157. freeQuery.AddParameter("@p2", this.PoolName);
  158. freeQuery.AddParameter("@p3", this.PoolName);
  159. return this.Get(freeQuery);
  160. }
  161. }
  162. }