PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZebraFlickr/Core/Store/StoreWork.cs

https://bitbucket.org/garethl/zebraflickr
C# | 140 lines | 88 code | 23 blank | 29 comment | 4 complexity | b1d159cecec9a642a8b09666dd69e6c4 MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.Linq.Expressions;
  32. using SQLite;
  33. namespace ZebraFlickr.Core.Store
  34. {
  35. public class StoreWork : IDisposable
  36. {
  37. private readonly Action<SQLiteConnection> _onDisposed;
  38. private SQLiteConnection _connection;
  39. private Transaction _transaction;
  40. private int _usageCount = 1;
  41. public StoreWork(SQLiteConnection connection, Action<SQLiteConnection> onDisposed)
  42. {
  43. _connection = connection;
  44. _onDisposed = onDisposed;
  45. }
  46. #region Implementation of IDisposable
  47. /// <summary>
  48. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  49. /// </summary>
  50. /// <filterpriority>2</filterpriority>
  51. public void Dispose()
  52. {
  53. _usageCount--;
  54. if (_usageCount <= 0)
  55. {
  56. Debug.Assert(_connection != null);
  57. _onDisposed(_connection);
  58. _connection = null;
  59. }
  60. }
  61. #endregion
  62. public void IncrementUsages()
  63. {
  64. _usageCount++;
  65. }
  66. public ITransaction BeginTransaction()
  67. {
  68. if (_transaction != null)
  69. {
  70. _transaction.IncrementUsages();
  71. return _transaction;
  72. }
  73. _transaction = new Transaction(_connection, () => _transaction = null);
  74. return _transaction;
  75. }
  76. public void Add<T>(T value)
  77. {
  78. _connection.Insert(value);
  79. }
  80. public void Update<T>(T value)
  81. {
  82. _connection.Update(value);
  83. }
  84. public T FirstOrDefault<T>() where T : new()
  85. {
  86. return _connection.Table<T>().FirstOrDefault();
  87. }
  88. public T FirstOrDefault<T>(Expression<Func<T, bool>> @where) where T : new()
  89. {
  90. return _connection.Table<T>().Where(where).FirstOrDefault();
  91. }
  92. public T GetById<T>(int id) where T : new()
  93. {
  94. return _connection.Find<T>(id);
  95. }
  96. public IEnumerable<T> Get<T>() where T : new()
  97. {
  98. return _connection.Table<T>().Deferred();
  99. }
  100. public IEnumerable<T> Get<T>(Expression<Func<T, bool>> @where) where T : new()
  101. {
  102. return _connection.Table<T>().Deferred().Where(@where);
  103. }
  104. public void Delete<T>(object id)
  105. {
  106. _connection.Delete<T>(id);
  107. }
  108. public void DeleteAll<T>()
  109. {
  110. _connection.DeleteAll<T>();
  111. }
  112. public void Execute(string sql, params object[] args)
  113. {
  114. _connection.Execute(sql, args);
  115. }
  116. }
  117. }