Ejemplo n.º 1
0
        internal RowSetMetadata(BEBinaryReader reader)
        {
            var coldat          = new List <ColumnDesc>();
            var flags           = (RowSetMetadataFlags)reader.ReadInt32();
            int numberOfcolumns = reader.ReadInt32();

            _rawColumns = new ColumnDesc[numberOfcolumns];
            string gKsname    = null;
            string gTablename = null;

            if ((flags & RowSetMetadataFlags.HasMorePages) == RowSetMetadataFlags.HasMorePages)
            {
                PagingState = reader.ReadBytes();
            }
            else
            {
                PagingState = null;
            }

            if ((flags & RowSetMetadataFlags.NoMetadata) != RowSetMetadataFlags.NoMetadata)
            {
                if ((flags & RowSetMetadataFlags.GlobalTablesSpec) == RowSetMetadataFlags.GlobalTablesSpec)
                {
                    gKsname    = reader.ReadString();
                    gTablename = reader.ReadString();
                }

                for (int i = 0; i < numberOfcolumns; i++)
                {
                    var col = new ColumnDesc();
                    if ((flags & RowSetMetadataFlags.GlobalTablesSpec) != RowSetMetadataFlags.GlobalTablesSpec)
                    {
                        col.Keyspace = reader.ReadString();
                        col.Table    = reader.ReadString();
                    }
                    else
                    {
                        col.Keyspace = gKsname;
                        col.Table    = gTablename;
                    }
                    col.Name     = reader.ReadString();
                    col.TypeCode = (ColumnTypeCode)reader.ReadUInt16();
                    col.TypeInfo = GetColumnInfo(reader, col.TypeCode);
                    coldat.Add(col);
                }
                _rawColumns = coldat.ToArray();

                _columns      = new CqlColumn[_rawColumns.Length];
                ColumnIndexes = new Dictionary <string, int>();
                for (int i = 0; i < _rawColumns.Length; i++)
                {
                    _columns[i] = new CqlColumn
                    {
                        Name     = _rawColumns[i].Name,
                        Keyspace = _rawColumns[i].Keyspace,
                        Table    = _rawColumns[i].Table,
                        Type     = TypeCodec.GetDefaultTypeFromCqlType(
                            _rawColumns[i].TypeCode,
                            _rawColumns[i].TypeInfo),
                        TypeCode = _rawColumns[i].TypeCode,
                        TypeInfo = _rawColumns[i].TypeInfo,
                        Index    = i
                    };
                    //TODO: what with full long column names?
                    if (!ColumnIndexes.ContainsKey(_rawColumns[i].Name))
                    {
                        ColumnIndexes.Add(_rawColumns[i].Name, i);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle conversions for some types that, for backward compatibility,
        /// the result type can be more than 1 depending on the type provided by the user
        /// </summary>
        internal static object TryConvertToType(object value, ColumnDesc column, Type targetType)
        {
            if (value == null)
            {
                return(null);
            }
            switch (column.TypeCode)
            {
            case ColumnTypeCode.List:
            {
                //value is an array, according to TypeCodec
                if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType))
                {
                    //Return the underlying array
                    return(value);
                }
                var childTypeInfo = (ListColumnInfo)column.TypeInfo;
                return(Utils.ToCollectionType(typeof(List <>), TypeCodec.GetDefaultTypeFromCqlType(childTypeInfo.ValueTypeCode, childTypeInfo.ValueTypeInfo), (Array)value));
            }

            case ColumnTypeCode.Set:
            {
                //value is an array, according to TypeCodec
                if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType))
                {
                    //Return the underlying array
                    return(value);
                }
                var childTypeInfo = (SetColumnInfo)column.TypeInfo;
                var itemType      = TypeCodec.GetDefaultTypeFromCqlType(childTypeInfo.KeyTypeCode, childTypeInfo.KeyTypeInfo);
                if (targetType.IsGenericType)
                {
                    var genericType = targetType.GetGenericTypeDefinition();
                    if (genericType == typeof(SortedSet <>))
                    {
                        return(Utils.ToCollectionType(typeof(SortedSet <>), itemType, (Array)value));
                    }
                    if (genericType == typeof(HashSet <>))
                    {
                        return(Utils.ToCollectionType(typeof(HashSet <>), itemType, (Array)value));
                    }
                }
                return(Utils.ToCollectionType(typeof(List <>), itemType, (Array)value));
            }

            case ColumnTypeCode.Timestamp:
                //value is a DateTimeOffset
                if (targetType == typeof(object) || targetType == typeof(DateTimeOffset))
                {
                    return(value);
                }
                return(((DateTimeOffset)value).DateTime);

            case ColumnTypeCode.Timeuuid:
                //Value is a Uuid
                if (targetType == typeof(TimeUuid))
                {
                    return((TimeUuid)(Guid)value);
                }
                return(value);

            default:
                return(value);
            }
        }
Ejemplo n.º 3
0
        internal RowSetMetadata(BEBinaryReader reader, bool parsePartitionKeys = false)
        {
            if (reader == null)
            {
                //Allow to be created for unit tests
                return;
            }
            var coldat          = new List <ColumnDesc>();
            var flags           = (RowSetMetadataFlags)reader.ReadInt32();
            var numberOfcolumns = reader.ReadInt32();

            if (parsePartitionKeys)
            {
                PartitionKeys = new int[reader.ReadInt32()];
                for (var i = 0; i < PartitionKeys.Length; i++)
                {
                    PartitionKeys[i] = reader.ReadInt16();
                }
            }

            string gKsname    = null;
            string gTablename = null;

            if ((flags & RowSetMetadataFlags.HasMorePages) == RowSetMetadataFlags.HasMorePages)
            {
                PagingState = reader.ReadBytes();
            }

            if ((flags & RowSetMetadataFlags.NoMetadata) == RowSetMetadataFlags.NoMetadata)
            {
                return;
            }
            if ((flags & RowSetMetadataFlags.GlobalTablesSpec) == RowSetMetadataFlags.GlobalTablesSpec)
            {
                gKsname    = reader.ReadString();
                gTablename = reader.ReadString();
            }

            for (var i = 0; i < numberOfcolumns; i++)
            {
                var col = new ColumnDesc();
                if ((flags & RowSetMetadataFlags.GlobalTablesSpec) != RowSetMetadataFlags.GlobalTablesSpec)
                {
                    col.Keyspace = reader.ReadString();
                    col.Table    = reader.ReadString();
                }
                else
                {
                    col.Keyspace = gKsname;
                    col.Table    = gTablename;
                }
                col.Name     = reader.ReadString();
                col.TypeCode = (ColumnTypeCode)reader.ReadUInt16();
                col.TypeInfo = GetColumnInfo(reader, col.TypeCode);
                coldat.Add(col);
            }
            var rawColumns = coldat.ToArray();

            Columns       = new CqlColumn[rawColumns.Length];
            ColumnIndexes = new Dictionary <string, int>();
            for (var i = 0; i < rawColumns.Length; i++)
            {
                Columns[i] = new CqlColumn
                {
                    Name     = rawColumns[i].Name,
                    Keyspace = rawColumns[i].Keyspace,
                    Table    = rawColumns[i].Table,
                    Type     = TypeCodec.GetDefaultTypeFromCqlType(
                        rawColumns[i].TypeCode,
                        rawColumns[i].TypeInfo),
                    TypeCode = rawColumns[i].TypeCode,
                    TypeInfo = rawColumns[i].TypeInfo,
                    Index    = i
                };

                ColumnIndexes[rawColumns[i].Name] = i;
            }
        }