<commit_before><?php
/**
 * Created by PhpStorm.
 * User: bogdans
 * Date: 7/15/17
 * Time: 4:00 PM
 */

namespace ClickhouseClient\Client;


use ClickhouseClient\Client\Format\FormatInterface;
use ClickhouseClient\Client\Format\JsonFormat;
use ClickhouseClient\Connector\Connector;
use ClickhouseClient\Connector\Request;

class Client
{
    /** @var  Config */
    private $config;

    /** @var  Connector */
    private $connector;

    /** @var  FormatInterface */
    private $format;

    /**
     * Client constructor.
     * @param Config $config
     * @param string $defaultFormatClass
     */
    public function __construct(Config $config, string $defaultFormatClass = JsonFormat::class)
    {
        $this->config = $config;

        $connectorConfig = new \ClickhouseClient\Connector\Config();
        $connectorConfig->setHost($this->config->getBasics()['host']);
        $connectorConfig->setPort($this->config->getBasics()['port']);
        $connectorConfig->setProtocol($this->config->getBasics()['protocol']);
        $connectorConfig->setUser($this->config->getCredentials()['user']);
        $connectorConfig->setPassword($this->config->getCredentials()['password']);
        if ($this->config->getCurlOptions()) {
            $connectorConfig->setDefaultCurlOptions($this->config->getCurlOptions());
        }
        $this->connector = new Connector($connectorConfig);

        $this->format = null;
        if ($defaultFormatClass) {
            $this->isValidFormatClass($defaultFormatClass);
            $this->format = new $defaultFormatClass();
        }
    }

    /**
     * @param string $formatClass
     * @throws \Exception
     */
    private function isValidFormatClass(string $formatClass)
    {
        if (!in_array(FormatInterface::class, class_implements($formatClass))) {
            throw new \Exception('Default format class received (' . $formatClass . ') does not implement ' . FormatInterface::class);
        }
    }

    /**
     * @param string|null $formatClass
     * @return FormatInterface
     */
    private function defineFormat(string $formatClass = null)
    {
        if ($formatClass) {
            $this->isValidFormatClass($formatClass);
            return new $formatClass;
        }
        return new $this->format;
    }

    /**
     * @return \ClickhouseClient\Connector\Response
     */
    public function ping()
    {
        $response = $this->connector->performRequest(
            $this->connector->createResource()
        );
        return $response;
    }

    /**
     * @param string $sql
     * @param string|null $formatClass
     * @return \ClickhouseClient\Connector\Response
     */
    public function query(string $sql, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $response = $this->connector->performRequest(
            $this->connector->createResource(
                array_merge(
                    $this->config->getSettings(),
                    ['query' => $sql]
                )
            )
        );

        $response->setFormat($format);

        return $response;
    }

    public function queryStream(string $sql, $stream, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $ch = $this->connector->createResource(
            array_merge(
                $this->config->getSettings(),
                ['query' => $sql]
            )
        );

        //give curl the file pointer so that it can write to it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_FILE, $stream);

        $response = $this->connector->performRequest($ch);

        return $response;

    }

    public function queryClosure(string $sql, \Closure $closure, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $ch = $this->connector->createResource(
            array_merge(
                $this->config->getSettings(),
                ['query' => $sql]
            )
        );

        $buffer = '';

        //give curl the file pointer so that it can write to it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $str) use ($closure, &$buffer) {

            // explode lines into array
            $lines = explode("\n", $buffer . $str);

            // get last element
            $lastLine = end($lines);

            // check if is normat string
            if (substr($lastLine, -1) !== "\n") {
                // remove from lines array
                array_pop($lines);
                // add to buffer
                $buffer = $lastLine;<commit_msg>misspell<commit_after><?php
/**
 * Created by PhpStorm.
 * User: bogdans
 * Date: 7/15/17
 * Time: 4:00 PM
 */

namespace ClickhouseClient\Client;


use ClickhouseClient\Client\Format\FormatInterface;
use ClickhouseClient\Client\Format\JsonFormat;
use ClickhouseClient\Connector\Connector;
use ClickhouseClient\Connector\Request;

class Client
{
    /** @var  Config */
    private $config;

    /** @var  Connector */
    private $connector;

    /** @var  FormatInterface */
    private $format;

    /**
     * Client constructor.
     * @param Config $config
     * @param string $defaultFormatClass
     */
    public function __construct(Config $config, string $defaultFormatClass = JsonFormat::class)
    {
        $this->config = $config;

        $connectorConfig = new \ClickhouseClient\Connector\Config();
        $connectorConfig->setHost($this->config->getBasics()['host']);
        $connectorConfig->setPort($this->config->getBasics()['port']);
        $connectorConfig->setProtocol($this->config->getBasics()['protocol']);
        $connectorConfig->setUser($this->config->getCredentials()['user']);
        $connectorConfig->setPassword($this->config->getCredentials()['password']);
        if ($this->config->getCurlOptions()) {
            $connectorConfig->setDefaultCurlOptions($this->config->getCurlOptions());
        }
        $this->connector = new Connector($connectorConfig);

        $this->format = null;
        if ($defaultFormatClass) {
            $this->isValidFormatClass($defaultFormatClass);
            $this->format = new $defaultFormatClass();
        }
    }

    /**
     * @param string $formatClass
     * @throws \Exception
     */
    private function isValidFormatClass(string $formatClass)
    {
        if (!in_array(FormatInterface::class, class_implements($formatClass))) {
            throw new \Exception('Default format class received (' . $formatClass . ') does not implement ' . FormatInterface::class);
        }
    }

    /**
     * @param string|null $formatClass
     * @return FormatInterface
     */
    private function defineFormat(string $formatClass = null)
    {
        if ($formatClass) {
            $this->isValidFormatClass($formatClass);
            return new $formatClass;
        }
        return new $this->format;
    }

    /**
     * @return \ClickhouseClient\Connector\Response
     */
    public function ping()
    {
        $response = $this->connector->performRequest(
            $this->connector->createResource()
        );
        return $response;
    }

    /**
     * @param string $sql
     * @param string|null $formatClass
     * @return \ClickhouseClient\Connector\Response
     */
    public function query(string $sql, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $response = $this->connector->performRequest(
            $this->connector->createResource(
                array_merge(
                    $this->config->getSettings(),
                    ['query' => $sql]
                )
            )
        );

        $response->setFormat($format);

        return $response;
    }

    public function queryStream(string $sql, $stream, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $ch = $this->connector->createResource(
            array_merge(
                $this->config->getSettings(),
                ['query' => $sql]
            )
        );

        //give curl the file pointer so that it can write to it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_FILE, $stream);

        $response = $this->connector->performRequest($ch);

        return $response;

    }

    public function queryClosure(string $sql, \Closure $closure, string $formatClass = null)
    {
        $format = $this->defineFormat($formatClass);

        // add format to SQL
        $sql = rtrim(trim($sql), ';');
        $sql .= ' FORMAT ' . $format->queryFormat();

        $ch = $this->connector->createResource(
            array_merge(
                $this->config->getSettings(),
                ['query' => $sql]
            )
        );

        $buffer = '';

        //give curl the file pointer so that it can write to it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $str) use ($closure, &$buffer) {

            // explode lines into array
            $lines = explode("\n", $buffer . $str);

            // get last element
            $lastLine = end($lines);

            // check if is normal string
            if (substr($lastLine, -1) !== "\n") {
                // remove from lines array
                array_pop($lines);
                // add to buffer
                $buffer = $lastLine;
{'fraction_non_alphanumeric': 0.13900955690703737, 'fraction_numerical': 0.0017376194613379669, 'mean_word_length': 2.157573910393173, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 117, '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': '4515097', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2786, 'n_words': 591}