Exemple #1
0
        protected override void InternalClose()
        {
            while (_sourceTables.Count > 0)
            {
                PopSourceTable();
            }

            if (_sourceRow != null)
            {
                _sourceRow.Dispose();
                _sourceRow = null;
            }

            if (_targetRow != null)
            {
                _targetRow.Dispose();
                _targetRow = null;
            }

            if (_scan != null)
            {
                _scan.Dispose();
                _scan = null;
            }

            if (_buffer != null)
            {
                _buffer.Drop(Manager);
                _buffer = null;
            }

            _tableType           = null;
            _sequenceColumnIndex = -1;
        }
Exemple #2
0
        public override object CopyNativeAs(Schema.IDataType dataType)
        {
            // We need to construct a new TableVar here because otherwise the native table
            // will be constructed with the keys inferred for the source, resulting in
            // incorrect access plans (see Defect #33978 for more).
            Schema.ResultTableVar tableVar = new Schema.ResultTableVar(Node);
            tableVar.Owner = Program.Plan.User;
            tableVar.EnsureTableVarColumns();
            Program.EnsureKey(tableVar);

            if (!Active)
            {
                Open();
            }
            else
            {
                Reset();
            }

            NativeTable nativeTable = new NativeTable(Manager, tableVar);

            while (Next())
            {
                using (IRow row = Select())
                {
                    nativeTable.Insert(Manager, row);
                }
            }

            return(nativeTable);
        }
Exemple #3
0
        protected override void InternalClose()
        {
            if (_scan != null)
            {
                _scan.Dispose();
                _scan = null;
            }

            if (_buffer != null)
            {
                _buffer.Drop(Manager);
                _buffer = null;
            }

            if (_leftTable != null)
            {
                _leftTable.Dispose();
                _leftTable = null;
            }

            if (_rightTable != null)
            {
                _rightTable.Dispose();
                _rightTable = null;
            }

            if (_sourceRow != null)
            {
                _sourceRow.Dispose();
                _sourceRow = null;
            }
        }
Exemple #4
0
        protected override void InternalOpen()
        {
            // TODO: Rewrite this...
            Schema.TableType      tableType = new Schema.TableType();
            Schema.BaseTableVar   tableVar  = new Schema.BaseTableVar(tableType);
            Schema.TableVarColumn newColumn;
            foreach (Schema.TableVarColumn column in Node.TableVar.Columns)
            {
                newColumn = (Schema.TableVarColumn)column.Copy();
                tableType.Columns.Add(newColumn.Column);
                tableVar.Columns.Add(newColumn);
            }

            Schema.Order       order = new Schema.Order();
            Schema.OrderColumn newOrderColumn;
            Schema.OrderColumn orderColumn;
            for (int index = 0; index < Node.Order.Columns.Count; index++)
            {
                orderColumn                  = Node.Order.Columns[index];
                newOrderColumn               = new Schema.OrderColumn(tableVar.Columns[orderColumn.Column], orderColumn.Ascending, orderColumn.IncludeNils);
                newOrderColumn.Sort          = orderColumn.Sort;
                newOrderColumn.IsDefaultSort = orderColumn.IsDefaultSort;
                order.Columns.Add(newOrderColumn);
            }
            tableVar.Orders.Add(order);

            _table = new NativeTable(Manager, tableVar);
            PopulateTable();
            _scan = new Scan(Manager, _table, _table.ClusteredIndex, ScanDirection.Forward, null, null);
            _scan.Open();
        }
Exemple #5
0
 public TableScan(IValueManager manager, NativeTable table, Schema.Order key, ScanDirection direction, Row firstKey, Row lastKey) : base(manager, table.TableType)
 {
     _nativeTable = table;
     _key         = key;
     _direction   = direction;
     _firstKey    = firstKey;
     _lastKey     = lastKey;
 }
Exemple #6
0
 /// <remarks> Scan range keys are inclusive. </remarks>
 public Scan(IValueManager manager, NativeTable table, NativeRowTree accessPath, ScanDirection direction, IRow firstKey, IRow lastKey)
 {
     _manager    = manager;
     _table      = table;
     _accessPath = accessPath;
     _direction  = direction;
     _firstKey   = firstKey;
     _lastKey    = lastKey;
 }
Exemple #7
0
 protected override void InternalClose()
 {
     if (_scan != null)
     {
         _scan.Dispose();
     }
     _scan = null;
     if (_table != null)
     {
         _table.Drop(Manager);
         _table = null;
     }
 }
Exemple #8
0
        public override object CopyNativeAs(Schema.IDataType dataType)
        {
            NativeTable newTable = new NativeTable(Manager, _table.TableVar);

            using (Scan scan = new Scan(Manager, _table, _table.ClusteredIndex, ScanDirection.Forward, null, null))
            {
                scan.Open();
                while (scan.Next())
                {
                    using (IRow row = scan.GetRow())
                    {
                        newTable.Insert(Manager, row);
                    }
                }
            }
            return(newTable);
        }
Exemple #9
0
        public static void DisposeValue(IValueManager manager, object tempValue)
        {
            IDataValue localTempValue = tempValue as IDataValue;

            if (localTempValue != null)
            {
                localTempValue.Dispose();
                return;
            }

            if (tempValue is StreamID)
            {
                manager.StreamManager.Deallocate((StreamID)tempValue);
                return;
            }

            NativeRow nativeRow = tempValue as NativeRow;

            if (nativeRow != null)
            {
                for (int index = 0; index < nativeRow.Values.Length; index++)
                {
                    DisposeValue(manager, nativeRow.Values[index]);
                }
                return;
            }

            NativeList nativeList = tempValue as NativeList;

            if (nativeList != null)
            {
                for (int index = 0; index < nativeList.Values.Count; index++)
                {
                    DisposeValue(manager, nativeList.Values[index]);
                }
            }

            NativeTable nativeTable = tempValue as NativeTable;

            if (nativeTable != null)
            {
                DisposeNative(manager, nativeTable.TableType, nativeTable);
            }
        }
Exemple #10
0
        protected override void InternalOpen()
        {
            _sourceTables = new Stack(Program.Stack.MaxStackDepth, Program.Stack.MaxCallDepth);
            _sourceTables.PushWindow(0);
            _parentRows = new Stack(Program.Stack.MaxStackDepth, Program.Stack.MaxCallDepth);
            _parentRows.PushWindow(0);
            PushSourceTable(null);
            _sourceRow = new Row(Manager, ((TableNode)Node.Nodes[0]).DataType.RowType);
            _tableType = new Schema.TableType();
            _tableVar  = new Schema.BaseTableVar(_tableType, Program.TempDevice);
            Schema.TableVarColumn newColumn;
            foreach (Schema.TableVarColumn column in Node.TableVar.Columns)
            {
                newColumn = (Schema.TableVarColumn)column.Copy();
                _tableType.Columns.Add(newColumn.Column);
                _tableVar.Columns.Add(newColumn);
            }

            if (Node.SequenceColumnIndex < 0)
            {
                newColumn = new Schema.TableVarColumn(new Schema.Column(Keywords.Sequence, Program.DataTypes.SystemInteger), Schema.TableVarColumnType.Stored);
                _tableType.Columns.Add(newColumn.Column);
                _tableVar.Columns.Add(newColumn);
                _sequenceColumnIndex = _tableVar.Columns.Count - 1;
            }
            else
            {
                _sequenceColumnIndex = Node.SequenceColumnIndex;
            }

            _targetRow = new Row(Manager, _tableType.RowType);
            Schema.Key key = new Schema.Key();
            key.Columns.Add(_tableVar.Columns[_sequenceColumnIndex]);
            _tableVar.Keys.Add(key);
            _buffer = new NativeTable(Manager, _tableVar);
            _scan   = new Scan(Manager, _buffer, _buffer.ClusteredIndex, ScanDirection.Forward, null, null);
            _scan.Open();
            _sequence = 0;
            _empty    = false;
            InternalNext();
            _empty = _scan.EOF();
            _scan.First();
        }
Exemple #11
0
        protected override void InternalOpen()
        {
            _sourceRow = new Row(Manager, Node.DataType.RowType);
            _leftTable = (ITable)Node.Nodes[0].Execute(Program);
            try
            {
                _rightTable = (ITable)Node.Nodes[1].Execute(Program);
            }
            catch
            {
                _leftTable.Dispose();
                throw;
            }

            Schema.TableType      tableType = new Schema.TableType();
            Schema.BaseTableVar   tableVar  = new Schema.BaseTableVar(tableType, Program.TempDevice);
            Schema.TableVarColumn newColumn;
            foreach (Schema.TableVarColumn column in _leftTable.Node.TableVar.Columns)
            {
                newColumn = column.Inherit();
                tableType.Columns.Add(column.Column);
                tableVar.Columns.Add(column);
            }

            Schema.Key key = new Schema.Key();
            foreach (Schema.TableVarColumn column in Node.TableVar.Keys.MinimumKey(true).Columns)
            {
                key.Columns.Add(tableVar.Columns[column.Name]);
            }
            tableVar.Keys.Add(key);

            _buffer = new NativeTable(Manager, tableVar);
            PopulateBuffer();

            _scan = new Scan(Manager, _buffer, _buffer.ClusteredIndex, ScanDirection.Forward, null, null);
            _scan.Open();
        }
Exemple #12
0
 public LocalTable(TableNode tableNode, Program program) : base(tableNode, program)
 {
     _nativeTable = new NativeTable(program.ValueManager, tableNode.TableVar);
     _key         = program.OrderFromKey(program.FindClusteringKey(tableNode.TableVar));
 }
Exemple #13
0
        public static object CopyValue(IValueManager manager, object value)
        {
            IDataValue dataValue = value as IDataValue;

            if (dataValue != null)
            {
                return(dataValue.Copy());
            }

            ICloneable cloneable = value as ICloneable;

            if (cloneable != null)
            {
                return(cloneable.Clone());
            }

                        #if SILVERLIGHT
            System.Array array = value as System.Array;
            if (array != null)
            {
                return(array.Clone());
            }
                        #endif

            if (value is StreamID)
            {
                return(manager.StreamManager.Reference((StreamID)value));
            }

            NativeRow nativeRow = value as NativeRow;
            if (nativeRow != null)
            {
                NativeRow newRow = new NativeRow(nativeRow.Values.Length);
                for (int index = 0; index < nativeRow.Values.Length; index++)
                {
                                        #if USEDATATYPESINNATIVEROW
                    newRow.DataTypes[index] = nativeRow.DataTypes[index];
                                        #endif
                    newRow.Values[index] = CopyValue(manager, nativeRow.Values[index]);
                }
                return(newRow);
            }

            NativeList nativeList = value as NativeList;
            if (nativeList != null)
            {
                NativeList newList = new NativeList();
                for (int index = 0; index < nativeList.Values.Count; index++)
                {
                    newList.DataTypes.Add(nativeList.DataTypes[index]);
                    newList.Values.Add(CopyValue(manager, nativeList.Values[index]));
                }
                return(newList);
            }

            NativeTable nativeTable = value as NativeTable;
            if (nativeTable != null)
            {
                return(CopyNative(manager, nativeTable.TableType, nativeTable));
            }

            return(value);
        }
Exemple #14
0
        public static object CopyNative(IValueManager manager, Schema.IDataType dataType, object tempValue)
        {
            // This code is duplicated in the descendent CopyNative methods for performance
            if (tempValue == null)
            {
                return(tempValue);
            }

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    return(manager.StreamManager.Reference((StreamID)tempValue));
                }

                ICloneable cloneable = tempValue as ICloneable;
                if (cloneable != null)
                {
                    return(cloneable.Clone());
                }

                if (scalarType.IsCompound)
                {
                    return(CopyNative(manager, scalarType.CompoundRowType, tempValue));
                }

                return(tempValue);
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                NativeRow nativeRow = (NativeRow)tempValue;
                NativeRow newRow    = new NativeRow(rowType.Columns.Count);
                for (int index = 0; index < rowType.Columns.Count; index++)
                {
                                        #if USEDATATYPESINNATIVEROW
                    newRow.DataTypes[index] = nativeRow.DataTypes[index];
                    newRow.Values[index]    = CopyNative(manager, nativeRow.DataTypes[index], nativeRow.Values[index]);
                                        #else
                    newRow.Values[index] = CopyNative(AManager, rowType.Columns[index].DataType, nativeRow.Values[index]);
                                        #endif
                }
                return(newRow);
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                NativeList nativeList = (NativeList)tempValue;
                NativeList newList    = new NativeList();
                for (int index = 0; index < nativeList.DataTypes.Count; index++)
                {
                    newList.DataTypes.Add(nativeList.DataTypes[index]);
                    newList.Values.Add(CopyNative(manager, nativeList.DataTypes[index], nativeList.Values[index]));
                }
                return(newList);
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                NativeTable nativeTable = (NativeTable)tempValue;
                NativeTable newTable    = new NativeTable(manager, nativeTable.TableVar);
                using (Scan scan = new Scan(manager, nativeTable, nativeTable.ClusteredIndex, ScanDirection.Forward, null, null))
                {
                    scan.Open();
                    while (scan.Next())
                    {
                        using (IRow row = scan.GetRow())
                        {
                            newTable.Insert(manager, row);
                        }
                    }
                }
                return(newTable);
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(tempValue);
            }

            var runtimeType = manager.GetRuntimeType(tempValue);
            if (runtimeType != null)
            {
                return(CopyNative(manager, runtimeType, tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
Exemple #15
0
 public TableValue(IValueManager manager, Schema.ITableType tableType, NativeTable table) : base(manager, tableType)
 {
     _table = table;
 }
Exemple #16
0
        /// <summary>
        /// Converts the C# host representation of a value to the "Native" representation (using NativeLists, NativeRows, and NativeTables)
        /// </summary>
        /// <param name="dataType">The target data type for the conversion.</param>
        /// <param name="value">The source value to be converted.</param>
        /// <returns>The value in its "Native" representation.</returns>
        public static object ToNativeOf(IValueManager valueManager, Schema.IDataType dataType, object value)
        {
            if (value != null)
            {
                var scalarType = dataType as Schema.IScalarType;
                if (scalarType != null)
                {
                    if (scalarType.Equals(valueManager.DataTypes.SystemString) && !(value is String))
                    {
                        return(value.ToString());                        // The usual scenario would be an enumerated type...
                    }
                    if (scalarType.Equals(valueManager.DataTypes.SystemDateTime) && value is DateTimeOffset)
                    {
                        return(new DateTime(((DateTimeOffset)value).Ticks));
                    }

                    return(value);                    // Otherwise, return the C# representation directly
                }

                var listType = dataType as Schema.IListType;
                if (listType != null && value != null)
                {
                    var listValue = value as ListValue;
                    if (listValue != null)
                    {
                        return(listValue);
                    }

                    var nativeList = value as NativeList;
                    if (nativeList != null)
                    {
                        return(nativeList);
                    }

                    var iList = value as IList;
                    if (iList != null)
                    {
                        var newList = new NativeList();
                        for (int index = 0; index < iList.Count; index++)
                        {
                            newList.DataTypes.Add(listType.ElementType);
                            newList.Values.Add(ToNativeOf(valueManager, listType.ElementType, iList[index]));
                        }

                        return(newList);
                    }

                    throw new RuntimeException(RuntimeException.Codes.InternalError, String.Format("Unexpected type for property: {0}", value.GetType().FullName));
                }

                var tableType = dataType as Schema.ITableType;
                if (tableType != null && value != null)
                {
                    var tableValue = value as TableValue;
                    if (tableValue != null)
                    {
                        return(tableValue);
                    }

                    var nativeTable = value as NativeTable;
                    if (nativeTable != null)
                    {
                        return(nativeTable);
                    }

                    var iDictionary = value as IDictionary;
                    if (iDictionary != null)
                    {
                        var newTableVar = new Schema.BaseTableVar(tableType);
                        newTableVar.EnsureTableVarColumns();
                        // Assume the first column is the key, this is potentially problematic, but without key information in table types, not much else we can do....
                        // The assumption here is that the C# representation of a "table" is a dictionary
                        newTableVar.Keys.Add(new Schema.Key(new Schema.TableVarColumn[] { newTableVar.Columns[0] }));
                        var newTable = new NativeTable(valueManager, newTableVar);
                        foreach (DictionaryEntry entry in iDictionary)
                        {
                            using (Row row = new Row(valueManager, newTableVar.DataType.RowType))
                            {
                                row[0] = ToNativeOf(valueManager, tableType.Columns[0].DataType, entry.Key);
                                row[1] = ToNativeOf(valueManager, tableType.Columns[1].DataType, entry.Value);
                                newTable.Insert(valueManager, row);
                            }
                        }

                        return(newTable);
                    }

                    throw new RuntimeException(RuntimeException.Codes.InternalError, String.Format("Unexpected type for property: {0}", value.GetType().FullName));
                }
            }

            return(value);
        }