<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Transform;
using Orchard.ContentManagement.Records;
using Orchard.Data.Providers;
using Orchard.Environment.Configuration;
using Orchard.Utility.Extensions;

namespace Orchard.ContentManagement {

    public class DefaultHqlQuery : IHqlQuery {
        private readonly ISession _session;
        private readonly IEnumerable<ISqlStatementProvider> _sqlStatementProviders;
        private readonly ShellSettings _shellSettings;
        private VersionOptions _versionOptions;

        protected IJoin _from;
        protected readonly List<Tuple<IAlias, Join>> _joins = new List<Tuple<IAlias, Join>>();
        protected readonly List<Tuple<IAlias, Action<IHqlExpressionFactory>>> _wheres = new List<Tuple<IAlias, Action<IHqlExpressionFactory>>>();
        protected readonly List<Tuple<IAlias, Action<IHqlSortFactory>>> _sortings = new List<Tuple<IAlias, Action<IHqlSortFactory>>>();

        private bool cacheable;

        public IContentManager ContentManager { get; private set; }

        public DefaultHqlQuery(
            IContentManager contentManager, 
            ISession session,
            IEnumerable<ISqlStatementProvider> sqlStatementProviders,
            ShellSettings shellSettings) {
            _session = session;
            _sqlStatementProviders = sqlStatementProviders;
            _shellSettings = shellSettings;
            ContentManager = contentManager;
        }

        internal string PathToAlias(string path) {
            if (String.IsNullOrWhiteSpace(path)) {
                throw new ArgumentException("Path can't be empty");
            }

            return Char.ToLower(path[0], CultureInfo.InvariantCulture) + path.Substring(1);
        }

        internal Join BindNamedAlias(string alias) {
            var tuple = _joins.FirstOrDefault(x => x.Item2.Name == alias);
            return tuple == null ? null : tuple.Item2;
        }

        internal IAlias BindCriteriaByPath(IAlias alias, string path) {
            return BindCriteriaByAlias(alias, path, PathToAlias(path));
        }

        internal IAlias BindCriteriaByAlias(IAlias alias, string path, string aliasName) {
            // is this Join already existing (based on aliasName)

            Join join = BindNamedAlias(aliasName);

            if (join == null) {
                join = new Join(path, aliasName);
                _joins.Add(new Tuple<IAlias, Join>(alias, join));
            }

            return join;
        }

        internal IAlias BindTypeCriteria() {
            // ([ContentItemVersionRecord] >join> [ContentItemRecord]) >join> [ContentType]
            return BindCriteriaByAlias(BindItemCriteria(), "ContentType", "ct");
        }

        internal IAlias BindItemCriteria() {
            // [ContentItemVersionRecord] >join> [ContentItemRecord]
            return BindCriteriaByAlias(BindItemVersionCriteria(), typeof(ContentItemRecord).Name, "ci");
        }

        internal IAlias BindItemVersionCriteria() {
            return _from ?? (_from = new Join(typeof(ContentItemVersionRecord).FullName, "civ", ""));
        }

        internal IAlias BindPartCriteria<TRecord>() where TRecord : ContentPartRecord {
            return BindPartCriteria(typeof(TRecord));
        }

        internal IAlias BindPartCriteria(Type contentPartRecordType) {
            if (!contentPartRecordType.IsSubclassOf(typeof(ContentPartRecord))) {
                throw new ArgumentException("The type must inherit from ContentPartRecord", "contentPartRecordType");
            }

            if (contentPartRecordType.IsSubclassOf(typeof(ContentPartVersionRecord))) {
                return BindCriteriaByPath(BindItemVersionCriteria(), contentPartRecordType.Name);
            }
            return BindCriteriaByPath(BindItemCriteria(), contentPartRecordType.Name);
        }

        internal void Where(IAlias alias, Action<IHqlExpressionFactory> predicate) {
            _wheres.Add(new Tuple<IAlias, Action<IHqlExpressionFactory>>(alias, predicate));
        }
        
        internal IAlias ApplyHqlVersionOptionsRestrictions(VersionOptions versionOptions) {
            var alias = BindItemVersionCriteria();

            if (versionOptions == null) {
                Where(alias, x => x.Eq("Published", true));
            }
            else if (versionOptions.IsPublished) {
                Where(alias, x => x.Eq("Published", true));
            }
            else if (versionOptions.IsLatest) {
                Where(alias, x => x.Eq("Latest", true));
            }
            else if (versionOptions.IsDraft) {
                Where(alias, x => x.And(y => y.Eq("Latest", true), y => y.Eq("Published", false)));
            }
            else if (versionOptions.IsAllVersions) {
                // no-op... all versions will be returned by default
            }
            else {
                throw new ApplicationException("Invalid VersionOptions for content query");
            }

            return alias;
        }

        public IHqlQuery Join(Action<IAliasFactory> alias) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);
            return this;
        }

        public IHqlQuery Where(Action<IAliasFactory> alias, Action<IHqlExpressionFactory> predicate) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);
            Where(aliasFactory.Current, predicate);
            return this;
        }

        public IHqlQuery OrderBy(Action<IAliasFactory> alias, Action<IHqlSortFactory> order) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);

            _sortings.Add(new Tuple<IAlias, Action<IHqlSortFactory>>(aliasFactory.Current, order));
            return this;
        }

        public IHqlQuery ForType(params string[] contentTypeNames) {
            if (contentTypeNames != null && contentTypeNames.Length != 0) {
                Where(BindTypeCriteria(), x => x.InG("Name", contentTypeNames));
            }
            return this;
        }

        public IHqlQuery ForVersion(VersionOptions options) {
            _versionOptions = options;
            return this;
        }

        public IHqlQuery<T> ForPart<T>() where T : IContent {
            return new DefaultHqlQuery<T>(this);
        }

        public IEnumerable<ContentItem> List() {
            return Slice(0, 0);
        }

        public IEnumerable<ContentItem> Slice(int skip, int count) {
            ApplyHqlVersionOptionsRestrictions(_versionOptions);
            cacheable = true;
            
            var hql = ToHql(false);

            var query = _session
                .CreateQuery(hql)
                .SetCacheable(cacheable)
                ;

            if (skip != 0) {
                query.SetFirstResult(skip);
            }
            if (count != 0 && count != Int32.MaxValue) {
                query.SetMaxResults(count);
            }

            var ids = query
                .SetResultTransformer(Transformers.AliasToEntityMap)
                .List<IDictionary>()
                .Select(x => (int)x["Id"]);

            return ContentManager.GetManyByVersionId(ids, QueryHints.Empty);
        }

        public int Count() {
            ApplyHqlVersionOptionsRestrictions(_versionOptions);
            var hql = ToHql(true);
            hql = "select count(Id) from Orchard.ContentManagement.Records.ContentItemVersionRecord where Id in ( " + hql + " )";
            return Convert.ToInt32(_session.CreateQuery(hql)
                           .SetCacheable(true)
                           .UniqueResult())
                ;
        }

        public string ToHql(bool count) {
            var sb = new StringBuilder();

            if (count) {
                sb.Append("select distinct civ.Id as Id").AppendLine();
            }
            else {
                sb.Append("select distinct civ.Id as Id");

                // add sort properties in the select
                foreach (var sort in _sortings) {
                    var sortFactory = new DefaultHqlSortFactory();
                    sort.Item2(sortFactory);

                    if (!sortFactory.Randomize) {
                        sb.Append(", ");
                        sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName);
                    }
                    else {
                        // select distinct can't be used with newid()
                        cacheable = false;
                        sb.Replace("select distinct", "select ");
                    }
                }

                sb.AppendLine();
            }

            sb.Append("from ").Append(_from.TableName).Append(" as ").Append(_from.Name).AppendLine();

            foreach (var join in _joins) {
                sb.Append(join.Item2.Type).Append(" ").Append(join.Item1.Name + "." + join.Item2.TableName).Append(" as ").Append(join.Item2.Name).AppendLine();
            }

            // generating where clause
            if (_wheres.Any()) {
                sb.Append("where ");

                var expressions = new List<string>();

                foreach (var where in _wheres) {
                    var expressionFactory = new DefaultHqlExpressionFactory();
                    where.Item2(expressionFactory);
                    expressions.Add(expressionFactory.Criterion.ToHql(where.Item1));
                }

                sb.Append("(").Append(String.Join(") AND (", expressions.ToArray())).Append(")").AppendLine();
            }

            // generating order by clause
            bool firstSort = true;
            foreach (var sort in _sortings) {
                if (!firstSort) {
                    sb.Append(", ");
                }
                else {
                    sb.Append("order by ");
                    firstSort = false;
                }

                var sortFactory = new DefaultHqlSortFactory();
                sort.Item2(sortFactory);

                if (sortFactory.Randomize) {

                    string command = null;

                    foreach (var sqlStatementProvider in _sqlStatementProviders) {
                        if (!String.Equals(sqlStatementProvider.DataProvider, _shellSettings.DataProvider)) {
                            continue;
                        }

                        command = sqlStatementProvider.GetStatement("random") ?? command;
                    }

                    if (command != null) {
                        sb.Append(command);    
                    }
                }
                else {
                    sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName);
                    if (!sortFactory.Ascending) {
                        sb.Append(" desc");
                    }
                }
            }

            // no order clause was specified, use a default sort order, unless it's a count
            // query hence it doesn't need one
            if (firstSort && !count) {
                sb.Append("order by Id");
            }

            return sb.ToString();
        }

    }

    public class DefaultHqlQuery<TPart> : IHqlQuery<TPart> where TPart : IContent {
        private readonly DefaultHqlQuery _query;

        public DefaultHqlQuery(DefaultHqlQuery query) {
            _query = query;
        }

        public IContentManager ContentManager {
            get { return _query.ContentManager; }
        }

        public IHqlQuery<TPart> ForType(params string[] contentTypes) {
            _query.ForType(contentTypes);
            return new DefaultHqlQuery<TPart>(_query);
        }

        public IHqlQuery<TPart> ForVersion(VersionOptions options) {
            _query.ForVersion(options);
            return new DefaultHqlQuery<TPart>(_query);
        }

        IEnumerable<TPart> IHqlQuery<TPart>.List() {
            return _query.List().AsPart<TPart>();
        }<commit_msg>#19847: Fixing queries with not sorting filter<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Transform;
using Orchard.ContentManagement.Records;
using Orchard.Data.Providers;
using Orchard.Environment.Configuration;
using Orchard.Utility.Extensions;

namespace Orchard.ContentManagement {

    public class DefaultHqlQuery : IHqlQuery {
        private readonly ISession _session;
        private readonly IEnumerable<ISqlStatementProvider> _sqlStatementProviders;
        private readonly ShellSettings _shellSettings;
        private VersionOptions _versionOptions;

        protected IJoin _from;
        protected readonly List<Tuple<IAlias, Join>> _joins = new List<Tuple<IAlias, Join>>();
        protected readonly List<Tuple<IAlias, Action<IHqlExpressionFactory>>> _wheres = new List<Tuple<IAlias, Action<IHqlExpressionFactory>>>();
        protected readonly List<Tuple<IAlias, Action<IHqlSortFactory>>> _sortings = new List<Tuple<IAlias, Action<IHqlSortFactory>>>();

        private bool cacheable;

        public IContentManager ContentManager { get; private set; }

        public DefaultHqlQuery(
            IContentManager contentManager, 
            ISession session,
            IEnumerable<ISqlStatementProvider> sqlStatementProviders,
            ShellSettings shellSettings) {
            _session = session;
            _sqlStatementProviders = sqlStatementProviders;
            _shellSettings = shellSettings;
            ContentManager = contentManager;
        }

        internal string PathToAlias(string path) {
            if (String.IsNullOrWhiteSpace(path)) {
                throw new ArgumentException("Path can't be empty");
            }

            return Char.ToLower(path[0], CultureInfo.InvariantCulture) + path.Substring(1);
        }

        internal Join BindNamedAlias(string alias) {
            var tuple = _joins.FirstOrDefault(x => x.Item2.Name == alias);
            return tuple == null ? null : tuple.Item2;
        }

        internal IAlias BindCriteriaByPath(IAlias alias, string path) {
            return BindCriteriaByAlias(alias, path, PathToAlias(path));
        }

        internal IAlias BindCriteriaByAlias(IAlias alias, string path, string aliasName) {
            // is this Join already existing (based on aliasName)

            Join join = BindNamedAlias(aliasName);

            if (join == null) {
                join = new Join(path, aliasName);
                _joins.Add(new Tuple<IAlias, Join>(alias, join));
            }

            return join;
        }

        internal IAlias BindTypeCriteria() {
            // ([ContentItemVersionRecord] >join> [ContentItemRecord]) >join> [ContentType]
            return BindCriteriaByAlias(BindItemCriteria(), "ContentType", "ct");
        }

        internal IAlias BindItemCriteria() {
            // [ContentItemVersionRecord] >join> [ContentItemRecord]
            return BindCriteriaByAlias(BindItemVersionCriteria(), typeof(ContentItemRecord).Name, "ci");
        }

        internal IAlias BindItemVersionCriteria() {
            return _from ?? (_from = new Join(typeof(ContentItemVersionRecord).FullName, "civ", ""));
        }

        internal IAlias BindPartCriteria<TRecord>() where TRecord : ContentPartRecord {
            return BindPartCriteria(typeof(TRecord));
        }

        internal IAlias BindPartCriteria(Type contentPartRecordType) {
            if (!contentPartRecordType.IsSubclassOf(typeof(ContentPartRecord))) {
                throw new ArgumentException("The type must inherit from ContentPartRecord", "contentPartRecordType");
            }

            if (contentPartRecordType.IsSubclassOf(typeof(ContentPartVersionRecord))) {
                return BindCriteriaByPath(BindItemVersionCriteria(), contentPartRecordType.Name);
            }
            return BindCriteriaByPath(BindItemCriteria(), contentPartRecordType.Name);
        }

        internal void Where(IAlias alias, Action<IHqlExpressionFactory> predicate) {
            _wheres.Add(new Tuple<IAlias, Action<IHqlExpressionFactory>>(alias, predicate));
        }
        
        internal IAlias ApplyHqlVersionOptionsRestrictions(VersionOptions versionOptions) {
            var alias = BindItemVersionCriteria();

            if (versionOptions == null) {
                Where(alias, x => x.Eq("Published", true));
            }
            else if (versionOptions.IsPublished) {
                Where(alias, x => x.Eq("Published", true));
            }
            else if (versionOptions.IsLatest) {
                Where(alias, x => x.Eq("Latest", true));
            }
            else if (versionOptions.IsDraft) {
                Where(alias, x => x.And(y => y.Eq("Latest", true), y => y.Eq("Published", false)));
            }
            else if (versionOptions.IsAllVersions) {
                // no-op... all versions will be returned by default
            }
            else {
                throw new ApplicationException("Invalid VersionOptions for content query");
            }

            return alias;
        }

        public IHqlQuery Join(Action<IAliasFactory> alias) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);
            return this;
        }

        public IHqlQuery Where(Action<IAliasFactory> alias, Action<IHqlExpressionFactory> predicate) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);
            Where(aliasFactory.Current, predicate);
            return this;
        }

        public IHqlQuery OrderBy(Action<IAliasFactory> alias, Action<IHqlSortFactory> order) {
            var aliasFactory = new DefaultAliasFactory(this);
            alias(aliasFactory);

            _sortings.Add(new Tuple<IAlias, Action<IHqlSortFactory>>(aliasFactory.Current, order));
            return this;
        }

        public IHqlQuery ForType(params string[] contentTypeNames) {
            if (contentTypeNames != null && contentTypeNames.Length != 0) {
                Where(BindTypeCriteria(), x => x.InG("Name", contentTypeNames));
            }
            return this;
        }

        public IHqlQuery ForVersion(VersionOptions options) {
            _versionOptions = options;
            return this;
        }

        public IHqlQuery<T> ForPart<T>() where T : IContent {
            return new DefaultHqlQuery<T>(this);
        }

        public IEnumerable<ContentItem> List() {
            return Slice(0, 0);
        }

        public IEnumerable<ContentItem> Slice(int skip, int count) {
            ApplyHqlVersionOptionsRestrictions(_versionOptions);
            cacheable = true;
            
            var hql = ToHql(false);

            var query = _session
                .CreateQuery(hql)
                .SetCacheable(cacheable)
                ;

            if (skip != 0) {
                query.SetFirstResult(skip);
            }
            if (count != 0 && count != Int32.MaxValue) {
                query.SetMaxResults(count);
            }

            var ids = query
                .SetResultTransformer(Transformers.AliasToEntityMap)
                .List<IDictionary>()
                .Select(x => (int)x["Id"]);

            return ContentManager.GetManyByVersionId(ids, QueryHints.Empty);
        }

        public int Count() {
            ApplyHqlVersionOptionsRestrictions(_versionOptions);
            var hql = ToHql(true);
            hql = "select count(Id) from Orchard.ContentManagement.Records.ContentItemVersionRecord where Id in ( " + hql + " )";
            return Convert.ToInt32(_session.CreateQuery(hql)
                           .SetCacheable(true)
                           .UniqueResult())
                ;
        }

        public string ToHql(bool count) {
            var sb = new StringBuilder();

            if (count) {
                sb.Append("select distinct civ.Id as Id").AppendLine();
            }
            else {
                sb.Append("select distinct civ.Id as Id");

                // add sort properties in the select
                foreach (var sort in _sortings) {
                    var sortFactory = new DefaultHqlSortFactory();
                    sort.Item2(sortFactory);

                    if (!sortFactory.Randomize) {
                        sb.Append(", ");
                        sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName);
                    }
                    else {
                        // select distinct can't be used with newid()
                        cacheable = false;
                        sb.Replace("select distinct", "select ");
                    }
                }

                sb.AppendLine();
            }

            sb.Append("from ").Append(_from.TableName).Append(" as ").Append(_from.Name).AppendLine();

            foreach (var join in _joins) {
                sb.Append(join.Item2.Type).Append(" ").Append(join.Item1.Name + "." + join.Item2.TableName).Append(" as ").Append(join.Item2.Name).AppendLine();
            }

            // generating where clause
            if (_wheres.Any()) {
                sb.Append("where ");

                var expressions = new List<string>();

                foreach (var where in _wheres) {
                    var expressionFactory = new DefaultHqlExpressionFactory();
                    where.Item2(expressionFactory);
                    expressions.Add(expressionFactory.Criterion.ToHql(where.Item1));
                }

                sb.Append("(").Append(String.Join(") AND (", expressions.ToArray())).Append(")").AppendLine();
            }

            // generating order by clause
            bool firstSort = true;
            foreach (var sort in _sortings) {
                if (!firstSort) {
                    sb.Append(", ");
                }
                else {
                    sb.Append("order by ");
                    firstSort = false;
                }

                var sortFactory = new DefaultHqlSortFactory();
                sort.Item2(sortFactory);

                if (sortFactory.Randomize) {

                    string command = null;

                    foreach (var sqlStatementProvider in _sqlStatementProviders) {
                        if (!String.Equals(sqlStatementProvider.DataProvider, _shellSettings.DataProvider)) {
                            continue;
                        }

                        command = sqlStatementProvider.GetStatement("random") ?? command;
                    }

                    if (command != null) {
                        sb.Append(command);    
                    }
                }
                else {
                    sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName);
                    if (!sortFactory.Ascending) {
                        sb.Append(" desc");
                    }
                }
            }

            // no order clause was specified, use a default sort order, unless it's a count
            // query hence it doesn't need one
            if (firstSort && !count) {
                sb.Append("order by civ.Id");
            }

            return sb.ToString();
        }

    }

    public class DefaultHqlQuery<TPart> : IHqlQuery<TPart> where TPart : IContent {
        private readonly DefaultHqlQuery _query;

        public DefaultHqlQuery(DefaultHqlQuery query) {
            _query = query;
        }

        public IContentManager ContentManager {
            get { return _query.ContentManager; }
        }

        public IHqlQuery<TPart> ForType(params string[] contentTypes) {
            _query.ForType(contentTypes);
            return new DefaultHqlQuery<TPart>(_query);
        }

        public IHqlQuery<TPart> ForVersion(VersionOptions options) {
            _query.ForVersion(options);
            return new DefaultHqlQuery<TPart>(_query);
        }

        IEnumerable<TPart> IHqlQuery<TPart>.List() {
            return _query.List().AsPart<TPart>();
        }
{'fraction_non_alphanumeric': 0.09489531487803875, 'fraction_numerical': 0.0020978158035457203, 'mean_word_length': 1.7996315062183326, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3514075', 'n_tokens_mistral': 6235, 'n_tokens_neox': 5877, 'n_words': 1322}