Inheritance: JoinedTable
        public void NewVirtualTableFromOneTableSource()
        {
            var table = new VirtualTable(ObjectName.Parse("#table#"), left, new long[] { 1 });

            Assert.Equal(1, table.RowCount);
            Assert.Equal(2, table.TableInfo.Columns.Count);
        }
Beispiel #2
0
        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 async Task GetLastValueOfOneTableSource()
        {
            var table = new VirtualTable(ObjectName.Parse("#table#"), left, new long[] { 1 });

            var value = await table.GetValueAsync(0, 1);

            Assert.NotNull(value);
            Assert.IsType <SqlBooleanType>(value.Type);
        }
Beispiel #4
0
        protected override void ExecuteStatement(ExecutionContext context)
        {
            if (!context.Request.Context.CursorExists(CursorName))
                throw new StatementException(String.Format("The cursor '{0}' was not found in the current context.", CursorName));

            var cursor = context.Request.Context.FindCursor(CursorName);

            if (cursor == null)
                throw new StatementException(String.Format("The cursor '{0}' was not found in the current context.", CursorName));
            if (cursor.Status == CursorStatus.Closed)
                throw new StatementException(String.Format("The cursor '{0}' was already closed.", CursorName));

            int offset = -1;
            if (OffsetExpression != null)
                offset = OffsetExpression.EvaluateToConstant(context.Request, null);

            var row = cursor.Fetch(Direction, offset);

            if (row != null) {
                var result = new VirtualTable(row.Table, new List<int> {row.RowId.RowNumber});
                context.SetResult(result);
            }
        }
        public static ITable SelectRange(this ITable thisTable, ObjectName columnName, IndexRange[] ranges)
        {
            // If this table is empty then there is no range to select so
            // trivially return this object.
            if (thisTable.RowCount == 0)
                return thisTable;

            // Are we selecting a black or null range?
            if (ranges == null || ranges.Length == 0)
                // Yes, so return an empty table
                return thisTable.EmptySelect();

            // Are we selecting the entire range?
            if (ranges.Length == 1 &&
                ranges[0].Equals(IndexRange.FullRange))
                // Yes, so return this table.
                return thisTable;

            // Must be a non-trivial range selection.

            // Find the column index of the column selected
            int column = thisTable.IndexOfColumn(columnName);

            if (column == -1) {
                throw new Exception(
                    "Unable to find the column given to select the range of: " +
                    columnName.Name);
            }

            // Select the range
            var rows = thisTable.SelectRowsRange(column, ranges);

            // Make a new table with the range selected
            var result = new VirtualTable(thisTable, rows.ToArray());

            // We know the new set is ordered by the column.
            result.SortColumn = column;

            return result;
        }
        public static ITable Join(this ITable table, ITable otherTable, bool quick)
        {
            ITable outTable;

            if (quick) {
                // This implementation doesn't materialize the join
                outTable = new NaturallyJoinedTable(table, otherTable);
            } else {
                var tabs = new [] { table, otherTable};
                var rowSets = new IList<int>[2];

                // Optimized trivial case, if either table has zero rows then result of
                // join will contain zero rows also.
                if (table.RowCount == 0 || otherTable.RowCount == 0) {
                    rowSets[0] = new List<int>(0);
                    rowSets[1] = new List<int>(0);
                } else {
                    // The natural join algorithm.
                    List<int> thisRowSet = new List<int>();
                    List<int> tableRowSet = new List<int>();

                    // Get the set of all rows in the given table.
                    var tableSelectedSet = otherTable.Select(x => x.RowId.RowNumber).ToList();

                    int tableSelectedSetSize = tableSelectedSet.Count;

                    // Join with the set of rows in this table.
                    var e = table.GetEnumerator();
                    while (e.MoveNext()) {
                        int rowIndex = e.Current.RowId.RowNumber;
                        for (int i = 0; i < tableSelectedSetSize; ++i) {
                            thisRowSet.Add(rowIndex);
                        }

                        tableRowSet.AddRange(tableSelectedSet);
                    }

                    // The row sets we are joining from each table.
                    rowSets[0] = thisRowSet;
                    rowSets[1] = tableRowSet;
                }

                // Create the new VirtualTable with the joined tables.
                outTable = new VirtualTable(tabs, rowSets);
            }

            return outTable;
        }
        public static ITable ExhaustiveSelect(this ITable table, IRequest context, SqlExpression expression)
        {
            var result = table;

            // Exit early if there's nothing in the table to select from
            int rowCount = table.RowCount;
            if (rowCount > 0) {
                var tableResolver = table.GetVariableResolver();
                List<int> selectedSet = new List<int>(rowCount);

                foreach (var row in table) {
                    int rowIndex = row.RowId.RowNumber;

                    var rowResolver = tableResolver.ForRow(rowIndex);

                    // Resolve expression into a constant.
                    var exp = expression.Evaluate(context, rowResolver);
                    if (exp.ExpressionType != SqlExpressionType.Constant)
                        throw new NotSupportedException();

                    var value = ((SqlConstantExpression) exp).Value;
                    // If resolved to true then include in the selected set.
                    if (!value.IsNull && value.Type is BooleanType &&
                        value == true) {
                        selectedSet.Add(rowIndex);
                    }
                }

                result = new VirtualTable(table, selectedSet); ;
            }

            return result;
        }