<commit_before><?php
/**
 * Database Proxy Class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Database\Driver\Mysqli;

class Proxy implements \Skeleton\Database\Driver\ProxyBaseInterface {
	/**
	 * @var mysqli $database The database connection to MySQL
	 * @access public
	 */
	public $database = null;

	/**
	 * @var int $query_counter The number of queries executed
	 * @access public
	 */
	public $query_counter = 0;

	/**
	 * @var array $query_log Array containing all executed queries
	 * @access public
	 */
	public $query_log = [];

	/**
	 * Connected
	 *
	 * @var bool $connected
	 * @access private
	 */
	private $connected = false;

	/**
	 * Connection string
	 *
	 * @var string $dsn
	 * @access private
	 */
	private $dsn = null;

	/**
	 * Database_Proxy constructor
	 *
	 * @access public
	 */
	public function __construct($dsn) {
		$this->dsn = $dsn;
	}

	/**
	 * Connect to the database by providing a DSN
	 *
	 * @access public
	 * @param string $dsn The database source name you want to connect to
	 * @throws Exception Throws an Exception when the Database is unavailable
	 */
	public function connect() {
		mysqli_report(MYSQLI_REPORT_OFF);
		$settings = parse_url($this->dsn);

		// If we can't even parse the DSN, don't bother
		if (!isset($settings['path']) OR !isset($settings['host']) OR !isset($settings['user'])) {
			throw new \Exception('Could not connect to database: DSN incorrect');
		}

		// We don't support connecting to UNIX sockets the traditional way
		if ($settings['host'] == 'unix(') {
			throw new \Exception('Could not connect to database: UNIX socket syntax is wrong');
		}

		$settings['path'] = substr($settings['path'], 1);
		$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path']);

		// If there is an error connecting to the database, stop doing what you're doing
		if ($this->database->connect_errno != 0) {
			throw new \Exception('Could not connect to database: ' . $this->database->connect_error);
		}

		$this->database->set_charset('utf8');
		$this->connected = true;
	}

	/**
	 * Get the DBMS we are currently connected to
	 *
	 * @access public
	 * @return string $database_type
	 */
	public function get_dbms() {
		return 'mysql';
	}

	/**
	 * Filter fields to insert/update table
	 *
	 * @access public
	 * @param string $table
	 * @param array $data
	 * @return $filtered_data
	 */
	private function filter_table_data($table, $data) {
		// if we don't have any correction to do, better go back directly
		if (\Skeleton\Database\Config::$trim_content === false &&
			\Skeleton\Database\Config::$purge_properties === false &&
			\Skeleton\Database\Config::$auto_null === false) {
			return $data;
		}

		// getting table definition
		$table_definition = $this->get_table_definition($table);

		$result = [];

		// if we don't want the purge of the properties not part of the storage
		if (\Skeleton\Database\Config::$purge_properties === false) {
			$result = $data;
		}

		// loop through all fields of the table definition and apply corrections if needed
		foreach ($table_definition as $field) {

			// trim content
			if (array_key_exists($field['Field'], $data)) {
				$value = $data[$field['Field']];

				if (\Skeleton\Database\Config::$trim_content) {
					$varchar_start = strpos($field['Type'], 'varchar');
					if ($varchar_start === 0) {
						$limit = trim(strstr(strstr($field['Type'], '('), ')', true), '(');
						$value = substr($value, 0, $limit);
					}

					if ($field['Type'] == 'tinytext' || $field['Type'] == 'tinyblob') {
						$value = substr($value, 0, 2^8);
					}
					if ($field['Type'] == 'text' || $field['Type'] == 'blob') {
						$value = substr($value, 0, 2^16);
					}
					if ($field['Type'] == 'mediumtext' || $field['Type'] == 'mediumblob') {
						$value = substr($value, 0, 2^24);
					}
					if ($field['Type'] == 'longtext' || $field['Type'] == 'longblob') {
						$value = substr($value, 0, 2^32);
					}
				}

				$result[ $field['Field'] ] = $value;
			}<commit_msg>Fix a issue with lenght of {|tiny|medium|long}text fields<commit_after><?php
/**
 * Database Proxy Class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Database\Driver\Mysqli;

class Proxy implements \Skeleton\Database\Driver\ProxyBaseInterface {
	/**
	 * @var mysqli $database The database connection to MySQL
	 * @access public
	 */
	public $database = null;

	/**
	 * @var int $query_counter The number of queries executed
	 * @access public
	 */
	public $query_counter = 0;

	/**
	 * @var array $query_log Array containing all executed queries
	 * @access public
	 */
	public $query_log = [];

	/**
	 * Connected
	 *
	 * @var bool $connected
	 * @access private
	 */
	private $connected = false;

	/**
	 * Connection string
	 *
	 * @var string $dsn
	 * @access private
	 */
	private $dsn = null;

	/**
	 * Database_Proxy constructor
	 *
	 * @access public
	 */
	public function __construct($dsn) {
		$this->dsn = $dsn;
	}

	/**
	 * Connect to the database by providing a DSN
	 *
	 * @access public
	 * @param string $dsn The database source name you want to connect to
	 * @throws Exception Throws an Exception when the Database is unavailable
	 */
	public function connect() {
		mysqli_report(MYSQLI_REPORT_OFF);
		$settings = parse_url($this->dsn);

		// If we can't even parse the DSN, don't bother
		if (!isset($settings['path']) OR !isset($settings['host']) OR !isset($settings['user'])) {
			throw new \Exception('Could not connect to database: DSN incorrect');
		}

		// We don't support connecting to UNIX sockets the traditional way
		if ($settings['host'] == 'unix(') {
			throw new \Exception('Could not connect to database: UNIX socket syntax is wrong');
		}

		$settings['path'] = substr($settings['path'], 1);
		$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path']);

		// If there is an error connecting to the database, stop doing what you're doing
		if ($this->database->connect_errno != 0) {
			throw new \Exception('Could not connect to database: ' . $this->database->connect_error);
		}

		$this->database->set_charset('utf8');
		$this->connected = true;
	}

	/**
	 * Get the DBMS we are currently connected to
	 *
	 * @access public
	 * @return string $database_type
	 */
	public function get_dbms() {
		return 'mysql';
	}

	/**
	 * Filter fields to insert/update table
	 *
	 * @access public
	 * @param string $table
	 * @param array $data
	 * @return $filtered_data
	 */
	private function filter_table_data($table, $data) {
		// if we don't have any correction to do, better go back directly
		if (\Skeleton\Database\Config::$trim_content === false &&
			\Skeleton\Database\Config::$purge_properties === false &&
			\Skeleton\Database\Config::$auto_null === false) {
			return $data;
		}

		// getting table definition
		$table_definition = $this->get_table_definition($table);

		$result = [];

		// if we don't want the purge of the properties not part of the storage
		if (\Skeleton\Database\Config::$purge_properties === false) {
			$result = $data;
		}

		// loop through all fields of the table definition and apply corrections if needed
		foreach ($table_definition as $field) {

			// trim content
			if (array_key_exists($field['Field'], $data)) {
				$value = $data[$field['Field']];

				if (\Skeleton\Database\Config::$trim_content) {
					$varchar_start = strpos($field['Type'], 'varchar');
					if ($varchar_start === 0) {
						$limit = trim(strstr(strstr($field['Type'], '('), ')', true), '(');
						$value = substr($value, 0, $limit);
					}

					if ($field['Type'] == 'tinytext' || $field['Type'] == 'tinyblob') {
						$value = substr($value, 0, 256);
					}
					if ($field['Type'] == 'text' || $field['Type'] == 'blob') {
						$value = substr($value, 0, 65536);
					}
					if ($field['Type'] == 'mediumtext' || $field['Type'] == 'mediumblob') {
						$value = substr($value, 0, 16777216);
					}
					if ($field['Type'] == 'longtext' || $field['Type'] == 'longblob') {
						$value = substr($value, 0, 4294967296);
					}
				}

				$result[ $field['Field'] ] = $value;
			}
{'fraction_non_alphanumeric': 0.16607945571619487, 'fraction_numerical': 0.006925039484874256, 'mean_word_length': 3.4642082429501087, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 43, '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': '4930135', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2532, 'n_words': 747}