SqliteExtensions.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. namespace MediaBrowser.Server.Implementations.Persistence
  12. {
  13. /// <summary>
  14. /// Class SQLiteExtensions
  15. /// </summary>
  16. public static class SqliteExtensions
  17. {
  18. /// <summary>
  19. /// Connects to db.
  20. /// </summary>
  21. public static async Task<IDbConnection> ConnectToDb(string dbPath, bool isReadOnly, bool enablePooling, int? cacheSize, ILogger logger)
  22. {
  23. if (string.IsNullOrEmpty(dbPath))
  24. {
  25. throw new ArgumentNullException("dbPath");
  26. }
  27. var connectionstr = new SQLiteConnectionStringBuilder
  28. {
  29. PageSize = 4096,
  30. CacheSize = cacheSize ?? 2000,
  31. SyncMode = SynchronizationModes.Normal,
  32. DataSource = dbPath,
  33. JournalMode = SQLiteJournalModeEnum.Wal,
  34. Pooling = enablePooling,
  35. ReadOnly = isReadOnly
  36. };
  37. var connectionString = connectionstr.ConnectionString;
  38. if (enablePooling)
  39. {
  40. connectionString += ";Max Pool Size=100";
  41. }
  42. //logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, connectionString);
  43. var connection = new SQLiteConnection(connectionString);
  44. await connection.OpenAsync().ConfigureAwait(false);
  45. return connection;
  46. }
  47. }
  48. }