Ejemplo n.º 1
0
        public string ConfigureCommand(IIncludeJoin[] joins, ISelector selector, NpgsqlCommand command, string sql, int limit)
        {
            var fields = selector.SelectFields().ToArray();


            if (HasSelectTransform())
            {
                sql = $"select {fields[0]} from ({sql}) as {_tableAlias}";
            }
            else
            {
                fields[0] = "x";

                sql = $"select {fields.Join(", ")} from  ({sql}) as {_tableAlias}";
            }


            if (joins.Any())
            {
                sql += " " + joins.Select(x => x.JoinTextFor(_tableAlias, _document)).Join(" ");
            }


            var @where = buildWhereFragment(_document);

            if (@where != null)
            {
                sql += " where " + @where.ToSql(command);
            }

            var orderBy = determineOrderClause(_document);

            if (orderBy.IsNotEmpty())
            {
                sql += orderBy;
            }

            sql = _query.ApplySkip(command, sql);
            sql = _query.ApplyTake(command, limit, sql);



            return(sql);
        }
Ejemplo n.º 2
0
        public void ConfigureCommand(IIncludeJoin[] joins, ISelector selector, CommandBuilder sql, int limit)
        {
            var innerSql = sql.ToString();

            sql.Clear();

            var fields = selector.SelectFields().ToArray();

            if (HasSelectTransform())
            {
                sql.Append("select ");
                sql.Append(fields[0]);
                sql.Append(" from (");
                sql.Append(innerSql);
                sql.Append(") as ");
                sql.Append(_tableAlias);
            }
            else
            {
                fields[0] = "x";

                sql.Append("select ");
                sql.Append(fields[0]);

                for (var i = 1; i < fields.Length; i++)
                {
                    sql.Append(", ");
                    sql.Append(fields[i]);
                }

                sql.Append(" from (");
                sql.Append(innerSql);
                sql.Append(") as ");
                sql.Append(_tableAlias);
            }

            if (joins.Any())
            {
                foreach (var join in joins)
                {
                    sql.Append(" ");
                    join.AppendJoin(sql, _tableAlias, _document);
                }
            }

            var where = buildWhereFragment(_document);
            if (where != null)
            {
                sql.Append(" where ");
                where.Apply(sql);
            }

            var orderBy = determineOrderClause(_document);

            if (orderBy.IsNotEmpty())
            {
                sql.Append(orderBy);
            }

            _query.ApplySkip(sql);
            _query.ApplyTake(limit, sql);
        }