Ejemplo n.º 1
4
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNative(IValueManager manager, Schema.IDataType dataType, object tempValue)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    return(new Scalar(manager, scalarType, (StreamID)tempValue));
                }
                if (scalarType.IsGeneric)
                {
                    return(new Scalar(manager, (Schema.ScalarType)manager.GetRuntimeType(tempValue.GetType()), tempValue));
                }
                return(new Scalar(manager, scalarType, tempValue));
            }

            if (tempValue == null)
            {
                return(null);
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                return(new Row(manager, rowType, (NativeRow)tempValue));
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                return(new ListValue(manager, listType, (NativeList)tempValue));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)tempValue));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)tempValue));
            }

            var runtimeType = manager.GetRuntimeType(tempValue);

            if (runtimeType != null)
            {
                return(FromNative(manager, runtimeType, tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
Ejemplo n.º 2
0
        private void Create(IValueManager manager)
        {
            TableType = TableVar.DataType;
            RowType   = TableType.RowType;

            Schema.RowType keyRowType;
            Schema.RowType dataRowType;

            // Create the map index required to store data as described by the given table variable
            keyRowType  = new Schema.RowType(Key.Columns);
            dataRowType = new Schema.RowType();
            foreach (Schema.Column column in TableVar.DataType.Columns)
            {
                dataRowType.Columns.Add(new Schema.Column(column.Name, column.DataType));
            }

            Index =
                new NativeRowMap
                (
                    Key,
                    EqualitySorts,
                    keyRowType,
                    dataRowType
                );
        }
Ejemplo n.º 3
0
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNativeRow(IValueManager manager, Schema.IRowType rowType, NativeRow nativeRow, int nativeRowIndex)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
                        #if USEDATATYPESINNATIVEROW
            Schema.IDataType dataType = nativeRow.DataTypes[nativeRowIndex];
            if (dataType == null)
            {
                dataType = rowType.Columns[nativeRowIndex].DataType;
            }
                        #else
            Schema.IDataType dataType = ARowType.Columns[ANativeRowIndex].DataType;
                        #endif

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                return(new RowInternedScalar(manager, scalarType, nativeRow, nativeRowIndex));
            }

            Schema.IRowType localRowType = dataType as Schema.IRowType;
            if (localRowType != null)
            {
                return(new Row(manager, localRowType, (NativeRow)nativeRow.Values[nativeRowIndex]));
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                return(new ListValue(manager, listType, (NativeList)nativeRow.Values[nativeRowIndex]));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)nativeRow.Values[nativeRowIndex]));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)nativeRow.Values[nativeRowIndex]));
            }

            var runtimeType = manager.GetRuntimeType(nativeRow.Values[nativeRowIndex]);
            if (runtimeType != null)
            {
                return(new RowInternedScalar(manager, (Schema.IScalarType)runtimeType, nativeRow, nativeRowIndex));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
Ejemplo n.º 4
0
        public IDataValue Copy()
        {
            // This code is duplicated in the FromNative and CopyAs methods for performance...
            object tempValue = CopyNative();

            Schema.IScalarType scalarType = DataType as Schema.IScalarType;

            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    Scalar scalar = new Scalar(Manager, scalarType, (StreamID)tempValue);
                    scalar.ValuesOwned = true;
                    return(scalar);
                }
                return(new Scalar(Manager, scalarType, tempValue));
            }

            Schema.IRowType rowType = DataType as Schema.IRowType;
            if (rowType != null)
            {
                Row row = new Row(Manager, rowType, (NativeRow)tempValue);
                row.ValuesOwned = true;
                return(row);
            }

            Schema.IListType listType = DataType as Schema.IListType;
            if (listType != null)
            {
                ListValue list = new ListValue(Manager, listType, (NativeList)tempValue);
                list.ValuesOwned = true;
                return(list);
            }

            Schema.ITableType tableType = DataType as Schema.ITableType;
            if (tableType != null)
            {
                TableValue table = new TableValue(Manager, tableType, (NativeTable)tempValue);
                table.ValuesOwned = true;
                return(table);
            }

            Schema.ICursorType cursorType = DataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(Manager, cursorType, (int)tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, DataType == null ? "<null>" : DataType.GetType().Name);
        }
Ejemplo n.º 5
0
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNativeList(IValueManager manager, Schema.IListType listType, NativeList nativeList, int nativeListIndex)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
            Schema.IDataType dataType = nativeList.DataTypes[nativeListIndex];
            if (dataType == null)
            {
                dataType = listType.ElementType;
            }

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                return(new ListInternedScalar(manager, scalarType, nativeList, nativeListIndex));
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                return(new Row(manager, rowType, (NativeRow)nativeList.Values[nativeListIndex]));
            }

            Schema.IListType localListType = dataType as Schema.IListType;
            if (localListType != null)
            {
                return(new ListValue(manager, localListType, (NativeList)nativeList.Values[nativeListIndex]));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)nativeList.Values[nativeListIndex]));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)nativeList.Values[nativeListIndex]));
            }

            var runtimeType = manager.GetRuntimeType(nativeList.Values[nativeListIndex]);

            if (runtimeType != null)
            {
                return(new ListInternedScalar(manager, (Schema.IScalarType)runtimeType, nativeList, nativeListIndex));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, nativeList.DataTypes[nativeListIndex] == null ? "<null>" : nativeList.DataTypes[nativeListIndex].GetType().Name);
        }
Ejemplo n.º 6
0
        /// <summary>Returns the host representation of the given physical value.</summary>
        public static IDataValue FromPhysical(IValueManager manager, Schema.IDataType dataType, byte[] buffer, int offset)
        {
            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                Scalar scalar = new Scalar(manager, scalarType, null);
                scalar.ReadFromPhysical(buffer, offset);
                return(scalar);
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                Row row = new Row(manager, rowType);
                row.ReadFromPhysical(buffer, offset);
                return(row);
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                ListValue list = new ListValue(manager, listType);
                list.ReadFromPhysical(buffer, offset);
                return(list);
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                TableValue table = new TableValue(manager, tableType, null);
                table.ReadFromPhysical(buffer, offset);
                return(table);
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                CursorValue cursor = new CursorValue(manager, cursorType, -1);
                cursor.ReadFromPhysical(buffer, offset);
                return(cursor);
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
Ejemplo n.º 7
0
        // TODO: Compile row types for each index, saving column indexes to prevent the need for lookup during insert, update, and delete.
        private void Create(IValueManager manager)
        {
            TableType = TableVar.DataType;
            RowType   = TableType.RowType;

            Schema.RowType keyRowType;
            Schema.RowType dataRowType;

            // Create the indexes required to store data as described by the given table variable
            // Determine Fanout, Capacity, Clustering Key
            Schema.Order clusteringKey = manager.FindClusteringOrder(TableVar);
            keyRowType  = new Schema.RowType(clusteringKey.Columns);
            dataRowType = new Schema.RowType();
            foreach (Schema.Column column in TableVar.DataType.Columns)
            {
                if (!clusteringKey.Columns.Contains(column.Name))
                {
                    dataRowType.Columns.Add(new Schema.Column(column.Name, column.DataType));
                }
            }

            // Add an internal identifier for uniqueness of keys in nonunique indexes
                        #if USEINTERNALID
            _internalIDColumn = new Schema.TableVarColumn(new Schema.Column(InternalIDColumnName, manager.DataTypes.SystemGuid), Schema.TableVarColumnType.InternalID);
            dataRowType.Columns.Add(_internalIDColumn.Column);
                        #endif

            // Create the Clustered index
            ClusteredIndex =
                new NativeRowTree
                (
                    clusteringKey,
                    keyRowType,
                    dataRowType,
                    _fanout,
                    _capacity,
                    true
                );

            // DataLength and DataColumns for all non clustered indexes is the key length and columns of the clustered key
            dataRowType = keyRowType;

            // Create non clustered indexes for each key and order (unique sets)
            Schema.Order key;
            foreach (Schema.Key nonClusteredKey in TableVar.Keys)
            {
                if (!nonClusteredKey.IsSparse && nonClusteredKey.Enforced)
                {
                    if (!manager.OrderIncludesKey(ClusteredIndex.Key, nonClusteredKey))
                    {
                        key = manager.OrderFromKey(nonClusteredKey);
                        if (!NonClusteredIndexes.Contains(key))
                        {
                            keyRowType = new Schema.RowType(key.Columns);

                            NonClusteredIndexes.Add
                            (
                                new NativeRowTree
                                (
                                    key,
                                    keyRowType,
                                    dataRowType,
                                    _fanout,
                                    _capacity,
                                    false
                                )
                            );
                        }
                    }
                }
                else
                {
                    // This is a potentially non-unique index, so add a GUID to ensure uniqueness of the key in the BTree
                    key = manager.OrderFromKey(nonClusteredKey);
                                        #if USEINTERNALID
                    Schema.OrderColumn uniqueColumn = new Schema.OrderColumn(_internalIDColumn, key.IsAscending);
                    uniqueColumn.Sort = manager.GetUniqueSort(uniqueColumn.Column.DataType);
                    key.Columns.Add(uniqueColumn);
                                        #endif

                    if (!NonClusteredIndexes.Contains(key))
                    {
                        keyRowType = new Schema.RowType(key.Columns);

                        NonClusteredIndexes.Add
                        (
                            new NativeRowTree
                            (
                                key,
                                keyRowType,
                                dataRowType,
                                _fanout,
                                _capacity,
                                false
                            )
                        );
                    }
                }
            }

            foreach (Schema.Order order in TableVar.Orders)
            {
                // This is a potentially non-unique index, so add a GUID to ensure uniqueness of the key in the BTree
                key = new Schema.Order(order);
                                #if USEINTERNALID
                if (!manager.OrderIncludesOrder(key, clusteringKey))
                {
                    Schema.OrderColumn uniqueColumn = new Schema.OrderColumn(_internalIDColumn, order.IsAscending);
                    uniqueColumn.Sort = manager.GetUniqueSort(uniqueColumn.Column.DataType);
                    key.Columns.Add(uniqueColumn);
                }
                                #endif

                if (!NonClusteredIndexes.Contains(key))
                {
                    keyRowType = new Schema.RowType(key.Columns);

                    NonClusteredIndexes.Add
                    (
                        new NativeRowTree
                        (
                            key,
                            keyRowType,
                            dataRowType,
                            _fanout,
                            _capacity,
                            false
                        )
                    );
                }
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
 protected Table(IValueManager manager, Schema.ITableType tableType) : base(manager, tableType)
 {
 }
Ejemplo n.º 10
0
 public TableValue(IValueManager manager, Schema.ITableType tableType, NativeTable table) : base(manager, tableType)
 {
     _table = table;
 }