Esempio n. 1
0
        public void ExecuteBuildsAndExecutesCommandNotInTransaction()
        {
            var result = 1;

            var mockSqlDialect = new Mock <ISqlDialect>();

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(result);

            var mockConnection = new Mock <IDbConnection>();

            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new SessionListeners());

            Assert.Equal(result, session.ExecuteAsync(new SqlQuery("")).Result);

            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
Esempio n. 2
0
        public async IAsyncEnumerable <QueryReader> EnumerateAsync(IEnumerable <QueryData> queries, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            if (queries == null)
            {
                throw new ArgumentNullException(nameof(queries));
            }

            if (this.Options.Schemas == null)
            {
                throw new InvalidOperationException("No schema builder found.");
            }

            await using AsyncSession connection = new AsyncSession(this.Options);

            foreach (QueryData queryData in queries.NotNull())
            {
                Query query = new Query(queryData);

                if (string.IsNullOrWhiteSpace(queryData.QueryText))
                {
                    continue;
                }

                await foreach (DbDataReader dataReader in connection.ExecuteAsync(query, cancellationToken).ConfigureAwait(false))
                {
                    yield return(new QueryReader(dataReader, this.Options.Schemas));
                }
            }
        }
Esempio n. 3
0
        public async Task <IList <TItem> > ListAsync <TItem>(IEnumerable <QueryData> queries, CancellationToken cancellationToken = default)
        {
            if (queries == null)
            {
                throw new ArgumentNullException(nameof(queries));
            }

            if (this.Options.Schemas == null)
            {
                throw new InvalidOperationException("No schema builder found.");
            }

            ResultAdapter <TItem> adapter = new ResultAdapter <TItem>(this.Options.Schemas);

            await using AsyncSession connection = new AsyncSession(this.Options);

            foreach (QueryData queryData in queries.NotNull())
            {
                Query builder = new Query(queryData);

                if (string.IsNullOrWhiteSpace(queryData.QueryText))
                {
                    continue;
                }

                await foreach (DbDataReader dataReader in connection.ExecuteAsync(builder, cancellationToken).ConfigureAwait(false))
                {
                    await adapter.AddResultAsync(dataReader, cancellationToken).ConfigureAwait(false);
                }
            }

            return(adapter.ToList());
        }
Esempio n. 4
0
        public async Task ExecuteAsync(IEnumerable <CommandData> commands, CancellationToken cancellationToken = default)
        {
            FieldMap fieldMap = new FieldMap();

            await using AsyncSession session = new AsyncSession(this.Options);

            foreach (CommandData commandData in commands.NotNull())
            {
                Command command = new Command(commandData, fieldMap);

                if (string.IsNullOrWhiteSpace(commandData.CommandText))
                {
                    continue;
                }

                await foreach (DbDataReader dataReader in session.ExecuteAsync(command, cancellationToken).ConfigureAwait(false))
                {
                    TableIdentity      tableInfo  = TableIdentity.FromRecord(dataReader);
                    FieldData[]        fields     = command.GetHeading(tableInfo);
                    MetadataIdentity[] attributes = fields.Select(f => f?.Attribute).ToArray();

                    var binder = FuncCache.GetFieldDataBinder(attributes, tableInfo);

                    if (await dataReader.ReadAsync().ConfigureAwait(false))
                    {
                        binder(dataReader, fields);
                    }
                }
            }

            foreach (FieldData fieldData in fieldMap)
            {
                fieldData.Bind();
            }
        }
Esempio n. 5
0
        public void ExecuteThrowsArgumentNullExceptionForNullSqlQuery()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new SessionListeners());

            var exception = Assert.Throws <AggregateException>(
                () => session.ExecuteAsync(null).Result);

            Assert.Equal("sqlQuery", ((ArgumentNullException)exception.InnerException).ParamName);
        }
Esempio n. 6
0
        public void ExecuteThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new SessionListeners());

            using (session)
            {
            }

            var exception = Assert.Throws <AggregateException>(
                () => session.ExecuteAsync(new SqlQuery("SELECT")).Result);

            Assert.IsType <ObjectDisposedException>(exception.InnerException);
        }
Esempio n. 7
0
        public async Task Test_ConnectionManagement_WithAsyncSession()
        {
            var connection = new SqliteConnection(DatabaseHelper.TestDbConnectionString);

            QueryOptions options = new QueryOptions()
            {
                ConnectionFactory = () => connection,
                Schemas           = DatabaseHelper.Default.Schemas,
            };

            await using (var ado = new AsyncSession(options))
            {
                await foreach (var r in ado.ExecuteAsync(new SqlOperation("SELECT 12; SELECT 12"), CancellationToken.None))
                {
                    (await r.ReadAsync()).ShouldBeTrue();
                    (await r.GetFieldValueAsync <int>(0)).ShouldBe(12);
                    (await r.ReadAsync()).ShouldBeFalse();
                }
            }

            connection.State.ShouldBe(ConnectionState.Closed);
        }
        public void ExecuteThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            using (session)
            {
            }

            var exception = Assert.Throws<AggregateException>(
                () => session.ExecuteAsync(new SqlQuery("SELECT")).Result);

            Assert.IsType<ObjectDisposedException>(exception.InnerException);
        }
        public void ExecuteThrowsArgumentNullExceptionForNullSqlQuery()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<AggregateException>(
                () => session.ExecuteAsync(null).Result);

            Assert.Equal("sqlQuery", ((ArgumentNullException)exception.InnerException).ParamName);
        }
        public void ExecuteBuildsAndExecutesCommandNotInTransaction()
        {
            var result = 1;

            var mockSqlDialect = new Mock<ISqlDialect>();

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(result);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            Assert.Equal(result, session.ExecuteAsync(new SqlQuery("")).Result);

            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }