<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace DatabaseConnection
{
    /// <summary>
    /// Class for inteacting with high scores table in the database
    /// </summary>
    public class HighScoresTable : DBTable
    {
        /// <summary>
        /// the number of high scores to track
        /// </summary>
        public static int NUM_HIGH_SCORES = 3;

        /// <summary>
        /// No argument constructor
        /// </summary>
        public HighScoresTable()
        {
        }

        /// <summary>
        /// Create a table for high scores with the following attributes
        /// high_score_id - INT (Primary Key)
        /// high_score - INT
        /// high_score_name - string
        /// high_score_game INT
        /// </summary>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool createTable()
        {
            string query = "CREATE TABLE IF NOT EXISTS high_scores (" +
                "high_score_id INT NOT NULL AUTO_INCREMENT," +
                "high_score INT NOT NULL," +
                "high_score_name varchar(50) NOT NULL," +
                "high_score_game INT," +
                "PRIMARY KEY (high_score_id)" +
                ")";
            bool success;
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;
            }
            else
            {
                this.errorMessage = "Failed to open database connection";
                success = false;
            }

            return success;
        }

        /// <summary>
        /// Removes a record from the database based on primary key
        /// </summary>
        /// <param name="primaryKey">high_score_id for the question to delete</param>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool deleteRecord(int primaryKey)
        {
            bool success;
            string qry = "DELETE FROM high_scores WHERE high_score_id=@h";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                cmd.Parameters.AddWithValue("@h", primaryKey);
                int rowsAffected = cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;
                if(rowsAffected==0)
                {
                    success = false;
                    errorMessage = "No rows were deleted";
                }
            }
            else
            {
                success = false;
                errorMessage = "Failed to open database connection";
            }
            return success;
        }

        /// <summary>
        /// Inserts a record into the database. This method will ignore the ID property of the passed object and assign an available unique id
        /// </summary>
        /// <param name="record">HighScoresTable object to be added</param>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool insertRecord(object record)
        {
            if(!(record is HighScoreData))
            {
                errorMessage = "Record is not HighScoreData type";
                return false;
            }
            bool success;
            string qry = "INSERT INTO high_scores(high_score, high_score_name, high_score_game) VALUES(@a, @b, @c)";
            
            if(this.openConnection())
            {
                HighScoreData d = (HighScoreData)record;
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                cmd.Parameters.AddWithValue("@a", d.score);
                cmd.Parameters.AddWithValue("@b", d.name);
                cmd.Parameters.AddWithValue("@c", d.gameID);
                int rowsAffected = cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;

                if (rowsAffected == 0)
                {
                    success = false;
                    errorMessage = "No rows were inserted";
                }

            }
            else
            {
                success = false;
                errorMessage = "Failed to open database connection";
            }

            return success;
        }
        /// <summary>
        /// reads the entire high score table
        /// </summary>
        /// <returns>List of HighScoreData objects. Each object in the list is a record from the table. This could be
        /// an empty list</returns>
        public override List<object> readAll()
        {
            List<object> records = new List<object>();
            string qry = "SELECT * FROM high_scores";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();

                while(dataReader.Read())
                {
                    records.Add(new HighScoreData(Int32.Parse(dataReader["high_score_id"].ToString()), Int32.Parse(dataReader["high_score"].ToString()), dataReader["high_score_name"].ToString(), Int32.Parse(dataReader["high_score_game"].ToString())));
                }
                dataReader.Close();
                this.closeConnection();
            }
            else
            {
                errorMessage = "Failed to open database connection";
            }
            return records;
        }
        /// <summary>
        /// Attempts to select one record from the database
        /// </summary>
        /// <param name="primaryKey">high_score_id of the record to select</param>
        /// <returns>The selected record as a HighScoreData object</returns>
        public override object readById(int primaryKey)
        {
            HighScoreData result;
            string qry = "SELECT * FROM high_scores WHERE high_score_id=@h";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);<commit_msg>Sort High Scores<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace DatabaseConnection
{
    /// <summary>
    /// Class for inteacting with high scores table in the database
    /// </summary>
    public class HighScoresTable : DBTable
    {
        /// <summary>
        /// the number of high scores to track
        /// </summary>
        public static int NUM_HIGH_SCORES = 3;

        /// <summary>
        /// No argument constructor
        /// </summary>
        public HighScoresTable()
        {
        }

        /// <summary>
        /// Create a table for high scores with the following attributes
        /// high_score_id - INT (Primary Key)
        /// high_score - INT
        /// high_score_name - string
        /// high_score_game INT
        /// </summary>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool createTable()
        {
            string query = "CREATE TABLE IF NOT EXISTS high_scores (" +
                "high_score_id INT NOT NULL AUTO_INCREMENT," +
                "high_score INT NOT NULL," +
                "high_score_name varchar(50) NOT NULL," +
                "high_score_game INT," +
                "PRIMARY KEY (high_score_id)" +
                ")";
            bool success;
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;
            }
            else
            {
                this.errorMessage = "Failed to open database connection";
                success = false;
            }

            return success;
        }

        /// <summary>
        /// Removes a record from the database based on primary key
        /// </summary>
        /// <param name="primaryKey">high_score_id for the question to delete</param>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool deleteRecord(int primaryKey)
        {
            bool success;
            string qry = "DELETE FROM high_scores WHERE high_score_id=@h";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                cmd.Parameters.AddWithValue("@h", primaryKey);
                int rowsAffected = cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;
                if(rowsAffected==0)
                {
                    success = false;
                    errorMessage = "No rows were deleted";
                }
            }
            else
            {
                success = false;
                errorMessage = "Failed to open database connection";
            }
            return success;
        }

        /// <summary>
        /// Inserts a record into the database. This method will ignore the ID property of the passed object and assign an available unique id
        /// </summary>
        /// <param name="record">HighScoresTable object to be added</param>
        /// <returns>true if the operation succeeds and false if it fails</returns>
        public override bool insertRecord(object record)
        {
            if(!(record is HighScoreData))
            {
                errorMessage = "Record is not HighScoreData type";
                return false;
            }
            bool success;
            string qry = "INSERT INTO high_scores(high_score, high_score_name, high_score_game) VALUES(@a, @b, @c)";
            
            if(this.openConnection())
            {
                HighScoreData d = (HighScoreData)record;
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                cmd.Parameters.AddWithValue("@a", d.score);
                cmd.Parameters.AddWithValue("@b", d.name);
                cmd.Parameters.AddWithValue("@c", d.gameID);
                int rowsAffected = cmd.ExecuteNonQuery();
                this.closeConnection();
                success = true;

                if (rowsAffected == 0)
                {
                    success = false;
                    errorMessage = "No rows were inserted";
                }

            }
            else
            {
                success = false;
                errorMessage = "Failed to open database connection";
            }

            return success;
        }
        /// <summary>
        /// reads the entire high score table
        /// </summary>
        /// <returns>List of HighScoreData objects. Each object in the list is a record from the table. This could be
        /// an empty list sorted in descending order by score</returns>
        public override List<object> readAll()
        {
            List<object> records = new List<object>();
            string qry = "SELECT * FROM high_scores ORDER BY high_score DESC";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();

                while(dataReader.Read())
                {
                    records.Add(new HighScoreData(Int32.Parse(dataReader["high_score_id"].ToString()), Int32.Parse(dataReader["high_score"].ToString()), dataReader["high_score_name"].ToString(), Int32.Parse(dataReader["high_score_game"].ToString())));
                }
                dataReader.Close();
                this.closeConnection();
            }
            else
            {
                errorMessage = "Failed to open database connection";
            }
            return records;
        }
        /// <summary>
        /// Attempts to select one record from the database
        /// </summary>
        /// <param name="primaryKey">high_score_id of the record to select</param>
        /// <returns>The selected record as a HighScoreData object</returns>
        public override object readById(int primaryKey)
        {
            HighScoreData result;
            string qry = "SELECT * FROM high_scores WHERE high_score_id=@h";
            if (this.openConnection())
            {
                MySqlCommand cmd = new MySqlCommand(qry, connection);
{'fraction_non_alphanumeric': 0.09502831969792322, 'fraction_numerical': 0.0017306482064191316, 'mean_word_length': 1.490303623898139, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '6943599', 'n_tokens_mistral': 3158, 'n_tokens_neox': 2982, 'n_words': 889}