public async Task <IAsyncCommandChannel> CreateAsync(CreationParameters <MemorySqliteConnectionStringBuilder> creationParameters)
        {
            var mustPlayCreationScripts = true;
            var connection = Instances.AddOrUpdate(
                creationParameters.ConnectionString.MemoryInstanceGuid,
                guid => new SqliteConnection(creationParameters.ConnectionString.ConnectionString),
                (guid, previous) =>
            {
                mustPlayCreationScripts = creationParameters.EraseIfExists;
                return(creationParameters.EraseIfExists ? new SqliteConnection(creationParameters.ConnectionString.ConnectionString) : previous);
            });

            var channel = new AsyncCommandChannel(new AsyncMemoryConnection(connection));

            if (mustPlayCreationScripts)
            {
                await channel.ExecuteAsync(creationParameters.Script);

                foreach (var creationParametersAdditionalScript in creationParameters.AdditionalScripts)
                {
                    await channel.ExecuteAsync(creationParametersAdditionalScript);
                }
            }

            return(channel);
        }
예제 #2
0
        public async Task <IAsyncCommandChannel> CreateAsync(CreationParameters <SqliteConnectionStringBuilder> creationParameters)
        {
            if (creationParameters.ConnectionString.DataSource == ":memory:")
            {
                throw new NotSupportedException("Use " + nameof(MemorySqliteCommandChannelFactory));
            }

            CreationOrDeletionLock.EnterUpgradeableReadLock();

            try
            {
                if (creationParameters.EraseIfExists)
                {
                    DeleteIfExists(creationParameters.ConnectionString);
                }

                CreationOrDeletionLock.EnterWriteLock();

                try
                {
                    var derivedConnectionString = new SqliteConnectionStringBuilder(creationParameters.ConnectionString.ToString())
                    {
                        Mode = SqliteOpenMode.ReadWriteCreate
                    };

                    var connection = new SqliteConnection(derivedConnectionString.ToString());
                    connection.Open();
                    var adapter = new AsyncCommandChannel(
                        new AsyncThreadSafeConnection(
                            new AsyncConnection(
                                new SqliteConnection(creationParameters.ConnectionString.ToString())
                                )
                            )
                        );

                    foreach (var script in creationParameters.AdditionalScripts.Prepend(creationParameters.Script))
                    {
                        if (!string.IsNullOrWhiteSpace(script))
                        {
                            await adapter.ExecuteAsync(script);
                        }
                    }

                    return(adapter);
                }
                finally
                {
                    CreationOrDeletionLock.ExitWriteLock();
                }
            }
            finally
            {
                CreationOrDeletionLock.ExitUpgradeableReadLock();
            }
        }