Example #1
0
        public IEnumerable <KeyValuePair <TKey, IEnumerable <TValue> > > GetAll()
        {
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _getAll;

                var tmp = new System.Collections.Generic.Dictionary <TKey, List <TValue> >();
                var ret = new System.Collections.Generic.Dictionary <TKey, IEnumerable <TValue> >();
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (_keySerializer.TryDeserialize(reader, 0, out var key))
                        {
                            if (_valueSerializer.TryDeserialize(reader, 1, out var value))
                            {
                                if (!tmp.TryGetValue(key, out var values))
                                {
                                    values = new List <TValue>();
                                    tmp.Add(key, values);
                                    ret.Add(key, values);
                                }

                                values.Add(value);
                            }
                        }
                    }
                }

                return(ret);
            }
        }
Example #2
0
        public bool TryGet(TKey key, out TValue value)
        {
            ThrowIfDropped();

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _getQuery;
                command.Parameters.AddWithValue("@key", _keySerializer.Serialize(key));

                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        value = default(TValue);
                        return(false);
                    }

                    _valueSerializer.TryDeserialize(reader, valueOrdinal: 1, value: out value);
                    return(true);
                }
            }
        }
Example #3
0
 private IEnumerable <KeyValuePair <TKey, TValue> > GetAllInternal()
 {
     using (var command = _connection.CreateCommand())
     {
         command.CommandText = _getAllQuery;
         using (var reader = command.ExecuteReader())
         {
             while (reader.Read())
             {
                 if (_keySerializer.TryDeserialize(reader, valueOrdinal: 0, value: out var key))
                 {
                     if (_valueSerializer.TryDeserialize(reader, valueOrdinal: 1, value: out var value))
                     {
                         yield return(new KeyValuePair <TKey, TValue>(key, value));
                     }
                 }
             }
         }
     }
 }
Example #4
0
        public bool TryGetValue(RowId row, out TValue value)
        {
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _getByRowId;
                command.Parameters.AddWithValue("@rowid", row.Id);

                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        value = default(TValue);
                        return(false);
                    }

                    return(_valueSerializer.TryDeserialize(reader, 0, out value));
                }
            }
        }
Example #5
0
        private IEnumerable <KeyValuePair <Interval <TKey>, TValue> > GetAllInternal()
        {
            using (var command = _connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT minimum, maximum, value FROM {0}", _tableName);

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (_keySerializer.TryDeserialize(reader, 0, out var minimum) &&
                            _keySerializer.TryDeserialize(reader, 1, out var maximum) &&
                            _valueSerializer.TryDeserialize(reader, 2, out var value))
                        {
                            var interval = Interval.Create(minimum, maximum);
                            yield return(new KeyValuePair <Interval <TKey>, TValue>(interval, value));
                        }
                    }
                }
            }
        }
Example #6
0
        private IEnumerable <TValue> GetValuesInternal(TKey minimum, TKey maximum)
        {
            using (var command = _connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT value FROM {0} WHERE NOT (@maximum < minimum OR @minimum > maximum)",
                                                    _tableName);
                command.Parameters.AddWithValue("@minimum", _keySerializer.Serialize(minimum));
                command.Parameters.AddWithValue("@maximum", _keySerializer.Serialize(maximum));

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (_valueSerializer.TryDeserialize(reader, 0, out var value))
                        {
                            yield return(value);
                        }
                    }
                }
            }
        }
Example #7
0
        public T GetValue(RowId key)
        {
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT value FROM {0} WHERE id = @id", _tableName);
                command.Parameters.AddWithValue("@id", key.Id);
                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        throw new KeyNotFoundException();
                    }

                    if (!_serializer.TryDeserialize(reader, 0, out var value))
                    {
                        throw new NotImplementedException();
                    }

                    return(value);
                }
            }
        }
Example #8
0
        private bool TryPeek(out RowId rowId, out T value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _tryPeek;
                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        value = default(T);
                        rowId = new RowId();
                        return(false);
                    }

                    rowId = new RowId(reader.GetInt64(0));
                    return(_serializer.TryDeserialize(reader, 1, out value));
                }
            }
        }