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()); }
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())); }
private List <String> GetColumnNames(InsertCommandContext insertCommandContext) { var generatedKey = insertCommandContext.GetGeneratedKeyContext(); ShardingAssert.ShouldBeNotNull(generatedKey, "generatedKey is required"); List <String> result = new List <string>(insertCommandContext.GetColumnNames()); result.Remove(generatedKey.GetColumnName()); result.Add(generatedKey.GetColumnName()); return(result); }
private ICollection <string> GetColumnNames(InsertCommandContext insertCommandContext) { var generatedKey = insertCommandContext.GetGeneratedKeyContext(); if (generatedKey != null && generatedKey.IsGenerated()) { ICollection <string> result = new LinkedList <string>(insertCommandContext.GetColumnNames()); result.Remove(generatedKey.GetColumnName()); return(result); } return(insertCommandContext.GetColumnNames()); }
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())); }