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); } } }
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); } }
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); } }
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)); } }
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); } }
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); } }
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); } }
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); } }
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)); } }
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); } }
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); } }
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); } }
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)); } }
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()); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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)); } }
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(); } }
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); } }
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)); } }
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)); } }