Ejemplo n.º 1
0
        public void Enumerate_Reset()
        {
            P <int> p = default;
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values(@x)", p.C))
                    using (var select = tbl.Stmt("select x from t where x=@x", r.C, p.C))
                    {
                        p.Value = 1;
                        insert.Bind(p).Execute();
                        p.Value = 2;
                        insert.Bind(p).Execute();
                        p.Value = 1;
                        using (var enumerator = select.Bind(p).Execute().GetEnumerator())
                        {
                            Assert.True(enumerator.MoveNext());
                            enumerator.Current.AssignTo(out r);
                            Assert.Equal(1, r.Value);

                            enumerator.Reset();
                            Assert.True(enumerator.MoveNext());
                            enumerator.Current.AssignTo(out r);
                            Assert.Equal(1, r.Value);
                        }
                    }
        }
Ejemplo n.º 2
0
        public void Execute_Bind_Rebind()
        {
            P <int, string> p = default;
            R <int, string> r = default;

            using (var tbl = new TestTable("create table t (x int, y text)"))
                using (var insert = tbl.Stmt("insert into t values (@x, @y)", p.C))
                    using (var select = tbl.Stmt("select x, y from t", r.C))
                    {
                        p.Value1 = 1;
                        p.Value2 = "1";
                        insert.Bind(p).Execute();
                        p.Value1 = 2;
                        p.Value2 = "2";
                        insert.Bind(p).Execute();
                        int sum = 0;
                        foreach (var row in select.Execute())
                        {
                            row.AssignTo(out r);
                            sum += r.Value1;
                            Assert.Equal(r.Value1.ToString(), r.Value2);
                        }
                        Assert.Equal(3, sum);
                    }
        }
Ejemplo n.º 3
0
        public void SelectMany_Twice()
        {
            P <int> p = default;
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values(@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = 1;
                        insert.Bind(p).Execute();
                        p.Value = 2;
                        insert.Bind(p).Execute();
                        int sum  = 0;
                        var rows = select.Execute();
                        foreach (var row in rows)
                        {
                            row.AssignTo(out r);
                            sum += r.Value;
                        }
                        foreach (var row in rows)
                        {
                            row.AssignTo(out r);
                            sum += r.Value;
                        }
                        Assert.Equal(6, sum);
                    }
        }
Ejemplo n.º 4
0
        public void To_Missing()
        {
            R <Struct> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (1)"))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        insert.Execute();
                        Assert.Throws <AssignmentException>(() => select.Execute(out r));
                    }
        }
Ejemplo n.º 5
0
 public void Statement2_Execute_ResultNull()
 {
     using (var tbl = new TestTable("create table t (x text)"))
         using (var insert = tbl.Stmt("insert into t values ('x')"))
             using (var stmt = tbl.Stmt <string?, string?>("select null from t where x=@x"))
             {
                 insert.Execute();
                 string?s = null;
                 stmt.Bind("x");
                 Assert.True(stmt.Execute(out s));
                 Assert.Null(s);
             }
 }
Ejemplo n.º 6
0
        public void Struct_Null()
        {
            R <Struct?> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (null)"))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        insert.Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Null(r.Value);
                    }
        }
Ejemplo n.º 7
0
        public void SelectOne()
        {
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values(1)"))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        insert.Execute();
                        select.Execute(out r);
                        Assert.Equal(1, r.Value);
                    }
        }
Ejemplo n.º 8
0
        public void Scalar_FromString()
        {
            R <string> r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt <string>("insert into t values (@x)"))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        string value = "hello";
                        insert.Bind(value).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(value, r.Value);
                    }
        }
Ejemplo n.º 9
0
        public void Guid_Text_Invalid()
        {
            P <string> p = default;
            R <Guid>   r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = "not a guid";
                        insert.Bind(p).Execute();
                        Assert.Throws <AssignmentException>(() => select.Execute(out r));
                    }
        }
Ejemplo n.º 10
0
        public void GuidNull_Text_Null()
        {
            P <string> p = default;
            R <Guid?>  r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Null(r.Value);
                    }
        }
Ejemplo n.º 11
0
        public void Scalar_FromInt()
        {
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt <int>("insert into t values (@x)"))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        int value = 5;
                        insert.Bind(value).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(value, r.Value);
                    }
        }
Ejemplo n.º 12
0
        public void DateTimeOffsetNull_Null()
        {
            P <DateTimeOffset?> p = default;
            R <DateTimeOffset?> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Null(r.Value);
                    }
        }
Ejemplo n.º 13
0
        public void To_Fails()
        {
            R <int> r    = default;
            var     conv = ResultConverter.Builder <R <int> >()
                           .With(re => re.Value, (long v) => throw new Exception("throw"))
                           .Compile();

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values(1)"))
                    using (var select = tbl.Stmt("select x from t", conv))
                    {
                        insert.Execute();
                        Assert.Throws <AssignmentException>(() => select.Execute(out r));
                    }
        }
Ejemplo n.º 14
0
        public void MemoryChar(string value)
        {
            P <ReadOnlyMemory <char> > p = default;
            R <ReadOnlyMemory <char> > r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = value.AsMemory();
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(value.AsMemory().ToString(), r.Value.ToString());
                    }
        }
Ejemplo n.º 15
0
        public void Enum()
        {
            P <E> p = default;
            R <E> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = E.Value;
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(E.Value, r.Value);
                    }
        }
Ejemplo n.º 16
0
        public void Float_Null(float?value)
        {
            P <float?> p = default;
            R <float?> r = default;

            using (var tbl = new TestTable("create table t (x float)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = value;
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(value, r.Value);
                    }
        }
Ejemplo n.º 17
0
        public void Result_Ignore()
        {
            R <string, int> r    = default;
            var             conv = ResultConverter.Builder <R <string, int> >()
                                   .Ignore(re => re.Value1)
                                   .Compile();

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (1)"))
                    using (var select = tbl.Stmt("select x from t", conv))
                    {
                        insert.Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(1, r.Value2);
                    }
        }
Ejemplo n.º 18
0
        public void DateTimeOffset_Integer_Value()
        {
            P <DateTimeOffset> p = default;
            R <DateTimeOffset> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        var dt = DateTimeOffset.Now;
                        p.Value = dt;
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(dt, r.Value);
                    }
        }
Ejemplo n.º 19
0
        public void Guid_Text(string format)
        {
            P <string> p = default;
            R <Guid>   r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        Guid g = Guid.NewGuid();
                        p.Value = g.ToString(format);
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(g, r.Value);
                    }
        }
Ejemplo n.º 20
0
        public void TimeSpanNull_Text_Value()
        {
            P <string>    p = default;
            R <TimeSpan?> r = default;

            using (var tbl = new TestTable("create table t (x text)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        TimeSpan t = TimeSpan.FromMinutes(42);
                        p.Value = t.ToString();
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(t, r.Value);
                    }
        }
Ejemplo n.º 21
0
        public void Execute_Bind_Multiple()
        {
            P <int, string> p = default;
            R <int, string> r = default;

            using (var tbl = new TestTable("create table t (x int, y text)"))
                using (var insert = tbl.Stmt("insert into t values (@x, @y)", p.C))
                    using (var select = tbl.Stmt("select x, y from t", r.C))
                    {
                        p.Value1 = 1;
                        p.Value2 = "one";
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(1, r.Value1);
                        Assert.Equal("one", r.Value2);
                    }
        }
Ejemplo n.º 22
0
        public void Rebind()
        {
            P <int> p = default;
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values(@x)", p.C))
                    using (var sum = tbl.Stmt("select sum(x) from t", r.C))
                    {
                        p.Value = 1;
                        insert.Bind(p).Execute();
                        p.Value = 2;
                        insert.Bind(p).Execute();
                        sum.Execute(out r);
                        Assert.Equal(3, r.Value);
                    }
        }
Ejemplo n.º 23
0
 public void Statement2_Bind_ParamNull()
 {
     using (var tbl = new TestTable("create table t (x text)"))
         using (var stmt = tbl.Stmt <string, string?>("select x from t where x=@x"))
         {
             stmt.Bind(null);
         }
 }
Ejemplo n.º 24
0
 public void Statement1_Bind_ParamsNull()
 {
     using (var tbl = new TestTable("create table t (x text)"))
         using (var stmt = tbl.Stmt <string?>("insert into t values (@x)"))
         {
             stmt.Bind(null);
         }
 }
Ejemplo n.º 25
0
        public void Custom_FromInt()
        {
            P <int> p    = default;
            R <int> r    = default;
            var     conv = ResultConverter.Builder <R <int> >()
                           .With(re => re.Value, (long v) => - 1 * (int)v).Compile();

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                    using (var select = tbl.Stmt("select x from t", conv))
                    {
                        p.Value = 5;
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(-1 * p.Value, r.Value);
                    }
        }
Ejemplo n.º 26
0
 public void Disposed_Bind()
 {
     using (var tbl = new TestTable("create table t (x)"))
     {
         var insert = tbl.Stmt <int>("insert into t values (@x)");
         insert.Dispose();
         Assert.Throws <ObjectDisposedException>(() => insert.Bind(1));
     }
 }
Ejemplo n.º 27
0
 public void Statement2_DisposedTwice()
 {
     using (var tbl = new TestTable("create table t (x int)"))
     {
         var stmt = tbl.Stmt <int, int>("select x from t where x=@x");
         stmt.Dispose();
         stmt.Dispose();
     }
 }
Ejemplo n.º 28
0
        public void Custom_ToInt()
        {
            P <int> p    = default;
            var     conv = ParameterConverter.Builder <P <int> >()
                           .With(pa => pa.Value, v => - 1 * v)
                           .Ignore(pa => pa.C).Compile();
            R <int> r = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", conv))
                    using (var select = tbl.Stmt("select x from t", r.C))
                    {
                        p.Value = 5;
                        insert.Bind(p).Execute();
                        Assert.True(select.Execute(out r));
                        Assert.Equal(-1 * p.Value, r.Value);
                    }
        }
Ejemplo n.º 29
0
 public void Statement1_Bind_Disposed()
 {
     using (var tbl = new TestTable("create table t (x int)"))
     {
         var stmt = tbl.Stmt <int>("insert into t values (@x)");
         stmt.Dispose();
         Assert.Throws <ObjectDisposedException>(
             () => stmt.Bind(1));
     }
 }
Ejemplo n.º 30
0
        public void From_Missing()
        {
            P <Struct> p = default;

            using (var tbl = new TestTable("create table t (x int)"))
                using (var insert = tbl.Stmt("insert into t values (@x)", p.C))
                {
                    Assert.Throws <BindingException>(() => insert.Bind(p));
                }
        }