<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Modl.Query;

namespace Modl.Linq.Parsers
{
    internal class WhereParser<T> : ExpressionVisitor where T : Modl<T>, new()
    {
        protected Select<T> select;

        internal WhereParser(Select<T> select)
        {
            this.select = select;
        }

        //internal void ParseWhere(Expression expression)
        //{
        //    Visit(expression);
        //}

        protected override Expression VisitBinary(BinaryExpression node)
        {
            //string value = ExpressionTreeHelpers.GetValueFromEqualsExpression(node, typeof(T), (((MemberExpression)node.Left).Member.Name));
            
            string field = Modl<T>.GetFieldName(((MemberExpression)node.Left).Member.Name);
            object value = ExpressionTreeHelpers.GetValueFromExpression(node.Right);

            select.Where(field).EqualTo(value);

            return base.VisitBinary(node);
        }
    }
}
<commit_msg>Cleaned up where parser a bit.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Modl.Query;
using Modl.Exceptions;

namespace Modl.Linq.Parsers
{
    internal class WhereParser<T> : ExpressionVisitor where T : Modl<T>, new()
    {
        protected Select<T> select;

        internal WhereParser(Select<T> select)
        {
            this.select = select;
        }

        //internal void ParseWhere(Expression expression)
        //{
        //    Visit(expression);
        //}

        protected override Expression VisitBinary(BinaryExpression node)
        {
            var fields = GetFields(node);
            var where = select.Where(fields.Key);

            if (node.NodeType == ExpressionType.Equal)
                where.EqualTo(fields.Value);
            else
                throw new NotImplementedException("Operation not implemented");


            
            //string field = Modl<T>.GetFieldName(((MemberExpression)node.Left).Member.Name);
            //object value = ExpressionTreeHelpers.GetValueFromExpression(node.Right);

            

            return base.VisitBinary(node);
        }

        protected KeyValuePair<string, object> GetFields(BinaryExpression node)
        {
            if (node.Left is ConstantExpression && node.Right is ConstantExpression)
                throw new InvalidQueryException("Unable to compare 2 constants.");

            if (node.Left is MemberExpression)
                return GetValues(node.Left, node.Right);
            else
                return GetValues(node.Right, node.Left);
        }

        protected KeyValuePair<string, object> GetValues(Expression field, Expression value)
        {
            return new KeyValuePair<string, object>((string)GetValue(field), GetValue(value));
        }

        protected object GetValue(Expression expression)
        {
            if (expression is ConstantExpression)
                return ((ConstantExpression)expression).Value;
            else
                return Modl<T>.GetFieldName(((MemberExpression)expression).Member.Name);
        }
    }
}

{'fraction_non_alphanumeric': 0.09678416821273964, 'fraction_numerical': 0.00030921459492888067, 'mean_word_length': 2.254527162977867, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3807836', 'n_tokens_mistral': 816, 'n_tokens_neox': 799, 'n_words': 180}