Exemplo n.º 1
0
        private static ColumnDesc[] AdaptKeyTypes(string typesString)
        {
            if (typesString == null)
            {
                return(new ColumnDesc[0]);
            }
            var indexes = new List <int>();

            for (var i = 1; i < typesString.Length; i++)
            {
                if (typesString[i] == ',')
                {
                    indexes.Add(i + 1);
                }
            }
            if (typesString.StartsWith(CompositeTypeName))
            {
                indexes.Insert(0, CompositeTypeName.Length + 1);
                indexes.Add(typesString.Length);
            }
            else
            {
                indexes.Insert(0, 0);
                //we are talking about indexes
                //the next valid start indexes would be at length + 1
                indexes.Add(typesString.Length + 1);
            }
            var types = new ColumnDesc[indexes.Count - 1];

            for (var i = 0; i < types.Length; i++)
            {
                types[i] = DataTypeParser.ParseFqTypeName(typesString, indexes[i], indexes[i + 1] - indexes[i] - 1);
            }
            return(types);
        }
Exemplo 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 || targetType == typeof(object))
            {
                return(value);
            }
            switch (column.TypeCode)
            {
            case ColumnTypeCode.List:
            case ColumnTypeCode.Set:
                return(TryConvertToCollection(value, column, targetType));

            case ColumnTypeCode.Map:
                return(TryConvertDictionary((IDictionary)value, column, targetType));

            case ColumnTypeCode.Timestamp:
                // The type of the value is DateTimeOffset
                if (targetType == typeof(object) || targetType.GetTypeInfo().IsAssignableFrom(typeof(DateTimeOffset)))
                {
                    return(value);
                }
                return(((DateTimeOffset)value).DateTime);

            case ColumnTypeCode.Timeuuid:
                // The type of the value is a Uuid
                if (targetType.GetTypeInfo().IsAssignableFrom(typeof(TimeUuid)) && !(value is TimeUuid))
                {
                    return((TimeUuid)(Guid)value);
                }
                return(value);

            default:
                return(value);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of Function metadata.
 /// </summary>
 public FunctionMetadata(string name, string keyspaceName, string[] signature, string[] argumentNames, ColumnDesc[] argumentTypes,
                         string body, bool calledOnNullInput, string language, ColumnDesc returnType)
 {
     Name              = name;
     KeyspaceName      = keyspaceName;
     Signature         = signature;
     ArgumentNames     = argumentNames;
     ArgumentTypes     = argumentTypes;
     Body              = body;
     CalledOnNullInput = calledOnNullInput;
     Language          = language;
     ReturnType        = returnType;
 }
 public AggregateMetadata(string name, string keyspaceName, string[] signature, ColumnDesc[] argumentTypes,
                          string stateFunction, ColumnDesc stateType, string finalFunction, string initialCondition, ColumnDesc returnType)
 {
     Name             = name;
     KeyspaceName     = keyspaceName;
     Signature        = signature;
     ArgumentTypes    = argumentTypes;
     StateFunction    = stateFunction;
     StateType        = stateType;
     FinalFunction    = finalFunction;
     InitialCondition = initialCondition;
     ReturnType       = returnType;
 }
Exemplo n.º 5
0
        private static object TryConvertToCollection(object value, ColumnDesc column, Type targetType)
        {
            var targetTypeInfo = targetType.GetTypeInfo();
            // value is an array, according to TypeCodec
            var  childType = value.GetType().GetTypeInfo().GetElementType();
            Type childTargetType;

            if (targetTypeInfo.IsArray)
            {
                childTargetType = targetTypeInfo.GetElementType();
                if (childTargetType.GetTypeInfo().IsAssignableFrom(childType))
                {
                    return(value);
                }
                return(GetArray((Array)value, childTargetType, column.TypeInfo));
            }
            if (Utils.IsIEnumerable(targetType, out childTargetType))
            {
                var genericTargetType = targetType.GetGenericTypeDefinition();
                // Is IEnumerable
                if (!childTargetType.GetTypeInfo().IsAssignableFrom(childType))
                {
                    // Conversion is needed
                    value = GetArray((Array)value, childTargetType, column.TypeInfo);
                }
                if (genericTargetType == typeof(IEnumerable <>))
                {
                    // The target type is an interface
                    return(value);
                }
                if (column.TypeCode == ColumnTypeCode.List || genericTargetType == typeof(List <>) ||
                    genericTargetType == typeof(IList <>))
                {
                    // Use List<T> by default when a list is expected and the target type
                    // is not an object or an array
                    return(Utils.ToCollectionType(typeof(List <>), childTargetType, (Array)value));
                }
                if (genericTargetType == typeof(SortedSet <>) || genericTargetType == typeof(ISet <>))
                {
                    return(Utils.ToCollectionType(typeof(SortedSet <>), childTargetType, (Array)value));
                }
                if (genericTargetType == typeof(HashSet <>))
                {
                    return(Utils.ToCollectionType(typeof(HashSet <>), childTargetType, (Array)value));
                }
            }
            throw new InvalidCastException(string.Format("Unable to cast object of type '{0}' to type '{1}'",
                                                         value.GetType(), targetType));
        }
Exemplo n.º 6
0
        private static IDictionary TryConvertDictionary(IDictionary value, ColumnDesc column, Type targetType)
        {
            if (targetType.GetTypeInfo().IsInstanceOfType(value))
            {
                return(value);
            }
            var  mapColumnInfo = (MapColumnInfo)column.TypeInfo;
            Type childTargetKeyType;
            Type childTargetValueType;

            if (!Utils.IsIDictionary(targetType, out childTargetKeyType, out childTargetValueType))
            {
                throw new InvalidCastException(string.Format("Unable to cast object of type '{0}' to type '{1}'",
                                                             value.GetType(), targetType));
            }
            var childTypes = value.GetType().GetTypeInfo().GetGenericArguments();

            if (!childTargetKeyType.GetTypeInfo().IsAssignableFrom(childTypes[0]) ||
                !childTargetValueType.GetTypeInfo().IsAssignableFrom(childTypes[1]))
            {
                // Convert to target type
                var type   = typeof(SortedDictionary <,>).MakeGenericType(childTargetKeyType, childTargetValueType);
                var result = (IDictionary)Activator.CreateInstance(type);
                foreach (DictionaryEntry entry in value)
                {
                    result.Add(
                        TryConvertToType(entry.Key, new ColumnDesc
                    {
                        TypeCode = mapColumnInfo.KeyTypeCode,
                        TypeInfo = mapColumnInfo.KeyTypeInfo
                    }, childTargetKeyType),
                        TryConvertToType(entry.Value, new ColumnDesc
                    {
                        TypeCode = mapColumnInfo.ValueTypeCode,
                        TypeInfo = mapColumnInfo.ValueTypeInfo
                    }, childTargetValueType));
                }
                return(result);
            }
            return(value);
        }
        private IColumnInfo GetColumnInfo(FrameReader reader, ColumnTypeCode code)
        {
            ColumnTypeCode innercode;

            switch (code)
            {
            case ColumnTypeCode.List:
                innercode = (ColumnTypeCode)reader.ReadUInt16();
                return(new ListColumnInfo
                {
                    ValueTypeCode = innercode,
                    ValueTypeInfo = GetColumnInfo(reader, innercode)
                });

            case ColumnTypeCode.Map:
                innercode = (ColumnTypeCode)reader.ReadUInt16();
                IColumnInfo kci        = GetColumnInfo(reader, innercode);
                var         vinnercode = (ColumnTypeCode)reader.ReadUInt16();
                IColumnInfo vci        = GetColumnInfo(reader, vinnercode);
                return(new MapColumnInfo
                {
                    KeyTypeCode = innercode,
                    KeyTypeInfo = kci,
                    ValueTypeCode = vinnercode,
                    ValueTypeInfo = vci
                });

            case ColumnTypeCode.Set:
                innercode = (ColumnTypeCode)reader.ReadUInt16();
                return(new SetColumnInfo
                {
                    KeyTypeCode = innercode,
                    KeyTypeInfo = GetColumnInfo(reader, innercode)
                });

            case ColumnTypeCode.Custom:
                return(new CustomColumnInfo {
                    CustomTypeName = reader.ReadString()
                });

            case ColumnTypeCode.Udt:
                var udtInfo     = new UdtColumnInfo(reader.ReadString() + "." + reader.ReadString());
                var fieldLength = reader.ReadInt16();
                for (var i = 0; i < fieldLength; i++)
                {
                    var dataType = new ColumnDesc
                    {
                        Name     = reader.ReadString(),
                        TypeCode = (ColumnTypeCode)reader.ReadUInt16(),
                    };

                    dataType.TypeInfo = GetColumnInfo(reader, dataType.TypeCode);
                    udtInfo.Fields.Add(dataType);
                }
                return(udtInfo);

            case ColumnTypeCode.Tuple:
            {
                var tupleInfo     = new TupleColumnInfo();
                var elementLength = reader.ReadInt16();
                for (var i = 0; i < elementLength; i++)
                {
                    var dataType = new ColumnDesc
                    {
                        TypeCode = (ColumnTypeCode)reader.ReadUInt16(),
                    };
                    dataType.TypeInfo = GetColumnInfo(reader, dataType.TypeCode);
                    tupleInfo.Elements.Add(dataType);
                }
                return(tupleInfo);
            }

            default:
                return(null);
            }
        }