Exemplo n.º 1
0
 internal static void RegisterTypeInterpreter(ColumnTypeCode type_code)
 {
     {
         var mth = typeof(TypeInterpreter).GetMethod("ConvertFrom" + (type_code.ToString()), new Type[] { typeof(IColumnInfo), typeof(byte[]), typeof(Type) });
         GoMethods[(byte)type_code] = (CqlConvertDel)Delegate.CreateDelegate(typeof(CqlConvertDel), mth);
     }
     {
         var mth = typeof(TypeInterpreter).GetMethod("GetDefaultTypeFrom" + (type_code.ToString()), new Type[] { typeof(IColumnInfo) });
         TypMethods[(byte)type_code] = (GetDefaultTypeFromCqlTypeDel)Delegate.CreateDelegate(typeof(GetDefaultTypeFromCqlTypeDel), mth);
     }
     {
         var mth = typeof(TypeInterpreter).GetMethod("InvConvertFrom" + (type_code.ToString()), new Type[] { typeof(IColumnInfo), typeof(byte[]) });
         InvMethods[(byte)type_code] = (InvCqlConvertDel)Delegate.CreateDelegate(typeof(InvCqlConvertDel), mth);
     }
 }
Exemplo n.º 2
0
        private static string GetTypeString(PocoColumn column, ColumnTypeCode typeCode, IColumnInfo typeInfo)
        {
            if (typeInfo == null)
            {
                //Is a single type
                return(typeCode.ToString().ToLower());
            }
            string typeName    = null;
            var    frozenKey   = column != null && column.HasFrozenKey;
            var    frozenValue = column != null && column.HasFrozenValue;

            if (typeInfo is MapColumnInfo)
            {
                var mapInfo = (MapColumnInfo)typeInfo;
                typeName = "map<" +
                           WrapFrozen(frozenKey, GetTypeString(null, mapInfo.KeyTypeCode, mapInfo.KeyTypeInfo)) +
                           ", " +
                           WrapFrozen(frozenValue, GetTypeString(null, mapInfo.ValueTypeCode, mapInfo.ValueTypeInfo)) +
                           ">";
            }
            else if (typeInfo is SetColumnInfo)
            {
                var setInfo = (SetColumnInfo)typeInfo;
                typeName = "set<" +
                           WrapFrozen(frozenKey, GetTypeString(null, setInfo.KeyTypeCode, setInfo.KeyTypeInfo)) +
                           ">";
            }
            else if (typeInfo is ListColumnInfo)
            {
                var setInfo = (ListColumnInfo)typeInfo;
                typeName = "list<" +
                           WrapFrozen(frozenValue, GetTypeString(null, setInfo.ValueTypeCode, setInfo.ValueTypeInfo)) +
                           ">";
            }
            else if (typeInfo is TupleColumnInfo)
            {
                var tupleInfo = (TupleColumnInfo)typeInfo;
                typeName = "tuple<" +
                           string.Join(", ", tupleInfo.Elements.Select(e => GetTypeString(null, e.TypeCode, e.TypeInfo))) +
                           ">";
            }
            else if (typeInfo is UdtColumnInfo)
            {
                var udtInfo = (UdtColumnInfo)typeInfo;
                // Escape keyspace and name from the UDT
                typeName = string.Join(".", udtInfo.Name.Split('.').Select(k => "\"" + k + "\""));
            }

            if (typeName == null)
            {
                throw new NotSupportedException(string.Format("Type {0} is not supported", typeCode));
            }
            return(WrapFrozen(column != null && column.IsFrozen, typeName));
        }
Exemplo n.º 3
0
        private static Dictionary <string, dynamic> GetDefinitionByType(
            Dictionary <string, dynamic> dictionary, IGenericSerializer genericSerializer, ColumnTypeCode typeCode, IColumnInfo typeInfo)
        {
            if (typeInfo is UdtColumnInfo udtTypeInfo)
            {
                var udtMap = genericSerializer.GetUdtMapByName(udtTypeInfo.Name);
                if (udtMap == null)
                {
                    throw new InvalidOperationException($"Could not find UDT mapping for {udtTypeInfo.Name}");
                }
                return(GetUdtTypeDefinition(dictionary, genericSerializer.GetUdtMapByName(udtTypeInfo.Name), genericSerializer));
            }

            dictionary.Add("cqlType", typeCode.ToString().ToLower());

            if (typeInfo is TupleColumnInfo tupleColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    tupleColumnInfo.Elements.Select(c => GetDefinitionByType(genericSerializer, c.TypeCode, c.TypeInfo)));
            }

            if (typeInfo is MapColumnInfo mapColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] {
                    GetDefinitionByType(genericSerializer, mapColumnInfo.KeyTypeCode, mapColumnInfo.KeyTypeInfo),
                    GetDefinitionByType(genericSerializer, mapColumnInfo.ValueTypeCode, mapColumnInfo.ValueTypeInfo)
                });
            }

            if (typeInfo is ListColumnInfo listColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, listColumnInfo.ValueTypeCode, listColumnInfo.ValueTypeInfo) });
            }

            if (typeInfo is SetColumnInfo setColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, setColumnInfo.KeyTypeCode, setColumnInfo.KeyTypeInfo) });
            }

            return(dictionary);
        }
Exemplo n.º 4
0
 private static string GetTypeString(ColumnTypeCode typeCode, IColumnInfo typeInfo)
 {
     if (typeInfo == null)
     {
         //Is a single type
         return(typeCode.ToString().ToLower());
     }
     if (typeInfo is MapColumnInfo)
     {
         var mapInfo = typeInfo as MapColumnInfo;
         return("map<" +
                GetTypeString(mapInfo.KeyTypeCode, mapInfo.KeyTypeInfo) +
                ", " +
                GetTypeString(mapInfo.ValueTypeCode, mapInfo.ValueTypeInfo) +
                ">");
     }
     if (typeInfo is SetColumnInfo)
     {
         var setInfo = typeInfo as SetColumnInfo;
         return("set<" +
                GetTypeString(setInfo.KeyTypeCode, setInfo.KeyTypeInfo) +
                ">");
     }
     if (typeInfo is ListColumnInfo)
     {
         var setInfo = typeInfo as ListColumnInfo;
         return("list<" +
                GetTypeString(setInfo.ValueTypeCode, setInfo.ValueTypeInfo) +
                ">");
     }
     if (typeInfo is TupleColumnInfo)
     {
         var tupleInfo = typeInfo as TupleColumnInfo;
         return("tuple<" +
                String.Join(", ", tupleInfo.Elements.Select(e => GetTypeString(e.TypeCode, e.TypeInfo))) +
                ">");
     }
     if (typeInfo is UdtColumnInfo)
     {
         var udtInfo = typeInfo as UdtColumnInfo;
         return(udtInfo.Name);
     }
     throw new NotSupportedException(String.Format("Type {0} is not suppoted", typeCode));
 }
Exemplo n.º 5
0
        private static void WriteTypedValue(
            ColumnTypeCode type, IColumnInfo columnInfo,
            byte[] bytes, int index, int count, BinaryWriter writer)
        {
            switch (type)
            {
            case ColumnTypeCode.Boolean:
            case ColumnTypeCode.TinyInt:
            case ColumnTypeCode.SmallInt:
            case ColumnTypeCode.Int:
            case ColumnTypeCode.Bigint:
            case ColumnTypeCode.Ascii:
            case ColumnTypeCode.Text:
            case ColumnTypeCode.Json:
            case ColumnTypeCode.Varchar:
            case ColumnTypeCode.Blob:
            case ColumnTypeCode.Inet:
            case ColumnTypeCode.Uuid:
            case ColumnTypeCode.Timeuuid:
            case ColumnTypeCode.Date:
            case ColumnTypeCode.Time:
                writer.Write(bytes, index, count);
                break;

            case ColumnTypeCode.Float:
                var copy = new byte[4];
                Array.Copy(bytes, index, copy, 0, count);
                Array.Reverse(copy);
                var floatValue = BitConverter.ToSingle(copy, 0);
                if (float.IsNaN(floatValue))
                {
                    writer.Write(0xc07f);
                }
                else
                {
                    writer.Write(bytes, index, count);
                }
                break;

            case ColumnTypeCode.Double:
                var doubleValue = BitConverter.Int64BitsToDouble(IPAddress.NetworkToHostOrder(BitConverter.ToInt64(bytes, index)));
                if (double.IsNaN(doubleValue))
                {
                    writer.Write((long)0xf87f);
                }
                else
                {
                    writer.Write(bytes, index, count);
                }
                break;

            case ColumnTypeCode.Timestamp:
                var v = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(bytes, index)) * 1000;
                writer.Write(IPAddress.HostToNetworkOrder(v));
                break;

            case ColumnTypeCode.List:
            case ColumnTypeCode.Set:
                var childColumnDesc = ((ICollectionColumnInfo)columnInfo).GetChildType();
                var length          = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                index += 4;
                for (var i = 0; i != length; ++i)
                {
                    var size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                    index += 4;
                    WriteTypedValue(childColumnDesc.TypeCode, childColumnDesc.TypeInfo, bytes, index, size, writer);
                    index += size;
                }
                break;

            case ColumnTypeCode.Map:
                var mapColumnInfo = (MapColumnInfo)columnInfo;
                var mapLength     = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                index += 4;
                for (var i = 0; i != mapLength; ++i)
                {
                    var size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                    index += 4;
                    WriteTypedValue(mapColumnInfo.KeyTypeCode, mapColumnInfo.KeyTypeInfo, bytes, index, size, writer);
                    index += size;

                    size   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                    index += 4;
                    WriteTypedValue(mapColumnInfo.ValueTypeCode, mapColumnInfo.ValueTypeInfo, bytes, index, size, writer);
                    index += size;
                }
                break;

            case ColumnTypeCode.Udt:
                var udtColumnInfo = (UdtColumnInfo)columnInfo;
                var end           = index + count;
                for (var fieldIndex = 0; index < end; ++fieldIndex)
                {
                    var size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index));
                    index += 4;
                    WriteTypedValue(udtColumnInfo.Fields[fieldIndex].TypeCode, udtColumnInfo.Fields[fieldIndex].TypeInfo, bytes, index, size, writer);
                    index += size;
                }
                break;

            case ColumnTypeCode.Counter:
            case ColumnTypeCode.Custom:
            case ColumnTypeCode.Decimal:
            case ColumnTypeCode.Tuple:
            case ColumnTypeCode.Varint:
                throw new InvalidTypeException("Datatype " + type.ToString() + " not supported in a partition key column");

            default:
                throw new InvalidTypeException("Unknown datatype " + type.ToString() + " for a partition key column");
            }
        }
        /// <summary>
        ///  Returns a new string whose textual value is the same as this string,
        ///  but whose binary representation is in Unicode normalization form C
        ///  then return a lowercase form.
        /// </summary>
        ///
        /// <param name="self">The string to be formatted.</param>
        /// <returns>Normalized string.</returns>
        ///
        /// <exception cref="NullReferenceException">Thrown when the arguments are null or empty.</exception>
        internal static string NormalizeString([NotNull] this ColumnTypeCode self)
        {
            Check.NotNull(self, $"The argument [Column type code]");

            return(self.ToString().Normalize().ToLower(DEFAULT_CUTURE));
        }