Example #1
0
 //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();
 }
        /// <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();
        }
        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());
        }
Example #4
0
 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(")");
        }
        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();
        }