<commit_before>/*
 * Copyright (c) 2013 <NAME> (<EMAIL>)
 *
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
 * or copy at http://opensource.org/licenses/MIT)
 */

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>

#include <pkmn/enums.hpp>
#include <pkmn/paths.hpp>
#include <pkmn/database/queries.hpp>

#include "../SQLiteCpp/src/SQLiteC++.h"

namespace pkmn
{
    namespace database
    {
        static SQLite::Database db(get_database_path().c_str());

        //Transform identifier strings into proper format for database
        std::string to_database_format(std::string identifier)
        {
            //Some manual changes
            if(identifier == "Farfetch'd") identifier = "farfetchd";
            else if(identifier == "Mr. Mime") identifier = "mr-mime";
            else if(identifier == "Mime Jr.") identifier = "mime-jr";
            else if(identifier == "Exp. Share") identifier = "exp-share";

            std::transform(identifier.begin(), identifier.end(), identifier.begin(), ::tolower);
            std::replace(identifier.begin(), identifier.end(), ' ', '-');

            return identifier;
        }

        unsigned int get_ability_id(std::string ability_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT ability_id FROM ability_names WHERE name='" << ability_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_ability_name(unsigned int ability_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM ability_names WHERE local_language_id=9 AND ability_id=" << ability_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_damage_class(unsigned int type_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT damage_class_id FROM types WHERE id=" << type_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_egg_group_id(std::string egg_group_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT egg_group_id FROM egg_group_prose WHERE name='" << egg_group_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_egg_group_name(unsigned int egg_group_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM egg_group_prose WHERE local_language_id=9 AND egg_group_id=" << egg_group_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_game_id(std::string game_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT version_id FROM version_names WHERE name='" << game_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            if(query.executeStep()) return int(query.getColumn(0));
            else return 0;
        }

        std::string get_game_name(unsigned int game_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM version_names WHERE local_language_id=9 AND version_id=" << game_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_generation(unsigned int game_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT generation_id FROM version_groups WHERE id="
                         << "    (SELECT version_group_id FROM versions WHERE id=" << game_id << ")";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_index(unsigned int item_id, unsigned int version)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT game_index FROM item_game_indices WHERE item_id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_index(std::string item_name, unsigned int version)
        {
            return get_item_index(get_item_id(item_name), version);
        }

        std::string get_item_description(unsigned int item_id, unsigned int version)
        {
            //Main Gen 1 games have no item descriptions, so convert version ID to Stadium 2
            //Convert enums to version_group_id
            if(version < Games::GOLD) version = Games::STADIUM2;
            unsigned int version_group_id = get_version_group(version);

            std::ostringstream query_stream;
            query_stream << "SELECT flavor_text FROM item_flavor_text WHERE language_id=9 AND "
                         << "item_id=" << item_id << " AND version_group_id=" << version_group_id;
            SQLite::Statement query(db, query_stream.str().c_str());

            if(query.executeStep())
            {
                std::string entry = (const char*)query.getColumn(0);

                std::string s;
                std::istringstream iss(entry);
                entry = "";
                while(iss >> s)
                {
                    if (entry != "") entry += " " + s;
                    else entry = s;
                }
                return entry;
            }
            else return "None";
        }

        std::string get_item_description(std::string item_name, unsigned int version)
        {
            return get_item_description(get_item_id(item_name), version);
        }

        unsigned int get_item_id(std::string item_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT item_id FROM item_names WHERE name='" << item_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_id(unsigned int item_index, unsigned int game)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT item_id FROM item_game_indices WHERE generation_id="
                         << get_generation(game) << " AND game_index=" << item_index;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_item_name(unsigned int item_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM item_names WHERE local_language_id-9 AND item_id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_item_category(unsigned int item_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT category_id FROM items WHERE id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;<commit_msg>queries: fixed get_item_name to use English name<commit_after>/*
 * Copyright (c) 2013 <NAME> (<EMAIL>)
 *
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
 * or copy at http://opensource.org/licenses/MIT)
 */

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>

#include <pkmn/enums.hpp>
#include <pkmn/paths.hpp>
#include <pkmn/database/queries.hpp>

#include "../SQLiteCpp/src/SQLiteC++.h"

namespace pkmn
{
    namespace database
    {
        static SQLite::Database db(get_database_path().c_str());

        //Transform identifier strings into proper format for database
        std::string to_database_format(std::string identifier)
        {
            //Some manual changes
            if(identifier == "Farfetch'd") identifier = "farfetchd";
            else if(identifier == "Mr. Mime") identifier = "mr-mime";
            else if(identifier == "Mime Jr.") identifier = "mime-jr";
            else if(identifier == "Exp. Share") identifier = "exp-share";

            std::transform(identifier.begin(), identifier.end(), identifier.begin(), ::tolower);
            std::replace(identifier.begin(), identifier.end(), ' ', '-');

            return identifier;
        }

        unsigned int get_ability_id(std::string ability_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT ability_id FROM ability_names WHERE name='" << ability_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_ability_name(unsigned int ability_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM ability_names WHERE local_language_id=9 AND ability_id=" << ability_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_damage_class(unsigned int type_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT damage_class_id FROM types WHERE id=" << type_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_egg_group_id(std::string egg_group_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT egg_group_id FROM egg_group_prose WHERE name='" << egg_group_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_egg_group_name(unsigned int egg_group_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM egg_group_prose WHERE local_language_id=9 AND egg_group_id=" << egg_group_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_game_id(std::string game_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT version_id FROM version_names WHERE name='" << game_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            if(query.executeStep()) return int(query.getColumn(0));
            else return 0;
        }

        std::string get_game_name(unsigned int game_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM version_names WHERE local_language_id=9 AND version_id=" << game_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_generation(unsigned int game_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT generation_id FROM version_groups WHERE id="
                         << "    (SELECT version_group_id FROM versions WHERE id=" << game_id << ")";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_index(unsigned int item_id, unsigned int version)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT game_index FROM item_game_indices WHERE item_id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_index(std::string item_name, unsigned int version)
        {
            return get_item_index(get_item_id(item_name), version);
        }

        std::string get_item_description(unsigned int item_id, unsigned int version)
        {
            //Main Gen 1 games have no item descriptions, so convert version ID to Stadium 2
            //Convert enums to version_group_id
            if(version < Games::GOLD) version = Games::STADIUM2;
            unsigned int version_group_id = get_version_group(version);

            std::ostringstream query_stream;
            query_stream << "SELECT flavor_text FROM item_flavor_text WHERE language_id=9 AND "
                         << "item_id=" << item_id << " AND version_group_id=" << version_group_id;
            SQLite::Statement query(db, query_stream.str().c_str());

            if(query.executeStep())
            {
                std::string entry = (const char*)query.getColumn(0);

                std::string s;
                std::istringstream iss(entry);
                entry = "";
                while(iss >> s)
                {
                    if (entry != "") entry += " " + s;
                    else entry = s;
                }
                return entry;
            }
            else return "None";
        }

        std::string get_item_description(std::string item_name, unsigned int version)
        {
            return get_item_description(get_item_id(item_name), version);
        }

        unsigned int get_item_id(std::string item_name)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT item_id FROM item_names WHERE name='" << item_name << "'";
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        unsigned int get_item_id(unsigned int item_index, unsigned int game)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT item_id FROM item_game_indices WHERE generation_id="
                         << get_generation(game) << " AND game_index=" << item_index;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
        }

        std::string get_item_name(unsigned int item_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT name FROM item_names WHERE local_language_id=9 AND item_id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? ((const char*)(query.getColumn(0))) : "None";
        }

        unsigned int get_item_category(unsigned int item_id)
        {
            std::ostringstream query_stream;
            query_stream << "SELECT category_id FROM items WHERE id=" << item_id;
            SQLite::Statement query(db, query_stream.str().c_str());
            
            return (query.executeStep()) ? int(query.getColumn(0)) : 0;
{'fraction_non_alphanumeric': 0.11721224920802534, 'fraction_numerical': 0.004348096155040686, 'mean_word_length': 2.2212885154061626, 'pattern_counts': {'":': 0, '<': 183, '<?xml version=': 0, '>': 29, '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': '3915834', 'n_tokens_mistral': 4501, 'n_tokens_neox': 4381, 'n_words': 991}