コード例 #1
0
            /// <summary>
            /// Constructs the actual query
            /// </summary>
            /// <returns>A single update query</returns>
            public virtual string Build()
            {
                var row = new StringBuilder();

                if (CommentOut)
                {
                    row.Append("-- ");
                }

                // Return empty if there are no values or where clause or no table name set
                if (Values.Count == 0 || WhereClause.Count == 0 || string.IsNullOrEmpty(Table))
                {
                    return(string.Empty);
                }

                row.Append("UPDATE ");
                row.Append(Table);
                row.Append(" SET ");

                var count = 0;

                foreach (var values in Values)
                {
                    count++;
                    row.Append(values.Key);
                    row.Append("=");
                    row.Append(values.Value);
                    if (Values.Count != count)
                    {
                        row.Append(SQLUtil.CommaSeparator);
                    }
                }

                row.Append(" WHERE ");

                count = 0;
                foreach (var whereClause in WhereClause)
                {
                    count++;
                    row.Append(whereClause.Key);
                    row.Append("=");
                    row.Append(whereClause.Value is string?SQLUtil.Stringify(whereClause.Value) : whereClause.Value);
                    if (WhereClause.Count != count)
                    {
                        row.Append(" AND ");
                    }
                }

                row.Append(";");

                if (!String.IsNullOrWhiteSpace(Comment))
                {
                    row.Append(" -- " + Comment);
                }
                row.Append(Environment.NewLine);

                return(row.ToString());
            }