<commit_before>/*
 * Parts of the code used in this class 
 * originate from the OS project SqlFu:
 * https://github.com/sapiens/SqlFu
 * Licence used: Apache Licence v2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * If anyone feels offended by the use of this code
 * please feel free to send me an e-Mail: <EMAIL>
 */

using System;
using System.Linq.Expressions;
using RabbitDB.Mapping;
using RabbitDB.Storage;

namespace RabbitDB.Expressions
{
    internal interface IBuildUpdateTable<T>
    {
        IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, Expression<Func<T, object>> statement);
        IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, object value);
        void Set(string column, object value);
        void Where(Expression<Func<T, bool>> criteria);
    }

    internal class UpdateTableBuilder<T> : IBuildUpdateTable<T>
    {
        private readonly SqlExpressionBuilder<T> _builder;
        private TableInfo _tableInfo;
        private IDbProvider _dbProvider;

        public UpdateTableBuilder(IDbProvider dbProvider)
        {
            _builder = new SqlExpressionBuilder<T>(dbProvider);
            _dbProvider = dbProvider;
            _tableInfo = TableInfo<T>.GetTableInfo;
            _builder.Append(_tableInfo.GetBaseUpdate(dbProvider));
        }

        public IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, Expression<Func<T, object>> statement)
        {
            _builder.Append(string.Format(" {0}=", _dbProvider.EscapeName(_tableInfo.ResolveColumnName(column.Body.GetPropertyName()))));
            _builder.Write(statement);
            _builder.Append(",");
            return this;
        }

        public IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, object value)
        {
            Set(column.Body.GetPropertyName(), value);
            return this;
        }

        public void Set(string column, object value)
        {
            _builder.Append(string.Format(" {0}=@{1},", _dbProvider.EscapeName(_tableInfo.ResolveColumnName(column)), _builder.Parameters.NextIndex));
            _builder.Parameters.Add(value);
        }

        public void Where(Expression<Func<T, bool>> criteria)
        {
            _builder.EndEnumeration();
            _builder.Where(criteria);
        }

        public string GetSql()
        {
            return _builder.ToString();
        }

        public object[] GetParameters()
        {
            return _builder.Parameters.ToArray();
        }
    }
}<commit_msg>Method to prepare statement and parameters.<commit_after>/*
 * Parts of the code used in this class 
 * originate from the OS project SqlFu:
 * https://github.com/sapiens/SqlFu
 * Licence used: Apache Licence v2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * If anyone feels offended by the use of this code
 * please feel free to send me an e-Mail: <EMAIL>
 */

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using RabbitDB.Mapping;
using RabbitDB.Query;
using RabbitDB.Storage;

namespace RabbitDB.Expressions
{
    internal interface IBuildUpdateTable<T>
    {
        IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, Expression<Func<T, object>> statement);
        IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, object value);
        void Set(string column, object value);
        void Where(Expression<Func<T, bool>> criteria);
    }

    internal class UpdateTableBuilder<T> : IBuildUpdateTable<T>
    {
        private readonly SqlExpressionBuilder<T> _builder;
        private TableInfo _tableInfo;
        private IDbProvider _dbProvider;

        public UpdateTableBuilder(IDbProvider dbProvider)
        {
            _builder = new SqlExpressionBuilder<T>(dbProvider);
            _dbProvider = dbProvider;
            _tableInfo = TableInfo<T>.GetTableInfo;
            _builder.Append(_tableInfo.GetBaseUpdate(dbProvider));
        }

        public IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, Expression<Func<T, object>> statement)
        {
            _builder.Append(string.Format(" {0}=", _dbProvider.EscapeName(_tableInfo.ResolveColumnName(column.Body.GetPropertyName()))));
            _builder.Write(statement);
            _builder.Append(",");
            return this;
        }

        public IBuildUpdateTable<T> Set(Expression<Func<T, object>> column, object value)
        {
            Set(column.Body.GetPropertyName(), value);
            return this;
        }

        public void Set(string column, object value)
        {
            _builder.Append(string.Format(" {0}=@{1},", _dbProvider.EscapeName(_tableInfo.ResolveColumnName(column)), _builder.Parameters.NextIndex));
            _builder.Parameters.Add(value);
        }

        public void Where(Expression<Func<T, bool>> criteria)
        {
            _builder.EndEnumeration();
            _builder.Where(criteria);
        }

        internal static Tuple<string, QueryParameterCollection> PrepareForUpdate<TEntity>(TEntity entity, IDbProvider dbProvider, KeyValuePair<string, object>[] valuesToUpdate)
        {
            TableInfo tableInfo = TableInfo<TEntity>.GetTableInfo;
            string updateStatement = tableInfo.CreateUpdateStatement(dbProvider, valuesToUpdate);
            QueryParameterCollection queryParameterCollection = QueryParameterCollection.Create<TEntity>(new object[] { valuesToUpdate });
            queryParameterCollection.AddRange(tableInfo.GetPrimaryKeyValues<TEntity>(entity));

            return new Tuple<string, QueryParameterCollection>(updateStatement, queryParameterCollection);
        }

        public string GetSql()
        {
            return _builder.ToString();
        }

        public object[] GetParameters()
        {
            return _builder.Parameters.ToArray();
        }
    }
}
{'fraction_non_alphanumeric': 0.10319494276439432, 'fraction_numerical': 0.0023919357594396036, 'mean_word_length': 2.818656229615134, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4930347', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1557, 'n_words': 348}