public void PublishNormalizedStatementDefinition(string fingerprint, StatementDefinition definition) { lock (lockObject) { statements[fingerprint].CommandType = definition.CommandType; statements[fingerprint].StatementDefinition = definition; } }
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); } }
private string CalculateFingerprint(StatementDefinition definition) { return($"{definition.CommandType}_{String.Join("_", definition.IndependentQueries.Select(x => x.Fingerprint))}"); }