예제 #1
0
        public void PgSql()
        {
            PgSqlCommandExecutor.Configure();

            using (var connection = new Npgsql.NpgsqlConnection("host=localhost;username=postgres;password=Norther;database=Test"))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {

                    var testData = new TestTable() {
                        Foo = "Foo",
                        Name = "Hello",
                        TheDate = DateTime.Now
                    };

                    TestTable result;

                    using (Time(elapsed => Console.WriteLine("Insert : " + elapsed)))
                    {
                        result = connection.Insert(testData, transaction);
                    }

                    Console.WriteLine("Database generated UID : " + result.TheSecondId);

                    List<TestTable> bulkResult;

                    using (Time(elapsed => Console.WriteLine("Bulk insert : " + elapsed)))
                    {
                        bulkResult = connection.Insert(Enumerable.Range(0, 100).Select(i => new TestTable() { Name = i.ToString() }), transaction).ToList();

                        Assert.AreEqual(100, bulkResult.Count());
                    }

                    using (Time(elapsed => Console.WriteLine("Bulk update : " + elapsed)))
                    {
                        bulkResult = connection.Update(
                            bulkResult.Select(item => item.SetProperties(new TestTable() { Name = item.Name + " strongly typed mutation" })),
                            transaction).ToList();
                    }

                    using (Time(elapsed => Console.WriteLine("Update : " + elapsed)))
                    {
                        var newDate = DateTime.Now.AddYears(-50);
                        var update = connection.Update(
                                result.SetProperties(
                                    new { TheDate = newDate }
                                ),
                                transaction
                            );
                        Assert.AreEqual(newDate.Year, update.TheDate.Year);
                    }

                    using (Time(elapsed => Console.WriteLine("FindById : " + elapsed)))
                    {
                        Assert.IsNotNull(connection.FindById<TestTable>(new { result.Id, result.TheSecondId }, transaction));
                    }

                    transaction.Commit();
                }

            }
        }