Example #1
0
 private void BuildUpdate(ToDBCommand command, StringBuilder sql)
 {
     if (command.UpdateClause != null)
     {
         sql.Append("update " + command.UpdateClause.Item + " ");
     }
 }
Example #2
0
 void BuidDelete(ToDBCommand command, StringBuilder sql)
 {
     if (command.DeleteClause != null)
     {
         sql.Append("delete " + command.DeleteClause.Item + " ");
     }
 }
Example #3
0
 private void BuildSets(ToDBCommand command, StringBuilder sql)
 {
     if (command.SetItems != null && command.SetItems.Any())
     {
         sql.Append("set " + string.Join(",", command.SetItems.Select(x => x.Left + "=" + x.Right)) + " ");
     }
 }
Example #4
0
        public string ToSql(ToDBCommand command)
        {
            StringBuilder sql = new StringBuilder();

            ToSql(command, sql);
            return(sql.ToString().TrimEnd());
        }
Example #5
0
 void BuildOrderBy(ToDBCommand command, StringBuilder sql)
 {
     if (command.OrderBys.Any())
     {
         sql.Append("order by " + string.Join(",", command.OrderBys.Select(x => x.Column + " " + (x.Desc ? "desc" : "asc"))) + " ");
     }
 }
Example #6
0
 void BuildFrom(ToDBCommand command, StringBuilder sql)
 {
     if (command.FromClause != null)
     {
         sql.Append("from " + command.FromClause.Item + " ");
     }
 }
Example #7
0
        void BuildUnions(ToDBCommand command, StringBuilder sql)
        {
            foreach (Union union in command.Unions)
            {
                string op = "";
                switch (union.TypeOfUnion)
                {
                case Union.UnionType.Union:
                    op = "union";
                    break;

                case Union.UnionType.UnionAll:
                    op = "union all";
                    break;

                case Union.UnionType.Intersect:
                    op = "intersect";
                    break;

                case Union.UnionType.Except:
                    op = "except";
                    break;

                default:
                    throw new NotImplementedException();
                }
                sql.Append(op + " " + ToSql(union.Query));
            }
        }
Example #8
0
 void BuildGroupBys(ToDBCommand command, StringBuilder sql)
 {
     if (command.GroupBys != null && command.GroupBys.Any())
     {
         sql.Append("group by " + string.Join(",", command.GroupBys.Select(x => x.Column)) + " ");
     }
 }
Example #9
0
 void BuildWhere(ToDBCommand command, StringBuilder sql)
 {
     if (command.WhereClause.Items.Any())
     {
         sql.Append("where ");
         sql.Append(RenderConditions(command.WhereClause));
     }
 }
Example #10
0
 void BuildHavings(ToDBCommand command, StringBuilder sql)
 {
     if (command.HavingClause.Items.Any())
     {
         sql.Append("having ");
         sql.Append(RenderConditions(command.HavingClause));
     }
 }
Example #11
0
 void BuildInsert(ToDBCommand command, StringBuilder sql)
 {
     if (command.InsertClause != null)
     {
         sql.Append("insert " + command.InsertClause.Table.Item + " ");
         if (command.InsertClause.Columns.Any())
         {
             sql.Append("(" + string.Join(",", command.InsertClause.Columns) + ") ");
         }
     }
 }
Example #12
0
        string RenderSubQuery(ToDBCommand command, string alias)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("(");
            ToSql(command, sql);
            sql.Append(")");
            if (!string.IsNullOrWhiteSpace(alias))
            {
                sql.Append(" " + alias);
            }
            return(sql.ToString());
        }
Example #13
0
 void BuildValues(ToDBCommand command, StringBuilder sql)
 {
     if (command.ValuesClause != null && command.ValuesClause.Any())
     {
         sql.Append("values ");
         bool isFirst = true;
         foreach (var item in command.ValuesClause)
         {
             if (!isFirst)
             {
                 sql.Append(",");
             }
             sql.Append("(" + string.Join(",", item.Parameters) + ")");
             isFirst = false;
         }
         sql.Append(" ");
     }
 }
Example #14
0
        void ToSql(ToDBCommand command, StringBuilder sql)
        {
            BuidDelete(command, sql);
            BuildInsert(command, sql);
            BuildValues(command, sql);
            BuildUpdate(command, sql);
            BuildSets(command, sql);
            BuildSelect(command.SelectItems, sql);
            BuildFrom(command, sql);
            BuildJoins(command.Joins, sql);
            BuildWhere(command, sql);
            BuildUnions(command, sql);
            BuildGroupBys(command, sql);
            BuildHavings(command, sql);
            BuildOrderBy(command, sql);

            if (_options.RequireWhereOnUpdateAndDelete)
            {
                if (!command.WhereClause.Items.Any() && (command.DeleteClause != null || command.UpdateClause != null))
                {
                    throw new ToDBException("Update or Delete with no conditions. Set RequireWhereOnUpdateAndDelete to false in the options if this was intentional.");
                }
            }
        }
Example #15
0
        public static string ToSql(this ToDBCommand command, TSqlSerializerOptions options)
        {
            TSqlSerializer serializer = new TSqlSerializer(options);

            return(serializer.ToSql(command));
        }
Example #16
0
        public static string ToSql(this ToDBCommand command)
        {
            TSqlSerializer serializer = new TSqlSerializer();

            return(serializer.ToSql(command));
        }