//----------------------------------------------------------------------------- // // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // A copy of the License is located at // // http://aws.amazon.com/apache2.0 // // or in the "license" file accompanying this file. This file is distributed // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either // express or implied. See the License for the specific language governing // permissions and limitations under the License. // //----------------------------------------------------------------------------- using System.Text.RegularExpressions; namespace Amazon.XRay.Recorder.Handlers.SqlServer { /// /// Utilities for SQL handlers /// public static class SqlUtil { // 1st alternative: (?:'([^']|'')*') matches single quoted literals, i.e. string, datatime. // Example: // 'apple' // 'very ''strong''' // '' // 2nd alternative: (?:(-|\+)?\$?\d+(\.\d+)? matches number and money // Example: // 123.12 // -123 // +12 // $123.12 // -$123.12 private static readonly Regex _sqlLiteralRegex = new Regex(@"(?:'([^']|'')*')|(?:(-|\+)?\$?\d+(\.\d+)?)"); private static readonly Regex _portNumberRegex = new Regex(@",\d+$"); /// /// Sanitizes the TSQL query. /// /// The query. /// Sanitized query string public static string SanitizeTsqlQuery(string query) { return _sqlLiteralRegex.Replace(query, "?"); } /// /// Removes the port number from data source. /// /// The data source. /// The data source string with port number removed. public static string RemovePortNumberFromDataSource(string dataSource) { return _portNumberRegex.Replace(dataSource, string.Empty); } } }