Пример #1
0
        public Task InsertAsync <T>(T poco, CqlQueryOptions queryOptions = null)
        {
            // Get statement and bind values from POCO
            string             cql           = _cqlGenerator.GenerateInsert <T>();
            Func <T, object[]> getBindValues = _mapperFactory.GetValueCollector <T>(cql);

            object[] values = getBindValues(poco);

            return(ExecuteAsync(Cql.New(cql, values, queryOptions ?? CqlQueryOptions.None)));
        }
Пример #2
0
        /// <inheritdoc />
        public Task InsertAsync <T>(T poco, bool insertNulls, int?ttl, CqlQueryOptions queryOptions = null)
        {
            var pocoData        = _mapperFactory.PocoDataFactory.GetPocoData <T>();
            var queryIdentifier = string.Format("INSERT ID {0}/{1}", pocoData.KeyspaceName, pocoData.TableName);
            var getBindValues   = _mapperFactory.GetValueCollector <T>(queryIdentifier);
            //get values first to identify null values
            var values = getBindValues(poco);

            object[] queryParameters;
            //generate INSERT query based on null values (if insertNulls set)
            var cql = _cqlGenerator.GenerateInsert <T>(insertNulls, values, out queryParameters, ttl: ttl);

            return(ExecuteAsync(Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None)));
        }
Пример #3
0
        /// <inheritdoc />
        public Task InsertAsync <T>(T poco, string executionProfile, bool insertNulls, int?ttl, CqlQueryOptions queryOptions = null)
        {
            if (executionProfile == null)
            {
                throw new ArgumentNullException(nameof(executionProfile));
            }

            var pocoData        = _mapperFactory.PocoDataFactory.GetPocoData <T>();
            var queryIdentifier = $"INSERT ID {pocoData.KeyspaceName}/{pocoData.TableName}";
            var getBindValues   = _mapperFactory.GetValueCollector <T>(queryIdentifier);
            //get values first to identify null values
            var values = getBindValues(poco);
            //generate INSERT query based on null values (if insertNulls set)
            var cql         = _cqlGenerator.GenerateInsert <T>(insertNulls, values, out var queryParameters, ttl: ttl);
            var cqlInstance = Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None).WithExecutionProfile(executionProfile);

            return(ExecuteAsync(cqlInstance));
        }
Пример #4
0
        private void Insert <T>(bool ifNotExists, T poco, CqlQueryOptions queryOptions = null)
        {
            // Get statement and bind values from POCO
            string             cql           = _cqlGenerator.GenerateInsert <T>(ifNotExists);
            Func <T, object[]> getBindValues = _mapperFactory.GetValueCollector <T>(cql);

            object[] values = getBindValues(poco);

            _statements.Add(Cql.New(cql, values, queryOptions ?? CqlQueryOptions.None));
        }
Пример #5
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);
        }
Пример #6
0
        private void Insert <T>(bool ifNotExists, bool insertNulls, T poco, CqlQueryOptions queryOptions = null, int?ttl = null)
        {
            var pocoData        = _mapperFactory.PocoDataFactory.GetPocoData <T>();
            var queryIdentifier = $"INSERT ID {pocoData.KeyspaceName}/{pocoData.TableName}";
            var getBindValues   = _mapperFactory.GetValueCollector <T>(queryIdentifier);
            //get values first to identify null values
            var values = getBindValues(poco);
            //generate INSERT query based on null values (if insertNulls set)
            var cql         = _cqlGenerator.GenerateInsert <T>(insertNulls, values, out var queryParameters, ifNotExists, ttl);
            var cqlInstance = Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None);

            _statements.Add(cqlInstance);
        }
Пример #7
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);
        }
Пример #8
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));
        }
Пример #9
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);
        }
Пример #10
0
        private void Insert <T>(bool ifNotExists, bool insertNulls, T poco, CqlQueryOptions queryOptions = null)
        {
            var pocoData        = _mapperFactory.PocoDataFactory.GetPocoData <T>();
            var queryIdentifier = string.Format("INSERT ID {0}/{1}", pocoData.KeyspaceName, pocoData.TableName);
            var getBindValues   = _mapperFactory.GetValueCollector <T>(queryIdentifier);
            //get values first to identify null values
            var values = getBindValues(poco);

            //generate INSERT query based on null values (if insertNulls set)
            object[] queryParameters;
            var      cql = _cqlGenerator.GenerateInsert <T>(insertNulls, values, out queryParameters, ifNotExists);

            _statements.Add(Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None));
        }
Пример #11
0
        protected internal override string GetCql(out object[] values)
        {
            var pocoData        = _mapperFactory.PocoDataFactory.GetPocoData <TEntity>();
            var queryIdentifier = $"INSERT LINQ ID {Table.KeyspaceName}/{Table.Name}";
            var getBindValues   = _mapperFactory.GetValueCollector <TEntity>(queryIdentifier);
            //get values first to identify null values
            var pocoValues = getBindValues(_entity);
            //generate INSERT query based on null values (if insertNulls set)
            var cqlGenerator = new CqlGenerator(_mapperFactory.PocoDataFactory);
            //Use the table name from Table<TEntity> instance instead of PocoData
            var tableName = CqlInsert <TEntity> .CqlIdentifierHelper.EscapeTableNameIfNecessary(pocoData, Table.KeyspaceName, Table.Name);

            return(cqlGenerator.GenerateInsert <TEntity>(
                       _insertNulls, pocoValues, out values, _ifNotExists, _ttl, _timestamp, tableName));
        }
Пример #12
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);
 }
Пример #13
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));
 }
Пример #14
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);
        }
Пример #15
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);
 }