<commit_before><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2017 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * MySQLStatement.php
 * (Potentially) prepared MySQL statement
 */

namespace SmoothPHP\Framework\Database\Statements;

use SmoothPHP\Framework\Database\MySQL;
use SmoothPHP\Framework\Database\MySQLResult;

abstract class MySQLStatement {
	protected $mysql;

	protected $query;
	protected $params;
	protected $args;

	/* @var \mysqli_stmt */
	private $stmt;

	public function __construct(MySQL $mysql, $query) {
		$this->mysql = $mysql;
		$this->params = [''];
		$this->args = [];

		$this->query = preg_replace_callback('/%(d|f|s)/', function (array $matches) {
			$this->params[0] .= $matches[1];
			$this->args[] = null;
			$this->params[] = &$this->args[count($this->args) - 1];
			return '?';
		}, $query);

		$this->verifyStmtAwake();
	}

	public function __sleep() {
		return ['mysql', 'query', 'params', 'args'];
	}

	private function verifyStmtAwake() {
		if (!isset($this->stmt)) {
			$this->mysql->__wakeup();
			$this->stmt = $this->mysql->getConnection()->prepare($this->query);
			MySQL::checkError($this->mysql->getConnection());

			if (count($this->params) > 1) {
				call_user_func_array([$this->stmt, 'bind_param'], $this->params);
				MySQL::checkError($this->stmt);
			}
		}
	}

	public function getMySQLi_stmt() {
		$this->verifyStmtAwake();
		return $this->stmt;
	}

	/**
	 * @return MySQLResult|int
	 */
	public function execute() {
		$this->verifyStmtAwake();

		$args = func_get_args();
		if (isset($args[0]) && is_array($args[0]))
			$args = $args[0];

		for ($i = 0; $i < count($args); $i++)
			$this->args[$i] = $args[$i];

		$this->stmt->execute();
		MySQL::checkError($this->stmt);

		return $this->createResult();
	}

	protected abstract function createResult();
}
<commit_msg>Added %r value to mysql queries as a 'repeat last' value<commit_after><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2017 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * MySQLStatement.php
 * (Potentially) prepared MySQL statement
 */

namespace SmoothPHP\Framework\Database\Statements;

use SmoothPHP\Framework\Database\MySQL;
use SmoothPHP\Framework\Database\MySQLException;
use SmoothPHP\Framework\Database\MySQLResult;

abstract class MySQLStatement {
	protected $mysql;

	protected $query;
	protected $params;
	protected $args;

	/* @var \mysqli_stmt */
	private $stmt;

	public function __construct(MySQL $mysql, $query) {
		$this->mysql = $mysql;
		$this->params = [''];
		$this->args = [];

		$previousMatch = null;
		$this->query = preg_replace_callback('/%(d|f|s|r)/', function (array $matches) use (&$previousMatch) {
			if ($matches[1] != 'r') {
				$this->params[0] .= $matches[1];
				$this->args[] = null;
				$previousMatch = $matches[1];
			} else {
				if ($previousMatch == null)
					throw new MySQLException('Trying to use %r (repeat) in a query with no previous variables.');
				$this->params[0] .= $previousMatch;
			}
			$this->params[] = &$this->args[count($this->args) - 1];
			return '?';
		}, $query);

		$this->verifyStmtAwake();
	}

	public function __sleep() {
		return ['mysql', 'query', 'params', 'args'];
	}

	private function verifyStmtAwake() {
		if (!isset($this->stmt)) {
			$this->mysql->__wakeup();
			$this->stmt = $this->mysql->getConnection()->prepare($this->query);
			MySQL::checkError($this->mysql->getConnection());

			if (count($this->params) > 1) {
				call_user_func_array([$this->stmt, 'bind_param'], $this->params);
				MySQL::checkError($this->stmt);
			}
		}
	}

	public function getMySQLi_stmt() {
		$this->verifyStmtAwake();
		return $this->stmt;
	}

	/**
	 * @return MySQLResult|int
	 */
	public function execute() {
		$this->verifyStmtAwake();

		$args = func_get_args();
		if (isset($args[0]) && is_array($args[0]))
			$args = $args[0];

		for ($i = 0; $i < count($args); $i++)
			$this->args[$i] = $args[$i];

		$this->stmt->execute();
		MySQL::checkError($this->stmt);

		return $this->createResult();
	}

	protected abstract function createResult();
}

{'fraction_non_alphanumeric': 0.195600756859035, 'fraction_numerical': 0.006385998107852412, 'mean_word_length': 4.064670658682635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 72, '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': '6432278', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1459, 'n_words': 291}