<commit_before><?php

namespace PHPixie\DB\PDOV;


use App\Pixie;

/**
 * PDO Database implementation.
 * @package Database
 * @method exec
 */
class Connection extends \PHPixie\DB\Connection {

    /**
     * @var Pixie
     */
    public $pixie;

    /**
     * Connection object
     * @var \PDO
     * @link http://php.net/manual/en/class.pdo.php
     */
    public $conn;

    /**
     * Type of the database, e.g. mysql, pgsql etc.
     * @var string
     */
    public $db_type;

    /**
     * @var array
     */
    protected $dispErrorStates;

    /**
     * Vulnerability settings for current controller.
     * @var string
     */
    private $_settings;

    /**
     * Initializes database connection
     *
     * @param $pixie
     * @param string $config Name of the connection to initialize
     * @return \PHPixie\DB\PDOV\Connection
     */
    public function __construct($pixie, $config) {
        parent::__construct($pixie, $config);

        $this->conn = new \PDO(
            $pixie->config->get("db.{$config}.connection"),
            $pixie->config->get("db.{$config}.user", ''),
            $pixie->config->get("db.{$config}.password", '')
        );
        $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->db_type = strtolower(str_replace('PDO_', '', $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME)));
        if ($this->db_type != 'sqlite') {
            $this->conn->exec("SET NAMES utf8");
        }
    }

    /**
     * Builds a new Query implementation
     *
     * @param string $type Query type. Available types: select,update,insert,delete,count
     * @param array $settings
     * @return Query Returns a PDO implementation of a Query.
     * @see Query_Database
     */
    public function query($type, $settings = array()) {
        /** @var Query $query */
        $query = $this->pixie->db->query_driver('PDOV', $this, $type);
        $query->settings($this->_settings);
        return $query;
    }

    public function settings($val = null) {
        if ($val != null) {
            $this->_settings = $val;
        }
        return $this->_settings;
    }

    /**
     * Gets the id of the last inserted row.
     *
     * @return mixed Row id
     */
    public function insert_id() {
        if ($this->db_type == 'pgsql') {
            return $this->execute('SELECT lastval() as id')->current()->id;
        }
        return $this->conn->lastInsertId();
    }

    /**
     * Gets column names for the specified table
     *
     * @param string $table Name of the table to get columns from
     * @return array Array of column names
     */
    public function list_columns($table) {
        $columns = array();
        if ($this->db_type == 'mysql') {
            $table_desc = $this->execute("DESCRIBE `$table`");
            foreach ($table_desc as $column) {
                $columns[] = $column->Field;
            }
        }
        if ($this->db_type == 'pgsql') {
            $table_desc = $this->execute("select column_name from information_schema.columns where table_name = '{$table}' and table_catalog=current_database();");
            foreach ($table_desc as $column) {
                $columns[] = $column->column_name;
            }
        }
        if ($this->db_type == 'sqlite') {
            $table_desc = $this->execute("PRAGMA table_info('$table')");
            foreach ($table_desc as $column) {
                $columns[] = $column->name;
            }
        }
        return $columns;
    }

    /**
     * Executes a prepared statement query
     *
     * @param string $query A prepared statement query
     * @param array $params Parameters for the query
     * @throws \Exception
     * @return Result   PDO implementation of a database result
     * @see Database_Result
     */
    public function execute($query, $params = array()) {
        //return $this->pixie->db->result_driver('PDOV', $this->conn->query($query));

        $this->startBlindness();

        $stmt = $this->conn->prepare($query);
        if (!$stmt->execute($params)) {
            $error = $stmt->errorInfo();
            throw new \Exception("Database error:\n" . $error[2] . " \n in query:\n{$query}");
        }
        $result = $this->pixie->db->result_driver('PDOV', $stmt);

        $this->stopBlindness();

        return $result;
    }

    public function startBlindness()
    {
        $vulns = $this->getVulns();

        if ($vulns['sql'] && $vulns['sql']['blind']) {
            $this->dispErrorStates[] = $this->pixie->debug->display_errors;
            $this->pixie->debug->display_errors = false;
        }
    }


    public function stopBlindness()<commit_msg>Throw exception on bad PDO statement<commit_after><?php

namespace PHPixie\DB\PDOV;


use App\Pixie;

/**
 * PDO Database implementation.
 * @package Database
 * @method exec
 */
class Connection extends \PHPixie\DB\Connection {

    /**
     * @var Pixie
     */
    public $pixie;

    /**
     * Connection object
     * @var \PDO
     * @link http://php.net/manual/en/class.pdo.php
     */
    public $conn;

    /**
     * Type of the database, e.g. mysql, pgsql etc.
     * @var string
     */
    public $db_type;

    /**
     * @var array
     */
    protected $dispErrorStates;

    /**
     * Vulnerability settings for current controller.
     * @var string
     */
    private $_settings;

    /**
     * Initializes database connection
     *
     * @param $pixie
     * @param string $config Name of the connection to initialize
     * @return \PHPixie\DB\PDOV\Connection
     */
    public function __construct($pixie, $config) {
        parent::__construct($pixie, $config);

        $this->conn = new \PDO(
            $pixie->config->get("db.{$config}.connection"),
            $pixie->config->get("db.{$config}.user", ''),
            $pixie->config->get("db.{$config}.password", '')
        );
        $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->db_type = strtolower(str_replace('PDO_', '', $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME)));
        if ($this->db_type != 'sqlite') {
            $this->conn->exec("SET NAMES utf8");
        }
    }

    /**
     * Builds a new Query implementation
     *
     * @param string $type Query type. Available types: select,update,insert,delete,count
     * @param array $settings
     * @return Query Returns a PDO implementation of a Query.
     * @see Query_Database
     */
    public function query($type, $settings = array()) {
        /** @var Query $query */
        $query = $this->pixie->db->query_driver('PDOV', $this, $type);
        $query->settings($this->_settings);
        return $query;
    }

    public function settings($val = null) {
        if ($val != null) {
            $this->_settings = $val;
        }
        return $this->_settings;
    }

    /**
     * Gets the id of the last inserted row.
     *
     * @return mixed Row id
     */
    public function insert_id() {
        if ($this->db_type == 'pgsql') {
            return $this->execute('SELECT lastval() as id')->current()->id;
        }
        return $this->conn->lastInsertId();
    }

    /**
     * Gets column names for the specified table
     *
     * @param string $table Name of the table to get columns from
     * @return array Array of column names
     */
    public function list_columns($table) {
        $columns = array();
        if ($this->db_type == 'mysql') {
            $table_desc = $this->execute("DESCRIBE `$table`");
            foreach ($table_desc as $column) {
                $columns[] = $column->Field;
            }
        }
        if ($this->db_type == 'pgsql') {
            $table_desc = $this->execute("select column_name from information_schema.columns where table_name = '{$table}' and table_catalog=current_database();");
            foreach ($table_desc as $column) {
                $columns[] = $column->column_name;
            }
        }
        if ($this->db_type == 'sqlite') {
            $table_desc = $this->execute("PRAGMA table_info('$table')");
            foreach ($table_desc as $column) {
                $columns[] = $column->name;
            }
        }
        return $columns;
    }

    /**
     * Executes a prepared statement query
     *
     * @param string $query A prepared statement query
     * @param array $params Parameters for the query
     * @throws \Exception
     * @return Result   PDO implementation of a database result
     * @see Database_Result
     */
    public function execute($query, $params = array()) {
        //return $this->pixie->db->result_driver('PDOV', $this->conn->query($query));

        $this->startBlindness();

        $stmt = $this->conn->prepare($query);
        try {
            if (!$stmt->execute($params)) {
                $error = $stmt->errorInfo();
                throw new \Exception("Database error:\n" . $error[2] . " \n in query:\n{$query}");
            }
            $result = $this->pixie->db->result_driver('PDOV', $stmt);
        } catch (\Exception $e) {
            throw new \Exception("Database error:\n" . $error[2] . " \n in query:\n{$query}", 0, $e);
        }

        $this->stopBlindness();

        return $result;
    }

    public function startBlindness()
    {
        $vulns = $this->getVulns();

        if ($vulns['sql'] && $vulns['sql']['blind']) {
            $this->dispErrorStates[] = $this->pixie->debug->display_errors;
            $this->pixie->debug->display_errors = false;
        }
    }


    public function stopBlindness()
{'fraction_non_alphanumeric': 0.15490749451238633, 'fraction_numerical': 0.0006271558482282847, 'mean_word_length': 2.0588235294117645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, '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': '6408535', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2809, 'n_words': 656}