Exemple #1
0
        public override void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            if (_Selects == null)
            {
                return;
            }

            int    count   = _Selects.Count;
            string combine = "";

            switch (_CombineType)
            {
            case CombineType.Union:
                combine = " UNION ";
                break;

            case CombineType.UnionAll:
                combine = " UNION ALL ";
                break;

            case CombineType.Intersect:
                combine = " INTERSECT ";
                break;

            case CombineType.Except:
                combine = " EXCEPT ";
                break;

            case CombineType.Minus:
                combine = " MINUS ";
                break;
            }
            for (int x = 0; x < count; x++)
            {
                _Selects[x].GetSql(db_target, ref sql);
                if (x + 1 < count)
                {
                    sql.Append(combine);
                }
            }

            if (_OrderBy != null)
            {
                count = _OrderBy.Count;
                if (count > 0)
                {
                    sql.Append(" ORDER BY ");

                    for (int x = 0; x < count; x++)
                    {
                        IOrderByExpression item = _OrderBy[x];

                        item.GetSql(db_target, ref sql);
                        if (x + 1 < count)
                        {
                            sql.Append(",");
                        }
                    }
                }
            }
        }
Exemple #2
0
        public override void GetSql(DbTarget db_target, ref StringBuilder sql)
        {
            sql.Append("SELECT ");
            if (IsDistinct)
            {
                sql.Append("DISTINCT ");
            }

            if (db_target == DbTarget.SqlServer && Top > 0)
            {
                sql.Append(String.Format("TOP {0} ", _Top));
            }

            int count = Columns.Count;

            if (count == 0)
            {
                sql.Append(" * ");
            }
            else
            {
                for (int x = 0; x < count; x++)
                {
                    if (x > 0)
                    {
                        sql.Append(",");
                    }

                    SelectColumn col = _Columns[x];
                    col.GetSql(db_target, ref sql);
                }
            }
            if (_From != null)
            {
                _From.GetSql(db_target, ref sql);
            }

            if (_Where != null && _Where.Count > 0)
            {
                _Where.GetSql(db_target, ref sql);
            }

            if (_GroupBy != null)
            {
                count = _GroupBy.Count;
                if (count > 0)
                {
                    sql.Append(" GROUP BY ");

                    for (int x = 0; x < count; x++)
                    {
                        ISqlExpression item = _GroupBy[x];

                        if (!item.IsLiteral)
                        {
                            sql.Append("(");
                        }
                        item.GetSql(db_target, ref sql);
                        if (!item.IsLiteral)
                        {
                            sql.Append(")");
                        }
                        if (x + 1 < count)
                        {
                            sql.Append(", ");
                        }
                        else
                        {
                            sql.Append(" ");
                        }
                    }
                }
            }

            if (_Having != null && _Having.Count > 0)
            {
                _Having.GetSql(db_target, ref sql);
            }

            if (_OrderBy != null)
            {
                count = _OrderBy.Count;
                if (count > 0)
                {
                    sql.Append(" ORDER BY ");

                    for (int x = 0; x < count; x++)
                    {
                        IOrderByExpression item = _OrderBy[x];

                        item.GetSql(db_target, ref sql);
                        if (x + 1 < count)
                        {
                            sql.Append(",");
                        }
                    }
                }
            }

            if ((db_target == DbTarget.SqlLite || db_target == DbTarget.MySql) && Top > 0)
            {
                sql.Append(String.Format(" LIMIT {0} ", _Top));
            }
        }