public void Disposed_Execute() { using (var tbl = new TestTable("create table t (x)")) { var select = tbl.RStmt <int>("select x from t"); select.Dispose(); Assert.Throws <ObjectDisposedException>(() => select.Execute()); } }
public void Class_NoDefaultConstructor_Throws() { using (var tbl = new TestTable("create table t (x int)")) using (var insert = tbl.Stmt("insert into t values (5)")) using (var select = tbl.RStmt <ClassWithConstructor <int, string> >("select x from t")) { insert.Execute(); Assert.Throws <ArgumentException>(() => select.Execute(out _)); } }
public void Class_Default_Constructor() { using (var tbl = new TestTable("create table t (x int)")) using (var insert = tbl.Stmt("insert into t values (5)")) using (var select = tbl.RStmt <Class <int> >("select x from t")) { insert.Execute(); Assert.True(select.Execute(out var r)); Assert.Equal(expected: 5, r.Value); } }
public void Statement1R_Execute_Disposed() { using (var tbl = new TestTable("create table t (x text)")) { var select = tbl.RStmt <string?>("select x from t"); select.Dispose(); string?s = null; Assert.Throws <ObjectDisposedException>( () => select.Execute(out s)); } }
public void Statement1R_Execute_ConverterNull() { using (var tbl = new TestTable("create table t (x int)")) using (var select = tbl.RStmt <int>("select x from t")) { ResultConverter <string> r = null !; string?s = null; Assert.Throws <ArgumentNullException>( () => select.Execute(r, out s)); } }
public void GuidNull_Null() { using (var tbl = new TestTable("create table t (x text)")) using (var insert = tbl.Stmt <Guid?>("insert into t values (@x)")) using (var select = tbl.RStmt <Guid?>("select x from t")) { insert.Bind(null).Execute(); Guid?r = Guid.NewGuid(); Assert.True(select.Execute(out r)); Assert.Null(r); } }
public void Statement1R_Execute_ResultNull() { using (var tbl = new TestTable("create table t (x text)")) using (var insert = tbl.Stmt("insert into t values (null)")) using (var select = tbl.RStmt <string?>("select x from t")) { insert.Execute(); string?s = null; Assert.True(select.Execute(out s)); Assert.Null(s); } }
public void GuidNull_Value() { using (var tbl = new TestTable("create table t (x text)")) using (var insert = tbl.Stmt <Guid?>("insert into t values (@x)")) using (var select = tbl.RStmt <Guid?>("select x from t")) { Guid g = Guid.NewGuid(); insert.Bind(g).Execute(); Guid?r = default; Assert.True(select.Execute(out r)); Assert.Equal(g, r); } }
public void EnumNull(int?intValue) { using (var tbl = new TestTable("create table t (x int)")) using (var insert = tbl.Stmt <E?>("insert into t values (@x)")) using (var select = tbl.RStmt <E?>("select x from t")) { E?value = (E?)intValue; insert.Bind(value).Execute(); E?r = default; Assert.True(select.Execute(out r)); Assert.Equal(value, r); } }
public void Scalar_ToInt() { P <int> p = 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.RStmt <int>("select x from t")) { p.Value = 5; insert.Bind(p).Execute(); int r = default; Assert.True(select.Execute(out r)); Assert.Equal(p.Value, r); } }
public void Scalar_ToString() { P <string> p = 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.RStmt <string>("select x from t")) { p.Value = "hello"; insert.Bind(p).Execute(); string r = null !; Assert.True(select.Execute(out r)); Assert.Equal(p.Value, r); } }
public void Custom_ScalarGuidToText() { var conv = ParameterConverter.ScalarBuilder <Guid>() .With((Guid g, Span <char> b) => g.ToString().AsSpan().CopyTo(b), _ => 36) .Compile(); using (var tbl = new TestTable("create table t (x text)")) using (var insert = tbl.Stmt("insert into t values (@x)", conv)) using (var select = tbl.RStmt <Guid>("select x from t")) { Guid g = Guid.NewGuid(); insert.Bind(g).Execute(); Guid r = default; select.Execute(out r); Assert.Equal(g, r); } }
public void SelectMany_Linq(int n) { 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.RStmt <int>("select x from t")) { int expectedSum = 0; for (int i = 0; i < n; i++) { insert.Bind(i).Execute(); expectedSum += i; } IEnumerable <Row <int> > rows = select.Execute(); int sum = rows.Sum(r => { r.AssignTo(out int x); return(x); }); Assert.Equal(expectedSum, sum); } }
public void Custom_ToBlob() { var pc = ParameterConverter.ScalarBuilder <int>() .With((int i, Span <byte> span) => span[0] = (byte)(i + 1), _ => 1) .Compile(); var rc = ResultConverter.ScalarBuilder <int>() .With((ReadOnlySpan <byte> span) => span[0] + 1) .Compile(); using (var tbl = new TestTable("create table t (x)")) using (var insert = tbl.Stmt("insert into t values (@x)", pc)) using (var select = tbl.RStmt("select x from t", rc)) { insert.Bind(1).Execute(); int i = 0; Assert.True(select.Execute(out i)); Assert.Equal(3, i); } }
public void SelectMany_Array(int n) { 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.RStmt <int>("select x from t")) { int expectedSum = 0; for (int i = 0; i < n; i++) { insert.Bind(i).Execute(); expectedSum += i; } int j = 0; int[] values = new int[n]; foreach (Row <int> row in select.Execute()) { row.AssignTo(out values[j++]); } Assert.Equal(expectedSum, Enumerable.Sum(values)); } }
public void SelectMany_IEnumerable(int n) { 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.RStmt <int>("select x from t")) { int expectedSum = 0; for (int i = 0; i < n; i++) { insert.Bind(i).Execute(); expectedSum += i; } System.Collections.IEnumerable rows = select.Execute(); int sum = 0; foreach (object?obj in rows) { ((Row <int>)obj !).AssignTo(out int x); sum += x; } Assert.Equal(expectedSum, sum); } }