public List<Replacements> GetReplacements(List<QuerySpecification> queries) { var replacements = new List<Replacements>(); if (queries.Count < 1) return replacements; //as we iterate through tokens we only need the outermost query... var spec = queries.First(); TSqlParserToken lastToken = null; foreach (var token in spec.ScriptTokenStream) { if (token.TokenType == TSqlTokenType.EqualsSign && lastToken != null && lastToken.TokenType == TSqlTokenType.Bang) { var replacement = new Replacements(); var length = (token.Offset + token.Text.Length) - lastToken.Offset; replacement.Original = _script.Substring(lastToken.Offset, length); replacement.OriginalFragment = spec; replacement.OriginalLength = length; replacement.OriginalOffset = lastToken.Offset; replacement.Replacement = "<>"; replacements.Add(replacement); lastToken = token; } switch (token.TokenType) { case TSqlTokenType.WhiteSpace: case TSqlTokenType.MultilineComment: case TSqlTokenType.SingleLineComment: break; default: lastToken = token; break; } } return replacements; }
public List<Replacements> GetReplacements(List<DeleteSpecification> statements) { var replacements = new List<Replacements>(); foreach (var delete in statements) { if (delete.TopRowFilter == null) { var newStatement = buildChunkedDelete(delete); var replacement = new Replacements(); replacement.Original = _script.Substring(delete.StartOffset, delete.FragmentLength); replacement.OriginalFragment = delete; replacement.OriginalLength = delete.FragmentLength; replacement.OriginalOffset = delete.StartOffset; replacement.Replacement = newStatement; replacements.Add(replacement); } } return replacements; }
public List<Replacements> GetReplacements(List<TableDescriptor> dacTables) { var replacements = new List<Replacements>(); foreach (var table in _tables) { var addedDbo = false; var tableName = table.SchemaObject; if (table.SchemaObject.SchemaIdentifier == null) { tableName.Identifiers.Insert(0,new Identifier() {Value = "dbo"});; addedDbo = true; } var dacTable = dacTables.FirstOrDefault(p => p.Name.EqualsObjectIdentifierInsensitive(tableName) ); if (dacTable == null) continue; if (!dacTable.Name.EqualsObjectIdentifier(tableName)) { //same table different case var replacement = new Replacements(); replacement.OriginalOffset = table.StartOffset; replacement.OriginalFragment = table; replacement.OriginalLength = table.FragmentLength; replacement.Replacement = addedDbo ? dacTable.Name.GetName().CorrectQuote(tableName.BaseIdentifier.QuoteType) : string.Format("{0}.{1}", dacTable.Name.GetSchema().CorrectQuote(tableName.SchemaIdentifier.QuoteType), dacTable.Name.GetName().CorrectQuote(tableName.BaseIdentifier.QuoteType)); replacements.Add(replacement); } if (addedDbo) tableName.Identifiers.RemoveAt(0); } return replacements; }
public List<Replacements> GetReplacements(List<QuerySpecification> queries) { var replacements = new List<Replacements>(); var uniquifier = new Dictionary<string, object>(); foreach (var spec in queries) { var key = string.Format("{0}:{1}", spec.StartOffset, spec.FragmentLength); if (uniquifier.ContainsKey(key)) continue; uniquifier.Add(key, null); if (spec.OrderByClause == null) continue; if (spec.OrderByClause.OrderByElements.Any(p => p.Expression is IntegerLiteral)) { var integerOrderClauses = spec.OrderByClause.OrderByElements.Where(p => p.Expression is IntegerLiteral); var ordinalNames = spec.SelectElements; foreach (var ordinal in integerOrderClauses) { var position = Int32.Parse((ordinal.Expression as IntegerLiteral).Value); for (var i = 0; i < ordinalNames.Count; i++) { if (ordinalNames[i] is SelectStarExpression) return null; //can't re-write as we don't know what the ordinal relates to :( if (position - 1 == i) { if (!(ordinalNames[i] is SelectScalarExpression)) { return null; //col ref is something else?? } var replacement = new Replacements(); replacement.Original = (ordinal.Expression as IntegerLiteral).Value; replacement.OriginalFragment = ordinal; replacement.OriginalLength = ordinal.Expression.FragmentLength; replacement.OriginalOffset = ordinal.Expression.StartOffset; var expression = ordinalNames[i] as SelectScalarExpression; if (expression.ColumnName != null && !String.IsNullOrEmpty(expression.ColumnName.Value)) { replacement.Replacement = expression.ColumnName.Value; } else { replacement.Replacement = (expression.Expression as ColumnReferenceExpression).MultiPartIdentifier .ToNameString(); } replacements.Add(replacement); break; } } } } } return replacements; }
public List <Replacements> GetReplacements(List <QuerySpecification> queries) { var replacements = new List <Replacements>(); var uniquifier = new Dictionary <string, object>(); foreach (var spec in queries) { var key = string.Format("{0}:{1}", spec.StartOffset, spec.FragmentLength); if (uniquifier.ContainsKey(key)) { continue; } uniquifier.Add(key, null); if (spec.OrderByClause == null) { continue; } if (spec.OrderByClause.OrderByElements.Any(p => p.Expression is IntegerLiteral)) { var integerOrderClauses = spec.OrderByClause.OrderByElements.Where(p => p.Expression is IntegerLiteral); var ordinalNames = spec.SelectElements; foreach (var ordinal in integerOrderClauses) { var position = Int32.Parse((ordinal.Expression as IntegerLiteral).Value); for (var i = 0; i < ordinalNames.Count; i++) { if (ordinalNames[i] is SelectStarExpression) { return(null); //can't re-write as we don't know what the ordinal relates to :( } if (position - 1 == i) { if (!(ordinalNames[i] is SelectScalarExpression)) { return(null); //col ref is something else?? } var replacement = new Replacements(); replacement.Original = (ordinal.Expression as IntegerLiteral).Value; replacement.OriginalFragment = ordinal; replacement.OriginalLength = ordinal.Expression.FragmentLength; replacement.OriginalOffset = ordinal.Expression.StartOffset; var expression = ordinalNames[i] as SelectScalarExpression; if (expression.ColumnName != null && !String.IsNullOrEmpty(expression.ColumnName.Value)) { replacement.Replacement = expression.ColumnName.Value; } else { replacement.Replacement = (expression.Expression as ColumnReferenceExpression).MultiPartIdentifier .ToNameString(); } replacements.Add(replacement); break; } } } } } return(replacements); }
public ClippyReplacementOperation(Replacements replacement) { _replacement = replacement; }