<commit_before><?php
namespace php_active_record;

class MysqliConnection
{
    private $server;
    private $user;
    private $password;
    private $database;
    private $encoding;
    private $port;
    private $socket;
    private $mysqli;
    private $master_server;
    private $master_user;
    private $master_password;
    private $master_database;
    private $master_encoding;
    private $master_port;
    private $master_socket;
    private $master_mysqli;
    private $transaction_in_progres;

    function __construct($server, $user, $password, $database, $encoding, $port, $socket, $master_server, $master_user, $master_password, $master_database, $master_encoding, $master_port, $master_socket)
    {
        $this->server = $server;
        $this->user = $user;
        $this->password = $password;
        $this->database = $database;
        $this->encoding = $encoding;
        $this->port = $port;
        $this->socket = $socket;
        $this->master_server = $master_server;
        $this->master_user = $master_user;
        $this->master_password = $<PASSWORD>;
        $this->master_database = $master_database;
        $this->master_encoding = $master_encoding;
        $this->master_port = $master_port;
        $this->master_socket = $master_socket;
        $this->transaction_in_progress = false;

        if(!$this->encoding) $this->encoding = "utf8";
        if(!$this->port) $this->port = NULL;
        if(!$this->socket) $this->socket = NULL;
        if(!$this->master_port) $this->master_port = NULL;
        if(!$this->master_socket) $this->master_socket = NULL;
        if(!$this->master_encoding) $this->master_encoding = $this->encoding;
    }

    function insert($query)
    {
        $this->check();
        $this->debug($query, true);

        $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL insert Error: ' . $this->master_mysqli->error, E_USER_WARNING);
            mysql_debug('MySQL insert Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $this->master_mysqli->insert_id;
    }

    function update($query)
    {
        $this->check();
        $this->debug($query, true);

        $result = $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL update Error: ' . $this->master_mysqli->error, E_USER_WARNING);
            mysql_debug('MySQL update Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $result;
    }

    function master($query)
    {
        return $this->update($query);
    }

    function delete($query)
    {
        $this->check();
        $this->debug($query, true);

        $result = $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL delete Error: ' . $this->master_mysqli->error, E_USER_WARNING);
            mysql_debug('MySQL delete Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $result;
    }

    function select($query)
    {
        $this->check();
        $this->debug($query, false);

        $result = $this->mysqli->query($query);
        if($this->mysqli->errno)
        {
            trigger_error('MySQL select Error: ' . $this->mysqli->error, E_USER_WARNING);
            mysql_debug('MySQL select Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->mysqli)) return NULL;
        return $result;
    }

    function select_value($query)
    {
        if($result = $this->select($query))
        {
            if($row = $result->fetch_row()) return $row[0];
        }
    }

    public static function is_read_query($query)
    {
        return !self::is_write_query($query);
    }

    public static function is_write_query($query)
    {
        if(preg_match("/^(insert|update|delete|truncate|create|drop|alter|load data)/i", trim($query), $arr))
        {
            return true;
        }
        return false;
    }

    function query($query)
    {
        if(preg_match("/^(insert|update|delete|truncate|create|drop|alter|load data)/i", trim($query), $arr))
        {
            switch(strtolower($arr[1]))
            {
                case "insert":
                    return $this->insert($query);
                    break;
                case "update":
                    return $this->update($query);
                    break;
                default:
                    return $this->delete($query);
                    break;
            }
        }

        return $this->select($query);
    }

    function multi_query($query)
    {
        if(!trim($query)) return;
        $this->check();
        $this->debug($query, true);

        if($this->master_mysqli->multi_query($query))
        {
            $results = array();
            do
            {
                if($result = $this->master_mysqli->store_result()) $results[] = $result;
                elseif($this->master_mysqli->errno)
                {
                    trigger_error('MySQL multi_query Error: ' . $this->master_mysqli->error, E_USER_WARNING);
                }

                if($result) $result->free();
            }while(@$this->master_mysqli->next_result());
        }else
        {
            trigger_error('MySQL multi_query Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
    }

    function load_data_infile($path, $table, $action = "IGNORE", $set = '', $udelay = 500000, $maximum_rows_in_file = 50000)
    {
        if($action != "REPLACE") $action = "IGNORE";
        // how many rows to split the larger file into
        if($action == 'REPLACE') $maximum_rows_in_file = 20000;
        $tmp_file_path = temp_filepath();

        $this->begin_transaction();
        //$this->insert("SET FOREIGN_KEY_CHECKS = 0");
        if(!($LOAD_DATA_TEMP = fopen($tmp_file_path, "w+")))
        {
          debug(__CLASS__ .":". __LINE__ .": Couldn't open file: " .$tmp_file_path);
          return;
        }
        //flock($LOAD_DATA_TEMP, LOCK_EX);

        $line_counter = 0;
        $batch = 0;
        if(!($FILE = fopen($path, "r")))
        {
          debug(__CLASS__ .":". __LINE__ .": Couldn't open file: " .$path);
          return;
        }
        while(!feof($FILE))
        {
            if($line = fgets($FILE, 4096))
            {<commit_msg>More info from MySQL errors.<commit_after><?php
namespace php_active_record;

class MysqliConnection
{
    private $server;
    private $user;
    private $password;
    private $database;
    private $encoding;
    private $port;
    private $socket;
    private $mysqli;
    private $master_server;
    private $master_user;
    private $master_password;
    private $master_database;
    private $master_encoding;
    private $master_port;
    private $master_socket;
    private $master_mysqli;
    private $transaction_in_progres;

    function __construct($server, $user, $password, $database, $encoding, $port, $socket, $master_server, $master_user, $master_password, $master_database, $master_encoding, $master_port, $master_socket)
    {
        $this->server = $server;
        $this->user = $user;
        $this->password = $password;
        $this->database = $database;
        $this->encoding = $encoding;
        $this->port = $port;
        $this->socket = $socket;
        $this->master_server = $master_server;
        $this->master_user = $master_user;
        $this->master_password = $master_password;
        $this->master_database = $master_database;
        $this->master_encoding = $master_encoding;
        $this->master_port = $master_port;
        $this->master_socket = $master_socket;
        $this->transaction_in_progress = false;

        if(!$this->encoding) $this->encoding = "utf8";
        if(!$this->port) $this->port = NULL;
        if(!$this->socket) $this->socket = NULL;
        if(!$this->master_port) $this->master_port = NULL;
        if(!$this->master_socket) $this->master_socket = NULL;
        if(!$this->master_encoding) $this->master_encoding = $this->encoding;
    }

    function insert($query)
    {
        $this->check();
        $this->debug($query, true);

        $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL insert Error: ' . $this->master_mysqli->error .
                " ($query)", E_USER_WARNING);
            mysql_debug('MySQL insert Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $this->master_mysqli->insert_id;
    }

    function update($query)
    {
        $this->check();
        $this->debug($query, true);

        $result = $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL update Error: ' . $this->master_mysqli->error .
                " ($query)", E_USER_WARNING);
            mysql_debug('MySQL update Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $result;
    }

    function master($query)
    {
        return $this->update($query);
    }

    function delete($query)
    {
        $this->check();
        $this->debug($query, true);

        $result = $this->master_mysqli->query($query);
        if($this->master_mysqli->errno)
        {
            trigger_error('MySQL delete Error: ' . $this->master_mysqli->error .
                " ($query)", E_USER_WARNING);
            mysql_debug('MySQL delete Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->master_mysqli)) return NULL;
        return $result;
    }

    function select($query)
    {
        $this->check();
        $this->debug($query, false);

        $result = $this->mysqli->query($query);
        if($this->mysqli->errno)
        {
            trigger_error('MySQL select Error: ' . $this->mysqli->error .
                " ($query)", E_USER_WARNING);
            mysql_debug('MySQL select Error: ' . $this->master_mysqli->error, E_USER_WARNING);
        }
        if($err = mysqli_errno($this->mysqli)) return NULL;
        return $result;
    }

    function select_value($query)
    {
        if($result = $this->select($query))
        {
            if($row = $result->fetch_row()) return $row[0];
        }
    }

    public static function is_read_query($query)
    {
        return !self::is_write_query($query);
    }

    public static function is_write_query($query)
    {
        if(preg_match("/^(insert|update|delete|truncate|create|drop|alter|load data)/i", trim($query), $arr))
        {
            return true;
        }
        return false;
    }

    function query($query)
    {
        if(preg_match("/^(insert|update|delete|truncate|create|drop|alter|load data)/i", trim($query), $arr))
        {
            switch(strtolower($arr[1]))
            {
                case "insert":
                    return $this->insert($query);
                    break;
                case "update":
                    return $this->update($query);
                    break;
                default:
                    return $this->delete($query);
                    break;
            }
        }

        return $this->select($query);
    }

    function multi_query($query)
    {
        if(!trim($query)) return;
        $this->check();
        $this->debug($query, true);

        if($this->master_mysqli->multi_query($query))
        {
            $results = array();
            do
            {
                if($result = $this->master_mysqli->store_result()) $results[] = $result;
                elseif($this->master_mysqli->errno)
                {
                    trigger_error('MySQL multi_query Error: ' .
                        $this->master_mysqli->error .
                        " ($query)", E_USER_WARNING);
                }

                if($result) $result->free();
            }while(@$this->master_mysqli->next_result());
        }else
        {
            trigger_error('MySQL multi_query Error: ' .
                $this->master_mysqli->error .
                " ($query)", E_USER_WARNING);
        }
    }

    function load_data_infile($path, $table, $action = "IGNORE", $set = '', $udelay = 500000, $maximum_rows_in_file = 50000)
    {
        if($action != "REPLACE") $action = "IGNORE";
        // how many rows to split the larger file into
        if($action == 'REPLACE') $maximum_rows_in_file = 20000;
        $tmp_file_path = temp_filepath();

        $this->begin_transaction();
        //$this->insert("SET FOREIGN_KEY_CHECKS = 0");
        if(!($LOAD_DATA_TEMP = fopen($tmp_file_path, "w+")))
        {
          debug(__CLASS__ .":". __LINE__ .": Couldn't open file: " .$tmp_file_path);
          return;
        }
        //flock($LOAD_DATA_TEMP, LOCK_EX);

        $line_counter = 0;
        $batch = 0;
        if(!($FILE = fopen($path, "r")))
        {
          debug(__CLASS__ .":". __LINE__ .": Couldn't open file: " .$path);
          return;
        }
        while(!feof($FILE))
        {
            if($line = fgets($FILE, 4096))
            {
{'fraction_non_alphanumeric': 0.14908256880733944, 'fraction_numerical': 0.0038472920982539215, 'mean_word_length': 2.0860730593607304, 'pattern_counts': {'":': 12, '<': 6, '<?xml version=': 0, '>': 200, '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': '3830273', 'n_tokens_mistral': 4222, 'n_tokens_neox': 4073, 'n_words': 696}