コード例 #1
0
        public async Task ContextualizedQueryBuilderTestAsync()
        {
            var queryDispatcher =
                new QueryDispatcher(
                    new QueryExecutor(
                        new CommandPreparer(),
                        new ConnectionFactory(ConnectionString.Default)));

            var setupQueries = new String[]
            {
                "DROP TABLE IF EXISTS contextualized_query_builder_query_test;",
                "CREATE TABLE contextualized_query_builder_query_test (id INT NOT NULL, value TEXT NOT NULL);",
                "INSERT INTO contextualized_query_builder_query_test VALUES (1, 'James');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (2, 'Frank');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (3, 'Hopkins');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (4, 'Greenfield');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (5, 'Quarles');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (6, 'James');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (7, 'McIlvaine');",
                "INSERT INTO contextualized_query_builder_query_test VALUES (8, 'Riley');",
            };

            foreach (var setupQuery in setupQueries)
            {
                await queryDispatcher.ExecuteAsync(new Query(setupQuery));
            }

            var readerCollection = new ReaderCollection(new[]
            {
                Reader.Create(new TestItemReader())
            });

            var contextualizedQueryBuilder = new ContextualizedQueryBuilder(queryDispatcher, readerCollection);

            var item =
                await contextualizedQueryBuilder
                .SetQuery(@"
						SELECT id, value
						FROM contextualized_query_builder_query_test
						WHERE value = :value;"                        )
                .SetParameter("value", "Hopkins")
                .Build()
                .FirstAsync <TestItem>();

            Assert.NotNull(item);
            Assert.Equal(item.Id, 3);
            Assert.Equal(item.Value, "Hopkins");
        }
コード例 #2
0
        public async Task ReaderTestAsync()
        {
            var queryDispatcher =
                new QueryDispatcher(
                    new QueryExecutor(
                        new CommandPreparer(),
                        new ConnectionFactory(ConnectionString.Default)));

            var setupQueries = new String[]
            {
                "DROP TABLE IF EXISTS reader_test;",
                "CREATE TABLE reader_test (id INT NOT NULL, first_name TEXT NOT NULL, middle_name TEXT, last_name TEXT NOT NULL);",
                "INSERT INTO reader_test VALUES (417, 'Brendan', 'James', 'Baker');"
            };

            foreach (var setupQuery in setupQueries)
            {
                await queryDispatcher.ExecuteAsync(new Query(setupQuery));
            }

            var readerCollection = new ReaderCollection(new[]
            {
                Reader.Create(new PersonReader())
            });

            var contextualizedQueryBuilder = new ContextualizedQueryBuilder(queryDispatcher, readerCollection);

            var item =
                await contextualizedQueryBuilder
                .SetQuery(@"
						SELECT id, first_name, middle_name, last_name
						FROM reader_test
						WHERE last_name = :last_name;"                        )
                .SetParameter("last_name", "Baker")
                .Build()
                .SingleAsync <Person>();

            Assert.NotNull(item);
            Assert.Equal(item.Id, 417);
            Assert.Equal(item.FirstName, "Brendan");
            Assert.Equal(item.MiddleName, "James");
            Assert.Equal(item.LastName, "Baker");
        }