<commit_before>﻿using UnityEngine;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Linq;
public class MySQLDictionary : MonoBehaviour {

    public enum RequestType { Contains, Get, Add };
    public struct Request
    {
        public RequestType type;
        public string word;
        public WVObject<bool?> boolResult;
        public WVObject<WordAndEmoIdeal> wordEmoIdealResult;
        public EmotionModel.EmotionIdeal emotionIdeal;
    }
    private Queue<Request> _requestQueue;
    private Queue<Request> RequestQueue
    {
        get
        {
            if (_requestQueue == null) { _requestQueue = new Queue<Request>(); }
            return _requestQueue;
        }
    }

    private List<WVObject<WordAndEmoIdeal>> _wvObjectList_WordAndEmoIdeal; //this is a list of WVObjects, which stands for Word and Value Object
    private List<WVObject<WordAndEmoIdeal>> WVObjectList_WordAndEmoIdeal
    {
        get { if (_wvObjectList_WordAndEmoIdeal == null)
        { 
            _wvObjectList_WordAndEmoIdeal = new List<WVObject<WordAndEmoIdeal>>(); } return _wvObjectList_WordAndEmoIdeal;
        }
    }

    /// <summary>
    /// this is a list of contains object requests. Basically, when we want to see if a word is in the database,
    /// we create a new WVObject of type bool which--once it's finished contacting the db--will say whether it
    /// contians the word or not.
    /// </summary>
    private List<WVObject<bool?>> _wvObjectList_bool; //this is a list of WVObjects, which stands for Word and Value Object
    private List<WVObject<bool?>> WVObjectList_Bool
    {
        get { if (WVObjectList_Bool == null) { _wvObjectList_bool = new List<WVObject<bool?>>(); } return _wvObjectList_bool; }
        set { _wvObjectList_bool = value; }
    }

    public const int MAX_WORD_LENGTH = 30;
    private static MySQLDictionary _instance;
    private static MySQLDictionary Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("MySQL Dictionary").GetComponent<MySQLDictionary>();
                if (_instance == null)
                {
                    _instance = new GameObject().AddComponent<MySQLDictionary>();
                    _instance.name = "MySQL Dictionary";
                }
            }
            return _instance;
        }
    }

    private const string TABLE_NAME = "WordToEmotionIdeal";
    private const string TABLE_WORD = "Word";
    private const string TABLE_EMO_IDEAL = "EmotionIdealValue";

    private static string _source;
    private MySqlConnection _connection;
    public static MySqlConnection Connection
    {
        get
        {
            if (Instance._connection == null)
            {
                Connect();
            }
            return Instance._connection;
        }
    }


    // Use this for initialization
    void Start()
    {
        try
        {
            Connect();
        }
        catch (MySqlException e)
        {
            Debug.LogError("MySqlException " + e.StackTrace.ToString());
        }
    }

    #region Public Static Functions -- use these!

    public static void AddEntry(string word, EmotionModel.EmotionIdeal emotionIdeal)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<bool?> result = new WVObject<bool?>(word);

        Request newRequest = new Request();
        newRequest.type = RequestType.Add;
        newRequest.word = word;
        newRequest.emotionIdeal = emotionIdeal;
        newRequest.boolResult = result;

        Instance.RequestQueue.Enqueue(newRequest);
    }

    public static WVObject<bool?> DatabaseContainsEntry(string word)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<bool?> result = new WVObject<bool?>(word);
        Instance.StartCoroutine(Instance.DatabaseContains(word, result));

        Request newRequest = new Request();
        newRequest.type = RequestType.Contains;
        newRequest.word = word;
        newRequest.boolResult = result;

        Instance.RequestQueue.Enqueue(newRequest);

        return result;
    }

    public static WVObject<WordAndEmoIdeal> GetWordFromDatabase(string word)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<WordAndEmoIdeal> result = new WVObject<WordAndEmoIdeal>(word);
        Instance.StartCoroutine(Instance.GetWord(word));

        Request newRequest = new Request();
        newRequest.type = RequestType.Get;
        newRequest.word = word;
        newRequest.wordEmoIdealResult = result;

        Instance.RequestQueue.Enqueue(newRequest);

        return result;
    }

    #endregion

    static void Connect()
    {
        //         // Or option localhost, but still got error:
        // _source =       "Server=localhost;" +
        //               "Database = 5963_androidbase_1;" +    
        //           "User ID = root;" +
        //           "Pooling = false;" +
        //               "Password= ";

        _source = "Server=sql4.freemysqlhosting.net;" +
                     "Database=sql458495;" +
                     "User ID=sql458495;" +
                     "Password=<PASSWORD>%";

        try
        {
            if (Instance._connection != null && Instance._connection.State != System.Data.ConnectionState.Open)
            {
                Instance._connection.Close();
                Connection.Open();
            }
            else
            {
                Instance._connection = new MySqlConnection(_source);
                Instance._connection.Open();
            }

            //Now start the request loop
        }
        catch (MySqlException e)<commit_msg>* Now connections cannot occur simultaneously<commit_after>﻿using UnityEngine;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Linq;
public class MySQLDictionary : MonoBehaviour {

    public enum RequestType { Contains, Get, Add };
    public struct Request
    {
        public RequestType type;
        public string word;
        public WVObject<bool?> boolResult;
        public WVObject<WordAndEmoIdeal> wordEmoIdealResult;
        public EmotionModel.EmotionIdeal emotionIdeal;
    }
    private Queue<Request> _requestQueue;
    private Queue<Request> RequestQueue
    {
        get
        {
            if (_requestQueue == null) { _requestQueue = new Queue<Request>(); }
            return _requestQueue;
        }
    }

    private List<WVObject<WordAndEmoIdeal>> _wvObjectList_WordAndEmoIdeal; //this is a list of WVObjects, which stands for Word and Value Object
    private List<WVObject<WordAndEmoIdeal>> WVObjectList_WordAndEmoIdeal
    {
        get { if (_wvObjectList_WordAndEmoIdeal == null)
        { 
            _wvObjectList_WordAndEmoIdeal = new List<WVObject<WordAndEmoIdeal>>(); } return _wvObjectList_WordAndEmoIdeal;
        }
    }

    /// <summary>
    /// this is a list of contains object requests. Basically, when we want to see if a word is in the database,
    /// we create a new WVObject of type bool which--once it's finished contacting the db--will say whether it
    /// contians the word or not.
    /// </summary>
    private List<WVObject<bool?>> _wvObjectList_bool; //this is a list of WVObjects, which stands for Word and Value Object
    private List<WVObject<bool?>> WVObjectList_Bool
    {
        get { if (WVObjectList_Bool == null) { _wvObjectList_bool = new List<WVObject<bool?>>(); } return _wvObjectList_bool; }
        set { _wvObjectList_bool = value; }
    }

    public const int MAX_WORD_LENGTH = 30;
    private static MySQLDictionary _instance;
    private static MySQLDictionary Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("MySQL Dictionary").GetComponent<MySQLDictionary>();
                if (_instance == null)
                {
                    _instance = new GameObject().AddComponent<MySQLDictionary>();
                    _instance.name = "MySQL Dictionary";
                }
            }
            return _instance;
        }
    }

    private const string TABLE_NAME = "WordToEmotionIdeal";
    private const string TABLE_WORD = "Word";
    private const string TABLE_EMO_IDEAL = "EmotionIdealValue";

    private static string _source;
    private MySqlConnection _connection;
    public static MySqlConnection Connection
    {
        get
        {
            if (Instance._connection == null)
            {
                Connect();
            }
            return Instance._connection;
        }
    }


    // Use this for initialization
    void Start()
    {
        try
        {
            Connect();
        }
        catch (MySqlException e)
        {
            Debug.LogError("MySqlException " + e.StackTrace.ToString());
        }
    }

    #region Public Static Functions -- use these!

    public static void AddEntry(string word, EmotionModel.EmotionIdeal emotionIdeal)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<bool?> result = new WVObject<bool?>(word);

        Request newRequest = new Request();
        newRequest.type = RequestType.Add;
        newRequest.word = word;
        newRequest.emotionIdeal = emotionIdeal;
        newRequest.boolResult = result;

        Instance.RequestQueue.Enqueue(newRequest);
    }

    public static WVObject<bool?> DatabaseContainsEntry(string word)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<bool?> result = new WVObject<bool?>(word);

        Request newRequest = new Request();
        newRequest.type = RequestType.Contains;
        newRequest.word = word;
        newRequest.boolResult = result;

        Instance.RequestQueue.Enqueue(newRequest);

        return result;
    }

    public static WVObject<WordAndEmoIdeal> GetWordFromDatabase(string word)
    {
        //We create the WVObject, and pass it to the coroutine
        WVObject<WordAndEmoIdeal> result = new WVObject<WordAndEmoIdeal>(word);

        Request newRequest = new Request();
        newRequest.type = RequestType.Get;
        newRequest.word = word;
        newRequest.wordEmoIdealResult = result;

        Instance.RequestQueue.Enqueue(newRequest);

        return result;
    }

    #endregion

    static void Connect()
    {
        //         // Or option localhost, but still got error:
        // _source =       "Server=localhost;" +
        //               "Database = 5963_androidbase_1;" +    
        //           "User ID = root;" +
        //           "Pooling = false;" +
        //               "Password= ";

        _source = "Server=sql4.freemysqlhosting.net;" +
                     "Database=sql458495;" +
                     "User ID=sql458495;" +
                     "Password=<PASSWORD>%";

        try
        {
            if (Instance._connection != null && Instance._connection.State != System.Data.ConnectionState.Open)
            {
                Instance._connection.Close();
                Connection.Open();
            }
            else
            {
                Instance._connection = new MySqlConnection(_source);
                Instance._connection.Open();
            }

            //Now start the request loop
            Instance.StartCoroutine(Instance.ProcessRequestLoop());
        }
        catch (MySqlException e)
{'fraction_non_alphanumeric': 0.08760330578512397, 'fraction_numerical': 0.0034797738147020443, 'mean_word_length': 2.030047443331576, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, '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': '3015723', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3053, 'n_words': 781}