示例#1
0
        /// <summary>
        /// Specifies an individual mapping definition.  Usually used along with the <see cref="Map{TPoco}"/> class which
        /// allows you to define mappings with a fluent interface.  Will throw if a mapping has already been defined for a
        /// given POCO Type.
        /// </summary>
        public MappingConfiguration Define(params ITypeDefinition[] maps)
        {
            if (maps == null) return this;

            foreach (var typeDefinition in maps)
            {
                _typeDefinitions.Add(typeDefinition);
            }
            return this;
        }
示例#2
0
        /// <summary>
        /// Configures CqlPoco to use the collections of <see cref="Mappings"/> specified.  Users should sub-class the <see cref="Mappings"/>
        /// class and use the fluent interface there to define mappings for POCOs.
        /// </summary>
        public CqlClientConfiguration UseMappings(params Mappings[] mappings)
        {
            if (mappings == null)
            {
                return(this);
            }

            foreach (Mappings mapping in mappings)
            {
                foreach (ITypeDefinition typeDefinition in mapping.Definitions)
                {
                    _typeDefinitions.Add(typeDefinition);
                }
            }
            return(this);
        }
 public void GenerateUpdate_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId).Column(u => u.UserAge, cm => cm.WithName("AGE")));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     var cql = cqlGenerator.GenerateUpdate<ExplicitColumnsUser>();
     Assert.AreEqual("UPDATE users SET Name = ?, AGE = ? WHERE UserId = ?", cql);
 }
 public void PrependUpdate_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     var cql = Cql.New("SET Name = ? WHERE UserId = ?", "New name", Guid.Empty);
     cqlGenerator.PrependUpdate<ExplicitColumnsUser>(cql);
     Assert.AreEqual("UPDATE users SET Name = ? WHERE UserId = ?", cql.Statement);
 }
 public void AddSelect_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId).Column(u => u.UserAge, cm => cm.WithName("AGE")));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     var cql = Cql.New("WHERE UserId = ?", Guid.Empty);
     cqlGenerator.AddSelect<ExplicitColumnsUser>(cql);
     Assert.AreEqual("SELECT UserId, Name, AGE FROM users WHERE UserId = ?", cql.Statement);
 }
示例#6
0
        /// <summary>
        /// Adds a mapping for the Poco type specified (TPoco).
        /// </summary>
        public Map <TPoco> For <TPoco>()
        {
            if (Definitions.TryGetItem(typeof(TPoco), out ITypeDefinition map) == false)
            {
                map = new Map <TPoco>();
                Definitions.Add(map);
            }

            return((Map <TPoco>)map);
        }
        public void GenerateDelete_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>().TableName("USERS").PartitionKey(u => u.UserId));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = cqlGenerator.GenerateDelete <ExplicitColumnsUser>();

            Assert.AreEqual("DELETE FROM USERS WHERE UserId = ?", cql);
        }
        public void GenerateUpdate_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId).Column(u => u.UserAge, cm => cm.WithName("AGE")));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = cqlGenerator.GenerateUpdate <ExplicitColumnsUser>();

            Assert.AreEqual("UPDATE users SET Name = ?, AGE = ? WHERE UserId = ?", cql);
        }
        public void PrependUpdate_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = Cql.New("SET Name = ? WHERE UserId = ?", "New name", Guid.Empty);

            cqlGenerator.PrependUpdate <ExplicitColumnsUser>(cql);
            Assert.AreEqual("UPDATE users SET Name = ? WHERE UserId = ?", cql.Statement);
        }
        public void AddSelect_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>().TableName("users").PartitionKey(u => u.UserId).Column(u => u.UserAge, cm => cm.WithName("AGE")));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = Cql.New("WHERE UserId = ?", Guid.Empty);

            cqlGenerator.AddSelect <ExplicitColumnsUser>(cql);
            Assert.AreEqual("SELECT UserId, Name, AGE FROM users WHERE UserId = ?", cql.Statement);
        }
示例#11
0
        public void GenerateInsert_CaseSensitive_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>()
                      .TableName("USERS")
                      .PartitionKey(u => u.UserId)
                      .CaseSensitive());
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = cqlGenerator.GenerateInsert <ExplicitColumnsUser>(true, new object[0], out object[] queryParameters);

            Assert.AreEqual(@"INSERT INTO ""USERS"" (""Name"", ""UserAge"", ""UserId"") VALUES (?, ?, ?)", cql);
        }
示例#12
0
        public void GenerateInsert_Without_Nulls_Should_Throw_When_Value_Length_Dont_Match_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>()
                      .TableName("USERS")
                      .PartitionKey("ID")
                      .Column(u => u.UserId, cm => cm.WithName("ID")));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);

            Assert.Throws <ArgumentException>(() =>
                                              cqlGenerator.GenerateInsert <ExplicitColumnsUser>(false, new object[] { Guid.NewGuid() }, out object[] queryParameters));
        }
示例#13
0
        public void GenerateInsert_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>()
                      .TableName("USERS")
                      .PartitionKey("ID")
                      .Column(u => u.UserId, cm => cm.WithName("ID")));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = cqlGenerator.GenerateInsert <ExplicitColumnsUser>(true, new object[0], out object[] queryParameters);

            Assert.AreEqual(@"INSERT INTO USERS (ID, Name, UserAge) VALUES (?, ?, ?)", cql);
        }
        public void GenerateDelete_CaseSensitive_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>()
                      .TableName("USERS")
                      .PartitionKey("ID")
                      .Column(u => u.UserId, cm => cm.WithName("ID"))
                      .CaseSensitive());
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var cql          = cqlGenerator.GenerateDelete <ExplicitColumnsUser>();

            Assert.AreEqual(@"DELETE FROM ""USERS"" WHERE ""ID"" = ?", cql);
        }
示例#15
0
        public void GenerateInsert_Without_Nulls_First_Value_Null_Test()
        {
            var types = new LookupKeyedCollection <Type, ITypeDefinition>(td => td.PocoType);

            types.Add(new Map <ExplicitColumnsUser>()
                      .TableName("USERS")
                      .PartitionKey("ID")
                      .Column(u => u.UserId, cm => cm.WithName("ID")));
            var pocoFactory  = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var values       = new object[] { null, "name", 100 };
            var cql          = cqlGenerator.GenerateInsert <ExplicitColumnsUser>(false, values, out object[] queryParameters);

            Assert.AreEqual(@"INSERT INTO USERS (Name, UserAge) VALUES (?, ?)", cql);
            CollectionAssert.AreEqual(values.Where(v => v != null), queryParameters);

            cql = cqlGenerator.GenerateInsert <ExplicitColumnsUser>(false, values, out queryParameters, true);
            Assert.AreEqual(@"INSERT INTO USERS (Name, UserAge) VALUES (?, ?) IF NOT EXISTS", cql);
            CollectionAssert.AreEqual(values.Where(v => v != null), queryParameters);
        }
示例#16
0
 public void GenerateDelete_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>().TableName("USERS").PartitionKey(u => u.UserId));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     var cql = cqlGenerator.GenerateDelete<ExplicitColumnsUser>();
     Assert.AreEqual("DELETE FROM USERS WHERE UserId = ?", cql);
 }
示例#17
0
 public void GenerateDelete_CaseSensitive_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>()
         .TableName("USERS")
         .PartitionKey("ID")
         .Column(u => u.UserId, cm => cm.WithName("ID"))
         .CaseSensitive());
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     var cql = cqlGenerator.GenerateDelete<ExplicitColumnsUser>();
     Assert.AreEqual(@"DELETE FROM ""USERS"" WHERE ""ID"" = ?", cql);
 }
示例#18
0
 public void GenerateInsert_CaseSensitive_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>()
         .TableName("USERS")
         .PartitionKey(u => u.UserId)
         .CaseSensitive());
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     object[] queryParameters;
     var cql = cqlGenerator.GenerateInsert<ExplicitColumnsUser>(true, new object[0], out queryParameters);
     Assert.AreEqual(@"INSERT INTO ""USERS"" (""UserId"", ""Name"", ""UserAge"") VALUES (?, ?, ?)", cql);
 }
示例#19
0
 public void GenerateInsert_Without_Nulls_Should_Throw_When_Value_Length_Dont_Match_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>()
         .TableName("USERS")
         .PartitionKey("ID")
         .Column(u => u.UserId, cm => cm.WithName("ID")));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     object[] queryParameters;
     Assert.Throws<ArgumentException>(() =>
         cqlGenerator.GenerateInsert<ExplicitColumnsUser>(false, new object[] { Guid.NewGuid()}, out queryParameters));
 }
示例#20
0
        public void GenerateInsert_Without_Nulls_First_Value_Null_Test()
        {
            var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
            types.Add(new Map<ExplicitColumnsUser>()
                .TableName("USERS")
                .PartitionKey("ID")
                .Column(u => u.UserId, cm => cm.WithName("ID")));
            var pocoFactory = new PocoDataFactory(types);
            var cqlGenerator = new CqlGenerator(pocoFactory);
            var values = new object[] { null, "name", 100 };
            object[] queryParameters;
            var cql = cqlGenerator.GenerateInsert<ExplicitColumnsUser>(false, values, out queryParameters);
            Assert.AreEqual(@"INSERT INTO USERS (Name, UserAge) VALUES (?, ?)", cql);
            CollectionAssert.AreEqual(values.Where(v => v != null), queryParameters);

            cql = cqlGenerator.GenerateInsert<ExplicitColumnsUser>(false, values, out queryParameters, true);
            Assert.AreEqual(@"INSERT INTO USERS (Name, UserAge) VALUES (?, ?) IF NOT EXISTS", cql);
            CollectionAssert.AreEqual(values.Where(v => v != null), queryParameters);
        }
示例#21
0
 public void GenerateInsert_Test()
 {
     var types = new LookupKeyedCollection<Type, ITypeDefinition>(td => td.PocoType);
     types.Add(new Map<ExplicitColumnsUser>()
         .TableName("USERS")
         .PartitionKey("ID")
         .Column(u => u.UserId, cm => cm.WithName("ID")));
     var pocoFactory = new PocoDataFactory(types);
     var cqlGenerator = new CqlGenerator(pocoFactory);
     object[] queryParameters;
     var cql = cqlGenerator.GenerateInsert<ExplicitColumnsUser>(true, new object[0], out queryParameters);
     Assert.AreEqual(@"INSERT INTO USERS (ID, Name, UserAge) VALUES (?, ?, ?)", cql);
 }