<commit_before><?php

namespace Qandidate;

use PDO;

class SqlStorage implements GameStorage
{
    private $db;

    public function __construct($database_name, $database_username, $database_password)
    {
        $this->db = new PDO('mysql:host=localhost;dbname='.$database_name, $database_username, $database_password);
    }

    public function save(Api $game)
    {
        $stmt = $this->db->prepare("INSERT INTO game (uuid, game) VALUES (:uuid, :game)");
        $uuid = (string) $game;
        $serialized = serialize($game);
        $stmt->bindParam(':uuid', $uuid);
        $stmt->bindParam(':game', $serialized);
        $stmt->execute();
    }

    public function find($uuid)
    {
        $stmt = $this->db->prepare('SELECT * FROM game WHERE game.uuid = :uuid');
        $stmt->bindParam(':uuid', $uuid);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return unserialize($result['game']);
    }

    public static function wipeAndBoot($database_name, $database_user, $database_password)
    {
        $db = new PDO('mysql:host=localhost;dbname='.$database_name, $database_user, $database_password);
        $db->exec('DROP TABLE IF EXISTS game');
        $db->exec('CREATE TABLE game (uuid VARCHAR(255), game TEXT)');
    }

    public function findAll()
    {
        $stmt = $this->db->prepare('SELECT * FROM game');
        $stmt->execute();
        $gameRows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $callback = function ($item) {
            return unserialize($item['game']);
        };

        return array_map($callback, $gameRows);
    }
}<commit_msg>setting uuid as primary key<commit_after><?php

namespace Qandidate;

use PDO;

class SqlStorage implements GameStorage
{
    private $db;

    public function __construct($database_name, $database_username, $database_password)
    {
        $this->db = new PDO('mysql:host=localhost;dbname='.$database_name, $database_username, $database_password);
    }

    public function save(Api $game)
    {
        if ($this->find((string) $game)) {
            $stmt = $this->db->prepare("UPDATE game SET game.game =  :game WHERE game.uuid = :uuid");
        } else {
            try {
                $stmt = $this->db->prepare("INSERT INTO game (uuid, game) VALUES (:uuid, :game)");
            } catch (\Exception $exception) {
                // when resubmitting a form too fast
            }
        }
        $uuid = (string) $game;
        $serialized = serialize($game);
        $stmt->bindParam(':uuid', $uuid);
        $stmt->bindParam(':game', $serialized);
        $stmt->execute();
    }

    public function find($uuid)
    {
        $stmt = $this->db->prepare('SELECT * FROM game WHERE game.uuid = :uuid');
        $stmt->bindParam(':uuid', $uuid);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return unserialize($result['game']);
    }

    public static function wipeAndBoot($database_name, $database_user, $database_password)
    {
        $db = new PDO('mysql:host=localhost;dbname='.$database_name, $database_user, $database_password);
        $db->exec('DROP TABLE IF EXISTS game');
        $db->exec('CREATE TABLE game (uuid VARCHAR(255), game TEXT, PRIMARY KEY (uuid))');
    }

    public function findAll()
    {
        $stmt = $this->db->prepare('SELECT * FROM game');
        $stmt->execute();
        $gameRows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $callback = function ($item) {
            return unserialize($item['game']);
        };

        return array_map($callback, $gameRows);
    }
}
{'fraction_non_alphanumeric': 0.1587168758716876, 'fraction_numerical': 0.0016736401673640166, 'mean_word_length': 2.758909853249476, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '306782', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1056, 'n_words': 234}