<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CinchORM;

namespace CinchORM
{
    public static class Cinch
    {
        /// <summary>
        /// Checks if an object exists using the primary key.
        /// </summary>
        /// <param name="obj">The object to be searched.</param>
        /// <returns>The number of existent objects.</returns>
        public static int Exists<T>(T obj) where T : ModelBase
        {
            string query = String.Format(Queries.Exists, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified, obj.PrimaryKeyFullyQualified);

            if (obj.ID > 0)
            {
                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("value", SqlDbType.Int, obj.ID);

                    try
                    {
                        return dc.ExecuteScalarInt();
                    }
                    catch (Exception ex)
                    {
                        //do something useful here
                        throw ex;
                    }
                }
            }
            else
                throw new ApplicationException(String.Format("Could not check Existance for {0} because the objects primary key value is not set", obj.objName), new NullReferenceException());
        }

        /// <summary>
        /// Checks if an object exists using any of its properties.
        /// </summary>
        /// <param name="obj">The object to be searched.</param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <returns>The number of existent objects.</returns>
        public static int Exists<T>(T obj, PropertyInfo column, object value) where T : ModelBase
        {
            string query = String.Format(Queries.Exists, obj.TableNameFullyQualified, obj.TableName, String.Format("[{0}].[{1}]", obj.TableName, column.Name), obj.PrimaryKeyFullyQualified);
            if (value != null)
            {
                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("value", Conversion.GetSqlDbType(value.GetType()), value);

                    try
                    {
                        return dc.ExecuteScalarInt();
                    }
                    catch (Exception ex)
                    {
                        //do something useful here
                        throw ex;
                    }
                }
            }
            else
                throw new ApplicationException(String.Format("Could not check Existance for {0} because the column value is not set", obj.objName), new NullReferenceException());
        }

        /// <summary>
        /// Find first by primary key of existing object
        /// </summary>
        /// <returns></returns>
        public static T FindFirst<T>(T obj) where T : ModelBase
        {

            if (obj.ID > 0)
            {
                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);
                    dc.SetQuery(query);
                    dc.AddParameter("id", SqlDbType.Int, obj.ID);

                    return dc.FillObject<T>();
                }
            }
            else
                throw new ApplicationException(String.Format("Could not FindFirst for {0} because the objects primary key value is not set", obj.objName), new NullReferenceException());

        }

        /// <summary>
        /// Find first based on explicit primary key value
        /// </summary>
        /// <returns></returns>
        public static T FindFirst<T>(T obj, int ID) where T : ModelBase
        {
            T result = default(T);

            if (obj != null)
            {
                string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);

                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("id", SqlDbType.Int, ID);

                    result = dc.FillObject<T>();
                }
            }

            return result;
        }

        /// <summary>
        /// Find first based on explicit primary key value
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="ID">The primary key to be searched.</param>
        /// <returns>The object if found; null, otherwise.</returns>
        public static T FindFirst<T>(int ID) where T : ModelBase, new()
        {
            var obj = new T();

            return Cinch.FindFirst<T>(obj, ID);
        }

        /// <summary>
        /// Finds all as List based on where / param
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="where"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static List<T> Find<T>(T obj, string where = null, object[] param = null) where T : ModelBase
        {
            List<T> result = new List<T>();

            if (obj != null)
            {
                CinchMapping mappings = Mapper.MapQuery<T>(obj, where, param);

                //make sure we have a WHERE 
                if (!string.IsNullOrWhiteSpace(mappings.QueryString) &&
                    mappings.QueryString.ToLowerInvariant().IndexOf("where") == -1)
                {
                    mappings.QueryString = String.Format("WHERE {0}", mappings.QueryString);
                }

                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.Find, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, mappings.QueryString);
                    dc.SetQuery(query);

                    if (mappings.SqlParams != null && mappings.SqlParams.Count > 0)
                        dc.AddParameters(mappings.SqlParams);

                    result = dc.FillList<T>();
                }
            }

            return result;
        }

        public static int Delete<T>(
            string where = null,
            object[] param = null) where T : ModelBase, new()
        {
            T obj = new T();

            return Cinch.Delete(obj, where, param);
        }

        public static int Delete<T>(T obj, string where = null, object[] param = null) where T : ModelBase
        {
            int result = 0;

            if (obj != null)
            {
                CinchMapping mappings = Mapper.MapQuery<T>(obj, where, param);

                //make sure we have a WHERE 
                if (!string.IsNullOrWhiteSpace(mappings.QueryString) &&
                    mappings.QueryString.ToLowerInvariant().IndexOf("where") == -1)
                {
                    mappings.QueryString = String.Format("WHERE {0}", mappings.QueryString);
                }

                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.Delete, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, mappings.QueryString);
                    dc.SetQuery(query);

                    if (mappings.SqlParams != null && mappings.SqlParams.Count > 0)
                        dc.AddParameters(mappings.SqlParams);

                    return dc.ExecuteScalarInt();
                }
            }

            return result;
        }

        public static int Count<T>(<commit_msg>Fixed the delete method.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CinchORM;

namespace CinchORM
{
    public static class Cinch
    {
        /// <summary>
        /// Checks if an object exists using the primary key.
        /// </summary>
        /// <param name="obj">The object to be searched.</param>
        /// <returns>The number of existent objects.</returns>
        public static int Exists<T>(T obj) where T : ModelBase
        {
            string query = String.Format(Queries.Exists, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified, obj.PrimaryKeyFullyQualified);

            if (obj.ID > 0)
            {
                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("value", SqlDbType.Int, obj.ID);

                    try
                    {
                        return dc.ExecuteScalarInt();
                    }
                    catch (Exception ex)
                    {
                        //do something useful here
                        throw ex;
                    }
                }
            }
            else
                throw new ApplicationException(String.Format("Could not check Existance for {0} because the objects primary key value is not set", obj.objName), new NullReferenceException());
        }

        /// <summary>
        /// Checks if an object exists using any of its properties.
        /// </summary>
        /// <param name="obj">The object to be searched.</param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <returns>The number of existent objects.</returns>
        public static int Exists<T>(T obj, PropertyInfo column, object value) where T : ModelBase
        {
            string query = String.Format(Queries.Exists, obj.TableNameFullyQualified, obj.TableName, String.Format("[{0}].[{1}]", obj.TableName, column.Name), obj.PrimaryKeyFullyQualified);
            if (value != null)
            {
                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("value", Conversion.GetSqlDbType(value.GetType()), value);

                    try
                    {
                        return dc.ExecuteScalarInt();
                    }
                    catch (Exception ex)
                    {
                        //do something useful here
                        throw ex;
                    }
                }
            }
            else
                throw new ApplicationException(String.Format("Could not check Existance for {0} because the column value is not set", obj.objName), new NullReferenceException());
        }

        /// <summary>
        /// Find first by primary key of existing object
        /// </summary>
        /// <returns></returns>
        public static T FindFirst<T>(T obj) where T : ModelBase
        {

            if (obj.ID > 0)
            {
                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);
                    dc.SetQuery(query);
                    dc.AddParameter("id", SqlDbType.Int, obj.ID);

                    return dc.FillObject<T>();
                }
            }
            else
                throw new ApplicationException(String.Format("Could not FindFirst for {0} because the objects primary key value is not set", obj.objName), new NullReferenceException());

        }

        /// <summary>
        /// Find first based on explicit primary key value
        /// </summary>
        /// <returns></returns>
        public static T FindFirst<T>(T obj, int ID) where T : ModelBase
        {
            T result = default(T);

            if (obj != null)
            {
                string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);

                using (DataConnect dc = new DataConnect(query, CommandType.Text))
                {
                    dc.SetQuery(query);
                    dc.AddParameter("id", SqlDbType.Int, ID);

                    result = dc.FillObject<T>();
                }
            }

            return result;
        }

        /// <summary>
        /// Find first based on explicit primary key value
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="ID">The primary key to be searched.</param>
        /// <returns>The object if found; null, otherwise.</returns>
        public static T FindFirst<T>(int ID) where T : ModelBase, new()
        {
            var obj = new T();

            return Cinch.FindFirst<T>(obj, ID);
        }

        /// <summary>
        /// Finds all as List based on where / param
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="where"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static List<T> Find<T>(T obj, string where = null, object[] param = null) where T : ModelBase
        {
            List<T> result = new List<T>();

            if (obj != null)
            {
                CinchMapping mappings = Mapper.MapQuery<T>(obj, where, param);

                //make sure we have a WHERE 
                if (!string.IsNullOrWhiteSpace(mappings.QueryString) &&
                    mappings.QueryString.ToLowerInvariant().IndexOf("where") == -1)
                {
                    mappings.QueryString = String.Format("WHERE {0}", mappings.QueryString);
                }

                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.Find, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, mappings.QueryString);
                    dc.SetQuery(query);

                    if (mappings.SqlParams != null && mappings.SqlParams.Count > 0)
                        dc.AddParameters(mappings.SqlParams);

                    result = dc.FillList<T>();
                }
            }

            return result;
        }

        public static int Delete<T>(
            string where = null,
            object[] param = null) where T : ModelBase, new()
        {
            T obj = new T();

            return Cinch.Delete(obj, where, param);
        }

        public static int Delete<T>(T obj, string where = null, object[] param = null) where T : ModelBase
        {
            int result = 0;

            if (obj != null)
            {
                CinchMapping mappings = Mapper.MapQuery<T>(obj, where, param);

                //make sure we have a WHERE 
                if (!string.IsNullOrWhiteSpace(mappings.QueryString) &&
                    mappings.QueryString.ToLowerInvariant().IndexOf("where") == -1)
                {
                    mappings.QueryString = String.Format("WHERE {0}", mappings.QueryString);
                }

                using (DataConnect dc = new DataConnect(null, CommandType.Text))
                {
                    string query = String.Format(Queries.Delete, obj.TableNameFullyQualified, mappings.QueryString);
                    dc.SetQuery(query);

                    if (mappings.SqlParams != null && mappings.SqlParams.Count > 0)
                        dc.AddParameters(mappings.SqlParams);

                    return dc.ExecuteScalarInt();
                }
            }

            return result;
        }

        public static int Count<T>(
{'fraction_non_alphanumeric': 0.10756103637269557, 'fraction_numerical': 0.0017438963627304435, 'mean_word_length': 1.4699277034302416, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 0, '>': 131, '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': '7506358', 'n_tokens_mistral': 4100, 'n_tokens_neox': 3857, 'n_words': 1049}