Beispiel #1
0
        public void TestTransactionAutoRollback()
        {
            DoQuery("CREATE TEMPORARY TABLE Test (value int)");

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var getNumValues = qm.BuildQuery("SELECT COUNT(value) FROM test");

                    var addValue = qm.BuildQuery("INSERT INTO test (value) VALUES (?)");

                    var f = scheduler.Start(CrashyTransactionTask(qm, addValue));
                    try {
                        scheduler.WaitFor(f);
                        Assert.Fail("Did not throw");
                    } catch (FutureException fe) {
                        Exception inner = fe.InnerException;
                        Assert.IsInstanceOfType(typeof(InvalidOperationException), inner);
                    }

                    var  fgnv      = getNumValues.ExecuteScalar();
                    long numValues = Convert.ToInt64(
                        scheduler.WaitFor(fgnv)
                        );
                    Assert.AreEqual(0, numValues);
                }
        }
Beispiel #2
0
        public void TestDisposal()
        {
            DoQuery("DROP TABLE IF EXISTS Test");
            DoQuery("CREATE TABLE Test (value int)");
            for (int i = 0; i < 10; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            TaskEnumerator <IDataRecord> iter;
            IFuture f;

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q  = qm.BuildQuery("SELECT * FROM Test");
                    var q2 = qm.BuildQuery("SELECT COUNT(*) FROM Test");
                    iter = q.Execute();
                    scheduler.Start(iter.Fetch());
                    f = q2.ExecuteScalar();
                }

            iter.Dispose();

            try {
                int count = (int)f.Result;
                Assert.Fail("Future's result was not a ConnectionDisposedException");
            } catch (FutureException fe) {
                Assert.IsInstanceOfType(typeof(ConnectionDisposedException), fe.InnerException);
            }
        }
Beispiel #3
0
        public void TestTransactionPipelining()
        {
            DoQuery("CREATE TEMPORARY TABLE Test (value int)");

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var getNumValues = qm.BuildQuery("SELECT COUNT(value) FROM test");

                    var addValue = qm.BuildQuery("INSERT INTO test (value) VALUES (?)");

                    var t  = qm.CreateTransaction();
                    var fq = addValue.ExecuteNonQuery(1);
                    var fr = t.Rollback();

                    scheduler.WaitFor(Future.WaitForAll(t.Future, fq, fr));

                    var  fgnv      = getNumValues.ExecuteScalar();
                    long numValues = Convert.ToInt64(
                        scheduler.WaitFor(fgnv)
                        );
                    Assert.AreEqual(0, numValues);

                    t  = qm.CreateTransaction();
                    fq = addValue.ExecuteNonQuery(1);
                    var fc = t.Commit();

                    scheduler.WaitFor(Future.WaitForAll(t.Future, fq, fc));

                    fgnv      = getNumValues.ExecuteScalar();
                    numValues = Convert.ToInt64(
                        scheduler.WaitFor(fgnv)
                        );
                    Assert.AreEqual(1, numValues);
                }
        }
Beispiel #4
0
 public DatabasePropertySerializer(
     ConnectionWrapper database, string tableName,
     string nameColumn, string valueColumn,
     Func <IBoundMember, string> getMemberName
     ) : this(
         database.BuildQuery(String.Format("REPLACE INTO {0} ({1}, {2}) VALUES (?, ?)", tableName, nameColumn, valueColumn)),
         database.BuildQuery(String.Format("SELECT {2} FROM {0} WHERE {1} = ? LIMIT 1", tableName, nameColumn, valueColumn)),
         getMemberName
         )
 {
 }
Beispiel #5
0
        public void TestConnectionWrapper()
        {
            DoQuery("CREATE TEMPORARY TABLE Test (value int)");
            for (int i = 0; i < 100; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q = qm.BuildQuery("SELECT COUNT(value) FROM Test WHERE value = ?");

                    var f      = q.ExecuteScalar(5);
                    var result = scheduler.WaitFor(f);

                    Assert.AreEqual(result, 1);

                    q = qm.BuildQuery("SELECT @p0 - @p1");

                    f      = q.ExecuteScalar(2, 3);
                    result = scheduler.WaitFor(f);

                    Assert.AreEqual(result, -1);

                    f = q.ExecuteScalar(new NamedParam {
                        N = "p0", V = 4
                    }, new NamedParam {
                        N = "p1", V = 3
                    });
                    result = scheduler.WaitFor(f);

                    Assert.AreEqual(result, 1);

                    f = q.ExecuteScalar(5, new NamedParam {
                        N = "p1", V = 3
                    });
                    result = scheduler.WaitFor(f);

                    Assert.AreEqual(result, 2);

                    q = qm.BuildQuery("SELECT @parm1-@parm2");

                    f = q.ExecuteScalar(new NamedParam {
                        N = "parm1", V = 1
                    }, new NamedParam {
                        N = "parm2", V = 2
                    });
                    result = scheduler.WaitFor(f);

                    Assert.AreEqual(result, -1);
                }
        }
Beispiel #6
0
        public void InsertBenchmark(int warmupLength, int numInsertions, Func <IJobQueue> jobQueueFactory, Func <ConnectionWrapper, Query, int, IEnumerator <object> > insertTask)
        {
            DoQuery("CREATE TABLE Test (A INTEGER NOT NULL, B INTEGER NOT NULL)");

            var jobQueue = jobQueueFactory();
            var wjq      = jobQueue as WindowsMessageJobQueue;

            using (var scheduler = new TaskScheduler(() => jobQueue))
                using (var cw = new ConnectionWrapper(scheduler, Connection))
                    using (var query = cw.BuildQuery("INSERT INTO Test (A, B) VALUES (?, ?)")) {
                        scheduler.WaitFor(insertTask(cw, query, warmupLength));
                        DoQuery("DELETE FROM Test");

                        var  e         = insertTask(cw, query, numInsertions);
                        long timeStart = Time.Ticks;

                        scheduler.WaitFor(e);

                        long elapsed        = Time.Ticks - timeStart;
                        var  elapsedSeconds = (decimal)elapsed / Time.SecondInTicks;

                        Console.WriteLine(
                            "Inserted {0} row(s) in {1:00.000} second(s) at ~{2:000000.00} rows/sec.",
                            numInsertions, elapsedSeconds, numInsertions / elapsedSeconds
                            );
                    }
        }
Beispiel #7
0
        public void TestClonePipelining()
        {
            DoQuery("DROP TABLE IF EXISTS Test");
            DoQuery("CREATE TABLE Test (value int)");
            for (int i = 0; i < 10; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q      = qm.BuildQuery("SELECT * FROM Test");
                    var iter   = q.Execute();
                    var iterF  = scheduler.Start(iter.Fetch());
                    var fClone = qm.Clone();

                    Assert.IsFalse(fClone.Completed);

                    iter.Dispose();
                    iterF.Dispose();
                    scheduler.WaitFor(fClone);
                    using (var dupe = fClone.Result) {
                        q = dupe.BuildQuery("SELECT COUNT(value) FROM Test WHERE value = ?");
                        var f      = q.ExecuteScalar(5);
                        var result = scheduler.WaitFor(f);
                        Assert.AreEqual(result, 1);
                    }
                }
        }
Beispiel #8
0
        public void TestImplicitMappingClass()
        {
            Scheduler.WaitFor(Wrapper.ExecuteSQL("CREATE TEMPORARY TABLE Test (a int, b int)"));

            int rowCount = 100;

            using (var q = Wrapper.BuildQuery("INSERT INTO TEST (a, b) VALUES (?,?)"))
                for (int i = 0; i < rowCount; i++)
                {
                    Scheduler.WaitFor(q.ExecuteNonQuery(i, i * 2));
                }

            using (var q = Wrapper.BuildQuery("SELECT a, b FROM Test"))
                using (var e = q.Execute <ImplicitlyMappedClass>()) {
                    var items = (ImplicitlyMappedClass[])Scheduler.WaitFor(e.GetArray());

                    for (int i = 0; i < rowCount; i++)
                    {
                        Assert.AreEqual(i, items[i].A);
                        Assert.AreEqual(i * 2, items[i].B);
                    }

                    Assert.AreEqual(rowCount, items.Length);
                }
        }
Beispiel #9
0
        public void TestQueryPipelining()
        {
            DoQuery("CREATE TEMPORARY TABLE Test (value int)");
            for (int i = 0; i < 100; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q1 = qm.BuildQuery("SELECT value FROM test");
                    var q2 = qm.BuildQuery("INSERT INTO test (value) VALUES (?)");

                    var iterator = q1.Execute();
                    var f1       = scheduler.Start(iterator.Fetch());
                    var f2       = q2.ExecuteNonQuery(200);

                    f1.RegisterOnComplete((f) => {
                        Assert.IsNull(f.Error);
                        Assert.AreEqual(f1, f);
                        Assert.AreEqual(true, f.Result);
                        Assert.IsTrue(f1.Completed);
                        Assert.IsFalse(f2.Completed);
                    });

                    f2.RegisterOnComplete((f) => {
                        Assert.IsNull(f.Error);
                        Assert.AreEqual(f2, f);
                        Assert.IsTrue(f1.Completed);
                        Assert.IsTrue(f2.Completed);
                    });

                    scheduler.WaitFor(f1);

                    scheduler.WaitFor(scheduler.Start(new Sleep(1.0)));
                    Assert.IsFalse(f2.Completed);

                    iterator.Dispose();

                    scheduler.WaitFor(f2);
                }
        }
Beispiel #10
0
        public void TestQueryParameters()
        {
            using (var scheduler = new TaskScheduler())
                using (var wrapper = new ConnectionWrapper(scheduler, Connection)) {
                    scheduler.WaitFor(wrapper.ExecuteSQL("CREATE TEMPORARY TABLE Test (a INTEGER, b VARIANT)"));

                    using (var q = wrapper.BuildQuery("INSERT INTO Test (a, b) VALUES (?, ?)")) {
                        q.Parameters[1].DbType = DbType.Object;
                        Assert.AreEqual(DbType.Object, q.Parameters[1].DbType);
                    }
                }
        }
Beispiel #11
0
        public void TestGetResultAsObject()
        {
            DoQuery("DROP TABLE IF EXISTS Test");
            DoQuery("CREATE TABLE Test (value int)");
            for (int i = 0; i < 10; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q = qm.BuildQuery("SELECT * FROM Test");
                    var f = q.ExecuteArray <object>();

                    Assert.IsFalse(f.Completed);
                    var seq = scheduler.WaitFor(f);

                    Assert.AreEqual(
                        new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, seq
                        );
                }
        }
Beispiel #12
0
        public void TestDbTaskIterator()
        {
            DoQuery("CREATE TEMPORARY TABLE Test (value int)");
            for (int i = 0; i < 100; i++)
            {
                DoQuery(String.Format("INSERT INTO Test (value) VALUES ({0})", i));
            }

            using (var scheduler = new TaskScheduler())
                using (var qm = new ConnectionWrapper(scheduler, Connection)) {
                    var q = qm.BuildQuery("SELECT value FROM Test WHERE value = ?");

                    using (var iterator = q.Execute(5)) {
                        scheduler.WaitFor(iterator.Fetch());

                        using (var e = iterator.CurrentItems) {
                            Assert.IsTrue(e.MoveNext());
                            Assert.AreEqual(e.Current.GetInt32(0), 5);
                        }
                    }
                }
        }