<commit_before><?php
/**
 * Infernum
 * Copyright (C) 2011 IceFlame.net
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * @package  FlameCore\Infernum
 * @version  0.1-dev
 * @link     http://www.flamecore.org
 * @license  ISC License <http://opensource.org/licenses/ISC>
 */

namespace FlameCore\Infernum\Database\MySQL;

use FlameCore\Infernum\Database\Base\Connection as BaseConnection;

/**
 * This class allows you to execute operations in a MySQL database
 *
 * @author   <NAME> <<EMAIL>>
 */
class Connection extends BaseConnection
{
    /**
     * {@inheritdoc}
     */
    public function connect()
    {
        $this->link = @mysqli_connect($this->host, $this->user, $this->password, $this->database);

        if (mysqli_connect_errno())
            trigger_error('Failed connecting to the database: '.mysqli_connect_error(), E_USER_ERROR);
    }

    /**
     * {@inheritdoc}
     */
    public function disconnect()
    {
        mysqli_close($this->link);
    }

    /**
     * {@inheritdoc}
     */
    public function query($query, $vars = null)
    {
        $query = $this->prepareQuery($query, $vars);

        $result = @mysqli_query($this->link, $query);
        if ($result) {
            $this->queryCount++;

            if ($result instanceof \MySQLi_Result)
                return new Result($result);

            return true;
        }

        trigger_error('Database query failed: '.$this->getError(), E_USER_ERROR);
    }

    /**
     * {@inheritdoc}
     */
    public function select($table, $columns = '*', $params = array())
    {
        $sql = 'SELECT '.$columns.' FROM `'.$table.'`';

        if (isset($params['where']))
            $sql .= ' WHERE '.$params['where'];
        if (isset($params['limit']))
            $sql .= ' LIMIT '.$params['limit'];
        if (isset($params['group']))
            $sql .= ' GROUP BY '.$params['group'];
        if (isset($params['order']))
            $sql .= ' ORDER BY '.$params['order'];

        return $this->query($sql, isset($params['vars']) ? $params['vars'] : null);
    }

    /**
     * {@inheritdoc}
     */
    public function insert($table, $data)
    {
        foreach ($data as $column => $value) {
            $columns[] = '`'.$column.'`';
            $values[]  = $this->prepareValue($value);
        }

        $sql = 'INSERT INTO `'.$table.'` ('.implode(', ', $columns).') VALUES('.implode(', ', $values).')';
        return $this->query($sql);
    }

    /**
     * {@inheritdoc}
     */
    public function update($table, $data, $params = array())
    {
        foreach ($data as $key => $value) {
            $value = $this->prepareValue($value);
            $dataset[] = '`'.$key.'` = '.$value;
        }

        $sql = 'UPDATE `'.$table.'` SET '.implode(', ', $dataset);

        if (isset($params['where']))
            $sql .= ' WHERE '.$params['where'];
        if (isset($params['limit']))
            $sql .= ' LIMIT '.$params['limit'];

        return $this->query($sql, isset($params['vars']) ? $params['vars'] : null);
    }

    /**
     * {@inheritdoc}
     */
    public function importDump($file, $vars = null)
    {
        $dumpContent = file_get_contents($file);
        $queries = preg_split('/;\s*$/', $dumpContent);

        $this->beginTransaction();

        foreach ($queries as $query) {
            $query = trim($query);
            if ($query == '' || substr($query, 0, 2) == '--')<commit_msg>Improved and fixed Database/MySQL/Connection class<commit_after><?php
/**
 * Infernum
 * Copyright (C) 2011 IceFlame.net
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * @package  FlameCore\Infernum
 * @version  0.1-dev
 * @link     http://www.flamecore.org
 * @license  ISC License <http://opensource.org/licenses/ISC>
 */

namespace FlameCore\Infernum\Database\MySQL;

use FlameCore\Infernum\Database\Base\Connection as BaseConnection;

/**
 * This class allows you to execute operations in a MySQL database
 *
 * @author   <NAME> <<EMAIL>>
 */
class Connection extends BaseConnection
{
    /**
     * {@inheritdoc}
     */
    public function connect()
    {
        $this->link = @mysqli_connect($this->host, $this->user, $this->password, $this->database);

        if (mysqli_connect_errno())
            trigger_error('Failed connecting to the database: '.mysqli_connect_error(), E_USER_ERROR);
    }

    /**
     * {@inheritdoc}
     */
    public function disconnect()
    {
        mysqli_close($this->link);
    }

    /**
     * {@inheritdoc}
     */
    public function query($query, $vars = null)
    {
        $query = $this->prepareQuery($query, $vars);

        $result = @mysqli_query($this->link, $query);
        if ($result) {
            $this->queryCount++;

            if ($result instanceof \MySQLi_Result)
                return new Result($result);

            return true;
        }

        trigger_error('Database query failed: '.$this->getError(), E_USER_ERROR);
    }

    /**
     * {@inheritdoc}
     */
    public function select($table, $columns = '*', $params = array())
    {
        if (is_array($columns))
            $columns = '`' . join('`, `', $columns) . '`';

        $sql = 'SELECT '.$columns.' FROM `'.$table.'`';

        if (isset($params['where']))
            $sql .= ' WHERE '.$params['where'];
        if (isset($params['limit']))
            $sql .= ' LIMIT '.$params['limit'];
        if (isset($params['group']))
            $sql .= ' GROUP BY '.$params['group'];
        if (isset($params['order']))
            $sql .= ' ORDER BY '.$params['order'];

        return $this->query($sql, isset($params['vars']) ? $params['vars'] : null);
    }

    /**
     * {@inheritdoc}
     */
    public function insert($table, $data)
    {
        $columns = array();
        $values = array();

        foreach ($data as $column => $value) {
            $columns[] = '`'.$column.'`';
            $values[]  = $this->prepareValue($value);
        }

        $sql = 'INSERT INTO `'.$table.'` ('.implode(', ', $columns).') VALUES('.implode(', ', $values).')';
        return $this->query($sql);
    }

    /**
     * {@inheritdoc}
     */
    public function update($table, $data, $params = array())
    {
        $dataset = array();

        foreach ($data as $key => $value) {
            $value = $this->prepareValue($value);
            $dataset[] = '`'.$key.'` = '.$value;
        }

        $sql = 'UPDATE `'.$table.'` SET '.implode(', ', $dataset);

        if (isset($params['where']))
            $sql .= ' WHERE '.$params['where'];
        if (isset($params['limit']))
            $sql .= ' LIMIT '.$params['limit'];

        return $this->query($sql, isset($params['vars']) ? $params['vars'] : null);
    }

    /**
     * {@inheritdoc}
     */
    public function importDump($file, $vars = null)
    {
        $dumpContent = file_get_contents($file);
        $queries = preg_split('/;\s*$/', $dumpContent);

        $this->beginTransaction();

        foreach ($queries as $query) {
            $query = trim($query);
            if ($query == '' || substr($query, 0, 2) == '--')
{'fraction_non_alphanumeric': 0.16899739274709646, 'fraction_numerical': 0.0018961839298411946, 'mean_word_length': 2.4028225806451613, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2964312', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2434, 'n_words': 669}