<commit_before><?php
/**
 * This file is part of the TelegramBot package.
 *
 * (c) <NAME> aka LONGMAN <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Longman\TelegramBot;

use Longman\TelegramBot\DB;

/**
 * Class ConversationDB
 */
class ConversationDB extends DB
{
    /**
     * Initilize conversation table
     */
    public static function initializeConversation()
    {
        if (!defined('TB_CONVERSATION')) {
            define('TB_CONVERSATION', self::$table_prefix . 'conversation');
        }
    }

    /**
     * Select a conversation from the DB
     *
     * @param int  $user_id
     * @param int  $chat_id
     * @param bool $limit
     *
     * @return array|bool
     */
    public static function selectConversation($user_id, $chat_id, $limit = null)
    {
        if (!self::isDbConnected()) {
            return false;
        }

        try {
            $query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
            $query .= 'WHERE `status` = :status ';
            $query .= 'AND `chat_id` = :chat_id ';
            $query .= 'AND `user_id` = :user_id ';

            $tokens = [':chat_id' => $chat_id, ':user_id' => $user_id];
            if (!is_null($limit)) {
                $query .= ' LIMIT :limit';
            }
            $sth = self::$pdo->prepare($query);

            $active = 'active';
            $sth->bindParam(':status', $active, \PDO::PARAM_STR);
            $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
            $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
            $sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
            $sth->execute();

            $results = $sth->fetchAll(\PDO::FETCH_ASSOC);

        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $results;
    }

    /**
     * Insert the conversation in the database
     *
     * @param string $conversation_command
     * @param string $conversation_group_name
     * @param int    $user_id
     * @param int    $chat_id
     *
     * @return bool
     */
    public static function insertConversation($conversation_command, $conversation_group_name, $user_id, $chat_id)
    {
        if (!self::isDbConnected()) {
            return false;
        }

        try {
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
                (
                `status`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
                )
                VALUES (
                :status, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date
                )
               ');
            $active = 'active';
            //$data = json_encode('');
            $data = '""';
            $created_at = self::getTimestamp();

            $sth->bindParam(':status', $active);
            $sth->bindParam(':conversation_command', $conversation_command);
            $sth->bindParam(':conversation_name', $conversation_group_name);
            $sth->bindParam(':user_id', $user_id);
            $sth->bindParam(':chat_id', $chat_id);
            $sth->bindParam(':data', $data);
            $sth->bindParam(':date', $created_at);

            $status = $sth->execute();
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }

    /**
     * Update a specific conversation
     *
     * @param array $fields_values
     * @param array $where_fields_values
     *
     * @return bool
     */
    public static function updateConversation(array $fields_values, array $where_fields_values)
    {
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
    }

    /**
     * Update the conversation in the database
     *
     * @param string $table
     * @param array  $fields_values
     * @param array  $where_fields_values
     *
     * @todo This function is generic should be moved in DB.php
     *
     * @return bool
     */
    public static function update($table, array $fields_values, array $where_fields_values)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        //Auto update the field update_at
        $fields_values['updated_at'] = self::getTimestamp();

        //Values
        $update = '';
        $tokens = [];
        $tokens_counter = 0;
        $a = 0;
        foreach ($fields_values as $field => $value) {
            if ($a) {
                $update .= ', ';
            }
            ++$a;
            ++$tokens_counter;
            $update .= '`' . $field . '` = :' . $tokens_counter;
            $tokens[':' . $tokens_counter] = $value;
        }

        //Where
        $a = 0;
        $where  = '';
        foreach ($where_fields_values as $field => $value) {
            if ($a) {
                $where .= ' AND ';
            } else {
                ++$a;
                $where .= 'WHERE ';
            }
            ++$tokens_counter;
            $where .= '`' . $field .'`= :' . $tokens_counter ;
            $tokens[':' . $tokens_counter] = $value;
        }

        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
        try {
            $sth = self::$pdo->prepare($query);
            $status = $sth->execute($tokens);
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }
}
<commit_msg>Modify ConversationDB to use new database schema and names.<commit_after><?php
/**
 * This file is part of the TelegramBot package.
 *
 * (c) <NAME> aka LONGMAN <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Longman\TelegramBot;

use Longman\TelegramBot\DB;

/**
 * Class ConversationDB
 */
class ConversationDB extends DB
{
    /**
     * Initilize conversation table
     */
    public static function initializeConversation()
    {
        if (!defined('TB_CONVERSATION')) {
            define('TB_CONVERSATION', self::$table_prefix . 'conversation');
        }
    }

    /**
     * Select a conversation from the DB
     *
     * @param int  $user_id
     * @param int  $chat_id
     * @param bool $limit
     *
     * @return array|bool
     */
    public static function selectConversation($user_id, $chat_id, $limit = null)
    {
        if (!self::isDbConnected()) {
            return false;
        }

        try {
            $query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
            $query .= 'WHERE `status` = :status ';
            $query .= 'AND `chat_id` = :chat_id ';
            $query .= 'AND `user_id` = :user_id ';

            $tokens = [':chat_id' => $chat_id, ':user_id' => $user_id];
            if (!is_null($limit)) {
                $query .= ' LIMIT :limit';
            }
            $sth = self::$pdo->prepare($query);

            $active = 'active';
            $sth->bindParam(':status', $active, \PDO::PARAM_STR);
            $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
            $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
            $sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
            $sth->execute();

            $results = $sth->fetchAll(\PDO::FETCH_ASSOC);

        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $results;
    }

    /**
     * Insert the conversation in the database
     *
     * @param int    $user_id
     * @param int    $chat_id
     * @param string $command
     *
     * @return bool
     */
    public static function insertConversation($user_id, $chat_id, $command)
    {
        if (!self::isDbConnected()) {
            return false;
        }

        try {
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
                (
                `status`, `user_id`, `chat_id`, `command`, `data`, `created_at`, `updated_at`
                )
                VALUES (
                :status, :user_id, :chat_id, :command, :data, :date, :date
                )
               ');
            $active = 'active';
            //$data = json_encode('');
            $data = '""';
            $created_at = self::getTimestamp();

            $sth->bindParam(':status', $active);
            $sth->bindParam(':command', $command);
            $sth->bindParam(':user_id', $user_id);
            $sth->bindParam(':chat_id', $chat_id);
            $sth->bindParam(':data', $data);
            $sth->bindParam(':date', $created_at);

            $status = $sth->execute();
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }

    /**
     * Update a specific conversation
     *
     * @param array $fields_values
     * @param array $where_fields_values
     *
     * @return bool
     */
    public static function updateConversation(array $fields_values, array $where_fields_values)
    {
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
    }

    /**
     * Update the conversation in the database
     *
     * @param string $table
     * @param array  $fields_values
     * @param array  $where_fields_values
     *
     * @todo This function is generic should be moved in DB.php
     *
     * @return bool
     */
    public static function update($table, array $fields_values, array $where_fields_values)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        //Auto update the field update_at
        $fields_values['updated_at'] = self::getTimestamp();

        //Values
        $update = '';
        $tokens = [];
        $tokens_counter = 0;
        $a = 0;
        foreach ($fields_values as $field => $value) {
            if ($a) {
                $update .= ', ';
            }
            ++$a;
            ++$tokens_counter;
            $update .= '`' . $field . '` = :' . $tokens_counter;
            $tokens[':' . $tokens_counter] = $value;
        }

        //Where
        $a = 0;
        $where  = '';
        foreach ($where_fields_values as $field => $value) {
            if ($a) {
                $where .= ' AND ';
            } else {
                ++$a;
                $where .= 'WHERE ';
            }
            ++$tokens_counter;
            $where .= '`' . $field .'`= :' . $tokens_counter ;
            $tokens[':' . $tokens_counter] = $value;
        }

        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
        try {
            $sth = self::$pdo->prepare($query);
            $status = $sth->execute($tokens);
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }
}

{'fraction_non_alphanumeric': 0.14791172715666606, 'fraction_numerical': 0.0005471457231442641, 'mean_word_length': 1.7645576002016636, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 58, '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': '2605926', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3283, 'n_words': 712}