Exemplo n.º 1
0
        public virtual async Task SetAbsoluteKeyExpirationAsync(TKey key, DateTimeOffset expiration)
        {
            using (var cancellationTokenSource = CreateCancellationTokenSource())
            {
                using (var connection = await GetConnectionAsync(cancellationTokenSource.Token).ConfigureAwait(false))
                {
                    var keyColumnValues = KeyMapper.GetColumnValues(key);
                    var columnValues    = new Dictionary <string, object>
                    {
                        { _expirationColumnName, expiration }
                    };

                    using (var command = connection.CreateTextCommand(
                               DatabaseDriver.GetSqlStatementTemplate(SqlStatement.Update),
                               new
                    {
                        schemaName = DatabaseDriver.ParseIdentifier(Table.Schema ?? DatabaseDriver.DefaultSchema),
                        tableName = DatabaseDriver.ParseIdentifier(Table.Name),
                        columnValues = SqlHelper.GetCommaEqualsStatement(DatabaseDriver, columnValues.Keys.ToArray()),
                        filter = SqlHelper.GetAndEqualsStatement(DatabaseDriver, keyColumnValues.Keys.ToArray())
                    },
                               keyColumnValues.Concat(columnValues).Select(c => c.ToDbParameter(DatabaseDriver))))
                    {
                        if (await command.ExecuteNonQueryAsync(cancellationTokenSource.Token).ConfigureAwait(false) ==
                            0)
                        {
                            throw new ArgumentException("Invalid key", nameof(key));
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public virtual async Task <bool> RemoveExpirationAsync(TKey key)
        {
            using var cancellationTokenSource = CreateCancellationTokenSource();
            await using var connection        = await GetConnectionAsync(cancellationTokenSource.Token).ConfigureAwait(false);

            var keyColumnValues = KeyMapper.GetColumnValues(key);
            var columnValues    = new Dictionary <string, object>
            {
                { _expirationColumnName, DBNull.Value }
            };

            await using var command = connection.CreateTextCommand(
                            DatabaseDriver.GetSqlStatementTemplate(SqlStatement.Update),
                            new
            {
                schemaName   = DatabaseDriver.ParseIdentifier(Table.Schema ?? DatabaseDriver.DefaultSchema),
                tableName    = DatabaseDriver.ParseIdentifier(Table.Name),
                columnValues = SqlHelper.GetCommaEqualsStatement(DatabaseDriver, columnValues.Keys.ToArray()),
                filter       = SqlHelper.GetCombinedAndStatement(DatabaseDriver,
                                                                 SqlHelper.GetAndEqualsStatement(DatabaseDriver, keyColumnValues.Keys.ToArray()),
                                                                 SqlHelper.GetIsNotNullStatement(DatabaseDriver, columnValues.Keys.ToArray()))
            },
                            keyColumnValues.Concat(columnValues).Select(c => c.ToDbParameter(DatabaseDriver)));

            var teste = SqlHelper.GetCombinedAndStatement(DatabaseDriver,
                                                          SqlHelper.GetAndEqualsStatement(DatabaseDriver, keyColumnValues.Keys.ToArray()),
                                                          SqlHelper.GetIsNotNullStatement(DatabaseDriver, columnValues.Keys.ToArray()));

            if (await command.ExecuteNonQueryAsync(cancellationTokenSource.Token).ConfigureAwait(false) == 0)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        public virtual async Task <bool> TryUpdateAsync(TKey key, TValue newValue, TValue oldValue)
        {
            using (var cancellationTokenSource = CreateCancellationTokenSource())
            {
                using (var connection = await GetConnectionAsync(cancellationTokenSource.Token).ConfigureAwait(false))
                {
                    var oldColumnValues       = GetColumnValues(key, oldValue);
                    var filterOldColumnValues = oldColumnValues
                                                .Select(kv => new KeyValuePair <string, object>($"Old{kv.Key}", kv.Value))
                                                .ToDictionary(t => t.Key, t => t.Value);

                    var newColumnValues = GetColumnValues(key, newValue, true);

                    using (var command = connection.CreateTextCommand(
                               DatabaseDriver.GetSqlStatementTemplate(SqlStatement.Update),
                               new
                    {
                        schemaName = DatabaseDriver.ParseIdentifier(Table.Schema ?? DatabaseDriver.DefaultSchema),
                        tableName = DatabaseDriver.ParseIdentifier(Table.Name),
                        columnValues = SqlHelper.GetCommaEqualsStatement(DatabaseDriver, newColumnValues.Keys.ToArray()),
                        filter = SqlHelper.GetAndEqualsStatement(DatabaseDriver, oldColumnValues.Keys.ToArray(), filterOldColumnValues.Keys.ToArray())
                    },
                               newColumnValues.Concat(filterOldColumnValues).Select(c => c.ToDbParameter(DatabaseDriver))))
                    {
                        return(await command.ExecuteNonQueryAsync(cancellationTokenSource.Token).ConfigureAwait(false) == 1);
                    }
                }
            }
        }
Exemplo n.º 4
0
 public void DropTable(string schemaName, string tableName)
 {
     using (var connection = DatabaseDriver.CreateConnection(ConnectionString))
     {
         connection.Open();
         using (var command = connection.CreateCommand())
         {
             command.CommandText = $"DROP TABLE IF EXISTS {DatabaseDriver.ParseIdentifier(schemaName ?? DatabaseDriver.DefaultSchema)}.{DatabaseDriver.ParseIdentifier(tableName)}";
             command.ExecuteNonQuery();
         }
         connection.Close();
     }
 }