<commit_before><?php

/**
 * CodePax
 *
 * LICENSE
 *
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE
 * It is also available through the world-wide-web at this URL:
 * http://www.codepax.com/license.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 * */

/**
 * Class that handles "z_db_versions" table
 * for the Sql Server engine
 *
 * @category CodePax
 * @subpackage Models
 * @copyright Copyright (c) 2012 Zitec COM srl, Romania
 * @license New BSD http://www.codepax.com/license.html
 * */
class CodePax_DbVersions_SqlSrv extends CodePax_Sql implements CodePax_DbVersions_Interface {

    /**
     * Return the current DB version which can be
     * either a change script or a data change script
     *
     * The array keys are: version, date_added
     *
     * @param int $_script_type can take values of 0,1,2
     * @return array
     * */
    public function getLatestVersion($_script_type)
    {

        $query = 'SELECT TOP 1
			(CAST(major AS varchar) + \'.\' + CAST(minor AS varchar) + \'.\' + CAST(point AS varchar)) as version,
			date_added
			FROM ' . CodePax_DbVersions::TABLE_NAME . '
			WHERE script_type = ?
		 	ORDER BY date_added DESC';

        //set the script type param
        $stmt = sqlsrv_prepare($this->db, $query, array(&$_script_type));
        sqlsrv_execute($stmt);
        $version_array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

        if (!empty($version_array)) {
            $version_array['date_added'] = $version_array['date_added']->format('Y-m-d H:i:s');
        }

        if ($_script_type == DBVersions::TYPE_CHANGE_SCRIPT) {
            $_script_type = CodePax_DbVersions::TYPE_BASELINE;
            sqlsrv_execute($stmt);
            $baseline_version_array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

            if (!empty($baseline_version_array)) {
                $baseline_version_array['date_added'] = $baseline_version_array['date_added']->format('Y-m-d H:i:s');
            }
            $version_array = CodePax_DbVersions::getLatestDbVersion($version_array, $baseline_version_array);
        }

        //when nothing is found, a default is returned
        return empty($version_array) ? CodePax_DbVersions::getDefaultVersion() : $version_array;
    }

    /**
     * Adds a new version to database. The version can be
     * a change script, a baseline or a data change script<commit_msg>fixed a bug for db versioning with SQL Server<commit_after><?php

/**
 * CodePax
 *
 * LICENSE
 *
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE
 * It is also available through the world-wide-web at this URL:
 * http://www.codepax.com/license.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 * */

/**
 * Class that handles "z_db_versions" table
 * for the Sql Server engine
 *
 * @category CodePax
 * @subpackage Models
 * @copyright Copyright (c) 2012 Zitec COM srl, Romania
 * @license New BSD http://www.codepax.com/license.html
 * */
class CodePax_DbVersions_SqlSrv extends CodePax_Sql implements CodePax_DbVersions_Interface {

    /**
     * Return the current DB version which can be
     * either a change script or a data change script
     *
     * The array keys are: version, date_added
     *
     * @param int $_script_type can take values of 0,1,2
     * @return array
     * */
    public function getLatestVersion($_script_type)
    {

        $query = 'SELECT TOP 1
			(CAST(major AS varchar) + \'.\' + CAST(minor AS varchar) + \'.\' + CAST(point AS varchar)) as version,
			date_added
			FROM ' . CodePax_DbVersions::TABLE_NAME . '
			WHERE script_type = ?
		 	ORDER BY date_added DESC';

        //set the script type param
        $stmt = sqlsrv_prepare($this->db, $query, array(&$_script_type));
        sqlsrv_execute($stmt);
        $version_array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

        if (!empty($version_array)) {
            $version_array['date_added'] = $version_array['date_added']->format('Y-m-d H:i:s');
        }

        if ($_script_type == CodePax_DbVersions::TYPE_CHANGE_SCRIPT) {
            $_script_type = CodePax_DbVersions::TYPE_BASELINE;
            sqlsrv_execute($stmt);
            $baseline_version_array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

            if (!empty($baseline_version_array)) {
                $baseline_version_array['date_added'] = $baseline_version_array['date_added']->format('Y-m-d H:i:s');
            }
            $version_array = CodePax_DbVersions::getLatestDbVersion($version_array, $baseline_version_array);
        }

        //when nothing is found, a default is returned
        return empty($version_array) ? CodePax_DbVersions::getDefaultVersion() : $version_array;
    }

    /**
     * Adds a new version to database. The version can be
     * a change script, a baseline or a data change script
{'fraction_non_alphanumeric': 0.10532407407407407, 'fraction_numerical': 0.0030864197530864196, 'mean_word_length': 3.0100541376643464, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2130595', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1576, 'n_words': 459}