示例#1
0
        public void PutMany(IEnumerable <KeyValuePair <TKey, TValue> > values)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

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

            using (var transaction = _connection.BeginTransaction())
                using (var command = _connection.CreateCommand())
                {
                    command.CommandText = _putQuery;
                    var keyParameter   = command.Parameters.Add("@key", _keySerializer.DatabaseType);
                    var valueParameter = command.Parameters.Add("@value", _valueSerializer.DatabaseType);

                    foreach (var pair in values)
                    {
                        if (pair.Key == null)
                        {
                            throw new ArgumentException("Null keys cannot be added to this collection");
                        }

                        keyParameter.Value   = _keySerializer.Serialize(pair.Key);
                        valueParameter.Value = _valueSerializer.Serialize(pair.Value);
                        command.ExecuteNonQuery();
                    }

                    transaction.Commit();
                }
        }
示例#2
0
        public void Enqueue(T value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _put;
                command.Parameters.AddWithValue("@value", _serializer.Serialize(value));
                command.ExecuteNonQuery();
            }
        }
示例#3
0
        public RowId Put(T value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _put;
                command.Parameters.AddWithValue("@value", _serializer.Serialize(value));
                command.ExecuteNonQuery();
                return(new RowId(_connection.LastInsertRowId));
            }
        }
示例#4
0
        public void Put(TKey key, TValue value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = string.Format("INSERT OR REPLACE INTO {0} (key, value) VALUES (@key, @value)",
                                                    _tableName);

                command.Parameters.AddWithValue("@key", _keySerializer.Serialize(key));
                command.Parameters.AddWithValue("@value", _valueSerializer.Serialize(value));

                command.ExecuteNonQuery();
            }
        }
示例#5
0
        public RowId Put(Point2D key, TValue value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _putQuery;
                command.Parameters.AddWithValue("@x", key.X);
                command.Parameters.AddWithValue("@y", key.Y);
                command.Parameters.AddWithValue("@value", _valueSerializer.Serialize(value));
                command.ExecuteNonQuery();

                var id = _connection.LastInsertRowId;
                return(new RowId(id));
            }
        }
示例#6
0
        public bool Add(T value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

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

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = string.Format("INSERT OR IGNORE INTO {0} (value) VALUES (@value)", _tableName);
                command.Parameters.AddWithValue("@value", _serializer.Serialize(value));
                int numRowsAffected = command.ExecuteNonQuery();
                return(numRowsAffected > 0);
            }
        }
示例#7
0
        public void Put(Interval <TKey> interval, TValue value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

            using (var command = _connection.CreateCommand())
            {
                var id = Interlocked.Increment(ref _lastId);
                command.CommandText = string.Format("INSERT INTO {0} (id, minimum, maximum, value) VALUES (@id, @minimum, @maximum, @value)",
                                                    _tableName);
                command.Parameters.AddWithValue("@id", id);
                command.Parameters.AddWithValue("@minimum", _keySerializer.Serialize(interval.Minimum));
                command.Parameters.AddWithValue("@maximum", _keySerializer.Serialize(interval.Maximum));
                command.Parameters.AddWithValue("@value", _valueSerializer.Serialize(value));

                command.ExecuteNonQuery();
            }
        }
示例#8
0
        public RowId Put(TKey key, TValue value)
        {
            ThrowIfReadOnly();
            ThrowIfDropped();

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

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

                command.Parameters.AddWithValue("@key", _keySerializer.Serialize(key));
                command.Parameters.AddWithValue("@value", _valueSerializer.Serialize(value));
                command.ExecuteNonQuery();

                var id = _connection.LastInsertRowId;
                return(new RowId(id));
            }
        }
示例#9
0
        public bool ContainsKey(TKey key)
        {
            ThrowIfDropped();

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

            using (var command = _connection.CreateCommand())
            {
                command.CommandText = _existsQuery;
                command.Parameters.AddWithValue("@key", _keySerializer.Serialize(key));
                var value = Convert.ToInt64(command.ExecuteScalar());
                return(value != 0);
            }
        }