Exemplo n.º 1
0
        private void findCurrentEventLogPositions()
        {
            using (var conn = _tenant.OpenConnection())
            {
                var cmd = new NpgsqlCommand($"select name, last_seq_id from {_store.Events.ProgressionTable}");
                using (var reader = conn.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        var name            = reader.GetFieldValue <string>(0);
                        var lastEncountered = reader.GetFieldValue <long>(1);

                        var track = _tracks.Values.FirstOrDefault(x => x.ProgressionName == name);

                        if (track != null)
                        {
                            track.LastEncountered = lastEncountered;
                        }
                    }
                }
            }

            foreach (var track in _tracks.Values)
            {
                Logger.DeterminedStartingPosition(track);
            }
        }
Exemplo n.º 2
0
        public void All <T>(string transformName)
        {
            var transform = _tenant.TransformFor(transformName);
            var mapping   = _tenant.MappingFor(typeof(T));

            var cmd = CommandBuilder.BuildCommand(sql =>
            {
                writeBasicSql(sql, mapping, transform);

                var where = mapping.ToQueryableDocument().DefaultWhereFragment();
                if (where != null)
                {
                    sql.Append(" where ");
                    where.Apply(sql);
                }
            });

            using (var conn = _tenant.OpenConnection())
            {
                conn.Execute(cmd, c =>
                {
                    c.ExecuteNonQuery();
                });
            }
        }
Exemplo n.º 3
0
        public void DeleteAllDocuments()
        {
            var dbObjects = new DbObjects(_tenant, _options.Storage);

            using (var conn = _tenant.OpenConnection(CommandRunnerMode.Transactional))
            {
                dbObjects.DocumentTables().Each(tableName =>
                {
                    var sql = "truncate {0} cascade".ToFormat(tableName);
                    conn.Execute(sql);
                });
                conn.Commit();
            }
        }
Exemplo n.º 4
0
        private static IManagedConnection buildManagedConnection(SessionOptions options, ITenant tenant,
                                                                 CommandRunnerMode commandRunnerMode)
        {
            // Hate crap like this, but if we don't control the transation, use External to direct
            // IManagedConnection not to call commit or rollback
            if (!options.OwnsTransactionLifecycle && commandRunnerMode != CommandRunnerMode.ReadOnly)
            {
                commandRunnerMode = CommandRunnerMode.External;
            }

            if (options.Transaction != null)
            {
                options.Connection = options.Transaction.Connection;
            }


            if (options.Connection == null)
            {
                return(tenant.OpenConnection(commandRunnerMode, options.IsolationLevel, options.Timeout));
            }
            else
            {
                return(new ManagedConnection(options, commandRunnerMode));
            }
        }
Exemplo n.º 5
0
        public void DeleteAllEventData()
        {
            using var connection = _tenant.OpenConnection(CommandRunnerMode.Transactional);
            var deleteEventDataSql = toDeleteEventDataSql();

            connection.Execute(deleteEventDataSql);
            connection.Commit();
        }
Exemplo n.º 6
0
        public async Task <HighWaterStatistics> DetectInSafeZone(DateTimeOffset safeTimestamp, CancellationToken token)
        {
            await using var conn = _tenant.OpenConnection();

            var statistics = await loadCurrentStatistics(conn, token);

            _safeTimestamp.Value = safeTimestamp;
            using (var reader = await conn.ExecuteReaderAsync(_findSafeSequence, token))
            {
                if (await reader.ReadAsync(token))
                {
                    statistics.SafeStartMark = await reader.GetFieldValueAsync <long>(0, token);
                }
            }

            await calculateHighWaterMark(token, statistics, conn);

            return(statistics);
        }
Exemplo n.º 7
0
        public void CompletelyRemoveAll()
        {
            var dbObjects = new DbObjects(_tenant, _options.Storage);

            using (var connection = _tenant.OpenConnection(CommandRunnerMode.Transactional))
            {
                var schemaTables = dbObjects.SchemaTables();
                schemaTables
                    .Each(tableName => { connection.Execute($"DROP TABLE IF EXISTS {tableName} CASCADE;"); });

                var allSchemas = new object[] { _options.Storage.AllSchemaNames() };

                var drops = connection.GetStringList(DropAllFunctionSql, allSchemas)
                    .Concat(connection.GetStringList(DropAllSequencesSql, allSchemas));
                drops.Each(drop => connection.Execute(drop));
                connection.Commit();

                _tenant.ResetSchemaExistenceChecks();
            }
        }
Exemplo n.º 8
0
        private static IManagedConnection buildManagedConnection(SessionOptions options, ITenant tenant,
                                                                 CommandRunnerMode commandRunnerMode)
        {
            // TODO -- this is all spaghetti code. Make this some kind of more intelligent state machine
            // w/ the logic encapsulated into SessionOptions

            // Hate crap like this, but if we don't control the transation, use External to direct
            // IManagedConnection not to call commit or rollback
            if (!options.OwnsTransactionLifecycle && commandRunnerMode != CommandRunnerMode.ReadOnly)
            {
                commandRunnerMode = CommandRunnerMode.External;
            }


            if (options.Connection != null || options.Transaction != null)
            {
                options.OwnsConnection = false;
            }
            if (options.Transaction != null)
            {
                options.Connection = options.Transaction.Connection;
            }



#if NET46 || NETSTANDARD2_0
            if (options.Connection == null && options.DotNetTransaction != null)
            {
                var connection = tenant.CreateConnection();
                connection.Open();

                options.OwnsConnection = true;
                options.Connection     = connection;
            }

            if (options.DotNetTransaction != null)
            {
                options.Connection.EnlistTransaction(options.DotNetTransaction);
                options.OwnsTransactionLifecycle = false;
            }
#endif

            if (options.Connection == null)
            {
                return(tenant.OpenConnection(commandRunnerMode, options.IsolationLevel, options.Timeout));
            }
            else
            {
                return(new ManagedConnection(options, commandRunnerMode));
            }
        }
Exemplo n.º 9
0
        public void Generate(StoreOptions options, string[] schemaNames)
        {
            if (schemaNames == null)
            {
                throw new ArgumentNullException(nameof(schemaNames));
            }

            var sql = GenerateScript(options, schemaNames);

            if (sql != null)
            {
                using (var runner = _tenant.OpenConnection())
                {
                    runner.Execute(sql);
                }
            }
        }
Exemplo n.º 10
0
        private static IManagedConnection buildManagedConnection(SessionOptions options, ITenant tenant,
                                                                 CommandRunnerMode commandRunnerMode)
        {
            // Hate crap like this, but if we don't control the transation, use External to direct
            // IManagedConnection not to call commit or rollback
            if (!options.OwnsTransactionLifecycle && commandRunnerMode != CommandRunnerMode.ReadOnly)
            {
                commandRunnerMode = CommandRunnerMode.External;
            }

            if (options.Transaction != null)
            {
                options.Connection = options.Transaction.Connection;
            }



#if NET46 || NETSTANDARD2_0
            if (options.Connection == null && options.DotNetTransaction != null)
            {
                var connection = tenant.CreateConnection();
                connection.Open();


                options.Connection = connection;
            }

            if (options.DotNetTransaction != null)
            {
                options.Connection.EnlistTransaction(options.DotNetTransaction);
                options.OwnsTransactionLifecycle = false;
            }
#endif

            if (options.Connection == null)
            {
                return(tenant.OpenConnection(commandRunnerMode, options.IsolationLevel, options.Timeout));
            }
            else
            {
                return(new ManagedConnection(options, commandRunnerMode));
            }
        }
Exemplo n.º 11
0
        private async Task clearExistingState(CancellationToken token)
        {
            _logger.ClearingExistingState(this);
            var types = _projection.ProjectedTypes();

            using (var conn = _tenant.OpenConnection())
            {
                foreach (var type in types)
                {
                    var tableName = _tenant.MappingFor(type).TableName;
                    var sql       =
                        $"delete from {_store.Events.DatabaseSchemaName}.mt_event_progression where name = :name;truncate {tableName} cascade";

                    var cmd = new NpgsqlCommand(sql).With("name", _projection.GetEventProgressionName(type));

                    await conn.ExecuteAsync(cmd, token);
                }
            }
            LastEncountered = 0;
        }
Exemplo n.º 12
0
        private async Task clearExistingState(CancellationToken token)
        {
            _logger.ClearingExistingState(this);

            var tableName = _tenant.MappingFor(_projection.Produces).Table;
            var sql =
                $"delete from {_store.Events.DatabaseSchemaName}.mt_event_progression where name = :name;truncate {tableName} cascade";

            using (var conn = _tenant.OpenConnection())
            {
                await conn.ExecuteAsync(async (cmd, tkn) =>
                {
                    await cmd.Sql(sql)
                        .With("name", _projection.Produces.FullName)
                        .ExecuteNonQueryAsync(tkn)
                        .ConfigureAwait(false);
                }, token).ConfigureAwait(false);
            }

            LastEncountered = 0;
        }
 public ProjectionDocumentSession(DocumentStore store, ITenant tenant, ISessionWorkTracker workTracker) : base(
         store, new SessionOptions {
     Tracking = DocumentTracking.None
 }, tenant.OpenConnection(), tenant, workTracker)
 {
 }
Exemplo n.º 14
0
 public IManagedConnection OpenConnection(CommandRunnerMode mode        = CommandRunnerMode.AutoCommit,
                                          IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, int?timeout = null)
 {
     return(_inner.OpenConnection(mode, isolationLevel, timeout));
 }