Exemple #1
0
        public string BuildSQL(string condition, string having, params string[] sorts)
        {
            string sqlstr = SelectClause + " " + FromClause;

            if (condition != null && condition.Trim().Length > 0)
            {
                if (WhereClause == null || WhereClause.Trim().Length <= 0)
                {
                    sqlstr += " where " + condition;
                }
                else
                {
                    sqlstr += " where ( " + condition + " ) and ( " + WhereClause + " )";
                }
            }
            else if (WhereClause != null && WhereClause.Trim().Length > 0)
            {
                sqlstr += " where " + WhereClause;
            }
            if (GroupByList != null && GroupByList.Trim().Length > 0)
            {
                sqlstr += " group by " + GroupByList;
                if (having != null && having.Trim().Length > 0)
                {                //用户指定了条件并且有分组,则用户指定条件合并到HAVING子句中
                    if (HavingClause == null || HavingClause.Trim().Length <= 0)
                    {
                        sqlstr += " having " + having;
                    }
                    else
                    {
                        sqlstr += " having ( " + having + " ) and ( " + HavingClause + " )";
                    }
                }
                else if (HavingClause != null && HavingClause.Trim().Length > 0)
                {
                    sqlstr += " having " + HavingClause;
                }
            }
            if (sorts.Length > 0)
            {
                sqlstr += " " + string.Join(",", sorts);
            }
            else if (OrderByList != null && OrderByList.Trim().Length > 0)
            {
                sqlstr += " order by " + OrderByList;
            }
            return(sqlstr);
        }