<commit_before>using System;
using System.Collections.Generic;
using RabbitDB.Caching;
using RabbitDB.Entity;
using RabbitDB.Mapping;
using RabbitDB.Query;
using RabbitDB.Materialization;

namespace RabbitDB.Storage
{
    internal class DbEntityPersister
    {
        private IDbProvider _dbProvider;

        internal DbEntityPersister(IDbProvider dbProvider)
        {
            _dbProvider = dbProvider;
        }

        /// <summary>
        /// This method is ONLY for POCOs 
        /// who inherit from Entity.Entity
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        internal bool PersistChanges<TEntity>(TEntity entity) where TEntity : Entity.Entity
        {
            EntityInfo entityInfo = EntityInfoCacheManager.GetEntityInfo(entity);
            if (entity.MarkedForDeletion)
            {
                // Entity was already deleted
                if (entityInfo.EntityState == EntityState.Deleted)
                    return false;

                return Delete(entity, entityInfo);
            }
            // Entity was already deleted
            // or is not yet loaded!!
            else if (entityInfo.EntityState == EntityState.Deleted || entityInfo.EntityState == EntityState.None)
            {
                return Insert(entity, entityInfo);
            }
            else if (entityInfo.EntityState != EntityState.Deleted)
            {
                Tuple<bool, string, QueryParameterCollection> tuple = PrepareForUpdate<TEntity>(entity, entityInfo);
                if (tuple.Item1 == false) return false;

                return Update<TEntity>(entity, entityInfo, new SqlQuery(tuple.Item2, tuple.Item3));
            }

            return false;
        }

        private bool Update<TEntity>(TEntity entity, EntityInfo entityInfo, SqlQuery sqlQuery) where TEntity : Entity.Entity
        {
            Update<TEntity>(sqlQuery);
            entity.RaiseEntityUpdated();
            entityInfo.EntityState = EntityState.Updated;
            return true;
        }

        private bool Insert<TEntity>(TEntity entity, EntityInfo entityInfo) where TEntity : Entity.Entity
        {
            Insert(entity);
            entity.RaiseEntityInserted();
            entityInfo.EntityState = EntityState.Inserted;
            return true;
        }

        private bool Delete<TEntity>(TEntity entity, EntityInfo entityInfo) where TEntity : Entity.Entity
        {
            Delete(entity);
            entity.RaiseEntityDeleted();
            entityInfo.EntityState = EntityState.Deleted;
            return true;
        }

        private Tuple<bool, string, QueryParameterCollection> PrepareForUpdate<TEntity>(TEntity entity, EntityInfo entityInfo) where TEntity : Entity.Entity
        {
            // Any changes made to entity?!
            KeyValuePair<string, object>[] valuesToUpdate = EntityHashSetManager.ComputeUpdateValues(entity, entityInfo);

            if (valuesToUpdate == null || valuesToUpdate.Length == 0)
                return new Tuple<bool, string, QueryParameterCollection>(false, null, null);

            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<bool, string, QueryParameterCollection>(true, updateStatement, queryParameterCollection);
        }

        internal void Update<TEntity>(IQuery query)
        {
            _dbProvider.ExecuteCommand(query);
        }

        internal void Delete<TEntity>(TEntity entity)
        {
            TableInfo tableInfo = TableInfo<TEntity>.GetTableInfo;
            string deleteStatement = tableInfo.CreateDeleteStatement(_dbProvider);
            QueryParameterCollection arguments = QueryParameterCollection.Create<TEntity>(tableInfo.GetPrimaryKeyValues(entity));

            _dbProvider.ExecuteCommand(new SqlQuery(deleteStatement, arguments));
        }

        internal void Insert<TEntity>(TEntity entity)
        {
            TableInfo tableInfo = TableInfo<TEntity>.GetTableInfo;
            string insertStatement = tableInfo.CreateInsertStatement(_dbProvider);
            QueryParameterCollection arguments = QueryParameterCollection.Create<TEntity>(Utils.Utils.GetEntityArguments(entity, tableInfo));

            object insertId = _dbProvider.ExecuteScalar<object>(new SqlQuery(insertStatement, arguments));
            tableInfo.SetAutoNumber<TEntity>(entity, insertId);
        }<commit_msg>Refactored and removed method to other class.<commit_after>using System;
using System.Collections.Generic;
using RabbitDB.Caching;
using RabbitDB.Entity;
using RabbitDB.Expressions;
using RabbitDB.Mapping;
using RabbitDB.Materialization;
using RabbitDB.Query;

namespace RabbitDB.Storage
{
    internal class DbEntityPersister
    {
        private IDbProvider _dbProvider;

        internal DbEntityPersister(IDbProvider dbProvider)
        {
            _dbProvider = dbProvider;
        }

        /// <summary>
        /// This method is ONLY for POCOs 
        /// who inherit from Entity.Entity
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        internal bool PersistChanges<TEntity>(TEntity entity) where TEntity : Entity.Entity
        {
            EntityInfo entityInfo = EntityInfoCacheManager.GetEntityInfo(entity);
            if (entity.MarkedForDeletion)
            {
                // Entity was already deleted
                if (entityInfo.EntityState == EntityState.Deleted)
                    return false;

                return Delete(entity, entityInfo);
            }
            // Entity was already deleted
            // or is not yet loaded!!
            else if (entityInfo.EntityState == EntityState.Deleted || entityInfo.EntityState == EntityState.None)
            {
                return Insert(entity, entityInfo);
            }
            else if (entityInfo.EntityState != EntityState.Deleted)
            {
                Tuple<bool, string, QueryParameterCollection> tuple = PrepareForUpdate<TEntity>(entity, entityInfo);
                if (tuple.Item1 == false) return false;

                return Update<TEntity>(entity, entityInfo, new SqlQuery(tuple.Item2, tuple.Item3));
            }

            return false;
        }

        private bool Update<TEntity>(TEntity entity, EntityInfo entityInfo, SqlQuery sqlQuery) where TEntity : Entity.Entity
        {
            Update<TEntity>(sqlQuery);
            entity.RaiseEntityUpdated();
            entityInfo.EntityState = EntityState.Updated;
            return true;
        }

        private bool Insert<TEntity>(TEntity entity, EntityInfo entityInfo) where TEntity : Entity.Entity
        {
            Insert(entity);
            entity.RaiseEntityInserted();
            entityInfo.EntityState = EntityState.Inserted;
            return true;
        }

        private bool Delete<TEntity>(TEntity entity, EntityInfo entityInfo) where TEntity : Entity.Entity
        {
            Delete(entity);
            entity.RaiseEntityDeleted();
            entityInfo.EntityState = EntityState.Deleted;
            return true;
        }

        private Tuple<bool, string, QueryParameterCollection> PrepareForUpdate<TEntity>(TEntity entity, EntityInfo entityInfo)
        {
            // Any changes made to entity?!
            KeyValuePair<string, object>[] valuesToUpdate = EntityHashSetManager.ComputeUpdateValues(entity, entityInfo);

            if (valuesToUpdate == null || valuesToUpdate.Length == 0)
                return new Tuple<bool, string, QueryParameterCollection>(false, null, null);

            Tuple<string, QueryParameterCollection> result = UpdateTableBuilder<TEntity>.PrepareForUpdate<TEntity>(entity, _dbProvider, valuesToUpdate);
            return new Tuple<bool, string, QueryParameterCollection>(true, result.Item1, result.Item2);
        }

        internal void Update<TEntity>(IQuery query)
        {
            _dbProvider.ExecuteCommand(query);
        }

        internal void Delete<TEntity>(TEntity entity)
        {
            TableInfo tableInfo = TableInfo<TEntity>.GetTableInfo;
            string deleteStatement = tableInfo.CreateDeleteStatement(_dbProvider);
            QueryParameterCollection arguments = QueryParameterCollection.Create<TEntity>(tableInfo.GetPrimaryKeyValues(entity));

            _dbProvider.ExecuteCommand(new SqlQuery(deleteStatement, arguments));
        }

        internal void Insert<TEntity>(TEntity entity)
        {
            TableInfo tableInfo = TableInfo<TEntity>.GetTableInfo;
            string insertStatement = tableInfo.CreateInsertStatement(_dbProvider);
            QueryParameterCollection arguments = QueryParameterCollection.Create<TEntity>(Utils.Utils.GetEntityArguments(entity, tableInfo));

            object insertId = _dbProvider.ExecuteScalar<object>(new SqlQuery(insertStatement, arguments));
            tableInfo.SetAutoNumber<TEntity>(entity, insertId);
        }
{'fraction_non_alphanumeric': 0.08419925013390465, 'fraction_numerical': 0.0010712372790573112, 'mean_word_length': 2.4424778761061945, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, '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': '2754066', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2244, 'n_words': 498}