public async Task ExecuteWithReference() { var info = new SqlFunctionInfo(new ObjectName("count"), PrimitiveTypes.BigInt()); info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.VarChar())); var function = new SqlAggregateFunctionDelegate(info, accumulate => { SqlObject r; if (accumulate.IsFirst) { r = accumulate.Current; } else { var x = accumulate.Accumulation; var y = accumulate.Current; r = x.Add(y); } accumulate.SetResult(r); }); var result = await function.ExecuteAsync(context, SqlExpression.Reference(ObjectName.Parse("a"))); Assert.NotNull(result); Assert.NotNull(result.ReturnedValue); Assert.True(result.HasReturnedValue); Assert.IsType <SqlConstantExpression>(result.ReturnedValue); var value = ((SqlConstantExpression)result.ReturnedValue).Value; Assert.Equal(SqlObject.Integer(56), value); }
public static void EnuemrateRows() { var tableInfo = new TableInfo(new ObjectName("table1")); tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22))); var table = new TemporaryTable(tableInfo); table.AddRow(new SqlObject[] { SqlObject.Integer(22), SqlObject.String(new SqlString("test")), }); table.AddRow(new [] { SqlObject.Integer(15002933), SqlObject.String(new SqlString("test2")), }); Assert.Equal(2, table.Count()); var row1 = table.ElementAt(0); var row2 = table.ElementAt(1); Assert.Equal(SqlObject.Integer(22), row1["a"]); Assert.Equal(SqlObject.String(new SqlString("test")), row1["b"]); Assert.Equal(SqlObject.Integer(15002933), row2["a"]); }
public static void VisitFunction() { var exp = SqlExpression.Function(ObjectName.Parse("sys.func1"), new[] { new InvokeArgument("a", SqlObject.Integer(3)) }); Visit(exp); }
public OuterTableTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); var temp1 = new TemporaryTable(leftInfo); temp1.AddRow(new [] { SqlObject.Integer(23), SqlObject.Boolean(true) }); temp1.AddRow(new [] { SqlObject.Integer(54), SqlObject.Boolean(null) }); left = temp1; var rightInfo = new TableInfo(ObjectName.Parse("tab2")); rightInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); rightInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); var temp2 = new TemporaryTable(rightInfo); temp2.AddRow(new [] { SqlObject.Integer(15), SqlObject.Boolean(true) }); temp2.AddRow(new [] { SqlObject.Integer(544), SqlObject.Boolean(false) }); right = temp2; }
public async Task LeftOuterTable() { var leftRows = left.Select(x => x.Number); var rightRows = right.Select(x => x.Number); var v1 = new VirtualTable(new[] { left, right }, new[] { leftRows, rightRows }); var result = v1.Outer(left); Assert.NotNull(result); Assert.Equal(4, result.RowCount); Assert.Equal(4, result.TableInfo.Columns.Count); // the outher right join has placed the left table on top // while the previous merge had the columns at the beginning // +--------+--------+--------+--------+ // | tab1.a | tab1.b | tab2.a | tab2.b | // +-----------------------------------+ // var value1 = await result.GetValueAsync(1, 0); var value2 = await result.GetValueAsync(1, 1); Assert.Equal(SqlObject.Integer(54), value1); Assert.Equal(SqlObject.Boolean(null), value2); }
public SqlAggreateFunctionTests() { var group = new List <SqlObject> { SqlObject.Integer(33), SqlObject.Integer(22), SqlObject.Integer(1) }; var refResolver = new Mock <IReferenceResolver>(); refResolver.Setup(x => x.ResolveType(It.IsAny <ObjectName>())) .Returns <ObjectName>(name => PrimitiveTypes.Integer()); refResolver.Setup(x => x.ResolveReferenceAsync(It.IsAny <ObjectName>())) .Returns <ObjectName>(name => Task.FromResult(group[0])); var resolverMock = new Mock <IGroupResolver>(); resolverMock.SetupGet(x => x.Size) .Returns(group.Count); resolverMock.Setup(x => x.ResolveReferenceAsync(It.IsAny <ObjectName>(), It.IsAny <long>())) .Returns <ObjectName, long>((name, index) => Task.FromResult(group[(int)index])); resolverMock.Setup(x => x.GetResolver(It.IsAny <long>())) .Returns(refResolver.Object); var container = new ServiceContainer(); container.RegisterInstance <IGroupResolver>(resolverMock.Object); var mock = new Mock <IContext>(); mock.SetupGet(x => x.Scope) .Returns(container); context = mock.Object; }
public override SqlExpression VisitConstantNumeric(PlSqlParser.ConstantNumericContext context) { var value = context.numeric().GetText(); var formatInfo = new NumberFormatInfo { NumberDecimalSeparator = "." }; var number = SqlNumber.Parse(value, formatInfo); SqlObject obj; if (number.CanBeInt32) { obj = SqlObject.Integer((int)number); } else if (number.CanBeInt64) { obj = SqlObject.BigInt((long)number); } else { obj = SqlObject.Numeric(number); } return(SqlExpression.Constant(obj)); }
public static void VisitCondition() { var exp = SqlExpression.Condition(SqlExpression.Constant(SqlObject.Boolean(true)), SqlExpression.Constant(SqlObject.BigInt(902)), SqlExpression.Constant(SqlObject.Integer(433))); Visit(exp); }
public static void VisitBinary() { var exp = SqlExpression.Binary(SqlExpressionType.Add, SqlExpression.Constant(SqlObject.Integer(22)), SqlExpression.Constant(SqlObject.Integer(344))); Visit(exp); }
public void GetSubsetIndex() { var table = new FilterTable(left); var index = table.GetColumnIndex(0); Assert.NotNull(index); var rows = index.SelectGreater(new IndexKey(SqlObject.Integer(24))); Assert.Single(rows); }
public void SelectGreater() { var index = CreateFullIndex("a"); var result = index.SelectGreater(new[] { SqlObject.Integer(24) }); Assert.NotEmpty(result); Assert.Single(result); Assert.Equal(1, result.ElementAt(0)); }
public void SelectLessOrEqual() { var index = CreateFullIndex("a"); var result = index.SelectLessOrEqual(new[] { SqlObject.Integer(24) }); Assert.NotEmpty(result); Assert.Equal(2, result.Count()); Assert.Equal(0, result.ElementAt(0)); }
public static void SerializeGroup() { var exp = SqlExpression.Binary(SqlExpressionType.Equal, SqlExpression.Constant(SqlObject.Integer(33)), SqlExpression.Constant(SqlObject.Integer(54))); var group = SqlExpression.Group(exp); var result = BinarySerializeUtil.Serialize(group); Assert.NotNull(result); Assert.IsType <SqlBinaryExpression>(result.Expression); }
public static void GetGroupString() { var exp = SqlExpression.Binary(SqlExpressionType.Equal, SqlExpression.Constant(SqlObject.Integer(33)), SqlExpression.Constant(SqlObject.Integer(54))); var group = SqlExpression.Group(exp); const string expected = "(33 = 54)"; var sql = group.ToString(); Assert.Equal(expected, sql); }
public async Task EnumerateLimited() { var limited = new LimitedTable(left, -1, 2); Assert.Equal(2, limited.RowCount); Assert.Equal(3, limited.TableInfo.Columns.Count); var row1 = limited.First(); var value1 = await row1.GetValueAsync("a"); Assert.Equal(SqlObject.Integer(23), value1); }
public async void GroupId() { var function = SqlExpression.Function(new ObjectName("group_id")); var result = await function.ReduceAsync(context); Assert.NotNull(result); Assert.IsType <SqlConstantExpression>(result); var value = ((SqlConstantExpression)result).Value; Assert.Equal(SqlObject.Integer(0), value); }
public InsertSearchTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); leftInfo.Columns.Add(new ColumnInfo("c", PrimitiveTypes.Double())); left = new TemporaryTable(leftInfo); left.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true), SqlObject.Double(5563.22) }); left.AddRow(new[] { SqlObject.Integer(54), SqlObject.Boolean(null), SqlObject.Double(921.001) }); left.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true), SqlObject.Double(2010.221) }); }
public static void IntersetOnTwoKeys() { var set = new IndexRangeSet(); var result = set.Intersect(SqlExpressionType.LessThan, new IndexKey(SqlObject.Integer(3))); result = result.Intersect(SqlExpressionType.GreaterThan, new IndexKey(SqlObject.Integer(12))); Assert.NotNull(result); var ranges = result.ToArray(); Assert.Equal(0, ranges.Length); }
public FilterTableTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); left = new TemporaryTable(leftInfo); left.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true) }); left.AddRow(new[] { SqlObject.Integer(54), SqlObject.Boolean(null) }); left.BuildIndex(); }
public async Task LimitWithTotal() { var limited = new LimitedTable(left, -1, 2); Assert.Equal(2, limited.RowCount); Assert.Equal(3, limited.TableInfo.Columns.Count); var value1 = await limited.GetValueAsync(0, 0); var value2 = await limited.GetValueAsync(0, 1); Assert.Equal(SqlObject.Integer(23), value1); Assert.Equal(SqlObject.Boolean(true), value2); }
public static void InsertSort() { var index = new SortedCollection <SqlObject, long>(); var items = new BigList <SqlObject>(); items.Add(SqlObject.Integer(435)); items.Add(SqlObject.Integer(1920)); var comparer = new CollectionComparer(items); index.InsertSort(SqlObject.Double(2234.99), 45, comparer); Assert.Equal(1, index.Count); }
public VirtualTableTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); var temp = new TemporaryTable(leftInfo); temp.AddRow(new [] { SqlObject.Integer(23), SqlObject.Boolean(true) }); temp.AddRow(new [] { SqlObject.Integer(54), SqlObject.Boolean(null) }); left = temp; }
public FunctionTableTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); left = new TemporaryTable(leftInfo); left.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true) }); left.AddRow(new[] { SqlObject.Integer(54), SqlObject.Boolean(null) }); left.BuildIndex(); var scope = new ServiceContainer(); var mock = new Mock <IContext>(); mock.SetupGet(x => x.Scope) .Returns(scope); mock.Setup(x => x.Dispose()) .Callback(scope.Dispose); context = mock.Object; var group = new List <SqlObject> { SqlObject.Integer(33), SqlObject.Integer(22), SqlObject.Integer(1) }; var refResolver = new Mock <IReferenceResolver>(); refResolver.Setup(x => x.ResolveType(It.IsAny <ObjectName>())) .Returns <ObjectName>(name => PrimitiveTypes.Integer()); refResolver.Setup(x => x.ResolveReferenceAsync(It.IsAny <ObjectName>())) .Returns <ObjectName>(name => Task.FromResult(group[0])); var resolverMock = new Mock <IGroupResolver>(); resolverMock.SetupGet(x => x.Size) .Returns(group.Count); resolverMock.Setup(x => x.ResolveReferenceAsync(It.IsAny <ObjectName>(), It.IsAny <long>())) .Returns <ObjectName, long>((name, index) => Task.FromResult(group[(int)index])); resolverMock.Setup(x => x.GetResolver(It.IsAny <long>())) .Returns(refResolver.Object); // scope.AddMethodRegistry<SystemFunctionProvider>(); scope.RegisterInstance <IGroupResolver>(resolverMock.Object); }
public async Task LimitWithOffsetAndTotal() { var limited = new LimitedTable(left, 1, 1); Assert.Equal(1, limited.RowCount); Assert.Equal(3, limited.TableInfo.Columns.Count); var value1 = await limited.GetValueAsync(0, 0); var value2 = await limited.GetValueAsync(0, 1); Assert.Equal(SqlObject.Integer(54), value1); Assert.Equal(SqlObject.Boolean(null), value2); }
public async void Sum() { var function = SqlExpression.Function(new ObjectName("sum"), new[] { new InvokeArgument(SqlExpression.Reference(new ObjectName("a"))) }); var result = await function.ReduceAsync(context); Assert.NotNull(result); Assert.IsType <SqlConstantExpression>(result); var value = ((SqlConstantExpression)result).Value; Assert.Equal(SqlObject.Integer(47), value); }
private static void RemoveAll(SortedCollection <SqlObject, long> index, int count) { var items = new BigList <SqlObject>(); for (int i = 0; i < count; i++) { items.Add(SqlObject.Integer(i * 2)); } var comparer = new CollectionComparer(items); for (int i = 0; i < items.Count; i++) { index.RemoveSort(items[i], i, comparer); } }
public LimitedTableTests() { var leftInfo = new TableInfo(ObjectName.Parse("tab1")); leftInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.Integer())); leftInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.Boolean())); leftInfo.Columns.Add(new ColumnInfo("c", PrimitiveTypes.Double())); var temp = new TemporaryTable(leftInfo); temp.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true), SqlObject.Double(5563.22) }); temp.AddRow(new[] { SqlObject.Integer(54), SqlObject.Boolean(null), SqlObject.Double(921.001) }); temp.AddRow(new[] { SqlObject.Integer(23), SqlObject.Boolean(true), SqlObject.Double(2010.221) }); temp.BuildIndex(); left = temp; }
public SystemFunctionsTests() { var scope = new ServiceContainer(); scope.AddMethodRegistry <SystemFunctionProvider>(); var groups = new Dictionary <ObjectName, IList <SqlObject> >(); groups[new ObjectName("a")] = new List <SqlObject> { SqlObject.Integer(2), SqlObject.Integer(45) }; var groupResolver = new Mock <IGroupResolver>(); groupResolver.SetupGet(x => x.GroupId) .Returns(0); groupResolver.SetupGet(x => x.Size) .Returns(groups[new ObjectName("a")].Count); groupResolver.Setup(x => x.ResolveReferenceAsync(It.IsAny <ObjectName>(), It.IsAny <long>())) .Returns <ObjectName, long>((name, index) => { IList <SqlObject> group; groups.TryGetValue(name, out group); return(Task.FromResult(group[(int)index])); }); var refResolver = new Mock <IReferenceResolver>(); refResolver.Setup(x => x.ResolveType(It.IsAny <ObjectName>())) .Returns(PrimitiveTypes.Integer()); groupResolver.Setup(x => x.GetResolver(It.IsAny <long>())) .Returns(refResolver.Object); scope.RegisterInstance <IGroupResolver>(groupResolver.Object); scope.RegisterInstance <IReferenceResolver>(refResolver.Object); var mock = new Mock <IContext>(); mock.SetupGet(x => x.Scope) .Returns(scope); mock.SetupGet(x => x.ContextName) .Returns("test"); context = mock.Object; }
public static void ContainsItem() { var index = new SortedCollection <SqlObject, long>(); var items = new BigList <SqlObject>(); items.Add(SqlObject.Integer(435)); items.Add(SqlObject.Integer(1920)); items.Add(SqlObject.Integer(9182)); var comparer = new CollectionComparer(items); index.InsertSort(items[0], 0, comparer); index.InsertSort(items[1], 1, comparer); index.InsertSort(items[2], 2, comparer); Assert.True(index.Contains(SqlObject.Integer(435), comparer)); Assert.False(index.Contains(SqlObject.Integer(21), comparer)); }
public static void UnionTwoSets() { var set1 = new IndexRangeSet(); set1.Intersect(SqlExpressionType.GreaterThan, new IndexKey(SqlObject.Integer(3))); var set2 = new IndexRangeSet(); set2.Intersect(SqlExpressionType.LessThan, new IndexKey(SqlObject.Integer(12))); var result = set1.Union(set2); Assert.NotNull(result); var ranges = result.ToArray(); Assert.Equal(2, ranges.Length); }