<commit_before><?php

namespace mii\db;


/**
 * Database connection/query wrapper/helper.
 *
 *
 * @copyright  (c) 2015 <NAME>
 * @copyright  (c) 2008-2012 Kohana Team
 */
class Database
{

    // Query types
    const SELECT = 1;
    const INSERT = 2;
    const UPDATE = 3;
    const DELETE = 4;

    /**
     * @var  string  the last query executed
     */
    public $last_query;

    /**
     * @var string Instance name
     */
    protected $_instance;

    /**
     * @var \mysqli Raw server connection
     */
    protected $_connection;

    /**
     * @var array configuration array
     */
    protected $_config;


    /**
     * Stores the database configuration locally and name the instance.
     *
     * [!!] This method cannot be accessed directly, you must use [Database::instance].
     *
     * @return  void
     */
    public function __construct(array $config)
    {
        // Set the instance name
        //$this->_instance = $name;

        // Store the config locally
        $this->_config = $config;

        if (empty($this->_config['table_prefix'])) {
            $this->_config['table_prefix'] = '';
        }
    }


    /**
     * Disconnect from the database when the object is destroyed.
     *
     *     // Destroy the database instance
     *     unset(Database::instances[(string) $db], $db);
     *
     * [!!] Calling `unset($db)` is not enough to destroy the database, as it
     * will still be stored in `Database::$instances`.
     *
     * @return  void
     */
    public function __destruct()
    {
        $this->disconnect();
    }

    /**
     * Disconnect from the database. This is called automatically by [Database::__destruct].
     * Clears the database instance from [Database::$instances].
     *
     *     $db->disconnect();
     *
     * @return  boolean
     */
    public function disconnect()
    {
        try {
            // Database is assumed disconnected
            $status = true;

            if (is_resource($this->_connection)) {
                if ($status = $this->_connection->close()) {
                    // Clear the connection
                    $this->_connection = NULL;
                }
            }
        } catch (\Exception $e) {
            // Database is probably not disconnected
            $status = !is_resource($this->_connection);
        }

        return $status;
    }

    /**
     * Returns the database instance name.
     *
     *     echo (string) $db;
     *
     * @return  string
     */
    public function __toString()
    {
        return $this->_instance;
    }

    /**
     * Perform an SQL query of the given type.
     *
     *     // Make a SELECT query and use objects for results
     *     $db->query(Database::SELECT, 'SELECT * FROM groups', TRUE);
     *
     *     // Make a SELECT query and use "Model_User" for the results
     *     $db->query(Database::SELECT, 'SELECT * FROM users LIMIT 1', 'Model_User');
     *
     * @param   integer $type Database::SELECT, Database::INSERT, etc
     * @param   string $sql SQL query
     * @param   mixed $as_object result object class string, TRUE for stdClass, FALSE for assoc array
     * @param   array $params object construct parameters for result class
     * @return  Result   Result for SELECT queries
     * @return  array    list (insert id, row count) for INSERT queries
     * @return  integer  number of affected rows for all other queries
     */
    public function query($type, $sql, $as_object = false, array $params = NULL)
    {
        // Make sure the database is connected
        $this->_connection or $this->connect();

        $benchmark = false;
        if (config('debug')) {
            // Benchmark this query
            $benchmark = \mii\util\Profiler::start("Database", $sql);
        }

        // Execute the query
        if (($result = $this->_connection->query($sql)) === false) {
            if ($benchmark) {
                // This benchmark is worthless
                \mii\util\Profiler::delete($benchmark);
            }

            throw new DatabaseException(':error [ :query ]', [
                ':error' => $this->_connection->error,
                ':query' => $sql
            ], $this->_connection->errno);
        }

        if ($benchmark) {
            \mii\util\Profiler::stop($benchmark);
        }

        // Set the last query
        $this->last_query = $sql;

        if ($type === Database::SELECT) {
            // Return an iterator of results
            return new Result($result, $sql, $as_object, $params);
        } elseif ($type === Database::INSERT) {
            // Return a list of insert id and rows created
            return [
                $this->_connection->insert_id,
                $this->_connection->affected_rows,
            ];
        } else {<commit_msg>multi queries<commit_after><?php

namespace mii\db;


/**
 * Database connection/query wrapper/helper.
 *
 *
 * @copyright  (c) 2015 <NAME>
 * @copyright  (c) 2008-2012 Kohana Team
 */
class Database
{

    // Query types
    const SELECT = 1;
    const INSERT = 2;
    const UPDATE = 3;
    const DELETE = 4;

    const MULTI = 5;

    /**
     * @var  string  the last query executed
     */
    public $last_query;

    /**
     * @var string Instance name
     */
    protected $_instance;

    /**
     * @var \mysqli Raw server connection
     */
    protected $_connection;

    /**
     * @var array configuration array
     */
    protected $_config;


    /**
     * Stores the database configuration locally and name the instance.
     *
     * [!!] This method cannot be accessed directly, you must use [Database::instance].
     *
     * @return  void
     */
    public function __construct(array $config)
    {
        // Set the instance name
        //$this->_instance = $name;

        // Store the config locally
        $this->_config = $config;

        if (empty($this->_config['table_prefix'])) {
            $this->_config['table_prefix'] = '';
        }
    }


    /**
     * Disconnect from the database when the object is destroyed.
     *
     *     // Destroy the database instance
     *     unset(Database::instances[(string) $db], $db);
     *
     * [!!] Calling `unset($db)` is not enough to destroy the database, as it
     * will still be stored in `Database::$instances`.
     *
     * @return  void
     */
    public function __destruct()
    {
        $this->disconnect();
    }

    /**
     * Disconnect from the database. This is called automatically by [Database::__destruct].
     * Clears the database instance from [Database::$instances].
     *
     *     $db->disconnect();
     *
     * @return  boolean
     */
    public function disconnect()
    {
        try {
            // Database is assumed disconnected
            $status = true;

            if (is_resource($this->_connection)) {
                if ($status = $this->_connection->close()) {
                    // Clear the connection
                    $this->_connection = NULL;
                }
            }
        } catch (\Exception $e) {
            // Database is probably not disconnected
            $status = !is_resource($this->_connection);
        }

        return $status;
    }

    /**
     * Returns the database instance name.
     *
     *     echo (string) $db;
     *
     * @return  string
     */
    public function __toString()
    {
        return $this->_instance;
    }

    /**
     * Perform an SQL query of the given type.
     *
     *     // Make a SELECT query and use objects for results
     *     $db->query(Database::SELECT, 'SELECT * FROM groups', TRUE);
     *
     *     // Make a SELECT query and use "Model_User" for the results
     *     $db->query(Database::SELECT, 'SELECT * FROM users LIMIT 1', 'Model_User');
     *
     * @param   integer $type Database::SELECT, Database::INSERT, etc
     * @param   string $sql SQL query
     * @param   mixed $as_object result object class string, TRUE for stdClass, FALSE for assoc array
     * @param   array $params object construct parameters for result class
     * @return  Result   Result for SELECT queries
     * @return  array    list (insert id, row count) for INSERT queries
     * @return  integer  number of affected rows for all other queries
     */
    public function query($type, $sql, $as_object = false, array $params = NULL)
    {
        // Make sure the database is connected
        $this->_connection or $this->connect();

        $benchmark = false;
        if (config('debug')) {
            // Benchmark this query
            $benchmark = \mii\util\Profiler::start("Database", $sql);
        }

        // Execute the query
        if($type === Database::MULTI) {
            $result = $this->_connection->multi_query($sql);
            while ($this->_connection->next_result()) {;}
            if($this->_connection->errno) {

            }
        } else {
            $result = $this->_connection->query($sql);
        }

        if ( $result === false || $this->_connection->errno ) {
            if ($benchmark) {
                // This benchmark is worthless
                \mii\util\Profiler::delete($benchmark);
            }

            throw new DatabaseException(':error [ :query ]', [
                ':error' => $this->_connection->error,
                ':query' => $sql
            ], $this->_connection->errno);
        }

        if ($benchmark) {
            \mii\util\Profiler::stop($benchmark);
        }

        // Set the last query
        $this->last_query = $sql;

        if ($type === Database::SELECT) {
            // Return an iterator of results
            return new Result($result, $sql, $as_object, $params);
        } elseif ($type === Database::INSERT) {
            // Return a list of insert id and rows created
            return [
                $this->_connection->insert_id,
                $this->_connection->affected_rows,
            ];
        } else {
{'fraction_non_alphanumeric': 0.12296727564746035, 'fraction_numerical': 0.0035133507327845814, 'mean_word_length': 1.7605985037406484, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, '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': '1160119', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2643, 'n_words': 800}