<commit_before>﻿using System;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.SqlSyntax;

namespace Umbraco.Core.Persistence
{
    /// <summary>
    /// Extension methods adding strong types to PetaPoco's Sql Builder
    /// </summary>
    public static class PetaPocoSqlExtensions
    {
        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql From<T>(this Sql sql)
        {
            return From<T>(sql, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql From<T>(this Sql sql, ISqlSyntaxProvider sqlSyntax)
        {
            var type = typeof(T);
            var tableName = type.GetTableName();

            return sql.From(sqlSyntax.GetQuotedTableName(tableName));
        }

        public static Sql Where<T>(this Sql sql, Expression<Func<T, bool>> predicate)
        {
            var expresionist = new PocoToSqlExpressionHelper<T>();
            var whereExpression = expresionist.Visit(predicate);
            return sql.Where(whereExpression, expresionist.GetSqlParameters());
        }

        private static string GetFieldName<T>(Expression<Func<T, object>> fieldSelector, ISqlSyntaxProvider sqlSyntax)
        {
            var field = ExpressionHelper.FindProperty(fieldSelector) as PropertyInfo;
            var fieldName = field.GetColumnName();

            var type = typeof(T);
            var tableName = type.GetTableName();

            return sqlSyntax.GetQuotedTableName(tableName) + "." + sqlSyntax.GetQuotedColumnName(fieldName);
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql WhereIn<T>(this Sql sql, Expression<Func<T, object>> fieldSelector, IEnumerable values)
        {
            return sql.WhereIn(fieldSelector, values, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql WhereIn<T>(this Sql sql, Expression<Func<T, object>> fieldSelector, IEnumerable values, ISqlSyntaxProvider sqlSyntax)
        {
            var fieldName = GetFieldName(fieldSelector, sqlSyntax);
            return sql.Where(fieldName + " IN (@values)", new { values });
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql OrderBy<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember)
        {
            return OrderBy<TColumn>(sql, columnMember, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql OrderBy<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember, ISqlSyntaxProvider sqlSyntax)
        {
            var syntax = "(" + GetFieldName(columnMember, sqlSyntax) + ")";
            return sql.OrderBy(syntax);
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql OrderByDescending<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember)
        {<commit_msg>Add WhereAnyIn sql extension<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.SqlSyntax;

namespace Umbraco.Core.Persistence
{
    /// <summary>
    /// Extension methods adding strong types to PetaPoco's Sql Builder
    /// </summary>
    public static class PetaPocoSqlExtensions
    {
        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql From<T>(this Sql sql)
        {
            return From<T>(sql, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql From<T>(this Sql sql, ISqlSyntaxProvider sqlSyntax)
        {
            var type = typeof(T);
            var tableName = type.GetTableName();

            return sql.From(sqlSyntax.GetQuotedTableName(tableName));
        }

        public static Sql Where<T>(this Sql sql, Expression<Func<T, bool>> predicate)
        {
            var expresionist = new PocoToSqlExpressionHelper<T>();
            var whereExpression = expresionist.Visit(predicate);
            return sql.Where(whereExpression, expresionist.GetSqlParameters());
        }

        private static string GetFieldName<T>(Expression<Func<T, object>> fieldSelector, ISqlSyntaxProvider sqlSyntax)
        {
            var field = ExpressionHelper.FindProperty(fieldSelector) as PropertyInfo;
            var fieldName = field.GetColumnName();

            var type = typeof(T);
            var tableName = type.GetTableName();

            return sqlSyntax.GetQuotedTableName(tableName) + "." + sqlSyntax.GetQuotedColumnName(fieldName);
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql WhereIn<T>(this Sql sql, Expression<Func<T, object>> fieldSelector, IEnumerable values)
        {
            return sql.WhereIn(fieldSelector, values, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql WhereIn<T>(this Sql sql, Expression<Func<T, object>> fieldSelector, IEnumerable values, ISqlSyntaxProvider sqlSyntax)
        {
            var fieldName = GetFieldName(fieldSelector, sqlSyntax);
            return sql.Where(fieldName + " IN (@values)", new { values });
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql WhereAnyIn<TDto>(this Sql sql, Expression<Func<TDto, object>>[] fieldSelectors, IEnumerable values)
        {
            return sql.WhereAnyIn(fieldSelectors, values, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql WhereAnyIn<TDto>(this Sql sql, Expression<Func<TDto, object>>[] fieldSelectors, IEnumerable values, ISqlSyntaxProvider sqlSyntax)
        {
            var fieldNames = fieldSelectors.Select(x => GetFieldName(x, sqlSyntax)).ToArray();
            var sb = new StringBuilder();
            sb.Append("(");
            for (var i = 0; i < fieldNames.Length; i++)
            {
                if (i > 0) sb.Append(" OR ");
                sb.Append(fieldNames[i] + " IN (@values)");
            }
            sb.Append(")");
            return sql.Where(sb.ToString(), new { values });
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql OrderBy<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember)
        {
            return OrderBy<TColumn>(sql, columnMember, SqlSyntaxContext.SqlSyntaxProvider);
        }

        public static Sql OrderBy<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember, ISqlSyntaxProvider sqlSyntax)
        {
            var syntax = "(" + GetFieldName(columnMember, sqlSyntax) + ")";
            return sql.OrderBy(syntax);
        }

        [Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
        public static Sql OrderByDescending<TColumn>(this Sql sql, Expression<Func<TColumn, object>> columnMember)
        {
{'fraction_non_alphanumeric': 0.0978687605159843, 'fraction_numerical': 0.00028042624789680314, 'mean_word_length': 2.8661246612466122, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 67, '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': '5996022', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1824, 'n_words': 473}