Nhibernate QueryOver collation without hard coded column name

前端 未结 1 1117
迷失自我
迷失自我 2021-01-15 17:19

So I have the following sql

SELECT * FROM table Where Name COLLATE LATIN1_GENERAL_CI_AI LIKE \'myText%\'

which I want to implement using QueryOver

At

相关标签:
1条回答
  • 2021-01-15 18:11

    I've implemented it this way. Not sure if there is any better way...

    I. The like expression, profiting from the existing Like expression

    public class LikeCollationExpression : LikeExpression
    {
        const string CollationDefinition = " COLLATE {0} ";
        const string Latin_CI_AI = "LATIN1_GENERAL_CI_AI";
    
        // just a set of constructors
        public LikeCollationExpression(string propertyName, string value, char? escapeChar, bool ignoreCase) : base(propertyName, value, escapeChar, ignoreCase) { }
        public LikeCollationExpression(IProjection projection, string value, MatchMode matchMode) : base(projection, value, matchMode) { }
        public LikeCollationExpression(string propertyName, string value) : base(propertyName, value) { }
        public LikeCollationExpression(string propertyName, string value, MatchMode matchMode) : base(propertyName, value, matchMode) { }
        public LikeCollationExpression(string propertyName, string value, MatchMode matchMode, char? escapeChar, bool ignoreCase) : base(propertyName, value, matchMode, escapeChar, ignoreCase) { }
    
        // here we call the base and append the COLLATE
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
        {
            // base LIKE
            var result = base.ToSqlString(criteria, criteriaQuery, enabledFilters);
    
            var sqlStringBuilder = new SqlStringBuilder(result);
    
            // extend it with collate
            sqlStringBuilder.Add(string.Format(CollationDefinition, Latin_CI_AI ));
    
            return sqlStringBuilder.ToSqlString();
        }
    }
    

    II. the custom extension method

    public static class QueryOverExt
    {
        // here: WhereLikeCiAi() 
        public static IQueryOver<TRoot, TSubType> WhereLikeCiAi<TRoot, TSubType>(
            this IQueryOver<TRoot, TSubType> query
            , Expression<Func<TSubType, object>> expression
            , string value
            , MatchMode matchMode)
        {
            var name = ExpressionProcessor.FindMemberExpression(expression.Body);
            query
                .UnderlyingCriteria
                .Add
                (
                    new LikeCollationExpression(name, value, matchMode)
                );
            return query;
        }
    }
    

    III. the usage anyhwere in the QueryOverAPI

    ...
    query.WhereLikeCiAi(c => c.Name, "searchedString", MatchMode.Anywhere);
    
    0 讨论(0)
提交回复
热议问题