private static void AppendInsert(DataMergeDefinition mergeDefinition, StringBuilder builder) { if (!mergeDefinition.IncludeInsert) { return; } var mergeColumns = mergeDefinition.Columns .Where(c => !c.IsIgnored && c.CanInsert) .ToList(); builder .AppendLine("WHEN NOT MATCHED BY TARGET THEN ") .Append(' ', TabSize) .AppendLine("INSERT") .Append(' ', TabSize) .AppendLine("("); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append(QuoteIdentifier(mergeColumn.TargetColumn)); hasColumn = true; } builder.AppendLine(); builder .Append(' ', TabSize) .AppendLine(")") .Append(' ', TabSize) .AppendLine("VALUES") .Append(' ', TabSize) .AppendLine("("); hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append("s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder.AppendLine(); builder .Append(' ', TabSize) .AppendLine(")"); }
//public static IEnumerable<Cookie> GetAllCookies(this CookieContainer cc) //{ // var table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", // BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, // null, cc, new object[] { }); // foreach (var pathList in table.Values) // { // var lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", // BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, // null, pathList, new object[] { }); // foreach (CookieCollection colCookies in lstCookieCol.Values) // { // foreach (var c in colCookies.OfType<Cookie>()) // { // yield return c; // } // } // } //} //public static IEnumerable<Cookie> GetCookies(this CookieContainer cc, string name) //{ // return GetAllCookies(cc).Where(item => string.Compare(item.Name, name, StringComparison.OrdinalIgnoreCase) == 0); //} public static string GetRequestHeader(this QQHttpRequest request, CookieContainer cc) { var sb = new StringBuilder(); sb.AppendLineIf($"{HttpConstants.Referrer}: {request.Referrer}", !request.Referrer.IsNullOrEmpty()); sb.AppendLineIf($"{HttpConstants.UserAgent}: {request.UserAgent}", !request.UserAgent.IsNullOrEmpty()); sb.AppendLineIf($"{HttpConstants.ContentType}: {request.ContentType}", !request.ContentType.IsNullOrEmpty()); var cookies = cc.GetCookies(new Uri(request.Url)).OfType<Cookie>(); sb.AppendLine($"{HttpConstants.Cookie}: {string.Join("; ", cookies)}"); return sb.ToString(); }
public void AppendLineIfTest0() { StringBuilder sb = new StringBuilder(); sb.AppendLineIf(true, "True"); sb.AppendLineIf(false, "false"); sb.AppendLineIf(true, true); Assert.IsTrue(sb.ToString() == "True" + Environment.NewLine + "True" + Environment.NewLine); }
public void AppendLineIf() { var sb = new StringBuilder(); sb.AppendLineIf(true); Assert.Equal(Environment.NewLine, sb.ToString()); sb.Clear(); sb.AppendLineIf(false); Assert.Equal(string.Empty, sb.ToString()); }
public static string GetRequestHeader(this HttpRequestItem request, CookieCollection cookieCollection) { var sb = new StringBuilder(); sb.AppendLineIf($"{HttpConstants.Referrer}: { request.Referrer}", !request.Referrer.IsNullOrEmpty()); sb.AppendLineIf($"{HttpConstants.ContentType}: {request.ContentType}", !request.ContentType.IsNullOrEmpty()); var cookies = cookieCollection.OfType <Cookie>(); sb.AppendLine($"{HttpConstants.Cookie}: {string.Join("; ", cookies)}"); return(sb.ToString()); }
//public static IEnumerable<Cookie> GetAllCookies(this CookieContainer cc) //{ // var table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", // BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, // null, cc, new object[] { }); // foreach (var pathList in table.Values) // { // var lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", // BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, // null, pathList, new object[] { }); // foreach (CookieCollection colCookies in lstCookieCol.Values) // { // foreach (var c in colCookies.OfType<Cookie>()) // { // yield return c; // } // } // } //} //public static IEnumerable<Cookie> GetCookies(this CookieContainer cc, string name) //{ // return GetAllCookies(cc).Where(item => string.Compare(item.Name, name, StringComparison.OrdinalIgnoreCase) == 0); //} public static string GetRequestHeader(this QQHttpRequest request, CookieContainer cc) { var sb = new StringBuilder(); sb.AppendLineIf($"{HttpConstants.Referrer}: {request.Referrer}", !request.Referrer.IsNullOrEmpty()); sb.AppendLineIf($"{HttpConstants.UserAgent}: {request.UserAgent}", !request.UserAgent.IsNullOrEmpty()); sb.AppendLineIf($"{HttpConstants.ContentType}: {request.ContentType}", !request.ContentType.IsNullOrEmpty()); var cookies = cc.GetCookies(new Uri(request.Url)).OfType <Cookie>(); sb.AppendLine($"{HttpConstants.Cookie}: {string.Join("; ", cookies)}"); return(sb.ToString()); }
private static void AppendJoin(List <DataMergeColumn> mergeColumns, StringBuilder builder) { bool hasColumn; builder .AppendLine("ON") .AppendLine("("); hasColumn = false; foreach (var mergeColumn in mergeColumns.Where(c => c.IsKey)) { bool writeComma = hasColumn; builder .AppendLineIf(" AND ", v => writeComma) .Append(' ', TabSize) .Append("t.") .Append(QuoteIdentifier(mergeColumn.TargetColumn)) .Append(" = s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder .AppendLine() .Append(")") .AppendLine(); }
/// <summary> /// Builds the SQL for the temporary table used in the merge operation. /// </summary> /// <param name="mergeDefinition">The merge definition.</param> /// <returns></returns> public static string BuildTable(DataMergeDefinition mergeDefinition) { var builder = new StringBuilder(); builder .Append("CREATE TABLE ") .Append(TableIdentifier(mergeDefinition.TemporaryTable)) .AppendLine() .Append("(") .AppendLine(); bool hasColumn = false; foreach (var mergeColumn in mergeDefinition.Columns.Where(c => !c.IsIgnored)) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize) .Append(QuoteIdentifier(mergeColumn.SourceColumn)) .Append(" ") .Append(mergeColumn.NativeType) .Append(" NULL"); hasColumn = true; } builder .AppendLine() .Append(")") .AppendLine(); return builder.ToString(); }
/// <summary> /// Returns a more complete text dump of this exception, than just its text. /// </summary> public static string ToFullMessage(this Exception error, string additionalMessage, bool includeStackTrace, bool includeSource, bool includeData) { if (error == null) { throw new NullReferenceException("This exception object is null"); } var resultBuilder = new StringBuilder(); resultBuilder.AppendLineIf(additionalMessage, additionalMessage.HasValue()); var err = error; while (err != null) { resultBuilder.AppendLine(err.Message); if (includeData && err.Data != null && err.Data.Count > 0) { resultBuilder.AppendLine("\r\nException Data:\r\n{"); foreach (var i in err.Data) { resultBuilder.AppendLine(ToLogText(i).WithPrefix(" ")); } resultBuilder.AppendLine("}"); } if (err is ReflectionTypeLoadException) { foreach (var loaderEx in (err as ReflectionTypeLoadException).LoaderExceptions) { resultBuilder.AppendLine("Type load exception: " + loaderEx.ToFullMessage()); } } if (err is TargetInvocationException) { err = err.InnerException; } err = err.InnerException; if (err != null) { resultBuilder.AppendLine(); if (includeStackTrace) { resultBuilder.AppendLine("###############################################"); } resultBuilder.Append("Base issue: "); } } if (includeStackTrace && error.StackTrace.HasValue()) { var stackLines = error.StackTrace.Or("").Trim().ToLines(); stackLines = stackLines.Except(l => l.Trim().StartsWith("at System.Data.")).ToArray(); resultBuilder.AppendLine(stackLines.ToString("\r\n\r\n").WithPrefix("\r\n--------------------------------------\r\nSTACK TRACE:\r\n\r\n")); } return(resultBuilder.ToString()); }
private static void AppendUpdate(DataMergeDefinition mergeDefinition, StringBuilder builder) { if (!mergeDefinition.IncludeUpdate) { return; } var mergeColumns = mergeDefinition.Columns .Where(c => !c.IsIgnored && c.CanUpdate) .ToList(); builder .AppendLine("WHEN MATCHED THEN ") .Append(' ', TabSize) .AppendLine("UPDATE SET"); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append("t.") .Append(QuoteIdentifier(mergeColumn.TargetColumn)) .Append(" = s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder.AppendLine(); }
private static void AppendUsingSelect(DataMergeDefinition mergeDefinition, List <DataMergeColumn> mergeColumns, StringBuilder builder) { builder .AppendLine("USING") .AppendLine("(") .Append(' ', TabSize) .AppendLine("SELECT"); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder .AppendLine() .Append(' ', TabSize) .Append("FROM ") .Append(TableIdentifier(mergeDefinition.TemporaryTable)) .AppendLine() .AppendLine(")") .AppendLine("AS s"); }
/// <summary> /// Builds the SQL for the temporary table used in the merge operation. /// </summary> /// <param name="mergeDefinition">The merge definition.</param> /// <returns></returns> public static string BuildTable(DataMergeDefinition mergeDefinition) { var builder = new StringBuilder(); builder .Append("CREATE TABLE ") .Append(TableIdentifier(mergeDefinition.TemporaryTable)) .AppendLine() .Append("(") .AppendLine(); bool hasColumn = false; foreach (var mergeColumn in mergeDefinition.Columns.Where(c => !c.IsIgnored)) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize) .Append(QuoteIdentifier(mergeColumn.SourceColumn)) .Append(" ") .Append(mergeColumn.NativeType) .Append(" NULL"); hasColumn = true; } builder .AppendLine() .Append(")") .AppendLine(); return(builder.ToString()); }
private static string GetPropertySnippet(bool hasContract, string initialValue = null) { var sb = new StringBuilder(); sb.AppendLine("abstract class BaseClass {"); sb.AppendLineIf(hasContract, "[NonNull]"); sb.AppendLine("public abstract string TestProperty { get; set; }"); sb.AppendLine("}"); sb.AppendLine("class IntermediateClass : BaseClass {"); sb.AppendLine("public override string TestProperty { get; set; }"); sb.AppendLine("}"); sb.AppendLine("class TestClass : IntermediateClass {"); sb.Append("public string testField"); sb.AppendIf(initialValue != null, $" = \"{initialValue}\""); sb.AppendLine(";"); sb.AppendLine("public override string TestProperty { "); sb.AppendLine("get { return testField; }"); sb.AppendLine("set { testField = value; }"); sb.AppendLine("}"); sb.AppendLine("}"); return(sb.ToString()); }
private static string GetPropertySnippet(bool hasContract, bool explicitImplementation, string initialValue = null) { var sb = new StringBuilder(); sb.AppendLine("interface ITest {"); sb.AppendLineIf(hasContract, "[NonNull]"); sb.AppendLine("string TestProperty { get; set; }"); sb.AppendLine("}"); sb.AppendLine("interface IIntermediate : ITest { }"); sb.AppendLine("class TestClass : IIntermediate {"); sb.Append("public string testField"); sb.AppendIf(initialValue != null, $" = \"{initialValue}\""); sb.AppendLine(";"); sb.AppendIf(!explicitImplementation, "public "); sb.Append("string "); sb.AppendIf(explicitImplementation, "ITest."); sb.AppendLine("TestProperty { "); sb.AppendLine("get { return testField; }"); sb.AppendLine("set { testField = value; }"); sb.AppendLine("}"); sb.AppendLine("}"); return(sb.ToString()); }
public override string ToString() { var msg = new StringBuilder($"ErrorCode={ErrorCode}, ErrorMsg={this.GetAllMessages()}, StackTrace="); msg.AppendLineIf($"{Environment.NewLine}{StackTrace}", StackTrace != null); return(msg.ToString()); }
public static StringBuilder PagedBase(FilterModel filter) { var sql = new StringBuilder(); sql.AppendLineIf(filter.Sorting != null, $"Order By { filter.Sorting }") .AppendLine($"OFFSET { filter.SkipCount } Rows Fetch Next { filter.MaxResultCount } Rows Only;"); return(sql); }
private static string GetSnippet(bool hasGet, bool hasSet, bool hasContract, string initialFieldValue = null) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLine($"private string testField{(initialFieldValue == null ? "" : ($" = \"{initialFieldValue}\""))};"); sb.AppendLineIf(hasContract, "[NonNull]"); sb.AppendLine("public string TestProperty { "); sb.AppendLineIf(hasGet, "get { return testField; }"); sb.AppendLineIf(hasSet, "set { testField = value; }"); sb.AppendLine("}"); sb.AppendLine("}"); return(sb.ToString()); }
public void AppendLineIf_NullString_True_SomeText_Exception() { // Arrange StringBuilder stringBuilderToTest = null; // Act stringBuilderToTest.AppendLineIf(true, "Some Text To Add"); // Assert Assert.Fail($"Exception should have been thrown."); }
void HttpUnhandledExceptionToFullMessageAction(Exception error, StringBuilder builder) { try { builder.AppendLineIf((error as HttpUnhandledException)?.GetHtmlErrorMessage().TrimBefore("Server Error")); } catch { // No logging is needed } }
public void AppendLineIf() { // Type var @this = new StringBuilder(); // Exemples @this.AppendLineIf(x => x.Contains("F"), "Fizz", "Buzz"); // return "FizzBuzz"; // Unit Test Assert.AreEqual("Fizz" + Environment.NewLine, @this.ToString()); }
private static string GetPropertySnippet(bool hasContract, string result = null) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLineIf(hasContract, "[NonNull]"); sb.Append("public string TestProperty => "); sb.Append($"{(result == null ? "null" : $"\"{result}\"")};"); sb.AppendLine("}"); return(sb.ToString()); }
private static string GetSnippet(bool hasContract, bool hasLegacyContract) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.Append("public IEnumerable<int> TestMethod("); sb.AppendIf(hasContract, "[NonNull]"); sb.AppendLine("string text) {"); sb.AppendLineIf(hasLegacyContract, "if (text == null) throw new ArgumentNullException(nameof(text));"); sb.AppendLine("yield return 1; } }"); return(sb.ToString()); }
public void AppendLineIf_BaseText_True_SomeText_TypeOfStringBuilder() { // Arrange StringBuilder stringBuilderToTest = new StringBuilder(); stringBuilderToTest.AppendLine("BaseText"); // Act stringBuilderToTest.AppendLineIf(true, "SomeText"); // Assert Assert.IsInstanceOfType(stringBuilderToTest, typeof(StringBuilder)); }
public void AppendLineIf_BaseString_False_SomeText_BaseText_NewLine() { // Arrange StringBuilder stringBuilderToTest = new StringBuilder(); stringBuilderToTest.AppendLine("BaseText"); // Act stringBuilderToTest.AppendLineIf(false, "SomeText"); // Assert Assert.AreEqual($"BaseText{Environment.NewLine}", stringBuilderToTest.ToString()); }
private static string GetSnippet(ContractTypes contractTypes) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLineIf(contractTypes.HasFlag(ContractTypes.Post), "[return: NonNull]"); sb.Append("public string TestMethod("); sb.AppendIf(contractTypes.HasFlag(ContractTypes.Pre), "[NonNull]"); sb.AppendLine("string text, string output) {"); sb.AppendLine("return output; } }"); return(sb.ToString()); }
public void AppendIf_AppendToFalse_TextIsNotAppended() { var sb = new StringBuilder(); sb.AppendLine("Test"); // Act sb.AppendLineIf(false, "1234"); // Assert sb.ToString() .Should() .Be($"Test{Env.NewLine}"); }
public void AppendLineIf_AppendToTrue_LineIsAppended() { var sb = new StringBuilder(); sb.AppendLine("Test"); // Act sb.AppendLineIf(true, "1234"); // Assert sb.ToString() .Should() .Be($"Test{Env.NewLine}1234{Env.NewLine}"); }
public void SingleArgument_ConditionTrue() { // Arrange var builder = new StringBuilder(); var expected = "my string\r\n"; // Act builder.AppendLineIf(condition: 1 + 1 == 2, "my string"); var actual = builder.ToString(); // Assert Assert.Equal(expected, actual); }
private static string GetSnippet(ContractTypes contractTypes, string result = null) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLineIf(contractTypes.HasFlag(ContractTypes.Post), "[return: NonNull]"); sb.Append("public static explicit operator string("); sb.AppendIf(contractTypes.HasFlag(ContractTypes.Pre), "[NonNull]"); sb.AppendLine("TestClass obj) {"); sb.AppendLine($"return {(result == null ? "null" : $"\"{result}\"")};"); sb.AppendLine("} }"); return(sb.ToString()); }
private static string GetMethodSnippet(ContractTypes contractTypes, string result = null) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLineIf(contractTypes.HasFlag(ContractTypes.Post), "[return: NonNull]"); sb.Append("public string TestMethod("); sb.AppendIf(contractTypes.HasFlag(ContractTypes.Pre), "[NonNull]"); sb.AppendLine("string text) =>"); sb.Append($"{(result == null ? "null" : $"\"{result}\"")};"); sb.AppendLine("}"); return(sb.ToString()); }
public void NoArguments_ConditionFalse() { // Arrange var builder = new StringBuilder("test"); var expected = "test"; // Act builder.AppendLineIf(condition: 1 + 1 == 1); var actual = builder.ToString(); // Assert Assert.Equal(expected, actual); }
private static string GetSnippet(bool hasSet, bool hasContract, string initialValue = null) { var sb = new StringBuilder(); sb.AppendLine("class TestClass {"); sb.AppendLineIf(hasContract, "[NonNull]"); sb.Append("public string TestProperty { get; "); sb.AppendIf(hasSet, "set;"); sb.Append("}"); sb.AppendIf(initialValue != null, $" = \"{initialValue}\";"); sb.Append("}"); return(sb.ToString()); }
private static void AppendUpdate(DataMergeDefinition mergeDefinition, StringBuilder builder) { if (!mergeDefinition.IncludeUpdate) return; var mergeColumns = mergeDefinition.Columns .Where(c => !c.IsIgnored && c.CanUpdate) .ToList(); builder .AppendLine("WHEN MATCHED THEN ") .Append(' ', TabSize) .AppendLine("UPDATE SET"); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append("t.") .Append(QuoteIdentifier(mergeColumn.TargetColumn)) .Append(" = s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder.AppendLine(); }
public override string ToString() { var msg = new StringBuilder($"ErrorCode={ErrorCode}, ErrorMsg={this.GetAllMessages()}, StackTrace="); msg.AppendLineIf($"{Environment.NewLine}{StackTrace}", StackTrace != null); return msg.ToString(); }
/// <summary> /// Builds the SQL merge statement for the merge operation. /// </summary> /// <param name="mergeDefinition">The merge definition.</param> /// <returns></returns> public static string BuildMerge(DataMergeDefinition mergeDefinition) { var mergeColumns = mergeDefinition.Columns .Where(c => !c.IsIgnored) .ToList(); var builder = new StringBuilder(); if (mergeDefinition.IdentityInsert && mergeDefinition.IncludeInsert) { builder .Append("SET IDENTITY_INSERT ") .Append(TableIdentifier(mergeDefinition.TargetTable)) .AppendLine(" ON;") .AppendLine(); } builder .Append("MERGE INTO ") .Append(TableIdentifier(mergeDefinition.TargetTable)) .Append(" AS t") .AppendLine() .AppendLine("USING") .AppendLine("(") .Append(' ', TabSize) .AppendLine("SELECT"); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder .AppendLine() .Append(' ', TabSize) .Append("FROM ") .Append(TableIdentifier(mergeDefinition.TemporaryTable)) .AppendLine() .AppendLine(")") .AppendLine("AS s") .AppendLine("ON") .AppendLine("("); hasColumn = false; foreach (var mergeColumn in mergeColumns.Where(c => c.IsKey)) { bool writeComma = hasColumn; builder .AppendLineIf(" AND ", v => writeComma) .Append(' ', TabSize) .Append("t.") .Append(QuoteIdentifier(mergeColumn.TargetColumn)) .Append(" = s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder .AppendLine() .Append(")") .AppendLine(); // Insert AppendInsert(mergeDefinition, builder); // Update AppendUpdate(mergeDefinition, builder); // Delete AppendDelete(mergeDefinition, builder); // Output AppendOutput(mergeDefinition, builder); // merge must end with ; builder.Append(";"); if (mergeDefinition.IdentityInsert && mergeDefinition.IncludeInsert) { builder .Append("SET IDENTITY_INSERT ") .Append(TableIdentifier(mergeDefinition.TargetTable)) .AppendLine(" OFF;") .AppendLine(); } return builder.ToString(); }
private static void AppendInsert(DataMergeDefinition mergeDefinition, StringBuilder builder) { if (!mergeDefinition.IncludeInsert) return; var mergeColumns = mergeDefinition.Columns .Where(c => !c.IsIgnored && c.CanInsert) .ToList(); builder .AppendLine("WHEN NOT MATCHED BY TARGET THEN ") .Append(' ', TabSize) .AppendLine("INSERT") .Append(' ', TabSize) .AppendLine("("); bool hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append(QuoteIdentifier(mergeColumn.TargetColumn)); hasColumn = true; } builder.AppendLine(); builder .Append(' ', TabSize) .AppendLine(")") .Append(' ', TabSize) .AppendLine("VALUES") .Append(' ', TabSize) .AppendLine("("); hasColumn = false; foreach (var mergeColumn in mergeColumns) { bool writeComma = hasColumn; builder .AppendLineIf(",", v => writeComma) .Append(' ', TabSize * 2) .Append("s.") .Append(QuoteIdentifier(mergeColumn.SourceColumn)); hasColumn = true; } builder.AppendLine(); builder .Append(' ', TabSize) .AppendLine(")"); }