예제 #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);
        }
예제 #3
0
 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);
 }
예제 #4
0
 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);
 }
예제 #5
0
 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);
 }