SqliteExtensions.cs 1.6 KB

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