Beispiel #1
0
        public void Create_With_Frozen_Collection_Value()
        {
            string createQuery = null;
            var    serializer  = new Serializer(ProtocolVersion.MaxSupported);
            var    sessionMock = GetSessionMock(serializer);

            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(q => createQuery = q);
            var definition = new Map <UdtAndTuplePoco>()
                             .PartitionKey(c => c.Id1)
                             .Column(c => c.Id1, cm => cm.WithName("id"))
                             .Column(c => c.UdtList1, cm => cm.WithName("my_list").WithFrozenValue())
                             .Column(c => c.TupleMapValue1, cm => cm.WithName("my_map").WithFrozenValue())
                             .ExplicitColumns()
                             .TableName("tbl1");
            var udtInfo = new UdtColumnInfo("song");

            udtInfo.Fields.Add(new ColumnDesc {
                Name = "title", TypeCode = ColumnTypeCode.Ascii
            });
            udtInfo.Fields.Add(new ColumnDesc {
                Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp
            });
            var udtMap = UdtMap.For <Song>();

            udtMap.SetSerializer(serializer);
            udtMap.Build(udtInfo);
            serializer.SetUdtMap("song", udtMap);
            var table = GetTable <UdtAndTuplePoco>(sessionMock.Object, definition);

            table.Create();
            Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_list list<frozen<\"song\">>, my_map map<text, frozen<tuple<double, double>>>, PRIMARY KEY (id))", createQuery);
        }
Beispiel #2
0
        public void Create_With_Frozen_Udt()
        {
            string createQuery = null;
            var    serializer  = new SerializerManager(ProtocolVersion.MaxSupported);
            var    sessionMock = GetSessionMock(serializer);

            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(q => createQuery = q);
            var definition = new Map <UdtAndTuplePoco>()
                             .PartitionKey(c => c.Id1)
                             .Column(c => c.Id1, cm => cm.WithName("id"))
                             .Column(c => c.Udt1, cm => cm.WithName("my_udt").WithDbType <Song>().AsFrozen())
                             .ExplicitColumns()
                             .TableName("tbl1");
            var udtInfo = new UdtColumnInfo("ks1.song");

            udtInfo.Fields.Add(new ColumnDesc {
                Name = "title", TypeCode = ColumnTypeCode.Ascii
            });
            udtInfo.Fields.Add(new ColumnDesc {
                Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp
            });
            var udtMap = UdtMap.For <Song>().SetIgnoreCase(false);

            udtMap.SetSerializer(serializer.GetCurrentSerializer());
            udtMap.Build(udtInfo);
            serializer.SetUdtMap("song", udtMap);
            var table = GetTable <UdtAndTuplePoco>(sessionMock.Object, definition);

            table.Create();
            Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_udt frozen<\"ks1\".\"song\">, PRIMARY KEY (id))", createQuery);
        }
Beispiel #3
0
        public void Create_With_Frozen_Collection_Key()
        {
            string createQuery = null;
            var    sessionMock = new Mock <ISession>();

            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(q => createQuery = q);
            var definition = new Map <UdtAndTuplePoco>()
                             .PartitionKey(c => c.Id1)
                             .Column(c => c.Id1, cm => cm.WithName("id"))
                             .Column(c => c.UdtSet1, cm => cm.WithName("my_set").WithDbType <SortedSet <Song> >().WithFrozenKey())
                             .Column(c => c.TupleMapKey1, cm => cm.WithName("my_map").WithFrozenKey())
                             .ExplicitColumns()
                             .TableName("tbl1");
            var udtInfo = new UdtColumnInfo("song");

            udtInfo.Fields.Add(new ColumnDesc {
                Name = "title", TypeCode = ColumnTypeCode.Ascii
            });
            udtInfo.Fields.Add(new ColumnDesc {
                Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp
            });
            var udtMap = UdtMap.For <Song>();

            udtMap.Build(udtInfo);
            TypeCodec.SetUdtMap("song", udtMap);
            var table = GetTable <UdtAndTuplePoco>(sessionMock.Object, definition);

            table.Create();
            Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_set set<frozen<song>>, my_map map<frozen<tuple<double, double>>, text>, PRIMARY KEY (id))", createQuery);
        }
Beispiel #4
0
 protected override void Build(UdtColumnInfo definition)
 {
     this.Definition = definition;
     if (this._fieldNameToProperty.Count == 0)
     {
         this.Automap();
     }
 }
Beispiel #5
0
        /// <summary>
        /// Parses a given fully-qualified class type name to get the data type information
        /// </summary>
        /// <exception cref="ArgumentException" />
        internal static ColumnDesc ParseFqTypeName(string typeName, int startIndex = 0, int length = 0)
        {
            const StringComparison comparison = StringComparison.Ordinal;
            var dataType = new ColumnDesc();

            if (length == 0)
            {
                length = typeName.Length;
            }
            if (length > ReversedTypeName.Length && typeName.IndexOf(ReversedTypeName, startIndex, comparison) == startIndex)
            {
                //move the start index and subtract the length plus parenthesis
                startIndex         += ReversedTypeName.Length + 1;
                length             -= ReversedTypeName.Length + 2;
                dataType.IsReversed = true;
            }
            if (length > FrozenTypeName.Length && typeName.IndexOf(FrozenTypeName, startIndex, comparison) == startIndex)
            {
                //Remove the frozen
                startIndex       += FrozenTypeName.Length + 1;
                length           -= FrozenTypeName.Length + 2;
                dataType.IsFrozen = true;
            }
            if (typeName == EmptyTypeName)
            {
                dataType.TypeCode = ColumnTypeCode.Custom;
                dataType.TypeInfo = null;
                return(dataType);
            }
            //Quick check if its a single type
            if (length <= SingleFqTypeNamesLength)
            {
                ColumnTypeCode typeCode;
                if (startIndex > 0)
                {
                    typeName = typeName.Substring(startIndex, length);
                }
                if (SingleFqTypeNames.TryGetValue(typeName, out typeCode))
                {
                    dataType.TypeCode = typeCode;
                    return(dataType);
                }
                throw GetTypeException(typeName);
            }
            if (typeName.IndexOf(ListTypeName, startIndex, comparison) == startIndex)
            {
                //Its a list
                //org.apache.cassandra.db.marshal.ListType(innerType)
                //move cursor across the name and bypass the parenthesis
                startIndex += ListTypeName.Length + 1;
                length     -= ListTypeName.Length + 2;
                var innerTypes = ParseParams(typeName, startIndex, length);
                if (innerTypes.Count != 1)
                {
                    throw GetTypeException(typeName);
                }
                dataType.TypeCode = ColumnTypeCode.List;
                var subType = ParseFqTypeName(innerTypes[0]);
                dataType.TypeInfo = new ListColumnInfo()
                {
                    ValueTypeCode = subType.TypeCode,
                    ValueTypeInfo = subType.TypeInfo
                };
                return(dataType);
            }
            if (typeName.IndexOf(SetTypeName, startIndex, comparison) == startIndex)
            {
                //Its a set
                //org.apache.cassandra.db.marshal.SetType(innerType)
                //move cursor across the name and bypass the parenthesis
                startIndex += SetTypeName.Length + 1;
                length     -= SetTypeName.Length + 2;
                var innerTypes = ParseParams(typeName, startIndex, length);
                if (innerTypes.Count != 1)
                {
                    throw GetTypeException(typeName);
                }
                dataType.TypeCode = ColumnTypeCode.Set;
                var subType = ParseFqTypeName(innerTypes[0]);
                dataType.TypeInfo = new SetColumnInfo()
                {
                    KeyTypeCode = subType.TypeCode,
                    KeyTypeInfo = subType.TypeInfo
                };
                return(dataType);
            }
            if (typeName.IndexOf(MapTypeName, startIndex, comparison) == startIndex)
            {
                //org.apache.cassandra.db.marshal.MapType(keyType,valueType)
                //move cursor across the name and bypass the parenthesis
                startIndex += MapTypeName.Length + 1;
                length     -= MapTypeName.Length + 2;
                var innerTypes = ParseParams(typeName, startIndex, length);
                //It should contain the key and value types
                if (innerTypes.Count != 2)
                {
                    throw GetTypeException(typeName);
                }
                dataType.TypeCode = ColumnTypeCode.Map;
                var keyType   = ParseFqTypeName(innerTypes[0]);
                var valueType = ParseFqTypeName(innerTypes[1]);
                dataType.TypeInfo = new MapColumnInfo()
                {
                    KeyTypeCode   = keyType.TypeCode,
                    KeyTypeInfo   = keyType.TypeInfo,
                    ValueTypeCode = valueType.TypeCode,
                    ValueTypeInfo = valueType.TypeInfo
                };
                return(dataType);
            }
            if (typeName.IndexOf(UdtTypeName, startIndex, comparison) == startIndex)
            {
                //move cursor across the name and bypass the parenthesis
                startIndex += UdtTypeName.Length + 1;
                length     -= UdtTypeName.Length + 2;
                var udtParams = ParseParams(typeName, startIndex, length);
                if (udtParams.Count < 2)
                {
                    //It should contain at least the keyspace, name of the udt and a type
                    throw GetTypeException(typeName);
                }
                dataType.TypeCode = ColumnTypeCode.Udt;
                dataType.Keyspace = udtParams[0];
                dataType.Name     = HexToUtf8(udtParams[1]);
                var udtInfo = new UdtColumnInfo(dataType.Keyspace + "." + dataType.Name);
                for (var i = 2; i < udtParams.Count; i++)
                {
                    var p = udtParams[i];
                    var separatorIndex = p.IndexOf(':');
                    var c = ParseFqTypeName(p, separatorIndex + 1, p.Length - (separatorIndex + 1));
                    c.Name = HexToUtf8(p.Substring(0, separatorIndex));
                    udtInfo.Fields.Add(c);
                }
                dataType.TypeInfo = udtInfo;
                return(dataType);
            }
            if (typeName.IndexOf(TupleTypeName, startIndex, comparison) == startIndex)
            {
                //move cursor across the name and bypass the parenthesis
                startIndex += TupleTypeName.Length + 1;
                length     -= TupleTypeName.Length + 2;
                var tupleParams = ParseParams(typeName, startIndex, length);
                if (tupleParams.Count < 1)
                {
                    //It should contain at least the keyspace, name of the udt and a type
                    throw GetTypeException(typeName);
                }
                dataType.TypeCode = ColumnTypeCode.Tuple;
                var tupleInfo = new TupleColumnInfo();
                foreach (var subTypeName in tupleParams)
                {
                    tupleInfo.Elements.Add(ParseFqTypeName(subTypeName));
                }
                dataType.TypeInfo = tupleInfo;
                return(dataType);
            }
            throw GetTypeException(typeName);
        }
 public void Create_With_Frozen_Collection_Value()
 {
     string createQuery = null;
     var sessionMock = new Mock<ISession>();
     sessionMock
         .Setup(s => s.Execute(It.IsAny<string>()))
         .Returns(() => new RowSet())
         .Callback<string>(q => createQuery = q);
     var definition = new Map<UdtAndTuplePoco>()
         .PartitionKey(c => c.Id1)
         .Column(c => c.Id1, cm => cm.WithName("id"))
         .Column(c => c.UdtList1, cm => cm.WithName("my_list").WithFrozenValue())
         .Column(c => c.TupleMapValue1, cm => cm.WithName("my_map").WithFrozenValue())
         .ExplicitColumns()
         .TableName("tbl1");
     var udtInfo = new UdtColumnInfo("song");
     udtInfo.Fields.Add(new ColumnDesc { Name = "title", TypeCode = ColumnTypeCode.Ascii });
     udtInfo.Fields.Add(new ColumnDesc { Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp });
     var udtMap = UdtMap.For<Song>();
     udtMap.Build(udtInfo);
     TypeCodec.SetUdtMap("song", udtMap);
     var table = GetTable<UdtAndTuplePoco>(sessionMock.Object, definition);
     table.Create();
     Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_list list<frozen<song>>, my_map map<text, frozen<tuple<double, double>>>, PRIMARY KEY (id))", createQuery);
 }
 /// <summary>
 /// Parses a given fully-qualified class type name to get the data type information
 /// </summary>
 /// <exception cref="ArgumentException" />
 internal static ColumnDesc ParseFqTypeName(string typeName, int startIndex = 0, int length = 0)
 {
     const StringComparison comparison = StringComparison.Ordinal;
     var dataType = new ColumnDesc();
     if (length == 0)
     {
         length = typeName.Length;
     }
     if (length > ReversedTypeName.Length && typeName.IndexOf(ReversedTypeName, startIndex, comparison) == startIndex)
     {
         //move the start index and subtract the length plus parenthesis
         startIndex += ReversedTypeName.Length + 1;
         length -= ReversedTypeName.Length + 2;
         dataType.IsReversed = true;
     }
     if (length > FrozenTypeName.Length && typeName.IndexOf(FrozenTypeName, startIndex, comparison) == startIndex)
     {
         //Remove the frozen
         startIndex += FrozenTypeName.Length + 1;
         length -= FrozenTypeName.Length + 2;
         dataType.IsFrozen = true;
     }
     if (typeName == EmptyTypeName)
     {
         dataType.TypeCode = ColumnTypeCode.Custom;
         dataType.TypeInfo = null;
         return dataType;
     }
     //Quick check if its a single type
     if (length <= SingleFqTypeNamesLength)
     {
         ColumnTypeCode typeCode;
         if (startIndex > 0)
         {
             typeName = typeName.Substring(startIndex, length);
         }
         if (SingleFqTypeNames.TryGetValue(typeName, out typeCode))
         {
             dataType.TypeCode = typeCode;
             return dataType;
         }
         throw GetTypeException(typeName);
     }
     if (typeName.IndexOf(ListTypeName, startIndex, comparison) == startIndex)
     {
         //Its a list
         //org.apache.cassandra.db.marshal.ListType(innerType)
         //move cursor across the name and bypass the parenthesis
         startIndex += ListTypeName.Length + 1;
         length -= ListTypeName.Length + 2;
         var innerTypes = ParseParams(typeName, startIndex, length);
         if (innerTypes.Count != 1)
         {
             throw GetTypeException(typeName);
         }
         dataType.TypeCode = ColumnTypeCode.List;
         var subType = ParseFqTypeName(innerTypes[0]);
         dataType.TypeInfo = new ListColumnInfo()
         {
             ValueTypeCode = subType.TypeCode,
             ValueTypeInfo = subType.TypeInfo
         };
         return dataType;
     }
     if (typeName.IndexOf(SetTypeName, startIndex, comparison) == startIndex)
     {
         //Its a set
         //org.apache.cassandra.db.marshal.SetType(innerType)
         //move cursor across the name and bypass the parenthesis
         startIndex += SetTypeName.Length + 1;
         length -= SetTypeName.Length + 2;
         var innerTypes = ParseParams(typeName, startIndex, length);
         if (innerTypes.Count != 1)
         {
             throw GetTypeException(typeName);
         }
         dataType.TypeCode = ColumnTypeCode.Set;
         var subType = ParseFqTypeName(innerTypes[0]);
         dataType.TypeInfo = new SetColumnInfo()
         {
             KeyTypeCode = subType.TypeCode,
             KeyTypeInfo = subType.TypeInfo
         };
         return dataType;
     }
     if (typeName.IndexOf(MapTypeName, startIndex, comparison) == startIndex)
     {
         //org.apache.cassandra.db.marshal.MapType(keyType,valueType)
         //move cursor across the name and bypass the parenthesis
         startIndex += MapTypeName.Length + 1;
         length -= MapTypeName.Length + 2;
         var innerTypes = ParseParams(typeName, startIndex, length);
         //It should contain the key and value types
         if (innerTypes.Count != 2)
         {
             throw GetTypeException(typeName);
         }
         dataType.TypeCode = ColumnTypeCode.Map;
         var keyType = ParseFqTypeName(innerTypes[0]);
         var valueType = ParseFqTypeName(innerTypes[1]);
         dataType.TypeInfo = new MapColumnInfo()
         {
             KeyTypeCode = keyType.TypeCode,
             KeyTypeInfo = keyType.TypeInfo,
             ValueTypeCode = valueType.TypeCode,
             ValueTypeInfo = valueType.TypeInfo
         };
         return dataType;
     }
     if (typeName.IndexOf(UdtTypeName, startIndex, comparison) == startIndex)
     {
         //move cursor across the name and bypass the parenthesis
         startIndex += UdtTypeName.Length + 1;
         length -= UdtTypeName.Length + 2;
         var udtParams = ParseParams(typeName, startIndex, length);
         if (udtParams.Count < 2)
         {
             //It should contain at least the keyspace, name of the udt and a type
             throw GetTypeException(typeName);
         }
         dataType.TypeCode = ColumnTypeCode.Udt;
         dataType.Keyspace = udtParams[0];
         dataType.Name = HexToUtf8(udtParams[1]);
         var udtInfo = new UdtColumnInfo(dataType.Keyspace + "." + dataType.Name);
         for (var i = 2; i < udtParams.Count; i++)
         {
             var p = udtParams[i];
             var separatorIndex = p.IndexOf(':');
             var c = ParseFqTypeName(p, separatorIndex + 1, p.Length - (separatorIndex + 1));
             c.Name = HexToUtf8(p.Substring(0, separatorIndex));
             udtInfo.Fields.Add(c);
         }
         dataType.TypeInfo = udtInfo;
         return dataType;
     }
     if (typeName.IndexOf(TupleTypeName, startIndex, comparison) == startIndex)
     {
         //move cursor across the name and bypass the parenthesis
         startIndex += TupleTypeName.Length + 1;
         length -= TupleTypeName.Length + 2;
         var tupleParams = ParseParams(typeName, startIndex, length);
         if (tupleParams.Count < 1)
         {
             //It should contain at least the keyspace, name of the udt and a type
             throw GetTypeException(typeName);
         }
         dataType.TypeCode = ColumnTypeCode.Tuple;
         var tupleInfo = new TupleColumnInfo();
         foreach (var subTypeName in tupleParams)
         {
             tupleInfo.Elements.Add(ParseFqTypeName(subTypeName));
         }
         dataType.TypeInfo = tupleInfo;
         return dataType;
     }
     throw GetTypeException(typeName);
 }
 public void Create_With_Frozen_Udt()
 {
     string createQuery = null;
     var serializer = new Serializer(4);
     var sessionMock = GetSessionMock(serializer);
     sessionMock
         .Setup(s => s.Execute(It.IsAny<string>()))
         .Returns(() => new RowSet())
         .Callback<string>(q => createQuery = q);
     var definition = new Map<UdtAndTuplePoco>()
         .PartitionKey(c => c.Id1)
         .Column(c => c.Id1, cm => cm.WithName("id"))
         .Column(c => c.Udt1, cm => cm.WithName("my_udt").WithDbType<Song>().AsFrozen())
         .ExplicitColumns()
         .TableName("tbl1");
     var udtInfo = new UdtColumnInfo("song");
     udtInfo.Fields.Add(new ColumnDesc { Name = "title", TypeCode = ColumnTypeCode.Ascii });
     udtInfo.Fields.Add(new ColumnDesc { Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp });
     var udtMap = UdtMap.For<Song>();
     udtMap.SetSerializer(serializer);
     udtMap.Build(udtInfo);
     serializer.SetUdtMap("song", udtMap);
     var table = GetTable<UdtAndTuplePoco>(sessionMock.Object, definition);
     table.Create();
     Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_udt frozen<song>, PRIMARY KEY (id))", createQuery);
 }