SqliteUserDataRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //using System;
  2. //using System.Collections.Generic;
  3. //using System.IO;
  4. //using System.Linq;
  5. //using System.Threading;
  6. //using System.Threading.Tasks;
  7. //using MediaBrowser.Common.Configuration;
  8. //using MediaBrowser.Controller.Entities;
  9. //using MediaBrowser.Controller.Persistence;
  10. //using MediaBrowser.Model.Logging;
  11. //using SQLitePCL.pretty;
  12. //namespace Emby.Server.Implementations.Data
  13. //{
  14. // public class SqliteUserDataRepository : BaseSqliteRepository, IUserDataRepository
  15. // {
  16. // private SQLiteDatabaseConnection _connection;
  17. // public SqliteUserDataRepository(ILogger logger, IApplicationPaths appPaths)
  18. // : base(logger)
  19. // {
  20. // DbFilePath = Path.Combine(appPaths.DataPath, "userdata_v2.db");
  21. // }
  22. // protected override bool EnableConnectionPooling
  23. // {
  24. // get { return false; }
  25. // }
  26. // /// <summary>
  27. // /// Gets the name of the repository
  28. // /// </summary>
  29. // /// <value>The name.</value>
  30. // public string Name
  31. // {
  32. // get
  33. // {
  34. // return "SQLite";
  35. // }
  36. // }
  37. // /// <summary>
  38. // /// Opens the connection to the database
  39. // /// </summary>
  40. // /// <returns>Task.</returns>
  41. // public void Initialize(SQLiteDatabaseConnection connection, ReaderWriterLockSlim writeLock)
  42. // {
  43. // WriteLock.Dispose();
  44. // WriteLock = writeLock;
  45. // _connection = connection;
  46. // string[] queries = {
  47. // "create table if not exists UserDataDb.userdata (key nvarchar, userId GUID, rating float null, played bit, playCount int, isFavorite bit, playbackPositionTicks bigint, lastPlayedDate datetime null)",
  48. // "drop index if exists UserDataDb.idx_userdata",
  49. // "drop index if exists UserDataDb.idx_userdata1",
  50. // "drop index if exists UserDataDb.idx_userdata2",
  51. // "drop index if exists UserDataDb.userdataindex1",
  52. // "create unique index if not exists UserDataDb.userdataindex on userdata (key, userId)",
  53. // "create index if not exists UserDataDb.userdataindex2 on userdata (key, userId, played)",
  54. // "create index if not exists UserDataDb.userdataindex3 on userdata (key, userId, playbackPositionTicks)",
  55. // "create index if not exists UserDataDb.userdataindex4 on userdata (key, userId, isFavorite)",
  56. // //pragmas
  57. // "pragma temp_store = memory",
  58. // "pragma shrink_memory"
  59. // };
  60. // _connection.RunQueries(queries);
  61. // connection.RunInTransaction(db =>
  62. // {
  63. // var existingColumnNames = GetColumnNames(db, "userdata");
  64. // AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames);
  65. // AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames);
  66. // });
  67. // }
  68. // /// <summary>
  69. // /// Saves the user data.
  70. // /// </summary>
  71. // /// <param name="userId">The user id.</param>
  72. // /// <param name="key">The key.</param>
  73. // /// <param name="userData">The user data.</param>
  74. // /// <param name="cancellationToken">The cancellation token.</param>
  75. // /// <returns>Task.</returns>
  76. // /// <exception cref="System.ArgumentNullException">userData
  77. // /// or
  78. // /// cancellationToken
  79. // /// or
  80. // /// userId
  81. // /// or
  82. // /// userDataId</exception>
  83. // public Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken)
  84. // {
  85. // if (userData == null)
  86. // {
  87. // throw new ArgumentNullException("userData");
  88. // }
  89. // if (userId == Guid.Empty)
  90. // {
  91. // throw new ArgumentNullException("userId");
  92. // }
  93. // if (string.IsNullOrEmpty(key))
  94. // {
  95. // throw new ArgumentNullException("key");
  96. // }
  97. // return PersistUserData(userId, key, userData, cancellationToken);
  98. // }
  99. // public Task SaveAllUserData(Guid userId, IEnumerable<UserItemData> userData, CancellationToken cancellationToken)
  100. // {
  101. // if (userData == null)
  102. // {
  103. // throw new ArgumentNullException("userData");
  104. // }
  105. // if (userId == Guid.Empty)
  106. // {
  107. // throw new ArgumentNullException("userId");
  108. // }
  109. // return PersistAllUserData(userId, userData.ToList(), cancellationToken);
  110. // }
  111. // /// <summary>
  112. // /// Persists the user data.
  113. // /// </summary>
  114. // /// <param name="userId">The user id.</param>
  115. // /// <param name="key">The key.</param>
  116. // /// <param name="userData">The user data.</param>
  117. // /// <param name="cancellationToken">The cancellation token.</param>
  118. // /// <returns>Task.</returns>
  119. // public async Task PersistUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken)
  120. // {
  121. // cancellationToken.ThrowIfCancellationRequested();
  122. // using (WriteLock.Write())
  123. // {
  124. // _connection.RunInTransaction(db =>
  125. // {
  126. // SaveUserData(db, userId, key, userData);
  127. // });
  128. // }
  129. // }
  130. // private void SaveUserData(IDatabaseConnection db, Guid userId, string key, UserItemData userData)
  131. // {
  132. // var paramList = new List<object>();
  133. // var commandText = "replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (?, ?, ?,?,?,?,?,?,?,?)";
  134. // paramList.Add(key);
  135. // paramList.Add(userId.ToGuidParamValue());
  136. // paramList.Add(userData.Rating);
  137. // paramList.Add(userData.Played);
  138. // paramList.Add(userData.PlayCount);
  139. // paramList.Add(userData.IsFavorite);
  140. // paramList.Add(userData.PlaybackPositionTicks);
  141. // if (userData.LastPlayedDate.HasValue)
  142. // {
  143. // paramList.Add(userData.LastPlayedDate.Value.ToDateTimeParamValue());
  144. // }
  145. // else
  146. // {
  147. // paramList.Add(null);
  148. // }
  149. // paramList.Add(userData.AudioStreamIndex);
  150. // paramList.Add(userData.SubtitleStreamIndex);
  151. // db.Execute(commandText, paramList.ToArray());
  152. // }
  153. // /// <summary>
  154. // /// Persist all user data for the specified user
  155. // /// </summary>
  156. // private async Task PersistAllUserData(Guid userId, List<UserItemData> userDataList, CancellationToken cancellationToken)
  157. // {
  158. // cancellationToken.ThrowIfCancellationRequested();
  159. // using (WriteLock.Write())
  160. // {
  161. // _connection.RunInTransaction(db =>
  162. // {
  163. // foreach (var userItemData in userDataList)
  164. // {
  165. // SaveUserData(db, userId, userItemData.Key, userItemData);
  166. // }
  167. // });
  168. // }
  169. // }
  170. // /// <summary>
  171. // /// Gets the user data.
  172. // /// </summary>
  173. // /// <param name="userId">The user id.</param>
  174. // /// <param name="key">The key.</param>
  175. // /// <returns>Task{UserItemData}.</returns>
  176. // /// <exception cref="System.ArgumentNullException">
  177. // /// userId
  178. // /// or
  179. // /// key
  180. // /// </exception>
  181. // public UserItemData GetUserData(Guid userId, string key)
  182. // {
  183. // if (userId == Guid.Empty)
  184. // {
  185. // throw new ArgumentNullException("userId");
  186. // }
  187. // if (string.IsNullOrEmpty(key))
  188. // {
  189. // throw new ArgumentNullException("key");
  190. // }
  191. // var commandText = "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key = ? and userId=?";
  192. // var paramList = new List<object>();
  193. // paramList.Add(key);
  194. // paramList.Add(userId.ToGuidParamValue());
  195. // foreach (var row in _connection.Query(commandText, paramList.ToArray()))
  196. // {
  197. // return ReadRow(row);
  198. // }
  199. // return null;
  200. // }
  201. // public UserItemData GetUserData(Guid userId, List<string> keys)
  202. // {
  203. // if (userId == Guid.Empty)
  204. // {
  205. // throw new ArgumentNullException("userId");
  206. // }
  207. // if (keys == null)
  208. // {
  209. // throw new ArgumentNullException("keys");
  210. // }
  211. // if (keys.Count == 0)
  212. // {
  213. // return null;
  214. // }
  215. // return GetUserData(userId, keys[0]);
  216. // }
  217. // /// <summary>
  218. // /// Return all user-data associated with the given user
  219. // /// </summary>
  220. // /// <param name="userId"></param>
  221. // /// <returns></returns>
  222. // public IEnumerable<UserItemData> GetAllUserData(Guid userId)
  223. // {
  224. // if (userId == Guid.Empty)
  225. // {
  226. // throw new ArgumentNullException("userId");
  227. // }
  228. // var list = new List<UserItemData>();
  229. // using (WriteLock.Read())
  230. // {
  231. // var commandText = "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=?";
  232. // var paramList = new List<object>();
  233. // paramList.Add(userId.ToGuidParamValue());
  234. // foreach (var row in _connection.Query(commandText, paramList.ToArray()))
  235. // {
  236. // list.Add(ReadRow(row));
  237. // }
  238. // }
  239. // return list;
  240. // }
  241. // /// <summary>
  242. // /// Read a row from the specified reader into the provided userData object
  243. // /// </summary>
  244. // /// <param name="reader"></param>
  245. // private UserItemData ReadRow(IReadOnlyList<IResultSetValue> reader)
  246. // {
  247. // var userData = new UserItemData();
  248. // userData.Key = reader[0].ToString();
  249. // userData.UserId = reader[1].ReadGuid();
  250. // if (reader[2].SQLiteType != SQLiteType.Null)
  251. // {
  252. // userData.Rating = reader[2].ToDouble();
  253. // }
  254. // userData.Played = reader[3].ToBool();
  255. // userData.PlayCount = reader[4].ToInt();
  256. // userData.IsFavorite = reader[5].ToBool();
  257. // userData.PlaybackPositionTicks = reader[6].ToInt64();
  258. // if (reader[7].SQLiteType != SQLiteType.Null)
  259. // {
  260. // userData.LastPlayedDate = reader[7].ReadDateTime();
  261. // }
  262. // if (reader[8].SQLiteType != SQLiteType.Null)
  263. // {
  264. // userData.AudioStreamIndex = reader[8].ToInt();
  265. // }
  266. // if (reader[9].SQLiteType != SQLiteType.Null)
  267. // {
  268. // userData.SubtitleStreamIndex = reader[9].ToInt();
  269. // }
  270. // return userData;
  271. // }
  272. // protected override void Dispose(bool dispose)
  273. // {
  274. // // handled by library database
  275. // }
  276. // protected override void CloseConnection()
  277. // {
  278. // // handled by library database
  279. // }
  280. // }
  281. //}