Beispiel #1
0
        private async Task LoadInner()
        {
            using (var conn = _connector())
            {
                conn.Open();
                var command = conn.CreateCommand();
                command.CommandText = string.Format(SqlSelectAllFormat, _config.TableName);
                var reader = await command.ExecuteReaderAsync();

                while (await reader.ReadAsync())
                {
                    var contextKey = (int)reader[_config.ContextKeyColumn];
                    var elementKey = (int)reader[_config.ElementKeyColumn];
                    var value      = (string)reader[_config.ValueColumn];

                    ContextRecord record = null;
                    if (_contextRecords.TryGetValue(contextKey, out record))
                    {
                        record.Elements.Add(elementKey, value);
                    }
                    else
                    {
                        record = new ContextRecord();
                        record.Elements.Add(elementKey, value);
                        lock (_contextRecordsLock)
                        {
                            _contextRecords.Add(contextKey, record);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public bool TryGetValue(int contextKey, int elementKey, out string value)
        {
            ContextRecord context = null;

            if (ContextRecords.TryGetValue(contextKey, out context))
            {
                if (context.Elements.TryGetValue(elementKey, out value))
                {
                    return(true);
                }
            }
            value = null;
            return(false);
        }
Beispiel #3
0
        public async Task SaveValue(int contextKey, int elementKey, string value)
        {
            lock (_loadLock)
            {
                if (_loadState != SqlVaultLoadState.Loaded)
                {
                    throw new InvalidOperationException($"Vault is not in {SqlVaultLoadState.Loaded.ToString()} state.");
                }
            }

            using (var conn = _connector())
            {
                await conn.OpenAsync();

                var cmd = conn.CreateCommand();
                cmd.CommandText = string.Format(SqlSelectFormat, _config.TableName, _config.ContextKeyColumn, _config.ElementKeyColumn);
                AddParameter(ParamContextKey, contextKey, cmd);
                AddParameter(ParamElementKey, elementKey, cmd);
                AddParameter(ParamValue, value, cmd);

                var exists = false;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    exists = reader.HasRows;
                }

                cmd = conn.CreateCommand();
                if (exists)
                {
                    cmd.CommandText = string.Format(SqlUpdateFormat, _config.TableName, _config.ContextKeyColumn, _config.ElementKeyColumn, _config.ValueColumn);
                    AddParameter(ParamContextKey, contextKey, cmd);
                    AddParameter(ParamElementKey, elementKey, cmd);
                    AddParameter(ParamValue, value, cmd);
                    await cmd.ExecuteNonQueryAsync();

                    lock (_contextRecordsLock)
                    {
                        _contextRecords[contextKey].Elements[elementKey] = value;
                    }
                }
                else
                {
                    cmd.CommandText = string.Format(SqlInsertFormat, _config.TableName, _config.ContextKeyColumn, _config.ElementKeyColumn, _config.ValueColumn);
                    AddParameter(ParamContextKey, contextKey, cmd);
                    AddParameter(ParamElementKey, elementKey, cmd);
                    AddParameter(ParamValue, value, cmd);
                    await cmd.ExecuteNonQueryAsync();

                    ContextRecord context = null;
                    lock (_contextRecordsLock)
                    {
                        if (_contextRecords.TryGetValue(contextKey, out context))
                        {
                            context.Elements.Add(elementKey, value);
                        }
                        else
                        {
                            context = new ContextRecord();
                            context.Elements.Add(elementKey, value);
                            _contextRecords.Add(contextKey, context);
                        }
                    }
                }
            }
        }