public PutStatementCommandValidator(IMediator mediator) { _mediator = mediator; RuleFor(x => x.StatementId).NotEmpty(); RuleFor(x => x.Statement).SetValidator(new StatementValidator()); RuleFor(x => x).Must((request) => { return(!request.Statement.Id.HasValue || request.StatementId == request.Statement.Id.Value); }) .WithErrorCode("409") .WithMessage("The 'id' property of the Statement MUST match the 'statementId' parameter of the request.") .WithName("request"); RuleFor(x => x).MustAsync(async(request, cancellationToken) => { var savedStatement = await _mediator.Send(StatementQuery.Create(request.StatementId), cancellationToken); return(savedStatement == null || !savedStatement.Equals(request.Statement)); }) .WithErrorCode("409") .WithMessage("A stored statement with the of 'statementId' parameter doest not match request statement.") .WithName("request"); }
public Cursor(StatementQuery queryStatement) : base(100) { base.nodes = Expression.emptyArray; this._queryStatement = queryStatement; base.DataType = SqlType.SqlAllTypes; this._state = CursorState.Closed; this._result = null; this._navigator = null; }
public void Add(NormalizedStatement normalizedStatement, StatementQuery query, StatementQueryExtractedData queryExtractedData) { if (!normalizedStatementQueriesData.ContainsKey(normalizedStatement.ID)) { normalizedStatementQueriesData.Add(normalizedStatement.ID, new List <StatementQueryExtractedData>()); } normalizedStatementQueriesData[normalizedStatement.ID].Add(queryExtractedData); dataPerQuery.Add(new NormalizedStatementQueryPair(normalizedStatement.ID, query), queryExtractedData); }
public void TryAddPossibleCoveringIndices(IEnumerable <IndexDefinition> indexDefinitions, NormalizedStatement normalizedStatement, StatementQuery query) { TryAddPossibleIndices(indexDefinitions, normalizedStatement, query); var queryKey = new NormalizedStatementQueryPair(normalizedStatement.ID, query); if (!allCoveringPerQuery.ContainsKey(queryKey)) { allCoveringPerQuery.Add(queryKey, new HashSet <IndexDefinition>()); } foreach (var indexDefinition in indexDefinitions) { allCoveringPerQuery[queryKey].Add(indexDefinition); } }
/// <inheritdoc/> public async Task <Statement> GetStatement(Guid statementId, bool attachments, ResultFormat format, CancellationToken cancellationToken = default) { var stmt = await mediator.Send(StatementQuery.Create(statementId, attachments, format), cancellationToken); return(mapper.Map <Statement>(stmt)); }
private void GenerateIndices(StatementQueryExtractedData extractedData, NormalizedStatement normalizedStatement, StatementQuery query) { var possibleIndices = context.IndicesDesignData.PossibleIndices; var whereJoinAttributesToUse = GroupAttributesByRelationAndPrepare(extractedData.WhereAttributes.BTreeApplicable.Union(extractedData.JoinAttributes.BTreeApplicable)); var groupByAttributesToUse = GroupAttributesByRelationAndPrepare(extractedData.GroupByAttributes.BTreeApplicable); var orderByAttributesToUse = GroupAttributesByRelationAndPrepare(extractedData.OrderByAttributes.BTreeApplicable); // order by indices foreach (var kv in orderByAttributesToUse) { var relation = kv.Key; var attributes = kv.Value; var permutations = GenerateAllPermutations(relation, attributes); possibleIndices.TryAddPossibleIndices(permutations.AllPermutations, normalizedStatement, query); } // group by indices foreach (var kv in groupByAttributesToUse) { var relation = kv.Key; var attributes = kv.Value; var permutations = GenerateAllPermutations(relation, attributes); possibleIndices.TryAddPossibleIndices(permutations.AllPermutations, normalizedStatement, query); } // where U join, where U join + group by, where U join + order by // note: where U join + group by + order by is not needed (any atrribute in order by must be already in group by) foreach (var kv in whereJoinAttributesToUse) { var relation = kv.Key; var attributes = kv.Value; var whereJoinPermutations = GenerateAllPermutations(relation, attributes); // where U join possibleIndices.TryAddPossibleIndices(whereJoinPermutations.AllPermutations, normalizedStatement, query); if (whereJoinPermutations.LastGenerationNumber < settings.IndexMaxAttributesCount) { var maxAttributesCountToConcat = settings.IndexMaxAttributesCount - whereJoinPermutations.LastGenerationNumber; // where U join + group by if (groupByAttributesToUse.ContainsKey(relation)) { var attributesToConcat = groupByAttributesToUse[relation].Except(whereJoinAttributesToUse[relation]).OrderBy(x => x.CardinalityIndicator).Take(maxAttributesCountToConcat).ToHashSet(); var permutationsToConcat = GenerateAllPermutations(relation, attributesToConcat); var newPermutations = ConcatPermutations(whereJoinPermutations.AllPermutations, permutationsToConcat.AllPermutations); possibleIndices.TryAddPossibleIndices(newPermutations, normalizedStatement, query); } // where U join + order by if (orderByAttributesToUse.ContainsKey(relation)) { var attributesToConcat = orderByAttributesToUse[relation].Except(whereJoinAttributesToUse[relation]).OrderBy(x => x.CardinalityIndicator).Take(maxAttributesCountToConcat).ToHashSet(); var permutationsToConcat = GenerateAllPermutations(relation, attributesToConcat); var newPermutations = ConcatPermutations(whereJoinPermutations.AllPermutations, permutationsToConcat.AllPermutations); possibleIndices.TryAddPossibleIndices(newPermutations, normalizedStatement, query); } } } }
public NormalizedStatementQueryPair(long normalizedStatementID, StatementQuery query) { NormalizedStatementID = normalizedStatementID; Query = query; }
protected override void OnExecute() { if (context.QueryTrees.Count > 0) { StatementDefinition definition = new StatementDefinition(); Dictionary <string, StatementQuery> independentQueries = new Dictionary <string, StatementQuery>(); foreach (var tree in context.QueryTrees) { var commandType = Convert(tree.CommandType); if (commandTypeImportance[commandType] > commandTypeImportance[definition.CommandType]) { definition.CommandType = commandType; } foreach (var query in tree.IndependentQueries) { var toAdd = new StatementQuery(); toAdd.CommandType = Convert(query.CommandType); StringBuilder fingerprintBuilder = new StringBuilder(); fingerprintBuilder.Append("_" + toAdd.CommandType.ToString()); foreach (var expr in query.GroupByExpressions) { var e = Convert(expr); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.GroupByExpressions.Add(e); } foreach (var expr in query.HavingExpressions) { var e = Convert(expr); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.HavingExpressions.Add(e); } foreach (var expr in query.JoinExpressions) { var e = Convert(expr); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.JoinExpressions.Add(e); } foreach (var expr in query.OrderByExpressions) { var e = Convert(expr); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.OrderByExpressions.Add(e); } foreach (var a in query.ProjectionAttributes) { var e = Convert(a); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.ProjectionAttributes.Add(e); } foreach (var r in query.Relations) { var e = Convert(r); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.Relations.Add(e); } foreach (var expr in query.WhereExpressions) { var e = Convert(expr); fingerprintBuilder.Append("_" + e.CalculateFingerprint()); toAdd.WhereExpressions.Add(e); } using (var sha = SHA1.Create()) { toAdd.Fingerprint = System.Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(fingerprintBuilder.ToString()))); } if (!independentQueries.ContainsKey(toAdd.Fingerprint)) { independentQueries.Add(toAdd.Fingerprint, toAdd); } } } definition.IndependentQueries.AddRange(independentQueries.Values); definition.Fingerprint = CalculateFingerprint(definition); statementDataAccumulator.PublishNormalizedStatementDefinition(context.StatementData.NormalizedStatementFingerprint, definition); } else { StatementDefinition definition = new StatementDefinition(); definition.CommandType = context.StatementData.CommandType; statementDataAccumulator.PublishNormalizedStatementDefinition(context.StatementData.NormalizedStatementFingerprint, definition); } }