PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/Client/Inbox2/Core/DataAccess/SQLiteDataService.cs

http://github.com/waseems/inbox2_desktop
C# | 458 lines | 290 code | 94 blank | 74 comment | 15 complexity | 94ee05582e585f1c8ffc2cda8737996a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.Composition;
  5. using System.Data;
  6. using System.Data.SQLite;
  7. using System.Linq;
  8. using Inbox2.Core.Configuration;
  9. using Inbox2.Core.DataAccess.Query;
  10. using Inbox2.Core.DataAccess.Reflection;
  11. using Inbox2.Framework;
  12. using Inbox2.Framework.Persistance;
  13. using Inbox2.Platform.Channels.Extensions;
  14. using ObjectExtensions=Inbox2.Platform.Framework.Extensions.ObjectExtensions;
  15. namespace Inbox2.Core.DataAccess
  16. {
  17. [Export(typeof(IDataService))]
  18. public class SQLiteDataService : IDataService
  19. {
  20. public T SelectByKey<T>(long key) where T : new()
  21. {
  22. QueryGenerator<T> generator = new QueryGenerator<T>();
  23. using (SQLiteConnection connection = CreateConnection())
  24. {
  25. SQLiteCommand command = generator.GetSelectCommand(key.ToString());
  26. command.Parameters.AddWithValue("@" + generator.Map.PrimaryKey.ColumName, key);
  27. command.Connection = connection;
  28. using (SQLiteDataReader reader = command.ExecuteReader())
  29. {
  30. while (reader.Read())
  31. {
  32. T instance = new T();
  33. using (new JoeCulture())
  34. ObjectExtensions.CreateFrom(instance, reader);
  35. return instance;
  36. }
  37. }
  38. }
  39. return default(T);
  40. }
  41. public T SelectBy<T>(object queryObject) where T : new()
  42. {
  43. QueryGenerator<T> generator = new QueryGenerator<T>();
  44. using (SQLiteConnection connection = CreateConnection())
  45. {
  46. var properties = queryObject.GetProperties().ToList();
  47. SQLiteCommand command = generator.GetSelectCommand(properties);
  48. command.Connection = connection;
  49. using (SQLiteDataReader reader = command.ExecuteReader())
  50. {
  51. while (reader.Read())
  52. {
  53. T instance = new T();
  54. using (new JoeCulture())
  55. ObjectExtensions.CreateFrom(instance, reader);
  56. return instance;
  57. }
  58. }
  59. }
  60. return default(T);
  61. }
  62. public T SelectBy<T>(string query) where T : new()
  63. {
  64. var command = CreateCommand();
  65. command.CommandText = query;
  66. using (SQLiteConnection connection = CreateConnection())
  67. {
  68. command.Connection = connection;
  69. using (SQLiteDataReader reader = (SQLiteDataReader)command.ExecuteReader())
  70. {
  71. while (reader.Read())
  72. {
  73. T instance = new T();
  74. using (new JoeCulture())
  75. ObjectExtensions.CreateFrom(instance, reader);
  76. return instance;
  77. }
  78. }
  79. }
  80. return default(T);
  81. }
  82. public T SelectBy<T>(IDbCommand command) where T : new()
  83. {
  84. using (SQLiteConnection connection = CreateConnection())
  85. {
  86. command.Connection = connection;
  87. using (SQLiteDataReader reader = (SQLiteDataReader)command.ExecuteReader())
  88. {
  89. while (reader.Read())
  90. {
  91. T instance = new T();
  92. using (new JoeCulture())
  93. ObjectExtensions.CreateFrom(instance, reader);
  94. return instance;
  95. }
  96. }
  97. }
  98. return default(T);
  99. }
  100. public IEnumerable<T> SelectAllBy<T>(object queryObject) where T : new()
  101. {
  102. QueryGenerator<T> generator = new QueryGenerator<T>();
  103. using (SQLiteConnection connection = CreateConnection())
  104. {
  105. var properties = queryObject.GetProperties().ToList();
  106. SQLiteCommand command = generator.GetSelectCommand(properties);
  107. command.Connection = connection;
  108. using (SQLiteDataReader reader = command.ExecuteReader())
  109. {
  110. while (reader.Read())
  111. {
  112. T instance = new T();
  113. using (new JoeCulture())
  114. ObjectExtensions.CreateFrom(instance, reader);
  115. yield return instance;
  116. }
  117. }
  118. }
  119. }
  120. public IEnumerable<T> SelectAllBy<T>(IDbCommand command) where T : new()
  121. {
  122. using (SQLiteConnection connection = CreateConnection())
  123. {
  124. command.Connection = connection;
  125. using (SQLiteDataReader reader = (SQLiteDataReader)command.ExecuteReader())
  126. {
  127. while (reader.Read())
  128. {
  129. T instance = new T();
  130. using (new JoeCulture())
  131. ObjectExtensions.CreateFrom(instance, reader);
  132. yield return instance;
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Selects all items from the table associated with T.
  139. /// </summary>
  140. /// <typeparam name="T"></typeparam>
  141. /// <returns></returns>
  142. public IEnumerable<T> SelectAll<T>() where T : new()
  143. {
  144. QueryGenerator<T> generator = new QueryGenerator<T>();
  145. using (SQLiteConnection connection = CreateConnection())
  146. {
  147. SQLiteCommand command = generator.GetSelectCommand();
  148. command.Connection = connection;
  149. using (SQLiteDataReader reader = command.ExecuteReader())
  150. {
  151. while (reader.Read())
  152. {
  153. T instance = new T();
  154. using (new JoeCulture())
  155. ObjectExtensions.CreateFrom(instance, reader);
  156. yield return instance;
  157. }
  158. }
  159. }
  160. }
  161. public IEnumerable<T> SelectAll<T>(IDbCommand command) where T : new()
  162. {
  163. using (SQLiteConnection connection = CreateConnection())
  164. {
  165. command.Connection = connection;
  166. using (SQLiteDataReader reader = ((SQLiteCommand) command).ExecuteReader())
  167. {
  168. while (reader.Read())
  169. {
  170. T instance = new T();
  171. using (new JoeCulture())
  172. ObjectExtensions.CreateFrom(instance, reader);
  173. yield return instance;
  174. }
  175. }
  176. }
  177. }
  178. public IEnumerable<T> SelectAll<T>(string query) where T : new()
  179. {
  180. using (SQLiteConnection connection = CreateConnection())
  181. {
  182. SQLiteCommand command = new SQLiteCommand(query);
  183. command.Connection = connection;
  184. using (SQLiteDataReader reader = command.ExecuteReader())
  185. {
  186. while (reader.Read())
  187. {
  188. T instance = new T();
  189. using (new JoeCulture())
  190. ObjectExtensions.CreateFrom(instance, reader);
  191. yield return instance;
  192. }
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// Saves the specified instance.
  198. /// </summary>
  199. /// <typeparam name="T"></typeparam>
  200. /// <param name="instance">The instance.</param>
  201. public void Save<T>(T instance)
  202. {
  203. if (instance is IDataServiceHooks)
  204. (instance as IDataServiceHooks).BeforeSave();
  205. QueryGenerator<T> generator = new QueryGenerator<T>();
  206. using (SQLiteConnection connection = CreateConnection())
  207. {
  208. SQLiteCommand command = generator.GetInsertCommand(instance);
  209. command.Connection = connection;
  210. object id = command.ExecuteScalar();
  211. // Set the new id on the primary key
  212. Reflector.SetPropertyValue(instance, generator.Map.PrimaryKey, id);
  213. }
  214. if (instance is IDataServiceHooks)
  215. (instance as IDataServiceHooks).AfterSave();
  216. }
  217. /// <summary>
  218. /// Updates the specified instance.
  219. /// </summary>
  220. /// <typeparam name="T"></typeparam>
  221. /// <param name="instance">The instance.</param>
  222. public void Update<T>(T instance)
  223. {
  224. if (instance is IDataServiceHooks)
  225. (instance as IDataServiceHooks).BeforeUpdate();
  226. QueryGenerator<T> generator = new QueryGenerator<T>();
  227. SQLiteCommand command = generator.GetUpdateCommand(instance);
  228. ExecuteNonQuery(command);
  229. if (instance is IDataServiceHooks)
  230. (instance as IDataServiceHooks).AfterUpdate();
  231. }
  232. /// <summary>
  233. /// Deletes the specified instance.
  234. /// </summary>
  235. /// <typeparam name="T"></typeparam>
  236. /// <param name="instance">The instance.</param>
  237. public void Delete<T>(T instance)
  238. {
  239. if (instance is IDataServiceHooks)
  240. (instance as IDataServiceHooks).BeforeDelete();
  241. QueryGenerator<T> generator = new QueryGenerator<T>();
  242. SQLiteCommand command = generator.GetDeleteCommand(instance);
  243. ExecuteNonQuery(command);
  244. if (instance is IDataServiceHooks)
  245. (instance as IDataServiceHooks).AfterDelete();
  246. }
  247. /// <summary>
  248. /// Deletes all the rows of the specified type with the specified where clause.
  249. /// </summary>
  250. /// <typeparam name="T"></typeparam>
  251. /// <param name="queryObject">The query object.</param>
  252. public void Delete<T>(object queryObject)
  253. {
  254. QueryGenerator<T> generator = new QueryGenerator<T>();
  255. var properties = queryObject.GetProperties().ToList();
  256. SQLiteCommand command = generator.GetDeleteCommand(properties);
  257. ExecuteNonQuery(command);
  258. }
  259. /// <summary>
  260. /// Executes the given command and returns the scalar value casted to type T.
  261. /// </summary>
  262. /// <typeparam name="T"></typeparam>
  263. /// <param name="command">The command.</param>
  264. /// <returns></returns>
  265. public T ExecuteScalar<T>(IDbCommand command) where T : new()
  266. {
  267. using (SQLiteConnection connection = CreateConnection())
  268. {
  269. command.Connection = connection;
  270. var value = command.ExecuteScalar();
  271. TypeConverter conv = TypeDescriptor.GetConverter(typeof(T));
  272. return (T)conv.ConvertFromString(value.ToString());
  273. }
  274. }
  275. /// <summary>
  276. /// Executes the given query and returns the scalar value casted to type T.
  277. /// </summary>
  278. /// <typeparam name="T"></typeparam>
  279. /// <param name="query">The query.</param>
  280. /// <returns></returns>
  281. public T ExecuteScalar<T>(string query) where T : new()
  282. {
  283. return ExecuteScalar<T>(new SQLiteCommand(query));
  284. }
  285. /// <summary>
  286. /// Crreates the command.
  287. /// </summary>
  288. /// <returns></returns>
  289. public IDbCommand CreateCommand()
  290. {
  291. return new SQLiteCommand();
  292. }
  293. /// <summary>
  294. /// Creates the parameter.
  295. /// </summary>
  296. /// <returns></returns>
  297. public IDataParameter CreateParameter()
  298. {
  299. return new SQLiteParameter();
  300. }
  301. /// <summary>
  302. /// Creates the parameter with the given name and value.
  303. /// </summary>
  304. /// <param name="name">The name.</param>
  305. /// <param name="value">The value.</param>
  306. /// <returns></returns>
  307. public IDataParameter CreateParameter(string name, object value)
  308. {
  309. return new SQLiteParameter { ParameterName = name, Value = value };
  310. }
  311. /// <summary>
  312. /// Executes the query and returns a IDataReader instance.
  313. /// </summary>
  314. /// <param name="command">The command.</param>
  315. /// <returns></returns>
  316. public IDataReader ExecuteReader(IDbCommand command)
  317. {
  318. SQLiteConnection connection = CreateConnection();
  319. command.Connection = connection;
  320. return command.ExecuteReader(CommandBehavior.CloseConnection);
  321. }
  322. /// <summary>
  323. /// Executes the query and returns a IDataReader instance.
  324. /// </summary>
  325. /// <param name="query"></param>
  326. /// <returns></returns>
  327. public IDataReader ExecuteReader(string query)
  328. {
  329. var command = CreateCommand();
  330. command.CommandText = query;
  331. return ExecuteReader(command);
  332. }
  333. /// <summary>
  334. /// Executes the given command.
  335. /// </summary>
  336. /// <param name="command">The command.</param>
  337. public void ExecuteNonQuery(IDbCommand command)
  338. {
  339. using (SQLiteConnection connection = CreateConnection())
  340. {
  341. command.Connection = connection;
  342. command.ExecuteNonQuery();
  343. }
  344. }
  345. /// <summary>
  346. /// Executes the given query.
  347. /// </summary>
  348. /// <param name="query">The query.</param>
  349. public void ExecuteNonQuery(string query)
  350. {
  351. ExecuteNonQuery(new SQLiteCommand(query));
  352. }
  353. /// <summary>
  354. /// Clones the existing connection on the UI thread.
  355. /// </summary>
  356. /// <returns></returns>
  357. SQLiteConnection CreateConnection()
  358. {
  359. var connection = new SQLiteConnection("Data Source=" + DebugKeys.DefaultDataDirectory + "\\Inbox2.db3");
  360. connection.Open();
  361. return connection;
  362. }
  363. }
  364. }