<commit_before><?php
namespace Gaia\DB\Driver;

class MySQLi extends \MySQLi implements \Gaia\DB\Iface {
    
    protected $lock = FALSE;
    protected $txn = FALSE;
    
    public function execute( $query /*, ... */ ){
        $args = func_get_args();
        array_shift($args);
        return $this->query( $this->format_query_args( $query, $args ) );
    }
    
    public function query( $query, $mode = MYSQLI_STORE_RESULT ){
        if( $this->lock ) return FALSE;
        $res = parent::query( $query, $mode );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function multi_query( $query ){
        if( $this->lock ) return FALSE;
        $res = parent::multi_query( $query );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function real_query( $query ){
        if( $this->lock ) return FALSE;
        $res = parent::real_query( $query );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function close(){
        Connection::remove( $this );
        if( $this->lock ) return FALSE;
        $rs = parent::close();
        $this->lock = TRUE;
        return $rs;
    }
    
    public function prepare($query){
        trigger_error('unsupported method ' . __CLASS__ . '::' . __FUNCTION__, E_USER_ERROR);
        exit;
    }
    
    
    public function begin( $txn = FALSE ){
        if( $this->lock ) return FALSE;
        $this->txn = $txn;
        return $this->query('BEGIN WORK');
    }
    
    public function rollback(){
        if( ! $this->txn ) return parent::rollback(); 
        if( $this->lock ) return TRUE;
        $rs = parent::rollback();
        parent::close();
        $this->lock = TRUE;
        return $rs;
    }
    
    public function commit(){
        if( ! $this->txn ) return parent::commit(); 
        if( $this->lock ) return FALSE;
        $res = parent::commit();
        if( ! $res ) return $res;
        $this->txn = FALSE;
        return $res;
    }
    
    public function format_query( $query /*, ... */ ){
        $args = func_get_args();
        array_shift($args);
        return $this->format_query_args( $query, $args );
    }

    public function format_query_args($query, array $args) {
        if( ! $args || count( $args ) < 1 ) return $query;
        $conn = $this;
        return \Gaia\DB\Query::format( <commit_msg>fixing call to connection class in the mysqli wrapper<commit_after><?php
namespace Gaia\DB\Driver;
use Gaia\DB\Connection;

class MySQLi extends \MySQLi implements \Gaia\DB\Iface {
    
    protected $lock = FALSE;
    protected $txn = FALSE;
    
    public function execute( $query /*, ... */ ){
        $args = func_get_args();
        array_shift($args);
        return $this->query( $this->format_query_args( $query, $args ) );
    }
    
    public function query( $query, $mode = MYSQLI_STORE_RESULT ){
        if( $this->lock ) return FALSE;
        $res = parent::query( $query, $mode );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function multi_query( $query ){
        if( $this->lock ) return FALSE;
        $res = parent::multi_query( $query );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function real_query( $query ){
        if( $this->lock ) return FALSE;
        $res = parent::real_query( $query );
        if( $res ) return $res;
        if( $this->txn ) {
            if( is_callable( $this->txn ) ) call_user_func( $this->txn, $this );
            $this->lock = TRUE;
        }
        return $res;
    }
    
    public function close(){
        Connection::remove( $this );
        if( $this->lock ) return FALSE;
        $rs = parent::close();
        $this->lock = TRUE;
        return $rs;
    }
    
    public function prepare($query){
        trigger_error('unsupported method ' . __CLASS__ . '::' . __FUNCTION__, E_USER_ERROR);
        exit;
    }
    
    
    public function begin( $txn = FALSE ){
        if( $this->lock ) return FALSE;
        $this->txn = $txn;
        return $this->query('BEGIN WORK');
    }
    
    public function rollback(){
        if( ! $this->txn ) return parent::rollback(); 
        if( $this->lock ) return TRUE;
        $rs = parent::rollback();
        $this->close();
        $this->lock = TRUE;
        return $rs;
    }
    
    public function commit(){
        if( ! $this->txn ) return parent::commit(); 
        if( $this->lock ) return FALSE;
        $res = parent::commit();
        if( ! $res ) return $res;
        $this->txn = FALSE;
        return $res;
    }
    
    public function format_query( $query /*, ... */ ){
        $args = func_get_args();
        array_shift($args);
        return $this->format_query_args( $query, $args );
    }

    public function format_query_args($query, array $args) {
        if( ! $args || count( $args ) < 1 ) return $query;
        $conn = $this;
        return \Gaia\DB\Query::format( 
{'fraction_non_alphanumeric': 0.1534941031508537, 'fraction_numerical': 0.0003520506953001232, 'mean_word_length': 1.875506072874494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 62, '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': '4901766', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1797, 'n_words': 440}