<commit_before><?php

namespace Genesis\SQLExtension\Context;

use Behat\Gherkin\Node\TableNode;
use Exception;

class SQLBuilder implements Interfaces\SQLBuilderInterface
{
    /**
     * External references, format of an external ref [...|...:...].
     */
    private $refs = [];

    /**
     * Constructs a clause based on the glue, to be used for where and update clause.
     *
     * @param string $commandType
     * @param string $glue
     * @param array $columns
     *
     * @return string
     */
    public function constructSQLClause($commandType, $glue, array $columns)
    {
        $whereClause = [];

        foreach ($columns as $column => $value) {
            $newValue = ltrim($value, '!');
            $quotedValue = $this->quoteOrNot($newValue);
            $comparator = $this->getComparatorFromValue(
                $value,
                $glue,
                $commandType
            );

            // Make up the sql.
            $clause = sprintf(
                '`%s` %s %s',
                $column,
                $comparator,
                $quotedValue
            );

            $whereClause[] = $clause;
        }

        return implode($glue, $whereClause);
    }

    /**
     * Gets the comparator based on the value provided.
     * This could be =, LIKE, != or something else based on the value.
     *
     * @param string $value The value that holds the comparator info.
     * @param string $glue The glue used for the clause construction.
     * @param string $commandType The command type being constructed.
     *
     * @return string
     */
    private function getComparatorFromValue($value, $glue, $commandType)
    {
        $comparator = '%s=';
        $notOperator = '';
        $newValue = ltrim($value, '!');

        // Check if the supplied value is null and that the construct is not for insert and update,
        // if so change the format.
        if (strtolower($newValue) == 'null' and
            trim($glue) != ',' and
            in_array($commandType, ['update', 'select', 'delete'])) {
            $comparator = 'is%s';
        }

        // Check if a not is applied to the value.
        if (strpos($value, '!') === 0) {
            if (strtolower($newValue) == 'null' and
            trim($glue) != ',' and
            in_array($commandType, ['update', 'select', 'delete'])) {
                $notOperator = ' not';
            } else {
                $notOperator = '!';
            }
        }

        // Check if the value is surrounded by wildcards. If so, we'll want to use a LIKE comparator.
        if (preg_match('/^%.+%$/', $value)) {
            $comparator = 'LIKE';
        }

        return sprintf($comparator, $notOperator);
    }

    /**
     * Converts the incoming string param from steps to array.
     *
     * @param string $columns
     * @param mixed $query
     *
     * @return array
     */
    public function convertToArray($query)
    {
        // Temporary placeholder to protect escaped commas.
        $commaEscapeCode = '%|-|';
        $columnValuePair = [];
        // as a rule, each array element after this should have the ":" separator.
        // Would it be better to use preg_match here?
        $query = str_replace('\,', $commaEscapeCode, $query);

        if ($this->isAndOperatorForColumns($query)) {
            $columns = explode(',', $query);
        } else {
            $columns = explode('||', $query);<commit_msg>PR feedback actioned<commit_after><?php

namespace Genesis\SQLExtension\Context;

use Behat\Gherkin\Node\TableNode;
use Exception;

class SQLBuilder implements Interfaces\SQLBuilderInterface
{
    /**
     * External references, format of an external ref [...|...:...].
     */
    private $refs = [];

    /**
     * Constructs a clause based on the glue, to be used for where and update clause.
     *
     * @param string $commandType
     * @param string $glue
     * @param array $columns
     *
     * @return string
     */
    public function constructSQLClause($commandType, $glue, array $columns)
    {
        $whereClause = [];

        foreach ($columns as $column => $value) {
            $newValue = ltrim($value, '!');
            $quotedValue = $this->quoteOrNot($newValue);
            $comparator = $this->getComparatorFromValue(
                $value,
                $glue,
                $commandType
            );

            // Make up the sql.
            $clause = sprintf(
                '`%s` %s %s',
                $column,
                $comparator,
                $quotedValue
            );

            $whereClause[] = $clause;
        }

        return implode($glue, $whereClause);
    }

    /**
     * Gets the comparator based on the value provided.
     * This could be =, LIKE, != or something else based on the value.
     *
     * @param string $value The value that holds the comparator info.
     * @param string $glue The glue used for the clause construction.
     * @param string $commandType The command type being constructed.
     *
     * @return string
     */
    private function getComparatorFromValue($value, $glue, $commandType)
    {
        $comparator = '%s=';
        $notOperator = '';
        $newValue = ltrim($value, '!');

        // Check if the supplied value is null and that the construct is not for insert and update,
        // if so change the format.
        if (strtolower($newValue) == 'null' and
            trim($glue) != ',' and
            in_array($commandType, ['update', 'select', 'delete'])) {
            $comparator = 'is%s';
        }

        // Check if a not is applied to the value.
        if (strpos($value, '!') === 0) {
            if (strtolower($newValue) == 'null' and
            trim($glue) != ',' and
            in_array($commandType, ['update', 'select', 'delete'])) {
                $notOperator = ' not';
            } else {
                $notOperator = '!';
            }
        }

        // Check if the value is surrounded by wildcards. If so, we'll want to use a LIKE comparator.
        if (preg_match('/^%.+%$/', $value)) {
            $comparator = 'LIKE';
        }

        return sprintf($comparator, $notOperator);
    }

    /**
     * Converts the incoming string param from steps to array.
     *
     * @param mixed $query
     *
     * @return array
     */
    public function convertToArray($query)
    {
        // Temporary placeholder to protect escaped commas.
        $commaEscapeCode = '%|-|';
        $columnValuePair = [];
        // as a rule, each array element after this should have the ":" separator.
        // Would it be better to use preg_match here?
        $query = str_replace('\,', $commaEscapeCode, $query);

        if ($this->isAndOperatorForColumns($query)) {
            $columns = explode(',', $query);
        } else {
            $columns = explode('||', $query);
{'fraction_non_alphanumeric': 0.12941176470588237, 'fraction_numerical': 0.00029048656499636893, 'mean_word_length': 1.8083197389885808, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 11, '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': '1751252', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1909, 'n_words': 576}