<commit_before><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */
namespace Ouzo\Db;

use InvalidArgumentException;
use Ouzo\Config;
use Ouzo\Db;
use Ouzo\Db\Dialect\DialectFactory;
use Ouzo\Db\WhereClause\WhereClause;
use Ouzo\Restriction\Restriction;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Objects;
use PDO;

class QueryExecutor
{
    /**
     * @var Db
     */
    private $_db;
    private $_adapter;
    private $_query;
    private $_boundValues = array();

    public $_sql;
    public $_fetchStyle = PDO::FETCH_ASSOC;

    public function __construct($db, $query)
    {
        $this->_db = $db;
        $this->_query = $query;

        $this->_adapter = DialectFactory::create();
    }

    /**
     * @param $db
     * @param $query
     * @throws InvalidArgumentException
     * @return QueryExecutor
     */
    public static function prepare($db, $query)
    {
        if (empty($db) || !$db instanceof Db) {
            throw new InvalidArgumentException("Database handler not provided or is of wrong type");
        }
        if (!$query) {
            throw new InvalidArgumentException("Query object not provided");
        }
        if (!$query->table) {
            throw new InvalidArgumentException("Table name cannot be empty");
        }

        if (self::isEmptyResult($query->whereClauses)) {
            return new EmptyQueryExecutor();
        }
        return new QueryExecutor($db, $query);
    }

    public function fetch()
    {
        $this->_buildQuery();
        return $this->_fetch('fetch');
    }

    public function fetchAll()
    {
        $this->_buildQuery();
        return $this->_fetch('fetchAll');
    }

    public function fetchIterator()
    {
        $this->_buildQuery();
        $statement = StatementExecutor::prepare($this->_db->_dbHandle, $this->_sql, $this->_boundValues, $this->_query->options);
        return $statement->fetchIterator();

    }

    public function execute()
    {
        $this->_buildQuery();
        return $this->_db->execute($this->_sql, $this->_boundValues);
    }

    public function insert($sequence = '')
    {
        $this->execute();
        return $sequence ? $this->_db->lastInsertId($sequence) : null;
    }

    private function _fetch($function)
    {
        $statement = StatementExecutor::prepare($this->_db->_dbHandle, $this->_sql, $this->_boundValues, $this->_query->options);
        return $statement->executeAndFetch($function, $this->_fetchStyle);
    }

    public function getSql()
    {
        return $this->_sql;
    }

    public function getBoundValues()
    {
        return $this->_boundValues;
    }

    public function lastErrorMessage()
    {
        return $this->_db->lastErrorMessage();
    }

    public function _buildQuery()
    {
        $this->_fetchStyle = $this->_query->selectType;
        $queryBindValuesExtractor = new QueryBoundValuesExtractor($this->_query);
        $this->_boundValues = $queryBindValuesExtractor->extract();
        $this->_sql = $this->_adapter->buildQuery($this->_query);
    }

    private static function isEmptyResult($whereClauses)
    {
        return Arrays::any($whereClauses, function (WhereClause $whereClause) {
            return $whereClause->isNeverSatisfied();
        });
    }
}
<commit_msg>PDO returns last inserted ID as string, but we need it to be integer<commit_after><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */
namespace Ouzo\Db;

use InvalidArgumentException;
use Ouzo\Db;
use Ouzo\Db\Dialect\DialectFactory;
use Ouzo\Db\WhereClause\WhereClause;
use Ouzo\Utilities\Arrays;
use PDO;

class QueryExecutor
{
    /**
     * @var Db
     */
    private $_db;
    private $_adapter;
    private $_query;
    private $_boundValues = array();

    public $_sql;
    public $_fetchStyle = PDO::FETCH_ASSOC;

    public function __construct($db, $query)
    {
        $this->_db = $db;
        $this->_query = $query;

        $this->_adapter = DialectFactory::create();
    }

    /**
     * @param $db
     * @param $query
     * @throws InvalidArgumentException
     * @return QueryExecutor
     */
    public static function prepare($db, $query)
    {
        if (empty($db) || !$db instanceof Db) {
            throw new InvalidArgumentException("Database handler not provided or is of wrong type");
        }
        if (!$query) {
            throw new InvalidArgumentException("Query object not provided");
        }
        if (!$query->table) {
            throw new InvalidArgumentException("Table name cannot be empty");
        }

        if (self::isEmptyResult($query->whereClauses)) {
            return new EmptyQueryExecutor();
        }
        return new QueryExecutor($db, $query);
    }

    public function fetch()
    {
        $this->_buildQuery();
        return $this->_fetch('fetch');
    }

    public function fetchAll()
    {
        $this->_buildQuery();
        return $this->_fetch('fetchAll');
    }

    public function fetchIterator()
    {
        $this->_buildQuery();
        $statement = StatementExecutor::prepare($this->_db->_dbHandle, $this->_sql, $this->_boundValues, $this->_query->options);
        return $statement->fetchIterator();

    }

    public function execute()
    {
        $this->_buildQuery();
        return $this->_db->execute($this->_sql, $this->_boundValues);
    }

    public function insert($sequence = '')
    {
        $this->execute();
        return $sequence ? (int)$this->_db->lastInsertId($sequence) : null;
    }

    private function _fetch($function)
    {
        $statement = StatementExecutor::prepare($this->_db->_dbHandle, $this->_sql, $this->_boundValues, $this->_query->options);
        return $statement->executeAndFetch($function, $this->_fetchStyle);
    }

    public function getSql()
    {
        return $this->_sql;
    }

    public function getBoundValues()
    {
        return $this->_boundValues;
    }

    public function lastErrorMessage()
    {
        return $this->_db->lastErrorMessage();
    }

    public function _buildQuery()
    {
        $this->_fetchStyle = $this->_query->selectType;
        $queryBindValuesExtractor = new QueryBoundValuesExtractor($this->_query);
        $this->_boundValues = $queryBindValuesExtractor->extract();
        $this->_sql = $this->_adapter->buildQuery($this->_query);
    }

    private static function isEmptyResult($whereClauses)
    {
        return Arrays::any($whereClauses, function (WhereClause $whereClause) {
            return $whereClause->isNeverSatisfied();
        });
    }
}

{'fraction_non_alphanumeric': 0.132889874353289, 'fraction_numerical': 0.0, 'mean_word_length': 2.635679742074154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '954347', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1876, 'n_words': 433}