<commit_before><?php

namespace skiptirengu\mssql;

use Exception;
use PDOException;
use yii\db\mssql\Schema as BaseSchema;
use yii\db\mssql\TableSchema;

/**
 * Optimized Schema class for MS SQL databases.
 *
 * @author <NAME> <<EMAIL>>
 * @see https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-help-transact-sql
 */
class Schema extends BaseSchema
{
    /**
     * Exception thrown when trying to read a resulset without any fields
     */
    const NO_FIELDS_EXCEPTION = 'The active result for the query contains no fields';
    /**
     * @var string
     */
    public $constraintLoaderClass = ConstraintLoader::class;
    /**
     * @var string
     */
    public $columnLoaderClass = ColumnLoader::class;
    /**
     * @var string
     */
    public $identityLoderClass = IdentityLoader::class;
    /**
     * @var ConstraintLoader
     */
    public $constraintLoader;
    /**
     * @var ColumnLoader
     */
    public $columnLoader;
    /**
     * @var IdentityLoader
     */
    public $identityLoader;

    /**
     * @inheritdoc
     */
    public function loadTableSchema($name)
    {
        $table = new TableSchema();

        $this->resolveTableNames($table, $name);
        $this->prepareTableSchema($table);

        $this->findPrimaryKeys($table);
        if ($this->findColumns($table)) {
            $this->findForeignKeys($table);
        } else {
            $table = null;
        }

        return $table;
    }

    /**
     * Query the database for metadata for the given table
     *
     * @param TableSchema $table the table metadata
     * @return void
     * @throws Exception
     */
    public function prepareTableSchema($table)
    {
        $this->createLoaders();
        $reader = $this->createDataReader($table);
        do {
            try {
                if ($this->extractData($reader->readAll())) {
                    break;
                }
            } catch (PDOException $ex) {
                // just skip this exception in case NOCOUNT is set to OFF
                if (strpos($ex->getMessage(), self::NO_FIELDS_EXCEPTION) === false) {
                    $reader->close();
                    throw $ex;
                }
            } catch (Exception $ex) {
                // close cursor before re-throwing the exception
                $reader->close();
                throw $ex;
            }
        } while ($reader->nextResult());
        $reader->close();
    }

    /**
     * @return void
     */
    public function createLoaders()
    {
        $this->constraintLoader = new $this->constraintLoaderClass();
        $this->columnLoader = new $this->columnLoaderClass();
        $this->identityLoader = new $this->identityLoderClass();
    }

    /**
     * @param $table
     * @return \yii\db\DataReader
     */
    protected function createDataReader($table)
    {
        return $this->db->createCommand('sp_help :tbl', [':tbl' => $table->fullName])->query();
    }

    /**
     * Extract the metadata information for then given data row.
     *
     * @param array $info
     * @return bool
     */
    public function extractData($info)
    {
        // peek the first value of the rs to determine the kind of information it has
        $first = reset($info);

        if (isset($first['constraint_type'])) {
            $this->constraintLoader->load($info);
        } elseif (isset($first['Column_name'])) {
            $this->columnLoader->load($info);
        } elseif (isset($first['Identity'])) {
            $this->identityLoader->load($info);
        } else {
            return false;
        }

        return $this->constraintLoader->isLoaded
            && $this->columnLoader->isLoaded
            && $this->identityLoader->isLoaded;
    }

    /**
     * Resets the current table info
     *
     * @return void
     */
    protected function resetTableInfo()
    {
        $this->constraintLoader = null;
        $this->columnLoader = null;
        $this->identityLoader = null;
    }
}<commit_msg>Finally, override base Schema methods :)<commit_after><?php

namespace skiptirengu\mssql;

use Exception;
use PDOException;
use yii\db\mssql\Schema as BaseSchema;
use yii\db\mssql\TableSchema;

/**
 * Optimized Schema class for MS SQL databases.
 *
 * @author <NAME> <<EMAIL>>
 * @see https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-help-transact-sql
 */
class Schema extends BaseSchema
{
    /**
     * Exception thrown when trying to read a resulset without any fields
     */
    const NO_FIELDS_EXCEPTION = 'The active result for the query contains no fields';

    /**
     * @var string
     */
    public $constraintLoaderClass = ConstraintLoader::class;
    /**
     * @var string
     */
    public $columnLoaderClass = ColumnLoader::class;
    /**
     * @var string
     */
    public $identityLoderClass = IdentityLoader::class;
    /**
     * @var ConstraintLoader
     */
    public $constraintLoader;
    /**
     * @var ColumnLoader
     */
    public $columnLoader;
    /**
     * @var IdentityLoader
     */
    public $identityLoader;

    /**
     * @inheritdoc
     */
    public function loadTableSchema($name)
    {
        $table = new TableSchema();

        $this->resolveTableNames($table, $name);
        $this->prepareTableSchema($table);

        $this->findPrimaryKeys($table);
        if ($this->findColumns($table)) {
            $this->findForeignKeys($table);
        } else {
            $table = null;
        }

        return $table;
    }

    /**
     * Query the database for metadata for the given table
     *
     * @param TableSchema $table the table metadata
     * @return void
     * @throws Exception
     */
    public function prepareTableSchema($table)
    {
        $this->createLoaders();
        $reader = $this->createDataReader($table);
        do {
            try {
                if ($this->extractData($reader->readAll())) {
                    break;
                }
            } catch (PDOException $ex) {
                // just skip this exception in case NOCOUNT is set to OFF
                if (strpos($ex->getMessage(), self::NO_FIELDS_EXCEPTION) === false) {
                    $reader->close();
                    throw $ex;
                }
            } catch (Exception $ex) {
                // close cursor before re-throwing the exception
                $reader->close();
                throw $ex;
            }
        } while ($reader->nextResult());
        $reader->close();
    }

    /**
     * @return void
     */
    public function createLoaders()
    {
        $this->constraintLoader = new $this->constraintLoaderClass();
        $this->columnLoader = new $this->columnLoaderClass();
        $this->identityLoader = new $this->identityLoderClass();
    }

    /**
     * @param $table
     * @return \yii\db\DataReader
     */
    protected function createDataReader($table)
    {
        return $this->db->createCommand('sp_help :tbl', [':tbl' => $table->fullName])->query();
    }

    /**
     * Extract the metadata information for then given data row.
     *
     * @param array $info
     * @return bool
     */
    public function extractData($info)
    {
        // peek the first value of the rs to determine the kind of information it has
        $first = reset($info);

        if (isset($first['constraint_type'])) {
            $this->constraintLoader->load($info);
        } elseif (isset($first['Column_name'])) {
            $this->columnLoader->load($info);
        } elseif (isset($first['Identity'])) {
            $this->identityLoader->load($info);
        } else {
            return false;
        }

        return $this->constraintLoader->isLoaded
            && $this->columnLoader->isLoaded
            && $this->identityLoader->isLoaded;
    }

    /**
     * @inheritdoc
     */
    protected function findPrimaryKeys($table)
    {
        $table->primaryKey = $this->constraintLoader->tablePks;
    }

    /**
     * @inheritdoc
     */
    protected function findColumns($table)
    {
        if (empty($this->columnLoader->tableColumns)) {
            return false;
        }
        $this->columnLoader->setIdentityColumn($this->identityLoader);
        $this->columnLoader->setDefaultValuesForColumns($this->constraintLoader);
        $table->columns = $this->loadColumnSchema($this->columnLoader->tableColumns);
        return true;
    }

    /**
     * @inheritdoc
     */
    protected function findForeignKeys($table)
    {
        $table->foreignKeys = $this->constraintLoader->foreignKeys;
    }

    /**
     * Resets the current table info
     *
     * @return void
     */
    protected function resetTableInfo()
    {
        $this->constraintLoader = null;
        $this->columnLoader = null;
        $this->identityLoader = null;
    }
}
{'fraction_non_alphanumeric': 0.13162354536210596, 'fraction_numerical': 0.0, 'mean_word_length': 1.962516733601071, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 107, 'https://': 2, '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': '3340795', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2413, 'n_words': 541}