Exemplo n.º 1
0
        private void ImportUserDataIfNeeded(ManagedConnection connection)
        {
            if (!_fileSystem.FileExists(_importFile))
            {
                return;
            }

            var fileToImport = _importFile;
            var isImported   = connection.Query("select IsUserDataImported from DataSettings").SelectScalarBool().FirstOrDefault();

            if (isImported)
            {
                return;
            }

            ImportUserData(connection, fileToImport);

            connection.RunInTransaction(db =>
            {
                using (var statement = db.PrepareStatement("replace into DataSettings (IsUserDataImported) values (@IsUserDataImported)"))
                {
                    statement.TryBind("@IsUserDataImported", true);
                    statement.MoveNext();
                }
            }, TransactionMode);
        }
Exemplo n.º 2
0
        private void RemoveEmptyPasswordHashes(ManagedConnection connection)
        {
            foreach (var user in RetrieveAllUsers(connection))
            {
                // If the user password is the sha1 hash of the empty string, remove it
                if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal) &&
                    !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal))
                {
                    continue;
                }

                user.Password = null;
                var serialized = JsonSerializer.SerializeToUtf8Bytes(user, _jsonOptions);

                connection.RunInTransaction(db =>
                {
                    using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
                    {
                        statement.TryBind("@InternalId", user.InternalId);
                        statement.TryBind("@data", serialized);
                        statement.MoveNext();
                    }
                }, TransactionMode);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Retrieve all users from the database
 /// </summary>
 /// <returns>IEnumerable{User}.</returns>
 private IEnumerable <User> RetrieveAllUsers(ManagedConnection connection)
 {
     foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
     {
         yield return(GetUser(row));
     }
 }
Exemplo n.º 4
0
        private void DisposeConnection()
        {
            try
            {
                lock (_disposeLock)
                {
                    using (WriteLock.Write())
                    {
                        if (_connection != null)
                        {
                            using (_connection)
                            {
                                _connection.Close();
                            }
                            _connection = null;
                        }

                        CloseConnection();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error disposing database");
            }
        }
Exemplo n.º 5
0
        protected void RunDefaultInitialization(ManagedConnection db)
        {
            var queries = new List <string>
            {
                "PRAGMA journal_mode=WAL",
                "PRAGMA page_size=4096",
                "PRAGMA synchronous=Normal"
            };

            if (EnableTempStoreMemory)
            {
                queries.AddRange(new List <string>
                {
                    "pragma default_temp_store = memory",
                    "pragma temp_store = memory"
                });
            }
            else
            {
                queries.AddRange(new List <string>
                {
                    "pragma temp_store = file"
                });
            }

            db.ExecuteAll(string.Join(";", queries.ToArray()));
            Logger.LogInformation("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool dispose)
        {
            if (dispose)
            {
                try
                {
                    lock (_disposeLock)
                    {
                        using (WriteLock.Write())
                        {
                            if (_connection != null)
                            {
                                using (_connection)
                                {
                                }
                                _connection = null;
                            }

                            CloseConnection();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error disposing database", ex);
                }
            }
        }
Exemplo n.º 7
0
        public static void Attach(ManagedConnection db, string path, string alias)
        {
            var commandText = string.Format("attach @path as {0};", alias);

            using (var statement = db.PrepareStatement(commandText))
            {
                statement.TryBind("@path", path);
                statement.MoveNext();
            }
        }
Exemplo n.º 8
0
        private void ImportUserData(ManagedConnection connection, string file)
        {
            SqliteExtensions.Attach(connection, file, "UserDataBackup");

            var columns = "key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex";

            connection.RunInTransaction(db =>
            {
                db.Execute("REPLACE INTO userdata(" + columns + ") SELECT " + columns + " FROM UserDataBackup.userdata;");
            }, TransactionMode);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Opens the connection to the database
        /// </summary>
        /// <returns>Task.</returns>
        public void Initialize(ReaderWriterLockSlim writeLock, ManagedConnection managedConnection, IUserManager userManager)
        {
            _connection = managedConnection;

            WriteLock.Dispose();
            WriteLock = writeLock;

            using (var connection = CreateConnection())
            {
                var userDatasTableExists = TableExists(connection, "UserDatas");
                var userDataTableExists  = TableExists(connection, "userdata");

                var users = userDatasTableExists ? null : userManager.Users.ToArray();

                connection.RunInTransaction(db =>
                {
                    db.ExecuteAll(string.Join(";", new[] {
                        "create table if not exists UserDatas (key nvarchar not null, userId INT not null, rating float null, played bit not null, playCount int not null, isFavorite bit not null, playbackPositionTicks bigint not null, lastPlayedDate datetime null, AudioStreamIndex INT, SubtitleStreamIndex INT)",

                        "drop index if exists idx_userdata",
                        "drop index if exists idx_userdata1",
                        "drop index if exists idx_userdata2",
                        "drop index if exists userdataindex1",
                        "drop index if exists userdataindex",
                        "drop index if exists userdataindex3",
                        "drop index if exists userdataindex4",
                        "create unique index if not exists UserDatasIndex1 on UserDatas (key, userId)",
                        "create index if not exists UserDatasIndex2 on UserDatas (key, userId, played)",
                        "create index if not exists UserDatasIndex3 on UserDatas (key, userId, playbackPositionTicks)",
                        "create index if not exists UserDatasIndex4 on UserDatas (key, userId, isFavorite)"
                    }));

                    if (userDataTableExists)
                    {
                        var existingColumnNames = GetColumnNames(db, "userdata");

                        AddColumn(db, "userdata", "InternalUserId", "int", existingColumnNames);
                        AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames);
                        AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames);

                        if (!userDatasTableExists)
                        {
                            ImportUserIds(db, users);

                            db.ExecuteAll("INSERT INTO UserDatas (key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex) SELECT key, InternalUserId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex from userdata where InternalUserId not null");
                        }
                    }
                }, TransactionMode);
            }
        }
Exemplo n.º 10
0
 private void TryMigrateToLocalUsersTable(ManagedConnection connection)
 {
     try
     {
         connection.RunQueries(new[]
         {
             "INSERT INTO LocalUsersv2 (guid, data) SELECT guid,data from users"
         });
     }
     catch (Exception ex)
     {
         Logger.LogError(ex, "Error migrating users database");
     }
 }
Exemplo n.º 11
0
        private User GetUser(Guid guid, ManagedConnection connection)
        {
            using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
            {
                statement.TryBind("@guid", guid);

                foreach (var row in statement.ExecuteQuery())
                {
                    return(GetUser(row));
                }
            }

            return(null);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Opens the connection to the database
        /// </summary>
        /// <returns>Task.</returns>
        public void Initialize(ReaderWriterLockSlim writeLock, ManagedConnection managedConnection)
        {
            _connection = managedConnection;

            WriteLock.Dispose();
            WriteLock = writeLock;

            using (var connection = CreateConnection())
            {
                string[] queries =
                {
                    "create table if not exists userdata (key nvarchar, userId GUID, rating float null, played bit, playCount int, isFavorite bit, playbackPositionTicks bigint, lastPlayedDate datetime null)",

                    "create table if not exists DataSettings (IsUserDataImported bit)",

                    "drop index if exists idx_userdata",
                    "drop index if exists idx_userdata1",
                    "drop index if exists idx_userdata2",
                    "drop index if exists userdataindex1",

                    "create unique index if not exists userdataindex on userdata (key, userId)",
                    "create index if not exists userdataindex2 on userdata (key, userId, played)",
                    "create index if not exists userdataindex3 on userdata (key, userId, playbackPositionTicks)",
                    "create index if not exists userdataindex4 on userdata (key, userId, isFavorite)",

                    "pragma shrink_memory"
                };

                connection.RunQueries(queries);

                connection.RunInTransaction(db =>
                {
                    var existingColumnNames = GetColumnNames(db, "userdata");

                    AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames);
                    AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames);
                }, TransactionMode);

                try
                {
                    ImportUserDataIfNeeded(connection);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error in ImportUserDataIfNeeded", ex);
                }
            }
        }
Exemplo n.º 13
0
        protected bool TableExists(ManagedConnection connection, string name)
        {
            return(connection.RunInTransaction(db =>
            {
                using (var statement = PrepareStatement(db, "select DISTINCT tbl_name from sqlite_master"))
                {
                    foreach (var row in statement.ExecuteQuery())
                    {
                        if (string.Equals(name, row.GetString(0), StringComparison.OrdinalIgnoreCase))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }, ReadTransactionMode));
        }
Exemplo n.º 14
0
 private void SetInitialDatabseValues(ManagedConnection connection)
 {
     try
     {
         connection.RunQueries(new[]
         {
             "INSERT INTO Groups VALUES (NULL, 'Cliente')",
             "INSERT INTO Groups VALUES (NULL, 'Administrador')",
             "INSERT INTO Groups VALUES (NULL, 'Revendedor Master')",
             "INSERT INTO Groups VALUES (NULL, 'Revendedor')",
             "INSERT INTO Plains VALUES (NULL, 'Básico', 1)",
             "INSERT INTO Plains VALUES (NULL, 'Padrão', 2)",
             "INSERT INTO Plains VALUES (NULL, 'Premium', 4)"
         });
     }
     catch (Exception ex)
     {
         Logger.LogError(ex, "Error inserting initial data to database");
     }
 }
Exemplo n.º 15
0
        protected ManagedConnection CreateConnection(bool isReadOnly = false)
        {
            if (_connection != null)
            {
                return(_connection);
            }

            lock (WriteLock)
            {
                if (!_versionLogged)
                {
                    _versionLogged = true;
                    Logger.LogInformation("Sqlite version: " + SQLite3.Version);
                    Logger.LogInformation("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray()));
                }

                ConnectionFlags connectionFlags;

                if (isReadOnly)
                {
                    //Logger.LogInformation("Opening read connection");
                    //connectionFlags = ConnectionFlags.ReadOnly;
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }
                else
                {
                    //Logger.LogInformation("Opening write connection");
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }

                if (EnableSingleConnection)
                {
                    connectionFlags |= ConnectionFlags.PrivateCache;
                }
                else
                {
                    connectionFlags |= ConnectionFlags.SharedCached;
                }

                connectionFlags |= ConnectionFlags.NoMutex;

                var db = SQLite3.Open(DbFilePath, connectionFlags, null);

                try
                {
                    if (string.IsNullOrWhiteSpace(_defaultWal))
                    {
                        _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First();

                        Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
                    }

                    var queries = new List <string>
                    {
                        //"PRAGMA cache size=-10000"
                        //"PRAGMA read_uncommitted = true",
                        "PRAGMA synchronous=Normal"
                    };

                    if (CacheSize.HasValue)
                    {
                        queries.Add("PRAGMA cache_size=" + CacheSize.Value.ToString(CultureInfo.InvariantCulture));
                    }

                    if (EnableTempStoreMemory)
                    {
                        queries.Add("PRAGMA temp_store = memory");
                    }
                    else
                    {
                        queries.Add("PRAGMA temp_store = file");
                    }

                    foreach (var query in queries)
                    {
                        db.Execute(query);
                    }
                }
                catch
                {
                    using (db)
                    {
                    }

                    throw;
                }

                _connection = new ManagedConnection(db, false);

                return(_connection);
            }
        }
Exemplo n.º 16
0
 public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
 {
     return(connection.PrepareStatement(sql));
 }
Exemplo n.º 17
0
 public IStatement PrepareStatement(ManagedConnection connection, string sql)
 => connection.PrepareStatement(sql);
Exemplo n.º 18
0
        protected ManagedConnection CreateConnection(bool isReadOnly = false)
        {
            if (_connection != null)
            {
                return(_connection);
            }

            lock (WriteLock)
            {
                if (!_versionLogged)
                {
                    _versionLogged = true;
                    Logger.Info("Sqlite version: " + SQLite3.Version);
                    Logger.Info("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray()));
                }

                ConnectionFlags connectionFlags;

                if (isReadOnly)
                {
                    //Logger.Info("Opening read connection");
                    //connectionFlags = ConnectionFlags.ReadOnly;
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }
                else
                {
                    //Logger.Info("Opening write connection");
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }

                if (EnableSingleConnection)
                {
                    connectionFlags |= ConnectionFlags.PrivateCache;
                }
                else
                {
                    connectionFlags |= ConnectionFlags.SharedCached;
                }

                connectionFlags |= ConnectionFlags.NoMutex;

                var db = SQLite3.Open(DbFilePath, connectionFlags, null);

                if (string.IsNullOrWhiteSpace(_defaultWal))
                {
                    _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First();

                    Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
                }

                var queries = new List <string>
                {
                    //"PRAGMA cache size=-10000"
                    //"PRAGMA read_uncommitted = true",
                    "PRAGMA synchronous=Normal"
                };

                if (CacheSize.HasValue)
                {
                    queries.Add("PRAGMA cache_size=-" + CacheSize.Value.ToString(CultureInfo.InvariantCulture));
                }

                if (EnableTempStoreMemory)
                {
                    queries.Add("PRAGMA temp_store = memory");
                }

                //var cacheSize = CacheSize;
                //if (cacheSize.HasValue)
                //{

                //}

                ////foreach (var query in queries)
                ////{
                ////    db.Execute(query);
                ////}

                //Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First());
                //Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First());

                /*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase))
                 * {
                 *  queries.Add("PRAGMA journal_mode=WAL");
                 *
                 *  using (WriteLock.Write())
                 *  {
                 *      db.ExecuteAll(string.Join(";", queries.ToArray()));
                 *  }
                 * }
                 * else*/
                foreach (var query in queries)
                {
                    db.Execute(query);
                }

                _connection = new ManagedConnection(db, false);

                return(_connection);
            }
        }