<commit_before><?php
namespace Spot\Query;
use Spot\Mapper;

/**
 * Main query resolver
 *
 * @package Spot
 */
class Resolver
{
    protected $mapper;

    /**
     *  Constructor Method
     */
    public function __construct(Mapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * Migrate table structure changes to database
     */
    public function migrate()
    {
        // Mapper knows currently set entity
        $entity       = $this->mapper->entity();
        $table        = $entity::table();
        $fields       = $this->mapper->entityManager()->fields();
        $fieldIndexes = $this->mapper->entityManager()->fieldKeys();
        $connection   = $this->mapper->connection();

        $schemaManager = $this->mapper->connection()->getSchemaManager();
        $tableObject = $schemaManager->listTableDetails($table);
        $tableObjects[] = $tableObject;
        $schema = new \Doctrine\DBAL\Schema\Schema($tableObjects);

        $tableColumns = $tableObject->getColumns();
        $tableExists = !empty($tableColumns);
        if ($tableExists) {
            // Update existing table
            $existingTable = $schema->getTable($table);
            $newSchema = $this->migrateCreateSchema();
            $queries = $schema->getMigrateToSql($newSchema, $connection->getDatabasePlatform());
        } else {
            // Create new table
            $newSchema = $this->migrateCreateSchema();
            $queries = $newSchema->toSql($connection->getDatabasePlatform());
        }

        // Execute resulting queries
        $lastResult = false;
        foreach ($queries as $sql) {
            $lastResult = $connection->exec($sql);
        }

        return $lastResult;
    }

    /**
     * Migrate create schema
     */
    public function migrateCreateSchema()
    {
        $entityName   = $this->mapper->entity();
        $table        = $entityName::table();
        $fields       = $this->mapper->entityManager()->fields();
        $fieldIndexes = $this->mapper->entityManager()->fieldKeys();

        $schema = new \Doctrine\DBAL\Schema\Schema();
        $table  = $schema->createTable($table);

        foreach ($fields as $field => $fieldInfo) {
            $fieldType = $fieldInfo['type'];
            unset($fieldInfo['type']);
            $table->addColumn($field, $fieldType, $fieldInfo);
        }

        // PRIMARY
        if ($fieldIndexes['primary']) {
            $table->setPrimaryKey($fieldIndexes['primary']);
        }
        // UNIQUE
        foreach ($fieldIndexes['unique'] as $keyName => $keyFields) {
            $table->addUniqueIndex($keyFields, $keyName);
        }
        // INDEX
        foreach ($fieldIndexes['index'] as $keyName => $keyFields) {
            $table->addIndex($keyFields, $keyName);
        }

        return $schema;
    }

    /**
     * Find records with custom SQL query
     *
     * @param  string          $sql SQL query to execute
     * @throws \Spot\Exception
     */
    public function read(\Spot\Query $query)
    {
        $stmt = $query->builder()->execute();

        // Set PDO fetch mode
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);

        $collection = $query->mapper()->collection($stmt, $query->with());

        // Ensure statement is closed
        $stmt->closeCursor();

        return $collection;
    }

    /**
     * Create new row object with set properties
     *
     * @param string $table Table name
     * @param array  $data  Array of data to save in 'field' => 'value' format
     */
    public function create($table, array $data)
    {
        $connection = $this->mapper->connection();
        $result = $connection->insert($table, $data);

        return $result;
    }

    /**
     * Update
     *
     * @param string $table Table name
     * @param array  $data  Array of data to save in 'field' => 'value' format
     * @param array  $data  Array of data for WHERE clause in 'field' => 'value' format
     */
    public function update($table, array $data, array $where)
    {
        $connection = $this->mapper->connection();

        return $connection->update($table, $data, $where);
    }

    /**
     * Execute provided query and return result
     *
     * @param  string          $sql SQL query to execute
     * @throws \Spot\Exception
     */
    public function exec(\Spot\Query $query)
    {
        return $query->builder()->execute();
    }

    /**
     * Truncate Table
     *
     * @param string $table Table name
     * @param array  $data  Array of data for WHERE clause in 'field' => 'value' format
     */
    public function truncate($table, $cascade = false)<commit_msg>Applied PSR2 formatting to Query/Resolver<commit_after><?php
namespace Spot\Query;

use Spot\Mapper;

/**
 * Main query resolver
 *
 * @package Spot
 */
class Resolver
{
    protected $mapper;

    /**
     *  Constructor Method
     */
    public function __construct(Mapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * Migrate table structure changes to database
     */
    public function migrate()
    {
        // Mapper knows currently set entity
        $entity = $this->mapper->entity();
        $table = $entity::table();
        $fields = $this->mapper->entityManager()->fields();
        $fieldIndexes = $this->mapper->entityManager()->fieldKeys();
        $connection = $this->mapper->connection();

        $schemaManager = $this->mapper->connection()->getSchemaManager();
        $tableObject = $schemaManager->listTableDetails($table);
        $tableObjects[] = $tableObject;
        $schema = new \Doctrine\DBAL\Schema\Schema($tableObjects);

        $tableColumns = $tableObject->getColumns();
        $tableExists = !empty($tableColumns);
        if ($tableExists) {
            // Update existing table
            $existingTable = $schema->getTable($table);
            $newSchema = $this->migrateCreateSchema();
            $queries = $schema->getMigrateToSql($newSchema, $connection->getDatabasePlatform());
        } else {
            // Create new table
            $newSchema = $this->migrateCreateSchema();
            $queries = $newSchema->toSql($connection->getDatabasePlatform());
        }

        // Execute resulting queries
        $lastResult = false;
        foreach ($queries as $sql) {
            $lastResult = $connection->exec($sql);
        }

        return $lastResult;
    }

    /**
     * Migrate create schema
     */
    public function migrateCreateSchema()
    {
        $entityName = $this->mapper->entity();
        $table = $entityName::table();
        $fields = $this->mapper->entityManager()->fields();
        $fieldIndexes = $this->mapper->entityManager()->fieldKeys();

        $schema = new \Doctrine\DBAL\Schema\Schema();
        $table = $schema->createTable($table);

        foreach ($fields as $field => $fieldInfo) {
            $fieldType = $fieldInfo['type'];
            unset($fieldInfo['type']);
            $table->addColumn($field, $fieldType, $fieldInfo);
        }

        // PRIMARY
        if ($fieldIndexes['primary']) {
            $table->setPrimaryKey($fieldIndexes['primary']);
        }
        // UNIQUE
        foreach ($fieldIndexes['unique'] as $keyName => $keyFields) {
            $table->addUniqueIndex($keyFields, $keyName);
        }
        // INDEX
        foreach ($fieldIndexes['index'] as $keyName => $keyFields) {
            $table->addIndex($keyFields, $keyName);
        }

        return $schema;
    }

    /**
     * Find records with custom SQL query
     *
     * @param  string $sql SQL query to execute
     * @throws \Spot\Exception
     */
    public function read(\Spot\Query $query)
    {
        $stmt = $query->builder()->execute();

        // Set PDO fetch mode
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);

        $collection = $query->mapper()->collection($stmt, $query->with());

        // Ensure statement is closed
        $stmt->closeCursor();

        return $collection;
    }

    /**
     * Create new row object with set properties
     *
     * @param string $table Table name
     * @param array $data Array of data to save in 'field' => 'value' format
     */
    public function create($table, array $data)
    {
        $connection = $this->mapper->connection();
        $result = $connection->insert($table, $data);

        return $result;
    }

    /**
     * Update
     *
     * @param string $table Table name
     * @param array $data Array of data to save in 'field' => 'value' format
     * @param array $data Array of data for WHERE clause in 'field' => 'value' format
     */
    public function update($table, array $data, array $where)
    {
        $connection = $this->mapper->connection();

        return $connection->update($table, $data, $where);
    }

    /**
     * Execute provided query and return result
     *
     * @param  string $sql SQL query to execute
     * @throws \Spot\Exception
     */
    public function exec(\Spot\Query $query)
    {
        return $query->builder()->execute();
    }

    /**
     * Truncate Table
     *
     * @param string $table Table name
     * @param array $data Array of data for WHERE clause in 'field' => 'value' format
     */
    public function truncate($table, $cascade = false)
{'fraction_non_alphanumeric': 0.14336569579288025, 'fraction_numerical': 0.00010787486515641856, 'mean_word_length': 2.159850034083163, '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': '6689769', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2534, 'n_words': 615}