SqliteExtensions.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Data;
  3. using System.Data.SQLite;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.Logging;
  6. namespace Emby.Server.Core.Data
  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,
  17. bool isReadOnly,
  18. bool enablePooling,
  19. int? cacheSize,
  20. ILogger logger)
  21. {
  22. if (string.IsNullOrEmpty(dbPath))
  23. {
  24. throw new ArgumentNullException("dbPath");
  25. }
  26. SQLiteConnection.SetMemoryStatus(false);
  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. // This is causing crashing under linux
  35. Pooling = enablePooling && Environment.OSVersion.Platform == PlatformID.Win32NT,
  36. ReadOnly = isReadOnly
  37. };
  38. var connectionString = connectionstr.ConnectionString;
  39. if (!enablePooling)
  40. {
  41. logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, connectionString);
  42. }
  43. var connection = new SQLiteConnection(connectionString);
  44. await connection.OpenAsync().ConfigureAwait(false);
  45. return connection;
  46. }
  47. }
  48. }