Example #1
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);
        }
Example #2
0
        public override void ReadFromPhysical(byte[] buffer, int offset)
        {
            _table.Truncate(Manager);

            Streams.IConveyor int32Conveyor = Manager.GetConveyor(Manager.DataTypes.SystemInteger);

            if (buffer[offset] != 0)
            {
                offset++;

                int rowSize;
                int count = (int)int32Conveyor.Read(buffer, offset);

                for (int index = 0; index < count; index++)
                {
                    rowSize = (int)int32Conveyor.Read(buffer, offset);
                    offset += sizeof(int);
                    using (IRow row = (IRow)DataValue.FromPhysical(Manager, _table.RowType, buffer, offset))
                    {
                        _table.Insert(Manager, row);
                    }
                    offset += rowSize;
                }
            }
        }
Example #3
0
        protected void PopulateBuffer()
        {
            while (_leftTable.Next())
            {
                _leftTable.Select(_sourceRow);
                _buffer.Insert(Manager, _sourceRow);
            }

            while (_rightTable.Next())
            {
                _rightTable.Select(_sourceRow);
                if (!_buffer.HasRow(Manager, _sourceRow))
                {
                    _buffer.Insert(Manager, _sourceRow);
                }
            }
        }
Example #4
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);
        }
Example #5
0
        protected override bool InternalNext()
        {
            if (!_scan.Next())
            {
                ITable table;
                while (_sourceTables.Count > 0)
                {
                    // Retrieve the current cursor to be iterated
                    table = (ITable)_sourceTables[0];
                    bool contextPopped = false;
                    bool contextPushed = false;
                    if (_sourceTables.Count > 1)
                    {
                        // Push it's parent row context, if necessary
                        Program.Stack.Push(_parentRows.Peek(0));
                        contextPushed = true;
                    }
                    try
                    {
                        if (table.Next())
                        {
                            _sequence++;
                            table.Select(_sourceRow);

                            if (contextPushed)
                            {
                                contextPopped = true;
                                Program.Stack.Pop();
                            }

                            _targetRow.ClearValues();
                            for (int index = 0; index < _sourceRow.DataType.Columns.Count; index++)
                            {
                                _targetRow[index] = _sourceRow[index];
                            }
                            if (Node.LevelColumnIndex >= 0)
                            {
                                _targetRow[Node.LevelColumnIndex] = _sourceTables.Count;
                            }
                            if (_sequenceColumnIndex >= 0)
                            {
                                _targetRow[_sequenceColumnIndex] = _sequence;
                            }

                            _buffer.Insert(Manager, _targetRow);
                            if (!_scan.FindKey(_targetRow))
                            {
                                throw new RuntimeException(RuntimeException.Codes.NewRowNotFound);
                            }

                            // Use the current row to push a new cursor looking for children of this row
                            PushSourceTable(_sourceRow);
                            return(true);
                        }
                        else
                        {
                            // The current cursor has been exhausted, pop it.
                            PopSourceTable();
                        }
                    }
                    finally
                    {
                        if (contextPushed && !contextPopped)
                        {
                            Program.Stack.Pop();
                        }
                    }
                }
                return(false);
            }
            return(true);
        }
Example #6
0
 protected override void InternalInsert(IRow oldRow, IRow newRow, BitArray valueFlags, bool uncheckedValue)
 {
     _nativeTable.Insert(Manager, newRow);
 }
Example #7
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);
        }
Example #8
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);
        }