Beispiel #1
0
        public override SqlToken GenerateSqlToken(InsertCommandContext sqlCommandContext)
        {
            var result = FindPreviousSqlToken();

            ShardingAssert.ShouldBeNotNull(result, "not find PreviousSqlToken");
            var generatedKey = sqlCommandContext.GetGeneratedKeyContext();

            ShardingAssert.ShouldBeNotNull(generatedKey, "generatedKey is required");
            var generatedValues = generatedKey.GetGeneratedValues().Reverse().GetEnumerator();
            int count           = 0;

            foreach (var insertValueContext in sqlCommandContext.GetInsertValueContexts())
            {
                var insertValueToken = result.InsertValues[count];
                IDerivedSimpleExpressionSegment expressionSegment;
                if (IsToAddDerivedLiteralExpression(sqlCommandContext, count))
                {
                    expressionSegment = new DerivedLiteralExpressionSegment(generatedValues.Next());
                }
                else
                {
                    expressionSegment = new DerivedParameterMarkerExpressionSegment(insertValueContext.GetParametersCount(), string.Empty);
                }
                insertValueToken.GetValues().Add(expressionSegment);
                count++;
            }
            return(result);
        }
        /**
         * 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());
        }