/**
         * Create sharding conditions.
         *
         * @param insertCommandContext insert statement context
         * @param parameters SQL parameters
         * @return sharding conditions
         */
        public List <ShardingCondition> CreateShardingConditions(InsertCommandContext insertCommandContext,
                                                                 ParameterContext parameterContext)
        {
            ICollection <ShardingCondition> result = new LinkedList <ShardingCondition>();
            string tableName = insertCommandContext.GetSqlCommand().Table.GetTableName().GetIdentifier().GetValue();
            ICollection <string> columnNames = GetColumnNames(insertCommandContext);

            foreach (var insertValueContext in insertCommandContext.GetInsertValueContexts())
            {
                result.Add(CreateShardingCondition(tableName, columnNames.GetEnumerator(), insertValueContext,
                                                   parameterContext));
            }

            var generatedKey = insertCommandContext.GetGeneratedKeyContext();

            if (generatedKey != null && generatedKey.IsGenerated())
            {
                generatedKey.GetGeneratedValues().AddAll(GetGeneratedKeys(tableName,
                                                                          insertCommandContext.GetSqlCommand().GetValueListCount()));
                if (_shardingRule.IsShardingColumn(generatedKey.GetColumnName(), tableName))
                {
                    AppendGeneratedKeyCondition(generatedKey, tableName, result);
                }
            }

            return(result.ToList());
        }
        public override SqlToken GenerateSqlToken(InsertCommandContext sqlCommandContext)
        {
            var generatedKey = sqlCommandContext.GetGeneratedKeyContext();

            ShardingAssert.ShouldBeNotNull(generatedKey, "generatedKey is required");
            ShardingAssert.ShouldBeNotNull(sqlCommandContext.GetSqlCommand().SetAssignment, "setAssignment is required");
            int startIndex = sqlCommandContext.GetSqlCommand().SetAssignment.GetStopIndex() + 1;

            if (_parameterContext.IsEmpty())
            {
                return(new LiteralGeneratedKeyAssignmentToken(startIndex, generatedKey.GetColumnName(), generatedKey.GetGeneratedValues().LastOrDefault()));
            }
            return(new ParameterMarkerGeneratedKeyAssignmentToken(startIndex, generatedKey.GetColumnName()));
        }
Esempio n. 3
0
        public override SqlToken GenerateSqlToken(InsertCommandContext sqlCommandContext)
        {
            var insertColumnsSegment = sqlCommandContext.GetSqlCommand().InsertColumns;

            ShardingAssert.ShouldBeNotNull(insertColumnsSegment, "insertColumnsSegment is required");
            return(new UseDefaultInsertColumnsToken(insertColumnsSegment.GetStopIndex(), GetColumnNames(sqlCommandContext)));
        }
Esempio n. 4
0
        public override SqlToken GenerateSqlToken(InsertCommandContext sqlCommandContext)
        {
            var generatedKey = sqlCommandContext.GetGeneratedKeyContext();

            ShardingAssert.ShouldBeNotNull(generatedKey, "generatedKey is required");
            var sqlSegment = sqlCommandContext.GetSqlCommand().InsertColumns;

            ShardingAssert.ShouldBeNotNull(sqlSegment, "sqlSegment is required");
            return(new GeneratedKeyInsertColumnToken(sqlSegment.GetStopIndex(), generatedKey.GetColumnName()));
        }